camoucore 0.3.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +4 -3
- package/dist/camoufox/__main__.d.ts +10 -0
- package/dist/camoufox/__main__.js +1175 -0
- package/dist/camoufox/__version__.d.ts +31 -0
- package/dist/camoufox/__version__.js +32 -0
- package/dist/camoufox/_humanize_playwright.d.ts +54 -0
- package/dist/camoufox/_humanize_playwright.js +429 -0
- package/dist/camoufox/addons.d.ts +21 -0
- package/dist/camoufox/addons.js +56 -0
- package/dist/camoufox/async_api.d.ts +9 -0
- package/dist/camoufox/async_api.js +9 -0
- package/dist/camoufox/browser-pin.d.ts +46 -0
- package/dist/camoufox/browser-pin.js +96 -0
- package/dist/camoufox/cli.d.ts +7 -0
- package/dist/camoufox/cli.js +20 -0
- package/dist/camoufox/coherence.d.ts +141 -0
- package/dist/camoufox/coherence.js +708 -0
- package/dist/camoufox/data-files/audio-output-latency.json +15 -0
- package/dist/camoufox/data-files/browser-pin.json +7 -0
- package/dist/camoufox/data-files/display-profiles.json +6 -0
- package/dist/camoufox/data-files/essential-fonts.json +5 -0
- package/dist/camoufox/data-files/fingerprint-presets-v150.json +14544 -0
- package/dist/camoufox/data-files/fingerprint-presets.json +5192 -0
- package/dist/camoufox/data-files/font-bases.json +1888 -0
- package/dist/camoufox/data-files/font-groups.json +716 -0
- package/dist/camoufox/data-files/font-prefs.json +1046 -0
- package/dist/camoufox/data-files/fonts.json +240 -0
- package/dist/camoufox/data-files/fpgen.yml +44 -0
- package/dist/camoufox/data-files/launcher-constants.json +136 -0
- package/dist/camoufox/data-files/media-devices.json +180 -0
- package/dist/camoufox/data-files/repos.yml +40 -0
- package/dist/camoufox/data-files/voice-manifests.json +1864 -0
- package/dist/camoufox/data-files/voice-uris.json +188 -0
- package/dist/camoufox/data-files/warnings.yml +146 -0
- package/dist/camoufox/display_profile.d.ts +21 -0
- package/dist/camoufox/display_profile.js +129 -0
- package/dist/camoufox/driver_defaults.d.ts +27 -0
- package/dist/camoufox/driver_defaults.js +119 -0
- package/dist/camoufox/exceptions.d.ts +68 -0
- package/dist/camoufox/exceptions.js +122 -0
- package/dist/camoufox/fingerprints.d.ts +434 -0
- package/dist/camoufox/fingerprints.js +1936 -0
- package/dist/camoufox/gpudisplay.d.ts +158 -0
- package/dist/camoufox/gpudisplay.js +695 -0
- package/dist/camoufox/host_rendering.d.ts +38 -0
- package/dist/camoufox/host_rendering.js +180 -0
- package/dist/camoufox/humanize.d.ts +89 -0
- package/dist/camoufox/humanize.js +143 -0
- package/dist/camoufox/index.d.ts +21 -0
- package/dist/camoufox/index.js +21 -0
- package/dist/camoufox/ip.d.ts +45 -0
- package/dist/camoufox/ip.js +114 -0
- package/dist/camoufox/multiversion.d.ts +131 -0
- package/dist/camoufox/multiversion.js +490 -0
- package/dist/camoufox/node-floor.d.ts +5 -0
- package/dist/camoufox/node-floor.js +17 -0
- package/dist/camoufox/paths.d.ts +45 -0
- package/dist/camoufox/paths.js +62 -0
- package/dist/camoufox/pkgman.d.ts +361 -0
- package/dist/camoufox/pkgman.js +1066 -0
- package/dist/camoufox/product.d.ts +8 -0
- package/dist/camoufox/product.js +8 -0
- package/dist/camoufox/pycompat.d.ts +97 -0
- package/dist/camoufox/pycompat.js +458 -0
- package/dist/camoufox/server.d.ts +36 -0
- package/dist/camoufox/server.js +82 -0
- package/dist/camoufox/sync_api.d.ts +77 -0
- package/dist/camoufox/sync_api.js +207 -0
- package/dist/camoufox/utils.d.ts +638 -0
- package/dist/camoufox/utils.js +2507 -0
- package/dist/camoufox/warnings.d.ts +16 -0
- package/dist/camoufox/warnings.js +65 -0
- package/dist/camoufox/webgl.d.ts +52 -0
- package/dist/camoufox/webgl.js +264 -0
- package/dist/camouwolf/__main__.d.ts +10 -0
- package/dist/camouwolf/__main__.js +1180 -0
- package/dist/camouwolf/__version__.d.ts +31 -0
- package/dist/camouwolf/__version__.js +32 -0
- package/dist/camouwolf/_humanize_playwright.d.ts +53 -0
- package/dist/camouwolf/_humanize_playwright.js +427 -0
- package/dist/camouwolf/addons.d.ts +42 -0
- package/dist/camouwolf/addons.js +113 -0
- package/dist/camouwolf/async_api.d.ts +9 -0
- package/dist/camouwolf/async_api.js +9 -0
- package/dist/camouwolf/browser-pin.d.ts +46 -0
- package/dist/camouwolf/browser-pin.js +96 -0
- package/dist/camouwolf/cli.d.ts +7 -0
- package/dist/camouwolf/cli.js +20 -0
- package/dist/camouwolf/clienthints.d.ts +61 -0
- package/dist/camouwolf/clienthints.js +176 -0
- package/dist/camouwolf/coherence.d.ts +152 -0
- package/dist/camouwolf/coherence.js +554 -0
- package/dist/camouwolf/data-files/browser-pin.json +7 -0
- package/dist/camouwolf/data-files/camouwolf.defaults.json +118 -0
- package/dist/camouwolf/data-files/data/bundle-fonts.json +1983 -0
- package/dist/camouwolf/data-files/data/chrome-languages.json +517 -0
- package/dist/camouwolf/data-files/data/properties.json +321 -0
- package/dist/camouwolf/data-files/data/system-colors.json +35 -0
- package/dist/camouwolf/data-files/data/ubo.json +6 -0
- package/dist/camouwolf/data-files/fpgen.yml +41 -0
- package/dist/camouwolf/data-files/launcher-constants.json +26 -0
- package/dist/camouwolf/data-files/repos.yml +23 -0
- package/dist/camouwolf/data-files/warnings.yml +96 -0
- package/dist/camouwolf/driver_defaults.d.ts +25 -0
- package/dist/camouwolf/driver_defaults.js +114 -0
- package/dist/camouwolf/exceptions.d.ts +95 -0
- package/dist/camouwolf/exceptions.js +150 -0
- package/dist/camouwolf/fingerprints.d.ts +227 -0
- package/dist/camouwolf/fingerprints.js +753 -0
- package/dist/camouwolf/gpudisplay.d.ts +171 -0
- package/dist/camouwolf/gpudisplay.js +711 -0
- package/dist/camouwolf/host_rendering.d.ts +18 -0
- package/dist/camouwolf/host_rendering.js +114 -0
- package/dist/camouwolf/humanize.d.ts +84 -0
- package/dist/camouwolf/humanize.js +128 -0
- package/dist/camouwolf/index.d.ts +22 -0
- package/dist/camouwolf/index.js +21 -0
- package/dist/camouwolf/multiversion.d.ts +131 -0
- package/dist/camouwolf/multiversion.js +491 -0
- package/dist/camouwolf/node-floor.d.ts +5 -0
- package/dist/camouwolf/node-floor.js +17 -0
- package/dist/camouwolf/paths.d.ts +34 -0
- package/dist/camouwolf/paths.js +58 -0
- package/dist/camouwolf/pkgman.d.ts +331 -0
- package/dist/camouwolf/pkgman.js +972 -0
- package/dist/camouwolf/product.d.ts +3 -0
- package/dist/camouwolf/product.js +3 -0
- package/dist/camouwolf/pycompat.d.ts +92 -0
- package/dist/camouwolf/pycompat.js +461 -0
- package/dist/camouwolf/server.d.ts +36 -0
- package/dist/camouwolf/server.js +81 -0
- package/dist/camouwolf/switches.d.ts +77 -0
- package/dist/camouwolf/switches.js +222 -0
- package/dist/camouwolf/sync_api.d.ts +84 -0
- package/dist/camouwolf/sync_api.js +173 -0
- package/dist/camouwolf/utils.d.ts +644 -0
- package/dist/camouwolf/utils.js +2057 -0
- package/dist/camouwolf/warnings.d.ts +14 -0
- package/dist/camouwolf/warnings.js +59 -0
- package/dist/camouwolf/webgl.d.ts +41 -0
- package/dist/camouwolf/webgl.js +198 -0
- package/dist/data.d.ts +1 -1
- package/dist/data.js +3 -3
- package/dist/download.js +1 -1
- package/dist/exceptions.d.ts +1 -1
- package/dist/exceptions.js +1 -1
- package/dist/geolocation.js +1 -1
- package/dist/host.js +1 -1
- package/dist/ip.js +1 -1
- package/dist/locales.js +1 -1
- package/dist/mcp/cli.d.ts +15 -0
- package/dist/mcp/cli.js +108 -0
- package/dist/mcp/index.d.ts +8 -0
- package/dist/mcp/index.js +8 -0
- package/dist/mcp/main.d.ts +2 -0
- package/dist/mcp/main.js +8 -0
- package/dist/mcp/secrets.d.ts +50 -0
- package/dist/mcp/secrets.js +193 -0
- package/dist/mcp/server.d.ts +48 -0
- package/dist/mcp/server.js +653 -0
- package/dist/mcp/tree.d.ts +10 -0
- package/dist/mcp/tree.js +36 -0
- package/dist/passthrough.d.ts +1 -1
- package/dist/product.js +1 -1
- package/dist/settings.js +1 -1
- package/dist/telemetry.d.ts +0 -1
- package/dist/telemetry.js +5 -4
- package/dist/virtdisplay.d.ts +1 -1
- package/dist/virtdisplay.js +1 -1
- package/dist/warnings.js +1 -1
- package/package.json +57 -13
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The launcher entry points: Camoufox(), NewBrowser(), NewContext().
|
|
3
|
+
*
|
|
4
|
+
* TypeScript twin of python/camoucore/src/camoucore/camoufox/sync_api.py (and async_api.py, whose
|
|
5
|
+
* names async_api.ts re-exports). The Python library ships a sync and an
|
|
6
|
+
* async variant because Playwright-Python has two APIs; playwright-core has
|
|
7
|
+
* only one (promise-based), so the behaviours of both live here: the
|
|
8
|
+
* no-viewport default, the stock media defaults, CPU-core pinning with its
|
|
9
|
+
* launch lock (async_api), and virtual-display teardown.
|
|
10
|
+
*/
|
|
11
|
+
import { type Browser, type BrowserContext, type BrowserType } from "playwright-core";
|
|
12
|
+
import { type ProxyConfig } from "../ip.js";
|
|
13
|
+
import { type GpuDisplayOptions, type Headless, type LaunchOptions } from "./utils.js";
|
|
14
|
+
export type { Headless } from "./utils.js";
|
|
15
|
+
export interface NewBrowserOptions extends Omit<LaunchOptions, "headless">, GpuDisplayOptions {
|
|
16
|
+
/** Whether to run the browser headless. On Linux, `"virtual"` runs a real
|
|
17
|
+
* (non-headless) browser on an Xvfb display instead, and `"virtual-gpu"`
|
|
18
|
+
* on a headless Xorg server bound to one GPU. */
|
|
19
|
+
headless?: Headless;
|
|
20
|
+
/** A set of launch options generated by `launchOptions()` to use as-is. */
|
|
21
|
+
from_options?: Record<string, any>;
|
|
22
|
+
/** Whether to use a persistent context. */
|
|
23
|
+
persistent_context?: boolean;
|
|
24
|
+
/** Directory for the persistent profile (Playwright's user_data_dir). */
|
|
25
|
+
user_data_dir?: string;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Launches a Camoufox browser and returns it (Python: `with Camoufox(...) as
|
|
29
|
+
* browser`). Close it with `browser.close()`; a virtual display, if one was
|
|
30
|
+
* spawned, is torn down with it.
|
|
31
|
+
*/
|
|
32
|
+
export declare function Camoufox(options: NewBrowserOptions & {
|
|
33
|
+
persistent_context: true;
|
|
34
|
+
}): Promise<BrowserContext>;
|
|
35
|
+
export declare function Camoufox(options?: NewBrowserOptions): Promise<Browser>;
|
|
36
|
+
/**
|
|
37
|
+
* Launches a new browser instance for Camoufox given a set of launch options.
|
|
38
|
+
*
|
|
39
|
+
* `from_options`: a set of launch options generated by `launchOptions()` to
|
|
40
|
+
* use. `persistent_context`: whether to use a persistent context. Every other
|
|
41
|
+
* option is passed to `launchOptions()`.
|
|
42
|
+
*/
|
|
43
|
+
export declare function NewBrowser(playwright: BrowserType, options: NewBrowserOptions & {
|
|
44
|
+
persistent_context: true;
|
|
45
|
+
}): Promise<BrowserContext>;
|
|
46
|
+
export declare function NewBrowser(playwright: BrowserType, options?: NewBrowserOptions): Promise<Browser>;
|
|
47
|
+
export interface NewContextOptions extends Record<string, any> {
|
|
48
|
+
/** A fingerprint preset to use. If omitted, fpgen draws a new identity. */
|
|
49
|
+
preset?: Record<string, any>;
|
|
50
|
+
/** Target OS for the drawn identity ("windows", "macos", "linux"). On a
|
|
51
|
+
* browser launched without canvas noise, contexts claim the host's OS and
|
|
52
|
+
* GPU, and another os or preset throws ValueError. */
|
|
53
|
+
os?: string;
|
|
54
|
+
/** Firefox major version to claim in the UA. Defaults to the browser's own. */
|
|
55
|
+
ff_version?: string;
|
|
56
|
+
/** IPv4 or IPv6 address to spoof for WebRTC ICE candidates. */
|
|
57
|
+
webrtc_ip?: string;
|
|
58
|
+
/** Per-context proxy, in Playwright's format. */
|
|
59
|
+
proxy?: ProxyConfig;
|
|
60
|
+
/** Per-context geolocation. */
|
|
61
|
+
geolocation?: {
|
|
62
|
+
latitude: number;
|
|
63
|
+
longitude: number;
|
|
64
|
+
accuracy?: number;
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
/** snake_case -> camelCase, as camoufox.server.camel_case does. */
|
|
68
|
+
export declare function camelCase(snake: string): string;
|
|
69
|
+
/**
|
|
70
|
+
* Creates a new browser context with a unique fingerprint identity.
|
|
71
|
+
*
|
|
72
|
+
* Each context gets its own identity (navigator, screen, WebGL, fonts,
|
|
73
|
+
* voices), drawn by fpgen unless a preset is given, with its own audio noise
|
|
74
|
+
* seed. All values are applied via addInitScript so they self-destruct before
|
|
75
|
+
* page scripts can detect them.
|
|
76
|
+
*/
|
|
77
|
+
export declare function NewContext(browser: Browser, { preset, os, ff_version, webrtc_ip, proxy, geolocation, ...contextOptions }?: NewContextOptions): Promise<BrowserContext>;
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The launcher entry points: Camoufox(), NewBrowser(), NewContext().
|
|
3
|
+
*
|
|
4
|
+
* TypeScript twin of python/camoucore/src/camoucore/camoufox/sync_api.py (and async_api.py, whose
|
|
5
|
+
* names async_api.ts re-exports). The Python library ships a sync and an
|
|
6
|
+
* async variant because Playwright-Python has two APIs; playwright-core has
|
|
7
|
+
* only one (promise-based), so the behaviours of both live here: the
|
|
8
|
+
* no-viewport default, the stock media defaults, CPU-core pinning with its
|
|
9
|
+
* launch lock (async_api), and virtual-display teardown.
|
|
10
|
+
*/
|
|
11
|
+
import { ensureModel } from "fpgen-js";
|
|
12
|
+
import { firefox, } from "playwright-core";
|
|
13
|
+
import * as cpuAffinity from "../cpu_affinity.js";
|
|
14
|
+
import { ProxyHelper, proxyExitGeo } from "../ip.js";
|
|
15
|
+
import { SELECTOR } from "../locales.js";
|
|
16
|
+
import { attach } from "./_humanize_playwright.js";
|
|
17
|
+
import { attachDriverDefaults } from "./driver_defaults.js";
|
|
18
|
+
import { generateContextFingerprint } from "./fingerprints.js";
|
|
19
|
+
import { normalize } from "./humanize.js";
|
|
20
|
+
import { applyNoViewport, attachDesktopOnlyWarning, attachNoViewportDefault, attachPersistentGeometry, attachSharedTrafficWarning, attachSocks5Auth, attachStockMediaDefaults, attachViewportFit, attachVirtualDisplay, contextIdentity, contextScreenBound, driverPid, handOverContextGeometry, hostIdentity, launchGeometry, launchOptions, launchOs, pinnedCoreCount, STOCK_MEDIA_DEFAULTS, spoofsWindowDimensions, startVirtualDisplay, takeBackContextGeometry, } from "./utils.js";
|
|
21
|
+
export async function Camoufox(options = {}) {
|
|
22
|
+
return NewBrowser(firefox, options);
|
|
23
|
+
}
|
|
24
|
+
export async function NewBrowser(playwright, { headless, from_options, persistent_context, debug, ...kwargs } = {}) {
|
|
25
|
+
// humanize engines run in this process: checked before the launch,
|
|
26
|
+
// attached to the browser after it (also with from_options).
|
|
27
|
+
normalize(kwargs.humanize);
|
|
28
|
+
const [virtualDisplay, headlessBool] = await startVirtualDisplay(headless, debug, kwargs);
|
|
29
|
+
let fromOptions = from_options;
|
|
30
|
+
// Options the caller built leave its contexts to the caller
|
|
31
|
+
// (hostIdentity).
|
|
32
|
+
let canvasNoise = false;
|
|
33
|
+
try {
|
|
34
|
+
if (!fromOptions || !Object.keys(fromOptions).length) {
|
|
35
|
+
canvasNoise = kwargs.canvas_noise ?? undefined;
|
|
36
|
+
// Opt-in (2026-09-17). Pinning keeps the identity's core count by
|
|
37
|
+
// constraining the browser to that many cores; it costs real CPU,
|
|
38
|
+
// needs a launch lock, and does nothing on macOS. Off, the host's own
|
|
39
|
+
// snapped count is reported, so reported and measurable still agree.
|
|
40
|
+
kwargs.pin_cpu_cores ??= false;
|
|
41
|
+
fromOptions = await launchOptions({
|
|
42
|
+
...kwargs,
|
|
43
|
+
headless: headlessBool,
|
|
44
|
+
debug,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
// A failed launch must not leave the display behind.
|
|
50
|
+
virtualDisplay?.kill();
|
|
51
|
+
throw error;
|
|
52
|
+
}
|
|
53
|
+
// Playwright's default viewport deadlocks Juggler when the window is spoofed
|
|
54
|
+
// to a different size (daijro/camoufox#666), so default to no viewport.
|
|
55
|
+
const noViewportDefault = spoofsWindowDimensions(fromOptions);
|
|
56
|
+
// Pin the driver (and so the browser it is about to spawn) to as many cores
|
|
57
|
+
// as the identity reports, so measurable parallelism matches
|
|
58
|
+
// navigator.hardwareConcurrency; the driver gets its cores back afterwards.
|
|
59
|
+
const pinTo = pinnedCoreCount(fromOptions);
|
|
60
|
+
const launch = async () => attach(await launchWith(playwright, fromOptions, Boolean(persistent_context), noViewportDefault, virtualDisplay, canvasNoise), kwargs.humanize, launchOs(fromOptions), kwargs.i_know_what_im_doing);
|
|
61
|
+
try {
|
|
62
|
+
if (!pinTo) {
|
|
63
|
+
return await cpuAffinity.withUnpinnedLaunch(launch);
|
|
64
|
+
}
|
|
65
|
+
const pid = driverPid();
|
|
66
|
+
// The browser inherits the driver's mask at spawn, so two concurrent
|
|
67
|
+
// launches must not interleave pin/restore.
|
|
68
|
+
return await cpuAffinity.withPinLock(async () => {
|
|
69
|
+
const previous = cpuAffinity.pin(pid, pinTo);
|
|
70
|
+
try {
|
|
71
|
+
return await launch();
|
|
72
|
+
}
|
|
73
|
+
finally {
|
|
74
|
+
cpuAffinity.restore(pid, previous);
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
virtualDisplay?.kill();
|
|
80
|
+
throw error;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
async function launchWith(playwright, fromOptions, persistentContext, noViewportDefault, virtualDisplay, canvasNoise) {
|
|
84
|
+
// Persistent context. Python passes user_data_dir inside the options; the
|
|
85
|
+
// JS API takes it positionally. A user_data_dir alone also selects it.
|
|
86
|
+
const userDataDir = fromOptions.user_data_dir ?? fromOptions.userDataDir;
|
|
87
|
+
if (persistentContext || userDataDir !== undefined) {
|
|
88
|
+
let options = { ...fromOptions };
|
|
89
|
+
delete options.user_data_dir;
|
|
90
|
+
delete options.userDataDir;
|
|
91
|
+
if (noViewportDefault &&
|
|
92
|
+
!("viewport" in options || "noViewport" in options)) {
|
|
93
|
+
options = applyNoViewport(options);
|
|
94
|
+
}
|
|
95
|
+
else if ("noViewport" in options) {
|
|
96
|
+
options = applyNoViewport(options);
|
|
97
|
+
}
|
|
98
|
+
// The persistent context is created by the launch itself, so its media
|
|
99
|
+
// features come from these options rather than from newContext().
|
|
100
|
+
for (const [key, value] of Object.entries(STOCK_MEDIA_DEFAULTS)) {
|
|
101
|
+
if (!(key in options))
|
|
102
|
+
options[key] = value;
|
|
103
|
+
}
|
|
104
|
+
const context = await playwright.launchPersistentContext(userDataDir ?? "", options);
|
|
105
|
+
attachDriverDefaults(context);
|
|
106
|
+
attachPersistentGeometry(context, fromOptions);
|
|
107
|
+
return attachVirtualDisplay(context, virtualDisplay);
|
|
108
|
+
}
|
|
109
|
+
// Browser
|
|
110
|
+
const identity = await hostIdentity(fromOptions, canvasNoise);
|
|
111
|
+
const browser = await playwright.launch(fromOptions);
|
|
112
|
+
attachSocks5Auth(browser);
|
|
113
|
+
if (!fromOptions.proxy)
|
|
114
|
+
attachSharedTrafficWarning(browser);
|
|
115
|
+
attachViewportFit(browser, launchGeometry(fromOptions));
|
|
116
|
+
if (noViewportDefault) {
|
|
117
|
+
attachNoViewportDefault(browser);
|
|
118
|
+
}
|
|
119
|
+
attachStockMediaDefaults(browser);
|
|
120
|
+
attachDesktopOnlyWarning(browser);
|
|
121
|
+
attachDriverDefaults(browser);
|
|
122
|
+
browser._camoufoxHostIdentity = identity;
|
|
123
|
+
return attachVirtualDisplay(browser, virtualDisplay);
|
|
124
|
+
}
|
|
125
|
+
/** snake_case -> camelCase, as camoufox.server.camel_case does. */
|
|
126
|
+
export function camelCase(snake) {
|
|
127
|
+
if (snake.length < 2)
|
|
128
|
+
return snake;
|
|
129
|
+
const parts = snake.toLowerCase().split("_");
|
|
130
|
+
const joined = parts
|
|
131
|
+
.map((x) => (x ? x[0].toUpperCase() + x.slice(1) : ""))
|
|
132
|
+
.join("");
|
|
133
|
+
return ((snake[0] === "_" ? "_" : "") + joined[0].toLowerCase() + joined.slice(1));
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Creates a new browser context with a unique fingerprint identity.
|
|
137
|
+
*
|
|
138
|
+
* Each context gets its own identity (navigator, screen, WebGL, fonts,
|
|
139
|
+
* voices), drawn by fpgen unless a preset is given, with its own audio noise
|
|
140
|
+
* seed. All values are applied via addInitScript so they self-destruct before
|
|
141
|
+
* page scripts can detect them.
|
|
142
|
+
*/
|
|
143
|
+
export async function NewContext(browser, { preset, os, ff_version, webrtc_ip, proxy, geolocation, ...contextOptions } = {}) {
|
|
144
|
+
// Python's fpgen loads its model on import; here it is fetched on first use.
|
|
145
|
+
// launchOptions() awaits it, but a browser from connect() or a custom
|
|
146
|
+
// executable never went through launchOptions().
|
|
147
|
+
await ensureModel();
|
|
148
|
+
// The drawn UA carries fpgen's Firefox version, which must not disagree with
|
|
149
|
+
// the browser the page is actually talking to.
|
|
150
|
+
const ffVersion = ff_version || browser.version().split(".", 1)[0];
|
|
151
|
+
// Put a context behind a proxy where the proxy exits, as geoip places a
|
|
152
|
+
// launch: its WebRTC IP, clock, locale and position. A value the caller
|
|
153
|
+
// gave is kept.
|
|
154
|
+
let webrtcIp = webrtc_ip;
|
|
155
|
+
let position = geolocation;
|
|
156
|
+
if (proxy &&
|
|
157
|
+
!(webrtcIp &&
|
|
158
|
+
position &&
|
|
159
|
+
"timezoneId" in contextOptions &&
|
|
160
|
+
"locale" in contextOptions)) {
|
|
161
|
+
const exit = await proxyExitGeo(ProxyHelper.asString(proxy));
|
|
162
|
+
webrtcIp ||= exit.ip;
|
|
163
|
+
position ||= { latitude: exit.latitude, longitude: exit.longitude };
|
|
164
|
+
if (!("timezoneId" in contextOptions))
|
|
165
|
+
contextOptions.timezoneId = exit.timezone;
|
|
166
|
+
if (!("locale" in contextOptions))
|
|
167
|
+
contextOptions.locale = (await SELECTOR.fromRegion(exit.country)).asString;
|
|
168
|
+
}
|
|
169
|
+
const [contextOs, webglConfig] = contextIdentity(browser, os, preset);
|
|
170
|
+
const fp = generateContextFingerprint({
|
|
171
|
+
preset: preset,
|
|
172
|
+
os: contextOs,
|
|
173
|
+
ff_version: ffVersion,
|
|
174
|
+
webrtc_ip: webrtcIp,
|
|
175
|
+
webgl_config: webglConfig,
|
|
176
|
+
screen_bound: contextScreenBound(browser, contextOs, contextOptions),
|
|
177
|
+
});
|
|
178
|
+
// Merge the generated context options with user overrides (user wins). They
|
|
179
|
+
// are already Playwright's JS names (userAgent, deviceScaleFactor,
|
|
180
|
+
// timezoneId); re-casing them turned userAgent into `useragent`, which
|
|
181
|
+
// Playwright ignores, so the HTTP User-Agent contradicted navigator's.
|
|
182
|
+
const opts = {
|
|
183
|
+
...fp.context_options,
|
|
184
|
+
...contextOptions,
|
|
185
|
+
};
|
|
186
|
+
if (proxy)
|
|
187
|
+
opts.proxy = proxy;
|
|
188
|
+
if (position) {
|
|
189
|
+
opts.geolocation = position;
|
|
190
|
+
opts.permissions ??= ["geolocation"];
|
|
191
|
+
}
|
|
192
|
+
// A browser launched elsewhere takes SOCKS5 credentials too, and its
|
|
193
|
+
// contexts are fitted to their screens as well. The hand-over is read by
|
|
194
|
+
// the newContext() call below before it first awaits.
|
|
195
|
+
attachSocks5Auth(browser);
|
|
196
|
+
attachViewportFit(browser);
|
|
197
|
+
handOverContextGeometry(browser, fp);
|
|
198
|
+
let context;
|
|
199
|
+
try {
|
|
200
|
+
context = await browser.newContext(opts);
|
|
201
|
+
}
|
|
202
|
+
finally {
|
|
203
|
+
takeBackContextGeometry(browser);
|
|
204
|
+
}
|
|
205
|
+
await context.addInitScript(fp.init_script);
|
|
206
|
+
return context;
|
|
207
|
+
}
|