qwenproxy-cli 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +14 -14
- package/README.md +906 -906
- package/bin/qwenproxy.js +5 -1
- package/package.json +78 -78
- package/src/api/error-classifier.ts +159 -159
- package/src/api/error-helpers.ts +118 -118
- package/src/api/models.ts +261 -261
- package/src/api/server.ts +860 -859
- package/src/cache/memory-cache.ts +385 -385
- package/src/clean-cache.ts +204 -204
- package/src/core/account-concurrency.ts +671 -671
- package/src/core/account-manager.ts +301 -297
- package/src/core/account-priority.ts +163 -163
- package/src/core/accounts.ts +186 -186
- package/src/core/config.ts +383 -383
- package/src/core/crypto-utils.ts +79 -79
- package/src/core/database.ts +276 -276
- package/src/core/errors.ts +118 -118
- package/src/core/logger.ts +269 -269
- package/src/core/memory-usage.ts +84 -84
- package/src/core/metrics.ts +291 -291
- package/src/core/model-alias.ts +77 -77
- package/src/core/model-registry.ts +544 -544
- package/src/core/mutex.ts +119 -119
- package/src/core/paths.ts +199 -199
- package/src/core/prompt-limits.ts +214 -214
- package/src/core/reasoning-effort.ts +102 -102
- package/src/core/stream-registry.ts +96 -96
- package/src/core/waf-isolation.ts +117 -117
- package/src/core/watchdog.ts +195 -195
- package/src/delete-chats.ts +23 -23
- package/src/index.ts +65 -64
- package/src/login.ts +147 -147
- package/src/reset-cooldowns.ts +11 -11
- package/src/routes/anthropic/index.ts +355 -355
- package/src/routes/anthropic/translate.ts +522 -522
- package/src/routes/anthropic/types.ts +154 -154
- package/src/routes/anthropic/validation.ts +144 -144
- package/src/routes/chat/account.ts +1817 -1817
- package/src/routes/chat/context.ts +241 -241
- package/src/routes/chat/errors.ts +85 -85
- package/src/routes/chat/helpers.ts +268 -268
- package/src/routes/chat/index.ts +618 -618
- package/src/routes/chat/media.ts +285 -285
- package/src/routes/chat/retry-policy.ts +754 -754
- package/src/routes/chat/stop.ts +98 -98
- package/src/routes/chat/streaming.ts +2710 -2710
- package/src/routes/chat/validation.ts +526 -526
- package/src/routes/chat.ts +2 -2
- package/src/routes/completions.ts +290 -290
- package/src/routes/images.ts +139 -139
- package/src/routes/responses/adapter.ts +503 -503
- package/src/routes/responses/index.ts +405 -405
- package/src/routes/responses/state.ts +230 -230
- package/src/routes/responses/streaming.ts +528 -528
- package/src/routes/responses/types.ts +285 -285
- package/src/routes/responses/validation.ts +202 -202
- package/src/routes/upload.ts +731 -731
- package/src/routes/videos.ts +214 -214
- package/src/services/auth-playwright.ts +173 -173
- package/src/services/captcha-coordinator.ts +161 -161
- package/src/services/captcha-solver.ts +553 -553
- package/src/services/chat-cleanup.ts +80 -80
- package/src/services/context-meter.ts +317 -317
- package/src/services/fingerprint.ts +242 -242
- package/src/services/human-behavior.ts +173 -173
- package/src/services/media-generation.ts +1748 -1748
- package/src/services/playwright.ts +2800 -2800
- package/src/services/qwen-chat-pool.ts +345 -345
- package/src/services/qwen-errors.ts +133 -133
- package/src/services/qwen-headers.ts +79 -79
- package/src/services/qwen-thread-state.ts +393 -393
- package/src/services/qwen-url.ts +19 -19
- package/src/services/qwen.ts +3126 -3126
- package/src/services/session-keeper.ts +88 -88
- package/src/services/token-estimation-metrics.ts +118 -118
- package/src/sync/claude-code.ts +75 -75
- package/src/sync/codex.ts +123 -123
- package/src/sync/index.ts +362 -362
- package/src/sync/omp.ts +105 -105
- package/src/sync/opencode.ts +214 -214
- package/src/sync/types.ts +53 -53
- package/src/sync/utils.ts +27 -27
- package/src/sync-clients.ts +189 -189
- package/src/tools/instructions.ts +137 -137
- package/src/tools/manifest.ts +81 -81
- package/src/tools/parser.ts +2989 -2989
- package/src/tools/toolcall-tags.ts +142 -142
- package/src/tui/app.ts +264 -264
- package/src/tui/index.ts +61 -61
- package/src/tui/markdown.ts +258 -258
- package/src/tui/proxy-client.ts +331 -326
- package/src/tui/screen.ts +294 -278
- package/src/tui/server-manager.ts +270 -270
- package/src/tui/theme.ts +432 -432
- package/src/tui/types.ts +33 -33
- package/src/tui/views/accounts-view.ts +656 -656
- package/src/tui/views/chat-view.ts +1018 -823
- package/src/tui/views/logs-view.ts +479 -413
- package/src/tui/views/status-view.ts +204 -204
- package/src/tui/views/storage-view.ts +304 -291
- package/src/tui/views/sync-view.ts +409 -409
- package/src/types/ali-oss.d.ts +32 -32
- package/src/utils/context-truncation.ts +84 -84
- package/src/utils/json.ts +380 -380
- package/src/utils/session-id.ts +37 -37
- package/src/utils/tool-call-guard.ts +84 -84
- package/src/utils/types.ts +109 -109
|
@@ -1,96 +1,96 @@
|
|
|
1
|
-
import { metrics } from "./metrics.js";
|
|
2
|
-
|
|
3
|
-
const activeStreams = new Map<
|
|
4
|
-
string,
|
|
5
|
-
{
|
|
6
|
-
abortController: AbortController;
|
|
7
|
-
accountId: string;
|
|
8
|
-
uiSessionId: string;
|
|
9
|
-
targetResponseId: string;
|
|
10
|
-
headers: Record<string, string>;
|
|
11
|
-
/** True once at least one model chunk reached the client. */
|
|
12
|
-
emittedChunk: boolean;
|
|
13
|
-
}
|
|
14
|
-
>();
|
|
15
|
-
|
|
16
|
-
export function registerStream(
|
|
17
|
-
key: string,
|
|
18
|
-
entry: {
|
|
19
|
-
abortController: AbortController;
|
|
20
|
-
accountId: string;
|
|
21
|
-
uiSessionId: string;
|
|
22
|
-
targetResponseId: string;
|
|
23
|
-
headers: Record<string, string>;
|
|
24
|
-
},
|
|
25
|
-
): void {
|
|
26
|
-
const existing = activeStreams.get(key);
|
|
27
|
-
if (existing && existing.abortController !== entry.abortController) {
|
|
28
|
-
existing.abortController.abort();
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
activeStreams.set(key, { emittedChunk: false, ...entry });
|
|
32
|
-
metrics.gauge("streams.active", activeStreams.size);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export function getStream(key: string): ReturnType<typeof activeStreams.get> {
|
|
36
|
-
return activeStreams.get(key);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export function getStreamKeysBySessionId(sessionId: string): string[] {
|
|
40
|
-
const keys: string[] = [];
|
|
41
|
-
for (const [key, entry] of activeStreams.entries()) {
|
|
42
|
-
if (entry.uiSessionId === sessionId) {
|
|
43
|
-
keys.push(key);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
return keys;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export function getStreamKeyBySessionAndResponse(
|
|
50
|
-
sessionId: string,
|
|
51
|
-
responseId: string,
|
|
52
|
-
): string | undefined {
|
|
53
|
-
for (const [key, entry] of activeStreams.entries()) {
|
|
54
|
-
if (
|
|
55
|
-
entry.uiSessionId === sessionId &&
|
|
56
|
-
entry.targetResponseId === responseId
|
|
57
|
-
) {
|
|
58
|
-
return key;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
return undefined;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export function removeStream(key: string): void {
|
|
65
|
-
activeStreams.delete(key);
|
|
66
|
-
metrics.gauge("streams.active", activeStreams.size);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Mark a stream as having emitted at least one model chunk to the client.
|
|
71
|
-
* The emit-aware supersede uses this to avoid killing a generation the client
|
|
72
|
-
* has not consumed yet (e.g. a parallel title request racing the main stream).
|
|
73
|
-
*/
|
|
74
|
-
export function markStreamEmitted(key: string): void {
|
|
75
|
-
const entry = activeStreams.get(key);
|
|
76
|
-
if (entry) {
|
|
77
|
-
entry.emittedChunk = true;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
export function updateStreamTargetResponseId(
|
|
82
|
-
key: string,
|
|
83
|
-
targetResponseId: string,
|
|
84
|
-
): void {
|
|
85
|
-
const entry = activeStreams.get(key);
|
|
86
|
-
if (entry) {
|
|
87
|
-
entry.targetResponseId = targetResponseId;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
export function updateStreamSessionId(key: string, uiSessionId: string): void {
|
|
92
|
-
const entry = activeStreams.get(key);
|
|
93
|
-
if (entry) {
|
|
94
|
-
entry.uiSessionId = uiSessionId;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
1
|
+
import { metrics } from "./metrics.js";
|
|
2
|
+
|
|
3
|
+
const activeStreams = new Map<
|
|
4
|
+
string,
|
|
5
|
+
{
|
|
6
|
+
abortController: AbortController;
|
|
7
|
+
accountId: string;
|
|
8
|
+
uiSessionId: string;
|
|
9
|
+
targetResponseId: string;
|
|
10
|
+
headers: Record<string, string>;
|
|
11
|
+
/** True once at least one model chunk reached the client. */
|
|
12
|
+
emittedChunk: boolean;
|
|
13
|
+
}
|
|
14
|
+
>();
|
|
15
|
+
|
|
16
|
+
export function registerStream(
|
|
17
|
+
key: string,
|
|
18
|
+
entry: {
|
|
19
|
+
abortController: AbortController;
|
|
20
|
+
accountId: string;
|
|
21
|
+
uiSessionId: string;
|
|
22
|
+
targetResponseId: string;
|
|
23
|
+
headers: Record<string, string>;
|
|
24
|
+
},
|
|
25
|
+
): void {
|
|
26
|
+
const existing = activeStreams.get(key);
|
|
27
|
+
if (existing && existing.abortController !== entry.abortController) {
|
|
28
|
+
existing.abortController.abort();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
activeStreams.set(key, { emittedChunk: false, ...entry });
|
|
32
|
+
metrics.gauge("streams.active", activeStreams.size);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function getStream(key: string): ReturnType<typeof activeStreams.get> {
|
|
36
|
+
return activeStreams.get(key);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function getStreamKeysBySessionId(sessionId: string): string[] {
|
|
40
|
+
const keys: string[] = [];
|
|
41
|
+
for (const [key, entry] of activeStreams.entries()) {
|
|
42
|
+
if (entry.uiSessionId === sessionId) {
|
|
43
|
+
keys.push(key);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return keys;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function getStreamKeyBySessionAndResponse(
|
|
50
|
+
sessionId: string,
|
|
51
|
+
responseId: string,
|
|
52
|
+
): string | undefined {
|
|
53
|
+
for (const [key, entry] of activeStreams.entries()) {
|
|
54
|
+
if (
|
|
55
|
+
entry.uiSessionId === sessionId &&
|
|
56
|
+
entry.targetResponseId === responseId
|
|
57
|
+
) {
|
|
58
|
+
return key;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function removeStream(key: string): void {
|
|
65
|
+
activeStreams.delete(key);
|
|
66
|
+
metrics.gauge("streams.active", activeStreams.size);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Mark a stream as having emitted at least one model chunk to the client.
|
|
71
|
+
* The emit-aware supersede uses this to avoid killing a generation the client
|
|
72
|
+
* has not consumed yet (e.g. a parallel title request racing the main stream).
|
|
73
|
+
*/
|
|
74
|
+
export function markStreamEmitted(key: string): void {
|
|
75
|
+
const entry = activeStreams.get(key);
|
|
76
|
+
if (entry) {
|
|
77
|
+
entry.emittedChunk = true;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function updateStreamTargetResponseId(
|
|
82
|
+
key: string,
|
|
83
|
+
targetResponseId: string,
|
|
84
|
+
): void {
|
|
85
|
+
const entry = activeStreams.get(key);
|
|
86
|
+
if (entry) {
|
|
87
|
+
entry.targetResponseId = targetResponseId;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function updateStreamSessionId(key: string, uiSessionId: string): void {
|
|
92
|
+
const entry = activeStreams.get(key);
|
|
93
|
+
if (entry) {
|
|
94
|
+
entry.uiSessionId = uiSessionId;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
@@ -1,117 +1,117 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* WAF hard-block contingency (aligned with upstream account-isolation.ts).
|
|
3
|
-
*
|
|
4
|
-
* When a Baxia/TMD challenge could NOT be solved, quarantining the account is
|
|
5
|
-
* not enough: the fingerprint seed is derived from the accountId, so the
|
|
6
|
-
* account returns from cooldown on the SAME device identity the WAF already
|
|
7
|
-
* flagged, and the challenge re-propagates immediately. The contingency here:
|
|
8
|
-
* - escalates the quarantine window per consecutive hard block (×2, capped),
|
|
9
|
-
* - rotates the account's fingerprint seed (fresh device identity),
|
|
10
|
-
* - closes the account's Playwright context so the next use re-initializes
|
|
11
|
-
* with the rotated profile (cookies/storage persist in the profile dir).
|
|
12
|
-
*
|
|
13
|
-
* Strictly per-account: a block on one account never touches another's state.
|
|
14
|
-
*/
|
|
15
|
-
import { config } from "./config.ts";
|
|
16
|
-
import { logger } from "./logger.ts";
|
|
17
|
-
import { markAccountRateLimited } from "./account-manager.ts";
|
|
18
|
-
import { rotateFingerprintSeed } from "../services/fingerprint.ts";
|
|
19
|
-
|
|
20
|
-
const ESCALATION_FACTOR = 2;
|
|
21
|
-
const MAX_CONSECUTIVE_FOR_ESCALATION = 4;
|
|
22
|
-
|
|
23
|
-
interface WafBlockState {
|
|
24
|
-
consecutiveHardBlocks: number;
|
|
25
|
-
lastBlockAt: number;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const states = new Map<string, WafBlockState>();
|
|
29
|
-
|
|
30
|
-
export interface WafBlockResult {
|
|
31
|
-
cooldownMs: number;
|
|
32
|
-
fingerprintRotated: true;
|
|
33
|
-
escalated: boolean;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
type ContextResetListener = (accountId: string) => void | Promise<void>;
|
|
37
|
-
let contextResetListener: ContextResetListener | null = null;
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Register the hook that physically resets an account's browser context after
|
|
41
|
-
* a fingerprint rotation. Injected by the Playwright layer so this module
|
|
42
|
-
* stays decoupled from it (and unit-testable without a browser).
|
|
43
|
-
*/
|
|
44
|
-
export function setWafContextResetListener(fn: ContextResetListener | null): void {
|
|
45
|
-
contextResetListener = fn;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function getState(accountId: string): WafBlockState {
|
|
49
|
-
let state = states.get(accountId);
|
|
50
|
-
if (!state) {
|
|
51
|
-
state = { consecutiveHardBlocks: 0, lastBlockAt: 0 };
|
|
52
|
-
states.set(accountId, state);
|
|
53
|
-
}
|
|
54
|
-
return state;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export function getWafHardBlockCount(accountId: string): number {
|
|
58
|
-
return states.get(accountId)?.consecutiveHardBlocks ?? 0;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Record an unsolvable WAF challenge on `accountId`: quarantine (escalating),
|
|
63
|
-
* rotate the device fingerprint, and reset the browser context.
|
|
64
|
-
*/
|
|
65
|
-
export function recordWafHardBlock(accountId: string): WafBlockResult {
|
|
66
|
-
const state = getState(accountId);
|
|
67
|
-
state.consecutiveHardBlocks += 1;
|
|
68
|
-
state.lastBlockAt = Date.now();
|
|
69
|
-
|
|
70
|
-
const base = config.captcha.accountCooldownMs;
|
|
71
|
-
const cap = config.captcha.hardBlockMaxCooldownMs;
|
|
72
|
-
const exponent = Math.min(
|
|
73
|
-
state.consecutiveHardBlocks - 1,
|
|
74
|
-
MAX_CONSECUTIVE_FOR_ESCALATION,
|
|
75
|
-
);
|
|
76
|
-
const cooldownMs = Math.min(cap, base * Math.pow(ESCALATION_FACTOR, exponent));
|
|
77
|
-
|
|
78
|
-
if (cooldownMs > 0) {
|
|
79
|
-
markAccountRateLimited(accountId, cooldownMs, "WafChallenge");
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
rotateFingerprintSeed(accountId);
|
|
83
|
-
logger.warn(
|
|
84
|
-
`[WafIsolation] Hard WAF block on ${accountId}: fingerprint rotated (streak ${state.consecutiveHardBlocks}), quarantined ${Math.round(cooldownMs / 1000)}s`,
|
|
85
|
-
);
|
|
86
|
-
|
|
87
|
-
if (contextResetListener) {
|
|
88
|
-
// Best-effort: the next use re-initializes the context with the rotated
|
|
89
|
-
// profile; a failed close must not abort the quarantine.
|
|
90
|
-
void Promise.resolve(contextResetListener(accountId)).catch((error: unknown) => {
|
|
91
|
-
logger.warn(
|
|
92
|
-
`[WafIsolation] Context reset listener failed for ${accountId}: ${
|
|
93
|
-
error instanceof Error ? error.message : String(error)
|
|
94
|
-
}`,
|
|
95
|
-
);
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
return {
|
|
100
|
-
cooldownMs,
|
|
101
|
-
fingerprintRotated: true,
|
|
102
|
-
escalated: state.consecutiveHardBlocks > 1,
|
|
103
|
-
};
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/** A successful stream on the account clears the escalation streak. */
|
|
107
|
-
export function noteWafRecovery(accountId: string): void {
|
|
108
|
-
const state = states.get(accountId);
|
|
109
|
-
if (state && state.consecutiveHardBlocks > 0) {
|
|
110
|
-
state.consecutiveHardBlocks = 0;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/** Test/admin helper: drop all isolation state for an account. */
|
|
115
|
-
export function clearWafIsolation(accountId: string): void {
|
|
116
|
-
states.delete(accountId);
|
|
117
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* WAF hard-block contingency (aligned with upstream account-isolation.ts).
|
|
3
|
+
*
|
|
4
|
+
* When a Baxia/TMD challenge could NOT be solved, quarantining the account is
|
|
5
|
+
* not enough: the fingerprint seed is derived from the accountId, so the
|
|
6
|
+
* account returns from cooldown on the SAME device identity the WAF already
|
|
7
|
+
* flagged, and the challenge re-propagates immediately. The contingency here:
|
|
8
|
+
* - escalates the quarantine window per consecutive hard block (×2, capped),
|
|
9
|
+
* - rotates the account's fingerprint seed (fresh device identity),
|
|
10
|
+
* - closes the account's Playwright context so the next use re-initializes
|
|
11
|
+
* with the rotated profile (cookies/storage persist in the profile dir).
|
|
12
|
+
*
|
|
13
|
+
* Strictly per-account: a block on one account never touches another's state.
|
|
14
|
+
*/
|
|
15
|
+
import { config } from "./config.ts";
|
|
16
|
+
import { logger } from "./logger.ts";
|
|
17
|
+
import { markAccountRateLimited } from "./account-manager.ts";
|
|
18
|
+
import { rotateFingerprintSeed } from "../services/fingerprint.ts";
|
|
19
|
+
|
|
20
|
+
const ESCALATION_FACTOR = 2;
|
|
21
|
+
const MAX_CONSECUTIVE_FOR_ESCALATION = 4;
|
|
22
|
+
|
|
23
|
+
interface WafBlockState {
|
|
24
|
+
consecutiveHardBlocks: number;
|
|
25
|
+
lastBlockAt: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const states = new Map<string, WafBlockState>();
|
|
29
|
+
|
|
30
|
+
export interface WafBlockResult {
|
|
31
|
+
cooldownMs: number;
|
|
32
|
+
fingerprintRotated: true;
|
|
33
|
+
escalated: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
type ContextResetListener = (accountId: string) => void | Promise<void>;
|
|
37
|
+
let contextResetListener: ContextResetListener | null = null;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Register the hook that physically resets an account's browser context after
|
|
41
|
+
* a fingerprint rotation. Injected by the Playwright layer so this module
|
|
42
|
+
* stays decoupled from it (and unit-testable without a browser).
|
|
43
|
+
*/
|
|
44
|
+
export function setWafContextResetListener(fn: ContextResetListener | null): void {
|
|
45
|
+
contextResetListener = fn;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function getState(accountId: string): WafBlockState {
|
|
49
|
+
let state = states.get(accountId);
|
|
50
|
+
if (!state) {
|
|
51
|
+
state = { consecutiveHardBlocks: 0, lastBlockAt: 0 };
|
|
52
|
+
states.set(accountId, state);
|
|
53
|
+
}
|
|
54
|
+
return state;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function getWafHardBlockCount(accountId: string): number {
|
|
58
|
+
return states.get(accountId)?.consecutiveHardBlocks ?? 0;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Record an unsolvable WAF challenge on `accountId`: quarantine (escalating),
|
|
63
|
+
* rotate the device fingerprint, and reset the browser context.
|
|
64
|
+
*/
|
|
65
|
+
export function recordWafHardBlock(accountId: string): WafBlockResult {
|
|
66
|
+
const state = getState(accountId);
|
|
67
|
+
state.consecutiveHardBlocks += 1;
|
|
68
|
+
state.lastBlockAt = Date.now();
|
|
69
|
+
|
|
70
|
+
const base = config.captcha.accountCooldownMs;
|
|
71
|
+
const cap = config.captcha.hardBlockMaxCooldownMs;
|
|
72
|
+
const exponent = Math.min(
|
|
73
|
+
state.consecutiveHardBlocks - 1,
|
|
74
|
+
MAX_CONSECUTIVE_FOR_ESCALATION,
|
|
75
|
+
);
|
|
76
|
+
const cooldownMs = Math.min(cap, base * Math.pow(ESCALATION_FACTOR, exponent));
|
|
77
|
+
|
|
78
|
+
if (cooldownMs > 0) {
|
|
79
|
+
markAccountRateLimited(accountId, cooldownMs, "WafChallenge");
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
rotateFingerprintSeed(accountId);
|
|
83
|
+
logger.warn(
|
|
84
|
+
`[WafIsolation] Hard WAF block on ${accountId}: fingerprint rotated (streak ${state.consecutiveHardBlocks}), quarantined ${Math.round(cooldownMs / 1000)}s`,
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
if (contextResetListener) {
|
|
88
|
+
// Best-effort: the next use re-initializes the context with the rotated
|
|
89
|
+
// profile; a failed close must not abort the quarantine.
|
|
90
|
+
void Promise.resolve(contextResetListener(accountId)).catch((error: unknown) => {
|
|
91
|
+
logger.warn(
|
|
92
|
+
`[WafIsolation] Context reset listener failed for ${accountId}: ${
|
|
93
|
+
error instanceof Error ? error.message : String(error)
|
|
94
|
+
}`,
|
|
95
|
+
);
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return {
|
|
100
|
+
cooldownMs,
|
|
101
|
+
fingerprintRotated: true,
|
|
102
|
+
escalated: state.consecutiveHardBlocks > 1,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** A successful stream on the account clears the escalation streak. */
|
|
107
|
+
export function noteWafRecovery(accountId: string): void {
|
|
108
|
+
const state = states.get(accountId);
|
|
109
|
+
if (state && state.consecutiveHardBlocks > 0) {
|
|
110
|
+
state.consecutiveHardBlocks = 0;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Test/admin helper: drop all isolation state for an account. */
|
|
115
|
+
export function clearWafIsolation(accountId: string): void {
|
|
116
|
+
states.delete(accountId);
|
|
117
|
+
}
|