@mrrisega/dsh-remote 0.7.10-beta.3 → 0.7.12-beta.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.
Files changed (74) hide show
  1. package/clients/dsh-remote/agent/README.md +412 -0
  2. package/clients/dsh-remote/agent/agent-e2e-codex.mjs +185 -0
  3. package/clients/dsh-remote/agent/agent-e2e-real.mjs +443 -0
  4. package/clients/dsh-remote/agent/codex.mjs +736 -0
  5. package/clients/dsh-remote/agent/detect.mjs +399 -0
  6. package/clients/dsh-remote/agent/events.mjs +511 -0
  7. package/clients/dsh-remote/agent/gemini.mjs +503 -0
  8. package/clients/dsh-remote/agent/index.mjs +339 -0
  9. package/clients/dsh-remote/agent/opencode.mjs +628 -0
  10. package/clients/dsh-remote/agent/permission-bridge.mjs +534 -0
  11. package/clients/dsh-remote/agent/proc.mjs +179 -0
  12. package/clients/dsh-remote/agent/version.mjs +148 -0
  13. package/clients/dsh-remote/approval-coordinator.mjs +150 -0
  14. package/clients/dsh-remote/decision-poller.mjs +235 -0
  15. package/clients/dsh-remote/dsh-bridge.mjs +220 -1
  16. package/clients/dsh-remote/im-format.mjs +372 -0
  17. package/clients/dsh-remote/push-reporter.mjs +54 -0
  18. package/clients/dsh-remote/test/agent-codex.test.mjs +535 -0
  19. package/clients/dsh-remote/test/agent-detect.test.mjs +390 -0
  20. package/clients/dsh-remote/test/agent-events.test.mjs +340 -0
  21. package/clients/dsh-remote/test/agent-gemini.test.mjs +355 -0
  22. package/clients/dsh-remote/test/agent-jsonl-driver.test.mjs +165 -0
  23. package/clients/dsh-remote/test/agent-opencode.test.mjs +369 -0
  24. package/clients/dsh-remote/test/agent-permission-bridge.test.mjs +340 -0
  25. package/clients/dsh-remote/test/agent-proc.test.mjs +175 -0
  26. package/clients/dsh-remote/test/agent-runtime.test.mjs +282 -0
  27. package/clients/dsh-remote/test/agent-version.test.mjs +122 -0
  28. package/clients/dsh-remote/test/approval-coordinator.test.mjs +192 -0
  29. package/clients/dsh-remote/test/decision-poller.test.mjs +328 -0
  30. package/clients/dsh-remote/test/im-format.test.mjs +229 -0
  31. package/clients/dsh-remote/test/upstream-discovery.test.mjs +203 -2
  32. package/clients/dsh-remote/test/wecom-bot.test.mjs +1718 -0
  33. package/clients/dsh-remote/upstream-discovery.mjs +495 -43
  34. package/clients/dsh-remote/wecom-bot.mjs +2670 -0
  35. package/dsh-setup.mjs +95 -9
  36. package/package.json +2 -2
  37. package/packages/dsh-remote-web/lib/client.js +1377 -292
  38. package/packages/dsh-remote-web/lib/index.js +419 -12
  39. package/packages/dsh-remote-web/package.json +1 -1
  40. package/packages/dsh-remote-web/runtime/clients/dsh-remote/agent/README.md +412 -0
  41. package/packages/dsh-remote-web/runtime/clients/dsh-remote/agent/agent-e2e-codex.mjs +185 -0
  42. package/packages/dsh-remote-web/runtime/clients/dsh-remote/agent/agent-e2e-real.mjs +443 -0
  43. package/packages/dsh-remote-web/runtime/clients/dsh-remote/agent/codex.mjs +736 -0
  44. package/packages/dsh-remote-web/runtime/clients/dsh-remote/agent/detect.mjs +399 -0
  45. package/packages/dsh-remote-web/runtime/clients/dsh-remote/agent/events.mjs +511 -0
  46. package/packages/dsh-remote-web/runtime/clients/dsh-remote/agent/gemini.mjs +503 -0
  47. package/packages/dsh-remote-web/runtime/clients/dsh-remote/agent/index.mjs +339 -0
  48. package/packages/dsh-remote-web/runtime/clients/dsh-remote/agent/opencode.mjs +628 -0
  49. package/packages/dsh-remote-web/runtime/clients/dsh-remote/agent/permission-bridge.mjs +534 -0
  50. package/packages/dsh-remote-web/runtime/clients/dsh-remote/agent/proc.mjs +179 -0
  51. package/packages/dsh-remote-web/runtime/clients/dsh-remote/agent/version.mjs +148 -0
  52. package/packages/dsh-remote-web/runtime/clients/dsh-remote/approval-coordinator.mjs +150 -0
  53. package/packages/dsh-remote-web/runtime/clients/dsh-remote/decision-poller.mjs +235 -0
  54. package/packages/dsh-remote-web/runtime/clients/dsh-remote/dsh-bridge.mjs +258 -0
  55. package/packages/dsh-remote-web/runtime/clients/dsh-remote/push-reporter.mjs +533 -0
  56. package/packages/dsh-remote-web/runtime/clients/dsh-remote/wecom-bot.mjs +2444 -0
  57. package/packages/dsh-remote-web/runtime/dsh-setup.mjs +18 -3
  58. package/packages/dsh-remote-web/test/activation-entry.test.mjs +11 -1
  59. package/packages/dsh-remote-web/test/announcements-ui.test.mjs +8 -3
  60. package/packages/dsh-remote-web/test/connect-ui.test.mjs +16 -4
  61. package/packages/dsh-remote-web/test/im-bots.test.mjs +400 -0
  62. package/packages/dsh-remote-web/test/lib-panel-ia.mjs +98 -0
  63. package/packages/dsh-remote-web/test/login-fast.test.mjs +104 -0
  64. package/packages/dsh-remote-web/test/login-selfheal-ui.test.mjs +17 -6
  65. package/packages/dsh-remote-web/test/panel-ia.test.mjs +996 -0
  66. package/packages/dsh-remote-web/test/password-reset.test.mjs +11 -1
  67. package/packages/dsh-remote-web/test/plugin-link.test.mjs +78 -0
  68. package/packages/dsh-remote-web/test/purchase-agreement-ui.test.mjs +14 -5
  69. package/packages/dsh-remote-web/test/remote-access-ui.test.mjs +69 -119
  70. package/packages/dsh-remote-web/test/self-manage.test.mjs +5 -1
  71. package/packages/dsh-remote-web/test/settings-entry.test.mjs +42 -25
  72. package/packages/dsh-remote-web/test/sms-captcha-ui.test.mjs +15 -2
  73. package/packages/dsh-remote-web/test/telemetry.test.mjs +6 -4
  74. package/packages/dsh-remote-web/test/wechat-bot-ui.test.mjs +57 -130
