bexxcv 1.0.8 → 1.0.9
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 +42 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- 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 +17 -0
- package/dist/plugins/dodge.d.ts +21 -0
- package/dist/plugins/dodge.js +102 -0
- package/dist/plugins/gauntlet.d.ts +20 -0
- package/dist/plugins/gauntlet.js +119 -0
- package/dist/plugins/index.d.ts +27 -0
- package/dist/plugins/index.js +55 -0
- package/dist/plugins/lens.d.ts +20 -0
- package/dist/plugins/lens.js +89 -0
- package/dist/plugins/stash.d.ts +8 -0
- package/dist/plugins/stash.js +108 -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 +123 -0
- package/dist/schema.js +85 -0
- package/dist/sync_api.d.ts +3 -1
- package/dist/sync_api.js +9 -1
- 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/package.json +1 -1
package/README.md
CHANGED
|
@@ -336,6 +336,48 @@ node scripts/update-data.mjs --pin
|
|
|
336
336
|
|
|
337
337
|
---
|
|
338
338
|
|
|
339
|
+
## Plugins
|
|
340
|
+
|
|
341
|
+
Plugins are switched on with options and hook into the browser, its contexts and its pages. Each one is optional and does nothing unless you set its option.
|
|
342
|
+
|
|
343
|
+
```ts
|
|
344
|
+
const browser = await bexxcv({
|
|
345
|
+
gauntlet: { solve: ["uam", "turnstile"], maxTries: 3 },
|
|
346
|
+
wiretap: { match: ["/api/"], out: "capture.json" },
|
|
347
|
+
stash: "./profiles/cookies.json",
|
|
348
|
+
brake: { maxRps: 2 },
|
|
349
|
+
});
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
| Option | What it does |
|
|
353
|
+
|---|---|
|
|
354
|
+
| `gauntlet` | Runs the checks after every `page.goto()`: waits out the Cloudflare challenge (`uam`) and clicks Turnstile widgets (`turnstile`), retrying up to `maxTries`. While the challenge page is up it clicks the widget between mouse movements. `sustain` keeps human-like movement going for a number of milliseconds afterwards. A background check also runs every 1.5 seconds (`watch: false` turns it off), so a challenge that appears later in the session, for example when Cloudflare re-evaluates it, is handled too. Challenges are detected through the `cf-mitigated: challenge` header Cloudflare adds to every challenge page, with the page title as a fallback. Read the outcome with `gauntletResult(page)`. |
|
|
355
|
+
| `wiretap` | Records matching responses: `match` (substring, or `re:` plus a regular expression), `methods`, `bodies`, `maxBodyBytes`, `streamTimeout`. Server-sent event streams are split into `events`. Sensitive headers (`cookie`, `authorization`, `set-cookie`) are redacted unless `redact: false`. With `out` the capture is written to a private file when the browser closes, and `wiretapEntries(context)` returns it at any time. |
|
|
356
|
+
| `stash` | Cookie vault, a file path or `{ file, names, domains, saveEvery }`. Cookies are restored into every new context, expired ones are dropped, and everything is saved per domain when the browser closes. The file is created with mode 600. `cf_clearance` is tied to the device and IP it was issued for, so it is restored only when the proxy matches the one in use when it was saved, while other cookies are always restored. |
|
|
357
|
+
| `cocoon` | Persistent profile folder (`user_data_dir`) that keeps localStorage, IndexedDB and cache between runs. |
|
|
358
|
+
| `veneer` | Warns when a Firefox user agent is sent together with `sec-ch-ua` headers. With `{ strict: true }` those headers are stripped. |
|
|
359
|
+
| `brake` | Rate limiter for `page.goto()`: `maxRps` per host, and `Retry-After` is honored after 429 and 503 answers, with exponential backoff when the header is missing. |
|
|
360
|
+
| `dodge` | Proxy pool: `pool`, `rotateOn` (default 403 and 429), `healthCheck`, `cooldown`, `onBlocked`. Unhealthy proxies are skipped at launch, each new context gets the next proxy, and a proxy that answers a blocked status is paused. Keep the pool in one country, because the timezone and locale are derived from the launch proxy. |
|
|
361
|
+
| `lens` | Local fingerprint audit after the first page load: checks `navigator.webdriver`, user agent against platform, language, screen size against DPR, window size, core count and software WebGL renderers. Returns a score through `lensReport(page)` and prints failures unless `quiet: true`. Only `target: "local"` exists, no external site is contacted. |
|
|
362
|
+
| `plugins` | Your own plugins: an array of objects with a `name` and any of `beforeLaunch`, `beforeContext`, `onContext`, `onPage`, `beforeClose`. |
|
|
363
|
+
|
|
364
|
+
`gauntlet` already includes the `uam` waiting and the Turnstile clicking, so there is no need to set `uam` or `turnstile` next to it.
|
|
365
|
+
|
|
366
|
+
Writing a plugin:
|
|
367
|
+
|
|
368
|
+
```ts
|
|
369
|
+
const timing = {
|
|
370
|
+
name: "timing",
|
|
371
|
+
onPage(page) {
|
|
372
|
+
page.on("load", () => console.log("loaded", page.url()));
|
|
373
|
+
},
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
const browser = await bexxcv({ plugins: [timing] });
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
---
|
|
380
|
+
|
|
339
381
|
## Xvfb module
|
|
340
382
|
|
|
341
383
|
bexxcv exports all Xvfb utilities so you can manage virtual displays in your own code:
|
package/dist/index.d.ts
CHANGED
|
@@ -9,3 +9,5 @@ export { attachTurnstile, watchTurnstile, type TurnstileOptions, } from "./turns
|
|
|
9
9
|
export { attachUam, waitForUam, watchUam, type UamOptions, type UamResult } from "./uam.js";
|
|
10
10
|
export { dataStatus, doctor, type DataStatus } from "./doctor.js";
|
|
11
11
|
export { validateOptions } from "./schema.js";
|
|
12
|
+
export { applyPlugins, auditFingerprint, buildPlugins, gauntletResult, lensReport, runGauntlet, wiretapEntries, type BexxcvPlugin, type BrakeOptions, type DodgeOptions, type GauntletOptions, type LensOptions, type PluginOptions, type StashOptions, type VeneerOptions, type WiretapOptions, } from "./plugins/index.js";
|
|
13
|
+
export { solveTurnstile } from "./turnstile.js";
|
package/dist/index.js
CHANGED
|
@@ -9,3 +9,5 @@ export { attachTurnstile, watchTurnstile, } from "./turnstile.js";
|
|
|
9
9
|
export { attachUam, waitForUam, watchUam } from "./uam.js";
|
|
10
10
|
export { dataStatus, doctor } from "./doctor.js";
|
|
11
11
|
export { validateOptions } from "./schema.js";
|
|
12
|
+
export { applyPlugins, auditFingerprint, buildPlugins, gauntletResult, lensReport, runGauntlet, wiretapEntries, } from "./plugins/index.js";
|
|
13
|
+
export { solveTurnstile } from "./turnstile.js";
|
package/dist/pagehooks.d.ts
CHANGED
|
@@ -1,2 +1,8 @@
|
|
|
1
1
|
import type { Browser, BrowserContext, Page } from "playwright-core";
|
|
2
|
+
export type ContextOptions = NonNullable<Parameters<Browser["newContext"]>[0]>;
|
|
3
|
+
export interface ContextHooks {
|
|
4
|
+
before?: (options: ContextOptions) => void | Promise<void>;
|
|
5
|
+
after?: (context: BrowserContext, options: ContextOptions) => void | Promise<void>;
|
|
6
|
+
}
|
|
7
|
+
export declare function onEachContext<T extends Browser | BrowserContext>(target: T, hooks: ContextHooks): Promise<T>;
|
|
2
8
|
export declare function onEachPage<T extends Browser | BrowserContext>(target: T, handler: (page: Page) => void): T;
|
package/dist/pagehooks.js
CHANGED
|
@@ -1,3 +1,22 @@
|
|
|
1
|
+
export async function onEachContext(target, hooks) {
|
|
2
|
+
if (typeof target.newContext !== "function") {
|
|
3
|
+
await hooks.after?.(target, {});
|
|
4
|
+
return target;
|
|
5
|
+
}
|
|
6
|
+
const browser = target;
|
|
7
|
+
for (const context of browser.contexts()) {
|
|
8
|
+
await hooks.after?.(context, {});
|
|
9
|
+
}
|
|
10
|
+
const original = browser.newContext.bind(browser);
|
|
11
|
+
browser.newContext = async (options) => {
|
|
12
|
+
const next = { ...(options ?? {}) };
|
|
13
|
+
await hooks.before?.(next);
|
|
14
|
+
const context = await original(next);
|
|
15
|
+
await hooks.after?.(context, next);
|
|
16
|
+
return context;
|
|
17
|
+
};
|
|
18
|
+
return target;
|
|
19
|
+
}
|
|
1
20
|
export function onEachPage(target, handler) {
|
|
2
21
|
const seen = new WeakSet();
|
|
3
22
|
const watchContext = (context) => {
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { sleep } from "../random.js";
|
|
2
|
+
const WRAPPED = new WeakSet();
|
|
3
|
+
function hostOf(url, perHost) {
|
|
4
|
+
if (!perHost)
|
|
5
|
+
return "*";
|
|
6
|
+
try {
|
|
7
|
+
return new URL(url).host;
|
|
8
|
+
}
|
|
9
|
+
catch {
|
|
10
|
+
return "*";
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
function parseRetryAfter(value) {
|
|
14
|
+
if (!value)
|
|
15
|
+
return null;
|
|
16
|
+
const seconds = Number(value);
|
|
17
|
+
if (Number.isFinite(seconds))
|
|
18
|
+
return Math.max(0, seconds * 1000);
|
|
19
|
+
const date = Date.parse(value);
|
|
20
|
+
return Number.isNaN(date) ? null : Math.max(0, date - Date.now());
|
|
21
|
+
}
|
|
22
|
+
export function brake(options = {}) {
|
|
23
|
+
const interval = 1000 / (options.maxRps ?? 2);
|
|
24
|
+
const respect = options.respectRetryAfter ?? true;
|
|
25
|
+
const perHost = options.perHost ?? true;
|
|
26
|
+
const maxBackoff = options.maxBackoff ?? 120_000;
|
|
27
|
+
const nextSlot = new Map();
|
|
28
|
+
const blockedUntil = new Map();
|
|
29
|
+
const backoff = new Map();
|
|
30
|
+
const take = async (host) => {
|
|
31
|
+
const blocked = (blockedUntil.get(host) ?? 0) - Date.now();
|
|
32
|
+
if (blocked > 0)
|
|
33
|
+
await sleep(blocked);
|
|
34
|
+
const at = Math.max(Date.now(), nextSlot.get(host) ?? 0);
|
|
35
|
+
nextSlot.set(host, at + interval);
|
|
36
|
+
const wait = at - Date.now();
|
|
37
|
+
if (wait > 0)
|
|
38
|
+
await sleep(wait);
|
|
39
|
+
};
|
|
40
|
+
return {
|
|
41
|
+
name: "brake",
|
|
42
|
+
order: 90,
|
|
43
|
+
onContext(context) {
|
|
44
|
+
context.on("response", (response) => {
|
|
45
|
+
const request = response.request();
|
|
46
|
+
if (!request.isNavigationRequest())
|
|
47
|
+
return;
|
|
48
|
+
const host = hostOf(response.url(), perHost);
|
|
49
|
+
const status = response.status();
|
|
50
|
+
if (status === 429 || status === 503) {
|
|
51
|
+
const header = respect
|
|
52
|
+
? parseRetryAfter(response.headers()["retry-after"])
|
|
53
|
+
: null;
|
|
54
|
+
const next = Math.min(maxBackoff, (backoff.get(host) ?? 1000) * 2);
|
|
55
|
+
backoff.set(host, next);
|
|
56
|
+
blockedUntil.set(host, Date.now() + Math.min(maxBackoff, header ?? next));
|
|
57
|
+
}
|
|
58
|
+
else if (status < 400) {
|
|
59
|
+
backoff.delete(host);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
},
|
|
63
|
+
onPage(page) {
|
|
64
|
+
if (WRAPPED.has(page))
|
|
65
|
+
return;
|
|
66
|
+
WRAPPED.add(page);
|
|
67
|
+
const originalGoto = page.goto.bind(page);
|
|
68
|
+
page.goto = (async (...args) => {
|
|
69
|
+
await take(hostOf(String(args[0]), perHost));
|
|
70
|
+
return originalGoto(...args);
|
|
71
|
+
});
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
export function cocoon(options) {
|
|
4
|
+
return {
|
|
5
|
+
name: "cocoon",
|
|
6
|
+
order: 5,
|
|
7
|
+
beforeLaunch(launch) {
|
|
8
|
+
const dir = path.resolve(options.dir);
|
|
9
|
+
const current = launch.user_data_dir;
|
|
10
|
+
if (typeof current === "string" && path.resolve(current) !== dir) {
|
|
11
|
+
throw new Error('cocoon and user_data_dir point to different folders, set only one of them');
|
|
12
|
+
}
|
|
13
|
+
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
14
|
+
launch.user_data_dir = dir;
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { BexxcvPlugin } from "./types.js";
|
|
2
|
+
export interface DodgeOptions {
|
|
3
|
+
pool: string[];
|
|
4
|
+
rotateOn?: number[];
|
|
5
|
+
healthCheck?: boolean;
|
|
6
|
+
healthTimeout?: number;
|
|
7
|
+
cooldown?: number;
|
|
8
|
+
onBlocked?: (info: {
|
|
9
|
+
proxy: string;
|
|
10
|
+
status: number;
|
|
11
|
+
url: string;
|
|
12
|
+
}) => void;
|
|
13
|
+
}
|
|
14
|
+
type ProxyConfig = {
|
|
15
|
+
server: string;
|
|
16
|
+
username?: string;
|
|
17
|
+
password?: string;
|
|
18
|
+
};
|
|
19
|
+
export declare function toProxyConfig(url: string): ProxyConfig;
|
|
20
|
+
export declare function dodge(options: DodgeOptions): BexxcvPlugin;
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { publicIP } from "../ip.js";
|
|
2
|
+
export function toProxyConfig(url) {
|
|
3
|
+
const parsed = new URL(url);
|
|
4
|
+
const config = { server: `${parsed.protocol}//${parsed.host}` };
|
|
5
|
+
if (parsed.username)
|
|
6
|
+
config.username = decodeURIComponent(parsed.username);
|
|
7
|
+
if (parsed.password)
|
|
8
|
+
config.password = decodeURIComponent(parsed.password);
|
|
9
|
+
return config;
|
|
10
|
+
}
|
|
11
|
+
function timeout(promise, ms) {
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
const timer = setTimeout(() => reject(new Error("timeout")), ms);
|
|
14
|
+
promise.then((value) => {
|
|
15
|
+
clearTimeout(timer);
|
|
16
|
+
resolve(value);
|
|
17
|
+
}, (error) => {
|
|
18
|
+
clearTimeout(timer);
|
|
19
|
+
reject(error);
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
export function dodge(options) {
|
|
24
|
+
if (!options.pool || options.pool.length === 0) {
|
|
25
|
+
throw new Error("dodge needs at least one proxy in pool");
|
|
26
|
+
}
|
|
27
|
+
const rotateOn = new Set(options.rotateOn ?? [403, 429]);
|
|
28
|
+
const cooldown = options.cooldown ?? 300_000;
|
|
29
|
+
const states = options.pool.map((url) => ({ url, badUntil: 0 }));
|
|
30
|
+
const owners = new WeakMap();
|
|
31
|
+
let cursor = 0;
|
|
32
|
+
let launchProxy = null;
|
|
33
|
+
let firstContext = true;
|
|
34
|
+
const pick = () => {
|
|
35
|
+
const now = Date.now();
|
|
36
|
+
for (let i = 0; i < states.length; i++) {
|
|
37
|
+
const state = states[(cursor + i) % states.length];
|
|
38
|
+
if (state.badUntil <= now) {
|
|
39
|
+
cursor = (cursor + i + 1) % states.length;
|
|
40
|
+
return state;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const soonest = [...states].sort((a, b) => a.badUntil - b.badUntil)[0];
|
|
44
|
+
console.warn("[bexxcv] dodge: every proxy is cooling down, reusing the one that recovers first");
|
|
45
|
+
return soonest;
|
|
46
|
+
};
|
|
47
|
+
return {
|
|
48
|
+
name: "dodge",
|
|
49
|
+
order: 10,
|
|
50
|
+
async beforeLaunch(launch) {
|
|
51
|
+
if (options.healthCheck !== false) {
|
|
52
|
+
const limit = options.healthTimeout ?? 10_000;
|
|
53
|
+
await Promise.all(states.map(async (state) => {
|
|
54
|
+
try {
|
|
55
|
+
state.ip = await timeout(publicIP(state.url), limit);
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
state.badUntil = Date.now() + cooldown;
|
|
59
|
+
}
|
|
60
|
+
}));
|
|
61
|
+
if (states.every((state) => state.badUntil > Date.now())) {
|
|
62
|
+
throw new Error("dodge: no healthy proxy in pool");
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const chosen = pick();
|
|
66
|
+
launchProxy = chosen.url;
|
|
67
|
+
if (launch.proxy === undefined)
|
|
68
|
+
launch.proxy = toProxyConfig(chosen.url);
|
|
69
|
+
},
|
|
70
|
+
beforeContext(contextOptions) {
|
|
71
|
+
if (contextOptions.proxy)
|
|
72
|
+
return;
|
|
73
|
+
if (firstContext) {
|
|
74
|
+
firstContext = false;
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const chosen = pick();
|
|
78
|
+
contextOptions.proxy = toProxyConfig(chosen.url);
|
|
79
|
+
},
|
|
80
|
+
onContext(context, contextOptions) {
|
|
81
|
+
const server = contextOptions.proxy?.server;
|
|
82
|
+
const owner = states.find((state) => server && state.url.includes(new URL(state.url).host) && state.url.startsWith(server.split("//")[0]))?.url ??
|
|
83
|
+
launchProxy;
|
|
84
|
+
if (owner)
|
|
85
|
+
owners.set(context, owner);
|
|
86
|
+
context.on("response", (response) => {
|
|
87
|
+
const status = response.status();
|
|
88
|
+
if (!rotateOn.has(status))
|
|
89
|
+
return;
|
|
90
|
+
if (!response.request().isNavigationRequest())
|
|
91
|
+
return;
|
|
92
|
+
const proxy = owners.get(context);
|
|
93
|
+
const state = states.find((s) => s.url === proxy);
|
|
94
|
+
if (!state)
|
|
95
|
+
return;
|
|
96
|
+
state.badUntil = Date.now() + cooldown;
|
|
97
|
+
options.onBlocked?.({ proxy: state.url, status, url: response.url() });
|
|
98
|
+
console.warn(`[bexxcv] dodge: proxy answered ${status}, it is paused for ${Math.round(cooldown / 1000)}s. New contexts will use another proxy.`);
|
|
99
|
+
});
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Page } from "playwright-core";
|
|
2
|
+
import { type UamResult } from "../uam.js";
|
|
3
|
+
import type { BexxcvPlugin } from "./types.js";
|
|
4
|
+
export interface GauntletOptions {
|
|
5
|
+
solve?: Array<"uam" | "turnstile">;
|
|
6
|
+
maxTries?: number;
|
|
7
|
+
timeout?: number;
|
|
8
|
+
sustain?: number;
|
|
9
|
+
watch?: boolean;
|
|
10
|
+
interval?: number;
|
|
11
|
+
}
|
|
12
|
+
export interface GauntletResult {
|
|
13
|
+
passed: boolean;
|
|
14
|
+
tries: number;
|
|
15
|
+
uam: UamResult | null;
|
|
16
|
+
turnstile: string | null;
|
|
17
|
+
}
|
|
18
|
+
export declare function gauntletResult(page: Page): GauntletResult | null;
|
|
19
|
+
export declare function runGauntlet(page: Page, options?: GauntletOptions): Promise<GauntletResult>;
|
|
20
|
+
export declare function gauntlet(options?: GauntletOptions): BexxcvPlugin;
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { sleep } from "../random.js";
|
|
2
|
+
import { createClicker, findWidgets, solveTurnstile } from "../turnstile.js";
|
|
3
|
+
import { humanActivity, isChallenge, trackMitigation, waitForUam, } from "../uam.js";
|
|
4
|
+
const RESULTS = new WeakMap();
|
|
5
|
+
const WRAPPED = new WeakSet();
|
|
6
|
+
const RUNNING = new WeakSet();
|
|
7
|
+
export function gauntletResult(page) {
|
|
8
|
+
return RESULTS.get(page) ?? null;
|
|
9
|
+
}
|
|
10
|
+
async function widgetPending(page) {
|
|
11
|
+
try {
|
|
12
|
+
const widgets = await findWidgets(page);
|
|
13
|
+
return widgets.some((widget) => !widget.solved);
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
async function sustain(page, ms) {
|
|
20
|
+
const end = Date.now() + ms;
|
|
21
|
+
while (Date.now() < end && !page.isClosed()) {
|
|
22
|
+
try {
|
|
23
|
+
await humanActivity(page);
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
await sleep(500);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
export async function runGauntlet(page, options = {}) {
|
|
31
|
+
const solve = options.solve ?? ["uam", "turnstile"];
|
|
32
|
+
const maxTries = options.maxTries ?? 3;
|
|
33
|
+
const timeout = options.timeout ?? 30000;
|
|
34
|
+
const result = { passed: false, tries: 0, uam: null, turnstile: null };
|
|
35
|
+
const wantsTurnstile = solve.includes("turnstile");
|
|
36
|
+
for (let attempt = 1; attempt <= maxTries && !page.isClosed(); attempt++) {
|
|
37
|
+
result.tries = attempt;
|
|
38
|
+
for (const stage of solve) {
|
|
39
|
+
if (stage === "uam") {
|
|
40
|
+
const clicker = wantsTurnstile ? createClicker() : undefined;
|
|
41
|
+
result.uam = await waitForUam(page, { timeout, assist: clicker });
|
|
42
|
+
}
|
|
43
|
+
else if (stage === "turnstile") {
|
|
44
|
+
const token = await solveTurnstile(page, timeout);
|
|
45
|
+
if (token)
|
|
46
|
+
result.turnstile = token;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
const stillChallenged = await isChallenge(page);
|
|
50
|
+
const missingToken = wantsTurnstile && !result.turnstile && (await widgetPending(page));
|
|
51
|
+
if (!stillChallenged && !missingToken) {
|
|
52
|
+
result.passed = true;
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
await sleep(1500);
|
|
56
|
+
}
|
|
57
|
+
if (result.passed && options.sustain && options.sustain > 0) {
|
|
58
|
+
await sustain(page, options.sustain);
|
|
59
|
+
}
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
async function runGuarded(page, options, waitIfBusy) {
|
|
63
|
+
if (RUNNING.has(page)) {
|
|
64
|
+
if (!waitIfBusy)
|
|
65
|
+
return;
|
|
66
|
+
const limit = Date.now() + (options.timeout ?? 30000) * (options.maxTries ?? 3);
|
|
67
|
+
while (RUNNING.has(page) && Date.now() < limit)
|
|
68
|
+
await sleep(200);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
RUNNING.add(page);
|
|
72
|
+
try {
|
|
73
|
+
RESULTS.set(page, await runGauntlet(page, options));
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
console.warn(`[bexxcv] gauntlet failed: ${error.message}`);
|
|
77
|
+
}
|
|
78
|
+
finally {
|
|
79
|
+
RUNNING.delete(page);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
export function gauntlet(options = {}) {
|
|
83
|
+
return {
|
|
84
|
+
name: "gauntlet",
|
|
85
|
+
order: 50,
|
|
86
|
+
onPage(page) {
|
|
87
|
+
if (WRAPPED.has(page))
|
|
88
|
+
return;
|
|
89
|
+
WRAPPED.add(page);
|
|
90
|
+
trackMitigation(page);
|
|
91
|
+
const originalGoto = page.goto.bind(page);
|
|
92
|
+
page.goto = (async (...args) => {
|
|
93
|
+
const response = await originalGoto(...args);
|
|
94
|
+
await runGuarded(page, options, true);
|
|
95
|
+
return response;
|
|
96
|
+
});
|
|
97
|
+
if (options.watch === false)
|
|
98
|
+
return;
|
|
99
|
+
let active = true;
|
|
100
|
+
page.on("close", () => {
|
|
101
|
+
active = false;
|
|
102
|
+
});
|
|
103
|
+
const interval = options.interval ?? 1500;
|
|
104
|
+
void (async () => {
|
|
105
|
+
while (active && !page.isClosed()) {
|
|
106
|
+
try {
|
|
107
|
+
if (!RUNNING.has(page) && (await isChallenge(page))) {
|
|
108
|
+
await runGuarded(page, options, false);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
catch {
|
|
112
|
+
await sleep(500);
|
|
113
|
+
}
|
|
114
|
+
await sleep(interval);
|
|
115
|
+
}
|
|
116
|
+
})();
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type BrakeOptions } from "./brake.js";
|
|
2
|
+
import { type DodgeOptions } from "./dodge.js";
|
|
3
|
+
import { type GauntletOptions } from "./gauntlet.js";
|
|
4
|
+
import { type LensOptions } from "./lens.js";
|
|
5
|
+
import { type StashOptions } from "./stash.js";
|
|
6
|
+
import type { BexxcvPlugin } from "./types.js";
|
|
7
|
+
import { type VeneerOptions } from "./veneer.js";
|
|
8
|
+
import { type WiretapOptions } from "./wiretap.js";
|
|
9
|
+
export interface PluginOptions {
|
|
10
|
+
gauntlet?: boolean | GauntletOptions;
|
|
11
|
+
wiretap?: boolean | WiretapOptions;
|
|
12
|
+
stash?: string | StashOptions;
|
|
13
|
+
veneer?: boolean | VeneerOptions;
|
|
14
|
+
brake?: boolean | BrakeOptions;
|
|
15
|
+
dodge?: DodgeOptions;
|
|
16
|
+
cocoon?: string;
|
|
17
|
+
lens?: boolean | LensOptions;
|
|
18
|
+
plugins?: BexxcvPlugin[];
|
|
19
|
+
}
|
|
20
|
+
export declare const PLUGIN_KEYS: readonly ["gauntlet", "wiretap", "stash", "veneer", "brake", "dodge", "cocoon", "lens", "plugins"];
|
|
21
|
+
export declare function buildPlugins(options: PluginOptions): BexxcvPlugin[];
|
|
22
|
+
export declare function stripPluginOptions<T extends Record<string, unknown>>(options: T): T;
|
|
23
|
+
export { applyPlugins, runBeforeLaunch, type BexxcvPlugin } from "./types.js";
|
|
24
|
+
export { auditFingerprint, lensReport } from "./lens.js";
|
|
25
|
+
export { gauntletResult, runGauntlet } from "./gauntlet.js";
|
|
26
|
+
export { wiretapEntries } from "./wiretap.js";
|
|
27
|
+
export type { BrakeOptions, DodgeOptions, GauntletOptions, LensOptions, StashOptions, VeneerOptions, WiretapOptions, };
|
|
@@ -0,0 +1,55 @@
|
|
|
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 { lens } from "./lens.js";
|
|
6
|
+
import { stash } from "./stash.js";
|
|
7
|
+
import { veneer } from "./veneer.js";
|
|
8
|
+
import { wiretap } from "./wiretap.js";
|
|
9
|
+
export const PLUGIN_KEYS = [
|
|
10
|
+
"gauntlet",
|
|
11
|
+
"wiretap",
|
|
12
|
+
"stash",
|
|
13
|
+
"veneer",
|
|
14
|
+
"brake",
|
|
15
|
+
"dodge",
|
|
16
|
+
"cocoon",
|
|
17
|
+
"lens",
|
|
18
|
+
"plugins",
|
|
19
|
+
];
|
|
20
|
+
function opts(value) {
|
|
21
|
+
return value === true ? {} : value;
|
|
22
|
+
}
|
|
23
|
+
export function buildPlugins(options) {
|
|
24
|
+
const list = [];
|
|
25
|
+
if (options.dodge)
|
|
26
|
+
list.push(dodge(options.dodge));
|
|
27
|
+
if (options.cocoon)
|
|
28
|
+
list.push(cocoon({ dir: options.cocoon }));
|
|
29
|
+
if (options.stash) {
|
|
30
|
+
list.push(stash(typeof options.stash === "string" ? { file: options.stash } : options.stash));
|
|
31
|
+
}
|
|
32
|
+
if (options.wiretap)
|
|
33
|
+
list.push(wiretap(opts(options.wiretap)));
|
|
34
|
+
if (options.veneer)
|
|
35
|
+
list.push(veneer(opts(options.veneer)));
|
|
36
|
+
if (options.lens)
|
|
37
|
+
list.push(lens(opts(options.lens)));
|
|
38
|
+
if (options.gauntlet)
|
|
39
|
+
list.push(gauntlet(opts(options.gauntlet)));
|
|
40
|
+
if (options.brake)
|
|
41
|
+
list.push(brake(opts(options.brake)));
|
|
42
|
+
if (options.plugins)
|
|
43
|
+
list.push(...options.plugins);
|
|
44
|
+
return list;
|
|
45
|
+
}
|
|
46
|
+
export function stripPluginOptions(options) {
|
|
47
|
+
const copy = { ...options };
|
|
48
|
+
for (const key of PLUGIN_KEYS)
|
|
49
|
+
delete copy[key];
|
|
50
|
+
return copy;
|
|
51
|
+
}
|
|
52
|
+
export { applyPlugins, runBeforeLaunch } from "./types.js";
|
|
53
|
+
export { auditFingerprint, lensReport } from "./lens.js";
|
|
54
|
+
export { gauntletResult, runGauntlet } from "./gauntlet.js";
|
|
55
|
+
export { wiretapEntries } from "./wiretap.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,89 @@
|
|
|
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(() => {
|
|
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
|
+
return {
|
|
28
|
+
ua: navigator.userAgent,
|
|
29
|
+
platform: navigator.platform,
|
|
30
|
+
language: navigator.language,
|
|
31
|
+
languages: Array.from(navigator.languages),
|
|
32
|
+
cores: navigator.hardwareConcurrency,
|
|
33
|
+
webdriver: navigator.webdriver === true,
|
|
34
|
+
dpr: window.devicePixelRatio,
|
|
35
|
+
screenW: screen.width,
|
|
36
|
+
screenH: screen.height,
|
|
37
|
+
innerW: window.innerWidth,
|
|
38
|
+
innerH: window.innerHeight,
|
|
39
|
+
outerW: window.outerWidth,
|
|
40
|
+
outerH: window.outerHeight,
|
|
41
|
+
vendor,
|
|
42
|
+
renderer,
|
|
43
|
+
};
|
|
44
|
+
});
|
|
45
|
+
const checks = [];
|
|
46
|
+
const add = (id, ok, detail) => checks.push({ id, ok, detail });
|
|
47
|
+
add("webdriver", !probe.webdriver, `navigator.webdriver is ${probe.webdriver}`);
|
|
48
|
+
const platformOk = (/Windows/i.test(probe.ua) && /^Win/i.test(probe.platform)) ||
|
|
49
|
+
(/Macintosh|Mac OS/i.test(probe.ua) && /^Mac/i.test(probe.platform)) ||
|
|
50
|
+
(/Linux|X11/i.test(probe.ua) && !/Windows|Macintosh/i.test(probe.ua) && /Linux|X11/i.test(probe.platform));
|
|
51
|
+
add("ua-platform", platformOk, `${probe.platform} against the user agent`);
|
|
52
|
+
add("language", probe.languages[0] === probe.language, `language ${probe.language}, first of languages ${probe.languages[0]}`);
|
|
53
|
+
add("dpr-screen", isKnownPhysical(probe.screenW * probe.dpr, probe.screenH * probe.dpr), `${probe.screenW}x${probe.screenH} at DPR ${probe.dpr}`);
|
|
54
|
+
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}`);
|
|
55
|
+
add("cores", PLAUSIBLE_CORES.has(probe.cores), `${probe.cores} logical cores`);
|
|
56
|
+
add("webgl", probe.vendor !== null && probe.renderer !== null, probe.renderer ? `${probe.vendor} / ${probe.renderer}` : "WebGL is not available");
|
|
57
|
+
add("webgl-software", !(probe.renderer && /llvmpipe|swiftshader|software|mesa offscreen/i.test(probe.renderer)), probe.renderer ?? "no renderer");
|
|
58
|
+
const passed = checks.filter((check) => check.ok).length;
|
|
59
|
+
return { score: Math.round((passed / checks.length) * 100), checks };
|
|
60
|
+
}
|
|
61
|
+
export function lens(options = {}) {
|
|
62
|
+
return {
|
|
63
|
+
name: "lens",
|
|
64
|
+
order: 40,
|
|
65
|
+
onPage(page) {
|
|
66
|
+
if (options.audit === false || AUDITED.has(page))
|
|
67
|
+
return;
|
|
68
|
+
AUDITED.add(page);
|
|
69
|
+
page.once("load", () => {
|
|
70
|
+
void auditFingerprint(page)
|
|
71
|
+
.then((report) => {
|
|
72
|
+
REPORTS.set(page, report);
|
|
73
|
+
if (!options.quiet) {
|
|
74
|
+
console.log(`[bexxcv] lens: fingerprint score ${report.score}/100`);
|
|
75
|
+
for (const check of report.checks) {
|
|
76
|
+
if (!check.ok)
|
|
77
|
+
console.log(`[bexxcv] lens: ${check.id} failed, ${check.detail}`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (options.out) {
|
|
81
|
+
fs.mkdirSync(path.dirname(path.resolve(options.out)), { recursive: true });
|
|
82
|
+
fs.writeFileSync(options.out, `${JSON.stringify(report, null, 2)}\n`);
|
|
83
|
+
}
|
|
84
|
+
})
|
|
85
|
+
.catch(() => undefined);
|
|
86
|
+
});
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
}
|