bexxcv 1.0.8 → 1.1.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/README.md +70 -0
- package/dist/doctor.js +3 -1
- package/dist/human.d.ts +2 -0
- package/dist/human.js +12 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/locale.d.ts +1 -0
- package/dist/locale.js +18 -1
- package/dist/pagehooks.d.ts +6 -0
- package/dist/pagehooks.js +19 -0
- package/dist/plugins/brake.d.ts +8 -0
- package/dist/plugins/brake.js +74 -0
- package/dist/plugins/cocoon.d.ts +5 -0
- package/dist/plugins/cocoon.js +18 -0
- package/dist/plugins/dodge.d.ts +21 -0
- package/dist/plugins/dodge.js +103 -0
- package/dist/plugins/gauntlet.d.ts +20 -0
- package/dist/plugins/gauntlet.js +119 -0
- package/dist/plugins/human.d.ts +6 -0
- package/dist/plugins/human.js +38 -0
- package/dist/plugins/index.d.ts +32 -0
- package/dist/plugins/index.js +61 -0
- package/dist/plugins/lens.d.ts +20 -0
- package/dist/plugins/lens.js +135 -0
- package/dist/plugins/solvers.d.ts +5 -0
- package/dist/plugins/solvers.js +16 -0
- package/dist/plugins/stash.d.ts +8 -0
- package/dist/plugins/stash.js +109 -0
- package/dist/plugins/types.d.ts +13 -0
- package/dist/plugins/types.js +49 -0
- package/dist/plugins/veneer.d.ts +5 -0
- package/dist/plugins/veneer.js +37 -0
- package/dist/plugins/wiretap.d.ts +26 -0
- package/dist/plugins/wiretap.js +128 -0
- package/dist/realism.d.ts +4 -0
- package/dist/realism.js +96 -0
- package/dist/schema.js +99 -0
- package/dist/sync_api.d.ts +5 -2
- package/dist/sync_api.js +25 -2
- package/dist/turnstile.d.ts +12 -0
- package/dist/turnstile.js +70 -13
- package/dist/uam.d.ts +4 -0
- package/dist/uam.js +32 -6
- package/dist/utils.d.ts +2 -1
- package/dist/utils.js +17 -2
- package/package.json +1 -1
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { brake, type BrakeOptions } from "./brake.js";
|
|
2
|
+
import { cocoon } from "./cocoon.js";
|
|
3
|
+
import { dodge, type DodgeOptions } from "./dodge.js";
|
|
4
|
+
import { gauntlet, type GauntletOptions } from "./gauntlet.js";
|
|
5
|
+
import { human, type HumanPluginOptions } from "./human.js";
|
|
6
|
+
import { lens, type LensOptions } from "./lens.js";
|
|
7
|
+
import { stash, type StashOptions } from "./stash.js";
|
|
8
|
+
import type { BexxcvPlugin } from "./types.js";
|
|
9
|
+
import { veneer, type VeneerOptions } from "./veneer.js";
|
|
10
|
+
import { wiretap, type WiretapOptions } from "./wiretap.js";
|
|
11
|
+
export interface PluginOptions {
|
|
12
|
+
gauntlet?: boolean | GauntletOptions;
|
|
13
|
+
wiretap?: boolean | WiretapOptions;
|
|
14
|
+
stash?: string | StashOptions;
|
|
15
|
+
veneer?: boolean | VeneerOptions;
|
|
16
|
+
brake?: boolean | BrakeOptions;
|
|
17
|
+
dodge?: DodgeOptions;
|
|
18
|
+
cocoon?: string;
|
|
19
|
+
lens?: boolean | LensOptions;
|
|
20
|
+
human?: boolean | HumanPluginOptions;
|
|
21
|
+
plugins?: BexxcvPlugin[];
|
|
22
|
+
}
|
|
23
|
+
export declare const PLUGIN_KEYS: readonly ["gauntlet", "wiretap", "stash", "veneer", "brake", "dodge", "cocoon", "lens", "human", "plugins"];
|
|
24
|
+
export declare function buildPlugins(options: PluginOptions): BexxcvPlugin[];
|
|
25
|
+
export declare function stripPluginOptions<T extends Record<string, unknown>>(options: T): T;
|
|
26
|
+
export { applyPlugins, runBeforeLaunch, type BexxcvPlugin } from "./types.js";
|
|
27
|
+
export { auditFingerprint, lensReport } from "./lens.js";
|
|
28
|
+
export { gauntletResult, runGauntlet } from "./gauntlet.js";
|
|
29
|
+
export { wiretapEntries } from "./wiretap.js";
|
|
30
|
+
export { brake, cocoon, dodge, gauntlet, human, lens, stash, veneer, wiretap };
|
|
31
|
+
export { turnstile, uam } from "./solvers.js";
|
|
32
|
+
export type { HumanPluginOptions, BrakeOptions, DodgeOptions, GauntletOptions, LensOptions, StashOptions, VeneerOptions, WiretapOptions, };
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { brake } from "./brake.js";
|
|
2
|
+
import { cocoon } from "./cocoon.js";
|
|
3
|
+
import { dodge } from "./dodge.js";
|
|
4
|
+
import { gauntlet } from "./gauntlet.js";
|
|
5
|
+
import { human } from "./human.js";
|
|
6
|
+
import { lens } from "./lens.js";
|
|
7
|
+
import { stash } from "./stash.js";
|
|
8
|
+
import { veneer } from "./veneer.js";
|
|
9
|
+
import { wiretap } from "./wiretap.js";
|
|
10
|
+
export const PLUGIN_KEYS = [
|
|
11
|
+
"gauntlet",
|
|
12
|
+
"wiretap",
|
|
13
|
+
"stash",
|
|
14
|
+
"veneer",
|
|
15
|
+
"brake",
|
|
16
|
+
"dodge",
|
|
17
|
+
"cocoon",
|
|
18
|
+
"lens",
|
|
19
|
+
"human",
|
|
20
|
+
"plugins",
|
|
21
|
+
];
|
|
22
|
+
function opts(value) {
|
|
23
|
+
return value === true ? {} : value;
|
|
24
|
+
}
|
|
25
|
+
export function buildPlugins(options) {
|
|
26
|
+
const list = [];
|
|
27
|
+
if (options.dodge)
|
|
28
|
+
list.push(dodge(options.dodge));
|
|
29
|
+
if (options.cocoon)
|
|
30
|
+
list.push(cocoon({ dir: options.cocoon }));
|
|
31
|
+
if (options.stash) {
|
|
32
|
+
list.push(stash(typeof options.stash === "string" ? { file: options.stash } : options.stash));
|
|
33
|
+
}
|
|
34
|
+
if (options.wiretap)
|
|
35
|
+
list.push(wiretap(opts(options.wiretap)));
|
|
36
|
+
if (options.veneer)
|
|
37
|
+
list.push(veneer(opts(options.veneer)));
|
|
38
|
+
if (options.lens)
|
|
39
|
+
list.push(lens(opts(options.lens)));
|
|
40
|
+
if (options.gauntlet)
|
|
41
|
+
list.push(gauntlet(opts(options.gauntlet)));
|
|
42
|
+
if (options.human)
|
|
43
|
+
list.push(human(opts(options.human)));
|
|
44
|
+
if (options.brake)
|
|
45
|
+
list.push(brake(opts(options.brake)));
|
|
46
|
+
if (options.plugins)
|
|
47
|
+
list.push(...options.plugins);
|
|
48
|
+
return list;
|
|
49
|
+
}
|
|
50
|
+
export function stripPluginOptions(options) {
|
|
51
|
+
const copy = { ...options };
|
|
52
|
+
for (const key of PLUGIN_KEYS)
|
|
53
|
+
delete copy[key];
|
|
54
|
+
return copy;
|
|
55
|
+
}
|
|
56
|
+
export { applyPlugins, runBeforeLaunch } from "./types.js";
|
|
57
|
+
export { auditFingerprint, lensReport } from "./lens.js";
|
|
58
|
+
export { gauntletResult, runGauntlet } from "./gauntlet.js";
|
|
59
|
+
export { wiretapEntries } from "./wiretap.js";
|
|
60
|
+
export { brake, cocoon, dodge, gauntlet, human, lens, stash, veneer, wiretap };
|
|
61
|
+
export { turnstile, uam } from "./solvers.js";
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Page } from "playwright-core";
|
|
2
|
+
import type { BexxcvPlugin } from "./types.js";
|
|
3
|
+
export interface LensOptions {
|
|
4
|
+
audit?: boolean;
|
|
5
|
+
target?: "local";
|
|
6
|
+
quiet?: boolean;
|
|
7
|
+
out?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface LensCheck {
|
|
10
|
+
id: string;
|
|
11
|
+
ok: boolean;
|
|
12
|
+
detail: string;
|
|
13
|
+
}
|
|
14
|
+
export interface LensReport {
|
|
15
|
+
score: number;
|
|
16
|
+
checks: LensCheck[];
|
|
17
|
+
}
|
|
18
|
+
export declare function lensReport(page: Page): LensReport | null;
|
|
19
|
+
export declare function auditFingerprint(page: Page): Promise<LensReport>;
|
|
20
|
+
export declare function lens(options?: LensOptions): BexxcvPlugin;
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import { isKnownPhysical } from "../realism.js";
|
|
4
|
+
const REPORTS = new WeakMap();
|
|
5
|
+
const AUDITED = new WeakSet();
|
|
6
|
+
export function lensReport(page) {
|
|
7
|
+
return REPORTS.get(page) ?? null;
|
|
8
|
+
}
|
|
9
|
+
const PLAUSIBLE_CORES = new Set([2, 4, 6, 8, 10, 12, 14, 16, 20, 24, 32]);
|
|
10
|
+
export async function auditFingerprint(page) {
|
|
11
|
+
const probe = await page.evaluate(async () => {
|
|
12
|
+
let vendor = null;
|
|
13
|
+
let renderer = null;
|
|
14
|
+
try {
|
|
15
|
+
const canvas = document.createElement("canvas");
|
|
16
|
+
const gl = (canvas.getContext("webgl") ||
|
|
17
|
+
canvas.getContext("experimental-webgl"));
|
|
18
|
+
if (gl) {
|
|
19
|
+
const ext = gl.getExtension("WEBGL_debug_renderer_info");
|
|
20
|
+
vendor = String(gl.getParameter(ext ? ext.UNMASKED_VENDOR_WEBGL : gl.VENDOR));
|
|
21
|
+
renderer = String(gl.getParameter(ext ? ext.UNMASKED_RENDERER_WEBGL : gl.RENDERER));
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
vendor = null;
|
|
26
|
+
}
|
|
27
|
+
const natives = [
|
|
28
|
+
navigator.permissions && navigator.permissions.query,
|
|
29
|
+
HTMLCanvasElement.prototype.toDataURL,
|
|
30
|
+
Date.prototype.getTimezoneOffset,
|
|
31
|
+
Function.prototype.toString,
|
|
32
|
+
];
|
|
33
|
+
const nativeCode = natives.every((fn) => typeof fn !== "function" ||
|
|
34
|
+
/\[native code\]/.test(Function.prototype.toString.call(fn)));
|
|
35
|
+
let mediaDevices = null;
|
|
36
|
+
let mediaTimeout = false;
|
|
37
|
+
try {
|
|
38
|
+
if (navigator.mediaDevices && navigator.mediaDevices.enumerateDevices) {
|
|
39
|
+
const list = await Promise.race([
|
|
40
|
+
navigator.mediaDevices.enumerateDevices(),
|
|
41
|
+
new Promise((resolve) => setTimeout(() => resolve(null), 8000)),
|
|
42
|
+
]);
|
|
43
|
+
mediaDevices = list ? list.length : null;
|
|
44
|
+
mediaTimeout = list === null;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
mediaDevices = null;
|
|
49
|
+
}
|
|
50
|
+
const nav = navigator;
|
|
51
|
+
return {
|
|
52
|
+
ua: navigator.userAgent,
|
|
53
|
+
platform: navigator.platform,
|
|
54
|
+
language: navigator.language,
|
|
55
|
+
languages: Array.from(navigator.languages),
|
|
56
|
+
intlLocale: Intl.DateTimeFormat().resolvedOptions().locale,
|
|
57
|
+
cores: navigator.hardwareConcurrency,
|
|
58
|
+
webdriver: navigator.webdriver === true,
|
|
59
|
+
touch: navigator.maxTouchPoints,
|
|
60
|
+
dpr: window.devicePixelRatio,
|
|
61
|
+
screenW: screen.width,
|
|
62
|
+
screenH: screen.height,
|
|
63
|
+
innerW: window.innerWidth,
|
|
64
|
+
innerH: window.innerHeight,
|
|
65
|
+
outerW: window.outerWidth,
|
|
66
|
+
outerH: window.outerHeight,
|
|
67
|
+
vendor,
|
|
68
|
+
renderer,
|
|
69
|
+
nativeCode,
|
|
70
|
+
hasChrome: "chrome" in window,
|
|
71
|
+
hasUaData: "userAgentData" in navigator,
|
|
72
|
+
pdfViewer: typeof nav.pdfViewerEnabled === "boolean" ? nav.pdfViewerEnabled : null,
|
|
73
|
+
focused: document.hasFocus(),
|
|
74
|
+
visible: document.visibilityState === "visible",
|
|
75
|
+
mediaDevices,
|
|
76
|
+
mediaTimeout,
|
|
77
|
+
};
|
|
78
|
+
});
|
|
79
|
+
const checks = [];
|
|
80
|
+
const add = (id, ok, detail) => checks.push({ id, ok, detail });
|
|
81
|
+
const firefox = /Firefox\//.test(probe.ua);
|
|
82
|
+
const desktop = !/Android|Mobile|iPhone|iPad/i.test(probe.ua);
|
|
83
|
+
add("webdriver", !probe.webdriver, `navigator.webdriver is ${probe.webdriver}`);
|
|
84
|
+
const platformOk = (/Windows/i.test(probe.ua) && /^Win/i.test(probe.platform)) ||
|
|
85
|
+
(/Macintosh|Mac OS/i.test(probe.ua) && /^Mac/i.test(probe.platform)) ||
|
|
86
|
+
(/Linux|X11/i.test(probe.ua) && !/Windows|Macintosh/i.test(probe.ua) && /Linux|X11/i.test(probe.platform));
|
|
87
|
+
add("ua-platform", platformOk, `${probe.platform} against the user agent`);
|
|
88
|
+
add("language", probe.languages[0] === probe.language, `language ${probe.language}, first of languages ${probe.languages[0]}`);
|
|
89
|
+
add("locale-intl", probe.intlLocale.split("-")[0].toLowerCase() === probe.language.split("-")[0].toLowerCase(), `Intl locale ${probe.intlLocale}, navigator language ${probe.language}`);
|
|
90
|
+
add("dpr-screen", isKnownPhysical(probe.screenW * probe.dpr, probe.screenH * probe.dpr), `${probe.screenW}x${probe.screenH} at DPR ${probe.dpr}`);
|
|
91
|
+
add("window-size", probe.innerW <= probe.outerW && probe.innerH <= probe.outerH && probe.outerW <= probe.screenW + 16, `inner ${probe.innerW}x${probe.innerH}, outer ${probe.outerW}x${probe.outerH}, screen ${probe.screenW}x${probe.screenH}`);
|
|
92
|
+
add("cores", PLAUSIBLE_CORES.has(probe.cores), `${probe.cores} logical cores`);
|
|
93
|
+
add("touch", !desktop || probe.touch === 0, `maxTouchPoints ${probe.touch}`);
|
|
94
|
+
add("webgl", probe.vendor !== null && probe.renderer !== null, probe.renderer ? `${probe.vendor} / ${probe.renderer}` : "WebGL is not available");
|
|
95
|
+
add("webgl-software", !(probe.renderer && /llvmpipe|swiftshader|software|mesa offscreen/i.test(probe.renderer)), probe.renderer ?? "no renderer");
|
|
96
|
+
add("native-code", probe.nativeCode, probe.nativeCode ? "core APIs are native" : "a core API was replaced by JavaScript");
|
|
97
|
+
add("ua-hints", firefox ? !probe.hasUaData : probe.hasUaData, `userAgentData ${probe.hasUaData ? "present" : "absent"} for a ${firefox ? "Firefox" : "non-Firefox"} user agent`);
|
|
98
|
+
add("chrome-object", firefox ? !probe.hasChrome : true, `window.chrome ${probe.hasChrome ? "present" : "absent"}`);
|
|
99
|
+
add("pdf-viewer", !firefox || probe.pdfViewer === true, `pdfViewerEnabled ${probe.pdfViewer}`);
|
|
100
|
+
add("focus", probe.focused && probe.visible, `focused ${probe.focused}, visible ${probe.visible}`);
|
|
101
|
+
add("media-devices", (probe.mediaDevices === null || probe.mediaDevices > 0), probe.mediaTimeout
|
|
102
|
+
? "enumerateDevices timeout (headless fallback, non-fatal)"
|
|
103
|
+
: `${probe.mediaDevices} media devices`);
|
|
104
|
+
const passed = checks.filter((check) => check.ok).length;
|
|
105
|
+
return { score: Math.round((passed / checks.length) * 100), checks };
|
|
106
|
+
}
|
|
107
|
+
export function lens(options = {}) {
|
|
108
|
+
return {
|
|
109
|
+
name: "lens",
|
|
110
|
+
order: 40,
|
|
111
|
+
onPage(page) {
|
|
112
|
+
if (options.audit === false || AUDITED.has(page))
|
|
113
|
+
return;
|
|
114
|
+
AUDITED.add(page);
|
|
115
|
+
page.once("load", () => {
|
|
116
|
+
void auditFingerprint(page)
|
|
117
|
+
.then((report) => {
|
|
118
|
+
REPORTS.set(page, report);
|
|
119
|
+
if (!options.quiet) {
|
|
120
|
+
console.log(`[bexxcv] lens: fingerprint score ${report.score}/100`);
|
|
121
|
+
for (const check of report.checks) {
|
|
122
|
+
if (!check.ok)
|
|
123
|
+
console.log(`[bexxcv] lens: ${check.id} failed, ${check.detail}`);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if (options.out) {
|
|
127
|
+
fs.mkdirSync(path.dirname(path.resolve(options.out)), { recursive: true });
|
|
128
|
+
fs.writeFileSync(options.out, `${JSON.stringify(report, null, 2)}\n`);
|
|
129
|
+
}
|
|
130
|
+
})
|
|
131
|
+
.catch(() => undefined);
|
|
132
|
+
});
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type TurnstileOptions } from "../turnstile.js";
|
|
2
|
+
import { type UamOptions } from "../uam.js";
|
|
3
|
+
import type { BexxcvPlugin } from "./types.js";
|
|
4
|
+
export declare function turnstile(options?: TurnstileOptions): BexxcvPlugin;
|
|
5
|
+
export declare function uam(options?: UamOptions): BexxcvPlugin;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { watchTurnstile } from "../turnstile.js";
|
|
2
|
+
import { watchUam } from "../uam.js";
|
|
3
|
+
export function turnstile(options = {}) {
|
|
4
|
+
return {
|
|
5
|
+
name: "turnstile",
|
|
6
|
+
order: 45,
|
|
7
|
+
onPage: (page) => watchTurnstile(page, options),
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
export function uam(options = {}) {
|
|
11
|
+
return {
|
|
12
|
+
name: "uam",
|
|
13
|
+
order: 45,
|
|
14
|
+
onPage: (page) => watchUam(page, options),
|
|
15
|
+
};
|
|
16
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
function domainKey(cookie) {
|
|
4
|
+
return cookie.domain.replace(/^\./, "").toLowerCase();
|
|
5
|
+
}
|
|
6
|
+
function expired(cookie) {
|
|
7
|
+
return cookie.expires > 0 && cookie.expires < Date.now() / 1000;
|
|
8
|
+
}
|
|
9
|
+
function readFile(file) {
|
|
10
|
+
try {
|
|
11
|
+
const raw = JSON.parse(fs.readFileSync(file, "utf-8"));
|
|
12
|
+
if (raw && typeof raw === "object" && raw.domains)
|
|
13
|
+
return raw;
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
return { version: 1, saved: new Date(0).toISOString(), domains: {} };
|
|
17
|
+
}
|
|
18
|
+
return { version: 1, saved: new Date(0).toISOString(), domains: {} };
|
|
19
|
+
}
|
|
20
|
+
export function stash(input) {
|
|
21
|
+
const options = typeof input === "string" ? { file: input } : input;
|
|
22
|
+
const file = path.resolve(options.file);
|
|
23
|
+
const names = options.names ? new Set(options.names) : null;
|
|
24
|
+
const domains = options.domains?.map((d) => d.toLowerCase().replace(/^\./, "")) ?? null;
|
|
25
|
+
const known = new Set();
|
|
26
|
+
let timer = null;
|
|
27
|
+
let launchProxy = "direct";
|
|
28
|
+
const contextProxy = new WeakMap();
|
|
29
|
+
const wanted = (cookie) => {
|
|
30
|
+
if (names && !names.has(cookie.name))
|
|
31
|
+
return false;
|
|
32
|
+
if (domains) {
|
|
33
|
+
const key = domainKey(cookie);
|
|
34
|
+
if (!domains.some((d) => key === d || key.endsWith(`.${d}`)))
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
return true;
|
|
38
|
+
};
|
|
39
|
+
const BOUND = new Set(["cf_clearance"]);
|
|
40
|
+
const save = async (contexts) => {
|
|
41
|
+
const data = readFile(file);
|
|
42
|
+
for (const context of contexts) {
|
|
43
|
+
const grouped = {};
|
|
44
|
+
for (const cookie of await context.cookies()) {
|
|
45
|
+
if (!wanted(cookie) || expired(cookie))
|
|
46
|
+
continue;
|
|
47
|
+
(grouped[domainKey(cookie)] ??= []).push(cookie);
|
|
48
|
+
}
|
|
49
|
+
for (const [key, list] of Object.entries(grouped)) {
|
|
50
|
+
data.domains[key] = list;
|
|
51
|
+
(data.bound ??= {})[key] = contextProxy.get(context) ?? launchProxy;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
for (const [key, list] of Object.entries(data.domains)) {
|
|
55
|
+
const alive = list.filter((cookie) => !expired(cookie));
|
|
56
|
+
if (alive.length > 0)
|
|
57
|
+
data.domains[key] = alive;
|
|
58
|
+
else
|
|
59
|
+
delete data.domains[key];
|
|
60
|
+
}
|
|
61
|
+
data.saved = new Date().toISOString();
|
|
62
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
63
|
+
fs.writeFileSync(file, `${JSON.stringify(data, null, 2)}\n`, { mode: 0o600 });
|
|
64
|
+
};
|
|
65
|
+
return {
|
|
66
|
+
name: "stash",
|
|
67
|
+
order: 20,
|
|
68
|
+
beforeLaunch(launch) {
|
|
69
|
+
const proxy = launch.proxy;
|
|
70
|
+
if (typeof proxy === "string")
|
|
71
|
+
launchProxy = proxy;
|
|
72
|
+
else if (proxy && proxy.server)
|
|
73
|
+
launchProxy = proxy.server;
|
|
74
|
+
},
|
|
75
|
+
async onContext(context, contextOptions) {
|
|
76
|
+
known.add(context);
|
|
77
|
+
const current = contextOptions.proxy?.server ?? launchProxy;
|
|
78
|
+
contextProxy.set(context, current);
|
|
79
|
+
const data = readFile(file);
|
|
80
|
+
const cookies = Object.entries(data.domains).flatMap(([key, list]) => list.filter((cookie) => {
|
|
81
|
+
if (!wanted(cookie) || expired(cookie))
|
|
82
|
+
return false;
|
|
83
|
+
if (BOUND.has(cookie.name) && (data.bound?.[key] ?? "direct") !== current)
|
|
84
|
+
return false;
|
|
85
|
+
return true;
|
|
86
|
+
}));
|
|
87
|
+
if (cookies.length > 0) {
|
|
88
|
+
try {
|
|
89
|
+
await context.addCookies(cookies);
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
console.warn(`[bexxcv] stash could not load cookies: ${error.message}`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (options.saveEvery && !timer) {
|
|
96
|
+
timer = setInterval(() => {
|
|
97
|
+
void save([...known]).catch(() => undefined);
|
|
98
|
+
}, options.saveEvery);
|
|
99
|
+
timer.unref();
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
async beforeClose(contexts) {
|
|
103
|
+
if (timer)
|
|
104
|
+
clearInterval(timer);
|
|
105
|
+
timer = null;
|
|
106
|
+
await save(contexts);
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Browser, BrowserContext, Page } from "playwright-core";
|
|
2
|
+
import { type ContextOptions } from "../pagehooks.js";
|
|
3
|
+
export interface BexxcvPlugin {
|
|
4
|
+
name: string;
|
|
5
|
+
order?: number;
|
|
6
|
+
beforeLaunch?(options: Record<string, unknown>): void | Promise<void>;
|
|
7
|
+
beforeContext?(options: ContextOptions): void | Promise<void>;
|
|
8
|
+
onContext?(context: BrowserContext, options: ContextOptions): void | Promise<void>;
|
|
9
|
+
onPage?(page: Page, context: BrowserContext): void;
|
|
10
|
+
beforeClose?(contexts: BrowserContext[]): void | Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
export declare function runBeforeLaunch(plugins: BexxcvPlugin[], options: Record<string, unknown>): Promise<void>;
|
|
13
|
+
export declare function applyPlugins<T extends Browser | BrowserContext>(target: T, plugins: BexxcvPlugin[]): Promise<T>;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { onEachContext, onEachPage, } from "../pagehooks.js";
|
|
2
|
+
const DEFAULT_ORDER = 50;
|
|
3
|
+
function sortPlugins(plugins) {
|
|
4
|
+
return [...plugins].sort((a, b) => (a.order ?? DEFAULT_ORDER) - (b.order ?? DEFAULT_ORDER));
|
|
5
|
+
}
|
|
6
|
+
export async function runBeforeLaunch(plugins, options) {
|
|
7
|
+
for (const plugin of sortPlugins(plugins)) {
|
|
8
|
+
await plugin.beforeLaunch?.(options);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export async function applyPlugins(target, plugins) {
|
|
12
|
+
const sorted = sortPlugins(plugins);
|
|
13
|
+
await onEachContext(target, {
|
|
14
|
+
before: async (options) => {
|
|
15
|
+
for (const plugin of sorted)
|
|
16
|
+
await plugin.beforeContext?.(options);
|
|
17
|
+
},
|
|
18
|
+
after: async (context, options) => {
|
|
19
|
+
for (const plugin of sorted)
|
|
20
|
+
await plugin.onContext?.(context, options);
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
for (const plugin of sorted) {
|
|
24
|
+
if (!plugin.onPage)
|
|
25
|
+
continue;
|
|
26
|
+
const handler = plugin.onPage.bind(plugin);
|
|
27
|
+
onEachPage(target, (page) => handler(page, page.context()));
|
|
28
|
+
}
|
|
29
|
+
const closers = sorted.filter((plugin) => plugin.beforeClose);
|
|
30
|
+
if (closers.length > 0) {
|
|
31
|
+
const holder = target;
|
|
32
|
+
const originalClose = holder.close.bind(target);
|
|
33
|
+
holder.close = async (...args) => {
|
|
34
|
+
const contexts = typeof target.contexts === "function"
|
|
35
|
+
? target.contexts()
|
|
36
|
+
: [target];
|
|
37
|
+
for (const plugin of [...closers].reverse()) {
|
|
38
|
+
try {
|
|
39
|
+
await plugin.beforeClose?.(contexts);
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
console.warn(`[bexxcv] plugin "${plugin.name}" failed on close: ${error.message}`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return originalClose(...args);
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
return target;
|
|
49
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export function veneer(options = {}) {
|
|
2
|
+
let warned = false;
|
|
3
|
+
return {
|
|
4
|
+
name: "veneer",
|
|
5
|
+
order: 30,
|
|
6
|
+
async onContext(context) {
|
|
7
|
+
if (options.strict) {
|
|
8
|
+
await context.route("**/*", async (route) => {
|
|
9
|
+
const headers = route.request().headers();
|
|
10
|
+
if (/firefox/i.test(headers["user-agent"] ?? "")) {
|
|
11
|
+
const cleaned = {};
|
|
12
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
13
|
+
if (!key.toLowerCase().startsWith("sec-ch-ua"))
|
|
14
|
+
cleaned[key] = value;
|
|
15
|
+
}
|
|
16
|
+
if (Object.keys(cleaned).length !== Object.keys(headers).length) {
|
|
17
|
+
await route.continue({ headers: cleaned });
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
await route.continue();
|
|
22
|
+
});
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
context.on("request", (request) => {
|
|
26
|
+
if (warned)
|
|
27
|
+
return;
|
|
28
|
+
const headers = request.headers();
|
|
29
|
+
if (/firefox/i.test(headers["user-agent"] ?? "") &&
|
|
30
|
+
Object.keys(headers).some((key) => key.toLowerCase().startsWith("sec-ch-ua"))) {
|
|
31
|
+
warned = true;
|
|
32
|
+
console.warn("[bexxcv] veneer: a Firefox user agent is sending sec-ch-ua headers, something upstream is injecting them. Use veneer: { strict: true } to strip them.");
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { BrowserContext } from "playwright-core";
|
|
2
|
+
import type { BexxcvPlugin } from "./types.js";
|
|
3
|
+
export interface WiretapOptions {
|
|
4
|
+
match?: string[];
|
|
5
|
+
methods?: string[];
|
|
6
|
+
out?: string;
|
|
7
|
+
bodies?: boolean;
|
|
8
|
+
maxBodyBytes?: number;
|
|
9
|
+
streamTimeout?: number;
|
|
10
|
+
redact?: boolean;
|
|
11
|
+
}
|
|
12
|
+
export interface WiretapEntry {
|
|
13
|
+
time: string;
|
|
14
|
+
method: string;
|
|
15
|
+
url: string;
|
|
16
|
+
status: number;
|
|
17
|
+
requestHeaders: Record<string, string>;
|
|
18
|
+
postData: string | null;
|
|
19
|
+
responseHeaders: Record<string, string>;
|
|
20
|
+
contentType: string;
|
|
21
|
+
body: string | null;
|
|
22
|
+
bodyNote?: string;
|
|
23
|
+
events?: string[];
|
|
24
|
+
}
|
|
25
|
+
export declare function wiretapEntries(context: BrowserContext): WiretapEntry[];
|
|
26
|
+
export declare function wiretap(input?: string | string[] | WiretapOptions): BexxcvPlugin;
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
const SENSITIVE = new Set([
|
|
4
|
+
"cookie",
|
|
5
|
+
"set-cookie",
|
|
6
|
+
"authorization",
|
|
7
|
+
"proxy-authorization",
|
|
8
|
+
"x-api-key",
|
|
9
|
+
]);
|
|
10
|
+
const STORES = new WeakMap();
|
|
11
|
+
export function wiretapEntries(context) {
|
|
12
|
+
return STORES.get(context) ?? [];
|
|
13
|
+
}
|
|
14
|
+
function scrub(headers, redact) {
|
|
15
|
+
if (!redact)
|
|
16
|
+
return { ...headers };
|
|
17
|
+
const out = {};
|
|
18
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
19
|
+
out[key] = SENSITIVE.has(key.toLowerCase()) ? "[redacted]" : value;
|
|
20
|
+
}
|
|
21
|
+
return out;
|
|
22
|
+
}
|
|
23
|
+
function matcher(patterns) {
|
|
24
|
+
if (!patterns || patterns.length === 0)
|
|
25
|
+
return () => true;
|
|
26
|
+
const tests = patterns.map((pattern) => {
|
|
27
|
+
if (pattern.startsWith("re:")) {
|
|
28
|
+
const re = new RegExp(pattern.slice(3));
|
|
29
|
+
return (url) => re.test(url);
|
|
30
|
+
}
|
|
31
|
+
return (url) => url.includes(pattern);
|
|
32
|
+
});
|
|
33
|
+
return (url) => tests.some((test) => test(url));
|
|
34
|
+
}
|
|
35
|
+
function withTimeout(promise, ms) {
|
|
36
|
+
return new Promise((resolve, reject) => {
|
|
37
|
+
const timer = setTimeout(() => reject(new Error("timeout")), ms);
|
|
38
|
+
promise.then((value) => {
|
|
39
|
+
clearTimeout(timer);
|
|
40
|
+
resolve(value);
|
|
41
|
+
}, (error) => {
|
|
42
|
+
clearTimeout(timer);
|
|
43
|
+
reject(error);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
export function wiretap(input = {}) {
|
|
48
|
+
const options = typeof input === "string"
|
|
49
|
+
? { match: [input] }
|
|
50
|
+
: Array.isArray(input)
|
|
51
|
+
? { match: input }
|
|
52
|
+
: input;
|
|
53
|
+
const match = matcher(options.match);
|
|
54
|
+
const methods = new Set((options.methods ?? []).map((m) => m.toUpperCase()));
|
|
55
|
+
const redact = options.redact ?? true;
|
|
56
|
+
const bodies = options.bodies ?? true;
|
|
57
|
+
const maxBytes = options.maxBodyBytes ?? 1_000_000;
|
|
58
|
+
const streamTimeout = options.streamTimeout ?? 60_000;
|
|
59
|
+
const pending = new Set();
|
|
60
|
+
const capture = async (response, list) => {
|
|
61
|
+
const request = response.request();
|
|
62
|
+
if (methods.size > 0 && !methods.has(request.method()))
|
|
63
|
+
return;
|
|
64
|
+
const url = response.url();
|
|
65
|
+
if (!match(url))
|
|
66
|
+
return;
|
|
67
|
+
const responseHeaders = response.headers();
|
|
68
|
+
const contentType = responseHeaders["content-type"] ?? "";
|
|
69
|
+
const entry = {
|
|
70
|
+
time: new Date().toISOString(),
|
|
71
|
+
method: request.method(),
|
|
72
|
+
url,
|
|
73
|
+
status: response.status(),
|
|
74
|
+
requestHeaders: scrub(request.headers(), redact),
|
|
75
|
+
postData: request.postData(),
|
|
76
|
+
responseHeaders: scrub(responseHeaders, redact),
|
|
77
|
+
contentType,
|
|
78
|
+
body: null,
|
|
79
|
+
};
|
|
80
|
+
if (bodies) {
|
|
81
|
+
const stream = /event-stream/i.test(contentType);
|
|
82
|
+
try {
|
|
83
|
+
const text = await withTimeout(response.text(), stream ? streamTimeout : 10_000);
|
|
84
|
+
if (Buffer.byteLength(text) > maxBytes) {
|
|
85
|
+
entry.body = text.slice(0, maxBytes);
|
|
86
|
+
entry.bodyNote = "truncated";
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
entry.body = text;
|
|
90
|
+
}
|
|
91
|
+
if (stream) {
|
|
92
|
+
entry.events = text
|
|
93
|
+
.split(/\r?\n\r?\n/)
|
|
94
|
+
.map((chunk) => chunk.trim())
|
|
95
|
+
.filter(Boolean);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
entry.bodyNote = error.message;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
list.push(entry);
|
|
103
|
+
};
|
|
104
|
+
const save = async (contexts) => {
|
|
105
|
+
if (!options.out)
|
|
106
|
+
return;
|
|
107
|
+
await Promise.allSettled([...pending]);
|
|
108
|
+
const entries = contexts.flatMap((context) => wiretapEntries(context));
|
|
109
|
+
entries.sort((a, b) => a.time.localeCompare(b.time));
|
|
110
|
+
fs.mkdirSync(path.dirname(path.resolve(options.out)), { recursive: true });
|
|
111
|
+
fs.writeFileSync(options.out, `${JSON.stringify({ captured: entries.length, entries }, null, 2)}\n`, { mode: 0o600 });
|
|
112
|
+
};
|
|
113
|
+
return {
|
|
114
|
+
name: "wiretap",
|
|
115
|
+
order: 30,
|
|
116
|
+
onContext(context) {
|
|
117
|
+
const list = [];
|
|
118
|
+
STORES.set(context, list);
|
|
119
|
+
context.on("response", (response) => {
|
|
120
|
+
const task = capture(response, list)
|
|
121
|
+
.catch(() => undefined)
|
|
122
|
+
.finally(() => pending.delete(task));
|
|
123
|
+
pending.add(task);
|
|
124
|
+
});
|
|
125
|
+
},
|
|
126
|
+
beforeClose: save,
|
|
127
|
+
};
|
|
128
|
+
}
|
package/dist/realism.d.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
1
|
export declare function isKnownPhysical(width: number, height: number): boolean;
|
|
2
2
|
export declare function coherentDprWeights(pool: [number, number][], width: number, height: number): [number[], number[]];
|
|
3
3
|
export declare function reconcileWebglKeys(data: Record<string, unknown>, known: Record<string, string>): Record<string, unknown>;
|
|
4
|
+
export declare const REALISM_PROPERTIES: string[];
|
|
5
|
+
export declare function realismDefaults(known: Record<string, string>): Record<string, unknown>;
|
|
6
|
+
export declare function snapScreen(config: Record<string, unknown>, pool: [number, number][]): boolean;
|
|
7
|
+
export declare function languageList(language: string, region: string): string;
|