-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
178 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
// } | ||
})(); |