@@ -17,13 +17,19 @@
17
17
  */
18
18
  import assert from "node:assert/strict";
19
19
  import http from "node:http";
20
- import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
20
+ import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
21
21
  import os from "node:os";
22
22
  import path from "node:path";
23
- import test from "node:test";
23
+ import test, { beforeEach } from "node:test";
24
24
 
25
25
  import {
26
+ __resetUpstreamCachesForTest,
27
+ candidatePlan,
26
28
  candidatePorts,
29
+ discoverUpstreamFast,
30
+ discoverUpstreamFull,
31
+ isDshLikeProcessName,
32
+ isIgnoredListenProcess,
27
33
  discoverUpstream,
28
34
  listeningPortsOfPid,
29
35
  looksLikeDshWeb,
@@ -33,10 +39,16 @@ import {
33
39
  DESKTOP_DEFAULT_PORT,
34
40
  DESKTOP_PORT_SCAN,
35
41
  FALLBACK_UPSTREAM,
42
+ MAX_PROBE_CANDIDATES,
43
+ MAX_PROBE_CONCURRENCY,
44
+ PROBE_TIMEOUT_MS,
36
45
  UPSTREAM_FILE
37
46
  } from "../upstream-discovery.mjs";
38
47
 
39
48
  const DIR = mkdtempSync(path.join(os.tmpdir(), "dsh-upstream-"));
49
+ // 发现模块有进程内缓存(负缓存/已验证/监听表):用例之间必须清零,否则会互相污染
50
+ // (真实验证:前一个用例缓存了已关闭的假 dsh 端口,后面的"回退 3080"用例读到了它)。
51
+ beforeEach(() => { __resetUpstreamCachesForTest(); });
40
52
  process.on("exit", () => { try { rmSync(DIR, { recursive: true, force: true }); } catch { /* ignore */ } });
41
53
 
42
54
  /** 假的 dsh web:回一段带官方特征标记的 HTML(身份校验靠它)。 */
@@ -207,3 +219,192 @@ test("实测端口:能从本进程真实监听端口里读出来(macOS/Linux 用
207
219
  await close(srv);
208
220
  }
209
221
  });
