aboutsummaryrefslogtreecommitdiffstats
path: root/lib/anubis.php
diff options
context:
space:
mode:
authorlolcat <will@lolcat.ca>2025-08-11 01:55:15 +0000
committerlolcat <will@lolcat.ca>2025-08-11 01:55:15 +0000
commitcdf958d29333d448f4521f4d2faa2592b58e9b27 (patch)
tree528f2a0ffa789a6f4279d9f54a4a2aaf391f390f /lib/anubis.php
downloadshittyweb-search-cdf958d29333d448f4521f4d2faa2592b58e9b27.tar.gz
shittyweb-search-cdf958d29333d448f4521f4d2faa2592b58e9b27.tar.bz2
shittyweb-search-cdf958d29333d448f4521f4d2faa2592b58e9b27.zip
fix wikipedia crashgrafted
Diffstat (limited to 'lib/anubis.php')
-rw-r--r--lib/anubis.php100
1 files changed, 100 insertions, 0 deletions
diff --git a/lib/anubis.php b/lib/anubis.php
new file mode 100644
index 0000000..2bd6d90
--- /dev/null
+++ b/lib/anubis.php
@@ -0,0 +1,100 @@
+<?php
+
+//
+// Reference
+// https://github.com/TecharoHQ/anubis/blob/ecc716940e34ebe7249974f2789a99a2c7115e4e/web/js/proof-of-work.mjs
+//
+
+class anubis{
+
+ public function __construct(){
+
+ include_once "fuckhtml.php";
+ $this->fuckhtml = new fuckhtml();
+ }
+
+ public function scrape($html){
+
+ $this->fuckhtml->load($html);
+
+ $script =
+ $this->fuckhtml
+ ->getElementById(
+ "anubis_challenge",
+ "script"
+ );
+
+ if($script === false){
+
+ throw new Exception("Failed to scrape anubis challenge data");
+ }
+
+ $script =
+ json_decode(
+ $this->fuckhtml
+ ->getTextContent(
+ $script
+ ),
+ true
+ );
+
+ if($script === null){
+
+ throw new Exception("Failed to decode anubis challenge data");
+ }
+
+ if(
+ !isset($script["challenge"]) ||
+ !isset($script["rules"]["difficulty"]) ||
+ !is_int($script["rules"]["difficulty"]) ||
+ !is_string($script["challenge"])
+ ){
+
+ throw new Exception("Found invalid challenge data");
+ }
+
+ return $this->rape($script["challenge"], $script["rules"]["difficulty"]);
+ }
+
+ private function is_valid_hash($hash, $difficulty){
+
+ for ($i=0; $i<$difficulty; $i++) {
+
+ $index = (int)floor($i / 2);
+ $nibble = $i % 2;
+
+ $byte = ord($hash[$index]);
+ $nibble = ($byte >> ($nibble === 0 ? 4 : 0)) & 0x0f;
+
+ if($nibble !== 0){
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ public function rape($data, $difficulty = 5){
+
+ $nonce = 0;
+
+ while(true){
+
+ $hash_binary = hash("sha256", $data . $nonce, true);
+
+ if($this->is_valid_hash($hash_binary, $difficulty)){
+
+ $hash_hex = bin2hex($hash_binary);
+
+ return [
+ "response" => $hash_hex,
+ //"data" => $data,
+ //"difficulty" => $difficulty,
+ "nonce" => $nonce
+ ];
+ }
+
+ $nonce++;
+ }
+ }
+}