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