Skip to content

Commit

Permalink
Add support for /sorry/index CAPTCHA
Browse files Browse the repository at this point in the history
  • Loading branch information
omarroth committed Dec 4, 2019
1 parent 062867a commit 8236036
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/invidious/helpers/jobs.cr
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,60 @@ def bypass_captcha(captcha_key, logger)
response = YT_POOL.client &.post("/das_captcha", headers, form: inputs)

yield response.cookies.select { |cookie| cookie.name != "PREF" }
elsif response.headers["Location"]?.try &.includes?("/sorry/index")
location = response.headers["Location"].try { |u| URI.parse(u) }
client = QUIC::Client.new(location.host.not_nil!)
response = client.get(location.full_path)

html = XML.parse_html(response.body)
form = html.xpath_node(%(//form[@action="index"])).not_nil!
site_key = form.xpath_node(%(.//div[@class="g-recaptcha"])).try &.["data-sitekey"]

inputs = {} of String => String
form.xpath_nodes(%(.//input[@name])).map do |node|
inputs[node["name"]] = node["value"]
end

response = JSON.parse(HTTP::Client.post("https://api.anti-captcha.com/createTask", body: {
"clientKey" => CONFIG.captcha_key,
"task" => {
"type" => "NoCaptchaTaskProxyless",
"websiteURL" => location.to_s,
"websiteKey" => site_key,
},
}.to_json).body)

if response["error"]?
raise response["error"].as_s
end

task_id = response["taskId"].as_i

loop do
sleep 10.seconds

response = JSON.parse(HTTP::Client.post("https://api.anti-captcha.com/getTaskResult", body: {
"clientKey" => CONFIG.captcha_key,
"taskId" => task_id,
}.to_json).body)

if response["status"]?.try &.== "ready"
break
elsif response["errorId"]?.try &.as_i != 0
raise response["errorDescription"].as_s
end
end

inputs["g-recaptcha-response"] = response["solution"]["gRecaptchaResponse"].as_s
client.close
client = QUIC::Client.new("www.google.com")
response = client.post(location.full_path, form: inputs)
headers = HTTP::Headers{
"Cookie" => URI.parse(response.headers["location"]).query_params["google_abuse"].split(";")[0],
}
cookies = HTTP::Cookies.from_headers(headers)

yield cookies
end
rescue ex
logger.puts("Exception: #{ex.message}")
Expand Down

0 comments on commit 8236036

Please sign in to comment.