mslxdff 0.1.199 → 0.1.203
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 +3 -3
- package/bin/mslxdff.js +1 -1
- package/package.json +38 -38
- package/plugins/README.md +14 -14
- package/plugins/prefer-model.mjs.example +14 -14
- package/src/anthropic/count-tokens.js +61 -61
- package/src/anthropic/request-body.js +126 -126
- package/src/anthropic/shared.js +18 -18
- package/src/anthropic/sse-aggregate.js +56 -56
- package/src/anthropic/translate.js +287 -287
- package/src/anthropic/web-search.js +361 -361
- package/src/api-kind.js +31 -31
- package/src/autostart.js +288 -288
- package/src/bench/probe.js +1 -1
- package/src/bench/runner.js +1 -1
- package/src/bench/via-probe.js +1 -1
- package/src/bench/via.js +144 -144
- package/src/bench/workbuddy-bench.js +1 -1
- package/src/chat/config.js +11 -11
- package/src/chat/cooling.js +81 -81
- package/src/chat/direct.js +58 -58
- package/src/chat/index.js +6 -6
- package/src/chat/spinner.js +38 -38
- package/src/chat/sse.js +69 -69
- package/src/chat/stats.js +247 -247
- package/src/chat/store.js +48 -48
- package/src/chat/tool-handlers.js +67 -67
- package/src/chat/tools.js +1 -1
- package/src/chat/upstream.js +59 -59
- package/src/chat-pipeline/auto-race.js +158 -158
- package/src/chat-pipeline/empty-turn.js +147 -147
- package/src/chat-pipeline/engine.js +17 -17
- package/src/chat-pipeline/serial-trial.js +283 -283
- package/src/chat-pipeline/terminal-verdict.js +147 -147
- package/src/chooser.js +54 -54
- package/src/claude-desktop/apply.js +101 -101
- package/src/claude-desktop/context1m.js +38 -38
- package/src/claude-desktop/doctor.js +146 -146
- package/src/claude-desktop/slots.js +152 -152
- package/src/cli/bootstrap.js +1 -1
- package/src/cli/commands/claude-desktop.js +284 -284
- package/src/cli/commands/daemon.js +231 -231
- package/src/cli/commands/model/list-live.js +22 -22
- package/src/cli/commands/model/list-providers.js +76 -76
- package/src/cli/commands/model/list-render.js +77 -77
- package/src/cli/commands/model/list-sort.js +52 -52
- package/src/cli/commands/model/list-tty.js +76 -76
- package/src/cli/commands/model/list.js +166 -166
- package/src/cli/commands/model/live-models.js +24 -24
- package/src/cli/commands/model/stats.js +43 -43
- package/src/cli/commands/model/status.js +47 -47
- package/src/cli/commands/model.js +51 -51
- package/src/cli/commands/provider/add.js +108 -108
- package/src/cli/commands/provider/allowlist.js +99 -99
- package/src/cli/commands/provider/cline-free.js +99 -99
- package/src/cli/commands/provider/cline-login.js +218 -218
- package/src/cli/commands/provider/cline-quota.js +65 -65
- package/src/cli/commands/provider/codearts-login.js +45 -45
- package/src/cli/commands/provider/config.js +119 -119
- package/src/cli/commands/provider/globalqwenwork-login.js +126 -126
- package/src/cli/commands/provider/keys-guard.js +50 -50
- package/src/cli/commands/provider/keys.js +161 -161
- package/src/cli/commands/provider/qoder-checkin.js +81 -81
- package/src/cli/commands/provider/qoder-login.js +102 -102
- package/src/cli/commands/provider/raccoon-login.js +180 -180
- package/src/cli/commands/provider/raccoon-password.js +162 -162
- package/src/cli/commands/provider/raccoon-quota.js +109 -109
- package/src/cli/commands/provider/traework-checkin.js +144 -144
- package/src/cli/commands/provider/traework-login.js +164 -164
- package/src/cli/commands/provider/workbuddy-login.js +153 -153
- package/src/cli/commands/provider/zcode-login.js +77 -77
- package/src/cli/commands/provider/zcode-quota.js +55 -55
- package/src/cli/commands/provider.js +1 -1
- package/src/cli/commands/stats.js +149 -149
- package/src/cli/commands/system.js +261 -261
- package/src/cli/commands/timezone.js +47 -47
- package/src/cli/commands/use-group.js +60 -60
- package/src/cli/commands/workbuddy.js +189 -189
- package/src/cli/format.js +141 -141
- package/src/cli/group-helpers.js +70 -70
- package/src/cli/help.js +80 -80
- package/src/cli/index.js +70 -70
- package/src/cli/interactive.js +121 -121
- package/src/cli/join-core.js +99 -99
- package/src/cli/join-wizard.js +175 -175
- package/src/cli/policy.js +136 -136
- package/src/cli/provider-row.js +126 -126
- package/src/cli/util.js +24 -24
- package/src/compat.js +53 -53
- package/src/events.js +38 -38
- package/src/free-lane.js +131 -131
- package/src/free-watcher.js +63 -63
- package/src/groups.js +262 -262
- package/src/logs.js +195 -195
- package/src/metrics.js +62 -62
- package/src/model-capabilities/context-lookup.js +65 -65
- package/src/model-capabilities/merge.js +142 -142
- package/src/model-trace.js +187 -187
- package/src/models.js +1 -1
- package/src/opencode-identity.js +72 -72
- package/src/peers.js +254 -251
- package/src/plugins.js +107 -107
- package/src/providers/base.js +164 -164
- package/src/providers/classify.js +22 -22
- package/src/providers/cline/allowlist-sync.js +93 -93
- package/src/providers/cline/free-catalog.js +71 -71
- package/src/providers/cline/headers.js +37 -37
- package/src/providers/cline/index.js +130 -130
- package/src/providers/cline/models.js +150 -150
- package/src/providers/cline/usage.js +274 -274
- package/src/providers/codearts/auth-pool.js +148 -148
- package/src/providers/codearts/concurrency.js +34 -34
- package/src/providers/codearts/const.js +65 -65
- package/src/providers/codearts/dpop.js +95 -95
- package/src/providers/codearts/index.js +175 -175
- package/src/providers/codearts/login.js +214 -214
- package/src/providers/codearts/models.js +182 -182
- package/src/providers/codearts/sign.js +96 -96
- package/src/providers/codearts/sse.js +204 -204
- package/src/providers/codearts/stream.js +209 -209
- package/src/providers/codearts/sts.js +124 -124
- package/src/providers/codearts.js +1 -1
- package/src/providers/dispatcher.js +146 -146
- package/src/providers/generic.js +156 -156
- package/src/providers/globalqwenwork/account-store.js +141 -141
- package/src/providers/globalqwenwork/constants.js +73 -73
- package/src/providers/globalqwenwork/crypto.js +2 -2
- package/src/providers/globalqwenwork/http.js +2 -2
- package/src/providers/globalqwenwork/index.js +331 -331
- package/src/providers/globalqwenwork/payload.js +147 -147
- package/src/providers/globalqwenwork/rsa.js +2 -2
- package/src/providers/globalqwenwork/sse.js +2 -2
- package/src/providers/globalqwenwork/stream.js +2 -2
- package/src/providers/globalqwenwork.js +1 -1
- package/src/providers/globalworkbuddy/account-store.js +198 -198
- package/src/providers/globalworkbuddy/auth.js +97 -97
- package/src/providers/globalworkbuddy/catalog.js +138 -138
- package/src/providers/globalworkbuddy/constants.js +73 -73
- package/src/providers/globalworkbuddy/device-auth.js +120 -120
- package/src/providers/globalworkbuddy/errors.js +98 -98
- package/src/providers/globalworkbuddy/headers.js +89 -89
- package/src/providers/globalworkbuddy/models.js +96 -96
- package/src/providers/globalworkbuddy/payload.js +80 -80
- package/src/providers/index.js +5 -5
- package/src/providers/model-id.js +172 -172
- package/src/providers/opencode.js +19 -19
- package/src/providers/openrouter.js +31 -31
- package/src/providers/qoder/account-store.js +106 -106
- package/src/providers/qoder/aggregate.js +60 -60
- package/src/providers/qoder/baseprompt.json +591 -591
- package/src/providers/qoder/checkin.js +160 -160
- package/src/providers/qoder/constants.js +44 -44
- package/src/providers/qoder/encode.js +41 -41
- package/src/providers/qoder/fingerprint.js +38 -38
- package/src/providers/qoder/models.js +77 -77
- package/src/providers/qoder/oauth.js +84 -84
- package/src/providers/qoder/payload.js +162 -162
- package/src/providers/qoder/request.js +27 -27
- package/src/providers/qoder/session.js +134 -134
- package/src/providers/qoder/sticky.js +38 -38
- package/src/providers/qoder/stream.js +116 -116
- package/src/providers/qoder.js +1 -1
- package/src/providers/raccoon/account-store.js +216 -216
- package/src/providers/raccoon/auth.js +67 -67
- package/src/providers/raccoon/chat.js +228 -228
- package/src/providers/raccoon/const.js +180 -165
- package/src/providers/raccoon/credits.js +185 -175
- package/src/providers/raccoon/crypto.js +43 -0
- package/src/providers/raccoon/envelope.js +31 -31
- package/src/providers/raccoon/headers.js +45 -45
- package/src/providers/raccoon/index.js +216 -216
- package/src/providers/raccoon/login.js +279 -279
- package/src/providers/raccoon/models.js +160 -160
- package/src/providers/raccoon/password-login.js +102 -87
- package/src/providers/raccoon/qr-codewords.js +115 -115
- package/src/providers/raccoon/qr.js +236 -236
- package/src/providers/raccoon/sse.js +135 -135
- package/src/providers/responses-channel.js +120 -120
- package/src/providers/share-keys.js +53 -53
- package/src/providers/traework/account-store.js +234 -234
- package/src/providers/traework/chat.js +217 -217
- package/src/providers/traework/checkin.js +77 -77
- package/src/providers/traework/constants.js +41 -41
- package/src/providers/traework/errors.js +52 -52
- package/src/providers/traework/headers.js +79 -79
- package/src/providers/traework/index.js +132 -132
- package/src/providers/traework/market-identity.js +116 -116
- package/src/providers/traework/models.js +98 -98
- package/src/providers/traework/payload.js +86 -86
- package/src/providers/traework/sse.js +184 -184
- package/src/providers/traework/token.js +52 -52
- package/src/providers/traework.js +2 -2
- package/src/providers/workbuddy/account-store.js +186 -186
- package/src/providers/workbuddy/auth.js +158 -158
- package/src/providers/workbuddy/balance.js +86 -86
- package/src/providers/workbuddy/cat-travel.js +125 -125
- package/src/providers/workbuddy/checkin.js +75 -75
- package/src/providers/workbuddy/desktop-info.js +78 -78
- package/src/providers/workbuddy/growth-api.js +67 -67
- package/src/providers/workbuddy/growth-plans.js +63 -63
- package/src/providers/workbuddy/growth.js +181 -181
- package/src/providers/workbuddy/index.js +217 -217
- package/src/providers/workbuddy/models.js +126 -126
- package/src/providers/workbuddy/payload.js +141 -141
- package/src/providers/workbuddy/rotation-log.js +83 -83
- package/src/providers/workbuddy/sanitize-tools.js +61 -61
- package/src/providers/workbuddy/sdk-chat.js +26 -26
- package/src/providers/workbuddy-balance.js +78 -78
- package/src/providers/zcode/account-store.js +129 -129
- package/src/providers/zcode/auth.js +28 -28
- package/src/providers/zcode/chat.js +179 -179
- package/src/providers/zcode/const.js +54 -54
- package/src/providers/zcode/context-shape.js +162 -162
- package/src/providers/zcode/headers.js +93 -93
- package/src/providers/zcode/index.js +124 -124
- package/src/providers/zcode/models.js +65 -65
- package/src/providers/zcode/oauth.js +120 -120
- package/src/providers/zcode/quota.js +176 -176
- package/src/providers/zcode/sse.js +196 -196
- package/src/readline-compat.js +25 -25
- package/src/responses/translate.js +311 -311
- package/src/routes/chat/broadband-handler.js +72 -72
- package/src/routes/chat/empty-turn-report.js +31 -31
- package/src/routes/chat/exhausted-handler.js +87 -87
- package/src/routes/chat/hedge-handler.js +130 -130
- package/src/routes/chat/index.js +1 -1
- package/src/routes/chat/local-handler.js +63 -63
- package/src/routes/chat/peer-handler.js +81 -81
- package/src/routes/chat.js +3 -3
- package/src/routes/fallback.js +88 -88
- package/src/routes/groups-relay.js +214 -214
- package/src/routes/groups.js +122 -122
- package/src/routes/helpers.js +83 -83
- package/src/routes/index.js +147 -147
- package/src/routes/messages-route.js +365 -365
- package/src/routes/peers.js +23 -1
- package/src/routes/relay-queue.js +181 -181
- package/src/routes/relay.js +60 -60
- package/src/routes/responses-route.js +213 -213
- package/src/routes/stream-body.js +84 -84
- package/src/routes/stream-hold.js +337 -337
- package/src/routes/stream-scan.js +366 -366
- package/src/routes.js +3 -3
- package/src/runtime/auto-update.js +113 -113
- package/src/runtime/bootstrap.js +8 -0
- package/src/runtime/broadband-stream.js +79 -79
- package/src/runtime/broadband.js +103 -103
- package/src/runtime/group-sync.js +81 -81
- package/src/runtime/keep-warm.js +120 -0
- package/src/runtime/lifecycle-forensics.js +116 -116
- package/src/runtime/lifecycle-log.js +87 -87
- package/src/runtime/log-stamp.js +73 -73
- package/src/runtime/proc-info.js +137 -137
- package/src/runtime/qoder-checkin.js +93 -93
- package/src/runtime/raccoon-checkin.js +126 -126
- package/src/runtime/server-lifecycle.js +189 -189
- package/src/runtime/traework-checkin.js +118 -118
- package/src/runtime/workbuddy-checkin.js +120 -120
- package/src/runtime/workbuddy-growth.js +133 -133
- package/src/server.js +7 -0
- package/src/state/facade.js +58 -58
- package/src/state/memory.js +162 -162
- package/src/state/merge.js +25 -25
- package/src/state/migrations/cline-unify.js +94 -94
- package/src/state/migrations.js +33 -33
- package/src/state/persist.js +42 -42
- package/src/state/provider-config.js +142 -142
- package/src/state/schemas/group.js +31 -31
- package/src/state/schemas/model.js +94 -94
- package/src/state/schemas/peer.js +31 -31
- package/src/state/schemas/port.js +10 -10
- package/src/state/schemas/provider.js +202 -202
- package/src/state/schemas/timezone.js +70 -70
- package/src/state/schemas/token.js +48 -48
- package/src/state/schemas/use-group.js +102 -102
- package/src/state/store.js +176 -176
- package/src/state.js +1 -1
- package/src/sync-claude-desktop.js +234 -234
- package/src/sync-claude.js +152 -152
- package/src/sync-codex.js +116 -116
- package/src/sync-workbuddy.js +158 -158
- package/src/talk-full-redact.js +129 -129
- package/src/talk-full.js +131 -131
- package/src/talk-log.js +257 -257
- package/src/text-tokens.js +52 -52
- package/src/thinking-effort.js +67 -67
- package/src/time.js +106 -106
- package/src/timeline.js +26 -26
- package/src/transport/index.js +1 -1
- package/src/upstream-engine/index.js +74 -72
- package/src/upstream-engine/mode.js +15 -15
- package/src/upstream-engine/sdk/attempt.js +221 -221
- package/src/upstream-engine/sdk/chat.js +28 -28
- package/src/upstream-engine/sdk/convert.js +192 -192
- package/src/upstream-engine/sdk/diagnose.js +62 -62
- package/src/upstream-engine/sdk/dispatch.js +55 -55
- package/src/upstream-engine/sdk/responses.js +176 -176
- package/src/upstream-engine/sdk/sse.js +166 -166
- package/src/upstream-responses.js +305 -305
- package/src/upstream.js +53 -1
- package/src/usage/record.js +106 -106
- package/src/usage/report.js +195 -195
- package/src/v6pool/claim.js +157 -157
- package/src/v6pool/config.js +65 -65
- package/src/v6pool/detect.js +130 -130
- package/src/v6pool/index.js +152 -152
- package/src/v6pool/prefix.js +140 -140
- package/src/v6pool/probe.js +40 -40
- package/src/v6pool/report.js +104 -104
- package/src/v6pool/rotate.js +263 -239
- package/src/warp-pool/config.js +74 -74
- package/src/warp-pool/health.js +106 -106
- package/src/warp-pool/identity.js +141 -141
- package/src/warp-pool/index.js +358 -358
- package/src/warp-pool/keys.js +29 -29
- package/src/warp-pool/nodes.js +95 -95
- package/src/warp-pool/ports.js +160 -160
- package/src/warp-pool/register.js +113 -113
- package/src/warp-pool/report.js +232 -232
- package/src/warp-pool/revive.js +168 -168
- package/src/warp-pool/rotate.js +214 -214
- package/src/warp-pool/strategy.js +43 -43
- package/src/warp-pool/verdict.js +69 -69
- package/src/warp-pool/wireproxy.js +162 -162
- package/docs/cli_help_mini.md +0 -146
- package/src/providers/AGENTS.md +0 -49
- package/src/routes/AGENTS.md +0 -37
package/src/routes/peers.js
CHANGED
|
@@ -52,7 +52,7 @@ export function peerStageCtx(ctx = {}) {
|
|
|
52
52
|
// 连接级超时(DNS + TCP/TLS 握手):黑洞节点(端口挂起)必须快速失败,
|
|
53
53
|
// 不能占用 30s 的响应超时(曾把一次请求拖到 92s)。仅 undici 可用时生效。
|
|
54
54
|
let peerDispatcher = null;
|
|
55
|
-
function getPeerDispatcher() {
|
|
55
|
+
export function getPeerDispatcher() {
|
|
56
56
|
if (peerDispatcher) return peerDispatcher;
|
|
57
57
|
const { Agent } = getUndici();
|
|
58
58
|
if (!Agent) return null;
|
|
@@ -68,6 +68,28 @@ function getPeerDispatcher() {
|
|
|
68
68
|
return peerDispatcher;
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
+
/**
|
|
72
|
+
* 保热:用**组员转发同一条连接池**(getPeerDispatcher)打 GET /health,把 (本机→组员) 的连接
|
|
73
|
+
* 保持在温热、且**正是 chat 会复用的那条 socket**(裸 fetch 的全局池热的是另一条,等于白热)。
|
|
74
|
+
* /health 无鉴权、无副作用、零额度。绝不抛;返回 {ok}。
|
|
75
|
+
*/
|
|
76
|
+
export async function warmPeerHealth(url, { timeoutMs = 5000 } = {}) {
|
|
77
|
+
const base = String(url || "").replace(/\/+$/, "");
|
|
78
|
+
if (!base) return { ok: false, error: "no url" };
|
|
79
|
+
const dispatcher = getPeerDispatcher();
|
|
80
|
+
try {
|
|
81
|
+
const res = await compatFetch(`${base}/health`, {
|
|
82
|
+
headers: { Accept: "application/json" },
|
|
83
|
+
signal: timeoutSignal(timeoutMs),
|
|
84
|
+
...(dispatcher ? { dispatcher } : {}),
|
|
85
|
+
});
|
|
86
|
+
try { if (res.body) await res.text().catch(() => {}); } catch {}
|
|
87
|
+
return { ok: Boolean(res?.ok), status: res?.status };
|
|
88
|
+
} catch (e) {
|
|
89
|
+
return { ok: false, error: errMsg(e) };
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
71
93
|
// 跨 caller 中继必须剥离 reasoning 加密态:encrypted_content 由上游按 caller(出口)签发,
|
|
72
94
|
// 换个节点转发会被拒(400 "reasoning encrypted_content was not issued to this caller")。
|
|
73
95
|
// 明文 reasoning_content 保留(不绑定 caller,thinking 模式需要)。
|
|
@@ -1,181 +1,181 @@
|
|
|
1
|
-
import { loadGroupsJoined } from "../state.js";
|
|
2
|
-
import { compatFetch } from "../compat.js";
|
|
3
|
-
|
|
4
|
-
const relayPending = new Map();
|
|
5
|
-
const relayPendingByReqId = new Map();
|
|
6
|
-
const streamSubscribers = new Map(); // key group::target -> Set<res>
|
|
7
|
-
|
|
8
|
-
function streamKey(group, target) { return `${group}::${target}`; }
|
|
9
|
-
|
|
10
|
-
export function subscribeStream({ group, target, res }) {
|
|
11
|
-
const k = streamKey(group, target);
|
|
12
|
-
const set = streamSubscribers.get(k) || new Set();
|
|
13
|
-
set.add(res);
|
|
14
|
-
streamSubscribers.set(k, set);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export function unsubscribeStream({ group, target, res }) {
|
|
18
|
-
const k = streamKey(group, target);
|
|
19
|
-
const set = streamSubscribers.get(k);
|
|
20
|
-
if (!set) return;
|
|
21
|
-
set.delete(res);
|
|
22
|
-
if (set.size === 0) streamSubscribers.delete(k);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export function clearStreamSubscribers() { streamSubscribers.clear(); }
|
|
26
|
-
export function getStreamSubscribers() { return streamSubscribers; }
|
|
27
|
-
|
|
28
|
-
export function pushToStream({ group, target, entry }) {
|
|
29
|
-
const k = streamKey(group, target);
|
|
30
|
-
const set = streamSubscribers.get(k);
|
|
31
|
-
if (!set || set.size === 0) return false;
|
|
32
|
-
const payload = JSON.stringify({ reqId: entry.reqId, body: entry.body, hops: entry.hops });
|
|
33
|
-
const frame = `event: relay\ndata: ${payload}\n\n`;
|
|
34
|
-
let pushed = 0;
|
|
35
|
-
for (const res of [...set]) {
|
|
36
|
-
try {
|
|
37
|
-
res.write(frame);
|
|
38
|
-
pushed++;
|
|
39
|
-
} catch {
|
|
40
|
-
set.delete(res);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
if (set.size === 0) streamSubscribers.delete(k);
|
|
44
|
-
return pushed > 0;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export function enqueueRelay({ group, target, reqId, body, hops }) {
|
|
48
|
-
const key = `${group}::${target}`;
|
|
49
|
-
// try stream push first — if any subscriber, push and don't enqueue to poll queue
|
|
50
|
-
const entryForPush = { reqId, body, hops };
|
|
51
|
-
if (pushToStream({ group, target, entry: entryForPush })) {
|
|
52
|
-
return new Promise((resolve, reject) => {
|
|
53
|
-
const timer = setTimeout(() => {
|
|
54
|
-
relayPendingByReqId.delete(reqId);
|
|
55
|
-
reject(new Error("relay timeout"));
|
|
56
|
-
}, 30_000);
|
|
57
|
-
timer.unref?.();
|
|
58
|
-
const entry = { reqId, body, hops, resolve, reject, timer };
|
|
59
|
-
relayPendingByReqId.set(reqId, entry);
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
const list = relayPending.get(key) || [];
|
|
63
|
-
return new Promise((resolve, reject) => {
|
|
64
|
-
const timer = setTimeout(() => {
|
|
65
|
-
const idx = list.findIndex((e) => e.reqId === reqId);
|
|
66
|
-
if (idx >= 0) list.splice(idx, 1);
|
|
67
|
-
relayPendingByReqId.delete(reqId);
|
|
68
|
-
reject(new Error("relay timeout"));
|
|
69
|
-
}, 30_000);
|
|
70
|
-
timer.unref?.();
|
|
71
|
-
const entry = { reqId, body, hops, resolve, reject, timer };
|
|
72
|
-
list.push(entry);
|
|
73
|
-
relayPending.set(key, list);
|
|
74
|
-
relayPendingByReqId.set(reqId, entry);
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export function dequeueRelayForPoll({ group, target, limit = 10 }) {
|
|
79
|
-
const key = `${group}::${target}`;
|
|
80
|
-
const list = relayPending.get(key) || [];
|
|
81
|
-
const batch = list.splice(0, limit);
|
|
82
|
-
if (list.length) relayPending.set(key, list);
|
|
83
|
-
else relayPending.delete(key);
|
|
84
|
-
return batch.map((e) => ({ reqId: e.reqId, body: e.body, hops: e.hops }));
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export function resolveRelay(reqId, result) {
|
|
88
|
-
const entry = relayPendingByReqId.get(reqId);
|
|
89
|
-
if (!entry) return false;
|
|
90
|
-
clearTimeout(entry.timer);
|
|
91
|
-
relayPendingByReqId.delete(reqId);
|
|
92
|
-
entry.resolve(result);
|
|
93
|
-
return true;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
export function getRelayPending() { return relayPending; }
|
|
97
|
-
export function getRelayPendingByReqId() { return relayPendingByReqId; }
|
|
98
|
-
|
|
99
|
-
export async function tryBroadbandRelay({ groups, token: myToken, model, body, hops, bus, logs, reqId, evt, res, mark, perf0, stages }) {
|
|
100
|
-
try {
|
|
101
|
-
const joined = loadGroupsJoined();
|
|
102
|
-
const broadbandGroups = joined.filter((g) => g.kind === "broadband" || g.myUrl?.startsWith("relay://"));
|
|
103
|
-
const allCandidates = [];
|
|
104
|
-
if (groups) {
|
|
105
|
-
const localGroups = groups.list();
|
|
106
|
-
for (const [gName, g] of Object.entries(localGroups)) {
|
|
107
|
-
for (const [id, m] of Object.entries(g.members || {})) {
|
|
108
|
-
if (id === "leader") continue;
|
|
109
|
-
const isBb = m?.kind === "broadband" || String(m?.url || "").startsWith("relay://");
|
|
110
|
-
if (!isBb) continue;
|
|
111
|
-
const staleMs = Number(process.env.MSLXDFF_BROADBAND_STALE_MS) > 0 ? Number(process.env.MSLXDFF_BROADBAND_STALE_MS) : 90_000;
|
|
112
|
-
if (typeof m.lastSeen === "number" && Date.now() - m.lastSeen > staleMs) continue;
|
|
113
|
-
allCandidates.push({ group: gName, target: m.url, member: m, via: "local-leader", leaderUrl: null });
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
for (const g of joined) {
|
|
118
|
-
if (!g.leaderUrl) continue;
|
|
119
|
-
try {
|
|
120
|
-
const controller = new AbortController();
|
|
121
|
-
const timer = setTimeout(() => controller.abort(), 5000);
|
|
122
|
-
const r = await compatFetch(`${g.leaderUrl}/v1/groups/join`, {
|
|
123
|
-
method: "POST",
|
|
124
|
-
headers: { "Content-Type": "application/json", "Authorization": `Bearer ${myToken}` },
|
|
125
|
-
body: JSON.stringify({ name: g.name, memberName: g.memberName, url: g.myUrl, token: myToken }),
|
|
126
|
-
signal: controller.signal,
|
|
127
|
-
});
|
|
128
|
-
clearTimeout(timer);
|
|
129
|
-
if (!r.ok) continue;
|
|
130
|
-
const data = await r.json().catch(() => ({}));
|
|
131
|
-
const members = data.members || {};
|
|
132
|
-
for (const [id, m] of Object.entries(members)) {
|
|
133
|
-
if (id === "leader") continue;
|
|
134
|
-
const isBb = m?.kind === "broadband" || String(m?.url || "").startsWith("relay://");
|
|
135
|
-
if (!isBb) continue;
|
|
136
|
-
if (m.url === g.myUrl) continue;
|
|
137
|
-
const staleMs = Number(process.env.MSLXDFF_BROADBAND_STALE_MS) > 0 ? Number(process.env.MSLXDFF_BROADBAND_STALE_MS) : 90_000;
|
|
138
|
-
if (typeof m.lastSeen === "number" && Date.now() - m.lastSeen > staleMs) continue;
|
|
139
|
-
allCandidates.push({ group: g.name, target: m.url, member: m, via: "via-leader", leaderUrl: g.leaderUrl });
|
|
140
|
-
}
|
|
141
|
-
} catch {}
|
|
142
|
-
}
|
|
143
|
-
if (!allCandidates.length) return null;
|
|
144
|
-
for (const cand of allCandidates) {
|
|
145
|
-
try {
|
|
146
|
-
evt?.("relay-try", { reqId, model, via: cand.via, target: cand.target, group: cand.group });
|
|
147
|
-
if (!cand.leaderUrl) {
|
|
148
|
-
const fwdBody = { model, ...body, model };
|
|
149
|
-
const reqIdLocal = `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 6)}`;
|
|
150
|
-
const promise = enqueueRelay({ group: cand.group, target: cand.target, reqId: reqIdLocal, body: fwdBody, hops });
|
|
151
|
-
const result = await promise;
|
|
152
|
-
if (result && result.status) {
|
|
153
|
-
return { via: "broadband-local", result, target: cand.target, group: cand.group };
|
|
154
|
-
}
|
|
155
|
-
} else {
|
|
156
|
-
const ctrl = new AbortController();
|
|
157
|
-
const t = setTimeout(() => ctrl.abort(), 35_000);
|
|
158
|
-
const r = await compatFetch(`${cand.leaderUrl}/v1/groups/relay/forward`, {
|
|
159
|
-
method: "POST",
|
|
160
|
-
headers: { "Content-Type": "application/json", "Authorization": `Bearer ${myToken}`, "x-mslxdff-hops": String(hops + 1) },
|
|
161
|
-
body: JSON.stringify({ group: cand.group, target: cand.target, body: { ...body, model }, hops: hops + 1, reqId }),
|
|
162
|
-
signal: ctrl.signal,
|
|
163
|
-
});
|
|
164
|
-
clearTimeout(t);
|
|
165
|
-
if (!r.ok) {
|
|
166
|
-
const txt = await r.text().catch(() => "");
|
|
167
|
-
evt?.("relay-fail", { reqId, model, via: cand.via, target: cand.target, status: r.status, message: txt.slice(0, 200) });
|
|
168
|
-
continue;
|
|
169
|
-
}
|
|
170
|
-
return { via: "broadband-via-leader", result: r, target: cand.target, group: cand.group };
|
|
171
|
-
}
|
|
172
|
-
} catch (err) {
|
|
173
|
-
evt?.("relay-fail", { reqId, model, via: cand.via, target: cand.target, message: String(err?.message || err).slice(0, 200) });
|
|
174
|
-
continue;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
return null;
|
|
178
|
-
} catch {
|
|
179
|
-
return null;
|
|
180
|
-
}
|
|
181
|
-
}
|
|
1
|
+
import { loadGroupsJoined } from "../state.js";
|
|
2
|
+
import { compatFetch } from "../compat.js";
|
|
3
|
+
|
|
4
|
+
const relayPending = new Map();
|
|
5
|
+
const relayPendingByReqId = new Map();
|
|
6
|
+
const streamSubscribers = new Map(); // key group::target -> Set<res>
|
|
7
|
+
|
|
8
|
+
function streamKey(group, target) { return `${group}::${target}`; }
|
|
9
|
+
|
|
10
|
+
export function subscribeStream({ group, target, res }) {
|
|
11
|
+
const k = streamKey(group, target);
|
|
12
|
+
const set = streamSubscribers.get(k) || new Set();
|
|
13
|
+
set.add(res);
|
|
14
|
+
streamSubscribers.set(k, set);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function unsubscribeStream({ group, target, res }) {
|
|
18
|
+
const k = streamKey(group, target);
|
|
19
|
+
const set = streamSubscribers.get(k);
|
|
20
|
+
if (!set) return;
|
|
21
|
+
set.delete(res);
|
|
22
|
+
if (set.size === 0) streamSubscribers.delete(k);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function clearStreamSubscribers() { streamSubscribers.clear(); }
|
|
26
|
+
export function getStreamSubscribers() { return streamSubscribers; }
|
|
27
|
+
|
|
28
|
+
export function pushToStream({ group, target, entry }) {
|
|
29
|
+
const k = streamKey(group, target);
|
|
30
|
+
const set = streamSubscribers.get(k);
|
|
31
|
+
if (!set || set.size === 0) return false;
|
|
32
|
+
const payload = JSON.stringify({ reqId: entry.reqId, body: entry.body, hops: entry.hops });
|
|
33
|
+
const frame = `event: relay\ndata: ${payload}\n\n`;
|
|
34
|
+
let pushed = 0;
|
|
35
|
+
for (const res of [...set]) {
|
|
36
|
+
try {
|
|
37
|
+
res.write(frame);
|
|
38
|
+
pushed++;
|
|
39
|
+
} catch {
|
|
40
|
+
set.delete(res);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (set.size === 0) streamSubscribers.delete(k);
|
|
44
|
+
return pushed > 0;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function enqueueRelay({ group, target, reqId, body, hops }) {
|
|
48
|
+
const key = `${group}::${target}`;
|
|
49
|
+
// try stream push first — if any subscriber, push and don't enqueue to poll queue
|
|
50
|
+
const entryForPush = { reqId, body, hops };
|
|
51
|
+
if (pushToStream({ group, target, entry: entryForPush })) {
|
|
52
|
+
return new Promise((resolve, reject) => {
|
|
53
|
+
const timer = setTimeout(() => {
|
|
54
|
+
relayPendingByReqId.delete(reqId);
|
|
55
|
+
reject(new Error("relay timeout"));
|
|
56
|
+
}, 30_000);
|
|
57
|
+
timer.unref?.();
|
|
58
|
+
const entry = { reqId, body, hops, resolve, reject, timer };
|
|
59
|
+
relayPendingByReqId.set(reqId, entry);
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
const list = relayPending.get(key) || [];
|
|
63
|
+
return new Promise((resolve, reject) => {
|
|
64
|
+
const timer = setTimeout(() => {
|
|
65
|
+
const idx = list.findIndex((e) => e.reqId === reqId);
|
|
66
|
+
if (idx >= 0) list.splice(idx, 1);
|
|
67
|
+
relayPendingByReqId.delete(reqId);
|
|
68
|
+
reject(new Error("relay timeout"));
|
|
69
|
+
}, 30_000);
|
|
70
|
+
timer.unref?.();
|
|
71
|
+
const entry = { reqId, body, hops, resolve, reject, timer };
|
|
72
|
+
list.push(entry);
|
|
73
|
+
relayPending.set(key, list);
|
|
74
|
+
relayPendingByReqId.set(reqId, entry);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function dequeueRelayForPoll({ group, target, limit = 10 }) {
|
|
79
|
+
const key = `${group}::${target}`;
|
|
80
|
+
const list = relayPending.get(key) || [];
|
|
81
|
+
const batch = list.splice(0, limit);
|
|
82
|
+
if (list.length) relayPending.set(key, list);
|
|
83
|
+
else relayPending.delete(key);
|
|
84
|
+
return batch.map((e) => ({ reqId: e.reqId, body: e.body, hops: e.hops }));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function resolveRelay(reqId, result) {
|
|
88
|
+
const entry = relayPendingByReqId.get(reqId);
|
|
89
|
+
if (!entry) return false;
|
|
90
|
+
clearTimeout(entry.timer);
|
|
91
|
+
relayPendingByReqId.delete(reqId);
|
|
92
|
+
entry.resolve(result);
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function getRelayPending() { return relayPending; }
|
|
97
|
+
export function getRelayPendingByReqId() { return relayPendingByReqId; }
|
|
98
|
+
|
|
99
|
+
export async function tryBroadbandRelay({ groups, token: myToken, model, body, hops, bus, logs, reqId, evt, res, mark, perf0, stages }) {
|
|
100
|
+
try {
|
|
101
|
+
const joined = loadGroupsJoined();
|
|
102
|
+
const broadbandGroups = joined.filter((g) => g.kind === "broadband" || g.myUrl?.startsWith("relay://"));
|
|
103
|
+
const allCandidates = [];
|
|
104
|
+
if (groups) {
|
|
105
|
+
const localGroups = groups.list();
|
|
106
|
+
for (const [gName, g] of Object.entries(localGroups)) {
|
|
107
|
+
for (const [id, m] of Object.entries(g.members || {})) {
|
|
108
|
+
if (id === "leader") continue;
|
|
109
|
+
const isBb = m?.kind === "broadband" || String(m?.url || "").startsWith("relay://");
|
|
110
|
+
if (!isBb) continue;
|
|
111
|
+
const staleMs = Number(process.env.MSLXDFF_BROADBAND_STALE_MS) > 0 ? Number(process.env.MSLXDFF_BROADBAND_STALE_MS) : 90_000;
|
|
112
|
+
if (typeof m.lastSeen === "number" && Date.now() - m.lastSeen > staleMs) continue;
|
|
113
|
+
allCandidates.push({ group: gName, target: m.url, member: m, via: "local-leader", leaderUrl: null });
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
for (const g of joined) {
|
|
118
|
+
if (!g.leaderUrl) continue;
|
|
119
|
+
try {
|
|
120
|
+
const controller = new AbortController();
|
|
121
|
+
const timer = setTimeout(() => controller.abort(), 5000);
|
|
122
|
+
const r = await compatFetch(`${g.leaderUrl}/v1/groups/join`, {
|
|
123
|
+
method: "POST",
|
|
124
|
+
headers: { "Content-Type": "application/json", "Authorization": `Bearer ${myToken}` },
|
|
125
|
+
body: JSON.stringify({ name: g.name, memberName: g.memberName, url: g.myUrl, token: myToken }),
|
|
126
|
+
signal: controller.signal,
|
|
127
|
+
});
|
|
128
|
+
clearTimeout(timer);
|
|
129
|
+
if (!r.ok) continue;
|
|
130
|
+
const data = await r.json().catch(() => ({}));
|
|
131
|
+
const members = data.members || {};
|
|
132
|
+
for (const [id, m] of Object.entries(members)) {
|
|
133
|
+
if (id === "leader") continue;
|
|
134
|
+
const isBb = m?.kind === "broadband" || String(m?.url || "").startsWith("relay://");
|
|
135
|
+
if (!isBb) continue;
|
|
136
|
+
if (m.url === g.myUrl) continue;
|
|
137
|
+
const staleMs = Number(process.env.MSLXDFF_BROADBAND_STALE_MS) > 0 ? Number(process.env.MSLXDFF_BROADBAND_STALE_MS) : 90_000;
|
|
138
|
+
if (typeof m.lastSeen === "number" && Date.now() - m.lastSeen > staleMs) continue;
|
|
139
|
+
allCandidates.push({ group: g.name, target: m.url, member: m, via: "via-leader", leaderUrl: g.leaderUrl });
|
|
140
|
+
}
|
|
141
|
+
} catch {}
|
|
142
|
+
}
|
|
143
|
+
if (!allCandidates.length) return null;
|
|
144
|
+
for (const cand of allCandidates) {
|
|
145
|
+
try {
|
|
146
|
+
evt?.("relay-try", { reqId, model, via: cand.via, target: cand.target, group: cand.group });
|
|
147
|
+
if (!cand.leaderUrl) {
|
|
148
|
+
const fwdBody = { model, ...body, model };
|
|
149
|
+
const reqIdLocal = `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 6)}`;
|
|
150
|
+
const promise = enqueueRelay({ group: cand.group, target: cand.target, reqId: reqIdLocal, body: fwdBody, hops });
|
|
151
|
+
const result = await promise;
|
|
152
|
+
if (result && result.status) {
|
|
153
|
+
return { via: "broadband-local", result, target: cand.target, group: cand.group };
|
|
154
|
+
}
|
|
155
|
+
} else {
|
|
156
|
+
const ctrl = new AbortController();
|
|
157
|
+
const t = setTimeout(() => ctrl.abort(), 35_000);
|
|
158
|
+
const r = await compatFetch(`${cand.leaderUrl}/v1/groups/relay/forward`, {
|
|
159
|
+
method: "POST",
|
|
160
|
+
headers: { "Content-Type": "application/json", "Authorization": `Bearer ${myToken}`, "x-mslxdff-hops": String(hops + 1) },
|
|
161
|
+
body: JSON.stringify({ group: cand.group, target: cand.target, body: { ...body, model }, hops: hops + 1, reqId }),
|
|
162
|
+
signal: ctrl.signal,
|
|
163
|
+
});
|
|
164
|
+
clearTimeout(t);
|
|
165
|
+
if (!r.ok) {
|
|
166
|
+
const txt = await r.text().catch(() => "");
|
|
167
|
+
evt?.("relay-fail", { reqId, model, via: cand.via, target: cand.target, status: r.status, message: txt.slice(0, 200) });
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
return { via: "broadband-via-leader", result: r, target: cand.target, group: cand.group };
|
|
171
|
+
}
|
|
172
|
+
} catch (err) {
|
|
173
|
+
evt?.("relay-fail", { reqId, model, via: cand.via, target: cand.target, message: String(err?.message || err).slice(0, 200) });
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return null;
|
|
178
|
+
} catch {
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
181
|
+
}
|
package/src/routes/relay.js
CHANGED
|
@@ -1,60 +1,60 @@
|
|
|
1
|
-
import { readBody, json, authorized } from "./helpers.js";
|
|
2
|
-
import { compatFetch } from "../compat.js";
|
|
3
|
-
|
|
4
|
-
// POST /v1/relay 纯网络中继:A 把 targetUrl+headers+body 发给 B,B 原样 fetch 到上游再回给 A
|
|
5
|
-
// B 侧不查本地 providerConfigs、不做 model 前缀路由、不验 allowlist,仅当 TCP 出口
|
|
6
|
-
// 鉴权:复用全局 Bearer token(peer.token),与 /v1/chat/completions 同级,避免任意 SSRF
|
|
7
|
-
export async function relayHandler({ req, res, token }) {
|
|
8
|
-
if (!authorized(req, token)) {
|
|
9
|
-
res.statusCode = 401;
|
|
10
|
-
res.setHeader("WWW-Authenticate", "Bearer");
|
|
11
|
-
return json(res, 401, { error: "Unauthorized" });
|
|
12
|
-
}
|
|
13
|
-
let body;
|
|
14
|
-
try { body = await readBody(req); } catch { return json(res, 400, { error: "Invalid JSON body" }); }
|
|
15
|
-
const targetUrl = String(body.targetUrl || body.url || "").trim();
|
|
16
|
-
const method = String(body.method || "POST").toUpperCase();
|
|
17
|
-
const headers = body.headers && typeof body.headers === "object" ? body.headers : {};
|
|
18
|
-
const rawBody = body.body ?? body.payload ?? null;
|
|
19
|
-
|
|
20
|
-
if (!targetUrl) return json(res, 400, { error: "targetUrl is required" });
|
|
21
|
-
let u;
|
|
22
|
-
try { u = new URL(targetUrl); } catch { return json(res, 400, { error: "invalid targetUrl" }); }
|
|
23
|
-
if (u.protocol !== "https:" && u.protocol !== "http:") return json(res, 400, { error: "targetUrl must be http(s)" });
|
|
24
|
-
// 简单 SSRF 防护:禁止回环与内网段(除 127.0.0.1:8989 本身已鉴权,仍放行)
|
|
25
|
-
// 允许外网 https 上游(如 https://api.bai.com、https://opencode.ai),禁止 10/172.16/192.168
|
|
26
|
-
const host = u.hostname;
|
|
27
|
-
if (/^(10\.|192\.168\.|172\.(1[6-9]|2\d|3[0-1])\.)/.test(host) && host !== "127.0.0.1" && host !== "localhost") {
|
|
28
|
-
// 仍允许,因为组员可能是内网 IP 的上游(如自建网关),仅告警不拦
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// 透传头:只放行受控头,避免把内部头(如 x-mslxdff-*)误带给上游
|
|
32
|
-
const allowHeaders = new Set(["authorization", "content-type", "accept", "user-agent", "x-client-type", "x-platform", "x-task-id", "x-user-id", "x-domain", "x-enterprise-id", "x-tenant-id", "origin", "referer"]);
|
|
33
|
-
const fwdHeaders = {};
|
|
34
|
-
for (const [k, v] of Object.entries(headers)) {
|
|
35
|
-
const lk = String(k).toLowerCase();
|
|
36
|
-
if (allowHeaders.has(lk) && typeof v === "string" && v) fwdHeaders[k] = v;
|
|
37
|
-
}
|
|
38
|
-
// 强制 JSON
|
|
39
|
-
if (!fwdHeaders["Content-Type"] && !fwdHeaders["content-type"]) fwdHeaders["Content-Type"] = "application/json";
|
|
40
|
-
if (!fwdHeaders["Accept"] && !fwdHeaders["accept"]) fwdHeaders["Accept"] = "application/json";
|
|
41
|
-
|
|
42
|
-
const controller = new AbortController();
|
|
43
|
-
const timer = setTimeout(() => controller.abort(new Error("relay timeout 30000ms")), 30000);
|
|
44
|
-
try {
|
|
45
|
-
const fetchBody = rawBody == null ? undefined : (typeof rawBody === "string" ? rawBody : JSON.stringify(rawBody));
|
|
46
|
-
const r = await compatFetch(targetUrl, { method, headers: fwdHeaders, body: fetchBody, signal: controller.signal });
|
|
47
|
-
const txt = await r.text();
|
|
48
|
-
// 原样回透:状态码 + 头(仅透 content-type) + body
|
|
49
|
-
res.statusCode = r.status;
|
|
50
|
-
const ct = r.headers.get("content-type");
|
|
51
|
-
if (ct) res.setHeader("content-type", ct);
|
|
52
|
-
// 额外回透上游错误码便于 bench 区分
|
|
53
|
-
res.setHeader("x-mslxdff-relay-status", String(r.status));
|
|
54
|
-
return res.end(txt);
|
|
55
|
-
} catch (e) {
|
|
56
|
-
const msg = String(e?.message || e);
|
|
57
|
-
const isTimeout = /timeout|abort/i.test(msg);
|
|
58
|
-
return json(res, 502, { error: isTimeout ? "relay timeout" : `relay fetch failed: ${msg.slice(0, 300)}` });
|
|
59
|
-
} finally { clearTimeout(timer); }
|
|
60
|
-
}
|
|
1
|
+
import { readBody, json, authorized } from "./helpers.js";
|
|
2
|
+
import { compatFetch } from "../compat.js";
|
|
3
|
+
|
|
4
|
+
// POST /v1/relay 纯网络中继:A 把 targetUrl+headers+body 发给 B,B 原样 fetch 到上游再回给 A
|
|
5
|
+
// B 侧不查本地 providerConfigs、不做 model 前缀路由、不验 allowlist,仅当 TCP 出口
|
|
6
|
+
// 鉴权:复用全局 Bearer token(peer.token),与 /v1/chat/completions 同级,避免任意 SSRF
|
|
7
|
+
export async function relayHandler({ req, res, token }) {
|
|
8
|
+
if (!authorized(req, token)) {
|
|
9
|
+
res.statusCode = 401;
|
|
10
|
+
res.setHeader("WWW-Authenticate", "Bearer");
|
|
11
|
+
return json(res, 401, { error: "Unauthorized" });
|
|
12
|
+
}
|
|
13
|
+
let body;
|
|
14
|
+
try { body = await readBody(req); } catch { return json(res, 400, { error: "Invalid JSON body" }); }
|
|
15
|
+
const targetUrl = String(body.targetUrl || body.url || "").trim();
|
|
16
|
+
const method = String(body.method || "POST").toUpperCase();
|
|
17
|
+
const headers = body.headers && typeof body.headers === "object" ? body.headers : {};
|
|
18
|
+
const rawBody = body.body ?? body.payload ?? null;
|
|
19
|
+
|
|
20
|
+
if (!targetUrl) return json(res, 400, { error: "targetUrl is required" });
|
|
21
|
+
let u;
|
|
22
|
+
try { u = new URL(targetUrl); } catch { return json(res, 400, { error: "invalid targetUrl" }); }
|
|
23
|
+
if (u.protocol !== "https:" && u.protocol !== "http:") return json(res, 400, { error: "targetUrl must be http(s)" });
|
|
24
|
+
// 简单 SSRF 防护:禁止回环与内网段(除 127.0.0.1:8989 本身已鉴权,仍放行)
|
|
25
|
+
// 允许外网 https 上游(如 https://api.bai.com、https://opencode.ai),禁止 10/172.16/192.168
|
|
26
|
+
const host = u.hostname;
|
|
27
|
+
if (/^(10\.|192\.168\.|172\.(1[6-9]|2\d|3[0-1])\.)/.test(host) && host !== "127.0.0.1" && host !== "localhost") {
|
|
28
|
+
// 仍允许,因为组员可能是内网 IP 的上游(如自建网关),仅告警不拦
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// 透传头:只放行受控头,避免把内部头(如 x-mslxdff-*)误带给上游
|
|
32
|
+
const allowHeaders = new Set(["authorization", "content-type", "accept", "user-agent", "x-client-type", "x-platform", "x-task-id", "x-user-id", "x-domain", "x-enterprise-id", "x-tenant-id", "origin", "referer"]);
|
|
33
|
+
const fwdHeaders = {};
|
|
34
|
+
for (const [k, v] of Object.entries(headers)) {
|
|
35
|
+
const lk = String(k).toLowerCase();
|
|
36
|
+
if (allowHeaders.has(lk) && typeof v === "string" && v) fwdHeaders[k] = v;
|
|
37
|
+
}
|
|
38
|
+
// 强制 JSON
|
|
39
|
+
if (!fwdHeaders["Content-Type"] && !fwdHeaders["content-type"]) fwdHeaders["Content-Type"] = "application/json";
|
|
40
|
+
if (!fwdHeaders["Accept"] && !fwdHeaders["accept"]) fwdHeaders["Accept"] = "application/json";
|
|
41
|
+
|
|
42
|
+
const controller = new AbortController();
|
|
43
|
+
const timer = setTimeout(() => controller.abort(new Error("relay timeout 30000ms")), 30000);
|
|
44
|
+
try {
|
|
45
|
+
const fetchBody = rawBody == null ? undefined : (typeof rawBody === "string" ? rawBody : JSON.stringify(rawBody));
|
|
46
|
+
const r = await compatFetch(targetUrl, { method, headers: fwdHeaders, body: fetchBody, signal: controller.signal });
|
|
47
|
+
const txt = await r.text();
|
|
48
|
+
// 原样回透:状态码 + 头(仅透 content-type) + body
|
|
49
|
+
res.statusCode = r.status;
|
|
50
|
+
const ct = r.headers.get("content-type");
|
|
51
|
+
if (ct) res.setHeader("content-type", ct);
|
|
52
|
+
// 额外回透上游错误码便于 bench 区分
|
|
53
|
+
res.setHeader("x-mslxdff-relay-status", String(r.status));
|
|
54
|
+
return res.end(txt);
|
|
55
|
+
} catch (e) {
|
|
56
|
+
const msg = String(e?.message || e);
|
|
57
|
+
const isTimeout = /timeout|abort/i.test(msg);
|
|
58
|
+
return json(res, 502, { error: isTimeout ? "relay timeout" : `relay fetch failed: ${msg.slice(0, 300)}` });
|
|
59
|
+
} finally { clearTimeout(timer); }
|
|
60
|
+
}
|