dsh-mobile-plugin 0.5.128 → 0.5.130
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/client.js +21 -2
- package/lib/impl.js +14 -13
- package/lib/panel/index.html +27 -27
- package/lib/relay/config-store.js +7 -0
- package/lib/relay/core.js +5 -0
- package/lib/relay/ingress-guard.cjs +26 -0
- package/lib/relay/lan-proxy.js +20 -6
- package/lib/relay/pairing-payload.cjs +4 -1
- package/lib/relay/status.js +2 -0
- package/lib/routes/control-plane.js +71 -31
- package/lib/update-install.cjs +124 -4
- package/lib/zeronews/runner.cjs +44 -1
- package/lib/zeronews/wiring.cjs +16 -1
- package/package.json +15 -15
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// 2026-11-27:零讯客户端身份两格进 schema(连上时落盘,复用本机客户端前比对,手机配置口不开放)。
|
|
1
2
|
// 2026-11-25:专属回环入口端口进 schema(worker 绑定后落盘、手机配置口不开放),兜底期间的手填地址备份同属运行时字段。
|
|
2
3
|
// 固定端口设置随远程连接配置一并保存和投影。
|
|
3
4
|
// 远程控制配置的 schema 与投影:装配层从这里取,不把实现搬回去(状态仓外壳仍在 impl.js)。
|
|
@@ -106,6 +107,10 @@ const RemoteControlSettingsSchema = z.object({
|
|
|
106
107
|
// ZeroNews 的账户 AuthToken 是否已经写进本机客户端配置(面板据此决定还要不要显示粘贴框)。
|
|
107
108
|
// 存的是「做过没有」,不是 token 本身:token 只经一次请求交给官方 CLI,插件不保存凭据。
|
|
108
109
|
zeroNewsBound: z.boolean().default(false),
|
|
110
|
+
// 上次连上的零讯客户端身份(client_id / 接入节点):运行时由 zeronews/wiring.cjs 写入,
|
|
111
|
+
// 手机配置口不开放。本机已有客户端在跑时用它比对,避免 attach 到另一个身份的客户端。
|
|
112
|
+
zeroNewsClientId: z.string().max(200).default(''),
|
|
113
|
+
zeroNewsNode: z.string().max(300).default(''),
|
|
109
114
|
// 节点小宝那条 TCP 线路的外网地址(`域名:端口`,空 = 没接)。用户在面板填一次,之后随通用线路
|
|
110
115
|
// 清单上报。手机配置口同样不开放:设备端和线路都在节点小宝那边,手机端只该看到一条线路。
|
|
111
116
|
jdxbUrl: z.string().max(300).default(''),
|
|
@@ -160,6 +165,8 @@ function configOf(state) {
|
|
|
160
165
|
zeroNewsEnabled: state.zeroNewsEnabled === true,
|
|
161
166
|
// 账户 token 写过没有:决定面板还显不显示粘贴框;不涉及凭据本身。
|
|
162
167
|
zeroNewsBound: state.zeroNewsBound === true,
|
|
168
|
+
zeroNewsClientId: typeof state.zeroNewsClientId === 'string' ? state.zeroNewsClientId : '',
|
|
169
|
+
zeroNewsNode: typeof state.zeroNewsNode === 'string' ? state.zeroNewsNode : '',
|
|
163
170
|
// FRP 隧道地址:运行时写入的独立线路,空 = 没在跑 / 没分到地址。
|
|
164
171
|
frpUrl: typeof state.frpUrl === 'string' ? state.frpUrl : '',
|
|
165
172
|
// 用户手填的节点小宝线路地址;同样按空串兜底(没接这条线路)。
|
package/lib/relay/core.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// 2026-11-27:零讯客户端身份两格进默认状态(连上时落盘,复用本机客户端前比对)。
|
|
1
2
|
// 2026-11-25:专属回环入口端口与兜底期手填地址备份进默认状态(运行时字段,手机配置口不开放)。
|
|
2
3
|
// 中转地址的废弃内置项在读盘与落盘时统一清理;配置收敛保持两格自洽。
|
|
3
4
|
// 通用线路引擎的运行时线路清单(customRoutes)随状态落盘,默认空:没接第三方线路时上报清单逐字不变。
|
|
@@ -102,6 +103,10 @@ function defaultState() {
|
|
|
102
103
|
zeroNewsEnabled: false,
|
|
103
104
|
// 账户 AuthToken 是否已经写进本机客户端配置:只记「做过没有」,不存 token。
|
|
104
105
|
zeroNewsBound: false,
|
|
106
|
+
// 上次连上的零讯客户端身份(client_id / 接入节点):运行时由 zeronews/wiring.cjs 落盘,
|
|
107
|
+
// 复用本机客户端时据此认人;不进 sidecar,手机配置口也不开放。
|
|
108
|
+
zeroNewsClientId: '',
|
|
109
|
+
zeroNewsNode: '',
|
|
105
110
|
// FRP 隧道此刻分到的公网地址(运行时由 frpc 发现后写入)。它是**独立一条线路**,
|
|
106
111
|
// 与用户手填的「TCP 直连」分开:那条归用户,这条归本机跑着的 frpc。
|
|
107
112
|
frpUrl: '',
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
// 2026-11-27:外部目标提示只在真绕开时给;配了 frp / 手填目标不等于端口被占。
|
|
2
|
+
// 2026-11-27:线路级问题码也搬进来(原先在装配层里占 9 行,把装配层顶过了行数守卫)。
|
|
1
3
|
// 2026-11-25:新增回环入口判据:端口有效性、漂移/遮蔽信号、三条线路落点与兜底原因码,只判据不碰进程。
|
|
2
4
|
'use strict';
|
|
3
5
|
|
|
@@ -78,6 +80,29 @@ function fallbackReasonOf(input = {}) {
|
|
|
78
80
|
return 'port-conflict';
|
|
79
81
|
}
|
|
80
82
|
|
|
83
|
+
/**
|
|
84
|
+
* 线路级问题码:兜底已经顶上时,也要让用户看见「这条线路自己落点不对」。
|
|
85
|
+
*
|
|
86
|
+
* 兜底原因码(fallbackReason)说「兜底也顶不上、要用户接着处理」;这里只在它没有值时补一条
|
|
87
|
+
* 线路自身的线索。两者分开是给面板和手机看的:旧 zeronews 隧道落在旧端口、外部软件手填了
|
|
88
|
+
* 目标、回环被抢又没拿到专属入口,各有各的改法,不能混成一句笼统的端口冲突。
|
|
89
|
+
*
|
|
90
|
+
* 判据全在这一处,装配层只把现状交进来。返回 `{ fallbackReason, routeIssue }` 两格,
|
|
91
|
+
* 省得调用方再算一遍兜底码。
|
|
92
|
+
*/
|
|
93
|
+
function routeIssueOf(input = {}) {
|
|
94
|
+
const fallbackReason = fallbackReasonOf(input);
|
|
95
|
+
if (fallbackReason) return { fallbackReason, routeIssue: fallbackReason };
|
|
96
|
+
const signals = input.signals || {};
|
|
97
|
+
const ingressPort = validPort(input.ingressPort);
|
|
98
|
+
let routeIssue = '';
|
|
99
|
+
if (signals.needsBypass === true && ingressPort > 0 && input.hasLegacyTunnel === true
|
|
100
|
+
&& validPort(input.legacyTunnelPort) !== ingressPort) routeIssue = 'zeronews-stale';
|
|
101
|
+
else if (signals.needsBypass === true && input.externalTarget === true) routeIssue = 'external-target';
|
|
102
|
+
else if (signals.conflict === 'shadowed' && ingressPort === 0) routeIssue = 'port-conflict';
|
|
103
|
+
return { fallbackReason, routeIssue };
|
|
104
|
+
}
|
|
105
|
+
|
|
81
106
|
const KNOWN_STATES = new Set(['ok', 'blocked', 'unknown']);
|
|
82
107
|
let noted = { state: '', reason: '', at: 0 };
|
|
83
108
|
|
|
@@ -106,6 +131,7 @@ module.exports = {
|
|
|
106
131
|
signalsOf,
|
|
107
132
|
tunnelPortOf,
|
|
108
133
|
fallbackReasonOf,
|
|
134
|
+
routeIssueOf,
|
|
109
135
|
noteLoopback,
|
|
110
136
|
snapshot,
|
|
111
137
|
};
|
package/lib/relay/lan-proxy.js
CHANGED
|
@@ -571,12 +571,26 @@ function createLanProxy(opts) {
|
|
|
571
571
|
const up = http.request(
|
|
572
572
|
{ hostname: upstreamHost, port: upstreamPort, method: 'POST', path: '/api/mobile/v1/pair', headers },
|
|
573
573
|
(ures) => {
|
|
574
|
-
const
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
574
|
+
const chunks = [];
|
|
575
|
+
ures.on('data', (chunk) => chunks.push(chunk));
|
|
576
|
+
ures.on('end', () => {
|
|
577
|
+
if (res.headersSent || res.writableEnded) return;
|
|
578
|
+
const raw = Buffer.concat(chunks);
|
|
579
|
+
// 宿主那个「浏览器访问被禁」的 403 是批准通道没准备好,不是电脑拒绝这台手机。
|
|
580
|
+
// 在这里就换成可区分的结果,别让它一路穿到手机、被读成「配对码不对或电脑拒绝」。
|
|
581
|
+
if (ures.statusCode === 403 && /Browser access is disabled/i.test(raw.toString('utf8'))) {
|
|
582
|
+
res.writeHead(503, { 'content-type': 'application/json; charset=utf-8' });
|
|
583
|
+
res.end(JSON.stringify({ ok: false, code: 'approval-unavailable', error: '配对通道暂时没准备好' }));
|
|
584
|
+
return;
|
|
585
|
+
}
|
|
586
|
+
const out = {};
|
|
587
|
+
for (const [k, v] of Object.entries(ures.headers)) {
|
|
588
|
+
if (k.toLowerCase() === 'content-length') continue;
|
|
589
|
+
if (!HOP_BY_HOP.has(k)) out[k] = v;
|
|
590
|
+
}
|
|
591
|
+
res.writeHead(ures.statusCode, out);
|
|
592
|
+
res.end(raw);
|
|
593
|
+
});
|
|
580
594
|
},
|
|
581
595
|
);
|
|
582
596
|
up.on('error', (error) => {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// 2026-11-27:进二维码的服务商线路名字截到 40 字——名字只是显示用,长名字能把码撑到扫不出来。
|
|
1
2
|
// 2026-11-24:服务商的 http(s) 地址同时进 lanUrls,让不认 routes 的老包也能扫码走服务商直连;
|
|
2
3
|
// 二维码只带电脑自己配的线路,新机没自己的线路时蜂窝首次配对失败属于预期。
|
|
3
4
|
// 2026-11-24:出码前的「有没有可用地址」收成 `pairingHasAddress`,服务商线路也算数——两个出码入口同用一处判据。
|
|
@@ -66,7 +67,9 @@ function pairingRoutes(state, taken) {
|
|
|
66
67
|
out.push({
|
|
67
68
|
url,
|
|
68
69
|
transport: row.transport === 'tcp' ? 'tcp' : 'http',
|
|
69
|
-
|
|
70
|
+
// 名字只是面板/手机上给人看的标签,不参与拨号;按二维码的字节预算截到 40,
|
|
71
|
+
// 八条线路各带 80 字长名字足以把整张码撑爆。完整名字仍在心跳与总控那份清单里。
|
|
72
|
+
name: typeof row.name === 'string' ? row.name.trim().slice(0, 40) : '',
|
|
70
73
|
});
|
|
71
74
|
};
|
|
72
75
|
for (const row of providerLinesOf(state)) push(row);
|
package/lib/relay/status.js
CHANGED
|
@@ -159,6 +159,8 @@ function createStatusView(deps) {
|
|
|
159
159
|
bypassing: ingressView.bypassing === true,
|
|
160
160
|
conflict: typeof ingressView.conflict === 'string' ? ingressView.conflict : '',
|
|
161
161
|
fallbackReason: typeof ingressView.fallbackReason === 'string' ? ingressView.fallbackReason : '',
|
|
162
|
+
// 线路级问题:兜底顶上时也可能有旧隧道落点没改,面板据此单独提示。
|
|
163
|
+
routeIssue: typeof ingressView.routeIssue === 'string' ? ingressView.routeIssue : '',
|
|
162
164
|
},
|
|
163
165
|
sidecar: {
|
|
164
166
|
state: getSidecarState(),
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
// 2026-11-27:面板中转检测与 APP 同判据——不跟跳、回包必须 ok 且服务名正确,别再给假绿灯。
|
|
2
|
+
// 2026-11-27:二维码回归一次性门票——隧道段只在唯一通路时进码,点对点只带身份不带候选清单。
|
|
1
3
|
// 2026-11-25:自检把回环判据 note 给 ingress-guard,巡检判定变了才触发总控重报;端口探针仍是「局域网能不能绑」。
|
|
2
4
|
// 2026-11-25:面板保存优先地址也过宿主那道地址分栏校验,存了不生效的地址当场拒并报同一句中文。
|
|
3
5
|
// 2026-11-25:自检注入回环 health 探针,端口被抢占时不再报 ok。
|
|
@@ -55,7 +57,7 @@ const { addressSlotProblem } = require('../relay/address-slot.cjs');
|
|
|
55
57
|
const { detectPublicIpv4 } = require('../relay/public-ip.cjs');
|
|
56
58
|
// 「手机连不上时哪一环断了」这份连接自检:本机只读、不改任何东西(口径见
|
|
57
59
|
// connection-check.js 与 firewall-probe.js 的文件头)。
|
|
58
|
-
const { createConnectionCheck } = require('../relay/connection-check.js');
|
|
60
|
+
const { createConnectionCheck, loopbackVerdictOf } = require('../relay/connection-check.js');
|
|
59
61
|
// 回环入口判据:自检只负责把回环 health 的结论记进来,落点 / 兜底原因由状态视图同一份判据读。
|
|
60
62
|
const ingressGuard = require('../relay/ingress-guard.cjs');
|
|
61
63
|
const readReceipt = require('../read-receipt.cjs');
|
|
@@ -68,7 +70,7 @@ const { createSelfCheck } = require('../mobile/self-check.cjs');
|
|
|
68
70
|
// 「查有没有新版」与「装新版」这两格都归插件自己:查直接问 npm 注册表,装在 profile 里跑
|
|
69
71
|
// 它自己的包管理器(见 `../update-registry.cjs` / `../update-install.cjs`)。不再借道市场。
|
|
70
72
|
const { checkForUpdate } = require('../update-registry.cjs');
|
|
71
|
-
const { declaredSource, profileRegistry } = require('../update-install.cjs');
|
|
73
|
+
const { declaredSource, profileRegistry, readUpdateLog, UPDATE_LOG_KEEP_DAYS } = require('../update-install.cjs');
|
|
72
74
|
const { clearCompletionForAllDevices, invalidateSessionList } = require('../mobile/session-cache.js');
|
|
73
75
|
const { recordSessionRead } = require('../mobile/session-last-turn.js');
|
|
74
76
|
// 收件目录的占用与清理:和移动端那条路由是同一份实现(`lib/mobile/inbox-clean.js`)。
|
|
@@ -403,6 +405,25 @@ function registerControlPlaneRoutes(deps) {
|
|
|
403
405
|
});
|
|
404
406
|
// 巡检只认判据的变化:账没变就不重复让总控重报,避免面板三秒一轮把上报刷爆。
|
|
405
407
|
let lastIngressVerdictKey = null;
|
|
408
|
+
/**
|
|
409
|
+
* 把一次回环 health 结论记进入口判据;只有判据变了才让总控重报。
|
|
410
|
+
* 自检和轻量回环探针共用这一份,免得两处各记一套、面板与状态互相矛盾。
|
|
411
|
+
*/
|
|
412
|
+
const applyLoopbackVerdict = (verdict) => {
|
|
413
|
+
if (typeof noteLoopbackVerdict === 'function') noteLoopbackVerdict(verdict || null)
|
|
414
|
+
const signals = ingressGuard.signalsOf({
|
|
415
|
+
requestedPort: state.lanPort,
|
|
416
|
+
boundPort: typeof lanPortInUse === 'function' ? lanPortInUse() : 0,
|
|
417
|
+
drifted: typeof getLanPortAdjusted === 'function' ? getLanPortAdjusted() : undefined,
|
|
418
|
+
loopbackBlocked: ingressGuard.snapshot().state === 'blocked',
|
|
419
|
+
})
|
|
420
|
+
const key = JSON.stringify(signals)
|
|
421
|
+
if (key === lastIngressVerdictKey) return
|
|
422
|
+
lastIngressVerdictKey = key
|
|
423
|
+
if (typeof onIngressVerdictChanged === 'function') {
|
|
424
|
+
try { onIngressVerdictChanged() } catch { /* 重报失败不拖垮本次回包 */ }
|
|
425
|
+
}
|
|
426
|
+
}
|
|
406
427
|
const localOnly = baseLocalOnly;
|
|
407
428
|
|
|
408
429
|
registerRoute({ kind: 'exact', path: '/api/relay/feedback', handler: localOnly(['GET', 'POST'], (req, res) =>
|
|
@@ -549,8 +570,9 @@ function registerControlPlaneRoutes(deps) {
|
|
|
549
570
|
* 中转候选的测速(`POST /api/relay/probe-relays`,body `{ urls: string[] }`)。
|
|
550
571
|
*
|
|
551
572
|
* 必须由宿主代测:引导页在浏览器里,直连那些域名会被 CORS 挡住,读到的是浏览器拦截而不是
|
|
552
|
-
*
|
|
553
|
-
* `dsh-mobile-relay`——只看 `/healthz`
|
|
573
|
+
* 真实耗时。判定与 APP 拨中转同一套判据:`/healthz` 回 2xx 且 `/api/mobile/v1/health`
|
|
574
|
+
* 回的 `ok` 为真、`service` 是 `dsh-mobile-relay`——只看 `/healthz` 的话任何静态站都会被
|
|
575
|
+
* 当成可用中转,跟跳转更会把反代跳过去的页面算成通。超时照实报 null。
|
|
554
576
|
*/
|
|
555
577
|
registerRoute({ kind: 'exact', path: '/api/relay/probe-relays', handler: localOnly('POST', async (req, res) => {
|
|
556
578
|
const body = await readJson(req)
|
|
@@ -565,22 +587,23 @@ function registerControlPlaneRoutes(deps) {
|
|
|
565
587
|
const controller = new AbortController()
|
|
566
588
|
const timer = setTimeout(() => controller.abort(), RELAY_PROBE_TIMEOUT_MS)
|
|
567
589
|
try {
|
|
568
|
-
const response = await fetch(`${url}/healthz`, { signal: controller.signal, redirect: '
|
|
590
|
+
const response = await fetch(`${url}/healthz`, { signal: controller.signal, redirect: 'manual' })
|
|
569
591
|
// 读完响应体再停表:只等到响应头会把「连得上但发不出东西」算成很快。
|
|
570
592
|
const text = await response.text().catch(() => '')
|
|
571
593
|
if (!response.ok) return { url, ms: null }
|
|
572
594
|
// **只认运维意义上「这一台确实是中转」**:`/healthz` 回 200 的站点满互联网都是,
|
|
573
595
|
// 光看它等于把随便一个域名也当可用中转。
|
|
574
|
-
// 中转自己那条地址体检 `/api/mobile/v1/health` 会带回 `service
|
|
575
|
-
|
|
596
|
+
// 中转自己那条地址体检 `/api/mobile/v1/health` 会带回 `ok` 与 `service`;跟跳会把
|
|
597
|
+
// 反代跳过去的页面当成中转,手机那侧一律不跟跳,这里也别跟。
|
|
598
|
+
const who = await fetch(`${url}/api/mobile/v1/health`, { signal: controller.signal, redirect: 'manual' })
|
|
576
599
|
.then((r) => (r.ok ? r.json() : null))
|
|
577
600
|
.catch(() => null)
|
|
578
601
|
const elapsed = Date.now() - started
|
|
579
|
-
//
|
|
580
|
-
if (who && who.service === 'dsh-mobile-relay') return { url, ms: elapsed, service: 'dsh-mobile-relay', version: who.version }
|
|
602
|
+
// 手机拨中转要求回包 ok 为真且服务名对得上,面板照同一条判据,别再给假绿灯。
|
|
603
|
+
if (who && who.ok === true && who.service === 'dsh-mobile-relay') return { url, ms: elapsed, service: 'dsh-mobile-relay', version: who.version }
|
|
581
604
|
// 直连地址打回的是**宿主自己**:回 `service=dsh-mobile-plugin` + `instanceId`。带上实例号,
|
|
582
605
|
// 面板据此确认「这个地址绕回来就是本机」(回环检测),而不是别的机器或别的服务。
|
|
583
|
-
if (who && who.service === 'dsh-mobile-plugin') return { url, ms: elapsed, service: 'dsh-mobile-plugin', instanceId: who.instanceId || '', apiVersion: who.apiVersion }
|
|
606
|
+
if (who && who.ok === true && who.service === 'dsh-mobile-plugin') return { url, ms: elapsed, service: 'dsh-mobile-plugin', instanceId: who.instanceId || '', apiVersion: who.apiVersion }
|
|
584
607
|
return { url, ms: null }
|
|
585
608
|
} catch {
|
|
586
609
|
return { url, ms: null }
|
|
@@ -1127,7 +1150,10 @@ function registerControlPlaneRoutes(deps) {
|
|
|
1127
1150
|
}
|
|
1128
1151
|
const source = previewUrl ? { ...state, relayUrl: previewUrl, relayUrls: [previewUrl] } : state
|
|
1129
1152
|
const { payload } = pairingPayload(source, lanPortInUse())
|
|
1130
|
-
|
|
1153
|
+
// 隧道这一整段又长又敏感(地址、钥匙、证明),而它是**配好之后**才需要的数据通道:
|
|
1154
|
+
// 手机配完凭已经通着的那条连接报名就能换到入口地址。所以只有它本身就是唯一通路
|
|
1155
|
+
// (没中转、没局域网/服务商线路)时才写进码里;有别的路可走就不重复投递。
|
|
1156
|
+
if (tailcat && !pairingHasAddress(payload)) {
|
|
1131
1157
|
payload.tailcat = {
|
|
1132
1158
|
version: 1,
|
|
1133
1159
|
address: tailcat.address,
|
|
@@ -1137,8 +1163,10 @@ function registerControlPlaneRoutes(deps) {
|
|
|
1137
1163
|
expiresAt: tailcat.expiresAt,
|
|
1138
1164
|
}
|
|
1139
1165
|
}
|
|
1140
|
-
//
|
|
1141
|
-
//
|
|
1166
|
+
// 点对点隧道:二维码只带**身份**(电脑公钥 + 端口),不带打洞候选清单。两端各持一半
|
|
1167
|
+
// 密钥,之后每次起来都用同一对,不需要窗口、也不需要临时钥匙 —— 与上面那条一次性
|
|
1168
|
+
// 节点不是一回事。候选是手机配好之后由会合接口回给它的(那边那份才是打洞的起点),
|
|
1169
|
+
// 写进码里只是重复投递:十几条带方括号 IPv6 的地址能把二维码直接撑爆。
|
|
1142
1170
|
if (deps.p2p && typeof deps.p2p.exchange === 'function') {
|
|
1143
1171
|
const exchanged = deps.p2p.exchange(null)
|
|
1144
1172
|
if (exchanged && exchanged.ok) {
|
|
@@ -1146,8 +1174,6 @@ function registerControlPlaneRoutes(deps) {
|
|
|
1146
1174
|
version: 1,
|
|
1147
1175
|
peerKey: exchanged.publicKey,
|
|
1148
1176
|
port: exchanged.port,
|
|
1149
|
-
candidates: Array.isArray(exchanged.candidates) ? exchanged.candidates : [],
|
|
1150
|
-
localCandidates: Array.isArray(exchanged.localCandidates) ? exchanged.localCandidates : [],
|
|
1151
1177
|
}
|
|
1152
1178
|
}
|
|
1153
1179
|
}
|
|
@@ -1415,6 +1441,27 @@ function registerControlPlaneRoutes(deps) {
|
|
|
1415
1441
|
}
|
|
1416
1442
|
sendJson(res, 200, { ok: true, port, available: await canListen(port, '0.0.0.0'), ours: false, listening, currentPort })
|
|
1417
1443
|
}) })
|
|
1444
|
+
/**
|
|
1445
|
+
* 轻量回环实测:只拨本机 health,不跑防火墙,供面板按分钟级节流轮询。
|
|
1446
|
+
*
|
|
1447
|
+
* 与自检共用同一份回环判据和入口账;直连没在听就报 unknown,不拿“没测到”当“被抢”。
|
|
1448
|
+
*/
|
|
1449
|
+
registerRoute({ kind: 'exact', path: '/api/relay/loopback', handler: localOnly('GET', async (_req, res) => {
|
|
1450
|
+
const snapshot = status() || {}
|
|
1451
|
+
const lan = snapshot.lan && typeof snapshot.lan === 'object' ? snapshot.lan : {}
|
|
1452
|
+
const port = Number.isInteger(lan.port) && lan.port > 0 ? lan.port : 0
|
|
1453
|
+
if (!port || lan.state !== 'listening') {
|
|
1454
|
+
return sendJson(res, 200, { ok: true, at: Date.now(), port, state: 'unknown', reason: '直连端口没在听,回环没法实测' })
|
|
1455
|
+
}
|
|
1456
|
+
let verdict
|
|
1457
|
+
try {
|
|
1458
|
+
verdict = loopbackVerdictOf(await probeTcpHealth('127.0.0.1', port, LOOPBACK_PROBE_TIMEOUT_MS))
|
|
1459
|
+
} catch {
|
|
1460
|
+
verdict = { state: 'unknown', reason: '回环 health 探测失败,无法判断端口是否被抢占' }
|
|
1461
|
+
}
|
|
1462
|
+
applyLoopbackVerdict(verdict)
|
|
1463
|
+
return sendJson(res, 200, { ok: true, at: Date.now(), port, ...verdict })
|
|
1464
|
+
}) })
|
|
1418
1465
|
/**
|
|
1419
1466
|
* 连接自检:`GET /api/relay/selfcheck`(只读)。
|
|
1420
1467
|
*
|
|
@@ -1428,22 +1475,7 @@ function registerControlPlaneRoutes(deps) {
|
|
|
1428
1475
|
registerRoute({ kind: 'exact', path: '/api/relay/selfcheck', handler: localOnly('GET', async (req, res) => {
|
|
1429
1476
|
const force = /[?&]force=1\b/.test(String(req?.url || ''))
|
|
1430
1477
|
const result = await connectionCheck.run({ force })
|
|
1431
|
-
|
|
1432
|
-
// 不会在别处各判一遍;探测失败(loopback=null)就清掉证据,不误判成被抢。
|
|
1433
|
-
if (typeof noteLoopbackVerdict === 'function') noteLoopbackVerdict(result?.listening?.loopback || null)
|
|
1434
|
-
const signals = ingressGuard.signalsOf({
|
|
1435
|
-
requestedPort: state.lanPort,
|
|
1436
|
-
boundPort: typeof lanPortInUse === 'function' ? lanPortInUse() : 0,
|
|
1437
|
-
drifted: typeof getLanPortAdjusted === 'function' ? getLanPortAdjusted() : undefined,
|
|
1438
|
-
loopbackBlocked: ingressGuard.snapshot().state === 'blocked',
|
|
1439
|
-
})
|
|
1440
|
-
const key = JSON.stringify(signals)
|
|
1441
|
-
if (key !== lastIngressVerdictKey) {
|
|
1442
|
-
lastIngressVerdictKey = key
|
|
1443
|
-
if (typeof onIngressVerdictChanged === 'function') {
|
|
1444
|
-
try { onIngressVerdictChanged() } catch { /* 重报失败不拖垮自检回包 */ }
|
|
1445
|
-
}
|
|
1446
|
-
}
|
|
1478
|
+
applyLoopbackVerdict(result?.listening?.loopback || null)
|
|
1447
1479
|
sendJson(res, 200, result)
|
|
1448
1480
|
}) })
|
|
1449
1481
|
registerRoute({ kind: 'exact', path: '/api/relay/reconnect', handler: localOnly('POST', async (_req, res) => {
|
|
@@ -1587,6 +1619,11 @@ function registerControlPlaneRoutes(deps) {
|
|
|
1587
1619
|
}
|
|
1588
1620
|
return sendJson(res, 200, { ok: outcome.installed || outcome.skipped, data: outcome })
|
|
1589
1621
|
}) })
|
|
1622
|
+
// 更新日志:把装包模块落在 <DSH_HOME>/logs 的那几行原样读回给面板查看/复制。只读本机文件,
|
|
1623
|
+
// 内容已由写入端限长、只记事实;老版本没有这个模块时回空串,面板照常显示「暂无日志」。
|
|
1624
|
+
registerRoute({ kind: 'exact', path: '/api/relay/update/log', handler: localOnly('GET', async (_req, res) => {
|
|
1625
|
+
return sendJson(res, 200, { ok: true, data: { text: readUpdateLog(), keepDays: UPDATE_LOG_KEEP_DAYS } });
|
|
1626
|
+
}) })
|
|
1590
1627
|
// 自动更新开关(默认关)。开=外壳每 2 分钟问一次有没有新版,有就自己装并就地切换
|
|
1591
1628
|
// (轮询住在外壳里,因为「没人看着也要发生」;装与换代用的就是上面这两条同一套实现)。
|
|
1592
1629
|
registerRoute({ kind: 'exact', path: '/api/relay/update/auto', handler: localOnly('POST', async (req, res) => {
|
|
@@ -1702,6 +1739,8 @@ const WEB_ACCESS_ROUTES = new Set([
|
|
|
1702
1739
|
'/api/relay/probe-relays',
|
|
1703
1740
|
'/api/relay/probe-direct',
|
|
1704
1741
|
'/api/relay/status',
|
|
1742
|
+
// 面板按分钟级读的轻量回环实测:与状态同一档只读数据,只在本机面板里读。
|
|
1743
|
+
'/api/relay/loopback',
|
|
1705
1744
|
// 面板流速图:只读的上下行累计与每秒快照,跟状态同档、只在本机面板里读。
|
|
1706
1745
|
'/api/relay/traffic',
|
|
1707
1746
|
'/api/relay/panel',
|
|
@@ -1763,6 +1802,7 @@ const WEB_ACCESS_ROUTES = new Set([
|
|
|
1763
1802
|
'/api/relay/update/check',
|
|
1764
1803
|
'/api/relay/update/apply',
|
|
1765
1804
|
'/api/relay/update/auto',
|
|
1805
|
+
'/api/relay/update/log',
|
|
1766
1806
|
'/api/relay/update/ota/status',
|
|
1767
1807
|
'/api/relay/update/ota/check',
|
|
1768
1808
|
'/api/relay/update/ota/apply',
|
package/lib/update-install.cjs
CHANGED
|
@@ -17,6 +17,11 @@
|
|
|
17
17
|
* 把嵌套目录删掉、改成顶层 hoist,同一个 package.json 的查找路径没变,缓存仍返回已删的嵌套路径,
|
|
18
18
|
* `accessSync` 抛 ENOENT,一次正常升级就被判成安装不完整并回退。这是进程级缓存,不重启 DSH
|
|
19
19
|
* 会每轮更新复现,所以每次自检解析依赖前清空。
|
|
20
|
+
*
|
|
21
|
+
* 自动更新同一版本真装失败后不再每两分钟重装回滚:失败账挂在进程上(见 AUTO_FAILED_KEY),
|
|
22
|
+
* 面板手动点更新与下一版发布都不受影响;默认仍自动更新。
|
|
23
|
+
* 更新每一步与最终结果落在 <DSH_HOME>/logs/mobile-plugin-update-<日期>.log(保留 7 天),
|
|
24
|
+
* 供面板「查看更新日志」读取;写日志失败不影响更新本身。
|
|
20
25
|
*/
|
|
21
26
|
|
|
22
27
|
const fs = require('node:fs');
|
|
@@ -43,6 +48,75 @@ const OUTPUT_TAIL = 4000;
|
|
|
43
48
|
/** 等 profile 写锁的上限:DSH 的插件管理自己最长等 120 秒,这里不跟它比耐心,超时就让出这一轮。 */
|
|
44
49
|
const PROFILE_LOCK_WAIT_MS = 30 * 1000;
|
|
45
50
|
|
|
51
|
+
/**
|
|
52
|
+
* 自动更新失败过的版本账(进程级)。
|
|
53
|
+
*
|
|
54
|
+
* 外壳每一拍都带 `?v=Date.now()` 重新 import 盘上这一代(见 shell/updater.js 的
|
|
55
|
+
* installedInstaller),模块级变量活不过一拍,所以这份账挂在 globalThis 上;重启 DSH 清零,
|
|
56
|
+
* 与外壳 swapAttempts 的口径一致。只记「真装了、而且失败」的那一版:等锁 / 有对话在跑
|
|
57
|
+
* 这类「现在不行」是可重试,不能记成这版坏了,否则新装的 pnpm、刚松开的锁都要重启才恢复。
|
|
58
|
+
*/
|
|
59
|
+
const AUTO_FAILED_KEY = Symbol.for('dsh-mobile-plugin.autoFailedVersions');
|
|
60
|
+
function autoFailedVersions() {
|
|
61
|
+
if (!globalThis[AUTO_FAILED_KEY]) globalThis[AUTO_FAILED_KEY] = new Set();
|
|
62
|
+
return globalThis[AUTO_FAILED_KEY];
|
|
63
|
+
}
|
|
64
|
+
function autoFailKey(profileNodeModules, version) {
|
|
65
|
+
return `${profileNodeModules}\n${version}`;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* 更新过程日志:每一步与最终结果都落一行到 `<DSH_HOME>/logs/mobile-plugin-update-<日期>.log`,
|
|
70
|
+
* 按天一个文件、保留最近 7 天。写失败绝不影响更新本身;日志里只记事实(版本、命令、失败原因),
|
|
71
|
+
* 不记令牌。路径按调用时的 env 解析(测试可注入 DSH_HOME),不 require 外壳的 shared/paths。
|
|
72
|
+
*/
|
|
73
|
+
const UPDATE_LOG_PREFIX = 'mobile-plugin-update-';
|
|
74
|
+
const UPDATE_LOG_KEEP_DAYS = 7;
|
|
75
|
+
function updateLogDir(env = process.env) {
|
|
76
|
+
return path.join(env.DSH_HOME || path.join(os.homedir(), '.dsh'), 'logs');
|
|
77
|
+
}
|
|
78
|
+
function updateLogDay(now) {
|
|
79
|
+
const d = new Date(now);
|
|
80
|
+
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
|
|
81
|
+
}
|
|
82
|
+
function updateLogPath(now, env = process.env) {
|
|
83
|
+
return path.join(updateLogDir(env), `${UPDATE_LOG_PREFIX}${updateLogDay(now)}.log`);
|
|
84
|
+
}
|
|
85
|
+
function appendUpdateLog(event, { env = process.env, now = Date.now() } = {}) {
|
|
86
|
+
try {
|
|
87
|
+
const file = updateLogPath(now, env);
|
|
88
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
89
|
+
const fields = Object.entries(event)
|
|
90
|
+
.map(([k, v]) => `${k}=${String(v ?? '').replace(/\s+/g, ' ').slice(0, 800)}`)
|
|
91
|
+
.join(' ');
|
|
92
|
+
fs.appendFileSync(file, `${new Date(now).toISOString()} ${fields}\n`);
|
|
93
|
+
// 轮转:只清我们自己这个前缀、且早于保留期的文件,不碰 logs 目录里别人的东西。
|
|
94
|
+
const cutoff = updateLogDay(now - UPDATE_LOG_KEEP_DAYS * 24 * 60 * 60 * 1000);
|
|
95
|
+
for (const name of fs.readdirSync(updateLogDir(env))) {
|
|
96
|
+
if (!name.startsWith(UPDATE_LOG_PREFIX) || !name.endsWith('.log')) continue;
|
|
97
|
+
const day = name.slice(UPDATE_LOG_PREFIX.length, -4);
|
|
98
|
+
if (day < cutoff) { try { fs.rmSync(path.join(updateLogDir(env), name)); } catch { /* 清不掉就算了 */ } }
|
|
99
|
+
}
|
|
100
|
+
} catch { /* 日志失败不影响更新 */ }
|
|
101
|
+
}
|
|
102
|
+
/** 读最近保留期内的更新日志(旧到新拼起来),给面板「查看更新日志」用。 */
|
|
103
|
+
function readUpdateLog({ env = process.env, now = Date.now() } = {}) {
|
|
104
|
+
const dir = updateLogDir(env);
|
|
105
|
+
let names;
|
|
106
|
+
try { names = fs.readdirSync(dir); } catch { return ''; }
|
|
107
|
+
const cutoff = updateLogDay(now - UPDATE_LOG_KEEP_DAYS * 24 * 60 * 60 * 1000);
|
|
108
|
+
const days = names
|
|
109
|
+
.filter(name => name.startsWith(UPDATE_LOG_PREFIX) && name.endsWith('.log'))
|
|
110
|
+
.map(name => name.slice(UPDATE_LOG_PREFIX.length, -4))
|
|
111
|
+
.filter(day => /^\d{4}-\d{2}-\d{2}$/.test(day) && day >= cutoff)
|
|
112
|
+
.sort();
|
|
113
|
+
const out = [];
|
|
114
|
+
for (const day of days) {
|
|
115
|
+
try { out.push(fs.readFileSync(path.join(dir, `${UPDATE_LOG_PREFIX}${day}.log`), 'utf8').trimEnd()); } catch { /* 跳过读不到的 */ }
|
|
116
|
+
}
|
|
117
|
+
return out.filter(Boolean).join('\n');
|
|
118
|
+
}
|
|
119
|
+
|
|
46
120
|
/** 锁记录里那个 `pid\n` 进程还在不在;认不出来的一律当「活着」,不抢。 */
|
|
47
121
|
function holderProcessExited(record) {
|
|
48
122
|
if (!/^\d+\n$/.test(record)) return false;
|
|
@@ -715,7 +789,14 @@ async function installLatest({
|
|
|
715
789
|
packageManager = null,
|
|
716
790
|
beforeInstall = null,
|
|
717
791
|
onEvent = () => {},
|
|
792
|
+
// 等 profile 写锁的上限;只给守卫注入短等待用,生产走默认。
|
|
793
|
+
lockWaitMs = PROFILE_LOCK_WAIT_MS,
|
|
718
794
|
} = {}) {
|
|
795
|
+
// 每一步事件落盘(成功也记,出事时能看到走到哪一步);调用方传的 onEvent 照旧收到。
|
|
796
|
+
const emit = event => {
|
|
797
|
+
appendUpdateLog(event, { env });
|
|
798
|
+
onEvent(event);
|
|
799
|
+
};
|
|
719
800
|
const beforeVersion = installedVersion(profileNodeModules, name);
|
|
720
801
|
const profileDir = path.dirname(profileNodeModules);
|
|
721
802
|
const searchDirs = dirs ?? toolSearchDirs({ platform, env });
|
|
@@ -756,7 +837,7 @@ async function installLatest({
|
|
|
756
837
|
if (probe.ok) {
|
|
757
838
|
return { command: hostInvocation.command, prefix: hostInvocation.args, manager: 'pnpm', kind: 'host', env: hostInvocation.env };
|
|
758
839
|
}
|
|
759
|
-
|
|
840
|
+
emit({ phase: 'host-manager-unusable', output: probe.output });
|
|
760
841
|
}
|
|
761
842
|
const pnpm = resolveExecutable('pnpm', searchDirs, { platform, env });
|
|
762
843
|
if (pnpm) return { command: pnpm, prefix: [], manager: 'pnpm', kind: 'path', env: {} };
|
|
@@ -795,7 +876,7 @@ async function installLatest({
|
|
|
795
876
|
: ['install', `${name}@${target}`, '--no-audit', '--no-fund'];
|
|
796
877
|
const args = installArgs(version);
|
|
797
878
|
const command = [picked.manager, ...args].join(' ');
|
|
798
|
-
|
|
879
|
+
emit({ phase: 'install-started', command, manager: picked.kind });
|
|
799
880
|
|
|
800
881
|
const runManager = (extraArgs, baseArgs = args) => run(picked.command, [...picked.prefix, ...baseArgs, ...extraArgs], {
|
|
801
882
|
cwd: profileDir,
|
|
@@ -848,7 +929,7 @@ async function installLatest({
|
|
|
848
929
|
let rollback = null;
|
|
849
930
|
// 只有更新破坏了原本完整的安装才重新装旧版;依赖和锁文件都交给同一个包管理器恢复。
|
|
850
931
|
if (previousComplete && installationProblem(profileNodeModules, name, beforeVersion)) {
|
|
851
|
-
|
|
932
|
+
emit({ phase: 'rollback-started', version: beforeVersion });
|
|
852
933
|
const restored = await runManager(['--force'], installArgs(beforeVersion));
|
|
853
934
|
const restoreProblem = installationProblem(profileNodeModules, name, beforeVersion);
|
|
854
935
|
after = installedVersion(profileNodeModules, name);
|
|
@@ -906,7 +987,7 @@ async function installLatest({
|
|
|
906
987
|
};
|
|
907
988
|
|
|
908
989
|
try {
|
|
909
|
-
return await withProfileLock(profileDir, performInstall);
|
|
990
|
+
return await withProfileLock(profileDir, performInstall, { waitMs: lockWaitMs });
|
|
910
991
|
} catch (error) {
|
|
911
992
|
if (error && error.code === 'PROFILE_BUSY') {
|
|
912
993
|
// 「现在不行」,不是坏了:DSH 的插件管理正握着锁在装/卸别的插件。一行都不写盘,下个 tick 再来。
|
|
@@ -1041,7 +1122,42 @@ async function installLatestIfNeeded(options = {}) {
|
|
|
1041
1122
|
};
|
|
1042
1123
|
}
|
|
1043
1124
|
}
|
|
1125
|
+
// 自动那一轮:同一版本在本进程已经真装失败过,就不再每两分钟重装再回滚一次。
|
|
1126
|
+
// 回滚把盘上版本退回旧版、注册表还是这一版,于是每次 check 又说有新版 —— 死循环,还全程
|
|
1127
|
+
// 握着 profile 写锁,让插件市场的每次写入都在 2 秒后超时。手动点更新(force 或面板那颗普通
|
|
1128
|
+
// 按钮)都不在这里被挡,用户随时能自己再试;下一版发布时 key 变了,也照常装。
|
|
1129
|
+
if (auto && !force && autoFailedVersions().has(autoFailKey(profileNodeModules, check.latestVersion))) {
|
|
1130
|
+
appendUpdateLog({ phase: 'auto-deferred', version: check.latestVersion, auto: true }, { env: options.env });
|
|
1131
|
+
return {
|
|
1132
|
+
installed: false,
|
|
1133
|
+
skipped: true,
|
|
1134
|
+
retryable: false,
|
|
1135
|
+
beforeVersion,
|
|
1136
|
+
installedVersion: beforeVersion,
|
|
1137
|
+
source: check.source,
|
|
1138
|
+
command: null,
|
|
1139
|
+
output: '',
|
|
1140
|
+
check,
|
|
1141
|
+
failure: {
|
|
1142
|
+
code: 'AUTO_VERSION_DEFERRED',
|
|
1143
|
+
message: `自动安装 ${check.latestVersion} 已经失败过,本轮不再重装;在面板点一次更新可手动重试,或等下一版。`,
|
|
1144
|
+
},
|
|
1145
|
+
};
|
|
1146
|
+
}
|
|
1044
1147
|
const result = await installLatest({ ...options, version: check.latestVersion });
|
|
1148
|
+
// 结果单独记一行:失败码与原因是这次报告最缺的东西(phase 事件只进控制台,出事时手里没证据)。
|
|
1149
|
+
appendUpdateLog({
|
|
1150
|
+
phase: 'result', auto: auto === true, force: force === true,
|
|
1151
|
+
version: check.latestVersion, before: beforeVersion, after: result.installedVersion,
|
|
1152
|
+
installed: result.installed === true, skipped: result.skipped === true,
|
|
1153
|
+
failure: result.failure?.code ?? '', reason: result.failure?.message ?? '',
|
|
1154
|
+
rollback: result.rollback ? `${result.rollback.toVersion}:${result.rollback.ok}` : '',
|
|
1155
|
+
}, { env: options.env });
|
|
1156
|
+
// 只记「真跑了包管理器、真没装上、也不是『现在不行』」的失败。等锁、有对话在跑都没动盘,
|
|
1157
|
+
// 下个 tick 照常再试,不能记成这版坏了 —— 否则装个 pnpm、锁一松开都得重启 DSH 才恢复。
|
|
1158
|
+
if (auto && !force && !result.skipped && result.retryable !== true && result.command && !result.installed) {
|
|
1159
|
+
autoFailedVersions().add(autoFailKey(profileNodeModules, check.latestVersion));
|
|
1160
|
+
}
|
|
1045
1161
|
return { ...result, check };
|
|
1046
1162
|
}
|
|
1047
1163
|
|
|
@@ -1067,4 +1183,8 @@ module.exports = {
|
|
|
1067
1183
|
OUTPUT_TAIL,
|
|
1068
1184
|
// profile 写锁单测直接打它:验「拿不到锁就让路、拿到就串行」,比隔着整条安装链路更快更稳。
|
|
1069
1185
|
withProfileLock,
|
|
1186
|
+
// 更新日志:装包时落盘、面板路由读出来展示。
|
|
1187
|
+
appendUpdateLog,
|
|
1188
|
+
readUpdateLog,
|
|
1189
|
+
UPDATE_LOG_KEEP_DAYS,
|
|
1070
1190
|
};
|