-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
api: randomize cipherlist for making requests to services (#574)
this makes cobalt less prone to TLS client fingerprinting, as it avoids having the default node.js TLS fingerprint that is shared by all node.js applications.
- Loading branch information
Showing
2 changed files
with
32 additions
and
0 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
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,28 @@ | ||
import tls from 'node:tls'; | ||
import { randomBytes } from 'node:crypto'; | ||
|
||
const ORIGINAL_CIPHERS = tls.DEFAULT_CIPHERS; | ||
|
||
// How many ciphers from the top of the list to shuffle. | ||
// The remaining ciphers are left in the original order. | ||
const TOP_N_SHUFFLE = 8; | ||
|
||
// Modified variation of https://stackoverflow.com/a/12646864 | ||
const shuffleArray = (array) => { | ||
for (let i = array.length - 1; i > 0; i--) { | ||
const j = randomBytes(4).readUint32LE() % array.length; | ||
[array[i], array[j]] = [array[j], array[i]]; | ||
} | ||
|
||
return array; | ||
} | ||
|
||
export const randomizeCiphers = () => { | ||
do { | ||
const cipherList = ORIGINAL_CIPHERS.split(':'); | ||
const shuffled = shuffleArray(cipherList.slice(0, TOP_N_SHUFFLE)); | ||
const retained = cipherList.slice(TOP_N_SHUFFLE); | ||
|
||
tls.DEFAULT_CIPHERS = [ ...shuffled, ...retained ].join(':'); | ||
} while (tls.DEFAULT_CIPHERS === ORIGINAL_CIPHERS); | ||
} |