Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove legacy proxy code #4570

Merged
merged 1 commit into from Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 0 additions & 11 deletions src/invidious/videos.cr
Expand Up @@ -394,17 +394,6 @@ def fetch_video(id, region)
.dig?("microformat", "playerMicroformatRenderer", "availableCountries")
.try &.as_a.map &.as_s || [] of String

# Check for region-blocks
if info["reason"]?.try &.as_s.includes?("your country")
bypass_regions = PROXY_LIST.keys & allowed_regions
if !bypass_regions.empty?
region = bypass_regions[rand(bypass_regions.size)]
region_info = extract_video_info(video_id: id, proxy_region: region)
region_info["region"] = JSON::Any.new(region) if region
info = region_info if !region_info["reason"]?
end
end

if reason = info["reason"]?
if reason == "Video unavailable"
raise NotFoundException.new(reason.as_s || "")
Expand Down
4 changes: 2 additions & 2 deletions src/invidious/videos/parser.cr
Expand Up @@ -50,9 +50,9 @@ def parse_related_video(related : JSON::Any) : Hash(String, JSON::Any)?
}
end

def extract_video_info(video_id : String, proxy_region : String? = nil)
def extract_video_info(video_id : String)
# Init client config for the API
client_config = YoutubeAPI::ClientConfig.new(proxy_region: proxy_region)
client_config = YoutubeAPI::ClientConfig.new

# Fetch data from the player endpoint
player_response = YoutubeAPI.player(video_id: video_id, params: "", client_config: client_config)
Expand Down
44 changes: 14 additions & 30 deletions src/invidious/yt_backend/connection_pool.cr
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down
316 changes: 0 additions & 316 deletions src/invidious/yt_backend/proxy.cr

This file was deleted.

18 changes: 4 additions & 14 deletions src/invidious/yt_backend/youtube_api.cr
Expand Up @@ -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.
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down