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
package/README.md
CHANGED
|
@@ -336,6 +336,76 @@ node scripts/update-data.mjs --pin
|
|
|
336
336
|
|
|
337
337
|
---
|
|
338
338
|
|
|
339
|
+
## Plugins
|
|
340
|
+
|
|
341
|
+
Plugins add behavior to the browser, its contexts and its pages. Pass them as a list in the second argument, so the options in the first argument stay about the browser itself.
|
|
342
|
+
|
|
343
|
+
```ts
|
|
344
|
+
import { bexxcv, brake, gauntlet, human, stash, wiretap } from "bexxcv";
|
|
345
|
+
|
|
346
|
+
const browser = await bexxcv(
|
|
347
|
+
{ preset: "realistic", headless: false, os: "windows" },
|
|
348
|
+
[
|
|
349
|
+
gauntlet({ solve: ["uam", "turnstile"], maxTries: 3 }),
|
|
350
|
+
human(),
|
|
351
|
+
stash("./profiles/cookies.json"),
|
|
352
|
+
wiretap("/api/"),
|
|
353
|
+
brake({ maxRps: 2 }),
|
|
354
|
+
],
|
|
355
|
+
);
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
The same plugins can also be switched on with a flat option such as `bexxcv({ gauntlet: { solve: ["uam"] } })`. It does the same thing and is only a shortcut.
|
|
359
|
+
|
|
360
|
+
| Plugin | What it does |
|
|
361
|
+
|---|---|
|
|
362
|
+
| `gauntlet(opts)` | 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 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)`. |
|
|
363
|
+
| `uam(opts)` and `turnstile(opts)` | The two halves of `gauntlet` as separate plugins. |
|
|
364
|
+
| `human(opts)` | Replaces `page.click`, `page.fill`, `page.type` and `page.hover` with versions that move the pointer, hold the button, type with uneven timing and occasional corrected typos. Options: `speed`, `typoRate`, `nativeCursor`, `think` (a `[min, max]` pause in milliseconds before each action, or `false`). Locator methods are not replaced. |
|
|
365
|
+
| `stash(file)` | 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. |
|
|
366
|
+
| `cocoon(dir)` | Persistent profile folder (`user_data_dir`) that keeps localStorage, IndexedDB and cache between runs. |
|
|
367
|
+
| `wiretap(match)` | Records matching responses. Pass a substring, a list, or `{ match, methods, bodies, maxBodyBytes, streamTimeout, out, redact }`. Use `re:` plus a regular expression for patterns. Server-sent event streams are split into `events`. Sensitive headers 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. |
|
|
368
|
+
| `brake(opts)` | 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. |
|
|
369
|
+
| `dodge(pool)` | Proxy pool: a list of proxy URLs or `{ pool, rotateOn, 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. |
|
|
370
|
+
| `veneer(opts)` | Warns when a Firefox user agent is sent together with `sec-ch-ua` headers. With `{ strict: true }` those headers are stripped. |
|
|
371
|
+
| `lens(opts)` | Local fingerprint audit after the first page load, see below. |
|
|
372
|
+
|
|
373
|
+
Writing a plugin:
|
|
374
|
+
|
|
375
|
+
```ts
|
|
376
|
+
const timing = {
|
|
377
|
+
name: "timing",
|
|
378
|
+
onPage(page) {
|
|
379
|
+
page.on("load", () => console.log("loaded", page.url()));
|
|
380
|
+
},
|
|
381
|
+
};
|
|
382
|
+
|
|
383
|
+
const browser = await bexxcv({}, [timing]);
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
A plugin object can have `beforeLaunch`, `beforeContext`, `onContext`, `onPage` and `beforeClose`, and an `order` number that decides the sequence between plugins.
|
|
387
|
+
|
|
388
|
+
### Realistic preset
|
|
389
|
+
|
|
390
|
+
`preset: "realistic"` fills in the options that make a session look like a normal desktop browser, without overriding anything you set yourself:
|
|
391
|
+
|
|
392
|
+
| Option | Value | Why |
|
|
393
|
+
|---|---|---|
|
|
394
|
+
| `geoip` | `true` | Timezone, locale and geolocation follow the IP address, so they agree with where the traffic comes from |
|
|
395
|
+
| `humanize` | `true` | Natural cursor paths |
|
|
396
|
+
| `enable_cache` | `true` | Real browsers cache, and repeat visits load faster |
|
|
397
|
+
| `realism` | `true` | Adds a microphone, a webcam and a speaker to the media device list when the installed browser supports those properties. Headless servers normally report none. Run `npx bexxcv doctor` to see which properties your browser supports |
|
|
398
|
+
|
|
399
|
+
### Fingerprint audit
|
|
400
|
+
|
|
401
|
+
`lens()` checks the browser from the inside and returns a score with the failing checks (`lensReport(page)`, or `auditFingerprint(page)` on demand): `navigator.webdriver`, user agent against platform, language against `Intl`, screen size against DPR, window size, core count, touch points, WebGL availability and software renderers, replaced native functions, client hints and `window.chrome` for a Firefox user agent, the PDF viewer, window focus and the number of media devices. It never contacts an external site.
|
|
402
|
+
|
|
403
|
+
### What no module can promise
|
|
404
|
+
|
|
405
|
+
Bot detection also looks at things outside the browser: the reputation of the IP address, how many sessions share it, request timing across pages, and what the server learns over time. bexxcv keeps the browser side consistent and human-like, but it cannot make every protection treat a session as human. Use residential or ISP proxies, keep one profile per identity (`stash` or `cocoon`), avoid running many sessions from one IP, and check the result with `lens()` and `npx bexxcv doctor`.
|
|
406
|
+
|
|
407
|
+
---
|
|
408
|
+
|
|
339
409
|
## Xvfb module
|
|
340
410
|
|
|
341
411
|
bexxcv exports all Xvfb utilities so you can manage virtual displays in your own code:
|
package/dist/doctor.js
CHANGED
|
@@ -3,7 +3,7 @@ import * as fs from "node:fs";
|
|
|
3
3
|
import * as path from "node:path";
|
|
4
4
|
import { fileURLToPath } from "node:url";
|
|
5
5
|
import { getPath } from "./pkgman.js";
|
|
6
|
-
import { reconcileWebglKeys } from "./realism.js";
|
|
6
|
+
import { REALISM_PROPERTIES, reconcileWebglKeys } from "./realism.js";
|
|
7
7
|
const currentDir = import.meta.dirname ?? path.dirname(fileURLToPath(import.meta.url));
|
|
8
8
|
const DATA_DIR = path.join(currentDir, "data-files");
|
|
9
9
|
export const DEFAULT_MAX_AGE_DAYS = 180;
|
|
@@ -103,6 +103,8 @@ export async function doctor(options = {}) {
|
|
|
103
103
|
}
|
|
104
104
|
const props = readProperties();
|
|
105
105
|
if (props) {
|
|
106
|
+
const supported = REALISM_PROPERTIES.filter((key) => key in props);
|
|
107
|
+
lines.push(`realism properties supported by this browser: ${supported.length > 0 ? supported.join(", ") : "none"}`);
|
|
106
108
|
try {
|
|
107
109
|
const { webGl2Enabled: _skip, ...rest } = JSON.parse(sample.data);
|
|
108
110
|
reconcileWebglKeys(rest, props);
|
package/dist/human.d.ts
CHANGED
|
@@ -54,6 +54,8 @@ export declare class Human {
|
|
|
54
54
|
pause(min?: number, max?: number): Promise<void>;
|
|
55
55
|
moveTo(x: number, y: number): Promise<void>;
|
|
56
56
|
click(target: string | Point, options?: HumanClickOptions): Promise<void>;
|
|
57
|
+
hover(target: string | Point): Promise<void>;
|
|
58
|
+
clear(selector: string): Promise<void>;
|
|
57
59
|
type(target: string | null, text: string): Promise<void>;
|
|
58
60
|
scroll(deltaY: number): Promise<void>;
|
|
59
61
|
scrollTo(selector: string): Promise<void>;
|
package/dist/human.js
CHANGED
|
@@ -97,6 +97,18 @@ export class Human {
|
|
|
97
97
|
await sleep(between(45, 130));
|
|
98
98
|
await this.page.mouse.up({ button: options.button });
|
|
99
99
|
}
|
|
100
|
+
async hover(target) {
|
|
101
|
+
const point = typeof target === "string" ? await this.pointInside(target) : target;
|
|
102
|
+
await this.moveTo(point.x, point.y);
|
|
103
|
+
await sleep(between(80, 260) / this.speed);
|
|
104
|
+
}
|
|
105
|
+
async clear(selector) {
|
|
106
|
+
await this.click(selector);
|
|
107
|
+
await sleep(between(80, 220) / this.speed);
|
|
108
|
+
await this.page.keyboard.press("ControlOrMeta+A");
|
|
109
|
+
await sleep(between(60, 160) / this.speed);
|
|
110
|
+
await this.page.keyboard.press("Backspace");
|
|
111
|
+
}
|
|
100
112
|
async type(target, text) {
|
|
101
113
|
if (target) {
|
|
102
114
|
await this.click(target);
|
package/dist/index.d.ts
CHANGED
|
@@ -9,3 +9,6 @@ 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";
|
|
14
|
+
export { brake, cocoon, dodge, gauntlet, human, lens, stash, turnstile, uam, veneer, wiretap, type HumanPluginOptions, } from "./plugins/index.js";
|
package/dist/index.js
CHANGED
|
@@ -9,3 +9,6 @@ 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";
|
|
14
|
+
export { brake, cocoon, dodge, gauntlet, human, lens, stash, turnstile, uam, veneer, wiretap, } from "./plugins/index.js";
|
package/dist/locale.d.ts
CHANGED
|
@@ -23,4 +23,5 @@ export declare function geoipAllowed(): void;
|
|
|
23
23
|
export declare function downloadMMDB(): Promise<void>;
|
|
24
24
|
export declare function removeMMDB(): void;
|
|
25
25
|
export declare function getGeolocation(ip: string): Promise<Geolocation>;
|
|
26
|
+
export declare function isBrowserLanguage(tag: string): boolean;
|
|
26
27
|
export {};
|
package/dist/locale.js
CHANGED
|
@@ -280,7 +280,9 @@ class StatisticalLocaleSelector {
|
|
|
280
280
|
}
|
|
281
281
|
fromRegion(region) {
|
|
282
282
|
const [languages, probabilities] = this.loadTerritoryData(region);
|
|
283
|
-
const
|
|
283
|
+
const weights = probabilities.map((p, i) => isBrowserLanguage(languages[i]) ? p ** 6 : 0);
|
|
284
|
+
const usable = weights.some((w) => w > 0);
|
|
285
|
+
const language = this.weightedRandomChoice(languages, usable ? weights : probabilities).replace("_", "-");
|
|
284
286
|
return normalizeLocale(`${language}-${region}`);
|
|
285
287
|
}
|
|
286
288
|
fromLanguage(language) {
|
|
@@ -290,3 +292,18 @@ class StatisticalLocaleSelector {
|
|
|
290
292
|
}
|
|
291
293
|
}
|
|
292
294
|
const SELECTOR = new StatisticalLocaleSelector();
|
|
295
|
+
export function isBrowserLanguage(tag) {
|
|
296
|
+
const normalized = tag.replace("_", "-");
|
|
297
|
+
if (!/^([a-z]{2}|fil)(-[A-Za-z]{4})?$/.test(normalized))
|
|
298
|
+
return false;
|
|
299
|
+
const base = normalized.split("-")[0].toLowerCase();
|
|
300
|
+
const UNSAFE = new Set(["jv", "su"]);
|
|
301
|
+
if (UNSAFE.has(base))
|
|
302
|
+
return false;
|
|
303
|
+
try {
|
|
304
|
+
return Intl.DateTimeFormat.supportedLocalesOf([normalized]).length > 0;
|
|
305
|
+
}
|
|
306
|
+
catch {
|
|
307
|
+
return false;
|
|
308
|
+
}
|
|
309
|
+
}
|
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,18 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
export function cocoon(input) {
|
|
4
|
+
const options = typeof input === "string" ? { dir: input } : input;
|
|
5
|
+
return {
|
|
6
|
+
name: "cocoon",
|
|
7
|
+
order: 5,
|
|
8
|
+
beforeLaunch(launch) {
|
|
9
|
+
const dir = path.resolve(options.dir);
|
|
10
|
+
const current = launch.user_data_dir;
|
|
11
|
+
if (typeof current === "string" && path.resolve(current) !== dir) {
|
|
12
|
+
throw new Error('cocoon and user_data_dir point to different folders, set only one of them');
|
|
13
|
+
}
|
|
14
|
+
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
15
|
+
launch.user_data_dir = dir;
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
}
|
|
@@ -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(input: string[] | DodgeOptions): BexxcvPlugin;
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,103 @@
|
|
|
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(input) {
|
|
24
|
+
const options = Array.isArray(input) ? { pool: input } : input;
|
|
25
|
+
if (!options.pool || options.pool.length === 0) {
|
|
26
|
+
throw new Error("dodge needs at least one proxy in pool");
|
|
27
|
+
}
|
|
28
|
+
const rotateOn = new Set(options.rotateOn ?? [403, 429]);
|
|
29
|
+
const cooldown = options.cooldown ?? 300_000;
|
|
30
|
+
const states = options.pool.map((url) => ({ url, badUntil: 0 }));
|
|
31
|
+
const owners = new WeakMap();
|
|
32
|
+
let cursor = 0;
|
|
33
|
+
let launchProxy = null;
|
|
34
|
+
let firstContext = true;
|
|
35
|
+
const pick = () => {
|
|
36
|
+
const now = Date.now();
|
|
37
|
+
for (let i = 0; i < states.length; i++) {
|
|
38
|
+
const state = states[(cursor + i) % states.length];
|
|
39
|
+
if (state.badUntil <= now) {
|
|
40
|
+
cursor = (cursor + i + 1) % states.length;
|
|
41
|
+
return state;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const soonest = [...states].sort((a, b) => a.badUntil - b.badUntil)[0];
|
|
45
|
+
console.warn("[bexxcv] dodge: every proxy is cooling down, reusing the one that recovers first");
|
|
46
|
+
return soonest;
|
|
47
|
+
};
|
|
48
|
+
return {
|
|
49
|
+
name: "dodge",
|
|
50
|
+
order: 10,
|
|
51
|
+
async beforeLaunch(launch) {
|
|
52
|
+
if (options.healthCheck !== false) {
|
|
53
|
+
const limit = options.healthTimeout ?? 10_000;
|
|
54
|
+
await Promise.all(states.map(async (state) => {
|
|
55
|
+
try {
|
|
56
|
+
state.ip = await timeout(publicIP(state.url), limit);
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
state.badUntil = Date.now() + cooldown;
|
|
60
|
+
}
|
|
61
|
+
}));
|
|
62
|
+
if (states.every((state) => state.badUntil > Date.now())) {
|
|
63
|
+
throw new Error("dodge: no healthy proxy in pool");
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const chosen = pick();
|
|
67
|
+
launchProxy = chosen.url;
|
|
68
|
+
if (launch.proxy === undefined)
|
|
69
|
+
launch.proxy = toProxyConfig(chosen.url);
|
|
70
|
+
},
|
|
71
|
+
beforeContext(contextOptions) {
|
|
72
|
+
if (contextOptions.proxy)
|
|
73
|
+
return;
|
|
74
|
+
if (firstContext) {
|
|
75
|
+
firstContext = false;
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
const chosen = pick();
|
|
79
|
+
contextOptions.proxy = toProxyConfig(chosen.url);
|
|
80
|
+
},
|
|
81
|
+
onContext(context, contextOptions) {
|
|
82
|
+
const server = contextOptions.proxy?.server;
|
|
83
|
+
const owner = states.find((state) => server && state.url.includes(new URL(state.url).host) && state.url.startsWith(server.split("//")[0]))?.url ??
|
|
84
|
+
launchProxy;
|
|
85
|
+
if (owner)
|
|
86
|
+
owners.set(context, owner);
|
|
87
|
+
context.on("response", (response) => {
|
|
88
|
+
const status = response.status();
|
|
89
|
+
if (!rotateOn.has(status))
|
|
90
|
+
return;
|
|
91
|
+
if (!response.request().isNavigationRequest())
|
|
92
|
+
return;
|
|
93
|
+
const proxy = owners.get(context);
|
|
94
|
+
const state = states.find((s) => s.url === proxy);
|
|
95
|
+
if (!state)
|
|
96
|
+
return;
|
|
97
|
+
state.badUntil = Date.now() + cooldown;
|
|
98
|
+
options.onBlocked?.({ proxy: state.url, status, url: response.url() });
|
|
99
|
+
console.warn(`[bexxcv] dodge: proxy answered ${status}, it is paused for ${Math.round(cooldown / 1000)}s. New contexts will use another proxy.`);
|
|
100
|
+
});
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
}
|
|
@@ -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,6 @@
|
|
|
1
|
+
import { type HumanOptions } from "../human.js";
|
|
2
|
+
import type { BexxcvPlugin } from "./types.js";
|
|
3
|
+
export interface HumanPluginOptions extends HumanOptions {
|
|
4
|
+
think?: [number, number] | false;
|
|
5
|
+
}
|
|
6
|
+
export declare function human(options?: HumanPluginOptions): BexxcvPlugin;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Human } from "../human.js";
|
|
2
|
+
import { between, sleep } from "../random.js";
|
|
3
|
+
const WRAPPED = new WeakSet();
|
|
4
|
+
export function human(options = {}) {
|
|
5
|
+
const think = options.think === false ? null : (options.think ?? [150, 700]);
|
|
6
|
+
const speed = options.speed ?? 1;
|
|
7
|
+
return {
|
|
8
|
+
name: "human",
|
|
9
|
+
order: 60,
|
|
10
|
+
onPage(page) {
|
|
11
|
+
if (WRAPPED.has(page))
|
|
12
|
+
return;
|
|
13
|
+
WRAPPED.add(page);
|
|
14
|
+
const actor = new Human(page, options);
|
|
15
|
+
const pause = async () => {
|
|
16
|
+
if (think)
|
|
17
|
+
await sleep(between(think[0], think[1]) / speed);
|
|
18
|
+
};
|
|
19
|
+
page.click = (async (selector, clickOptions) => {
|
|
20
|
+
await pause();
|
|
21
|
+
await actor.click(selector, { button: clickOptions?.button });
|
|
22
|
+
});
|
|
23
|
+
page.hover = (async (selector) => {
|
|
24
|
+
await pause();
|
|
25
|
+
await actor.hover(selector);
|
|
26
|
+
});
|
|
27
|
+
page.fill = (async (selector, value) => {
|
|
28
|
+
await pause();
|
|
29
|
+
await actor.clear(selector);
|
|
30
|
+
await actor.type(null, value);
|
|
31
|
+
});
|
|
32
|
+
page.type = (async (selector, text) => {
|
|
33
|
+
await pause();
|
|
34
|
+
await actor.type(selector, text);
|
|
35
|
+
});
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
}
|