From ceb252986e2f836f36d2f2d5a54c87ebec607120 Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Mon, 25 May 2020 12:52:15 -0500 Subject: [PATCH] Update captcha job --- shard.yml | 2 +- src/invidious/helpers/jobs.cr | 14 +++++++++----- src/invidious/helpers/utils.cr | 9 ++++++--- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/shard.yml b/shard.yml index f3668ff4..59baf650 100644 --- a/shard.yml +++ b/shard.yml @@ -26,7 +26,7 @@ dependencies: version: ~> 0.1.2 lsquic: github: omarroth/lsquic.cr - version: ~> 0.1.9 + branch: dev crystal: 0.34.0 diff --git a/src/invidious/helpers/jobs.cr b/src/invidious/helpers/jobs.cr index dc25d823..c6e0ef42 100644 --- a/src/invidious/helpers/jobs.cr +++ b/src/invidious/helpers/jobs.cr @@ -295,7 +295,9 @@ def bypass_captcha(captcha_key, logger) inputs[node["name"]] = node["value"] end - response = JSON.parse(HTTP::Client.post("https://api.anti-captcha.com/createTask", body: { + captcha_client = HTTPClient.new(URI.parse("https://api.anti-captcha.com")) + captcha_client.family = CONFIG.force_resolve || Socket::Family::INET + response = JSON.parse(captcha_client.post("/createTask", body: { "clientKey" => CONFIG.captcha_key, "task" => { "type" => "NoCaptchaTaskProxyless", @@ -310,7 +312,7 @@ def bypass_captcha(captcha_key, logger) loop do sleep 10.seconds - response = JSON.parse(HTTP::Client.post("https://api.anti-captcha.com/getTaskResult", body: { + response = JSON.parse(captcha_client.post("/getTaskResult", body: { "clientKey" => CONFIG.captcha_key, "taskId" => task_id, }.to_json).body) @@ -323,9 +325,11 @@ def bypass_captcha(captcha_key, logger) end inputs["g-recaptcha-response"] = response["solution"]["gRecaptchaResponse"].as_s - client = HTTPClient.new(location) - client.family = CONFIG.force_resolve || Socket::Family::INET - response = client.post(location.full_path, form: inputs) + headers["content-type"] = "application/x-www-form-urlencoded" + headers["origin"] = "https://www.google.com" + headers["user-agent"] = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36" + + response = YT_POOL.client &.post("/sorry/index", headers: headers, form: inputs) headers = HTTP::Headers{ "Cookie" => URI.parse(response.headers["location"]).query_params["google_abuse"].split(";")[0], } diff --git a/src/invidious/helpers/utils.cr b/src/invidious/helpers/utils.cr index 79a69cf9..1fff206d 100644 --- a/src/invidious/helpers/utils.cr +++ b/src/invidious/helpers/utils.cr @@ -2,13 +2,16 @@ require "lsquic" require "pool/connection" def add_yt_headers(request) - request.headers["x-youtube-client-name"] ||= "1" - request.headers["x-youtube-client-version"] ||= "1.20180719" request.headers["user-agent"] ||= "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36" request.headers["accept-charset"] ||= "ISO-8859-1,utf-8;q=0.7,*;q=0.7" request.headers["accept"] ||= "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" request.headers["accept-language"] ||= "en-us,en;q=0.5" - request.headers["cookie"] = "#{(CONFIG.cookies.map { |c| "#{c.name}=#{c.value}" }).join("; ")}; #{request.headers["cookie"]?}" + return if request.resource.starts_with? "/sorry/index" + request.headers["x-youtube-client-name"] ||= "1" + request.headers["x-youtube-client-version"] ||= "1.20180719" + if !CONFIG.cookies.empty? + request.headers["cookie"] = "#{(CONFIG.cookies.map { |c| "#{c.name}=#{c.value}" }).join("; ")}; #{request.headers["cookie"]?}" + end end struct QUICPool