222
+
223
+ // ===========================================================================
224
+ // 2026-11-07:不再乱敲本机所有端口(微信 CPU 高 + 登录慢)
225
+ // ===========================================================================
226
+
227
+ test("★ 进程名过滤:只保留像 dsh 宿主的端口,IM 客户端/无关进程全部跳过并计数", () => {
228
+ // 黑名单优先:名字里带 wechat 的即使像 node 也拒绝
229
+ assert.equal(isIgnoredListenProcess("WeChat"), true);
230
+ assert.equal(isIgnoredListenProcess("企业微信"), true);
231
+ assert.equal(isIgnoredListenProcess("wecom-node"), true);
232
+ assert.equal(isDshLikeProcessName("wechat-node"), false, "黑名单优先于白名单");
233
+ assert.equal(isDshLikeProcessName("QQ"), false);
234
+ assert.equal(isDshLikeProcessName("node"), true);
235
+ assert.equal(isDshLikeProcessName("nodejs"), true);
236
+ assert.equal(isDshLikeProcessName("dsh-web"), true);
237
+ assert.equal(isDshLikeProcessName("deepseek-harness"), true);
238
+ assert.equal(isDshLikeProcessName("Electron"), true);
239
+ assert.equal(isDshLikeProcessName("python3"), false);
240
+ assert.equal(isDshLikeProcessName(""), false);
241
+
242
+ const plan = candidatePlan({
243
+ relayDir: "", // 不读端口文件:本用例只关心进程名过滤
244
+ env: {},
245
+ platform: "win32",
246
+ includeStatic: false,
247
+ listeners: [
248
+ { port: 40001, name: "WeChat", pid: 1 },
249
+ { port: 40002, name: "企业微信", pid: 2 },
250
+ { port: 40003, name: "node", pid: 3 },
251
+ { port: 40004, name: "Electron", pid: 4 },
252
+ { port: 40005, name: "deepseek-harness", pid: 5 },
253
+ { port: 40006, name: "python3", pid: 6 },
254
+ ],
255
+ });
256
+ const ports = plan.entries.map((e) => e.port).sort((a, b) => a - b);
257
+ assert.deepEqual(ports, [40003, 40004, 40005], "★ 只保留 node/electron/deepseek 的端口;微信/企微/python 全部排除");
258
+ assert.equal(plan.skipped, 3, "★ skipped 必须统计被跳过的非 node 进程端口(微信、企微、python)");
259
+ assert.ok(plan.entries.every((e) => e.source === "listen"));
260
+ });
261
+
262
+ test("★ 拿不到进程名时**绝不退化成全量探测**(SKIP 整层,不瞎敲)", () => {
263
+ // listeners:false = 模拟 lsof/ss 不可用(如 NAS/精简系统)
264
+ const plan = candidatePlan({ relayDir: "", env: {}, platform: "win32", includeStatic: false, listeners: false });
265
+ assert.equal(plan.entries.length, 0, "拿不到进程名 → 本层一个候选都不收");
266
+ assert.equal(plan.skipped, 0, "这不是'跳过',是'整层不可用'");
267
+ // 旧实现会把所有监听端口都收进来(listenPorts 注入路径保持兼容,供测试/特殊部署显式使用)
268
+ const injected = candidatePlan({ relayDir: "", env: {}, platform: "win32", includeStatic: false, listenPorts: [40003, 40004] });
269
+ assert.deepEqual(injected.entries.map((e) => e.port).sort(), [40003, 40004], "显式注入路径保持既有语义");
270
+ });
271
+
272
+ test("★ 负缓存:判定'不是 dsh'的端口 10 分钟内不再探测", async () => {
273
+ const stranger = await listen((_req, res) => { res.writeHead(200, { "content-type": "text/plain" }); res.end(STRANGER); });
274
+ writeHint("not-a-url");
275
+ try {
276
+ const first = await discoverUpstreamFull({ relayDir: DIR, env: {}, extraPorts: [stranger.port], listeners: false, platform: "win32", fetchImpl: blockedFetch, timeoutMs: 300 });
277
+ assert.ok(first.probed.includes(stranger.port), "第一次必须探测该端口");
278
+ const second = await discoverUpstreamFull({ relayDir: DIR, env: {}, extraPorts: [stranger.port], listeners: false, platform: "win32", fetchImpl: blockedFetch, timeoutMs: 300 });
279
+ assert.ok(!second.probed.includes(stranger.port), "★ 负缓存内不得重复探测(微信 CPU 的根因之一就是反复敲)");
280
+ } finally {
281
+ await close(stranger);
282
+ }
283
+ });
284
+
285
+ test("★ fast 路径:没有 hint/缓存/进程端口时**一个请求都不发**(<50ms 语义)", async () => {
286
+ writeHint("not-a-url");
287
+ let calls = 0;
288
+ const fetchImpl = () => { calls += 1; return Promise.reject(new Error("fast 不该探测")); };
289
+ const t0 = Date.now();
290
+ const fast = await discoverUpstreamFast({
291
+ relayDir: DIR, env: {}, platform: "win32", listeners: false, fetchImpl
292
+ });
293
+ const cost = Date.now() - t0;
294
+ assert.equal(fast.url, "", "没有可用来源时返回空(由调用方决定回退)");
295
+ assert.equal(calls, 0, "★ fast 不得探测未知端口");
296
+ assert.ok(cost < 50, `fast 必须很快(实际 ${cost}ms)`);
297
+ });
298
+
299
+ test("★ 探测成本:超时 300ms;150ms 才回的 dsh 仍能命中,>300ms 的不再等", async () => {
300
+ assert.equal(PROBE_TIMEOUT_MS, 300, "默认探测超时必须是 300ms");
301
+ let pending = [];
302
+ const slowOk = await listen((_req, res) => {
303
+ pending.push(setTimeout(() => { res.writeHead(200, { "content-type": "text/html" }); res.end(DSH_HTML); }, 150));
304
+ });
305
+ const tooSlow = await listen((_req, res) => {
306
+ pending.push(setTimeout(() => { res.writeHead(200, { "content-type": "text/html" }); res.end(DSH_HTML); }, 450));
307
+ });
308
+ try {
309
+ assert.equal(await probeDshWeb(slowOk.url), true, "150ms 内响应 → 必须命中(缩短超时不能把慢机器误杀)");
310
+ assert.equal(await probeDshWeb(tooSlow.url), false, "超过 300ms 的静默端口不再等");
311
+ } finally {
312
+ for (const t of pending) clearTimeout(t);
313
+ pending = [];
314
+ await close(slowOk); await close(tooSlow);
315
+ }
316
+ });
317
+
318
+ test("★ 并发上限:候选再多也最多 16 个并发探测", async () => {
319
+ const servers = [];
320
+ try {
321
+ for (let i = 0; i < 20; i++) {
322
+ servers.push(await listen((_req, res) => {
323
+ setTimeout(() => { res.writeHead(200, { "content-type": "text/plain" }); res.end(STRANGER); }, 40);
324
+ }));
325
+ }
326
+ const inflight = { now: 0, max: 0 };
327
+ const countingFetch = (input, init) => {
328
+ inflight.now += 1;
329
+ inflight.max = Math.max(inflight.max, inflight.now);
330
+ return Promise.resolve(blockedFetch(input, init)).finally(() => { inflight.now -= 1; });
331
+ };
332
+ writeHint("not-a-url");
333
+ await discoverUpstreamFull({
334
+ relayDir: DIR, env: {}, platform: "win32", listeners: false,
335
+ extraPorts: servers.map((s) => s.port), fetchImpl: countingFetch, timeoutMs: 300
336
+ });
337
+ assert.ok(inflight.max <= MAX_PROBE_CONCURRENCY, `★ 并发不得超过 ${MAX_PROBE_CONCURRENCY}(实际 ${inflight.max})`);
338
+ assert.ok(inflight.max >= 2, "应当确实并行(否则发现会串行变慢)");
339
+ } finally {
340
+ for (const s of servers) await close(s);
341
+ }
342
+ });
343
+
344
+ test("★ 行为不回归:过滤后仍能发现非默认端口上的 dsh web(能力保留)", async () => {
345
+ const dsh = await listen((_req, res) => { res.writeHead(200, { "content-type": "text/html" }); res.end(DSH_HTML); });
346
+ writeHint("not-a-url");
347
+ try {
348
+ const found = await discoverUpstreamFull({
349
+ relayDir: DIR, env: {}, platform: "win32", listeners: false, fetchImpl: blockedFetch,
350
+ extraPorts: [dsh.port], timeoutMs: 300
351
+ });
352
+ assert.equal(found.url, dsh.url, "非默认端口仍必须能发现(过滤只排除非 dsh 进程,不排除位置)");
353
+ assert.equal(found.source, "extra");
354
+ } finally {
355
+ await close(dsh);
356
+ }
357
+ });
358
+
359
+ test("★ 每个候选最多 1 次请求:连 HTTP 都不通的端口不再打第二条探测", async () => {
360
+ // ① 死端口:旧实现 / + /dsh-remote/self = 2 个请求;现在 1 个
361
+ let calls = 0;
362
+ const deadFetch = () => { calls += 1; return Promise.reject(Object.assign(new Error("ECONNREFUSED"), { code: "ECONNREFUSED" })); };
363
+ assert.equal(await probeDshWeb("http://127.0.0.1:1", { fetchImpl: deadFetch }), false);
364
+ assert.equal(calls, 1, "★ 首探连 HTTP 响应都没拿到 → 不再打 /dsh-remote/self(省一半连接)");
365
+
366
+ // ② 正常 HTTP 的陌生服务:首页特征不中时仍要问一次 /dsh-remote/self(保持识别能力)
367
+ const stranger = await listen((_req, res) => { res.writeHead(200, { "content-type": "text/plain" }); res.end(STRANGER); });
368
+ let n = 0;
369
+ const real = (input, init) => { n += 1; return realFetch(input, init); };
370
+ try {
371
+ assert.equal(await probeDshWeb(stranger.url, { fetchImpl: real }), false);
372
+ assert.equal(n, 2, "拿到了 HTTP 响应(只是特征不中)→ 允许第二次探测 /dsh-remote/self");
373
+ } finally {
374
+ await close(stranger);
375
+ }
376
+ });
377
+
378
+ test("★ 候选上限:超过 20 个时整层放弃 listen 扫描(只探 hint/进程/静态)", async () => {
379
+ // 注入 24 个"像 node"的监听端口(超过 LISTEN_PROBE_LIMIT 与 MAX_PROBE_CANDIDATES)
380
+ const listeners = Array.from({ length: 24 }, (_, i) => ({ port: 45000 + i, name: "node", pid: 1000 + i }));
381
+ let calls = 0;
382
+ const fetchImpl = () => { calls += 1; return Promise.reject(new Error("blocked")); };
383
+ writeHint("not-a-url");
384
+ const found = await discoverUpstreamFull({
385
+ relayDir: DIR, env: {}, platform: "win32", listeners, fetchImpl, timeoutMs: 60
386
+ });
387
+ assert.ok(MAX_PROBE_CANDIDATES === 20);
388
+ assert.ok(!found.probed.some((p) => p >= 45000 && p < 45024), "★ 超限时不得探测 listen 层的端口");
389
+ // 静态候选仍会探(3080 + Desktop 区间),所以请求数应为 13 而不是 13+24
390
+ assert.ok(calls <= DESKTOP_PORT_SCAN + 1, `请求数应被压到静态候选量级(实际 ${calls})`);
391
+ });
392
+
393
+ test("微信通道与上游发现互不相干:不枚举/不探测本机端口(源级断言,Lead 补充报告第 3 条)", () => {
394
+ for (const f of ["wechat-channel.mjs", "wechat-runtime.mjs"]) {
395
+ const src = readFileSync(new URL(`../${f}`, import.meta.url), "utf8");
396
+ assert.ok(!/upstream-discovery/.test(src), `${f} 不得 import 上游发现(微信通道只跟 ilinkai.weixin.qq.com 通信)`);
397
+ assert.ok(!/allLoopbackListeningPorts|candidatePorts|listeningPortsWithProcess/.test(src), `${f} 不得枚举本机监听端口`);
398
+ assert.ok(!/lsof|netstat/.test(src), `${f} 不得调用 lsof/netstat 扫本机端口`);
399
+ }
400
+ });
401
+
402
+ test("dsh-setup watcher:失败指数退避 + 端口文件事件驱动(源级契约)", () => {
403
+ const setup = readFileSync(new URL("../../../dsh-setup.mjs", import.meta.url), "utf8");
404
+ assert.match(setup, /UPSTREAM_DISCOVERY_BACKOFF_MS\s*=\s*\[15_000,\s*30_000,\s*60_000,\s*300_000\]/,
405
+ "连续失败退避必须是 15s→30s→60s→300s");
406
+ assert.match(setup, /if \(now < upstreamDiscoveryNextAt\) return upstreamUrl\(\);/, "退避检查必须在 force 分支之外(force 不得打穿退避)");
407
+ assert.match(setup, /watchUpstreamFile\(ensureBridge\)/, "watcher 必须监听 .dsh-upstream 的变化(事件驱动替代全量扫描)");
408
+ assert.match(setup, /fs\.watchFile\(file, \{ interval: 1000, persistent: false \}/, "监听用 1s stat(不扫端口)");
409
+ assert.ok(/upstreamDiscoveryFails = 0;\s*\n\s*upstreamDiscoveryNextAt = 0;/.test(setup), "端口文件变化时必须清零退避,立即采用新端口");
410
+ });