Remove legacy proxy code

This commit is contained in:
syeopite
2024-04-07 11:08:12 -07:00
parent 08390acd0c
commit 990931ff67
5 changed files with 20 additions and 373 deletions

View File

@@ -24,25 +24,20 @@ struct YoutubeConnectionPool
@pool = build_pool()
end
def client(region = nil, &block)
if region
conn = make_client(url, region, force_resolve = true)
def client(&block)
conn = pool.checkout
begin
response = yield conn
else
conn = pool.checkout
begin
response = yield conn
rescue ex
conn.close
conn = HTTP::Client.new(url)
rescue ex
conn.close
conn = HTTP::Client.new(url)
conn.family = CONFIG.force_resolve
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"
response = yield conn
ensure
pool.release(conn)
end
conn.family = CONFIG.force_resolve
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"
response = yield conn
ensure
pool.release(conn)
end
response
@@ -60,9 +55,9 @@ struct YoutubeConnectionPool
end
def make_client(url : URI, region = nil, force_resolve : Bool = false)
client = HTTPClient.new(url, OpenSSL::SSL::Context::Client.insecure)
client = HTTP::Client.new(url)
# Some services do not support IPv6.
# Force the usage of a specific configured IP Family
if force_resolve
client.family = CONFIG.force_resolve
end
@@ -71,17 +66,6 @@ def make_client(url : URI, region = nil, force_resolve : Bool = false)
client.read_timeout = 10.seconds
client.connect_timeout = 10.seconds
if region
PROXY_LIST[region]?.try &.sample(40).each do |proxy|
begin
proxy = HTTPProxy.new(proxy_host: proxy[:ip], proxy_port: proxy[:port])
client.set_proxy(proxy)
break
rescue ex
end
end
end
return client
end

File diff suppressed because one or more lines are too long

View File

@@ -188,10 +188,6 @@ module YoutubeAPI
# conf_2 = ClientConfig.new(client_type: ClientType::Android)
# YoutubeAPI::player(video_id: "dQw4w9WgXcQ", client_config: conf_2)
#
# # Proxy request through russian proxies
# conf_3 = ClientConfig.new(proxy_region: "RU")
# YoutubeAPI::next({video_id: "dQw4w9WgXcQ"}, client_config: conf_3)
# ```
#
struct ClientConfig
# Type of client to emulate.
@@ -202,16 +198,11 @@ module YoutubeAPI
# (this is passed as the `gl` parameter).
property region : String | Nil
# ISO code of country where the proxy is located.
# Used in case of geo-restricted videos.
property proxy_region : String | Nil
# Initialization function
def initialize(
*,
@client_type = ClientType::Web,
@region = "US",
@proxy_region = nil
@region = "US"
)
end
@@ -271,9 +262,8 @@ module YoutubeAPI
# Convert to string, for logging purposes
def to_s
return {
client_type: self.name,
region: @region,
proxy_region: @proxy_region,
client_type: self.name,
region: @region,
}.to_s
end
end
@@ -620,7 +610,7 @@ module YoutubeAPI
LOGGER.trace("YoutubeAPI: POST data: #{data}")
# Send the POST request
body = YT_POOL.client(client_config.proxy_region) do |client|
body = YT_POOL.client() do |client|
client.post(url, headers: headers, body: data.to_json) do |response|
self._decompress(response.body_io, response.headers["Content-Encoding"]?)
end