Skip to content

Commit

Permalink
complete refactor of everything
Browse files Browse the repository at this point in the history
  • Loading branch information
NullDev committed Nov 13, 2023
1 parent 52c30b8 commit e5fa10d
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 88 deletions.
52 changes: 52 additions & 0 deletions web/src/challange/challanger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import Loader from "./loader";
import Challenges from "./challanges";

/**
* Decide and solve browser challenges.
*
* @class Challenger
*/
class Challenger extends Challenges {
/**
* Creates an instance of Challenger.
*
* @memberof Challenger
*/
constructor(){
super();
this.loader = new Loader();
}

/**
* Do challenge.
*
* @return {Promise<void>}
* @memberof Challenger
*/
async doChallenge(){
this.loader.start();

const challenge = await this.getChallenge();
const { t } = challenge;

if (t === 0) await this.challengeNone();
else if (t === 1) await this.challengePOW(challenge);
else if (t === 2) await this.challengeFingerprinting();
else await this.challengeUnknown();

this.loader.stop();
}

/**
* Get challenge.
*
* @return {Promise<object>}
* @memberof Challenger
*/
async getChallenge(){
const resp = await fetch("/cdn-cgi/challenge-platform/challenge");
return await resp.json();
}
}

export default Challenger;
78 changes: 78 additions & 0 deletions web/src/challange/challanges.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* Collection of challenges.
*
* @class Challenges
*/
class Challenges {
/**
* Calculate native hash.
*
* @param {string} data
* @param {AlgorithmIdentifier} method
* @return {Promise<string>}
* @memberof Challenger
*/
async #nativeHash(data, method){
const hashBuffer = await crypto.subtle.digest(method, new TextEncoder().encode(data));
const hashArray = Array.from(new Uint8Array(hashBuffer));
return hashArray.map(b => b.toString(16).padStart(2, "0")).join("");
}

/**
* Challenge POW.
*
* @param {object} challenge
* @return {Promise<void>}
* @memberof Challenger
*/
async challengePOW(challenge){
let hash; let i;
// eslint-disable-next-line no-constant-condition
for (i = 0; true; i++){
hash = await this.#nativeHash(challenge.r + i.toString(), "sha-256");
if (hash.startsWith("0000")){
console.log(challenge);
console.log(i);
console.log(hash);
break;
}
}

await fetch("/cdn-cgi/challenge-platform/challenge", {
method: "POST",
body: challenge.r + "-" + challenge.s + "-" + i.toString(),
});
}

/**
* Do browser fingerprinting.
*
* @return {Promise<void>}
* @memberof Challenger
*/
async challengeFingerprinting(){
}

/**
* No challenge.
*
* @return {Promise<void>}
* @memberof Challenger
*/
async challengeNone(){
return;
}

/**
* Unknown challenge from server.
* Reload page.
*
* @return {Promise<void>}
* @memberof Challenger
*/
async challengeUnknown(){
window.location.reload();
}
}

export default Challenges;
37 changes: 37 additions & 0 deletions web/src/challange/loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Countdown loader.
*
* @class Loader
*/
class Loader {
/**
* Creates an instance of Loader.
*
* @memberof Loader
*/
constructor(){
this.loader = /** @type {HTMLDivElement} */ (document.querySelector(".circle-loader"));
this.checkmark = /** @type {HTMLDivElement} */ (document.querySelector(".checkmark"));
}

/**
* Start the countdown.
*
* @memberof Loader
*/
start(){
this.loader.style.display = "inline-block";
}

/**
* Stop the countdown.
*
* @memberof Loader
*/
stop(){
this.loader.classList.add("load-complete");
this.checkmark.style.display = "block";
}
}

export default Loader;
99 changes: 11 additions & 88 deletions web/src/main.js
Original file line number Diff line number Diff line change
@@ -1,91 +1,14 @@
import './style.css'

const fetchChallenge = async () => {
const resp = await fetch("/cdn-cgi/challenge-platform/challenge");
return await resp.json();
}

const challengeNone = async () => {

}

const nativeHash = async (data, method) => {
const buffer = new TextEncoder().encode(data);
const hashBuffer = await crypto.subtle.digest(method, buffer)
const hashArray = Array.from(new Uint8Array(hashBuffer));
return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
}


const challengePOW = async (challenge) => {
let hash, i;
for (i = 0; true; i++) {
hash = await nativeHash(challenge.r + i.toString(), 'sha-256')
if (hash.startsWith("0000")) {
console.log(challenge)
console.log(i)
console.log(hash)
break;
import "./style.css";
import Challenger from "./challange/challanger";

(() => {
window.addEventListener("DOMContentLoaded", async() => {
if (!navigator.cookieEnabled){
const noCookie = document.getElementById("no-cookie");
if (noCookie) noCookie.style.display = "block";
}
}

return fetch("/cdn-cgi/challenge-platform/challenge", {
method: "POST",
body: challenge.r + "-" + challenge.s + "-" + i.toString()
const challanger = new Challenger();
await challanger.doChallenge();
});
}

const reload = window.location.reload;
const challengeUnknown = reload;

const doChallenge = async () => {
const challenge = await fetchChallenge();

let challengeFunc;
switch (challenge.t) {
case 0:
challengeFunc = challengeNone
break;
case 1:
challengeFunc = challengePOW
break;
default:
challengeFunc = challengeUnknown
}

return challengeFunc(challenge)
}

const showCountdown = async (seconds) => {
const secondsTmp = seconds;

const countdown = document.querySelector(".captcha-container .countdown-number");
countdown.textContent = seconds;
countdown.style.color = "black";

const timer = setInterval(() => {
seconds = --seconds <= 0 ? secondsTmp : seconds;
countdown.textContent = seconds;
}, 1000);

setTimeout(() => {
clearInterval(timer);
}, seconds * 1000);
}

const onContentLoaded = async () => {
if (!navigator.cookieEnabled) {
document.getElementById('no-cookie').style.display = "block";
}

await doChallenge();

await showCountdown(5);
}

window.addEventListener("DOMContentLoaded", onContentLoaded)

const cookieName = "berghain";
// if (getParameterByName(cookieName) !== null && getCookie(cookieName) === undefined) {
// document.getElementById('loop-warning').style.display = "block";
// }
})();

0 comments on commit e5fa10d

Please sign in to comment.