add support for cucked urls

This commit is contained in:
lolcat committed 2026-11-30 17:19:39 -05:00
1 parent e03c582f74
commit ffb1fc00f3
8 files changed
+223 -318

No files matched your search

+85
View File
@@ -334,4 +334,89 @@ class backend{
return false;
}
//
// SIGNATURES
//
public function hmac_sign(int $purpose, string $payload){
if(config::HMAC_SECRET === null){
return "";
}
$header = pack("CN", $purpose, intdiv(time(), 60));
$hash = hash_hmac("sha256", $header . $payload, config::HMAC_SECRET, true);
return rtrim(strtr(base64_encode(
$header . $hash
), "+/", "-_"), "=");
}
public function hmac_verify(int $expected_purpose, string $payload, string $signature){
if(config::HMAC_SECRET === null){
return [
"valid" => true
];
}
$decoded = base64_decode(strtr($signature, "-_", "+/"));
if(strlen($decoded) !== 37){
return [
"valid" => false,
"reason" => "Invalid signature size"
];
}
// extract bullshit
$purpose = unpack("C", $decoded)[1];
$timestamp = unpack("N", substr($decoded, 1, 4))[1];
$signature = substr($decoded, 5);
// check purpose
if($purpose !== $expected_purpose){
return [
"valid" => false,
"reason" => "This signature is used for something else"
];
}
// check expiration
$now = intdiv(time(), 60);
if(
$timestamp > $now ||
$now > $timestamp + config::HMAC_TTL
){
return [
"valid" => false,
"reason" => "Signature expired"
];
}
// check hmac
$r_header = pack("CN", $expected_purpose, $timestamp);
$r_hash = hash_hmac("sha256", $r_header . $payload, config::HMAC_SECRET, true);
if(!hash_equals($signature, $r_hash)){
return [
"valid" => false,
"reason" => "Signature mismatch"
];
}
return [
"valid" => true,
"timestamp" => $timestamp,
"signature" => $signature
];
}
}
+4 -5
View File
@@ -1148,7 +1148,7 @@ class frontend{
"ddg" => "DuckDuckGo",
"yandex" => "Yandex",
"brave" => "Brave",
"google" => "Google",
//"google" => "Google",
"google_api" => "Google API",
"google_cse" => "Google CSE",
"yahoo_japan" => "Yahoo! JAPAN",
@@ -1183,7 +1183,7 @@ class frontend{
"ddg" => "DuckDuckGo",
"brave" => "Brave",
"yandex" => "Yandex",
"google" => "Google",
//"google" => "Google",
"yahoo_japan" => "Yahoo! JAPAN",
"startpage" => "Startpage",
"naver" => "Naver",
@@ -1201,7 +1201,7 @@ class frontend{
"option" => [
"ddg" => "DuckDuckGo",
"brave" => "Brave",
"google" => "Google",
//"google" => "Google",
"yahoo_japan" => "Yahoo! JAPAN",
"startpage" => "Startpage",
//"mojeek" => "Mojeek",
@@ -1214,8 +1214,7 @@ class frontend{
$filters["scraper"] = [
"display" => "Scraper",
"option" => [
"sc" => "SoundCloud",
"swisscows" => "Swisscows (SoundCloud)"
"sc" => "SoundCloud"
//"spotify" => "Spotify"
]
];
+36 -41
View File
@@ -1,17 +1,17 @@
<?php
class sc{
class brave{
public function __construct(){
include "lib/backend.php";
$this->backend = new backend("sc");
$this->backend = new backend("brave");
include "lib/fuckhtml.php";
$this->fuckhtml = new fuckhtml();
}
private function get($proxy, $url, $get = []){
private function get(/*$proxy, */$url, $get = []){
$curlproc = curl_init();
@@ -29,7 +29,7 @@ class sc{
["User-Agent: " . config::USER_AGENT,
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language: en-US,en;q=0.5",
"Accept-Encoding: gzip",
"Accept-Encoding: gzip, deflate, br, zstd",
"DNT: 1",
"Sec-GPC: 1",
"Connection: keep-alive",
@@ -40,7 +40,8 @@ class sc{
"Sec-Fetch-User: ?1",
"Priority: u=0, i"];
$this->backend->assign_proxy($curlproc, $proxy);
// i dont think proxies are necessary here...
//$this->backend->assign_proxy($curlproc, $proxy);
curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
curl_setopt($curlproc, CURLOPT_HTTPHEADER, $headers);
@@ -61,57 +62,51 @@ class sc{
return $data;
}
function resolve($id){
function resolve($id, $signature){
if(
!preg_match(
'/^(t|p)[0-9]+$/',
$id
)
){
$sig_status = $this->backend->hmac_verify(4, "brave,{$id}", $signature);
if($sig_status["valid"] === false){
throw new Exception("ID is invalid");
throw new Exception("[Signature] " . $sig_status["reason"]);
}
$type = $id[0] == "t" ? "track" : "playlist";
$id = substr($id, 1);
try{
$html =
$this->get(
$this->backend->get_ip(),
"https://w.soundcloud.com/player/",
[
"url" => "http://api.soundcloud.com/{$type}s/{$id}",
"show_artwork" => "true"
]
);
}catch(Exception $error){
throw new Exception("Failed to fetch song embed page");
}
$html =
$this->get(
//$this->backend->get_ip(),
"https://search.brave.com/go/v1/{$id}",
);
$this->fuckhtml->load($html);
$link =
$a =
$this->fuckhtml
->getElementsByAttributeValue(
"rel",
"canonical",
"link"
->getElementsByClassName(
"destination",
"a"
);
if(count($link) === 0){
if(count($a) === 0){
throw new Exception("Soundcloud could not resolve the song ID to an URL");
throw new Exception("Failed to find link");
}
return
$link =
$this->fuckhtml
->getTextContent(
$link[0]
["attributes"]
["href"]
$a[0]["attributes"]["href"]
);
if(
preg_match(
'/^(https?:|)\/\//',
$link
)
){
return $link;
}
throw new Exception("Got malformed link: " . $link);
}
}
+8 -1
View File
@@ -62,7 +62,14 @@ class google{
return $data;
}
function resolve($id){
function resolve($id, $signature){
$sig_status = $this->backend->hmac_verify(4, "google,{$id}", $signature);
if($sig_status["valid"] === false){
throw new Exception("[Signature] " . $sig_status["reason"]);
}
$html =
$this->get(
+18 -5
View File
@@ -5,8 +5,8 @@ $resolver = new resolver();
class resolver{
public const resolvers = [
"sc",
"google"
"google",
"brave"
];
public function __construct(){
@@ -20,7 +20,6 @@ class resolver{
){
$this->do400("Missing or invalid scraper");
return;
}
if(
@@ -29,25 +28,37 @@ class resolver{
){
$this->do400("Missing or invalid target");
return;
}
if(
!isset($_GET["sig"]) ||
!is_string($_GET["sig"])
){
$this->do400("Missing or malformed signature");
}
$scraper = $_GET["scraper"];
$target = $_GET["target"];
$signature = $_GET["sig"];
try{
include "resolve/{$scraper}.php";
$resolver = new $scraper();
$link = $resolver->resolve($target);
$link = $resolver->resolve($target, $signature);
if(
is_string($link) &&
!empty($link)
){
http_response_code(302);
header("Location: {$link}");
echo 'If your ass is not getting redirected, <a href="' . htmlspecialchars($link) . '">click here</a>.';
}else{
$this->do404("Fuck! Failed to resolve URL: Scraper returned no URL");
}
}catch(Exception $error){
@@ -60,6 +71,7 @@ class resolver{
header("Content-Type: text/plain");
http_response_code(400);
echo htmlspecialchars($message);
die();
}
public function do404($message){
@@ -67,5 +79,6 @@ class resolver{
header("Content-Type: text/plain");
http_response_code(404);
echo htmlspecialchars($message);
die();
}
}
+31 -12
View File
@@ -537,7 +537,7 @@ class brave{
$cluster["description"]
)
),
"url" => $cluster["url"],
"url" => $this->convert_goto_to_resolver($cluster["url"]),
"date" => null
];
}
@@ -554,7 +554,7 @@ class brave{
$sublink[] = [
"title" => $this->titledots($r["title"]),
"description" => null,
"url" => $r["url"],
"url" => $this->convert_goto_to_resolver($r["url"]),
"date" => null
];
}
@@ -864,7 +864,7 @@ class brave{
$result["description"]
)
),
"url" => $result["url"],
"url" => $this->convert_goto_to_resolver($result["url"]),
"date" => $date,
"type" => "web",
"thumb" => $thumb,
@@ -954,7 +954,7 @@ class brave{
$description[] = [
"type" => "link",
"url" => $rating["profile"]["url"],
"url" => $this->convert_goto_to_resolver($rating["profile"]["url"]),
"value" => $rating["profile"]["name"]
];
@@ -1006,7 +1006,9 @@ class brave{
$sublink[
$this->fuckhtml->getTextContent($name)
] =
$this->fuckhtml->getTextContent($row["url"]);
$this->fuckhtml->getTextContent(
$this->convert_goto_to_resolver($row["url"])
);
}
}
@@ -1025,7 +1027,7 @@ class brave{
$out["answer"][] = [
"title" => $this->fuckhtml->getTextContent($info["title"]),
"description" => $description,
"url" => $info["url"],
"url" => $this->convert_goto_to_resolver($info["url"]),
"thumb" => $thumb,
"table" => $table,
"sublink" => $sublink
@@ -1077,7 +1079,7 @@ class brave{
"ratio" => null,
"url" => null
],
"url" => $video["url"]
"url" => $this->convert_goto_to_resolver($video["url"])
];
}
}
@@ -1103,7 +1105,7 @@ class brave{
"ratio" => null,
"url" => null
],
"url" => $news["url"]
"url" => $this->convert_goto_to_resolver($news["url"])
];
}
}
@@ -1143,7 +1145,7 @@ class brave{
)
)
),
"url" => $disc["url"],
"url" => $this->convert_goto_to_resolver($disc["url"]),
"date" => isset($disc["age"]) ? strtotime($disc["age"]) : null,
"type" => "web",
"thumb" => [
@@ -1254,7 +1256,7 @@ class brave{
"height" => (int)$result["thumbnail"]["height"]
]
],
"url" => $result["url"]
"url" => $this->convert_goto_to_resolver($result["url"])
];
}
}
@@ -1425,7 +1427,7 @@ class brave{
"duration" => $result["video"]["duration"] == "null" ? null : $this->hms2int($result["video"]["duration"]),
"views" => $result["video"]["views"] == "null" ? null : (int)$result["video"]["views"],
"thumb" => $thumb,
"url" => $result["url"]
"url" => $this->convert_goto_to_resolver($result["url"])
];
}
}
@@ -1571,7 +1573,7 @@ class brave{
"description" => $news["description"],
"date" => !isset($news["age"]) || $news["age"] == "void 0" || $news["age"] == "null" ? null : strtotime($news["age"]),
"thumb" => $thumb,
"url" => $news["url"]
"url" => $this->convert_goto_to_resolver($news["url"])
];
}
}
@@ -2802,6 +2804,23 @@ class brave{
return false;
}
private function convert_goto_to_resolver($url){
if(
preg_match(
'/^\/go\/v1\/([^\/?&]+)/',
$url,
$slug
) === 1
){
return "/resolver?scraper=brave&target={$slug[1]}&sig=" . $this->backend->hmac_sign(4, "brave,{$slug[1]}");
}
// we got a normal URL, yay yipee
return $url;
}
private function detect_captcha(){
$title =
+35 -244
View File
@@ -642,22 +642,6 @@ class google{
];
}
elseif(
preg_match(
'/https?:\/\/share\.google\/([^"]+)+/',
$url,
$link_regex
) === 1
){
// share.google
$this->urls_to_resolve[] = [
"replace" => $replace,
"slug" => $link_regex[1],
"type" => "share"
];
}
// rest is ignored, since its not a redirect
// always return url
@@ -704,86 +688,6 @@ class google{
return $url;
}
private function urlfuck_shareservice($url, $slug){
$curlproc = curl_init();
curl_setopt($curlproc, CURLOPT_URL, "https://www.google.com/httpservice/retry/SearchApiService/GetShortenedKpSharingUrl?udm=14&reqpld=[[[\"/goto?url%3D" . rawurlencode($slug) . "\"],1,null,null,null,null,35,[\"st\"]]]&msc=gwsrpc");
curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
curl_setopt($curlproc, CURLOPT_HTTPHEADER,
["User-Agent: " . config::USER_AGENT,
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language: en-US,en;q=0.5",
"Accept-Encoding: gzip, deflate, br, zstd",
"DNT: 1",
"Sec-GPC: 1",
"Connection: keep-alive",
"Upgrade-Insecure-Requests: 1",
"Sec-Fetch-Dest: document",
"Sec-Fetch-Mode: navigate",
"Sec-Fetch-Site: none",
"Sec-Fetch-User: ?1",
"Priority: u=0, i"]
);
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, $this->backend->get_ip(null, true));
curl_multi_add_handle($this->proc, $curlproc);
$this->handles[] = [
"replacement" => $url,
"handle" => $curlproc
];
return $url;
}
private function urlfuck_share($url, $slug){
$curlproc = curl_init();
curl_setopt($curlproc, CURLOPT_URL, "https://www.google.com/share.google?q=" . rawurlencode($slug));
curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
curl_setopt($curlproc, CURLOPT_HTTPHEADER,
["User-Agent: " . config::USER_AGENT,
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language: en-US,en;q=0.5",
"Accept-Encoding: gzip, deflate, br, zstd",
"DNT: 1",
"Sec-GPC: 1",
"Connection: keep-alive",
"Upgrade-Insecure-Requests: 1",
"Sec-Fetch-Dest: document",
"Sec-Fetch-Mode: navigate",
"Sec-Fetch-Site: none",
"Sec-Fetch-User: ?1",
"Priority: u=0, i"]
);
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, $this->backend->get_ip(null, true));
curl_multi_add_handle($this->proc, $curlproc);
$this->handles[] = [
"replacement" => $url,
"handle" => $curlproc
];
return $url;
}
private function resolve_gotos(&$scraped_data){
foreach($this->urls_to_resolve as $item){
@@ -861,140 +765,6 @@ class google{
curl_multi_close($this->proc);
}
private function resolve_share(&$scraped_data){
foreach($this->urls_to_resolve as $item){
$this->urlfuck_shareservice($item["replace"], $item["slug"]);
}
do{
do{
$status = curl_multi_exec($this->proc, $active);
}while($status === CURLM_CALL_MULTI_PERFORM);
if($active){
curl_multi_select($this->proc);
}
}while($active && $status === CURLM_OK);
//
// if we reach this, we're done downloading garbage
//
// get the share links
$this->urls_to_resolve = [];
for($i=0; $i<count($this->handles); $i++){
$html = curl_multi_getcontent($this->handles[$i]["handle"]);
if(
preg_match(
'/https?:\/\/share\.google\/(?:[^"]+)/',
$html,
$link_regex
) !== 1
){
// failed to find link, will serve a resolver target as fallback
$fallback = true;
$slug = explode("url=", $this->handles[$i]["replacement"]);
$link = "/resolver?scraper=google&target=" . rawurlencode($slug[1]);
}else{
$fallback = false;
$link = $link_regex[0];
}
if(
(
$fallback === false &&
preg_match(
'/^(https?:|)\/\//',
$link
)
) ||
$fallback === true
){
$this->redirect_add_url($link, $this->handles[$i]["replacement"]);
}
}
// resolve shareable urls
$this->init_multi_curl();
foreach($this->urls_to_resolve as $item){
$this->urlfuck_share($item["replace"], $item["slug"]);
}
do{
do{
$status = curl_multi_exec($this->proc, $active);
}while($status === CURLM_CALL_MULTI_PERFORM);
if($active){
curl_multi_select($this->proc);
}
}while($active && $status === CURLM_OK);
// parse downloaded garbage
$keys = [];
$replacements = [];
for($i=0; $i<count($this->handles); $i++){
$html = curl_multi_getcontent($this->handles[$i]["handle"]);
$this->fuckhtml->load($html);
$a =
$this->fuckhtml
->getElementsByTagName(
"a"
);
if(count($a) === 0){
// failed to find link, will serve a resolver target as fallback
$fallback = true;
$slug = explode("url=", $this->handles[$i]["replacement"]);
$link = "/resolver?scraper=google&target=" . rawurlencode($slug[1]);
}else{
$fallback = false;
$link =
$this->fuckhtml
->getTextContent(
$a[0]["attributes"]["href"]
);
}
if(
(
$fallback === false &&
preg_match(
'/^(https?:|)\/\//',
$link
)
) ||
$fallback === true
){
$keys[] = $this->handles[$i]["replacement"];
$replacements[] = $link;
}
}
$this->recursive_replace($keys, $replacements, $scraped_data);
curl_multi_close($this->proc);
}
private function recursive_replace(&$keys, &$replacements, &$pointer){
foreach($pointer as $key => &$value){
@@ -1058,7 +828,7 @@ class google{
$params = [
"q" => $get["s"],
"hl" => "en",
"udm" => 14
//"udm" => 14
];
// country
@@ -1334,7 +1104,7 @@ class google{
}
// sometimes the link is an encrypted redirect, handle redirection securely
$notices[] = $this->redirect_add_url($link);
$notices[] = $this->convert_goto_to_resolver($link);
}
}
@@ -1697,14 +1467,14 @@ class google{
$this->fuckhtml
->getTextContent($sublink),
"date" => null,
"url" => $this->redirect_add_url($sublink_url)
"url" => $this->convert_goto_to_resolver($sublink_url)
];
}
$out["web"][] = [
"title" => $title,
"description" => $description,
"url" => $this->redirect_add_url($url),
"url" => $this->convert_goto_to_resolver($url),
"date" => $date,
"type" => "web",
"thumb" => $thumb,
@@ -1713,7 +1483,8 @@ class google{
];
}
$this->resolve_share($out);
// no longer possible
//$this->resolve_gotos($out);
return $out;
}
@@ -1756,8 +1527,8 @@ class google{
$params = [
"q" => $search,
"udm" => "2", // get images
"start" => 0
"tbm" => "isch", // get images
//"start" => 0
];
// country (image search uses cr instead of gl)
@@ -1767,7 +1538,7 @@ class google{
}
// nsfw
$params["safe"] = $nsfw == "yes" ? "off" : "active";
//$params["safe"] = $nsfw == "yes" ? "off" : "active";
// generate tbs
$tbs = [];
@@ -1959,6 +1730,7 @@ class google{
);
}
// somehow still works? gotta keep an eye on it
$this->resolve_gotos($out);
return $out;
@@ -1992,7 +1764,7 @@ class google{
$params = [
"q" => $search,
"udm" => "7",
"hl" => "en"
//"hl" => "en"
];
// country
@@ -2363,11 +2135,12 @@ class google{
"duration" => $duration,
"views" => null,
"thumb" => $thumb,
"url" => $this->redirect_add_url($url)
"url" => $this->convert_goto_to_resolver($url)
];
}
$this->resolve_share($out);
// does not work
//$this->resolve_gotos($out);
return $out;
}
@@ -2407,7 +2180,7 @@ class google{
$params = [
"q" => $search,
"tbm" => "nws",
"hl" => "en"
//"hl" => "en"
];
// country
@@ -2726,14 +2499,15 @@ class google{
"date" => $date,
"thumb" => $thumb,
"url" =>
$this->redirect_add_url(
$this->convert_goto_to_resolver(
$a["attributes"]
["href"]
)
];
}
$this->resolve_share($out);
// does not work
//$this->resolve_gotos($out);
return $out;
}
@@ -3258,6 +3032,23 @@ class google{
return rtrim(strtr(base64_encode($data), "+/", "-_"), "=");
}
private function convert_goto_to_resolver($url){
if(
preg_match(
'/^\/goto\?url=([^&]+)/',
$url,
$slug
) === 1
){
return "/resolver?scraper=google&target={$slug[1]}&sig=" . $this->backend->hmac_sign(4, "google,{$slug[1]}");
}
// we got a normal URL, yay yipee
return $url;
}
private function detect_sorry(){
// detect recaptcha
+6 -10
View File
@@ -223,10 +223,10 @@ $settings = [
"value" => "brave",
"text" => "Brave"
],
[
/*[
"value" => "google",
"text" => "Google"
],
],*/
[
"value" => "google_api",
"text" => "Google API"
@@ -321,10 +321,10 @@ $settings = [
"value" => "yandex",
"text" => "Yandex"
],
[
/*[
"value" => "google",
"text" => "Google"
],
],*/
[
"value" => "yahoo_japan",
"text" => "Yahoo! JAPAN",
@@ -367,10 +367,10 @@ $settings = [
"value" => "brave",
"text" => "Brave"
],
[
/*[
"value" => "google",
"text" => "Google"
],
],*/
[
"value" => "yahoo_japan",
"text" => "Yahoo! JAPAN",
@@ -396,10 +396,6 @@ $settings = [
[
"value" => "sc",
"text" => "SoundCloud"
],
[
"value" => "swisscows",
"text" => "Swisscows"
]
/*[
"value" => "spotify",