-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #152 from fingerprintjs/dev
dev
- Loading branch information
Showing
13 changed files
with
152 additions
and
124 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,74 @@ | ||
import { | ||
AbstractDetectorDict, | ||
AbstractSourceDict, | ||
BotdError, | ||
BotDetectionResult, | ||
BotKind, | ||
Component, | ||
ComponentDict, | ||
DetectionDict, | ||
State, | ||
} from './types' | ||
|
||
export function detect<T extends ComponentDict, K extends AbstractDetectorDict<T>>( | ||
components: T, | ||
detectors: K, | ||
): [DetectionDict<K>, BotDetectionResult] { | ||
const detections = {} as DetectionDict<K> | ||
let finalDetection: BotDetectionResult = { | ||
bot: false, | ||
} | ||
|
||
for (const detectorName in detectors) { | ||
const detector = detectors[detectorName as keyof typeof detectors] | ||
const detectorRes = detector(components) | ||
|
||
let detection: BotDetectionResult = { bot: false } | ||
|
||
if (typeof detectorRes === 'string') { | ||
detection = { bot: true, botKind: detectorRes } | ||
} else if (detectorRes) { | ||
detection = { bot: true, botKind: BotKind.Unknown } | ||
} | ||
|
||
detections[detectorName as keyof typeof detectors] = detection | ||
|
||
if (detection.bot) { | ||
finalDetection = detection | ||
} | ||
} | ||
|
||
return [detections, finalDetection] | ||
} | ||
|
||
export async function collect<T extends AbstractSourceDict>(sources: T): Promise<ComponentDict<T>> { | ||
const components = {} as ComponentDict<T> | ||
const sourcesKeys = Object.keys(sources) as (keyof typeof sources)[] | ||
|
||
await Promise.all( | ||
sourcesKeys.map(async (sourceKey) => { | ||
const res = sources[sourceKey] | ||
|
||
try { | ||
components[sourceKey] = ({ | ||
value: await res(), | ||
state: State.Success, | ||
} as Component<any>) as any | ||
} catch (error) { | ||
if (error instanceof BotdError) { | ||
components[sourceKey] = { | ||
state: error.state, | ||
error: `${error.name}: ${error.message}`, | ||
} | ||
} else { | ||
components[sourceKey] = { | ||
state: State.UnexpectedBehaviour, | ||
error: error instanceof Error ? `${error.name}: ${error.message}` : String(error), | ||
} | ||
} | ||
} | ||
}), | ||
) | ||
|
||
return components | ||
} |
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 |
---|---|---|
@@ -1,18 +1,23 @@ | ||
import { arrayIncludes } from '../utils/ponyfills' | ||
import { BrowserEngineKind, BrowserKind, ComponentDict, DetectorResponse, State } from '../types' | ||
import { getBrowserEngineKind, getBrowserKind } from '../utils/browser' | ||
|
||
export function detectEvalLengthInconsistency({ evalLength }: ComponentDict): DetectorResponse { | ||
if (evalLength.state !== State.Success) return | ||
export function detectEvalLengthInconsistency({ | ||
evalLength, | ||
browserKind, | ||
browserEngineKind, | ||
}: ComponentDict): DetectorResponse { | ||
if ( | ||
evalLength.state !== State.Success || | ||
browserKind.state !== State.Success || | ||
browserEngineKind.state !== State.Success | ||
) | ||
return | ||
|
||
const length = evalLength.value | ||
const browser = getBrowserKind() | ||
const browserEngine = getBrowserEngineKind() | ||
if (browserEngine == BrowserEngineKind.Unknown) { | ||
return false | ||
} | ||
if (browserEngineKind.value === BrowserEngineKind.Unknown) return false | ||
return ( | ||
(length === 37 && !arrayIncludes([BrowserEngineKind.Webkit, BrowserEngineKind.Gecko], browserEngine)) || | ||
(length === 39 && !arrayIncludes([BrowserKind.IE], browser)) || | ||
(length === 33 && !arrayIncludes([BrowserEngineKind.Chromium], browserEngine)) | ||
(length === 37 && !arrayIncludes([BrowserEngineKind.Webkit, BrowserEngineKind.Gecko], browserEngineKind.value)) || | ||
(length === 39 && !arrayIncludes([BrowserKind.IE], browserKind.value)) || | ||
(length === 33 && !arrayIncludes([BrowserEngineKind.Chromium], browserEngineKind.value)) | ||
) | ||
} |
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 |
---|---|---|
@@ -1,10 +1,23 @@ | ||
import { BotKind, BrowserEngineKind, BrowserKind, ComponentDict, DetectorResponse, State } from '../types' | ||
import { getBrowserEngineKind, getBrowserKind, isAndroid } from '../utils/browser' | ||
|
||
export function detectPluginsLengthInconsistency({ pluginsLength }: ComponentDict): DetectorResponse { | ||
if (pluginsLength.state !== State.Success) return | ||
const browserKind = getBrowserKind() | ||
const browserEngineKind = getBrowserEngineKind() | ||
if (browserKind !== BrowserKind.Chrome || isAndroid() || browserEngineKind !== BrowserEngineKind.Chromium) return | ||
export function detectPluginsLengthInconsistency({ | ||
pluginsLength, | ||
android, | ||
browserKind, | ||
browserEngineKind, | ||
}: ComponentDict): DetectorResponse { | ||
if ( | ||
pluginsLength.state !== State.Success || | ||
android.state !== State.Success || | ||
browserKind.state !== State.Success || | ||
browserEngineKind.state !== State.Success | ||
) | ||
return | ||
if ( | ||
browserKind.value !== BrowserKind.Chrome || | ||
android.value || | ||
browserEngineKind.value !== BrowserEngineKind.Chromium | ||
) | ||
return | ||
if (pluginsLength.value === 0) return BotKind.HeadlessChrome | ||
} |
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 |
---|---|---|
@@ -1,9 +1,8 @@ | ||
import { BotKind, ComponentDict, DetectorResponse, State } from '../types' | ||
import { isAndroid } from '../utils/browser' | ||
|
||
export function detectRTT({ rtt }: ComponentDict): DetectorResponse { | ||
if (rtt.state !== State.Success) return | ||
export function detectRTT({ rtt, android }: ComponentDict): DetectorResponse { | ||
if (rtt.state !== State.Success || android.state !== State.Success) return | ||
// Rtt is 0 on android webview | ||
if (isAndroid()) return | ||
if (android.value) return | ||
if (rtt.value === 0) return BotKind.HeadlessChrome | ||
} |
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,10 +1,9 @@ | ||
import { BotKind, ComponentDict, DetectorResponse, State } from '../types' | ||
import { getDocumentFocus } from '../utils/browser' | ||
|
||
export function detectWindowSize({ windowSize }: ComponentDict): DetectorResponse { | ||
if (windowSize.state !== State.Success) return false | ||
export function detectWindowSize({ windowSize, documentFocus }: ComponentDict): DetectorResponse { | ||
if (windowSize.state !== State.Success || documentFocus.state !== State.Success) return false | ||
const { outerWidth, outerHeight } = windowSize.value | ||
// When a page is opened in a new tab without focusing it right away, the window outer size is 0x0 | ||
if (!getDocumentFocus()) return | ||
if (!documentFocus.value) return | ||
if (outerWidth === 0 && outerHeight === 0) return BotKind.HeadlessChrome | ||
} |
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
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
Oops, something went wrong.