dsh-remote-web 0.7.0-beta.1 → 0.7.0-beta.2
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/index.js
CHANGED
|
@@ -740,6 +740,34 @@ function applySaaSMode(cfg) {
|
|
|
740
740
|
* 老版本(无该鉴权)下 connection 服务没有 authenticatedUrl → 静默跳过,行为不变。
|
|
741
741
|
*/
|
|
742
742
|
const HARNESS_COOKIE_FILE = ".harness-cookie.json";
|
|
743
|
+
/**
|
|
744
|
+
* Cookie 按 **authority 分文件**(0.7.0-beta.2 起,2026-10-09 事故根治)。
|
|
745
|
+
*
|
|
746
|
+
* 【事故】`~/.dsh-remote/` 只有一份 `.harness-cookie.json`,而 Web 端(3080)与桌面端(每次启动
|
|
747
|
+
* 随机端口)是**两个宿主进程**,各自的插件都往里写"自己 authority 的 Cookie" → 互相覆盖。
|
|
748
|
+
* 桥撞 401 后重读磁盘,拿到的是另一个宿主刚写的 Cookie → 重试 → 还是 401 → 再读 → 又被
|
|
749
|
+
* 覆盖回去……**乒乓,永不自愈**,手机端持续白页(现象与"自愈坏了"一模一样,极易误判)。
|
|
750
|
+
* 【根治】每个宿主只写自己 authority 的文件:`.harness-cookie.<authority>.json`
|
|
751
|
+
* (`:` → `_`,如 `.harness-cookie.127.0.0.1_19387.json`),互不覆盖。旧版共享文件继续**写**
|
|
752
|
+
* 一份镜像(旧桥还能用),但新桥只认当前上游 authority 的那一份,foreign 一律跳过。
|
|
753
|
+
* 命名规则与桥半 `clients/dsh-remote/dsh-bridge.mjs` 同名,漂移由
|
|
754
|
+
* `clients/dsh-remote/test/harness-cookie-authority.test.mjs` 的跨文件不变量锁住。
|
|
755
|
+
*/
|
|
756
|
+
function authoritySlug(authority) {
|
|
757
|
+
return String(authority).replace(/[^A-Za-z0-9._-]/g, "_");
|
|
758
|
+
}
|
|
759
|
+
/** 本宿主专属 Cookie 文件名(与桥半同规则)。 */
|
|
760
|
+
function harnessCookieFileFor(authority) {
|
|
761
|
+
return `${HARNESS_COOKIE_FILE.slice(0, -".json".length)}.${authoritySlug(authority)}.json`;
|
|
762
|
+
}
|
|
763
|
+
/** 本宿主专属「作废」标记名(与桥半同规则)。 */
|
|
764
|
+
function harnessCookieRevokedFileFor(authority) {
|
|
765
|
+
return `${HARNESS_COOKIE_REVOKED_FILE}.${authoritySlug(authority)}`;
|
|
766
|
+
}
|
|
767
|
+
/** 由本进程 webServer 端口推出 authority(与 bridge 的 Host 口径一致)。 */
|
|
768
|
+
function harnessAuthorityOfPort(port) {
|
|
769
|
+
return port ? `127.0.0.1:${port}` : "";
|
|
770
|
+
}
|
|
743
771
|
/**
|
|
744
772
|
* 手机端 401「dsh web authentication required; reopen the URL printed by dsh web」的根因就在这里:
|
|
745
773
|
* 该 Cookie 由本插件在进程内经 ?token= 换取后交给 bridge 代持,**dsh web 每次重启都会换签名密钥**,
|
|
@@ -786,22 +814,47 @@ async function mintHarnessCookie(ctx, relayDir) {
|
|
|
786
814
|
if (UNINSTALLED_DIRS.has(relayDir)) return false;
|
|
787
815
|
const out = { authority: `127.0.0.1:${port}`, cookie, mintedAt: Date.now() };
|
|
788
816
|
mkdirSync(dirname(configPathOf(relayDir)), { recursive: true });
|
|
789
|
-
|
|
790
|
-
|
|
817
|
+
// ① 本宿主专属文件(权威,两个宿主互不覆盖 —— 事故根治点)
|
|
818
|
+
const ownPath = join(relayDir, harnessCookieFileFor(out.authority));
|
|
819
|
+
writeFileSync(ownPath, JSON.stringify(out, null, 2), { mode: 0o600 });
|
|
820
|
+
hardenFile(ownPath); // 会话 Cookie ≈ dsh web 的完整访问权,同样要收紧
|
|
821
|
+
// ② 旧版共享文件镜像:只为还跑着旧桥(<0.7.0-beta.2)的机器保留 —— 新桥优先读 ①,
|
|
822
|
+
// 且对 authority 不符的镜像**直接跳过**,所以这里写镜像不会再引发乒乓。
|
|
823
|
+
const legacyPath = join(relayDir, HARNESS_COOKIE_FILE);
|
|
824
|
+
writeFileSync(legacyPath, JSON.stringify(out, null, 2), { mode: 0o600 });
|
|
825
|
+
hardenFile(legacyPath);
|
|
791
826
|
return true;
|
|
792
827
|
} catch {
|
|
793
828
|
return false;
|
|
794
829
|
}
|
|
795
830
|
}
|
|
796
831
|
|
|
797
|
-
/**
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
832
|
+
/**
|
|
833
|
+
* 读取当前 authority 可用的 Cookie 文件状态:{cookie, authority, mintedAt} 或 null。
|
|
834
|
+
* ① 本宿主专属文件优先;
|
|
835
|
+
* ② 旧版共享文件仅在 authority 对得上、或旧插件没写 authority 字段(兼容)时采用;
|
|
836
|
+
* ③ 声明了别的 authority → 明确不用(那属于另一个宿主,用了必然 401)。
|
|
837
|
+
* @param {string} relayDir 配置目录
|
|
838
|
+
* @param {string} [authority] 本进程 authority(`127.0.0.1:<port>`);缺省只做兼容读取
|
|
839
|
+
*/
|
|
840
|
+
function readHarnessCookie(relayDir, authority) {
|
|
841
|
+
const read = (file) => {
|
|
842
|
+
try {
|
|
843
|
+
const j = JSON.parse(readFileSync(join(relayDir, file), "utf8"));
|
|
844
|
+
return j && typeof j.cookie === "string" && j.cookie ? j : null;
|
|
845
|
+
} catch {
|
|
846
|
+
return null;
|
|
847
|
+
}
|
|
848
|
+
};
|
|
849
|
+
if (authority) {
|
|
850
|
+
const own = read(harnessCookieFileFor(authority));
|
|
851
|
+
if (own) return own;
|
|
804
852
|
}
|
|
853
|
+
const legacy = read(HARNESS_COOKIE_FILE);
|
|
854
|
+
if (!legacy) return null;
|
|
855
|
+
const claimed = typeof legacy.authority === "string" ? legacy.authority : "";
|
|
856
|
+
if (claimed === "" || !authority || claimed === authority) return legacy;
|
|
857
|
+
return null; // foreign authority:另一个宿主的 Cookie,本宿主用不了
|
|
805
858
|
}
|
|
806
859
|
|
|
807
860
|
let harnessMintInflight = false;
|
|
@@ -819,9 +872,22 @@ let harnessMintLastAt = 0;
|
|
|
819
872
|
*/
|
|
820
873
|
async function ensureHarnessCookie(ctx, relayDir, opts) {
|
|
821
874
|
const force = !!(opts && opts.force);
|
|
822
|
-
const
|
|
875
|
+
const authority = harnessAuthorityOfPort(ctx.webServer?.port);
|
|
876
|
+
const ownRevokedPath = authority ? join(relayDir, harnessCookieRevokedFileFor(authority)) : "";
|
|
877
|
+
const legacyRevokedPath = join(relayDir, HARNESS_COOKIE_REVOKED_FILE);
|
|
878
|
+
const cur = readHarnessCookie(relayDir, authority);
|
|
879
|
+
// 作废标记两个来源:
|
|
880
|
+
// ① 本宿主专属标记(新桥 0.7.0-beta.2+ 写)—— 精确,不会被人家的 401 催着重换;
|
|
881
|
+
// ② 旧版共享标记(旧桥写)—— 仅当它**晚于**我们手上这份 Cookie 时才认:说明是它被拒了。
|
|
882
|
+
// 否则(比如另一个宿主刚被拒、标记比我们的 Cookie 还旧)不理会,避免两个宿主互相催。
|
|
883
|
+
let revoked = ownRevokedPath !== "" && existsSync(ownRevokedPath);
|
|
884
|
+
if (!revoked && existsSync(legacyRevokedPath)) {
|
|
885
|
+
try {
|
|
886
|
+
const revokedAt = Number(readFileSync(legacyRevokedPath, "utf8").trim()) || 0;
|
|
887
|
+
revoked = !cur || revokedAt > Number(cur.mintedAt || 0);
|
|
888
|
+
} catch { /* 标记读不到就当没有 */ }
|
|
889
|
+
}
|
|
823
890
|
if (!force && !revoked) {
|
|
824
|
-
const cur = readHarnessCookie(relayDir);
|
|
825
891
|
if (cur && Date.now() - Number(cur.mintedAt || 0) < HARNESS_COOKIE_STALE_MS) return true;
|
|
826
892
|
}
|
|
827
893
|
if (harnessMintInflight) return false; // 去重:同一时刻只换一次
|
|
@@ -831,7 +897,11 @@ async function ensureHarnessCookie(ctx, relayDir, opts) {
|
|
|
831
897
|
try {
|
|
832
898
|
const ok = await mintHarnessCookie(ctx, relayDir);
|
|
833
899
|
if (ok || revoked) {
|
|
834
|
-
|
|
900
|
+
// 清标记:① 本宿主专属标记(精确);② 旧版共享标记(旧桥写的,清了不影响新桥)。
|
|
901
|
+
// 代价:多宿主机器上可能清掉"另一个宿主"的旧标记 —— 那只让对方的**快速**自愈退化成
|
|
902
|
+
// 30 分钟周期刷新,不会造成错误状态,故不做更细的归属判定(复杂度不值)。
|
|
903
|
+
if (ownRevokedPath) { try { rmSync(ownRevokedPath, { force: true }); } catch { /* ignore */ } }
|
|
904
|
+
try { rmSync(legacyRevokedPath, { force: true }); } catch { /* ignore */ }
|
|
835
905
|
}
|
|
836
906
|
if (!ok && revoked) console.warn("[dsh-remote-web] 浏览器会话 Cookie 已被 dsh web 作废,重取失败,下次查状态再试");
|
|
837
907
|
return ok;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-remote-web",
|
|
3
|
-
"version": "0.7.0-beta.
|
|
3
|
+
"version": "0.7.0-beta.2",
|
|
4
4
|
"description": "公网远程控制 DeepSeek Harness(dsh web):安装即得专属加密地址,人在外面也能用手机访问电脑上的 dsh——无需同一局域网/WiFi、无需公网 IP 与内网穿透,全程加密;手机端 100% 还原电脑体验(对话/工具/审批/设置)。技术用户可选自建服务,流量走自己的服务器。Remote control DeepSeek Harness (dsh web) from anywhere over the public internet — install, get an encrypted URL, use it from your phone on any network (dual-half cordis plugin: Settings panel + same-origin /dsh-remote routes). (2026-09 由 dsh-remote-ui 更名 / renamed from dsh-remote-ui; dsh-remote 的 dsh web 插件半,不是纯 UI/皮肤插件)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -320,21 +320,100 @@ const HARNESS_COOKIE_FILE = ".harness-cookie.json";
|
|
|
320
320
|
const HARNESS_UNAUTHORIZED_TEXT = "dsh web authentication required";
|
|
321
321
|
/** 撞到 401 时写的「作废」标记:插件在面板轮询时看到它就会立刻重换 Cookie(见 node 半 ensureHarnessCookie)。 */
|
|
322
322
|
const HARNESS_COOKIE_REVOKED_FILE = ".harness-cookie-revoked";
|
|
323
|
-
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Cookie 按 **authority 分文件**(0.7.0-beta.2 起,2026-10-09 事故根治)。
|
|
326
|
+
*
|
|
327
|
+
* 【事故】`~/.dsh-remote/` 只有一份 `.harness-cookie.json`,而 Web 端(3080)与桌面端(每次启动
|
|
328
|
+
* 随机端口)是**两个宿主、两个进程**,各自的插件都会往里写"自己 authority 的 Cookie" →
|
|
329
|
+
* 互相覆盖。桥撞 401 后重读磁盘,拿到的是**另一个宿主刚写的** Cookie(与失败的不是同一个)
|
|
330
|
+
* → 重试 → 还是 401 → 再读 → 又被覆盖回去……**乒乓,永不自愈**。手机端持续白页。
|
|
331
|
+
* 【根治】每个宿主只写自己 authority 的文件:`.harness-cookie.<authority>.json`
|
|
332
|
+
* (`:` 换成 `_`,如 `.harness-cookie.127.0.0.1_19387.json`),互不覆盖;桥只认
|
|
333
|
+
* **当前上游 authority** 的那一份。旧版共享文件继续兼容读取,但**foreign authority 一律不用**
|
|
334
|
+
* —— 那正是乒乓的来源。命名规则与插件半 `packages/dsh-remote-web/lib/index.js` 同名,
|
|
335
|
+
* 漂移由 `test/harness-cookie-authority.test.mjs` 的跨文件不变量锁住。
|
|
336
|
+
*/
|
|
337
|
+
function authoritySlug(authority) {
|
|
338
|
+
return String(authority).replace(/[^A-Za-z0-9._-]/g, "_");
|
|
339
|
+
}
|
|
340
|
+
/** 本宿主专属 Cookie 文件名(与插件半同规则)。 */
|
|
341
|
+
function harnessCookieFileFor(authority) {
|
|
342
|
+
return `${HARNESS_COOKIE_FILE.slice(0, -".json".length)}.${authoritySlug(authority)}.json`;
|
|
343
|
+
}
|
|
344
|
+
/** 本宿主专属「作废」标记名(与插件半同规则)。 */
|
|
345
|
+
function harnessCookieRevokedFileFor(authority) {
|
|
346
|
+
return `${HARNESS_COOKIE_REVOKED_FILE}.${authoritySlug(authority)}`;
|
|
347
|
+
}
|
|
348
|
+
/** 当前上游的 authority(`127.0.0.1:<port>`);取不到返回空串。 */
|
|
349
|
+
function upstreamAuthorityOf(upstream) {
|
|
324
350
|
try {
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
351
|
+
return new URL(String(upstream || UPSTREAM)).host || "";
|
|
352
|
+
} catch {
|
|
353
|
+
return "";
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
/** 已就"哪个 foreign authority 在抢"告警过的集合(每对只吵一次,不刷屏)。 */
|
|
357
|
+
const foreignCookieWarned = new Set();
|
|
358
|
+
function warnForeignCookieOnce(claimed, authority) {
|
|
359
|
+
const key = `${claimed}->${authority}`;
|
|
360
|
+
if (foreignCookieWarned.has(key)) return;
|
|
361
|
+
foreignCookieWarned.add(key);
|
|
362
|
+
console.warn(
|
|
363
|
+
`[bridge] 检测到另一宿主的会话 Cookie(${claimed}),而当前上游是 ${authority} —— ` +
|
|
364
|
+
`一台机器只能服务一个宿主,已跳过该 Cookie(不再乒乓重试)。` +
|
|
365
|
+
`若手机端仍 401,请确认 .dsh-upstream 指向正在运行的那个宿主,并让该宿主的插件重发 Cookie。`
|
|
366
|
+
);
|
|
367
|
+
}
|
|
368
|
+
function readCookieDoc(dir, file) {
|
|
369
|
+
try {
|
|
370
|
+
const j = JSON.parse(fs.readFileSync(path.join(dir, file), "utf8"));
|
|
371
|
+
return j && typeof j.cookie === "string" && j.cookie ? j : null;
|
|
372
|
+
} catch {
|
|
373
|
+
return null;
|
|
374
|
+
}
|
|
328
375
|
}
|
|
376
|
+
/**
|
|
377
|
+
* 当前上游该用的浏览器会话 Cookie(空串 = 没有可用的)。
|
|
378
|
+
*
|
|
379
|
+
* 取值顺序(2026-10-09 起):
|
|
380
|
+
* ① 本宿主专属文件 `.harness-cookie.<authority>.json` —— 新插件写的,权威;
|
|
381
|
+
* ② 旧版共享 `.harness-cookie.json` —— 仅当它声明的 `authority` 与当前上游一致,
|
|
382
|
+
* 或旧插件根本没写 authority 字段(向后兼容)时才用;
|
|
383
|
+
* ③ 声明了**别的** authority → 明确不用,并告警一次(这就是乒乓的源头)。
|
|
384
|
+
*/
|
|
329
385
|
function harnessCookieOf() {
|
|
386
|
+
const dir = path.dirname(CONFIG_PATH);
|
|
387
|
+
const authority = upstreamAuthorityOf();
|
|
388
|
+
if (authority) {
|
|
389
|
+
const own = readCookieDoc(dir, harnessCookieFileFor(authority));
|
|
390
|
+
if (own) return own.cookie;
|
|
391
|
+
}
|
|
392
|
+
const legacy = readCookieDoc(dir, HARNESS_COOKIE_FILE);
|
|
393
|
+
if (!legacy) return "";
|
|
394
|
+
const claimed = typeof legacy.authority === "string" ? legacy.authority : "";
|
|
395
|
+
if (claimed === "" || authority === "" || claimed === authority) return legacy.cookie;
|
|
396
|
+
warnForeignCookieOnce(claimed, authority);
|
|
397
|
+
return "";
|
|
398
|
+
}
|
|
399
|
+
/** 本宿主 Cookie 是否处于「已作废、等插件重换」状态(诊断用,不参与取值)。 */
|
|
400
|
+
function harnessCookieRevokedFor(authority) {
|
|
330
401
|
try {
|
|
331
|
-
|
|
332
|
-
const j = JSON.parse(fs.readFileSync(p, "utf8"));
|
|
333
|
-
return j && typeof j.cookie === "string" && j.cookie ? j.cookie : "";
|
|
402
|
+
return fs.existsSync(path.join(path.dirname(CONFIG_PATH), harnessCookieRevokedFileFor(authority)));
|
|
334
403
|
} catch {
|
|
335
|
-
return
|
|
404
|
+
return false;
|
|
336
405
|
}
|
|
337
406
|
}
|
|
407
|
+
function markHarnessCookieRevoked() {
|
|
408
|
+
const dir = path.dirname(CONFIG_PATH);
|
|
409
|
+
const authority = upstreamAuthorityOf();
|
|
410
|
+
// 只写**本宿主**的标记:两个宿主各写各的,插件侧才不会被人家的 401 反复催着重换。
|
|
411
|
+
// (旧版共享标记 `.harness-cookie-revoked` 不再写:它是"两个写者抢一份文件"的同款隐患。)
|
|
412
|
+
if (!authority) return;
|
|
413
|
+
try {
|
|
414
|
+
fs.writeFileSync(path.join(dir, harnessCookieRevokedFileFor(authority)), String(Date.now()), { mode: 0o600 });
|
|
415
|
+
} catch { /* 忽略:标记只为加速自愈 */ }
|
|
416
|
+
}
|
|
338
417
|
|
|
339
418
|
/**
|
|
340
419
|
* 解析稳定 deviceId:优先级 env DSH_BRIDGE_DEVICE_ID > argv[2] > 配置 device_id > 生成。
|
|
@@ -1568,6 +1647,18 @@ function wechatAppUrl() {
|
|
|
1568
1647
|
}
|
|
1569
1648
|
|
|
1570
1649
|
let wechatRuntime = null;
|
|
1650
|
+
|
|
1651
|
+
/**
|
|
1652
|
+
* 微信事件通道要连的上游(**每次重连都会调一次**)。
|
|
1653
|
+
*
|
|
1654
|
+
* 行为:先顺手触发一次上游重发现(内部有 15s 冷却 + 单飞,反复调用不会打爆 lsof/探测),
|
|
1655
|
+
* 再返回**当下**的 UPSTREAM。这样宿主换端口时通道能自己跟上,而不必重启 bridge。
|
|
1656
|
+
*/
|
|
1657
|
+
function currentUpstreamForChannel() {
|
|
1658
|
+
void refreshUpstream("微信事件通道重连");
|
|
1659
|
+
return UPSTREAM;
|
|
1660
|
+
}
|
|
1661
|
+
|
|
1571
1662
|
function startWeChat() {
|
|
1572
1663
|
if (WECHAT_DISABLED) {
|
|
1573
1664
|
console.log("[bridge] 微信通道已禁用(DSH_WECHAT=0)");
|
|
@@ -1578,10 +1669,15 @@ function startWeChat() {
|
|
|
1578
1669
|
const cfg = loadLocalConfig();
|
|
1579
1670
|
wechatRuntime = createWeChatRuntime({
|
|
1580
1671
|
relayDir,
|
|
1581
|
-
//
|
|
1582
|
-
//
|
|
1583
|
-
//
|
|
1584
|
-
|
|
1672
|
+
// 传**取值器**而不是当次的值(0.7.0-beta.2 修复)。
|
|
1673
|
+
//
|
|
1674
|
+
// 【2026-10-09 真机事故】这里以前传的是 `UPSTREAM` 这个**当次快照**:bridge 起来时宿主还在
|
|
1675
|
+
// 3080,随后宿主换了端口(Desktop 每次启动随机),隧道转发侧因为每次读最新 UPSTREAM 而
|
|
1676
|
+
// 一切正常,**唯独微信事件通道抱着 3080 死循环重连** —— 现象极具迷惑性:手机能开页面、
|
|
1677
|
+
// bridge-status 显示 online,但任务跑完**一条推送都没有**,日志里只有刷屏的
|
|
1678
|
+
// `connect ECONNREFUSED 127.0.0.1:3080`。
|
|
1679
|
+
// 现在取值器每次重连都会重新求值,并在打到死端口时主动触发一次上游重发现。
|
|
1680
|
+
upstream: () => currentUpstreamForChannel(),
|
|
1585
1681
|
cookieOf: harnessCookieOf,
|
|
1586
1682
|
secret: process.env.DSH_BRIDGE_SECRET || (typeof cfg.bridge_secret === "string" ? cfg.bridge_secret : ""),
|
|
1587
1683
|
// 免费/付费分层:档位来自账号的生效套餐,App 链接是免费用户越界时的转化入口。
|
|
@@ -457,6 +457,17 @@ class EventSubscriber extends EventEmitter {
|
|
|
457
457
|
*/
|
|
458
458
|
#cursors = new Map();
|
|
459
459
|
|
|
460
|
+
/**
|
|
461
|
+
* 上一次**连接尝试**用的上游 URL(不是"成功"的那一次)。
|
|
462
|
+
*
|
|
463
|
+
* 为什么记"尝试"而不是"成功":上游端口从 A 换到 B 时,第一轮打 B 必然是失败的
|
|
464
|
+
* (B 还没起来),若只在成功后才更新,就会每轮都判成"目标变了"而重置退避 ——
|
|
465
|
+
* 退避彻底失效、变成猛敲 B。记"上次尝试过的目标"才能做到**同一个目标只重置一次**。
|
|
466
|
+
*/
|
|
467
|
+
#lastConnectUrl = null;
|
|
468
|
+
/** 上一次成功求值到的上游(getter 抛错/返回空时的回退,保证不会去打空地址)。 */
|
|
469
|
+
#lastGoodUpstream = null;
|
|
470
|
+
|
|
460
471
|
#seq = 0;
|
|
461
472
|
#lastFrameAt = 0;
|
|
462
473
|
#authStatus = null;
|
|
@@ -465,11 +476,19 @@ class EventSubscriber extends EventEmitter {
|
|
|
465
476
|
|
|
466
477
|
constructor(options = {}) {
|
|
467
478
|
super();
|
|
468
|
-
|
|
479
|
+
const upstreamGiven = typeof options.upstream === "function"
|
|
480
|
+
? true
|
|
481
|
+
: typeof options.upstream === "string" && options.upstream.trim() !== "";
|
|
482
|
+
if (!upstreamGiven) {
|
|
469
483
|
throw new TypeError("dsh-events: options.upstream is required (e.g. http://127.0.0.1:3080)");
|
|
470
484
|
}
|
|
485
|
+
// upstream 允许是**函数**(每次连接时求值):宿主端口会变(`dsh web --port 0`、
|
|
486
|
+
// DSH Desktop 每次启动随机端口),写死字符串的话端口一变,本订阅器就会永久打旧端口。
|
|
487
|
+
const upstreamGetter = typeof options.upstream === "function"
|
|
488
|
+
? options.upstream
|
|
489
|
+
: () => options.upstream;
|
|
471
490
|
this.#opts = {
|
|
472
|
-
|
|
491
|
+
upstreamGetter,
|
|
473
492
|
cookie: options.cookie ?? "",
|
|
474
493
|
muxPath: options.muxPath ?? MUX_PATH,
|
|
475
494
|
/** HTTP 路由(默认 `/api/$events/result`)。 */
|
|
@@ -631,8 +650,27 @@ class EventSubscriber extends EventEmitter {
|
|
|
631
650
|
}
|
|
632
651
|
}
|
|
633
652
|
|
|
634
|
-
|
|
635
|
-
|
|
653
|
+
/**
|
|
654
|
+
* 当前上游地址(**每次调用都重新求值**,见构造函数里 upstream 可为函数的说明)。
|
|
655
|
+
*
|
|
656
|
+
* 求值失败(函数抛错 / 返回空 / 不是合法 URL)时回退到上一次成功用过的地址,
|
|
657
|
+
* 保证"取不到新地址"永远不会变成"打一个空 URL"。真的没得回退时返回空串,
|
|
658
|
+
* 由 `#connect()` 如实报错,而不是静默重连到垃圾地址。
|
|
659
|
+
*/
|
|
660
|
+
#upstream() {
|
|
661
|
+
let raw;
|
|
662
|
+
try {
|
|
663
|
+
raw = this.#opts.upstreamGetter();
|
|
664
|
+
} catch (error) {
|
|
665
|
+
this.#log("warn", "upstream getter threw", { error: String(error) });
|
|
666
|
+
return this.#lastGoodUpstream ?? "";
|
|
667
|
+
}
|
|
668
|
+
const value = typeof raw === "string" ? raw.replace(/\/+$/, "") : "";
|
|
669
|
+
return value || this.#lastGoodUpstream || "";
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
#muxUrl(upstream) {
|
|
673
|
+
const url = new URL(upstream);
|
|
636
674
|
url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
|
|
637
675
|
url.pathname = this.#opts.muxPath;
|
|
638
676
|
url.search = "";
|
|
@@ -643,16 +681,35 @@ class EventSubscriber extends EventEmitter {
|
|
|
643
681
|
#connect() {
|
|
644
682
|
if (this.#closed) return;
|
|
645
683
|
this.#state = "connecting";
|
|
684
|
+
const upstream = this.#upstream();
|
|
685
|
+
if (upstream === "") {
|
|
686
|
+
this.#state = "auth-failed";
|
|
687
|
+
this.#emitAuthError(0, "upstream 不可用(取不到宿主地址)", "ws");
|
|
688
|
+
return;
|
|
689
|
+
}
|
|
690
|
+
// 目标变了 = 宿主换了端口(Desktop 每次启动随机 / `dsh web` 重启到新端口)。
|
|
691
|
+
// 把退避重置,否则旧目标上累积的长退避会让"换了端口反而更慢连上"(最长要等满 maxMs)。
|
|
692
|
+
if (upstream !== this.#lastConnectUrl) {
|
|
693
|
+
if (this.#lastConnectUrl !== null) {
|
|
694
|
+
this.#log("info", `上游切换 ${this.#lastConnectUrl} → ${upstream},重连退避已重置`, {
|
|
695
|
+
from: this.#lastConnectUrl,
|
|
696
|
+
to: upstream,
|
|
697
|
+
});
|
|
698
|
+
}
|
|
699
|
+
this.#lastConnectUrl = upstream;
|
|
700
|
+
this.#attempt = 0;
|
|
701
|
+
}
|
|
702
|
+
this.#lastGoodUpstream = upstream;
|
|
646
703
|
let url;
|
|
647
704
|
try {
|
|
648
|
-
url = this.#muxUrl();
|
|
705
|
+
url = this.#muxUrl(upstream);
|
|
649
706
|
} catch (error) {
|
|
650
707
|
this.#state = "auth-failed";
|
|
651
708
|
this.#emitAuthError(0, `upstream 不是合法 URL: ${String(error)}`, "ws");
|
|
652
709
|
return;
|
|
653
710
|
}
|
|
654
711
|
// Host 显式写成上游 authority(loopback 围栏),Cookie 复用 bridge 已持有的浏览器会话。
|
|
655
|
-
const headers = { Host: new URL(
|
|
712
|
+
const headers = { Host: new URL(upstream).host };
|
|
656
713
|
const cookie = this.#cookieValue();
|
|
657
714
|
if (cookie !== "") headers.Cookie = cookie;
|
|
658
715
|
|
|
@@ -1411,7 +1468,8 @@ class EventSubscriber extends EventEmitter {
|
|
|
1411
1468
|
async #rpc(method, args, eventId) {
|
|
1412
1469
|
const rpcId = randomUUID();
|
|
1413
1470
|
const envelope = { type: "client-request", rpcId, method, payload: { args } };
|
|
1414
|
-
|
|
1471
|
+
// 回执/RPC 也走**当前**上游(端口变了就跟着走),不是一个启动时固化的字符串。
|
|
1472
|
+
const url = `${this.#upstream()}${API_CHANNEL}/${method}`;
|
|
1415
1473
|
|
|
1416
1474
|
const timeoutMs = Number(this.#opts.rpcTimeoutMs);
|
|
1417
1475
|
const budgeted = Number.isFinite(timeoutMs) && timeoutMs > 0;
|
|
@@ -1984,7 +2042,7 @@ export function classifyTurnEnd(sessionId, event, at) {
|
|
|
1984
2042
|
/**
|
|
1985
2043
|
* 创建一个订阅器。
|
|
1986
2044
|
* @param {{
|
|
1987
|
-
* upstream: string,
|
|
2045
|
+
* upstream: string|(() => string), // 函数 = 每次连接/每次 RPC 重新求值(宿主端口会变)
|
|
1988
2046
|
* cookie?: string|(() => string),
|
|
1989
2047
|
* muxPath?: string,
|
|
1990
2048
|
* resultPath?: string,
|
|
@@ -340,7 +340,7 @@ export class WeChatRuntime {
|
|
|
340
340
|
/**
|
|
341
341
|
* @param {{
|
|
342
342
|
* relayDir: string,
|
|
343
|
-
* upstream?: string,
|
|
343
|
+
* upstream?: string|(() => string), // dsh web 地址,默认 http://127.0.0.1:3080;函数=每次重连重新求值
|
|
344
344
|
* cookieOf?: () => string, // 读 .harness-cookie.json 的回调(bridge 已有该逻辑)
|
|
345
345
|
* secret?: string, // 控制面密钥(bridge_secret);空则控制面拒绝所有请求
|
|
346
346
|
* logger?: any,
|
|
@@ -356,6 +356,16 @@ export class WeChatRuntime {
|
|
|
356
356
|
if (!opts.relayDir) throw new Error("WeChatRuntime: 缺少 relayDir");
|
|
357
357
|
this.relayDir = opts.relayDir;
|
|
358
358
|
this.upstream = String(opts.upstream || "http://127.0.0.1:3080").replace(/\/+$/, "");
|
|
359
|
+
/**
|
|
360
|
+
* 上游地址的**取值器**:恰好是字符串时是常量函数。
|
|
361
|
+
*
|
|
362
|
+
* 为什么不让它是个固化字符串:宿主端口会变(`dsh web --port 0`、DSH Desktop 每次启动随机端口),
|
|
363
|
+
* 而事件订阅器是**长连接** —— 固化字符串的后果是端口一变就永久重连到旧端口。
|
|
364
|
+
* 【2026-10-09 真机实证】bridge 启动时宿主还是 3080,随后换成 19387;隧道转发侧
|
|
365
|
+
* (每次都读最新 UPSTREAM)一切正常,唯独微信事件通道死循环刷
|
|
366
|
+
* `connect ECONNREFUSED 127.0.0.1:3080` —— 手机能开页面,但**任务完成没有任何推送**。
|
|
367
|
+
*/
|
|
368
|
+
this.upstreamOf = typeof opts.upstream === "function" ? opts.upstream : () => this.upstream;
|
|
359
369
|
this.cookieOf = typeof opts.cookieOf === "function" ? opts.cookieOf : () => "";
|
|
360
370
|
this.secret = String(opts.secret || "");
|
|
361
371
|
this.clock = opts.clock || Date.now;
|
|
@@ -1597,7 +1607,8 @@ export class WeChatRuntime {
|
|
|
1597
1607
|
// 而且完全静默。现在无论订阅器是新建还是注入,都保证挂上处理器(WeakSet 去重,可重复调用)。
|
|
1598
1608
|
if (!this.subscriber) {
|
|
1599
1609
|
this.subscriber = createEventSubscriber({
|
|
1600
|
-
|
|
1610
|
+
// 传**取值器**而不是当次的值:见构造函数里 upstreamOf 的说明(端口变了要能跟上)。
|
|
1611
|
+
upstream: () => this.upstreamOf(),
|
|
1601
1612
|
cookie: () => this.cookieOf(),
|
|
1602
1613
|
follow: true,
|
|
1603
1614
|
log: (level, message, meta) => {
|