diff options
| author | Omar Roth <omarroth@protonmail.com> | 2019-12-03 19:14:11 -0500 |
|---|---|---|
| committer | Omar Roth <omarroth@protonmail.com> | 2019-12-03 19:14:11 -0500 |
| commit | 823603650f7f45e7cdc27f0beb7d6e346efb6ec5 (patch) | |
| tree | 2cae7694a22ec5090103a1ed9e0e1f9f3eed8b35 | |
| parent | 062867a38dc111fe981e2c65239b557191daa7fa (diff) | |
| download | invidious-823603650f7f45e7cdc27f0beb7d6e346efb6ec5.tar.gz invidious-823603650f7f45e7cdc27f0beb7d6e346efb6ec5.tar.bz2 invidious-823603650f7f45e7cdc27f0beb7d6e346efb6ec5.zip | |
Add support for /sorry/index CAPTCHA
| -rw-r--r-- | src/invidious/helpers/jobs.cr | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/invidious/helpers/jobs.cr b/src/invidious/helpers/jobs.cr index 5838b5b3..f368d6df 100644 --- a/src/invidious/helpers/jobs.cr +++ b/src/invidious/helpers/jobs.cr @@ -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}") |
