Advertisement
Guest User

challenge.js

a guest
May 2nd, 2024
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. document.documentElement.classList.remove("no-scripts");
  2. document.documentElement.classList.add("scripts");
  3.  
  4. window.sssg_challenge = function (hashString, difficulty, numerToCalculatePatienceTime) {
  5.   console.log("Starting work.");
  6.  
  7.   const worker = new Worker("/.sssg/public/worker.js");
  8.  
  9.   document.getElementById("loader").setAttribute("class", "playing");
  10.  
  11.   let currentUnixTime = Date.now()
  12.   var deadLine = currentUnixTime + (6e4 - (currentUnixTime % 6e4)) + 6e4 * numerToCalculatePatienceTime;
  13.   var finalSolution = null
  14.   var amountOfCores = navigator.hardwareConcurrency
  15.   var initialPosition = 4503599627370496 * Math.random()
  16.   var attempt_number = initialPosition;
  17.  
  18.   console.log(`\t${deadLine - currentUnixTime}ms of patience.\n\tSalt is ${hashString}\n\tDifficutly of ${difficulty}\n\tInitial position of ${initialPosition}`);
  19.  
  20.   for (var u = 0; u < amountOfCores; ++u) worker.postMessage({ attempt: attempt_number, difficulty: difficulty, salt: hashString }), ++attempt_number;
  21.  
  22.   window.addEventListener("storage", (e) => {
  23.     "sssg_clearance" == e.key &&
  24.       e.newValue &&
  25.       (worker.terminate(),
  26.       "PAUSE" != e.newValue
  27.         ? (console.log(
  28.             `Accepting answer from LocalStorage!\n\t${e.newValue}`
  29.           ),
  30.           window.location.reload())
  31.         : console.log("SessionStorage instructs a pause."));
  32.   });
  33.  
  34.   worker.addEventListener("message",
  35.     ({ data: { attempt: attempt, solution: solution } }) => {
  36.       var newAttemptNumber = attempt + amountOfCores;
  37.  
  38.       if (null === solution) {
  39.         var howManyAttempts = newAttemptNumber - initialPosition;
  40.  
  41.         null === finalSolution && (howManyAttempts % 1e4 == 0 && (console.log(`\tAttempt #${howManyAttempts}`),
  42.         Date.now() > deadLine && (console.log("RAN OUT OF TIME!"), location.reload())),
  43.         worker.postMessage({ attempt: newAttemptNumber, difficulty: difficulty, salt: hashString }));
  44.  
  45.       } else {
  46.         console.log(`Work concluded after ${Date.now() - currentUnixTime}ms.\n\t${solution}`);
  47.         finalSolution = solution;
  48.         console.log("\tSubmitting to server...");
  49.  
  50.         let setPageStatus = function (e) {
  51.           "string" == typeof e
  52.             ? (document.getElementById("status").innerHTML = `<p>${e}</p>`)
  53.             : "error" in e
  54.             ? (console.log(
  55.                 `Error received in reply to server:\n\t${e.error}`
  56.               ),
  57.               (document.getElementById("status").innerHTML =
  58.                 "<p>Received a client error in reply. Please try again.</p>"))
  59.             : (console.log(`Incomprehensible reply:\n\t${e}`),
  60.               (document.getElementById("status").innerHTML = "<p>Received a server error in reply. This issue may be temporary. Try again.</p>")),
  61.          
  62.           document.getElementById("loader").setAttribute("class", "paused");
  63.           let t = document.querySelectorAll("#loader circle");
  64.           for (let e = 0; e < t.length; ++e) t[e].style = "opacity: 1 !important; fill: #a66b5e !important;";
  65.         };
  66.  
  67.         let statusSuccess = function (e) {
  68.           window.localStorage.setItem("sssg_clearance", "PAUSE");
  69.           document.getElementById("status").innerHTML = "<p>Work accepted! Validating session...</p>";
  70.           document.getElementById("loader").setAttribute("class", "paused");
  71.  
  72.           let t = document.querySelectorAll("#loader circle");
  73.           for (let e = 0; e < t.length; ++e) t[e].style = "fill: #6ba65e !important;";
  74.  
  75.           var XMLHttpReq1 = new XMLHttpRequest();
  76.  
  77.           XMLHttpReq1.onreadystatechange = function () {
  78.             if (XMLHttpReq1.readyState === 4)
  79.               try {
  80.                 var e = JSON.parse(XMLHttpReq1.response);
  81.                 "auth" in e ? reloadPage(e) : setPageStatus(e);
  82.               } catch (e) {
  83.                 setPageStatus("Server responded with an error."),
  84.                   console.log(`Bad Json: ${XMLHttpReq1.response}`),
  85.                   console.log(e);
  86.               }
  87.           }
  88.  
  89.           XMLHttpReq1.open("POST", "/.sssg/api/check", !0);
  90.  
  91.           XMLHttpReq1.setRequestHeader(
  92.             "Content-type",
  93.             "application/x-www-form-urlencoded"
  94.           );
  95.  
  96.           XMLHttpReq1.send(`f=${e.auth}`);
  97.         };
  98.  
  99.         let reloadPage = function (e) {
  100.           window.localStorage.setItem("sssg_clearance", e.auth);
  101.           document.getElementById("status").innerHTML = "<p>All done! Reloading page...</p>";
  102.           document.getElementById("loader").setAttribute("class", "paused");
  103.           let t = document.querySelectorAll("#loader circle");
  104.           for (let e = 0; e < t.length; ++e) t[e].style = "opacity: 1 !important; fill: #6ba65e !important;";
  105.  
  106.           setTimeout(function () {location.reload();}, 1500);
  107.         };
  108.  
  109.         var XMLHttpReq2 = new XMLHttpRequest();
  110.  
  111.         (XMLHttpReq2.onreadystatechange = function () {
  112.           if (4 === XMLHttpReq2.readyState)
  113.             try {
  114.               var e = JSON.parse(XMLHttpReq2.response);
  115.               "auth" in e ? statusSuccess(e) : setPageStatus(e);
  116.             } catch (e) {
  117.               setPageStatus("Server responded with an error."),
  118.                 console.log(`Bad Json: ${XMLHttpReq2.response}`),
  119.                 console.log(e);
  120.             }
  121.         }),
  122.           XMLHttpReq2.open("POST", "/.sssg/api/answer", !0),
  123.           XMLHttpReq2.setRequestHeader(
  124.             "Content-type",
  125.             "application/x-www-form-urlencoded"
  126.           ),
  127.           XMLHttpReq2.send(`a=${hashString}&b=${attempt}`);
  128.       }
  129.     }
  130.   );
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement