dsh-mobile-plugin 0.5.103 → 0.5.106
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/config/agent-presets/standard/skills/dsh-mobile-kb/SKILL.md +1 -1
- package/config/agent-presets/standard/skills/dsh-mobile-kb/references/cloudflare.md +1 -1
- package/config/agent-presets/standard/skills/dsh-mobile-kb/references/network-guide.md +4 -4
- package/config/agent-presets/standard/skills/dsh-mobile-kb/references/network.md +6 -2
- package/config/agent-presets/standard/skills/dsh-mobile-kb/references/relay.md +13 -3
- package/config/agent-presets/standard/skills/dsh-mobile-kb/references/zeronews.md +6 -0
- package/lib/cli/pair.cjs +7 -3
- package/lib/client.js +4 -1
- package/lib/cloudflared/runner.cjs +16 -12
- package/lib/cloudflared/wiring.cjs +1 -1
- package/lib/frpc/runner.cjs +14 -1
- package/lib/http/routing.js +8 -166
- package/lib/impl.js +26 -31
- package/lib/index.js +30 -5
- package/lib/mobile/guarded.js +170 -0
- package/lib/mobile/mux.js +18 -2
- package/lib/mobile/permissions.js +1 -0
- package/lib/mobile/plugin-update.js +29 -0
- package/lib/p2p/session.cjs +3 -1
- package/lib/p2p/wiring.cjs +17 -2
- package/lib/panel/index.html +13 -73
- package/lib/relay/control.js +78 -44
- package/lib/relay/core.js +29 -1
- package/lib/relay/lan-network.cjs +2 -7
- package/lib/relay/pairing-payload.cjs +68 -1
- package/lib/relay/provider-lines.cjs +53 -0
- package/lib/routes/control-plane.js +40 -6
- package/lib/routes/mobile-api.js +7 -21
- package/lib/shared/e2ee-request.js +1 -1
- package/lib/shared/private-address.js +29 -0
- package/lib/shared/via-header.js +6 -3
- package/lib/shell/auto-updater.js +47 -0
- package/lib/shell/runtime.js +11 -0
- package/lib/shell/transport-host.js +350 -106
- package/lib/shell/update-consent.js +111 -0
- package/lib/shell/updater.js +3 -3
- package/lib/sidecar-lifecycle.cjs +53 -2
- package/lib/tailcat/session.cjs +2 -1
- package/lib/tailcat/wiring.cjs +18 -3
- package/lib/update-registry.cjs +31 -3
- package/lib/zeronews/account.cjs +19 -2
- package/lib/zeronews/login-error.cjs +43 -0
- package/lib/zeronews/purge.cjs +138 -0
- package/lib/zeronews/runner.cjs +123 -22
- package/lib/zeronews/wiring.cjs +81 -23
- package/package.json +21 -1
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 自动更新的「先问一声」编排(shell/auto-updater)。
|
|
3
|
+
*
|
|
4
|
+
* 从稳定外壳 `lib/index.js` 里搬出来:外壳要保持薄(只装载、不长业务),而这一块是货真价实的
|
|
5
|
+
* 业务编排 —— 从业务上回答「这一步该不该装」。
|
|
6
|
+
*
|
|
7
|
+
* 职责只有一件:自动更新每拍做一次「查新版 → 问状态机(有人连着就先停、10 分钟兜底)→ 该装
|
|
8
|
+
* 才装」。定时器不在这里,仍在 index.js(它得进 ctx.effect、unref、能随卸载清掉);这里只给
|
|
9
|
+
* 一拍 `tick()`,index.js 到点调它。
|
|
10
|
+
*
|
|
11
|
+
* 判断「有没有人在用」由外部喂 `activeMuxConnections()`(外壳持着 mux 服务,知道现在有几条
|
|
12
|
+
* 手机长连接)。号数拿不到一律当 0:那台机器也没人在用,照旧直接更。
|
|
13
|
+
*/
|
|
14
|
+
export function createAutoUpdater({ consent, check, applyUpdate, log = () => {}, runningVersion = () => null, activeMuxConnections = () => 0 }) {
|
|
15
|
+
const appActive = () => {
|
|
16
|
+
try {
|
|
17
|
+
return Number(activeMuxConnections()) > 0;
|
|
18
|
+
} catch {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 自动更新的一拍:查到有没有新版,再按「有没有手机连着 / 有没有推迟」决定装还是先停。
|
|
25
|
+
*
|
|
26
|
+
* 有人连着就停住等确认(最多 10 分钟,见 `shell/update-consent.js`);没人用照旧直接装。
|
|
27
|
+
* 手动更新(手机上点「更新」)不走这条,直接 `applyUpdate`。
|
|
28
|
+
*/
|
|
29
|
+
async function tick() {
|
|
30
|
+
const result = await check();
|
|
31
|
+
const decision = consent.evaluate({
|
|
32
|
+
runningVersion: (() => { try { return runningVersion(); } catch { return null; } })(),
|
|
33
|
+
latestVersion: result?.latestVersion ?? null,
|
|
34
|
+
updateAvailable: result?.updateAvailable === true,
|
|
35
|
+
appActive: appActive(),
|
|
36
|
+
});
|
|
37
|
+
if (decision.action !== 'update') {
|
|
38
|
+
if (decision.action === 'hold') {
|
|
39
|
+
log('info', 'dsh-mobile-plugin: 有手机正连着,插件更新先等用户确认(%s)', decision.status.pendingVersion ?? '');
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
return applyUpdate({ auto: true });
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return { tick, appActive };
|
|
47
|
+
}
|
package/lib/shell/runtime.js
CHANGED
|
@@ -414,6 +414,8 @@ export function createRuntime() {
|
|
|
414
414
|
async function reload(preferVersion, options = {}) {
|
|
415
415
|
if (detached) throw new Error('插件已经卸载,不能再换代')
|
|
416
416
|
if (swapping) return swapping
|
|
417
|
+
// 三条主链路的"提交时才换实例"协调器:提交成功才换、失败保留旧实例(见 shell/transport-host.js)。
|
|
418
|
+
const swap = (() => { try { return typeof ctx?.get === 'function' ? ctx.get('dshMobileTransportSwap') || null : null } catch { return null } })()
|
|
417
419
|
swapping = (async () => {
|
|
418
420
|
const target = resolveGeneration(preferVersion)
|
|
419
421
|
if (!target) throw new Error('找不到任何一代实现:包内的 lib/impl.js 缺失')
|
|
@@ -441,6 +443,8 @@ export function createRuntime() {
|
|
|
441
443
|
// 蓝绿:不再先卸旧代。新代先登记到分发表(staging),旧代继续服务在飞请求;提交后才卸
|
|
442
444
|
// 旧代。这样换代码那一刻没有路由空窗,也不会因为新旧两代注册同一路径撞 duplicate route。
|
|
443
445
|
try {
|
|
446
|
+
// 换代事务开始:这期间新代注入的 providers/authorize 只暂存,提交成功才落、失败即丢。
|
|
447
|
+
if (swap && typeof swap.begin === 'function') { try { swap.begin() } catch { /* ignore */ } }
|
|
444
448
|
const next = await activateLoaded(prepared, { commit: false })
|
|
445
449
|
// 原子提交:分发表切到新代
|
|
446
450
|
const prevLive = commitActivation()
|
|
@@ -460,6 +464,7 @@ export function createRuntime() {
|
|
|
460
464
|
loadedCurrent = oldLoaded
|
|
461
465
|
current = oldCurrent
|
|
462
466
|
activatedAt = oldActivatedAt
|
|
467
|
+
if (swap && typeof swap.rollback === 'function') { try { swap.rollback() } catch { /* ignore */ } }
|
|
463
468
|
const brokenFiber = next.fiber
|
|
464
469
|
if (brokenFiber) { try { await brokenFiber.dispose() } catch { /* 卸不干净也照退 */ } }
|
|
465
470
|
lastRollback = { at: new Date().toISOString(), ok: true, toVersion: oldCurrent?.version ?? null, fromVersion: target.version, reason: failure }
|
|
@@ -469,6 +474,8 @@ export function createRuntime() {
|
|
|
469
474
|
throw error
|
|
470
475
|
}
|
|
471
476
|
}
|
|
477
|
+
// 自检通过 = 这次换代真的成功:这时才把三条主链路的实例换成新实现;失败路径不会走到这。
|
|
478
|
+
if (swap && typeof swap.commit === 'function') { try { await swap.commit() } catch (error) { lastError = `换代切换传输失败:${error?.message || error}` } }
|
|
472
479
|
if (oldCurrent) previous = oldCurrent
|
|
473
480
|
// 成功:旧代排空后卸掉(在飞请求仍跑旧 handler 闭包)。
|
|
474
481
|
await drain()
|
|
@@ -479,6 +486,7 @@ export function createRuntime() {
|
|
|
479
486
|
return status()
|
|
480
487
|
} catch (error) {
|
|
481
488
|
// 装载直接抛错:activateLoaded 已 rollbackActivation,旧代毫发无损(fiber/current 没动)。
|
|
489
|
+
if (swap && typeof swap.rollback === 'function') { try { swap.rollback() } catch { /* ignore */ } }
|
|
482
490
|
if (error?.code === 'self_check_failed') throw error
|
|
483
491
|
lastError = `装载 ${target.version} 失败:${error?.message || error}`
|
|
484
492
|
throw error
|
|
@@ -506,6 +514,8 @@ export function createRuntime() {
|
|
|
506
514
|
*/
|
|
507
515
|
async function attemptAll() {
|
|
508
516
|
const failures = []
|
|
517
|
+
// boot/补齐也接上 swap:某一代 adopt 之后失败要把它建出的实例收回,别让待建悬挂、也别继续跑坏代。
|
|
518
|
+
const swap = (() => { try { return typeof ctx?.get === 'function' ? ctx.get('dshMobileTransportSwap') || null : null } catch { return null } })()
|
|
509
519
|
for (const target of resolveGenerations()) {
|
|
510
520
|
if (detached) throw new Error('插件已经卸载,不再重试装载')
|
|
511
521
|
try {
|
|
@@ -520,6 +530,7 @@ export function createRuntime() {
|
|
|
520
530
|
return { ok: true, version: target.version }
|
|
521
531
|
} catch (error) {
|
|
522
532
|
failures.push(`装载 ${target.version}(${target.source})失败:${error?.message || error}`)
|
|
533
|
+
if (swap && typeof swap.abort === 'function') { try { await swap.abort() } catch { /* ignore */ } }
|
|
523
534
|
}
|
|
524
535
|
}
|
|
525
536
|
return { ok: false, failures }
|
|
@@ -1,169 +1,413 @@
|
|
|
1
1
|
// 稳定持有传输服务,并从当前实现读取地址与中转注册真值。
|
|
2
2
|
/**
|
|
3
|
-
* 稳定外壳持有的传输进程:sidecar(中转隧道 + 局域网监听)、FRP
|
|
3
|
+
* 稳定外壳持有的传输进程:sidecar(中转隧道 + 局域网监听)、FRP 直连、总控上报、
|
|
4
|
+
* 隧道助手(tailcat)、点对点(p2p)、mux 长连接。
|
|
4
5
|
*
|
|
5
|
-
*
|
|
6
|
-
* 连同隧道一起停掉再重建,手机就在那几秒断线。这里由外壳创建并持有,跨代存活;换代只把
|
|
7
|
-
* 配置/端口/状态来源重新注入(`setProviders` / `setPortProvider`),进程本身照跑。
|
|
6
|
+
* ## 三条主链路:提交时才换实例(永不重启 DSH)
|
|
8
7
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
8
|
+
* sidecar / 总控 / mux 是**单实例主连接**。实现住在一代里,实例由外壳持有;换代时**不立刻拆旧
|
|
9
|
+
* 实例**:新代把「待建工厂」登记下来,外壳给它一个**稳定门面**(方法都委托到当前实例)。运行时
|
|
10
|
+
* 在换代「提交成功且自检通过」后调 swap.commit():停旧、按新代工厂建新、起新;失败调
|
|
11
|
+
* swap.rollback():丢弃待建、旧实例毫发无损——回滚是干净的。
|
|
11
12
|
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
13
|
+
* 换代事务化:整个 reload 期间(runtime 调 swap.begin 到 commit/rollback 之间),这一代注入的
|
|
14
|
+
* providers / authorize **先暂存**,commit 才落到(新的或当前的)实例,rollback 直接丢弃。这样
|
|
15
|
+
* 即使指纹没变(最常见)也不会在 apply 期污染旧实例、回滚也还原得回来。
|
|
16
|
+
*
|
|
17
|
+
* 门面跨实例稳定(每次调用转发到「当前实例」);换代只换当前实例,不换门面引用。
|
|
18
|
+
*
|
|
19
|
+
* ## 可选线路:FRP / 隧道助手 / 点对点
|
|
20
|
+
*
|
|
21
|
+
* 仍按实现指纹沿用或重建(沿用不重连;变了只换这一条,DSH 不重启)。
|
|
22
|
+
*
|
|
23
|
+
* 外壳(`lib/index.js`)只调一次 `installTransportHosts(ctx)`,业务逻辑全在这个文件里。
|
|
14
24
|
*/
|
|
15
|
-
import
|
|
16
|
-
import frpcRunnerCjs from '../frpc/runner.cjs'
|
|
17
|
-
import controlCjs from '../relay/control.js'
|
|
18
|
-
import tailcatWiringCjs from '../tailcat/wiring.cjs'
|
|
19
|
-
import p2pWiringCjs from '../p2p/wiring.cjs'
|
|
20
|
-
import muxCjs from '../mobile/mux.js'
|
|
25
|
+
import crypto from 'node:crypto'
|
|
21
26
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
const
|
|
27
|
+
/** 门面:每次现取当前实例;属性读当前值,方法转发;换代事务期把配置/注册类方法暂存起来。 */
|
|
28
|
+
function makeFacade(getHandle, { props = [], stageMethods = [], stage = null } = {}) {
|
|
29
|
+
return new Proxy({}, {
|
|
30
|
+
get(_t, prop) {
|
|
31
|
+
if (typeof prop === 'symbol') return undefined
|
|
32
|
+
const current = getHandle()
|
|
33
|
+
if (props.includes(prop) || (current && typeof current[prop] !== 'function')) {
|
|
34
|
+
return current ? current[prop] : undefined
|
|
35
|
+
}
|
|
36
|
+
return (...args) => {
|
|
37
|
+
if (stage && stageMethods.includes(prop)) {
|
|
38
|
+
const staged = stage(prop, args)
|
|
39
|
+
if (staged) return staged.result
|
|
40
|
+
}
|
|
41
|
+
const handle = getHandle()
|
|
42
|
+
if (!handle || typeof handle[prop] !== 'function') return undefined
|
|
43
|
+
return handle[prop](...args)
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
})
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** 提交时才切换的槽(事务化):boot 建并启动;换代变化只登记待建;配置暂存;commit 落配置并换实例;rollback 全丢。 */
|
|
50
|
+
function createSwapSlot({ start = null, stop = null, props = [], stageMethods = [], isSwapping } = {}) {
|
|
51
|
+
let current = null // { fingerprint, handle }
|
|
52
|
+
let pending = null // { fingerprint, build }
|
|
53
|
+
let staged = [] // [[prop, args]]
|
|
54
|
+
const getHandle = () => (current ? current.handle : null)
|
|
55
|
+
// 换代过渡期把「配置/注册」类方法先暂存,commit 才落到当前(或新)实例。这里必须把「退订凭据」
|
|
56
|
+
// 交回给调用方:订阅类方法(隧道连上补推、选中线路回写)返回的是取消订阅函数,如果暂存时把它
|
|
57
|
+
// 吞成空,这一代 dispose 时就退订不了——反复同指纹热重载会在同一个长活实例上越挂越多,已卸载
|
|
58
|
+
// 那代的闭包还挂在订阅表里,隧道再连上会把它们当活的调起来。
|
|
59
|
+
const stage = (prop, args) => {
|
|
60
|
+
if (!isSwapping()) return null
|
|
61
|
+
const entry = { prop, args, off: null, cancelled: false }
|
|
62
|
+
staged.push(entry)
|
|
63
|
+
const off = () => {
|
|
64
|
+
entry.cancelled = true
|
|
65
|
+
const real = entry.off
|
|
66
|
+
entry.off = null
|
|
67
|
+
if (typeof real === 'function') { try { real() } catch { /* 已经退了 */ } }
|
|
68
|
+
}
|
|
69
|
+
return { result: off }
|
|
70
|
+
}
|
|
71
|
+
const facade = makeFacade(getHandle, { props, stageMethods, stage })
|
|
72
|
+
return {
|
|
73
|
+
facade,
|
|
74
|
+
get current() { return getHandle() },
|
|
75
|
+
adopt({ fingerprint, create: build } = {}) {
|
|
76
|
+
if (typeof build !== 'function') return facade
|
|
77
|
+
if (current && current.fingerprint === fingerprint) return facade
|
|
78
|
+
if (pending && pending.fingerprint === fingerprint) return facade
|
|
79
|
+
if (!current) { current = { fingerprint, handle: build() }; if (start) { try { Promise.resolve(start(current.handle)).catch(() => { /* 由自检兜 */ }) } catch { /* 由自检兜 */ } } ; return facade }
|
|
80
|
+
pending = { fingerprint, build }
|
|
81
|
+
return facade
|
|
82
|
+
},
|
|
83
|
+
async commit() {
|
|
84
|
+
const config = staged
|
|
85
|
+
staged = []
|
|
86
|
+
// 回放暂存的配置/注册:订阅类方法返回的退订函数要存进 entry.off,这一代 dispose 时才好退。
|
|
87
|
+
const replay = handle => {
|
|
88
|
+
for (const entry of config) {
|
|
89
|
+
if (entry.cancelled) continue
|
|
90
|
+
try {
|
|
91
|
+
if (handle && typeof handle[entry.prop] === 'function') {
|
|
92
|
+
const off = handle[entry.prop](...entry.args)
|
|
93
|
+
if (typeof off === 'function') entry.off = off
|
|
94
|
+
}
|
|
95
|
+
} catch { /* 回放失败不挡切换 */ }
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if (pending) {
|
|
99
|
+
const prev = current
|
|
100
|
+
const fingerprint = pending.fingerprint
|
|
101
|
+
const build = pending.build
|
|
102
|
+
pending = null
|
|
103
|
+
let next
|
|
104
|
+
try { next = build() } catch (error) { if (prev) current = prev; throw error }
|
|
105
|
+
replay(next)
|
|
106
|
+
if (prev) { try { await stop(prev.handle) } catch { /* 已经停了 */ } }
|
|
107
|
+
try { if (start) await start(next) } catch { /* 起不来就退回旧实例,别把没起来的设成当前 */ if (prev) { try { if (start) await start(prev.handle) } catch { /* 尽力恢复 */ } current = prev } return }
|
|
108
|
+
current = { fingerprint, handle: next }
|
|
109
|
+
} else {
|
|
110
|
+
replay(getHandle())
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
rollback() { pending = null; staged = [] },
|
|
114
|
+
/** boot/补齐里某一代先 adopt 建出实例后又装载失败:收回它,让回退代能干净重建。
|
|
115
|
+
* 必须 await 停完再返回:不等旧 worker 真退就进下一代,两个 sidecar 可能抢同一个 LAN 端口。 */
|
|
116
|
+
async reset() { const prev = current; current = null; pending = null; staged = []; if (prev) { try { if (stop) await stop(prev.handle) } catch { /* 已经停了 */ } } },
|
|
117
|
+
async dispose() { if (current) { const handle = current.handle; current = null; try { if (stop) await stop(handle) } catch { /* 已经停了 */ } } },
|
|
118
|
+
}
|
|
119
|
+
}
|
|
28
120
|
|
|
29
121
|
export function installTransportHosts(ctx) {
|
|
30
|
-
/** 外壳自己的告警出口:拿不到 logger 也照常往下走,不打日志不算失败。 */
|
|
31
122
|
const warn = (message, detail) => {
|
|
32
123
|
try { ctx.logger?.warn?.(`dsh-mobile-plugin: ${message}${detail ? ` ${detail}` : ''}`) } catch { /* 日志不许反噬 */ }
|
|
33
124
|
}
|
|
34
125
|
|
|
35
|
-
//
|
|
36
|
-
|
|
37
|
-
const
|
|
126
|
+
// 换代事务开关:runtime 在 reload 开始调 begin,在提交成功 commit / 失败 rollback 时关闭。
|
|
127
|
+
let inSwap = false
|
|
128
|
+
const isSwapping = () => inSwap
|
|
129
|
+
|
|
130
|
+
// ── sidecar:中转隧道 + 局域网监听(提交时才换实例) ────────────────
|
|
131
|
+
const sidecarToken = crypto.randomBytes(32).toString('hex')
|
|
132
|
+
const sidecarSlot = createSwapSlot({
|
|
133
|
+
start: handle => handle.start(),
|
|
134
|
+
stop: handle => handle.stop(),
|
|
135
|
+
props: ['sidecarToken'],
|
|
136
|
+
stageMethods: ['setProviders', 'onTunnelUp', 'onRouteAdopted'],
|
|
137
|
+
isSwapping,
|
|
138
|
+
})
|
|
139
|
+
const sidecarAdopt = ({ fingerprint, create } = {}) =>
|
|
140
|
+
sidecarSlot.adopt({ fingerprint, create: typeof create === 'function' ? () => create({ token: sidecarToken }) : create })
|
|
141
|
+
// 旧代(不认识 adopt)把这份服务当 lifecycle 用:把门面本身当 host,挂上 adopt/lifecycle,旧代 .start() 也不会崩。
|
|
142
|
+
const sidecarHost = new Proxy(sidecarSlot.facade, {
|
|
143
|
+
get(target, prop) {
|
|
144
|
+
if (prop === 'adopt') return sidecarAdopt
|
|
145
|
+
if (prop === 'lifecycle') return sidecarSlot.current
|
|
146
|
+
return target[prop]
|
|
147
|
+
},
|
|
148
|
+
})
|
|
38
149
|
ctx.reflect.provide('dshMobileSidecarHost', sidecarHost)
|
|
39
|
-
ctx.effect(() => () => sidecarHost.stop(), 'dsh-mobile-plugin: sidecar transport host')
|
|
40
150
|
|
|
41
|
-
// ── FRP 直连:frpc
|
|
151
|
+
// ── FRP 直连:frpc 子进程(可选线路:按指纹沿用/重建) ─────────────
|
|
42
152
|
let frpPortProvider = () => 0
|
|
43
|
-
|
|
153
|
+
let frpEntry = null
|
|
154
|
+
const frpRunnerOf = () => (frpEntry ? frpEntry.runner : null)
|
|
44
155
|
ctx.reflect.provide('dshMobileTransportFrp', {
|
|
45
|
-
runner
|
|
156
|
+
get runner() { return frpRunnerOf() },
|
|
157
|
+
adopt: ({ fingerprint, create } = {}) => {
|
|
158
|
+
if (typeof create !== 'function') return frpRunnerOf()
|
|
159
|
+
if (frpEntry && frpEntry.fingerprint === fingerprint) return frpEntry.runner
|
|
160
|
+
const previous = frpEntry
|
|
161
|
+
const runner = create({ portOf: () => frpPortProvider() })
|
|
162
|
+
frpEntry = { fingerprint, runner }
|
|
163
|
+
if (previous && previous.runner !== runner) { try { void previous.runner.dispose() } catch { /* 已经停了 */ } }
|
|
164
|
+
return runner
|
|
165
|
+
},
|
|
46
166
|
setPortProvider: fn => {
|
|
47
167
|
if (typeof fn !== 'function') return
|
|
48
168
|
frpPortProvider = fn
|
|
49
|
-
|
|
169
|
+
const runner = frpRunnerOf()
|
|
170
|
+
if (runner && runner.shouldAutoStart()) void runner.start()
|
|
50
171
|
},
|
|
51
172
|
})
|
|
52
|
-
ctx.effect(() => () => { void
|
|
173
|
+
ctx.effect(() => () => { const runner = frpRunnerOf(); if (runner) void runner.dispose() }, 'dsh-mobile-plugin: frpc transport host')
|
|
53
174
|
|
|
54
|
-
// ──
|
|
55
|
-
|
|
175
|
+
// ── 总控上报:常连的出站客户端(提交时才换客户端 / 事务化注入来源) ──
|
|
176
|
+
const controlDefaults = () => ({
|
|
56
177
|
getState: () => ({}),
|
|
57
178
|
persist: () => {},
|
|
58
179
|
getPort: () => 0,
|
|
59
180
|
getHistoryKey: () => '',
|
|
60
|
-
getRelayConnected: () =>
|
|
61
|
-
getConnectedRelay: () => { const tunnel =
|
|
62
|
-
// 身份冲突时触发换身份的动作,由当前这一代注入(换代换它即可)。缺省不启用。
|
|
181
|
+
getRelayConnected: () => { const lc = sidecarSlot.current; return lc && typeof lc.tunnelConnected === 'function' ? lc.tunnelConnected() : false },
|
|
182
|
+
getConnectedRelay: () => { const lc = sidecarSlot.current; const tunnel = lc ? lc.getSnapshot?.()?.tunnel : null; return tunnel?.state === 'connected' ? tunnel.relayUrl || tunnel.route?.url || '' : '' },
|
|
63
183
|
onIdentityConflict: null,
|
|
64
|
-
// 总控转达的「请自检」要回的那份诊断,由当前这一代注入(同一份内容,见 relay-control)。
|
|
65
184
|
getDiagnostics: null,
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
getState: () => controlProviders.getState(),
|
|
69
|
-
persist: () => controlProviders.persist(),
|
|
70
|
-
getPort: () => controlProviders.getPort(),
|
|
71
|
-
getHistoryKey: () => controlProviders.getHistoryKey(),
|
|
72
|
-
getRelayConnected: () => controlProviders.getRelayConnected(),
|
|
73
|
-
getConnectedRelay: () => controlProviders.getConnectedRelay(),
|
|
74
|
-
getDiagnostics: () => controlProviders.getDiagnostics(),
|
|
185
|
+
getProviderLines: null,
|
|
186
|
+
getLanTools: null,
|
|
75
187
|
log: warn,
|
|
76
|
-
onIdentityConflict: (...args) => {
|
|
77
|
-
const fn = controlProviders.onIdentityConflict
|
|
78
|
-
return typeof fn === 'function' ? fn(...args) : undefined
|
|
79
|
-
},
|
|
80
188
|
})
|
|
189
|
+
let controlCurrent = null // { fingerprint, client, store }
|
|
190
|
+
let controlPending = null // { fingerprint, create }
|
|
191
|
+
let controlStaged = null // 换代事务期暂存的来源
|
|
192
|
+
const controlFacade = makeFacade(() => (controlCurrent ? controlCurrent.client : null), { props: ['targetUrl'] })
|
|
193
|
+
const controlAdopt = ({ fingerprint, create } = {}) => {
|
|
194
|
+
if (typeof create !== 'function') return controlFacade
|
|
195
|
+
if (controlCurrent && controlCurrent.fingerprint === fingerprint) return controlFacade
|
|
196
|
+
if (controlPending && controlPending.fingerprint === fingerprint) return controlFacade
|
|
197
|
+
if (!controlCurrent) { const store = controlDefaults(); controlCurrent = { fingerprint, store, client: create(store) }; return controlFacade }
|
|
198
|
+
controlPending = { fingerprint, create }
|
|
199
|
+
return controlFacade
|
|
200
|
+
}
|
|
81
201
|
ctx.reflect.provide('dshMobileTransportControl', {
|
|
82
|
-
client
|
|
202
|
+
get client() { return controlFacade },
|
|
203
|
+
adopt: controlAdopt,
|
|
83
204
|
setProviders: p => {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
205
|
+
const patch = p && typeof p === 'object' ? p : {}
|
|
206
|
+
if (isSwapping()) { controlStaged = { ...(controlStaged || {}), ...patch }; return }
|
|
207
|
+
if (!controlCurrent) return
|
|
208
|
+
Object.assign(controlCurrent.store, patch)
|
|
209
|
+
controlCurrent.client.start()
|
|
210
|
+
controlCurrent.client.sync()
|
|
87
211
|
},
|
|
88
212
|
})
|
|
89
|
-
ctx.effect(() => () => controlClient.stop(), 'dsh-mobile-plugin: control transport host')
|
|
90
213
|
|
|
91
|
-
// ── 对话页长连接(mux
|
|
92
|
-
|
|
93
|
-
//
|
|
94
|
-
//
|
|
95
|
-
let
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
214
|
+
// ── 对话页长连接(mux,提交时才换服务 / 事务化注入鉴权) ────────────
|
|
215
|
+
let muxAuthorize = () => false
|
|
216
|
+
let muxCurrent = null // { fingerprint, service }
|
|
217
|
+
let muxPending = null // { fingerprint, build }
|
|
218
|
+
let muxStagedAuthorize = null
|
|
219
|
+
const muxFacade = makeFacade(() => (muxCurrent ? muxCurrent.service : null))
|
|
220
|
+
const buildMuxService = create => create({ upstream: { port: ctx.webServer.port }, authorize: req => muxAuthorize(req), onLog: message => { try { ctx.logger?.info?.(`dsh-mobile-plugin: ${message}`) } catch { /* ignore */ } } })
|
|
221
|
+
const muxAdopt = ({ fingerprint, create } = {}) => {
|
|
222
|
+
if (typeof create !== 'function') return muxFacade
|
|
223
|
+
if (muxCurrent && muxCurrent.fingerprint === fingerprint) return muxFacade
|
|
224
|
+
if (muxPending && muxPending.fingerprint === fingerprint) return muxFacade
|
|
225
|
+
if (!muxCurrent) { muxCurrent = { fingerprint, service: buildMuxService(create) }; return muxFacade }
|
|
226
|
+
muxPending = { fingerprint, build: () => buildMuxService(create) }
|
|
227
|
+
return muxFacade
|
|
228
|
+
}
|
|
101
229
|
ctx.reflect.provide('dshMobileTransportMux', {
|
|
102
|
-
service
|
|
103
|
-
|
|
230
|
+
get service() { return muxFacade },
|
|
231
|
+
/** 当前有几条手机长连接(自动更新用来判断「有没有人在用」)。服务还没起 / 老服务没有这个方法时按 0。 */
|
|
232
|
+
get activeMuxConnections() {
|
|
233
|
+
const svc = muxCurrent && muxCurrent.service
|
|
234
|
+
return svc && typeof svc.connectionCount === 'function' ? svc.connectionCount() : 0
|
|
235
|
+
},
|
|
236
|
+
adopt: muxAdopt,
|
|
237
|
+
setProviders: p => {
|
|
238
|
+
const fn = p && typeof p.authorize === 'function' ? p.authorize : null
|
|
239
|
+
if (!fn) return
|
|
240
|
+
if (isSwapping()) { muxStagedAuthorize = fn; return }
|
|
241
|
+
muxAuthorize = fn
|
|
242
|
+
},
|
|
104
243
|
})
|
|
105
244
|
try {
|
|
106
245
|
if (typeof ctx.webServer.registerUpgrade === 'function') {
|
|
107
|
-
ctx.effect(() => ctx.webServer.registerUpgrade({ path: '/api/mobile/v1/mux', handler:
|
|
246
|
+
ctx.effect(() => ctx.webServer.registerUpgrade({ path: '/api/mobile/v1/mux', handler: (req, socket, head) => { const s = muxCurrent ? muxCurrent.service : null; if (!s || typeof s.handleUpgrade !== 'function') { try { socket.destroy() } catch { /* ignore */ } return } return s.handleUpgrade(req, socket, head) }, auth: 'anonymous' }), 'dsh-mobile-plugin: mux upgrade route')
|
|
108
247
|
}
|
|
109
248
|
} catch { /* 宿主没有升级路由能力时这条不挂,App 走 HTTP/SSE 兜底 */ }
|
|
110
249
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
250
|
+
const commitControl = () => {
|
|
251
|
+
if (controlPending) {
|
|
252
|
+
const prev = controlCurrent
|
|
253
|
+
const fingerprint = controlPending.fingerprint
|
|
254
|
+
const create = controlPending.create
|
|
255
|
+
const store = { ...controlDefaults(), ...(controlStaged || {}) }
|
|
256
|
+
const client = create(store)
|
|
257
|
+
try { if (prev) prev.client.stop() } catch { /* 已经停了 */ }
|
|
258
|
+
try { client.start(); client.sync() } catch (error) {
|
|
259
|
+
// 新客户端起不来就别把没起来的设成当前(否则上报从此静默中断):退回旧客户端继续上报。
|
|
260
|
+
if (prev) { try { prev.client.start(); prev.client.sync() } catch { /* 尽力恢复 */ } controlCurrent = prev }
|
|
261
|
+
controlPending = null
|
|
262
|
+
controlStaged = null
|
|
263
|
+
warn('总控上报换客户端启动失败,保留旧客户端:', error?.message || String(error))
|
|
264
|
+
return
|
|
265
|
+
}
|
|
266
|
+
controlCurrent = { fingerprint, store, client }
|
|
267
|
+
controlPending = null
|
|
268
|
+
} else if (controlStaged && controlCurrent) {
|
|
269
|
+
Object.assign(controlCurrent.store, controlStaged)
|
|
270
|
+
try { controlCurrent.client.start(); controlCurrent.client.sync() } catch { /* ignore */ }
|
|
271
|
+
}
|
|
272
|
+
controlStaged = null
|
|
273
|
+
}
|
|
274
|
+
const commitMux = () => {
|
|
275
|
+
if (muxPending) {
|
|
276
|
+
const prev = muxCurrent
|
|
277
|
+
const fingerprint = muxPending.fingerprint
|
|
278
|
+
const build = muxPending.build
|
|
279
|
+
if (muxStagedAuthorize) muxAuthorize = muxStagedAuthorize
|
|
280
|
+
const service = build()
|
|
281
|
+
try { if (prev && prev.service !== service) prev.service.dropAll?.() } catch { /* ignore */ }
|
|
282
|
+
muxCurrent = { fingerprint, service }
|
|
283
|
+
muxPending = null
|
|
284
|
+
} else if (muxStagedAuthorize) {
|
|
285
|
+
muxAuthorize = muxStagedAuthorize
|
|
286
|
+
}
|
|
287
|
+
muxStagedAuthorize = null
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// ── 提交/回滚协调:运行时在换代开始 begin、提交成功 commit、失败 rollback ──
|
|
291
|
+
ctx.reflect.provide('dshMobileTransportSwap', {
|
|
292
|
+
begin: () => { inSwap = true },
|
|
293
|
+
commit: async () => {
|
|
294
|
+
try { await sidecarSlot.commit() } catch (error) { warn('sidecar 换代切换失败:', error?.message || String(error)) }
|
|
295
|
+
try { commitControl() } catch (error) { controlPending = null; controlStaged = null; warn('总控上报换代切换失败:', error?.message || String(error)) }
|
|
296
|
+
try { commitMux() } catch (error) { muxPending = null; muxStagedAuthorize = null; warn('mux 换代切换失败:', error?.message || String(error)) }
|
|
297
|
+
inSwap = false
|
|
298
|
+
},
|
|
299
|
+
rollback: () => {
|
|
300
|
+
try { sidecarSlot.rollback() } catch { /* ignore */ }
|
|
301
|
+
controlPending = null; controlStaged = null
|
|
302
|
+
muxPending = null; muxStagedAuthorize = null
|
|
303
|
+
inSwap = false
|
|
304
|
+
},
|
|
305
|
+
// boot/补齐里某代 adopt 建出实例后又装载失败:把三条主链路收回,回退代才能干净重建。
|
|
306
|
+
// 收 sidecar 要 await 停完,否则与回退代新建的 sidecar 抢同一个 LAN 端口。
|
|
307
|
+
abort: async () => {
|
|
308
|
+
try { await sidecarSlot.reset() } catch { /* ignore */ }
|
|
309
|
+
if (controlCurrent) { try { await controlCurrent.client.stop() } catch { /* ignore */ } }
|
|
310
|
+
controlCurrent = null; controlPending = null; controlStaged = null
|
|
311
|
+
if (muxCurrent) { try { await muxCurrent.service.dropAll?.() } catch { /* ignore */ } }
|
|
312
|
+
muxCurrent = null; muxPending = null; muxStagedAuthorize = null
|
|
313
|
+
inSwap = false
|
|
314
|
+
},
|
|
315
|
+
})
|
|
316
|
+
ctx.effect(() => () => { void sidecarSlot.dispose() }, 'dsh-mobile-plugin: sidecar transport host')
|
|
317
|
+
ctx.effect(() => () => { if (controlCurrent) { try { controlCurrent.client.stop() } catch { /* ignore */ } } }, 'dsh-mobile-plugin: control transport host')
|
|
318
|
+
ctx.effect(() => () => { if (muxCurrent) { try { muxCurrent.service.dropAll?.() } catch { /* ignore */ } } }, 'dsh-mobile-plugin: mux transport host')
|
|
319
|
+
|
|
320
|
+
// ── 隧道助手(直连隧道,可选线路:按指纹沿用/重建) ────────────────
|
|
115
321
|
const looseMount = (label, fn) => { try { return fn() } catch (error) { warn(`${label} 没挂上:${error && error.message ? error.message : error}`); return undefined } }
|
|
116
322
|
let tailcatDoor = null
|
|
117
323
|
let tailcatProviders = {}
|
|
324
|
+
let tailcatFingerprint = null
|
|
325
|
+
let tailcatDisposers = []
|
|
326
|
+
const tailcatCtlHolder = {
|
|
327
|
+
stop: () => { const d = tailcatDoor; return d && typeof d.stop === 'function' ? d.stop() : Promise.resolve() },
|
|
328
|
+
start: () => { const d = tailcatDoor; return d && typeof d.start === 'function' ? d.start() : undefined },
|
|
329
|
+
status: () => (tailcatDoor && typeof tailcatDoor.status === 'function' ? tailcatDoor.status() : null),
|
|
330
|
+
}
|
|
331
|
+
ctx.reflect.provide('dshMobileTailcatCtl', tailcatCtlHolder)
|
|
332
|
+
const disposeTailcat = () => {
|
|
333
|
+
const door = tailcatDoor
|
|
334
|
+
const disposers = tailcatDisposers
|
|
335
|
+
tailcatDoor = null
|
|
336
|
+
tailcatDisposers = []
|
|
337
|
+
for (const dispose of disposers) { try { dispose() } catch { /* 已经停了 */ } }
|
|
338
|
+
try { if (door && typeof door.stop === 'function') void door.stop() } catch { /* 已经停了 */ }
|
|
339
|
+
}
|
|
340
|
+
const tailcatBag = {
|
|
341
|
+
effect: (fn, label) => { const dispose = ctx.effect(fn, label); tailcatDisposers.push(dispose); return dispose },
|
|
342
|
+
reflect: { provide: (n, o) => { if (n === 'dshMobileTailcatCtl') return; try { ctx.reflect.provide(n, o) } catch { /* 已提供就交回壳里的稳定门面 */ } } },
|
|
343
|
+
}
|
|
118
344
|
ctx.reflect.provide('dshMobileTransportTailcat', {
|
|
119
|
-
ensure: providers => {
|
|
345
|
+
ensure: ({ providers, mount, fingerprint } = {}) => {
|
|
120
346
|
if (providers && typeof providers === 'object') tailcatProviders = { ...tailcatProviders, ...providers }
|
|
347
|
+
if (typeof mount !== 'function') return tailcatDoor
|
|
348
|
+
if (tailcatDoor && fingerprint === tailcatFingerprint) return tailcatDoor
|
|
349
|
+
disposeTailcat()
|
|
121
350
|
const pick = name => (typeof tailcatProviders[name] === 'function' ? tailcatProviders[name] : null)
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
}
|
|
351
|
+
tailcatDoor = mount({
|
|
352
|
+
mount: looseMount,
|
|
353
|
+
warn,
|
|
354
|
+
getState: () => (pick('getState') ? pick('getState')() : {}),
|
|
355
|
+
saveState: state => { const fn = pick('saveState'); if (fn) fn(state) },
|
|
356
|
+
lanEnabled: () => (pick('lanEnabled') ? pick('lanEnabled')() : false),
|
|
357
|
+
lanPort: () => (pick('lanPort') ? pick('lanPort')() : 0),
|
|
358
|
+
webPort: () => (pick('webPort') ? pick('webPort')() : 0),
|
|
359
|
+
webHost: () => (pick('webHost') ? pick('webHost')() : '127.0.0.1'),
|
|
360
|
+
hostName: () => (pick('hostName') ? pick('hostName')() : ''),
|
|
361
|
+
ctx: tailcatBag,
|
|
362
|
+
name: 'dsh-mobile-plugin',
|
|
363
|
+
})
|
|
364
|
+
tailcatFingerprint = fingerprint
|
|
137
365
|
return tailcatDoor
|
|
138
366
|
},
|
|
139
367
|
})
|
|
368
|
+
ctx.effect(() => () => disposeTailcat(), 'dsh-mobile-plugin: tailcat transport host')
|
|
140
369
|
|
|
141
|
-
// ──
|
|
142
|
-
// 与隧道助手同一套惰性 ensure:不碰它就不起端口/会话;换代只更新来源。
|
|
370
|
+
// ── 点对点隧道(可选线路:按指纹沿用/重建) ───────────────────────
|
|
143
371
|
let p2pDoor = null
|
|
144
372
|
let p2pProviders = {}
|
|
373
|
+
let p2pFingerprint = null
|
|
374
|
+
let p2pDisposers = []
|
|
375
|
+
const disposeP2P = () => {
|
|
376
|
+
const door = p2pDoor
|
|
377
|
+
const disposers = p2pDisposers
|
|
378
|
+
p2pDoor = null
|
|
379
|
+
p2pDisposers = []
|
|
380
|
+
for (const dispose of disposers) { try { dispose() } catch { /* 已经停了 */ } }
|
|
381
|
+
try { if (door && typeof door.stop === 'function') void door.stop() } catch { /* 已经停了 */ }
|
|
382
|
+
}
|
|
383
|
+
const p2pBag = {
|
|
384
|
+
effect: (fn, label) => { const dispose = ctx.effect(fn, label); p2pDisposers.push(dispose); return dispose },
|
|
385
|
+
}
|
|
145
386
|
ctx.reflect.provide('dshMobileTransportP2P', {
|
|
146
|
-
ensure: providers => {
|
|
387
|
+
ensure: ({ providers, mount, fingerprint } = {}) => {
|
|
147
388
|
if (providers && typeof providers === 'object') p2pProviders = { ...p2pProviders, ...providers }
|
|
389
|
+
if (typeof mount !== 'function') return p2pDoor
|
|
390
|
+
if (p2pDoor && fingerprint === p2pFingerprint) return p2pDoor
|
|
391
|
+
disposeP2P()
|
|
148
392
|
const pick = name => (typeof p2pProviders[name] === 'function' ? p2pProviders[name] : null)
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
}
|
|
393
|
+
p2pDoor = mount({
|
|
394
|
+
mount: looseMount,
|
|
395
|
+
warn,
|
|
396
|
+
getState: () => (pick('getState') ? pick('getState')() : {}),
|
|
397
|
+
saveState: state => { const fn = pick('saveState'); if (fn) fn(state) },
|
|
398
|
+
localPort: () => (pick('localPort') ? pick('localPort')() : 0),
|
|
399
|
+
lanEnabled: () => (pick('lanEnabled') ? pick('lanEnabled')() : false),
|
|
400
|
+
lanPort: () => (pick('lanPort') ? pick('lanPort')() : 0),
|
|
401
|
+
webPort: () => (pick('webPort') ? pick('webPort')() : 0),
|
|
402
|
+
webHost: () => (pick('webHost') ? pick('webHost')() : '127.0.0.1'),
|
|
403
|
+
ctx: p2pBag,
|
|
404
|
+
name: 'dsh-mobile-plugin',
|
|
405
|
+
})
|
|
406
|
+
p2pFingerprint = fingerprint
|
|
164
407
|
return p2pDoor
|
|
165
408
|
},
|
|
166
409
|
})
|
|
410
|
+
ctx.effect(() => () => disposeP2P(), 'dsh-mobile-plugin: p2p transport host')
|
|
167
411
|
|
|
168
|
-
return {
|
|
412
|
+
return { get sidecarLifecycle() { return sidecarSlot.current }, get frpRunner() { return frpRunnerOf() }, get controlClient() { return controlCurrent ? controlCurrent.client : null }, tailcatDoor: () => tailcatDoor, p2pDoor: () => p2pDoor }
|
|
169
413
|
}
|