Skip to content

Commit

Permalink
Add parameter to disable force_resolve in make_client (#4335)
Browse files Browse the repository at this point in the history
* 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.

* Reenable force_resolve on pubsub subcribe request

* Make force_resolve false by default in make_client

* Remove missed explicit force_resolve=false
  • Loading branch information
syeopite committed Jan 10, 2024
1 parent b16f66e commit 1c0b420
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/invidious/routes/video_playback.cr
Expand Up @@ -42,7 +42,7 @@ module Invidious::Routes::VideoPlayback
headers["Range"] = "bytes=#{range_for_head}"
end

client = make_client(URI.parse(host), region)
client = make_client(URI.parse(host), region, force_resolve = true)
response = HTTP::Client::Response.new(500)
error = ""
5.times do
Expand All @@ -57,7 +57,7 @@ module Invidious::Routes::VideoPlayback
if new_host != host
host = new_host
client.close
client = make_client(URI.parse(new_host), region)
client = make_client(URI.parse(new_host), region, force_resolve = true)
end

url = "#{location.request_target}&host=#{location.host}#{region ? "&region=#{region}" : ""}"
Expand All @@ -71,7 +71,7 @@ module Invidious::Routes::VideoPlayback
fvip = "3"

host = "https://r#{fvip}---#{mn}.googlevideo.com"
client = make_client(URI.parse(host), region)
client = make_client(URI.parse(host), region, force_resolve = true)
rescue ex
error = ex.message
end
Expand Down Expand Up @@ -196,7 +196,7 @@ module Invidious::Routes::VideoPlayback
break
else
client.close
client = make_client(URI.parse(host), region)
client = make_client(URI.parse(host), region, force_resolve = true)
end
end

Expand Down
15 changes: 10 additions & 5 deletions src/invidious/yt_backend/connection_pool.cr
Expand Up @@ -26,7 +26,7 @@ struct YoutubeConnectionPool

def client(region = nil, &block)
if region
conn = make_client(url, region)
conn = make_client(url, region, force_resolve = true)
response = yield conn
else
conn = pool.checkout
Expand Down Expand Up @@ -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 = false)
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
Expand All @@ -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 = false, &block)
client = make_client(url, region, force_resolve)
begin
yield client
ensure
Expand Down

0 comments on commit 1c0b420

Please sign in to comment.