Skip to content

Commit

Permalink
added basic fingerprinting
Browse files Browse the repository at this point in the history
  • Loading branch information
NullDev committed Nov 13, 2023
1 parent e5fa10d commit acf2d1a
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
32 changes: 31 additions & 1 deletion web/src/challange/challanges.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import Fingerprinting from "./fingerprinting";

/**
* Collection of challenges.
*
* @class Challenges
*/
class Challenges {
class Challenges extends Fingerprinting {
constructor(){
super();
}

/**
* Calculate native hash.
*
Expand All @@ -18,6 +24,20 @@ class Challenges {
return hashArray.map(b => b.toString(16).padStart(2, "0")).join("");
}

/**
* Send failed fingerprinting to server.
*
* @param {string} reason
* @return {Promise<void>}
* @memberof Challenger
*/
async #sendFingerprintingResult(reason){
fetch("/cdn-cgi/challenge-platform/challenge", {
method: "POST",
body: reason,
});
}

/**
* Challenge POW.
*
Expand Down Expand Up @@ -51,6 +71,16 @@ class Challenges {
* @memberof Challenger
*/
async challengeFingerprinting(){
const usualUa = this.isUsualBrowserBasedOnUa();
if (!usualUa) return await this.#sendFingerprintingResult("ua");

const notHeadless = this.isNotHeadless();
if (!notHeadless) return await this.#sendFingerprintingResult("headless");

const canvasSupported = this.isCanvasSupported();
if (!canvasSupported) return await this.#sendFingerprintingResult("canvas");

return await this.#sendFingerprintingResult("ok");
}

/**
Expand Down
55 changes: 55 additions & 0 deletions web/src/challange/fingerprinting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Browser based fingerprinting
*
* @class Fingerprinting
*/
class Fingerprinting {
/**
* Check user agent string for usual browsers.
*
* @return {boolean}
* @memberof Fingerprinting
*/
isUsualBrowserBasedOnUa(){
const ua = navigator.userAgent;
const isUsualBrowser = ua.includes("Chrome")
|| ua.includes("Firefox")
|| ua.includes("Safari")
|| ua.includes("Edge")
|| ua.includes("Opera")
|| ua.includes("OPR")
|| ua.includes("MSIE")
|| ua.includes("Trident");
return isUsualBrowser;
}

/**
* Check if browser is headless.
*
* @return {boolean}
* @memberof Fingerprinting
*/
isNotHeadless(){
const isHeadless = !!navigator.webdriver // @ts-ignore
|| !!window.chrome
|| !!navigator.languages
|| navigator.userAgent.toLowerCase().includes("headless");
return !isHeadless;
}

/**
* Check if canvas is supported.
*
* @return {boolean}
* @memberof Fingerprinting
*/
isCanvasSupported(){
const canvas = document.createElement("canvas");
const gl = canvas.getContext("webgl") || canvas.getContext("experimental-webgl");
return !!gl;
}

// more to come
}

export default Fingerprinting;

0 comments on commit acf2d1a

Please sign in to comment.