dsh-mobile-plugin 0.5.74 → 0.5.76
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 +39 -89
- package/docs/extension-authoring.md +32 -3
- package/icon.png +0 -0
- package/lib/impl.js +10 -2
- package/lib/mobile/pairing.js +12 -1
- package/lib/panel/index.html +13 -13
- package/lib/relay/core.js +58 -6
- package/lib/relay/relay-probe.js +86 -0
- package/lib/routes/mobile-api.js +2 -0
- package/lib/sidecar-facade-worker.cjs +79 -16
- package/package.json +1 -1
package/lib/relay/core.js
CHANGED
|
@@ -93,7 +93,10 @@ function loadState() {
|
|
|
93
93
|
// **老文件里没有这一格**:默认值那个空数组会顶上来,把「只有 relayUrl 一条」判成
|
|
94
94
|
// 「明确没有中转」,地址当场被清空。所以先把这一格撤掉,交给收敛函数从 relayUrl 生出来。
|
|
95
95
|
if (!Object.prototype.hasOwnProperty.call(raw, 'relayUrls')) delete merged.relayUrls;
|
|
96
|
-
|
|
96
|
+
const state = normalizeRelayAddresses(merged);
|
|
97
|
+
// 老设备补齐:早先只写了第一台内置公用中转的,读盘时就补成整份(见 expandDefaultPublicRelays)。
|
|
98
|
+
expandDefaultPublicRelays(state);
|
|
99
|
+
return state;
|
|
97
100
|
} catch {
|
|
98
101
|
const state = defaultState();
|
|
99
102
|
saveState(state);
|
|
@@ -147,12 +150,21 @@ function ensureHistoryKey(state) {
|
|
|
147
150
|
function normalizeRelayUrl(relayUrl) {
|
|
148
151
|
return String(relayUrl || '').trim().replace(/\/+$/, '');
|
|
149
152
|
}
|
|
153
|
+
/**
|
|
154
|
+
* 一台电脑最多认多少条中转地址。**这是安全上限,不是功能上限**:正常场景(内置公用 3 条、
|
|
155
|
+
* 按地区下发的就近短清单 5~8 条、自建几条)远低于它。挡的是「有人往配置/落盘里塞进成千上万条」
|
|
156
|
+
* ——那会让探活、心跳、隧道签名、总控上报全部随条目数线性膨胀,把电脑压垮(见 2026-11-19 讨论)。
|
|
157
|
+
* 超出部分按原顺序截断,不报错(与「脏值就地丢弃」同一条契约口径)。
|
|
158
|
+
*/
|
|
159
|
+
const MAX_RELAY_ADDRESSES = 64;
|
|
150
160
|
/**
|
|
151
161
|
* 一组中转地址的清洗:丢空串、去重、丢弃不以 http(s) 开头的,其余按原顺序留着。
|
|
152
162
|
*
|
|
153
163
|
* 这三条就是契约里的「读到的列表不满足就地丢弃、不报错」(docs/relay-multi-address.md)。
|
|
154
164
|
* 每一条都过 `normalizeRelayUrl` 的规范形态,否则列表首项与 `relayUrl` 会差一个尾斜杠,
|
|
155
165
|
* 那条「`relayUrls[0]` 恒等于 `relayUrl`」的不变式就不成立了。
|
|
166
|
+
*
|
|
167
|
+
* 末尾按 `MAX_RELAY_ADDRESSES` 封顶:这台电脑只留前 64 条有效地址。
|
|
156
168
|
*/
|
|
157
169
|
function normalizeRelayUrlList(list) {
|
|
158
170
|
if (!Array.isArray(list)) return [];
|
|
@@ -163,6 +175,7 @@ function normalizeRelayUrlList(list) {
|
|
|
163
175
|
if (!/^https?:\/\/[^\s/]+/i.test(url)) continue;
|
|
164
176
|
if (out.includes(url)) continue;
|
|
165
177
|
out.push(url);
|
|
178
|
+
if (out.length >= MAX_RELAY_ADDRESSES) break;
|
|
166
179
|
}
|
|
167
180
|
return out;
|
|
168
181
|
}
|
|
@@ -248,12 +261,18 @@ function signLanToken(secret, instanceId, ttlSec = MOBILE_TOKEN_TTL_SEC) { retur
|
|
|
248
261
|
function verifyLanToken(secret, token, instanceId) { return sharedToken.verifyLanToken(secret, token, instanceId); }
|
|
249
262
|
|
|
250
263
|
/**
|
|
251
|
-
*
|
|
264
|
+
* 内置公用中转的完整入口清单。**必须与面板 `React/src/lib/links.ts` 的 `PUBLIC_RELAYS`、
|
|
252
265
|
* App `services/viewmodels/public-relay.ts` 的 `PUBLIC_RELAY_URLS` 保持一致** —— 三处各有一份,
|
|
253
|
-
*
|
|
254
|
-
*
|
|
266
|
+
* 加一台公用中转要三处一起补,少补一处那台上的设备就会被漏判。
|
|
267
|
+
*
|
|
268
|
+
* 这里除了拿来判「是不是公用」,还是「老设备只配了第一台时补齐成整份」的地址来源(见
|
|
269
|
+
* `expandDefaultPublicRelays`):宿主必须自己握着这几条 URL,否则补不出另外两台。
|
|
255
270
|
*/
|
|
256
|
-
const
|
|
271
|
+
const PUBLIC_RELAY_URLS = ['https://ai-hub-de-1.wnluo.com', 'https://dsh.iouf.cn', 'https://dsh.yfyidc.cn'];
|
|
272
|
+
/** 按 host 比:协议、默认端口、尾斜杠的差异都不影响它是不是公用。 */
|
|
273
|
+
const PUBLIC_RELAY_HOSTS = new Set(PUBLIC_RELAY_URLS.map((url) => {
|
|
274
|
+
try { return new URL(url).host.toLowerCase(); } catch { return ''; }
|
|
275
|
+
}).filter(Boolean));
|
|
257
276
|
function isPublicRelayUrl(url) {
|
|
258
277
|
try {
|
|
259
278
|
return PUBLIC_RELAY_HOSTS.has(new URL(String(url).trim()).host.toLowerCase());
|
|
@@ -297,8 +316,41 @@ function stripPublicRelay(next) {
|
|
|
297
316
|
return true;
|
|
298
317
|
}
|
|
299
318
|
|
|
319
|
+
/**
|
|
320
|
+
* 老设备补齐:这台如果**只用公用中转、且只配了第一台**(早先引导只写 `PUBLIC_RELAY_URL` 那一台),
|
|
321
|
+
* 读盘时就补成整份内置清单,让电脑能选/失败切换到另外两台(2026-11-19 用户口径:要把流量自动
|
|
322
|
+
* 分到别人维护的那两台上)。
|
|
323
|
+
*
|
|
324
|
+
* 只在三个条件同时成立时动(就地改 state,返回有没有改):
|
|
325
|
+
* - `relayUrls` 非空、且**每一条都是内置公用中转**(有自建中转就不归这管,自建是我们的还是别人的由用户定);
|
|
326
|
+
* - 不是「配了公网自建而强制停用」的状态(`publicRelayBlocked`:有手填直连 / TCP 就不补);
|
|
327
|
+
* - 确实缺着内置清单里的某几台(缺才补,已经齐了就一字不动)。
|
|
328
|
+
* 补齐时保留原有顺序在前(现有的主用不被挤下去),缺的按内置清单顺序追加在后面;`relayUrl` 恒为首项。
|
|
329
|
+
* **不碰任何非公用地址**。
|
|
330
|
+
*/
|
|
331
|
+
function expandDefaultPublicRelays(state) {
|
|
332
|
+
if (!state || typeof state !== 'object') return false;
|
|
333
|
+
const urls = relayAddressesOf(state).relayUrls;
|
|
334
|
+
if (!urls.length) return false;
|
|
335
|
+
if (!urls.every((url) => isPublicRelayUrl(url))) return false;
|
|
336
|
+
if (publicRelayBlocked(state)) return false;
|
|
337
|
+
const hostOf = (url) => { try { return new URL(url).host.toLowerCase(); } catch { return ''; } };
|
|
338
|
+
const presentHosts = new Set(urls.map(hostOf).filter(Boolean));
|
|
339
|
+
const next = [...urls];
|
|
340
|
+
for (const url of PUBLIC_RELAY_URLS) {
|
|
341
|
+
const host = hostOf(url);
|
|
342
|
+
if (!host || presentHosts.has(host)) continue;
|
|
343
|
+
presentHosts.add(host);
|
|
344
|
+
next.push(url);
|
|
345
|
+
}
|
|
346
|
+
if (next.length === urls.length) return false;
|
|
347
|
+
state.relayUrls = next;
|
|
348
|
+
state.relayUrl = next[0];
|
|
349
|
+
return true;
|
|
350
|
+
}
|
|
351
|
+
|
|
300
352
|
module.exports = {
|
|
301
353
|
STATE_FILE, MOBILE_TOKEN_TTL_SEC, MOBILE_BEARER_TTL_SEC, defaultState, loadState, saveState, rotatePairing, genInstanceId, genPairingCode, genPairingTicket, genHistoryKey, ensureHistoryKey,
|
|
302
354
|
tunnelWsUrl, normalizeRelayUrl, normalizeRelayUrlList, normalizeRelayAddresses, relayAddressesOf, applyRelayAddressPatch, signLanToken, verifyLanToken,
|
|
303
|
-
isPublicRelayUrl, publicRelayBlocked, stripPublicRelay,
|
|
355
|
+
isPublicRelayUrl, publicRelayBlocked, stripPublicRelay, expandDefaultPublicRelays, PUBLIC_RELAY_URLS, MAX_RELAY_ADDRESSES,
|
|
304
356
|
};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
// 公用中转「电脑决定」的延迟探测:worker 连中转前先量一遍各候选的 /healthz 延迟,
|
|
3
|
+
// 把最快的那台排在前面再按序连(失败仍按现有逻辑顺延到下一台)。
|
|
4
|
+
//
|
|
5
|
+
// 两件事分开:`orderByLatency` 是纯函数(只排顺序、不碰网络),`probeRelayHealth` 是薄探测
|
|
6
|
+
// (发一次 GET、量到响应为止、超时或报错返回 null)。探测结果交给排序函数,两者互不依赖。
|
|
7
|
+
|
|
8
|
+
const http = require('node:http')
|
|
9
|
+
const https = require('node:https')
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 按实测延迟给候选地址排序:量到延迟的按升序在前,不可达 / 没量到的排后面。
|
|
13
|
+
*
|
|
14
|
+
* 不可达那一段保持它们原来在 `urls` 里的相对顺序(稳定),所以探测全挂时这里退化成原序。
|
|
15
|
+
* 只读入参、返回新数组,不改 `urls`。
|
|
16
|
+
*
|
|
17
|
+
* @param {string[]} urls 候选地址,原序
|
|
18
|
+
* @param {Map<string, number|null>|Object<string, number|null>} latencyByUrl URL→延迟毫秒,
|
|
19
|
+
* null / 缺失 = 不可达或未测到
|
|
20
|
+
* @returns {string[]} 新数组
|
|
21
|
+
*/
|
|
22
|
+
function orderByLatency(urls, latencyByUrl) {
|
|
23
|
+
const readLatency = (url) => {
|
|
24
|
+
if (latencyByUrl instanceof Map) return latencyByUrl.get(url)
|
|
25
|
+
return latencyByUrl ? latencyByUrl[url] : undefined
|
|
26
|
+
}
|
|
27
|
+
const reachable = []
|
|
28
|
+
const unreachable = []
|
|
29
|
+
for (const url of urls) {
|
|
30
|
+
const latency = readLatency(url)
|
|
31
|
+
if (typeof latency === 'number' && Number.isFinite(latency)) reachable.push({ url, latency })
|
|
32
|
+
else unreachable.push(url)
|
|
33
|
+
}
|
|
34
|
+
// 数组的 sort 是稳定排序:延迟相同的几条保持原相对顺序。
|
|
35
|
+
reachable.sort((a, b) => a.latency - b.latency)
|
|
36
|
+
return [...reachable.map((item) => item.url), ...unreachable]
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 对 `url + '/healthz'` 发一次 GET,量出从发起到拿到响应(任意状态码都算)的毫秒数。
|
|
41
|
+
*
|
|
42
|
+
* 超时或报错返回 null(= 这条当期不可达)。按 URL 协议选 http / https,用 `req.setTimeout`
|
|
43
|
+
* 起超时后 `destroy`,不走全局 fetch。
|
|
44
|
+
*
|
|
45
|
+
* @param {string} url 中转地址
|
|
46
|
+
* @param {{ timeoutMs?: number }} [options] 超时毫秒,默认 1500
|
|
47
|
+
* @returns {Promise<number|null>} 延迟毫秒,或 null
|
|
48
|
+
*/
|
|
49
|
+
function probeRelayHealth(url, options = {}) {
|
|
50
|
+
const timeoutMs = Number.isFinite(options.timeoutMs) ? options.timeoutMs : 1500
|
|
51
|
+
return new Promise((resolve) => {
|
|
52
|
+
let healthUrl
|
|
53
|
+
try {
|
|
54
|
+
healthUrl = new URL(`${String(url).replace(/\/+$/, '')}/healthz`)
|
|
55
|
+
} catch {
|
|
56
|
+
resolve(null)
|
|
57
|
+
return
|
|
58
|
+
}
|
|
59
|
+
const client = healthUrl.protocol === 'https:' ? https : http
|
|
60
|
+
const startedAt = Date.now()
|
|
61
|
+
let settled = false
|
|
62
|
+
const finish = (value) => {
|
|
63
|
+
if (settled) return
|
|
64
|
+
settled = true
|
|
65
|
+
resolve(value)
|
|
66
|
+
}
|
|
67
|
+
let req
|
|
68
|
+
try {
|
|
69
|
+
// agent:false:一次性连接,探完就关,别把 keep-alive 的 socket 留在进程里。
|
|
70
|
+
req = client.get(healthUrl, { agent: false }, (res) => {
|
|
71
|
+
res.resume()
|
|
72
|
+
finish(Date.now() - startedAt)
|
|
73
|
+
})
|
|
74
|
+
} catch {
|
|
75
|
+
finish(null)
|
|
76
|
+
return
|
|
77
|
+
}
|
|
78
|
+
req.on('error', () => finish(null))
|
|
79
|
+
req.setTimeout(timeoutMs, () => {
|
|
80
|
+
finish(null)
|
|
81
|
+
req.destroy()
|
|
82
|
+
})
|
|
83
|
+
})
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
module.exports = { orderByLatency, probeRelayHealth }
|
package/lib/routes/mobile-api.js
CHANGED
|
@@ -328,6 +328,8 @@ function registerMobileRoutes(deps) {
|
|
|
328
328
|
rotatePairing,
|
|
329
329
|
// 协议 v2 的配对封包里要下发实例历史密钥(HK)。取值器惰性生成并落盘。
|
|
330
330
|
getHistoryKey,
|
|
331
|
+
// 配对回包顺手带上总控地址与令牌:补「配过但没连过一次」的盲区(见 mobile/pairing.js)。
|
|
332
|
+
getControl,
|
|
331
333
|
log: (...args) => console.warn('[dsh-mobile-plugin] [pair]', ...args),
|
|
332
334
|
}),
|
|
333
335
|
})
|
|
@@ -16,8 +16,8 @@
|
|
|
16
16
|
* 3. 请求体一律先量后读:`/configure` 与镜像帧都有字节上限,超限立刻 413。
|
|
17
17
|
* 4. 换中转地址不是「拆了重建」:先在旧隧道上登记迁移,再建新隧道,注册不上要撤销
|
|
18
18
|
* (见 lib/relay/migrate.js)。这条顺序是「填错地址就把自己锁在外面」的唯一解。
|
|
19
|
-
* 5.
|
|
20
|
-
*
|
|
19
|
+
* 5. 启动与重连先按最近实测延迟把候选排成「最快优先」,再**依次试**,第一条注册成功的当主用,
|
|
20
|
+
* 全不通再从第一条重来;换成功只把它提到队首、报给宿主写回,**不发 migrate 帧**(自动换发生在旧地址已经
|
|
21
21
|
* 连不上的时刻,旧中转上那台机器的隧道早就断了,没人需要被指路)。
|
|
22
22
|
*/
|
|
23
23
|
|
|
@@ -29,6 +29,7 @@ const { createLanProxy } = require('./relay/lan-proxy.js')
|
|
|
29
29
|
const { createLanDiscovery, bindLanEndpoint } = require('./relay/lan-discovery.cjs')
|
|
30
30
|
const { lanUrls } = require('./relay/lan-network.cjs')
|
|
31
31
|
const core = require('./relay/core.js')
|
|
32
|
+
const { orderByLatency, probeRelayHealth } = require('./relay/relay-probe.js')
|
|
32
33
|
const { createReconnectScheduler } = require('./reconnect-scheduler.cjs')
|
|
33
34
|
const { isMirrorSnapshotFresh } = require('./mirror-snapshot.cjs')
|
|
34
35
|
const { snapshotAuthorized } = require('./relay/snapshot-auth.js')
|
|
@@ -70,6 +71,27 @@ let tunnelRoute = null
|
|
|
70
71
|
* `raceTunnels` 与 `openTunnel` 的 catch 负责(**只有全部都不通才报 error**)。
|
|
71
72
|
*/
|
|
72
73
|
let pendingAttempt = null
|
|
74
|
+
/**
|
|
75
|
+
* 每个候选地址最近一次实测的 /healthz 延迟(URL→毫秒;null = 不可达)。
|
|
76
|
+
*
|
|
77
|
+
* 电脑侧按实测延迟排序候选,最快的那台排前面先试。`relayLatencyAt` 是那次探测的时间戳,
|
|
78
|
+
* 5 分钟保鲜期内沿用这份结果;过期了 `relayRoutePlan` 就退回配置原序,等下一次
|
|
79
|
+
* `startTunnel` 重新探测再进来。
|
|
80
|
+
*/
|
|
81
|
+
let relayLatencyCache = new Map()
|
|
82
|
+
let relayLatencyAt = 0
|
|
83
|
+
/** 一轮探活里最多几条同时在探(地址池变大时的并发上限,见 refreshRelayLatency)。 */
|
|
84
|
+
const PROBE_CONCURRENCY = 6
|
|
85
|
+
/**
|
|
86
|
+
* 隧道世代号:`stopTunnel` 每执行一次 +1。
|
|
87
|
+
*
|
|
88
|
+
* `startTunnel` 现在要先 await 一次延迟探测再进 `raceTunnels`,这中间隔了几百毫秒 ——
|
|
89
|
+
* 原先没有这个窗口。若这期间又来一次配置下发 / 重连又调了 `startTunnel`,两条异步链会各自
|
|
90
|
+
* 跑一遍 `raceTunnels`,各自 open 一条**同 instanceId** 的隧道;中转上同实例的后连会顶掉前一条,
|
|
91
|
+
* 结果把刚连好的那条挤断。世代号让已经过期的链在进 `raceTunnels` 前、以及循环每一轮都自检,
|
|
92
|
+
* 一旦发现自己是旧的立刻收手。
|
|
93
|
+
*/
|
|
94
|
+
let tunnelGeneration = 0
|
|
73
95
|
let lanState = 'off'
|
|
74
96
|
let lanError = null
|
|
75
97
|
let lanDiscovery = null
|
|
@@ -358,6 +380,8 @@ function stopNetworkMonitor() {
|
|
|
358
380
|
}
|
|
359
381
|
function scheduleReconnect() { reconnect.schedule() }
|
|
360
382
|
function stopTunnel() {
|
|
383
|
+
// 换代:任何还在探测 / 正在排队的旧 startTunnel 异步链据此作废,别再开新隧道(见 tunnelGeneration)。
|
|
384
|
+
tunnelGeneration += 1
|
|
361
385
|
reconnect.cancel()
|
|
362
386
|
if (tunnel) tunnel.stop()
|
|
363
387
|
tunnel = null; tunnelState = 'off'; tunnelError = null
|
|
@@ -473,13 +497,22 @@ function startTunnel() {
|
|
|
473
497
|
if (!relayRoutePlan().length) { stopTunnel(); return }
|
|
474
498
|
stopTunnel(); tunnelState = 'connecting'
|
|
475
499
|
publishState()
|
|
476
|
-
|
|
500
|
+
// 连之前先把各候选实测一遍延迟,让 relayRoutePlan 给出「最快优先」的顺序;探完再按序连。
|
|
501
|
+
// 探测本身失败不回滚这次连接(catch 吞掉),别忘了探完还要判一次 enabled。
|
|
502
|
+
// 记下这一代:探测期间若又发生重建(世代号前进),这条链就作废,别去开同实例的第二条隧道。
|
|
503
|
+
const generation = tunnelGeneration
|
|
504
|
+
void (async () => {
|
|
505
|
+
try { await refreshRelayLatency() } catch {}
|
|
506
|
+
if (generation !== tunnelGeneration || !config.enabled) return
|
|
507
|
+
await raceTunnels(generation)
|
|
508
|
+
})()
|
|
477
509
|
}
|
|
478
510
|
/**
|
|
479
|
-
*
|
|
511
|
+
* 一次配置里可以按优先级试的中转地址。
|
|
480
512
|
*
|
|
481
|
-
*
|
|
482
|
-
*
|
|
513
|
+
* 公用中转改成「电脑决定」后,候选顺序取**最近一次实测延迟的快慢**(快的最先试);
|
|
514
|
+
* 探测结果超过 5 分钟保鲜期就退回 `relayUrls` 的原序 —— 那份原序是上一次写回收敛的结果,
|
|
515
|
+
* 下一次 `startTunnel` 会先重新探测再进来。
|
|
483
516
|
*
|
|
484
517
|
* `relayUrls` 缺席或为空(老宿主只下发 `relayUrl`)时退回单元素 —— 契约里那条兼容口径。
|
|
485
518
|
* 已按 `core.normalizeRelayUrl` 规范化并用它拼 `tunnelWsUrl`,脏地址在这里就被丢掉而不是
|
|
@@ -500,14 +533,38 @@ function relayRoutePlan() {
|
|
|
500
533
|
seen.add(url)
|
|
501
534
|
urls.push(url)
|
|
502
535
|
}
|
|
536
|
+
// 延迟结果还在保鲜期就按最快优先给出候选顺序;过期就按原序,等下次 startTunnel 重探。
|
|
537
|
+
// 只影响尝试顺序,不改地址本身,也不改去重 / 过滤的结果。
|
|
538
|
+
if (Date.now() - relayLatencyAt < 300000) return orderByLatency(urls, relayLatencyCache)
|
|
503
539
|
return urls
|
|
504
540
|
}
|
|
505
541
|
/**
|
|
506
|
-
*
|
|
542
|
+
* 并发探一遍当前所有候选中转的 /healthz,把实测延迟写进模块级缓存并记时间戳。
|
|
543
|
+
*
|
|
544
|
+
* 每条独立超时(1500ms);某条探测失败只是这条记成 null(不可达、排后面),不影响其它条,
|
|
545
|
+
* 也不阻止这次连接(排序函数会把它们稳定地排在后面,失败仍由 raceTunnels 顺延到下一台)。
|
|
546
|
+
*/
|
|
547
|
+
async function refreshRelayLatency() {
|
|
548
|
+
const urls = relayRoutePlan()
|
|
549
|
+
// 并发**分段**探,不一次性全打出去:地址池将来可能按地区变多,全并发会在同一瞬间打出一堆
|
|
550
|
+
// 连接(对自己的中转也等于一次小 DDoS),把 worker 的连接/内存压垮。每段最多 6 条同时探,
|
|
551
|
+
// 段内并发、段间顺序;每条的 1.5s 超时照旧只约束单条。
|
|
552
|
+
const results = new Map()
|
|
553
|
+
for (let index = 0; index < urls.length; index += PROBE_CONCURRENCY) {
|
|
554
|
+
const batch = urls.slice(index, index + PROBE_CONCURRENCY)
|
|
555
|
+
const probed = await Promise.all(batch.map(async (url) => [url, await probeRelayHealth(url, { timeoutMs: 1500 })]))
|
|
556
|
+
for (const [url, ms] of probed) results.set(url, ms)
|
|
557
|
+
}
|
|
558
|
+
relayLatencyCache = results
|
|
559
|
+
relayLatencyAt = Date.now()
|
|
560
|
+
}
|
|
561
|
+
/**
|
|
562
|
+
* 这一轮选中的中转写成当前主用,并把它**提到队首**,其余条目相对顺序不动。
|
|
507
563
|
*
|
|
508
564
|
* 为什么非提不可:`relayUrls[0]` 恒等于 `relayUrl`(core 的 normalize 每次落盘都会这么收敛)。
|
|
509
|
-
* 只把中选的地址写成 `relayUrl`
|
|
510
|
-
*
|
|
565
|
+
* 只把中选的地址写成 `relayUrl` 而列表原样不动,下一次读盘就被旧首项顶回去,写回等于没写。
|
|
566
|
+
* 启用延迟排序之后,中选那条**未必**是配置里的首项(`index 0` 只表示它是排序队列里最快的),
|
|
567
|
+
* 所以「要不要写回」改成看「写回后的顺序与当前 config 是否一致」,不是看「有没有从 0 挪到队首」。
|
|
511
568
|
*
|
|
512
569
|
* 两次账要一起结,漏一次就是「刚连上的隧道被自己拆掉」:
|
|
513
570
|
* - 本进程的 `config` 就地换成新顺序 —— 宿主随后会下发同一份(它从这份状态里读),
|
|
@@ -520,11 +577,14 @@ function adoptRoute(route) {
|
|
|
520
577
|
if (!route) return
|
|
521
578
|
const remaining = route.urls.filter(url => url !== route.url)
|
|
522
579
|
const next = [route.url, ...remaining]
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
580
|
+
// 中选那条必须成为当前主用:它是此刻真的连上的那台。延迟排序后 `index 0` 不等于
|
|
581
|
+
// `config.relayUrl`,若沿旧判据(index !== 0)就会漏写回,让 `/status` 与总控继续报那条
|
|
582
|
+
// 可能已经死掉的旧主用 —— 手机连不上时查到的地址就是错的。
|
|
583
|
+
const current = Array.isArray(config.relayUrls) ? config.relayUrls : []
|
|
584
|
+
const sameOrder = next.length === current.length && next.every((url, index) => url === current[index])
|
|
585
|
+
const adopted = !sameOrder
|
|
586
|
+
config.relayUrl = next[0]
|
|
587
|
+
if (adopted) config.relayUrls = next
|
|
528
588
|
tunnelRoute = {
|
|
529
589
|
url: route.url,
|
|
530
590
|
index: route.index,
|
|
@@ -532,7 +592,7 @@ function adoptRoute(route) {
|
|
|
532
592
|
total: route.urls.length,
|
|
533
593
|
// 报出去的是**写回之后**的顺序:宿主拿它落盘,与本进程那份一字不差。
|
|
534
594
|
urls: next,
|
|
535
|
-
//
|
|
595
|
+
// 顺序与当前 config 不同才需要宿主写回;一致时落盘是多余的一次磁盘写。
|
|
536
596
|
adopted,
|
|
537
597
|
}
|
|
538
598
|
}
|
|
@@ -547,10 +607,13 @@ function adoptRoute(route) {
|
|
|
547
607
|
* 会**从列表第一条重新开始**(`tunnelRoute` 在 `stopTunnel` 里作废),不许记住「上次卡在
|
|
548
608
|
* 第几条」。
|
|
549
609
|
*/
|
|
550
|
-
async function raceTunnels() {
|
|
610
|
+
async function raceTunnels(generation) {
|
|
551
611
|
const urls = relayRoutePlan()
|
|
552
612
|
if (!urls.length) { stopTunnel(); return false }
|
|
553
613
|
for (let index = 0; index < urls.length; index += 1) {
|
|
614
|
+
// 这一代已经被新的 startTunnel 取代(配置又变了 / 又重连了一次):立刻收手,
|
|
615
|
+
// 别继续开同实例的隧道去顶掉新的那条(见 tunnelGeneration)。
|
|
616
|
+
if (generation !== undefined && generation !== tunnelGeneration) return false
|
|
554
617
|
// 中途被关掉(用户点了关闭 / 配置改成 enabled:false):不试了,**顺手把这笔账清掉**。
|
|
555
618
|
// 留着它的话,那条隧道的 close 会被 `onClose` 当成「还在等注册结果」吞掉 —— 下一次
|
|
556
619
|
// 真正的掉线就报不出来,状态永远停在 connecting。
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-mobile-plugin",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.76",
|
|
4
4
|
"description": "Mobile API (/api/mobile/v1) and relay client for the DSH mobile app — desktop profile build (dsh 0.2.x, peers resolved by the host)",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"icon": "./icon.png",
|