diff --git a/extra/4play/config.json b/extra/4play/config.json new file mode 100644 index 0000000..a39c2cd --- /dev/null +++ b/extra/4play/config.json @@ -0,0 +1,6 @@ +{ + "port_4play": 3030, + "port_webserver": 3000, + "timeout": 30000, + "password": "cnc" +} diff --git a/extra/4play/page-render.js b/extra/4play/page-render.js index af7757d..366442b 100644 --- a/extra/4play/page-render.js +++ b/extra/4play/page-render.js @@ -1,17 +1,13 @@ const fplay = require("@lawlers/4play"); const http = require("http"); +const fs = require("fs"); -// 4play settings -var port = 3030; -var timeout = 30000; -var password = "cnc"; - -// ================================================== +// import 4play settings +var config = JSON.parse(fs.readFileSync("config.json")); // webserver var global_ws = null; -const webserver_port = 3000; var callback_q = new Map(); @@ -26,6 +22,11 @@ const server = http.createServer(async function(req, res){ const content_match = parsed.searchParams.get("content_match"); const fail_url_match = parsed.searchParams.get("fail_url_match"); const fail_content_match = parsed.searchParams.get("fail_content_match"); + var return_cookies = parsed.searchParams.get("return_cookies"); + const js_eval_url_match = parsed.searchParams.get("js_eval_url_match"); + const js_eval_content_match = parsed.searchParams.get("js_eval_content_match"); + const js_eval = parsed.searchParams.get("js_eval"); + var js_eval_isolated = parsed.searchParams.get("js_eval_isolated"); var container = parsed.searchParams.get("container"); @@ -46,7 +47,7 @@ const server = http.createServer(async function(req, res){ if( pw === null || - pw != password + pw != config.password ){ errors.push("pw"); @@ -68,6 +69,75 @@ const server = http.createServer(async function(req, res){ return; } + if( + js_eval_url_match === null && + js_eval_content_match === null && + js_eval !== null + ){ + + res.writeHead( + 400, + { + "Content-Type": "text/plain" + } + ); + + res.end("Missing &js_eval_url_match or &js_eval_content_match directive while &js_eval is present"); + return; + } + + if(js_eval !== null){ + + switch(js_eval_isolated){ + + case "true": + case "1": + js_eval_isolated = true; + break; + + case "false": + case "0": + js_eval_isolated = false; + break; + + default: + res.writeHead( + 400, + { + "Content-Type": "text/plain" + } + ); + + res.end("Invalid &js_eval_isolated value provided while &js_eval is present"); + return; + } + } + + switch(return_cookies){ + + case "true": + case "1": + return_cookies = true; + break; + + case null: + case "false": + case "0": + return_cookies = false; + break; + + default: + res.writeHead( + 400, + { + "Content-Type": "text/plain" + } + ); + + res.end("Invalid &return_cookies value provided"); + return; + } + console.log("Rendering " + url); if( @@ -122,7 +192,13 @@ const server = http.createServer(async function(req, res){ url_match: url_match, content_match: content_match, fail_url_match: fail_url_match, - fail_content_match: fail_content_match + fail_content_match: fail_content_match, + return_cookies: return_cookies, + newtab: newtab, + js_eval_url_match: js_eval_url_match, + js_eval_content_match: js_eval_content_match, + js_eval: js_eval, + js_eval_isolated: js_eval_isolated } ); @@ -131,9 +207,9 @@ const server = http.createServer(async function(req, res){ await fplay.tab_close(global_ws, newtab); }); -server.listen(webserver_port, function(){ +server.listen(config.port_webserver, function(){ - console.log("http server running"); + console.log("http server running on port " + config.port_webserver); }); function get_proxy(proxy_line){ @@ -206,7 +282,7 @@ function get_proxy(proxy_line){ fplay.event.on("server_ready", function(){ - console.log("listening on port " + port + " (timeout=" + timeout + ")"); + console.log("listening on port " + config.port + " (timeout=" + config.timeout + ")"); }); fplay.event.on("browser_connect", async function(ws){ @@ -236,9 +312,44 @@ fplay.event.on("web_response", async function(page){ var url_match = callback_data.url_match !== null ? page.url.match(new RegExp(callback_data.url_match, "i")) : null; var content_match = callback_data.content_match !== null ? body.match(new RegExp(callback_data.content_match, "i")) : null; + var fail_url_match = callback_data.fail_url_match !== null ? page.url.match(new RegExp(callback_data.fail_url_match, "i")) : null; var fail_content_match = callback_data.fail_content_match !== null ? body.match(new RegExp(callback_data.fail_content_match, "i")) : null; + var js_eval_url_match = callback_data.js_eval_url_match !== null ? page.url.match(new RegExp(callback_data.js_eval_url_match, "i")) : null; + var js_eval_content_match = callback_data.js_eval_content_match !== null ? body.match(new RegExp(callback_data.js_eval_content_match, "i")) : null; + + var return_cookies = callback_data.return_cookies; + + // run javascript on match + if( + ( + callback_data.js_eval_url_match !== null && + callback_data.js_eval_content_match !== null && + js_eval_url_match && + js_eval_content_match + ) || + ( + callback_data.js_eval_url_match === null && + callback_data.js_eval_content_match !== null && + js_eval_content_match + ) || + ( + callback_data.js_eval_url_match !== null && + callback_data.js_eval_content_match === null && + js_eval_url_match + ) + ){ + + // @todo: find a way to return js in headers later + var js = await fplay.tab_inject_js( + global_ws, + callback_data.newtab, + callback_data.js_eval, + callback_data.js_eval_isolated + ); + } + // handle negative response if( ( @@ -256,7 +367,7 @@ fplay.event.on("web_response", async function(page){ { "Content-Type": "text/plain", "X-Proxy": callback_data.proxy.raw, - "X-Container-ID": callback_data.container.id + "X-Container-ID": typeof callback_data.container.id == "undefined" ? callback_data.container : callback_data.container.id } ); @@ -294,12 +405,26 @@ fplay.event.on("web_response", async function(page){ ) ){ + // get cookies if applicable + var cookies = []; + + if(return_cookies){ + + cookies = await fplay.get_cookies( + global_ws, + { + storeId: callback_data.container.id + } + ); + } + callback_data.http_res.writeHead( 200, { "Content-Type": "text/plain;charset=UTF-8", "X-Proxy": callback_data.proxy.raw, - "X-Container-ID": typeof callback_data.container.id == "undefined" ? callback_data.container : callback_data.container.id + "X-Container-ID": typeof callback_data.container.id == "undefined" ? callback_data.container : callback_data.container.id, + ...(return_cookies && { "X-Cookies": JSON.stringify(cookies) }) } ); @@ -334,4 +459,4 @@ fplay.event.on("dom_load_fail", async function(page){ await fplay.container_delete(global_ws, callback_data.container); }); -fplay.init(port, password, timeout); +fplay.init(config.port_4play, config.password, config.timeout); diff --git a/scraper/yandex.php b/scraper/yandex.php index 50ce2fd..be252a3 100644 --- a/scraper/yandex.php +++ b/scraper/yandex.php @@ -14,7 +14,7 @@ class yandex{ // backend included in the scraper functions } - private function get($proxy, $url, $get = [], $nsfw, $get_cookie = 1){ + private function web_get($proxy, $url, $get = [], $nsfw, $get_cookie = 1){ $curlproc = curl_init(); @@ -115,6 +115,163 @@ class yandex{ return $data; } + private function get($session, $proxy, $url, $get = [], $nsfw){ + + $curlproc = curl_init(); + + if($get !== []){ + $get = http_build_query($get); + $url .= "?" . $get; + } + + curl_setopt($curlproc, CURLOPT_URL, $url); + + switch($nsfw){ + case "yes": $nsfw = "0"; break; + case "maybe": $nsfw = "1"; break; + case "no": $nsfw = "2"; break; + } + + $headers = + ["User-Agent: " . config::USER_AGENT, + "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8", + "Accept-Encoding: gzip", + "Accept-Language: en-US,en;q=0.5", + "DNT: 1", + ($session !== null ? "Cookie: {$session}" : null), + "Referer: https://yandex.com/images/search", + "Connection: keep-alive", + "Upgrade-Insecure-Requests: 1", + "Sec-Fetch-Dest: document", + "Sec-Fetch-Mode: navigate", + "Sec-Fetch-Site: cross-site", + "Upgrade-Insecure-Requests: 1"]; + + curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding + curl_setopt($curlproc, CURLOPT_HTTPHEADER, $headers); + + curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2); + curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true); + curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30); + curl_setopt($curlproc, CURLOPT_TIMEOUT, 30); + + $this->backend->assign_proxy($curlproc, $proxy); + + $data = curl_exec($curlproc); + + if(curl_errno($curlproc)){ + + throw new Exception(curl_error($curlproc)); + } + + curl_close($curlproc); + return $data; + } + + private function get_4play($proxy, $url){ + + if(config::FPLAY_EXTERNAL_ENDPOINT === null){ + + throw new Exception("Yandex returned a captcha. The instance maintainer should configure 4play to solve them"); + } + + try{ + $curlproc = curl_init(); + + $url = + config::FPLAY_EXTERNAL_ENDPOINT . + "?url=" . urlencode($url) . + "&pw=" . urlencode(config::FPLAY_PASSWORD) . + "&container=" .// urlencode($container) . + "&proxy=" . urlencode($proxy) . + "&url_match=" . urlencode("\/images\/search\?") . + //"&fail_url_match=" . urlencode("\/sorry\/"); + "&js_eval_url_match=" . urlencode("\/showcaptcha\?") . + "&js_eval=" . urlencode('document.getElementsByClassName("CheckboxCaptcha-Button")[0].click();') . + "&js_eval_isolated=" . "1" . + "&return_cookies=" . "1"; + + $header_out = []; + curl_setopt($curlproc, CURLOPT_HEADERFUNCTION, function($curlproc, $header) use (&$header_out){ + + $length = strlen($header); + $header_out[] = trim($header); + + return $length; + }); + + curl_setopt($curlproc, CURLOPT_URL, $url); + curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding + + curl_setopt($curlproc, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0); + curl_setopt($curlproc, CURLOPT_HTTPHEADER, [ + "User-Agent: " . config::USER_AGENT_FRIENDLY, + "Accept: text/plain" + ]); + + curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2); + curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true); + curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30); + curl_setopt($curlproc, CURLOPT_TIMEOUT, 20); // low timeout for if we fail the captcha + + curl_setopt($curlproc, CURLOPT_FOLLOWLOCATION, true); + + $this->backend->assign_proxy($curlproc, $proxy); + + $data = curl_exec($curlproc); + + if(curl_errno($curlproc)){ + + throw new Exception(curl_error($curlproc)); + } + + curl_close($curlproc); + }catch(Exception $error){ + + throw new Exception("4play timed out. Captcha solve may have failed or render API offline"); + } + + // check for failstate + if( + str_contains( + $header_out[0], + "400" + ) + ){ + + throw new Exception("Page renderer returned an error: " . $data); + } + + // parse headers + $cookies = []; + + foreach($header_out as $header){ + + $header = explode(":", $header, 2); + + if(strtolower($header[0]) == "x-cookies"){ + + $cookies = json_decode(trim($header[1]), true); + break; + } + } + + $cookie_str = []; + // return cookies as string + + foreach($cookies as $cookie){ + + $cookie_str[] = "{$cookie["name"]}={$cookie["value"]};"; + } + + return [ + "data" => $data, + "cookie" => implode(" ", $cookie_str) + ]; + } + public function getfilters($pagetype){ switch($pagetype){ @@ -270,7 +427,7 @@ class yandex{ $proxy = $this->backend->get_ip(); $cookie = - $this->get( + $this->web_get( $proxy, "https://yandex.ru/support2/smart-captcha/ru/", [], @@ -344,7 +501,7 @@ class yandex{ try{ $html = - $this->get( + $this->web_get( $proxy, "https://yandex.com/search/site/", $params, @@ -492,6 +649,9 @@ class yandex{ $nsfw = $request["nsfw"]; unset($request["nsfw"]); + + $session = $request["session"]; + unset($request["session"]); }else{ $search = $get["s"]; @@ -508,6 +668,7 @@ class yandex{ $type = $get["type"]; $layout = $get["layout"]; $format = $get["format"]; + $session = null; /* $handle = fopen("scraper/yandex.json", "r"); $json = fread($handle, filesize("scraper/yandex.json")); @@ -705,11 +866,11 @@ class yandex{ try{ $json = $this->get( + $session, $proxy, "https://yandex.com/images/search", $request, - $nsfw, - "yandex_i" + $nsfw ); }catch(Exception $err){ @@ -733,7 +894,38 @@ class yandex{ $json["type"] == "captcha" ){ - throw new Exception("Yandex blocked this 4get instance. Please try again in ~7 minutes."); + if($session !== null){ + + throw new Exception("Yandex keeps returning captchas. Aborting."); + } + + $session = $this->solve_captcha($proxy, $json["captcha"]["captcha-page"]); + + if($session === false){ + + throw new Exception("4play attempted to solve the JS challenge but was met with an image captcha"); + } + + // try again + try{ + $json = $this->get( + $session, + $proxy, + "https://yandex.com/images/search", + $request, + $nsfw + ); + }catch(Exception $err){ + + throw new Exception("Failed to get un-captchawalled JSON"); + } + + $json = json_decode($json, true); + + if($json === null){ + + throw new Exception("Failed to decode un-captchawalled JSON"); + } } $out = [ @@ -754,6 +946,7 @@ class yandex{ ){ $request["nsfw"] = $nsfw; + $request["session"] = $session; if(isset($request["p"])){ @@ -940,7 +1133,7 @@ class yandex{ */ try{ $json = - $this->get( + $this->web_get( $proxy, "https://yandex.com/video/search", $params, @@ -1104,4 +1297,19 @@ class yandex{ return trim($title); } + + private function solve_captcha($proxy, $captcha_url){ + + // https://yandex.com/showcaptcha?form-fb-hint=2.4.3a20b88c2e3d330a3c0e25a74135697f737300b15a83a73d502fc1f4b12458b536d7a8d6e2064642c7adac96279549d226833ebb381d74daca3a2454&mt=50E3FC3606D54AD20960EED7617BA353D2FB573258E7C759CE8C834CCE683F62AA2B98B8A7D491C5F4E1B90AF69704130A84C9652D13D4E67DCAF46D3CAA78A27FC6157D915D5AE534846C2D098335C6F831532FB8487D27F6120ABC5713EDA9AFDA4ABA14C04F10E2478C34FA2641DF5768249B7BCB314907F9E28794D6384ACB87FBB95A433A45B71266E2E8744695660027A55889AA186D08D378BB6B0FA14490CE5D&retpath=aHR0cHM6Ly95YW5kZXguY29tL2ltYWdlcy9zZWFyY2g_ZnJvbT10YWJiYXImcD0xJnNlcnBMaXN0VHlwZT1ob3Jpem9udGFsJnNlcnBpZD1YTV9mamh5RlVyc0U3ejFOVDFaWlpRJnRleHQ9bmFuYW5pZ2h0cmF5JnRtcGxfdmVyc2lvbj1yZWxlYXNlcy1mcm9udGVuZC1pbWFnZXMtdjEuMTkwNi4wX19jYTZlYmZiZGRkODIwZGRjMTkzMTM2OWJjOTZhYTE5NWQ0ODcxMjI5JnVpbmZvPXN3LTE5MjAtc2gtMTA4MC13dy0xMzA1LXdoLTk5Ny1wZC0xLXdwLTE2eDlfMTkyMHgxMDgwJnV0bV9yZWZlcnJlcj1odHRwcyUzQSUyRiUyRnlhbmRleC5jb20lMkZpbWFnZXMlMkZzZWFyY2glM0Zmcm9tJTNEdGFiYmFyJTI2dGV4dCUzRG5hbmFuaWdodHJheQ%2C%2C_7ae1ea9ca30de369b51cedba38493bd7&t=2%252F1796347471%252F2c7eee85cd849cc18df2b7505200496f&u=9870373719380275251&s=686715091a223e6294320f9b1e457000 + // document.getElementsByClassName("CheckboxCaptcha-Button")[0].click(); + + $session = $this->get_4play($proxy, $captcha_url); + + if($session["cookie"] != ""){ + + return $session["cookie"]; + } + + return false; + } }