mirror of
https://github.com/iv-org/invidious.git
synced 2024-11-25 23:17:23 +00:00
Automatically initialize proxy via stdlib override
This commit is contained in:
parent
eb8fcc9e88
commit
3b471ae964
@ -18,6 +18,40 @@ end
|
|||||||
class HTTP::Client
|
class HTTP::Client
|
||||||
property family : Socket::Family = Socket::Family::UNSPEC
|
property family : Socket::Family = Socket::Family::UNSPEC
|
||||||
|
|
||||||
|
# Override stdlib to automatically initialize proxy if configured
|
||||||
|
#
|
||||||
|
# Accurate as of crystal 1.10.1
|
||||||
|
|
||||||
|
def initialize(@host : String, port = nil, tls : TLSContext = nil)
|
||||||
|
check_host_only(@host)
|
||||||
|
|
||||||
|
{% if flag?(:without_openssl) %}
|
||||||
|
if tls
|
||||||
|
raise "HTTP::Client TLS is disabled because `-D without_openssl` was passed at compile time"
|
||||||
|
end
|
||||||
|
@tls = nil
|
||||||
|
{% else %}
|
||||||
|
@tls = case tls
|
||||||
|
when true
|
||||||
|
OpenSSL::SSL::Context::Client.new
|
||||||
|
when OpenSSL::SSL::Context::Client
|
||||||
|
tls
|
||||||
|
when false, nil
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
{% end %}
|
||||||
|
|
||||||
|
@port = (port || (@tls ? 443 : 80)).to_i
|
||||||
|
|
||||||
|
self.proxy = make_configured_http_proxy_client() if CONFIG.http_proxy
|
||||||
|
end
|
||||||
|
|
||||||
|
def initialize(@io : IO, @host = "", @port = 80)
|
||||||
|
@reconnect = false
|
||||||
|
|
||||||
|
self.proxy = make_configured_http_proxy_client() if CONFIG.http_proxy
|
||||||
|
end
|
||||||
|
|
||||||
private def io
|
private def io
|
||||||
io = @io
|
io = @io
|
||||||
return io if io
|
return io if io
|
||||||
|
@ -26,13 +26,15 @@ struct YoutubeConnectionPool
|
|||||||
|
|
||||||
def client(&block)
|
def client(&block)
|
||||||
conn = pool.checkout
|
conn = pool.checkout
|
||||||
|
# Proxy needs to be reinstated every time we get a client from the pool
|
||||||
conn.proxy = make_configured_http_proxy_client() if CONFIG.http_proxy
|
conn.proxy = make_configured_http_proxy_client() if CONFIG.http_proxy
|
||||||
|
|
||||||
begin
|
begin
|
||||||
response = yield conn
|
response = yield conn
|
||||||
rescue ex
|
rescue ex
|
||||||
conn.close
|
conn.close
|
||||||
conn = HTTP::Client.new(url)
|
|
||||||
|
|
||||||
|
conn = HTTP::Client.new(url)
|
||||||
conn.proxy = make_configured_http_proxy_client() if CONFIG.http_proxy
|
conn.proxy = make_configured_http_proxy_client() if CONFIG.http_proxy
|
||||||
conn.family = CONFIG.force_resolve
|
conn.family = CONFIG.force_resolve
|
||||||
conn.family = Socket::Family::INET if conn.family == Socket::Family::UNSPEC
|
conn.family = Socket::Family::INET if conn.family == Socket::Family::UNSPEC
|
||||||
@ -48,7 +50,6 @@ struct YoutubeConnectionPool
|
|||||||
private def build_pool
|
private def build_pool
|
||||||
DB::Pool(HTTP::Client).new(initial_pool_size: 0, max_pool_size: capacity, max_idle_pool_size: capacity, checkout_timeout: timeout) do
|
DB::Pool(HTTP::Client).new(initial_pool_size: 0, max_pool_size: capacity, max_idle_pool_size: capacity, checkout_timeout: timeout) do
|
||||||
conn = HTTP::Client.new(url)
|
conn = HTTP::Client.new(url)
|
||||||
conn.proxy = make_configured_http_proxy_client() if CONFIG.http_proxy
|
|
||||||
conn.family = CONFIG.force_resolve
|
conn.family = CONFIG.force_resolve
|
||||||
conn.family = Socket::Family::INET if conn.family == Socket::Family::UNSPEC
|
conn.family = Socket::Family::INET if conn.family == Socket::Family::UNSPEC
|
||||||
conn.before_request { |r| add_yt_headers(r) } if url.host == "www.youtube.com"
|
conn.before_request { |r| add_yt_headers(r) } if url.host == "www.youtube.com"
|
||||||
@ -69,8 +70,6 @@ def make_client(url : URI, region = nil, force_resolve : Bool = false)
|
|||||||
client.read_timeout = 10.seconds
|
client.read_timeout = 10.seconds
|
||||||
client.connect_timeout = 10.seconds
|
client.connect_timeout = 10.seconds
|
||||||
|
|
||||||
client.proxy = make_configured_http_proxy_client() if CONFIG.http_proxy
|
|
||||||
|
|
||||||
return client
|
return client
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user