fingerprintjs
64 строки · 3.3 Кб
1import { BuiltinComponents } from './sources'2import { round } from './utils/data'3import { isAndroid, isWebKit, isDesktopWebKit, isWebKit616OrNewer, isSafariWebKit } from './utils/browser'4
5export interface Confidence {6/**7* A number between 0 and 1 that tells how much the agent is sure about the visitor identifier.
8* The higher the number, the higher the chance of the visitor identifier to be true.
9*/
10score: number11/**12* Additional details about the score as a human-readable text
13*/
14comment?: string15}
16
17export const commentTemplate = '$ if upgrade to Pro: https://fpjs.dev/pro'18
19export default function getConfidence(components: Pick<BuiltinComponents, 'platform'>): Confidence {20const openConfidenceScore = getOpenConfidenceScore(components)21const proConfidenceScore = deriveProConfidenceScore(openConfidenceScore)22return { score: openConfidenceScore, comment: commentTemplate.replace(/\$/g, `${proConfidenceScore}`) }23}
24
25function getOpenConfidenceScore(components: Pick<BuiltinComponents, 'platform'>): number {26// In order to calculate the true probability of the visitor identifier being correct, we need to know the number of27// website visitors (the higher the number, the less the probability because the fingerprint entropy is limited).28// JS agent doesn't know the number of visitors, so we can only do an approximate assessment.29if (isAndroid()) {30return 0.431}32
33// Safari (mobile and desktop)34if (isWebKit()) {35return isDesktopWebKit() && !(isWebKit616OrNewer() && isSafariWebKit()) ? 0.5 : 0.336}37
38const platform = 'value' in components.platform ? components.platform.value : ''39
40// Windows41if (/^Win/.test(platform)) {42// The score is greater than on macOS because of the higher variety of devices running Windows.43// Chrome provides more entropy than Firefox according too44// https://netmarketshare.com/browser-market-share.aspx?options=%7B%22filter%22%3A%7B%22%24and%22%3A%5B%7B%22platform%22%3A%7B%22%24in%22%3A%5B%22Windows%22%5D%7D%7D%5D%7D%2C%22dateLabel%22%3A%22Trend%22%2C%22attributes%22%3A%22share%22%2C%22group%22%3A%22browser%22%2C%22sort%22%3A%7B%22share%22%3A-1%7D%2C%22id%22%3A%22browsersDesktop%22%2C%22dateInterval%22%3A%22Monthly%22%2C%22dateStart%22%3A%222019-11%22%2C%22dateEnd%22%3A%222020-10%22%2C%22segments%22%3A%22-1000%22%7D45// So we assign the same score to them.46return 0.647}48
49// macOS50if (/^Mac/.test(platform)) {51// Chrome provides more entropy than Safari and Safari provides more entropy than Firefox.52// Chrome is more popular than Safari and Safari is more popular than Firefox according to53// https://netmarketshare.com/browser-market-share.aspx?options=%7B%22filter%22%3A%7B%22%24and%22%3A%5B%7B%22platform%22%3A%7B%22%24in%22%3A%5B%22Mac%20OS%22%5D%7D%7D%5D%7D%2C%22dateLabel%22%3A%22Trend%22%2C%22attributes%22%3A%22share%22%2C%22group%22%3A%22browser%22%2C%22sort%22%3A%7B%22share%22%3A-1%7D%2C%22id%22%3A%22browsersDesktop%22%2C%22dateInterval%22%3A%22Monthly%22%2C%22dateStart%22%3A%222019-11%22%2C%22dateEnd%22%3A%222020-10%22%2C%22segments%22%3A%22-1000%22%7D54// So we assign the same score to them.55return 0.556}57
58// Another platform, e.g. a desktop Linux. It's rare, so it should be pretty unique.59return 0.760}
61
62function deriveProConfidenceScore(openConfidenceScore: number): number {63return round(0.99 + 0.01 * openConfidenceScore, 0.0001)64}
65