browser_fingerprint
Advanced tools
Comparing version 2.0.3 to 2.0.4
@@ -1,2 +0,1 @@ | ||
import * as path from "path"; | ||
import * as http from "http"; | ||
@@ -10,6 +9,5 @@ import * as request from "request-promise-native"; | ||
let fingerPrinter = new BrowserFingerprint(); | ||
let server: http.Server; | ||
let fingerprint: string; | ||
let server; | ||
let fingerprint; | ||
describe("browser fingerpint", () => { | ||
@@ -19,12 +17,9 @@ beforeAll(() => { | ||
.createServer((req, res) => { | ||
const { | ||
fingerprint, | ||
elementHash, | ||
headersHash, | ||
} = fingerPrinter.fingerprint(req); | ||
const { fingerprint, elementsHash, headersHash } = | ||
fingerPrinter.fingerprint(req); | ||
headersHash["Content-Type"] = "text/plain"; | ||
res.writeHead(200, headersHash); | ||
let resp = `Fingerprint: ${fingerprint} \r\n\r\n`; | ||
for (const i in elementHash) { | ||
resp += `Element ${i}: ${elementHash[i]}\r\n`; | ||
for (const i in elementsHash) { | ||
resp += `Element ${i}: ${elementsHash[i]}\r\n`; | ||
} | ||
@@ -152,2 +147,3 @@ res.end(resp); | ||
it("works with directives without value", async () => { | ||
// @ts-ignore | ||
const options = { settings: { httpOnly: null, secure: null } }; | ||
@@ -170,2 +166,3 @@ fingerPrinter = new BrowserFingerprint(options); | ||
const options = { | ||
// @ts-ignore | ||
settings: { expires: 3600000, httpOnly: null, path: "/", secure: null }, | ||
@@ -172,0 +169,0 @@ }; |
@@ -15,7 +15,2 @@ /// <reference types="node" /> | ||
constructor(options?: {}); | ||
defaults(): { | ||
cookieKey: string; | ||
toSetCookie: boolean; | ||
onlyStaticElements: boolean; | ||
}; | ||
/** | ||
@@ -28,26 +23,9 @@ * The goal is to come up with as many *potentially* unique traits for the connection and add them to the elementsHash. | ||
fingerprint: string; | ||
elementHash: { | ||
clientCookie: string; | ||
}; | ||
headersHash: {}; | ||
elementsHash?: undefined; | ||
} | { | ||
fingerprint: string; | ||
elementsHash: { | ||
httpVersion: string; | ||
remoteAddress: string; | ||
cookieKey: string; | ||
hashedHostName: string; | ||
remotePort: any; | ||
rand: any; | ||
time: any; | ||
hashedPid: any; | ||
}; | ||
headersHash: {}; | ||
elementHash?: undefined; | ||
headersHash: Record<string, string>; | ||
elementsHash: Record<string, string | number>; | ||
}; | ||
hashedHostName(): string; | ||
hashedPid(): string | number; | ||
parseCookies(req: any): {}; | ||
calculateHashFromElements(elementsHash: any): string; | ||
parseCookies(req: IncomingMessage): Record<string, string>; | ||
calculateHashFromElements(elementsHash: Record<string, string | number>): string; | ||
sortAndStringObject(o: { | ||
@@ -54,0 +32,0 @@ [key: string]: any; |
@@ -14,9 +14,2 @@ "use strict"; | ||
} | ||
defaults() { | ||
return { | ||
cookieKey: "__browser_fingerprint", | ||
toSetCookie: true, | ||
onlyStaticElements: false, | ||
}; | ||
} | ||
/** | ||
@@ -28,35 +21,27 @@ * The goal is to come up with as many *potentially* unique traits for the connection and add them to the elementsHash. | ||
fingerprint(req) { | ||
var _a, _b, _c; | ||
let fingerprint; | ||
let key; | ||
let value; | ||
let headersHash = {}; | ||
let elementsHash = { | ||
clientCookie: fingerprint, | ||
}; | ||
const cookies = this.parseCookies(req); | ||
const defaults = this.defaults(); | ||
for (const i in defaults) { | ||
if (this.options[i] == null) { | ||
this.options[i] = defaults[i]; | ||
} | ||
} | ||
// set defaults | ||
this.options.cookieKey = (_a = this.options.cookieKey) !== null && _a !== void 0 ? _a : "__browser_fingerprint"; | ||
this.options.toSetCookie = (_b = this.options.toSetCookie) !== null && _b !== void 0 ? _b : true; | ||
this.options.onlyStaticElements = (_c = this.options.onlyStaticElements) !== null && _c !== void 0 ? _c : false; | ||
// early returns | ||
if (cookies[this.options.cookieKey] != null) { | ||
fingerprint = cookies[this.options.cookieKey]; | ||
return { | ||
fingerprint, | ||
elementHash: { clientCookie: fingerprint }, | ||
headersHash: {}, | ||
}; | ||
return { fingerprint, elementsHash, headersHash }; | ||
} | ||
if (req.headers[this.options.cookieKey] != null) { | ||
fingerprint = req.headers[this.options.cookieKey]; | ||
return { | ||
fingerprint, | ||
elementHash: { clientCookie: fingerprint }, | ||
headersHash: {}, | ||
}; | ||
return { fingerprint, elementsHash, headersHash }; | ||
} | ||
if (req.headers[("x-" + this.options.cookieKey).toLowerCase()] != null) { | ||
fingerprint = req.headers[("x-" + this.options.cookieKey).toLowerCase()]; | ||
return { | ||
fingerprint, | ||
elementHash: { clientCookie: fingerprint }, | ||
headersHash: {}, | ||
}; | ||
return { fingerprint, elementsHash, headersHash }; | ||
} | ||
@@ -67,3 +52,3 @@ let remoteAddress = req.headers["x-forwarded-for"]; | ||
} | ||
let elementsHash = { | ||
elementsHash = { | ||
httpVersion: req.httpVersion, | ||
@@ -91,3 +76,3 @@ remoteAddress: remoteAddress, | ||
key = "header_" + j; | ||
elementsHash[key] = req.headers[j]; | ||
elementsHash[key] = String(req.headers[j]); | ||
} | ||
@@ -97,3 +82,2 @@ //@ts-ignore | ||
fingerprint = this.calculateHashFromElements(elementsHash); | ||
let headersHash = {}; | ||
if (this.options.toSetCookie === true) { | ||
@@ -172,5 +156,4 @@ if (this.options.settings !== undefined && | ||
const shaSum = crypto.createHash("sha1"); | ||
for (const i in elementsHash) { | ||
shaSum.update(elementsHash[i]); | ||
} | ||
for (const i in elementsHash) | ||
shaSum.update(String(elementsHash[i])); | ||
return shaSum.digest("hex"); | ||
@@ -177,0 +160,0 @@ } |
@@ -1,14 +0,2 @@ | ||
#!/usr/bin/env node | ||
declare const http: any; | ||
declare const port = 8080; | ||
declare const BrowserFingerprint: any; | ||
declare const options: { | ||
cookieKey: string; | ||
toSetCookie: boolean; | ||
onlyStaticElements: boolean; | ||
settings: { | ||
path: string; | ||
expires: number; | ||
}; | ||
}; | ||
declare const fingerPrinter: any; | ||
#!/usr/bin/env ts-node | ||
export {}; |
@@ -1,2 +0,4 @@ | ||
#!/usr/bin/env node | ||
#!/usr/bin/env ts-node | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const http = require("http"); | ||
@@ -3,0 +5,0 @@ const port = 8080; |
@@ -5,3 +5,3 @@ { | ||
"description": "uniquely identify browsers", | ||
"version": "2.0.3", | ||
"version": "2.0.4", | ||
"license": "Apache-2.0", | ||
@@ -25,11 +25,11 @@ "repository": { | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"@types/jest": "^26.0.22", | ||
"@types/node": "^14.14.37", | ||
"jest": "^26.6.3", | ||
"prettier": "^2.2.1", | ||
"@types/jest": "^27.4.0", | ||
"@types/node": "^17.0.6", | ||
"@types/request-promise-native": "^1.0.18", | ||
"jest": "^27.4.5", | ||
"prettier": "^2.5.1", | ||
"request-promise-native": "^1.0.8", | ||
"ts-jest": "^26.5.4", | ||
"typescript": "^4.2.3" | ||
"ts-jest": "^27.1.2", | ||
"typescript": "^4.5.4" | ||
}, | ||
@@ -36,0 +36,0 @@ "scripts": { |
# Browser Fingerprint | ||
[![Build Status](https://circleci.com/gh/actionhero/browser_fingerprint.png)](https://circleci.com/gh/actionhero/browser_fingerprint.png) | ||
[![test](https://github.com/actionhero/browser_fingerprint/actions/workflows/test.yml/badge.svg)](https://github.com/actionhero/browser_fingerprint/actions/workflows/test.yml) | ||
@@ -5,0 +5,0 @@ [![NPM](https://nodei.co/npm/browser_fingerprint.png)](https://nodei.co/npm/browser_fingerprint/) |
@@ -24,10 +24,2 @@ import { IncomingMessage } from "http"; | ||
defaults() { | ||
return { | ||
cookieKey: "__browser_fingerprint", | ||
toSetCookie: true, | ||
onlyStaticElements: false, | ||
}; | ||
} | ||
/** | ||
@@ -38,22 +30,25 @@ * The goal is to come up with as many *potentially* unique traits for the connection and add them to the elementsHash. | ||
*/ | ||
fingerprint(req: IncomingMessage) { | ||
fingerprint(req: IncomingMessage): { | ||
fingerprint: string; | ||
headersHash: Record<string, string>; | ||
elementsHash: Record<string, string | number>; | ||
} { | ||
let fingerprint: string; | ||
let key: string; | ||
let value: string; | ||
let headersHash: Record<string, string> = {}; | ||
let elementsHash: Record<string, string | number> = { | ||
clientCookie: fingerprint, | ||
}; | ||
const cookies = this.parseCookies(req); | ||
const defaults = this.defaults(); | ||
for (const i in defaults) { | ||
if (this.options[i] == null) { | ||
this.options[i] = defaults[i]; | ||
} | ||
} | ||
// set defaults | ||
this.options.cookieKey = this.options.cookieKey ?? "__browser_fingerprint"; | ||
this.options.toSetCookie = this.options.toSetCookie ?? true; | ||
this.options.onlyStaticElements = this.options.onlyStaticElements ?? false; | ||
// early returns | ||
if (cookies[this.options.cookieKey] != null) { | ||
fingerprint = cookies[this.options.cookieKey]; | ||
return { | ||
fingerprint, | ||
elementHash: { clientCookie: fingerprint }, | ||
headersHash: {}, | ||
}; | ||
return { fingerprint, elementsHash, headersHash }; | ||
} | ||
@@ -63,7 +58,3 @@ | ||
fingerprint = req.headers[this.options.cookieKey] as string; | ||
return { | ||
fingerprint, | ||
elementHash: { clientCookie: fingerprint }, | ||
headersHash: {}, | ||
}; | ||
return { fingerprint, elementsHash, headersHash }; | ||
} | ||
@@ -75,7 +66,3 @@ | ||
] as string; | ||
return { | ||
fingerprint, | ||
elementHash: { clientCookie: fingerprint }, | ||
headersHash: {}, | ||
}; | ||
return { fingerprint, elementsHash, headersHash }; | ||
} | ||
@@ -88,3 +75,3 @@ | ||
let elementsHash = { | ||
elementsHash = { | ||
httpVersion: req.httpVersion, | ||
@@ -94,6 +81,6 @@ remoteAddress: remoteAddress as string, | ||
hashedHostName: this.hashedHostName(), | ||
remotePort: undefined, | ||
rand: undefined, | ||
time: undefined, | ||
hashedPid: undefined, | ||
remotePort: undefined as number, | ||
rand: undefined as number, | ||
time: undefined as number, | ||
hashedPid: undefined as string | number, | ||
}; | ||
@@ -117,3 +104,3 @@ | ||
key = "header_" + j; | ||
elementsHash[key] = req.headers[j]; | ||
elementsHash[key] = String(req.headers[j]); | ||
} | ||
@@ -125,3 +112,2 @@ | ||
let headersHash = {}; | ||
if (this.options.toSetCookie === true) { | ||
@@ -196,6 +182,6 @@ if ( | ||
parseCookies(req) { | ||
const cookies = {}; | ||
parseCookies(req: IncomingMessage) { | ||
const cookies: Record<string, string> = {}; | ||
if (req.headers.cookie != null) { | ||
req.headers.cookie.split(";").forEach((cookie) => { | ||
req.headers.cookie.split(";").forEach((cookie: string) => { | ||
const parts = cookie.split("="); | ||
@@ -209,8 +195,5 @@ cookies[parts[0].trim()] = (parts[1] || "").trim(); | ||
calculateHashFromElements(elementsHash) { | ||
calculateHashFromElements(elementsHash: Record<string, string | number>) { | ||
const shaSum = crypto.createHash("sha1"); | ||
for (const i in elementsHash) { | ||
shaSum.update(elementsHash[i]); | ||
} | ||
for (const i in elementsHash) shaSum.update(String(elementsHash[i])); | ||
return shaSum.digest("hex"); | ||
@@ -217,0 +200,0 @@ } |
@@ -1,4 +0,4 @@ | ||
#!/usr/bin/env node | ||
#!/usr/bin/env ts-node | ||
const http = require("http"); | ||
import * as http from "http"; | ||
const port = 8080; | ||
@@ -24,5 +24,4 @@ | ||
.createServer((req, res) => { | ||
const { fingerprint, elementHash, headersHash } = fingerPrinter.fingerprint( | ||
req | ||
); | ||
const { fingerprint, elementHash, headersHash } = | ||
fingerPrinter.fingerprint(req); | ||
headersHash["Content-Type"] = "text/plain"; // append any other headers you want | ||
@@ -29,0 +28,0 @@ res.writeHead(200, headersHash); |
@@ -6,5 +6,6 @@ { | ||
"module": "commonjs", | ||
"target": "es2018" | ||
"target": "es2018", | ||
"noImplicitAny": true | ||
}, | ||
"include": ["./src/**/*"] | ||
} |
38717
8
618