mirror of
https://github.com/iv-org/invidious.git
synced 2024-11-22 13:37:24 +00:00
Refactor geo-bypass
This commit is contained in:
parent
6b12f11e10
commit
aeaeacbf8d
@ -694,7 +694,9 @@ post "/login" do |env|
|
||||
|
||||
challenge_req = {
|
||||
user_hash, nil, 1, nil,
|
||||
{1, nil, nil, nil, {password, nil, true}},
|
||||
{1, nil, nil, nil,
|
||||
{password, nil, true},
|
||||
},
|
||||
{nil, nil,
|
||||
{2, 1, nil, 1, "https://accounts.google.com/ServiceLogin?passive=1209600&continue=https%3A%2F%2Faccounts.google.com%2FManageAccount&followup=https%3A%2F%2Faccounts.google.com%2FManageAccount", nil, [] of String, 4, [] of String},
|
||||
1,
|
||||
@ -3342,45 +3344,24 @@ get "/videoplayback" do |env|
|
||||
host = "https://r#{fvip}---#{mn}.googlevideo.com"
|
||||
url = "/videoplayback?#{query_params.to_s}"
|
||||
|
||||
if query_params["region"]?
|
||||
client = make_client(URI.parse(host))
|
||||
response = HTTP::Client::Response.new(status_code: 403)
|
||||
|
||||
if !proxies[query_params["region"]]?
|
||||
halt env, status_code: 403
|
||||
end
|
||||
|
||||
proxies[query_params["region"]].each do |proxy|
|
||||
begin
|
||||
client = HTTPClient.new(URI.parse(host))
|
||||
client.read_timeout = 10.seconds
|
||||
client.connect_timeout = 10.seconds
|
||||
|
||||
proxy = HTTPProxy.new(proxy_host: proxy[:ip], proxy_port: proxy[:port])
|
||||
client.set_proxy(proxy)
|
||||
|
||||
response = client.head(url)
|
||||
if response.status_code == 200
|
||||
# For whatever reason the proxy needs to be set again
|
||||
client.set_proxy(proxy)
|
||||
break
|
||||
end
|
||||
rescue ex
|
||||
end
|
||||
end
|
||||
else
|
||||
client = make_client(URI.parse(host))
|
||||
response = client.head(url)
|
||||
end
|
||||
|
||||
if response.status_code != 200
|
||||
halt env, status_code: 403
|
||||
end
|
||||
region = query_params["region"]?
|
||||
client = make_client(URI.parse(host), proxies, region)
|
||||
response = client.head(url)
|
||||
|
||||
if response.headers["Location"]?
|
||||
url = URI.parse(response.headers["Location"])
|
||||
env.response.headers["Access-Control-Allow-Origin"] = "*"
|
||||
next env.redirect url.full_path
|
||||
|
||||
url = url.full_path
|
||||
if region
|
||||
url += "®ion=#{region}"
|
||||
end
|
||||
|
||||
next env.redirect url
|
||||
end
|
||||
|
||||
if response.status_code >= 400
|
||||
halt env, status_code: 403
|
||||
end
|
||||
|
||||
headers = env.request.headers
|
||||
@ -3389,6 +3370,7 @@ get "/videoplayback" do |env|
|
||||
headers.delete("User-Agent")
|
||||
headers.delete("Referer")
|
||||
|
||||
client = make_client(URI.parse(host), proxies, region)
|
||||
client.get(url, headers) do |response|
|
||||
env.response.status_code = response.status_code
|
||||
|
||||
|
@ -70,34 +70,18 @@ def fetch_youtube_comments(id, continuation, proxies, format)
|
||||
if body.match(/<meta itemprop="regionsAllowed" content="">/)
|
||||
bypass_channel = Channel({String, HTTPClient, HTTP::Headers} | Nil).new
|
||||
|
||||
proxies.each do |region, list|
|
||||
proxies.each do |proxy_region, list|
|
||||
spawn do
|
||||
proxy_html = %(<meta itemprop="regionsAllowed" content="">)
|
||||
proxy_client = make_client(YT_URL, proxies, proxy_region)
|
||||
|
||||
list.each do |proxy|
|
||||
begin
|
||||
proxy_client = HTTPClient.new(YT_URL)
|
||||
proxy_client.read_timeout = 10.seconds
|
||||
proxy_client.connect_timeout = 10.seconds
|
||||
response = proxy_client.get("/watch?v=#{id}&gl=US&hl=en&disable_polymer=1&has_verified=1&bpctr=9999999999")
|
||||
proxy_headers = HTTP::Headers.new
|
||||
proxy_headers["Cookie"] = response.cookies.add_request_headers(headers)["cookie"]
|
||||
proxy_html = response.body
|
||||
|
||||
proxy = HTTPProxy.new(proxy_host: proxy[:ip], proxy_port: proxy[:port])
|
||||
proxy_client.set_proxy(proxy)
|
||||
|
||||
response = proxy_client.get("/watch?v=#{id}&gl=US&hl=en&disable_polymer=1&has_verified=1&bpctr=9999999999")
|
||||
proxy_headers = HTTP::Headers.new
|
||||
proxy_headers["cookie"] = response.cookies.add_request_headers(headers)["cookie"]
|
||||
proxy_html = response.body
|
||||
|
||||
if !proxy_html.match(/<meta itemprop="regionsAllowed" content="">/)
|
||||
bypass_channel.send({proxy_html, proxy_client, proxy_headers})
|
||||
break
|
||||
end
|
||||
rescue ex
|
||||
end
|
||||
end
|
||||
|
||||
# If none of the proxies we tried returned a valid response
|
||||
if proxy_html.match(/<meta itemprop="regionsAllowed" content="">/)
|
||||
if !proxy_html.match(/<meta itemprop="regionsAllowed" content="">/)
|
||||
bypass_channel.send({proxy_html, proxy_client, proxy_headers})
|
||||
else
|
||||
bypass_channel.send(nil)
|
||||
end
|
||||
end
|
||||
@ -106,12 +90,12 @@ def fetch_youtube_comments(id, continuation, proxies, format)
|
||||
proxies.size.times do
|
||||
response = bypass_channel.receive
|
||||
if response
|
||||
session_token = response[0].match(/'XSRF_TOKEN': "(?<session_token>[A-Za-z0-9\_\-\=]+)"/).not_nil!["session_token"]
|
||||
itct = response[0].match(/itct=(?<itct>[^"]+)"/).not_nil!["itct"]
|
||||
ctoken = response[0].match(/'COMMENTS_TOKEN': "(?<ctoken>[^"]+)"/)
|
||||
html, client, headers = response
|
||||
|
||||
session_token = html.match(/'XSRF_TOKEN': "(?<session_token>[A-Za-z0-9\_\-\=]+)"/).not_nil!["session_token"]
|
||||
itct = html.match(/itct=(?<itct>[^"]+)"/).not_nil!["itct"]
|
||||
ctoken = html.match(/'COMMENTS_TOKEN': "(?<ctoken>[^"]+)"/)
|
||||
|
||||
client = response[1]
|
||||
headers = response[2]
|
||||
break
|
||||
end
|
||||
end
|
||||
|
@ -578,47 +578,26 @@ def fetch_video(id, proxies, region)
|
||||
info = info_channel.receive
|
||||
|
||||
if info["reason"]? && info["reason"].includes? "your country"
|
||||
bypass_channel = Channel(HTTPProxy | Nil).new
|
||||
bypass_channel = Channel({HTTPClient, String} | Nil).new
|
||||
|
||||
proxies.each do |region, list|
|
||||
proxies.each do |proxy_region, list|
|
||||
spawn do
|
||||
info = HTTP::Params.new({
|
||||
"reason" => [info["reason"]],
|
||||
})
|
||||
client = make_client(YT_URL, proxies, proxy_region)
|
||||
|
||||
list.each do |proxy|
|
||||
begin
|
||||
client = HTTPClient.new(YT_URL)
|
||||
client.read_timeout = 10.seconds
|
||||
client.connect_timeout = 10.seconds
|
||||
|
||||
proxy = HTTPProxy.new(proxy_host: proxy[:ip], proxy_port: proxy[:port])
|
||||
client.set_proxy(proxy)
|
||||
|
||||
info = HTTP::Params.parse(client.get("/get_video_info?video_id=#{id}&ps=default&eurl=&gl=US&hl=en&disable_polymer=1").body)
|
||||
if !info["reason"]?
|
||||
bypass_channel.send(proxy)
|
||||
break
|
||||
end
|
||||
rescue ex
|
||||
end
|
||||
end
|
||||
|
||||
# If none of the proxies we tried returned a valid response
|
||||
if info["reason"]?
|
||||
info = HTTP::Params.parse(client.get("/get_video_info?video_id=#{id}&ps=default&eurl=&gl=US&hl=en&disable_polymer=1").body)
|
||||
if !info["reason"]?
|
||||
bypass_channel.send({client, proxy_region})
|
||||
else
|
||||
bypass_channel.send(nil)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
proxies.size.times do
|
||||
proxy = bypass_channel.receive
|
||||
if proxy
|
||||
response = bypass_channel.receive
|
||||
if response
|
||||
begin
|
||||
client = HTTPClient.new(YT_URL)
|
||||
client.read_timeout = 10.seconds
|
||||
client.connect_timeout = 10.seconds
|
||||
client.set_proxy(proxy)
|
||||
client, proxy_region = response
|
||||
|
||||
html = XML.parse_html(client.get("/watch?v=#{id}&gl=US&hl=en&disable_polymer=1&has_verified=1&bpctr=9999999999").body)
|
||||
info = HTTP::Params.parse(client.get("/get_video_info?video_id=#{id}&el=detailpage&ps=default&eurl=&gl=US&hl=en&disable_polymer=1").body)
|
||||
@ -627,11 +606,7 @@ def fetch_video(id, proxies, region)
|
||||
info = HTTP::Params.parse(client.get("/get_video_info?video_id=#{id}&ps=default&eurl=&gl=US&hl=en&disable_polymer=1").body)
|
||||
end
|
||||
|
||||
proxy = {ip: proxy.proxy_host, port: proxy.proxy_port}
|
||||
region_proxies = proxies.select { |region, list| list.includes? proxy }
|
||||
if !region_proxies.empty?
|
||||
info["region"] = region_proxies.keys[0]
|
||||
end
|
||||
info["region"] = proxy_region
|
||||
|
||||
break
|
||||
rescue ex
|
||||
|
Loading…
Reference in New Issue
Block a user