Add option to disable force_resolve in make_client

Some websites such as archive.org and textcaptcha.com
does not support IPv6 and as such requests fail when Invidious requests
with IPv6 to those services.
This commit is contained in:
syeopite 2023-12-15 11:52:48 -08:00
parent 9e8baa3539
commit 2fea6a8499
No known key found for this signature in database
GPG Key ID: A73C186DA3955A1A
5 changed files with 14 additions and 9 deletions

View File

@ -2,7 +2,7 @@ module Invidious::Comments
extend self
def fetch_reddit(id, sort_by = "confidence")
client = make_client(REDDIT_URL)
client = make_client(REDDIT_URL, force_resolve: false)
headers = HTTP::Headers{"User-Agent" => "web:invidious:v#{CURRENT_VERSION} (by github.com/iv-org/invidious)"}
# TODO: Use something like #479 for a static list of instances to use here

View File

@ -302,7 +302,7 @@ def subscribe_pubsub(topic, key)
"hub.secret" => key.to_s,
}
return make_client(PUBSUB_URL, &.post("/subscribe", form: body))
return make_client(PUBSUB_URL, force_resolve: false, &.post("/subscribe", form: body))
end
def parse_range(range)

View File

@ -256,13 +256,13 @@ module Invidious::Routes::API::V1::Videos
file = URI.encode_www_form("#{id[0, 3]}/#{id}.xml")
location = make_client(ARCHIVE_URL, &.get("/download/youtubeannotations_#{index}/#{id[0, 2]}.tar/#{file}"))
location = make_client(ARCHIVE_URL, force_resolve: false, &.get("/download/youtubeannotations_#{index}/#{id[0, 2]}.tar/#{file}"))
if !location.headers["Location"]?
env.response.status_code = location.status_code
end
response = make_client(URI.parse(location.headers["Location"]), &.get(location.headers["Location"]))
response = make_client(URI.parse(location.headers["Location"]), force_resolve: false, &.get(location.headers["Location"]))
if response.body.empty?
haltf env, 404

View File

@ -62,7 +62,7 @@ struct Invidious::User
end
def generate_text(key)
response = make_client(TEXTCAPTCHA_URL, &.get("/github.com/iv.org/invidious.json").body)
response = make_client(TEXTCAPTCHA_URL, force_resolve: false, &.get("/github.com/iv.org/invidious.json").body)
response = JSON.parse(response)
tokens = response["a"].as_a.map do |answer|

View File

@ -59,9 +59,14 @@ struct YoutubeConnectionPool
end
end
def make_client(url : URI, region = nil)
def make_client(url : URI, region = nil, force_resolve : Bool = true)
client = HTTPClient.new(url, OpenSSL::SSL::Context::Client.insecure)
client.family = CONFIG.force_resolve
# Some services do not support IPv6.
if force_resolve
client.family = CONFIG.force_resolve
end
client.before_request { |r| add_yt_headers(r) } if url.host == "www.youtube.com"
client.read_timeout = 10.seconds
client.connect_timeout = 10.seconds
@ -80,8 +85,8 @@ def make_client(url : URI, region = nil)
return client
end
def make_client(url : URI, region = nil, &block)
client = make_client(url, region)
def make_client(url : URI, region = nil, force_resolve : Bool = true, &block)
client = make_client(url, region, force_resolve)
begin
yield client
ensure