Refactor geo-bypass

This commit is contained in:
Omar Roth 2018-11-20 10:07:50 -06:00
parent 6b12f11e10
commit aeaeacbf8d
3 changed files with 43 additions and 102 deletions

View File

@ -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) response = client.head(url)
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
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 += "&region=#{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

View File

@ -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| response = proxy_client.get("/watch?v=#{id}&gl=US&hl=en&disable_polymer=1&has_verified=1&bpctr=9999999999")
begin proxy_headers = HTTP::Headers.new
proxy_client = HTTPClient.new(YT_URL) proxy_headers["Cookie"] = response.cookies.add_request_headers(headers)["cookie"]
proxy_client.read_timeout = 10.seconds proxy_html = response.body
proxy_client.connect_timeout = 10.seconds
proxy = HTTPProxy.new(proxy_host: proxy[:ip], proxy_port: proxy[:port]) if !proxy_html.match(/<meta itemprop="regionsAllowed" content="">/)
proxy_client.set_proxy(proxy) bypass_channel.send({proxy_html, proxy_client, proxy_headers})
else
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="">/)
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

View File

@ -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| info = HTTP::Params.parse(client.get("/get_video_info?video_id=#{id}&ps=default&eurl=&gl=US&hl=en&disable_polymer=1").body)
begin if !info["reason"]?
client = HTTPClient.new(YT_URL) bypass_channel.send({client, proxy_region})
client.read_timeout = 10.seconds else
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"]?
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