@artenx/dshb 0.1.52 → 0.1.54
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/lib/auth/decorate-webserver.js +4 -4
- package/lib/auth/decorate-webserver.js.map +1 -1
- package/lib/auth/gate.d.ts +11 -3
- package/lib/auth/gate.js +14 -2
- package/lib/auth/gate.js.map +1 -1
- package/lib/auth/index.js +21 -2
- package/lib/auth/index.js.map +1 -1
- package/lib/auth/webserver.js +4 -4
- package/lib/auth/webserver.js.map +1 -1
- package/lib/client.js +285 -0
- package/lib/client.js.map +1 -1
- package/lib/core/index.js +2 -198
- package/lib/core/index.js.map +1 -1
- package/lib/core/node-registry.js +1 -1
- package/lib/relay-auth-CtwES4Xn.js +240 -0
- package/lib/relay-auth-CtwES4Xn.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { isLoopbackRequest } from "./auth/loopback.js";
|
|
2
|
+
import { credentialKey } from "@deepseek-ai/dsh-credentials";
|
|
3
|
+
import { createHash, createHmac, timingSafeEqual } from "node:crypto";
|
|
4
|
+
//#region src/core/sync/relay-auth.ts
|
|
5
|
+
const RELAY_COOKIE_PREFIX = "dsh-auth-";
|
|
6
|
+
const RELAY_COOKIE_VERSION = 1;
|
|
7
|
+
const RELAY_SECRET_BYTES = 32;
|
|
8
|
+
const RELAY_TICKET_MAX_AGE_MS = 6e5;
|
|
9
|
+
const MAX_TICKETS_PER_MINUTE = 30;
|
|
10
|
+
const AUTHORITY_PATTERN = /^(?:[A-Za-z0-9](?:[A-Za-z0-9.-]*[A-Za-z0-9])?|\[(?:[0-9A-Fa-f:.]+)\])(?::\d{1,5})?$/;
|
|
11
|
+
var RelayTicketError = class extends Error {
|
|
12
|
+
code;
|
|
13
|
+
constructor(code, message) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.code = code;
|
|
16
|
+
this.name = "RelayTicketError";
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
function encodeBase64Url(value) {
|
|
20
|
+
return Buffer.from(value).toString("base64").replaceAll("+", "-").replaceAll("/", "_").replace(/=+$/u, "");
|
|
21
|
+
}
|
|
22
|
+
function decodeBase64Url(value) {
|
|
23
|
+
if (!/^[A-Za-z0-9_-]*$/.test(value) || value.length % 4 === 1) return void 0;
|
|
24
|
+
const decoded = Buffer.from(value.replaceAll("-", "+").replaceAll("_", "/"), "base64");
|
|
25
|
+
return encodeBase64Url(decoded) === value ? decoded : void 0;
|
|
26
|
+
}
|
|
27
|
+
/** 校验并提取 credentials 记录中的浏览器会话签名密钥(32 字节)。 */
|
|
28
|
+
function browserSessionSecret(record) {
|
|
29
|
+
if (record === null || typeof record !== "object" || Array.isArray(record)) throw new RelayTicketError("relay-secret-unavailable", "browser session credential record is missing");
|
|
30
|
+
const payload = record.payload;
|
|
31
|
+
if (payload === null || typeof payload !== "object" || Array.isArray(payload)) throw new RelayTicketError("relay-secret-unavailable", "browser session credential payload is missing");
|
|
32
|
+
const { version, secret } = payload;
|
|
33
|
+
if (version !== RELAY_COOKIE_VERSION || typeof secret !== "string") throw new RelayTicketError("relay-secret-unavailable", "browser session credential payload is unsupported");
|
|
34
|
+
const decoded = decodeBase64Url(secret);
|
|
35
|
+
if (decoded === void 0 || decoded.byteLength !== RELAY_SECRET_BYTES) throw new RelayTicketError("relay-secret-unavailable", "browser session credential secret is invalid");
|
|
36
|
+
return decoded;
|
|
37
|
+
}
|
|
38
|
+
function validateRelayAuthority(value) {
|
|
39
|
+
if (typeof value !== "string" || value.length > 255 || !AUTHORITY_PATTERN.test(value)) throw new RelayTicketError("authority-invalid", "relay authority must be a bare host or host:port");
|
|
40
|
+
const port = value.includes("]:") || !value.includes("]") && value.includes(":") ? Number(value.slice(value.lastIndexOf(":") + 1)) : 80;
|
|
41
|
+
if (!Number.isInteger(port) || port < 1 || port > 65535) throw new RelayTicketError("authority-invalid", "relay authority port is invalid");
|
|
42
|
+
return value;
|
|
43
|
+
}
|
|
44
|
+
/** 与 browser-auth 相同的 cookie 名派生:dsh-auth- + base64url(sha256(authority))。 */
|
|
45
|
+
function relayCookieName(authority) {
|
|
46
|
+
return RELAY_COOKIE_PREFIX + encodeBase64Url(createHash("sha256").update(authority).digest());
|
|
47
|
+
}
|
|
48
|
+
/** 按远端 Host browser-auth 的格式铸造一个权威名绑定的短时效签名 cookie。 */
|
|
49
|
+
function mintRelayCookie(secret, authority, now, maxAgeMs = RELAY_TICKET_MAX_AGE_MS) {
|
|
50
|
+
const issuedAt = now;
|
|
51
|
+
const expiresAt = issuedAt + maxAgeMs;
|
|
52
|
+
const body = encodeBase64Url(Buffer.from(JSON.stringify({
|
|
53
|
+
version: RELAY_COOKIE_VERSION,
|
|
54
|
+
authority,
|
|
55
|
+
issuedAt,
|
|
56
|
+
expiresAt
|
|
57
|
+
}), "utf8"));
|
|
58
|
+
const signature = createHmac("sha256", secret).update(body).digest();
|
|
59
|
+
return {
|
|
60
|
+
name: relayCookieName(authority),
|
|
61
|
+
value: `v1.${body}.${encodeBase64Url(signature)}`,
|
|
62
|
+
expiresAt,
|
|
63
|
+
maxAgeSeconds: Math.floor(maxAgeMs / 1e3)
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
function relayCookieHeader(cookie) {
|
|
67
|
+
return `${cookie.name}=${cookie.value}`;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* 校验一个 dsh browser-auth 格式的 relay cookie 值是否由本机 secret 为
|
|
71
|
+
* 指定 authority 铸造且未过期。与 mintRelayCookie 对称:拆分 v1.body.sig,
|
|
72
|
+
* 重算 HMAC 做常量时间比较,并校验 body 内的 authority 与过期时间。
|
|
73
|
+
*/
|
|
74
|
+
function verifyRelayCookie(value, secret, authority, now) {
|
|
75
|
+
if (typeof value !== "string" || !value.startsWith("v1.")) return false;
|
|
76
|
+
const firstDot = value.indexOf(".", 3);
|
|
77
|
+
if (firstDot < 0) return false;
|
|
78
|
+
const body = value.slice(3, firstDot);
|
|
79
|
+
const signature = value.slice(firstDot + 1);
|
|
80
|
+
if (!body || !signature) return false;
|
|
81
|
+
if (!constantTimeEquals(signature, encodeBase64Url(createHmac("sha256", secret).update(body).digest()))) return false;
|
|
82
|
+
const decoded = decodeBase64Url(body);
|
|
83
|
+
if (decoded === void 0) return false;
|
|
84
|
+
let payload;
|
|
85
|
+
try {
|
|
86
|
+
payload = JSON.parse(decoded.toString("utf8"));
|
|
87
|
+
} catch {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
if (payload.authority !== authority) return false;
|
|
91
|
+
if (typeof payload.expiresAt !== "number" || payload.expiresAt <= now) return false;
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
/** 从请求 Cookie 头中提取并校验绑定到 req Host 的 relay cookie。 */
|
|
95
|
+
function requestHasValidRelayCookie(cookieHeader, secret, host, now) {
|
|
96
|
+
if (!cookieHeader || !host || !secret) return false;
|
|
97
|
+
const name = relayCookieName(host);
|
|
98
|
+
const re = new RegExp(`(?:^|;\\s*)${name}=([^;]+)`);
|
|
99
|
+
const match = cookieHeader.match(re);
|
|
100
|
+
if (!match) return false;
|
|
101
|
+
return verifyRelayCookie(match[1], secret, host, now);
|
|
102
|
+
}
|
|
103
|
+
function constantTimeEquals(actual, expected) {
|
|
104
|
+
const actualBytes = Buffer.from(actual, "utf8");
|
|
105
|
+
const expectedBytes = Buffer.from(expected, "utf8");
|
|
106
|
+
return actualBytes.byteLength === expectedBytes.byteLength && timingSafeEqual(actualBytes, expectedBytes);
|
|
107
|
+
}
|
|
108
|
+
function readJsonBody(req, maxBytes) {
|
|
109
|
+
return new Promise((resolve) => {
|
|
110
|
+
const chunks = [];
|
|
111
|
+
let size = 0;
|
|
112
|
+
let settled = false;
|
|
113
|
+
const fail = () => {
|
|
114
|
+
if (settled) return;
|
|
115
|
+
settled = true;
|
|
116
|
+
resolve(null);
|
|
117
|
+
};
|
|
118
|
+
req.on("data", (chunk) => {
|
|
119
|
+
if (settled) return;
|
|
120
|
+
size += chunk.length;
|
|
121
|
+
if (size > maxBytes) {
|
|
122
|
+
fail();
|
|
123
|
+
req.destroy();
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
chunks.push(chunk);
|
|
127
|
+
});
|
|
128
|
+
req.on("end", () => {
|
|
129
|
+
if (settled) return;
|
|
130
|
+
try {
|
|
131
|
+
const value = JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
132
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
133
|
+
fail();
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
settled = true;
|
|
137
|
+
resolve(value);
|
|
138
|
+
} catch {
|
|
139
|
+
fail();
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
req.on("error", fail);
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* 远端侧中继凭据铸造路由:POST /api/dshb/sync/internal/relay-ticket。
|
|
147
|
+
* 仅接受回环请求;用本机 credentials 的浏览器会话密钥为请求指定的
|
|
148
|
+
* authority 铸造 ≤10 分钟签名 cookie。凭据只在响应中返回一次。
|
|
149
|
+
*/
|
|
150
|
+
function registerRelayTicketRoutes(ctx, credentials) {
|
|
151
|
+
const rateLimit = {
|
|
152
|
+
windowStart: 0,
|
|
153
|
+
count: 0
|
|
154
|
+
};
|
|
155
|
+
ctx.effect(() => ctx.webServer.register({
|
|
156
|
+
kind: "exact",
|
|
157
|
+
path: "/api/dshb/sync/internal/relay-ticket",
|
|
158
|
+
handler: async (req, res) => {
|
|
159
|
+
const deny = (status, body) => {
|
|
160
|
+
res.writeHead(status, {
|
|
161
|
+
"content-type": "application/json",
|
|
162
|
+
"cache-control": "no-store"
|
|
163
|
+
});
|
|
164
|
+
res.end(JSON.stringify(body));
|
|
165
|
+
};
|
|
166
|
+
if (!isLoopbackRequest(req.socket.remoteAddress, req.headers.host)) {
|
|
167
|
+
deny(403, {
|
|
168
|
+
ok: false,
|
|
169
|
+
error: "loopback access required"
|
|
170
|
+
});
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
if (req.method !== "POST") {
|
|
174
|
+
deny(405, {
|
|
175
|
+
ok: false,
|
|
176
|
+
error: "method not allowed"
|
|
177
|
+
});
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
const now = Date.now();
|
|
181
|
+
if (now - rateLimit.windowStart > 6e4) {
|
|
182
|
+
rateLimit.windowStart = now;
|
|
183
|
+
rateLimit.count = 0;
|
|
184
|
+
}
|
|
185
|
+
rateLimit.count += 1;
|
|
186
|
+
if (rateLimit.count > MAX_TICKETS_PER_MINUTE) {
|
|
187
|
+
deny(429, {
|
|
188
|
+
ok: false,
|
|
189
|
+
error: "relay ticket rate limit exceeded"
|
|
190
|
+
});
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
const body = await readJsonBody(req, 1024);
|
|
194
|
+
let authority;
|
|
195
|
+
try {
|
|
196
|
+
authority = validateRelayAuthority(body?.authority);
|
|
197
|
+
} catch (error) {
|
|
198
|
+
deny(400, {
|
|
199
|
+
ok: false,
|
|
200
|
+
code: error instanceof RelayTicketError ? error.code : "authority-invalid",
|
|
201
|
+
error: error instanceof Error ? error.message : "authority is invalid"
|
|
202
|
+
});
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
try {
|
|
206
|
+
const cookie = mintRelayCookie(browserSessionSecret(await credentials.readRecord(credentialKey("client-connection", "browser-session"))), authority, now);
|
|
207
|
+
res.writeHead(200, {
|
|
208
|
+
"content-type": "application/json",
|
|
209
|
+
"cache-control": "no-store"
|
|
210
|
+
});
|
|
211
|
+
res.end(JSON.stringify({
|
|
212
|
+
ok: true,
|
|
213
|
+
cookie: {
|
|
214
|
+
name: cookie.name,
|
|
215
|
+
value: cookie.value,
|
|
216
|
+
maxAgeSeconds: cookie.maxAgeSeconds
|
|
217
|
+
}
|
|
218
|
+
}));
|
|
219
|
+
} catch (error) {
|
|
220
|
+
if (error instanceof RelayTicketError) {
|
|
221
|
+
deny(503, {
|
|
222
|
+
ok: false,
|
|
223
|
+
code: error.code,
|
|
224
|
+
error: error.message
|
|
225
|
+
});
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
deny(500, {
|
|
229
|
+
ok: false,
|
|
230
|
+
code: "relay-ticket-failed",
|
|
231
|
+
error: "relay ticket minting failed"
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}));
|
|
236
|
+
}
|
|
237
|
+
//#endregion
|
|
238
|
+
export { requestHasValidRelayCookie as a, relayCookieHeader as i, browserSessionSecret as n, registerRelayTicketRoutes as r, RelayTicketError as t };
|
|
239
|
+
|
|
240
|
+
//# sourceMappingURL=relay-auth-CtwES4Xn.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"relay-auth-CtwES4Xn.js","names":[],"sources":["../src/core/sync/relay-auth.ts"],"sourcesContent":["import { createHash, createHmac, timingSafeEqual } from 'node:crypto'\nimport type { IncomingMessage, ServerResponse } from 'node:http'\nimport type { Context } from '@deepseek-ai/cordis'\nimport type {} from '@deepseek-ai/dsh-host-webserver'\nimport type { CredentialKey } from '@deepseek-ai/dsh-credentials'\nimport { credentialKey } from '@deepseek-ai/dsh-credentials'\nimport { isLoopbackRequest } from '../../auth/loopback.js'\n\n// 中继凭据的格式与常量复制自 dsh-client-connection 的 browser-auth(本插件\n// 兼容范围内该格式稳定):cookie 名 dsh-auth- + base64url(sha256(authority)),\n// 载荷 v1.<payload>.<hmac>,HMAC-SHA256 密钥存于 credentials 的\n// client-connection/browser-session 记录。铸造只在本机回环内部路由上进行,\n// 凭据有效期 10 分钟且绝不落盘。\n\nconst RELAY_COOKIE_PREFIX = 'dsh-auth-'\nconst RELAY_COOKIE_VERSION = 1\nconst RELAY_SECRET_BYTES = 32\nexport const RELAY_TICKET_MAX_AGE_MS = 10 * 60 * 1000\nconst MAX_TICKETS_PER_MINUTE = 30\n\nconst AUTHORITY_PATTERN = /^(?:[A-Za-z0-9](?:[A-Za-z0-9.-]*[A-Za-z0-9])?|\\[(?:[0-9A-Fa-f:.]+)\\])(?::\\d{1,5})?$/\n\nexport class RelayTicketError extends Error {\n constructor(readonly code: string, message: string) {\n super(message)\n this.name = 'RelayTicketError'\n }\n}\n\nfunction encodeBase64Url(value: Uint8Array): string {\n return Buffer.from(value).toString('base64')\n .replaceAll('+', '-')\n .replaceAll('/', '_')\n .replace(/=+$/u, '')\n}\n\nfunction decodeBase64Url(value: string): Buffer | undefined {\n if (!/^[A-Za-z0-9_-]*$/.test(value) || value.length % 4 === 1) return undefined\n const decoded = Buffer.from(value.replaceAll('-', '+').replaceAll('_', '/'), 'base64')\n return encodeBase64Url(decoded) === value ? decoded : undefined\n}\n\n/** 校验并提取 credentials 记录中的浏览器会话签名密钥(32 字节)。 */\nexport function browserSessionSecret(record: unknown): Buffer {\n if (record === null || typeof record !== 'object' || Array.isArray(record)) {\n throw new RelayTicketError('relay-secret-unavailable', 'browser session credential record is missing')\n }\n const payload = (record as { payload?: unknown }).payload\n if (payload === null || typeof payload !== 'object' || Array.isArray(payload)) {\n throw new RelayTicketError('relay-secret-unavailable', 'browser session credential payload is missing')\n }\n const { version, secret } = payload as { version?: unknown; secret?: unknown }\n if (version !== RELAY_COOKIE_VERSION || typeof secret !== 'string') {\n throw new RelayTicketError('relay-secret-unavailable', 'browser session credential payload is unsupported')\n }\n const decoded = decodeBase64Url(secret)\n if (decoded === undefined || decoded.byteLength !== RELAY_SECRET_BYTES) {\n throw new RelayTicketError('relay-secret-unavailable', 'browser session credential secret is invalid')\n }\n return decoded\n}\n\nexport function validateRelayAuthority(value: unknown): string {\n if (typeof value !== 'string' || value.length > 255 || !AUTHORITY_PATTERN.test(value)) {\n throw new RelayTicketError('authority-invalid', 'relay authority must be a bare host or host:port')\n }\n const port = value.includes(']:') || (!value.includes(']') && value.includes(':'))\n ? Number(value.slice(value.lastIndexOf(':') + 1))\n : 80\n if (!Number.isInteger(port) || port < 1 || port > 65535) {\n throw new RelayTicketError('authority-invalid', 'relay authority port is invalid')\n }\n return value\n}\n\nexport interface RelayCookie {\n name: string\n value: string\n expiresAt: number\n maxAgeSeconds: number\n}\n\n/** 与 browser-auth 相同的 cookie 名派生:dsh-auth- + base64url(sha256(authority))。 */\nexport function relayCookieName(authority: string): string {\n return RELAY_COOKIE_PREFIX + encodeBase64Url(createHash('sha256').update(authority).digest())\n}\n\n/** 按远端 Host browser-auth 的格式铸造一个权威名绑定的短时效签名 cookie。 */\nexport function mintRelayCookie(secret: Buffer, authority: string, now: number, maxAgeMs = RELAY_TICKET_MAX_AGE_MS): RelayCookie {\n const issuedAt = now\n const expiresAt = issuedAt + maxAgeMs\n const body = encodeBase64Url(Buffer.from(JSON.stringify({\n version: RELAY_COOKIE_VERSION,\n authority,\n issuedAt,\n expiresAt,\n }), 'utf8'))\n const signature = createHmac('sha256', secret).update(body).digest()\n return {\n name: relayCookieName(authority),\n value: `v1.${body}.${encodeBase64Url(signature)}`,\n expiresAt,\n maxAgeSeconds: Math.floor(maxAgeMs / 1000),\n }\n}\n\nexport function relayCookieHeader(cookie: RelayCookie): string {\n return `${cookie.name}=${cookie.value}`\n}\n\n/**\n * 校验一个 dsh browser-auth 格式的 relay cookie 值是否由本机 secret 为\n * 指定 authority 铸造且未过期。与 mintRelayCookie 对称:拆分 v1.body.sig,\n * 重算 HMAC 做常量时间比较,并校验 body 内的 authority 与过期时间。\n */\nexport function verifyRelayCookie(value: unknown, secret: Buffer, authority: string, now: number): boolean {\n if (typeof value !== 'string' || !value.startsWith('v1.')) return false\n const firstDot = value.indexOf('.', 3)\n if (firstDot < 0) return false\n const body = value.slice(3, firstDot)\n const signature = value.slice(firstDot + 1)\n if (!body || !signature) return false\n const expectedSig = encodeBase64Url(createHmac('sha256', secret).update(body).digest())\n if (!constantTimeEquals(signature, expectedSig)) return false\n const decoded = decodeBase64Url(body)\n if (decoded === undefined) return false\n let payload: { authority?: unknown; expiresAt?: unknown; issuedAt?: unknown }\n try {\n payload = JSON.parse(decoded.toString('utf8'))\n } catch {\n return false\n }\n if (payload.authority !== authority) return false\n if (typeof payload.expiresAt !== 'number' || payload.expiresAt <= now) return false\n return true\n}\n\n/** 从请求 Cookie 头中提取并校验绑定到 req Host 的 relay cookie。 */\nexport function requestHasValidRelayCookie(\n cookieHeader: string | undefined,\n secret: Buffer,\n host: string | undefined,\n now: number,\n): boolean {\n if (!cookieHeader || !host || !secret) return false\n const name = relayCookieName(host)\n const re = new RegExp(`(?:^|;\\\\s*)${name}=([^;]+)`)\n const match = cookieHeader.match(re)\n if (!match) return false\n return verifyRelayCookie(match[1], secret, host, now)\n}\n\nfunction constantTimeEquals(actual: string, expected: string): boolean {\n const actualBytes = Buffer.from(actual, 'utf8')\n const expectedBytes = Buffer.from(expected, 'utf8')\n return actualBytes.byteLength === expectedBytes.byteLength && timingSafeEqual(actualBytes, expectedBytes)\n}\n\nfunction readJsonBody(req: IncomingMessage, maxBytes: number): Promise<Record<string, unknown> | null> {\n return new Promise((resolve) => {\n const chunks: Buffer[] = []\n let size = 0\n let settled = false\n const fail = (): void => {\n if (settled) return\n settled = true\n resolve(null)\n }\n req.on('data', (chunk: Buffer) => {\n if (settled) return\n size += chunk.length\n if (size > maxBytes) {\n fail()\n req.destroy()\n return\n }\n chunks.push(chunk)\n })\n req.on('end', () => {\n if (settled) return\n try {\n const value: unknown = JSON.parse(Buffer.concat(chunks).toString('utf8'))\n if (value === null || typeof value !== 'object' || Array.isArray(value)) {\n fail()\n return\n }\n settled = true\n resolve(value as Record<string, unknown>)\n } catch {\n fail()\n }\n })\n req.on('error', fail)\n })\n}\n\n/**\n * 远端侧中继凭据铸造路由:POST /api/dshb/sync/internal/relay-ticket。\n * 仅接受回环请求;用本机 credentials 的浏览器会话密钥为请求指定的\n * authority 铸造 ≤10 分钟签名 cookie。凭据只在响应中返回一次。\n */\nexport function registerRelayTicketRoutes(ctx: Context, credentials: {\n readRecord(key: CredentialKey): Promise<unknown>\n}): void {\n const rateLimit = { windowStart: 0, count: 0 }\n ctx.effect(() =>\n ctx.webServer.register({\n kind: 'exact',\n path: '/api/dshb/sync/internal/relay-ticket',\n handler: async (req: IncomingMessage, res: ServerResponse) => {\n const deny = (status: number, body: Record<string, unknown>): void => {\n res.writeHead(status, { 'content-type': 'application/json', 'cache-control': 'no-store' })\n res.end(JSON.stringify(body))\n }\n if (!isLoopbackRequest(req.socket.remoteAddress, req.headers.host)) {\n deny(403, { ok: false, error: 'loopback access required' })\n return\n }\n if (req.method !== 'POST') {\n deny(405, { ok: false, error: 'method not allowed' })\n return\n }\n const now = Date.now()\n if (now - rateLimit.windowStart > 60_000) {\n rateLimit.windowStart = now\n rateLimit.count = 0\n }\n rateLimit.count += 1\n if (rateLimit.count > MAX_TICKETS_PER_MINUTE) {\n deny(429, { ok: false, error: 'relay ticket rate limit exceeded' })\n return\n }\n const body = await readJsonBody(req, 1024)\n let authority: string\n try {\n authority = validateRelayAuthority(body?.authority)\n } catch (error) {\n const code = error instanceof RelayTicketError ? error.code : 'authority-invalid'\n deny(400, { ok: false, code, error: error instanceof Error ? error.message : 'authority is invalid' })\n return\n }\n try {\n const record = await credentials.readRecord(credentialKey('client-connection', 'browser-session'))\n const secret = browserSessionSecret(record)\n const cookie = mintRelayCookie(secret, authority, now)\n res.writeHead(200, { 'content-type': 'application/json', 'cache-control': 'no-store' })\n res.end(JSON.stringify({ ok: true, cookie: { name: cookie.name, value: cookie.value, maxAgeSeconds: cookie.maxAgeSeconds } }))\n } catch (error) {\n if (error instanceof RelayTicketError) {\n deny(503, { ok: false, code: error.code, error: error.message })\n return\n }\n deny(500, { ok: false, code: 'relay-ticket-failed', error: 'relay ticket minting failed' })\n }\n },\n }),\n )\n}\n\n/** 导出给测试:校验铸造值与浏览器认证的期望格式逐字节一致。 */\nexport const relayTicketTestKit = { constantTimeEquals }\n"],"mappings":";;;;AAcA,MAAM,sBAAsB;AAC5B,MAAM,uBAAuB;AAC7B,MAAM,qBAAqB;AAC3B,MAAa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GACA,MAAM,OAAO,MAAM,aAAa,KAAK,IAAI;GACzC,IAAI;GACJ,IAAI;IACF,YAAY,uBAAuB,MAAM,SAAS;GACpD,SAAS,OAAO;IAEd,KAAK,KAAK;KAAE,IAAI;KAAO,MADV,iBAAiB,mBAAmB,MAAM,OAAO;KACjC,OAAO,iBAAiB,QAAQ,MAAM,UAAU;IAAuB,CAAC;IACrG;GACF;GACA,IAAI;IAGF,MAAM,SAAS,gBADA,qBAAqB,MADf,YAAY,WAAW,cAAc,qBAAqB,iBAAiB,CAAC,CAE7D,GAAG,WAAW,GAAG;IACrD,IAAI,UAAU,KAAK;KAAE,gBAAgB;KAAoB,iBAAiB;IAAW,CAAC;IACtF,IAAI,IAAI,KAAK,UAAU;KAAE,IAAI;KAAM,QAAQ;MAAE,MAAM,OAAO;MAAM,OAAO,OAAO;MAAO,eAAe,OAAO;KAAc;IAAE,CAAC,CAAC;GAC/H,SAAS,OAAO;IACd,IAAI,iBAAiB,kBAAkB;KACrC,KAAK,KAAK;MAAE,IAAI;MAAO,MAAM,MAAM;MAAM,OAAO,MAAM;KAAQ,CAAC;KAC/D;IACF;IACA,KAAK,KAAK;KAAE,IAAI;KAAO,MAAM;KAAuB,OAAO;IAA8B,CAAC;GAC5F;EACF;CACF,CAAC,CACH;AACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@artenx/dshb",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.54",
|
|
4
4
|
"description": "让 DSH 变成随时可用的多设备 Agent 工作台,支持将远程节点添加到本地 DSH,并像操作本地内容一样访问远程节点,包括会话环境、文件目录、终端等。支持在安装了本插件的多个 DSH 实例间同步数据。若部署在云服务器,Web 端访问支持登录认证。真正实现远程节点本地化操作。最新版本已支持 DSH 桌面端。",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|