ssh-net 26.10.35 → 26.10.37
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/CHANGELOG.md +51 -0
- package/libexec/cli.mjs +1 -1
- package/libexec/commands/doctor.mjs +2 -2
- package/libexec/commands/install.mjs +20 -25
- package/libexec/commands/register.mjs +15 -14
- package/libexec/commands/start.mjs +0 -1
- package/libexec/commands/status.mjs +38 -36
- package/libexec/commands/stop.mjs +5 -3
- package/libexec/commands/uninstall.mjs +13 -13
- package/libexec/lib/jobctl.mjs +5 -7
- package/libexec/lib/launchd.mjs +9 -9
- package/libexec/lib/manifest.mjs +54 -19
- package/libexec/lib/paths.mjs +2 -0
- package/libexec/lib/probe.mjs +23 -8
- package/libexec/lib/render.mjs +4 -4
- package/libexec/lib/runtime.mjs +15 -9
- package/libexec/lib/systemd.mjs +5 -6
- package/libexec/sn-route +57 -11
- package/libexec/ssh-net-uplink-edge +48 -58
- package/libexec/ssh-net-uplink-relay +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,57 @@
|
|
|
7
7
|
GitHub Release(Actions 自动跑测试后 npm publish;跳号与 0.x 的完整
|
|
8
8
|
考据见 git 史)。
|
|
9
9
|
|
|
10
|
+
## [26.10.37] - 2026-10-03
|
|
11
|
+
|
|
12
|
+
### 短睡开盖恢复提速:本地清尸 + 回拨重试收敛
|
|
13
|
+
|
|
14
|
+
- **sn-route 僵尸口本地清尸**:edge 睡一分钟级开盖,旧回拨会话在
|
|
15
|
+
sshd 侧要 90 秒才回收,第一次重连必走中继、再等 core 30 秒重拨
|
|
16
|
+
才恢复直连(30–40 秒)。现在 sn-route 能认出「端口在但读不到
|
|
17
|
+
banner」的僵尸会话,过软关/印记两关后在本地毫秒级清掉它再让
|
|
18
|
+
core 重拨——恢复压到 3–4 秒,开盖后第一次重连即有机会直接走
|
|
19
|
+
peer。持口者实测是登录用户自己的 sshd-session(macOS 27 /
|
|
20
|
+
OpenSSH 10.3),三重限定(本端口、LISTEN、进程名 sshd*)防误杀;
|
|
21
|
+
软关 peer 与失败印记窗口绝不动手,清理失败静默退回旧行为
|
|
22
|
+
- **uplink-edge 改两拨模型**:core 回拨去掉「六次失败计数 + 30 秒
|
|
23
|
+
清零定时器 + 30 秒退避」整套——按需架构下唤醒只靠 edge 报名,
|
|
24
|
+
循环不再负责唤醒。现在拨号失败后 5 秒重试一次,再败即 disable
|
|
25
|
+
休眠等下次报名:合盖收敛约 5.5 分钟 6 次拨号 → 约 2 分钟 2 次,
|
|
26
|
+
事故期日志噪声大减。保留一次重试是为吸收开盖后上游 v6 路由几秒
|
|
27
|
+
钟的收敛差,避免 VS Code 已粘在中继上时回拨无人唤醒
|
|
28
|
+
- **ConnectTimeout 15→5(三个常驻隧道与报名 ssh 统一)**:它只管
|
|
29
|
+
TCP 握手,IPv6 直连健康路径毫秒级;黑洞时 5 秒覆盖 3 次 SYN 与
|
|
30
|
+
NDP 3×1 秒。保活参数 35s×3 不动——短睡恢复已不靠它,调短只会
|
|
31
|
+
增加漫游瞬断误杀
|
|
32
|
+
- **测试**:清尸组 +5 例(清尸、非 sshd 不杀、软关/印记不动手、
|
|
33
|
+
lsof 失败退化);uplink-edge 删 2 例加 3 例(二败休眠、二拨成功
|
|
34
|
+
常驻、退避中 TERM 立即退出不休眠);全套 218 绿
|
|
35
|
+
|
|
36
|
+
升级:edge `npm i -g ssh-net@latest` 后 sn-route 下条连接即生效;
|
|
37
|
+
core 侧 uplink-edge 脚本在下次 edge 报名时自动对账,或在 core 重跑
|
|
38
|
+
`ssh-net install` 立即生效。
|
|
39
|
+
|
|
40
|
+
## [26.10.36] - 2026-10-03
|
|
41
|
+
|
|
42
|
+
### 精简:清单单一来源补齐 + 一个报名竞态修复
|
|
43
|
+
|
|
44
|
+
- **uplink-edge 组件清单三处合一**:core install、`_register` 报名对账、
|
|
45
|
+
status 体检此前各抄一份实体/软链知识,收敛进 manifest
|
|
46
|
+
(`edgeCallbacksPlan`/`linkAddonPlan`),`install --without-link` 的
|
|
47
|
+
删除也由清单驱动;脚本名改常量引用,改名一处即可
|
|
48
|
+
- **报名 ssh 两源对齐**:`start peer` 的 ConnectTimeout 15 与额外保活
|
|
49
|
+
选项是相对 design 冻结表(5s)的静默漂移,改为与 sn-route 现场报名
|
|
50
|
+
完全同形(外层 60s timeout 仍兜底挂死)
|
|
51
|
+
- **sn-route 单飞竞态修复**:leader 报名秒败删锁的瞬间,晚启动的
|
|
52
|
+
follower 可能夹在「印记已写、锁已删」之间误接管、重复报名;抢空锁后
|
|
53
|
+
复查 PROBE_FAIL,新鲜则认 follower 不报名
|
|
54
|
+
- **退役产物清理统一**:bin/state/conf.d 三类退役清单合并为 STALE_FILES;
|
|
55
|
+
stop 的进程收束不再偷删开关(策略回命令层);lsof 预检两跑合一;
|
|
56
|
+
launchd/systemd 若干死代码清理
|
|
57
|
+
- **测试**:命令测试沙箱收敛为 makeCommandHarness(t.after 自动回收,
|
|
58
|
+
失败不泄漏临时目录)、fixture 加载单一入口、镜像脚手架合并;新增报名
|
|
59
|
+
argv/label 公式/片段名三处跨语言锚测试;全套 9.6s→7.5s
|
|
60
|
+
|
|
10
61
|
## [26.10.35] - 2026-10-03
|
|
11
62
|
|
|
12
63
|
### 精简:控制面重复收敛(纯内部重构,对外行为不变)
|
package/libexec/cli.mjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// show:它会 probe、经 relay 报名、等回拨(6s 预算),进度直接显示在终端。
|
|
3
3
|
// doctor 只负责关态分诊与结果转述。不新增状态、不踢常驻服务(默认形态没有
|
|
4
4
|
// 常驻)。仅 edge 有动作:core 的恢复靠 edge 报名,node 由 systemd 自动重启。
|
|
5
|
-
import
|
|
5
|
+
import { SN_ROUTE } from '../lib/manifest.mjs';
|
|
6
6
|
import { snRouteArgv } from '../lib/render.mjs';
|
|
7
7
|
|
|
8
8
|
export async function run(ctx) {
|
|
@@ -19,7 +19,7 @@ export async function run(ctx) {
|
|
|
19
19
|
// stdio:'inherit':sn-route 进度直达终端,同时其 [ -t 2 ] 才成立;
|
|
20
20
|
// timeout 略大于 6s 预算
|
|
21
21
|
const r = ctx.runner.runRaw(
|
|
22
|
-
|
|
22
|
+
paths.binScript(SN_ROUTE),
|
|
23
23
|
snRouteArgv(topo, self.alias, 'show'),
|
|
24
24
|
{ stdio: 'inherit', timeout: 8000 },
|
|
25
25
|
);
|
|
@@ -13,16 +13,19 @@ import { ensureRuntime } from '../lib/runtime.mjs';
|
|
|
13
13
|
import * as launchd from '../lib/launchd.mjs';
|
|
14
14
|
import * as systemd from '../lib/systemd.mjs';
|
|
15
15
|
import {
|
|
16
|
-
classifyLocalPort,
|
|
16
|
+
classifyLocalPort, lsofListen, v6LoopbackPids, processTree,
|
|
17
17
|
peerProbeOk,
|
|
18
18
|
} from '../lib/probe.mjs';
|
|
19
19
|
import { writeFileAtomic } from '../lib/fsutil.mjs';
|
|
20
|
-
import {
|
|
20
|
+
import {
|
|
21
|
+
SN_PROBE, edgeRuntimePlan, relayRuntimePlan,
|
|
22
|
+
edgeCallbacksPlan, linkAddonPlan, mergePlans,
|
|
23
|
+
} from '../lib/manifest.mjs';
|
|
21
24
|
import { edgeFragmentContent, edgeFragmentShouldExist } from '../lib/registration.mjs';
|
|
22
25
|
import {
|
|
23
|
-
linkLabel,
|
|
26
|
+
linkLabel,
|
|
24
27
|
relayLabel, relayUnitName,
|
|
25
|
-
edgeLabel,
|
|
28
|
+
edgeLabel,
|
|
26
29
|
plistFileName,
|
|
27
30
|
renderEdgeLinkPlist, renderRelayPlist, renderRelayUnit, renderRoutes,
|
|
28
31
|
} from '../lib/render.mjs';
|
|
@@ -60,8 +63,7 @@ async function precheckEdgePorts(ctx, linkOn) {
|
|
|
60
63
|
for (const port of forwardPorts) {
|
|
61
64
|
const c = classifyLocalPort(runner, port, jobPid, tree);
|
|
62
65
|
if (c.kind === 'foreign') {
|
|
63
|
-
|
|
64
|
-
throw new Error(`端口 ${port} 被非本服务进程占用(pid ${c.pids.join(',')}):\n${who}`);
|
|
66
|
+
throw new Error(`端口 ${port} 被非本服务进程占用(pid ${c.pids.join(',')}):\n${lsofListen(runner, port)}`);
|
|
65
67
|
}
|
|
66
68
|
}
|
|
67
69
|
}
|
|
@@ -69,16 +71,16 @@ async function precheckEdgePorts(ctx, linkOn) {
|
|
|
69
71
|
// 10022 持口者永远是本机 sshd,无法按 pid 溯源:读到 SSH banner 才认。
|
|
70
72
|
// 只盯 [::1](peer 全 v6 环回后的本服务监听面;127.0.0.1:10022 的持有
|
|
71
73
|
// 者如 VS Code 本地转发与本服务可共存,2026-09-30 起不判冲突——edge 实案)。
|
|
72
|
-
//
|
|
73
|
-
|
|
74
|
+
// 一次 lsof 同时拿持有者 pid 与报错展示文本;首装时 bin/sn-probe 还没拷上,
|
|
75
|
+
// 探针缺失不判僵尸(install 随后就拷)
|
|
76
|
+
const listeners = lsofListen(runner, CALLBACK_PORT);
|
|
74
77
|
if (
|
|
75
|
-
|
|
76
|
-
&& fs.existsSync(
|
|
78
|
+
v6LoopbackPids(listeners, CALLBACK_PORT).length > 0
|
|
79
|
+
&& fs.existsSync(paths.binScript(SN_PROBE))
|
|
77
80
|
&& !(await peerProbeOk(runner, paths.binDir))
|
|
78
81
|
) {
|
|
79
|
-
const who = runner.run('lsof', ['-nP', `-iTCP:${CALLBACK_PORT}`, '-sTCP:LISTEN']).stdout.trim();
|
|
80
82
|
throw new Error(
|
|
81
|
-
`端口 ${CALLBACK_PORT} 在听但读不到 SSH banner(僵尸监听或他进程占用):\n${
|
|
83
|
+
`端口 ${CALLBACK_PORT} 在听但读不到 SSH banner(僵尸监听或他进程占用):\n${listeners}`,
|
|
82
84
|
);
|
|
83
85
|
}
|
|
84
86
|
}
|
|
@@ -140,18 +142,14 @@ async function installEdge(ctx, args) {
|
|
|
140
142
|
// 默认零常驻:清掉旧版/加装过的 link 全部产物。升级时旧 plist 必须
|
|
141
143
|
// bootout,否则 launchd 每 ThrottleInterval 刷 ENOENT
|
|
142
144
|
removeJob(ctx, label, plistPath);
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
fs.rmSync(path.join(paths.stateDir, 'ROUTE_PEER'), { force: true });
|
|
147
|
-
fs.rmSync(path.join(paths.stateDir, 'LINK_ON'), { force: true });
|
|
145
|
+
const addon = linkAddonPlan(topo);
|
|
146
|
+
for (const [name] of [...addon.entities, ...addon.links])
|
|
147
|
+
fs.rmSync(paths.binScript(name), { force: true });
|
|
148
148
|
}
|
|
149
149
|
|
|
150
150
|
// conf.d 片段(design §4.2):edge 是 routes 的唯一安装角色——routes 含
|
|
151
151
|
// 选路 Match 与尾部 VS Code 打标块;新签名带本机别名
|
|
152
152
|
const routesChanged = syncConfFile(paths.routesFile, renderRoutes(topo, ctx.self.alias));
|
|
153
|
-
// 打标块并入 routes 后的旧文件清理(26.10.32 退役;按约定一个月后删本行)
|
|
154
|
-
fs.rmSync(path.join(paths.confDir, 'vscode-mark'), { force: true });
|
|
155
153
|
const fragMsg = routesChanged ? '(选路片段已更新)' : '';
|
|
156
154
|
if (!comp && !routesChanged && flag === undefined) {
|
|
157
155
|
return linkOn
|
|
@@ -170,11 +168,8 @@ function installCore(ctx) {
|
|
|
170
168
|
// 软链):否则 upgrade 后 core status 要对每台未联系的 edge 报组件异常,
|
|
171
169
|
// 直到它下次报名才自愈(2026-09-30 补)
|
|
172
170
|
const edges = launchd.edgeAliasesOnDisk(paths.launchAgentsDir);
|
|
173
|
-
// relay 基础形态 + 磁盘在册 edge
|
|
174
|
-
const plan = relayRuntimePlan(topo);
|
|
175
|
-
if (edges.length > 0)
|
|
176
|
-
plan.entities.push(['ssh-net-uplink-edge', 'ssh-net-uplink-edge']);
|
|
177
|
-
plan.links.push(...edges.map((alias) => [edgeLinkName(alias), 'ssh-net-uplink-edge']));
|
|
171
|
+
// relay 基础形态 + 磁盘在册 edge 的回拨组件(清单与报名/status 体检同源)
|
|
172
|
+
const plan = mergePlans(relayRuntimePlan(topo), edgeCallbacksPlan(edges));
|
|
178
173
|
const comp = compNote(ensureRuntime(ctx, plan.entities, plan.links));
|
|
179
174
|
const label = relayLabel(topo.relay.alias);
|
|
180
175
|
const content = renderRelayPlist({
|
|
@@ -235,7 +230,7 @@ function installNode(ctx) {
|
|
|
235
230
|
* - 三者齐备:从 state 纯渲染(缺失补、过期改、相同不动)。这是手删/磁盘
|
|
236
231
|
* 丢失的恢复通道——peer 稳态的 link 循环不报名,等不到 _register 自然补写
|
|
237
232
|
* - job 未注册(state 只有历史 v6,机器从这台 core 没建成过回拨)或处于
|
|
238
|
-
*
|
|
233
|
+
* 休眠(拨号失败收尸)或 state 无 v6:删片段——没有 job 兜底的入口是指向
|
|
239
234
|
* 陈旧 v6 的假入口,且永远等不到休眠收尸
|
|
240
235
|
* - topology 已删 edge(改名/退役):删片段;state 同名残留不动(既有
|
|
241
236
|
* 现象,uninstall 才整棵清)
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
// 等连接」语义——同 v6 的稳态报名也无条件 enable+kickstart,回拨立即重拨、
|
|
10
10
|
// 失败计数清零;定时器/周期报名不存在,无需区分「程序同步」与「人触发」。
|
|
11
11
|
// 先对账运行时脚本与 plist——uplink-edge 的脚本与 job 参数(user 等)变更
|
|
12
|
-
//
|
|
12
|
+
// 只有这条更新通道,缺了会永远用旧参数/旧目标拨号、拨号失败后休眠。
|
|
13
13
|
// launchctl 真返回成功才结束;失败 edge 下次连接自然重试。
|
|
14
14
|
import path from 'node:path';
|
|
15
15
|
import { explainV6Error } from '../lib/ipv6.mjs';
|
|
@@ -21,15 +21,16 @@ import { readFileOrNull } from '../lib/fsutil.mjs';
|
|
|
21
21
|
import {
|
|
22
22
|
writeEdgeRegistration, removeEdgeRegistration,
|
|
23
23
|
} from '../lib/registration.mjs';
|
|
24
|
+
import { edgeCallbacksPlan } from '../lib/manifest.mjs';
|
|
24
25
|
import {
|
|
25
|
-
edgeLabel,
|
|
26
|
+
edgeLabel, plistFileName, renderUplinkEdgePlist, coreViaRelayArgs,
|
|
26
27
|
} from '../lib/render.mjs';
|
|
27
28
|
|
|
28
29
|
/** edge 侧报名:取 v6 + 命令行 -J 经 relay 调 core 的 _register。
|
|
29
30
|
* start peer 用——控制面直接报一次名让 core 马上回拨。 */
|
|
30
31
|
function runEdgeAnnounce(ctx) {
|
|
31
32
|
const probe = ctx.runner.runRaw(
|
|
32
|
-
|
|
33
|
+
ctx.paths.binScript('sn-ipv6'), [], { timeout: 5000 },
|
|
33
34
|
);
|
|
34
35
|
// bin/sn-ipv6 缺失(未 install)别误报成「无 IPv6」。fatal 哨兵属性:
|
|
35
36
|
// enablePeer 靠它区分「没有现场选路可指望」的致命错与可重试的报名失败,
|
|
@@ -54,12 +55,12 @@ function runEdgeAnnounce(ctx) {
|
|
|
54
55
|
const v6Err = explainV6Error(v6);
|
|
55
56
|
if (v6Err) throw new Error(`本机 v6 选取异常(sn-ipv6 输出不是可报名地址:${v6Err})`);
|
|
56
57
|
// 报名固定走 relay(relay 7×24 在;peer 僵尸时自动选路会把报名一起挂死)。
|
|
57
|
-
//
|
|
58
|
-
//
|
|
58
|
+
// argv 与 sn-route 的现场报名完全同一形态(coreViaRelayArgs 默认 ConnectTimeout
|
|
59
|
+
// 5,design §3 冻结):无人值守首连信任 accept-new(探测侧 status 不带它,
|
|
60
|
+
// 差异在 coreViaRelayArgs 显式)+ IdentitiesOnly 防多 key 试错撞 MaxAuthTries
|
|
61
|
+
//(design §4.3)。挂死由外层 60s timeout 兜底
|
|
59
62
|
const r = ctx.runner.run('ssh', coreViaRelayArgs(ctx.topo, {
|
|
60
|
-
connectTimeout: 15,
|
|
61
63
|
acceptHost: true,
|
|
62
|
-
extraOpts: ['-o', 'ServerAliveInterval=10', '-o', 'ServerAliveCountMax=2'],
|
|
63
64
|
remoteCommand: `ssh-net _register ${ctx.self.alias} ${v6}`,
|
|
64
65
|
}), { timeout: 60_000 });
|
|
65
66
|
if (r.code !== 0)
|
|
@@ -70,7 +71,7 @@ function runEdgeAnnounce(ctx) {
|
|
|
70
71
|
|
|
71
72
|
/** kickstart 失败时把登记回滚到 job 实际在拨的 v6(null=两处都删)。
|
|
72
73
|
* 不回滚的话磁盘已是新 v6,下次报名若进程已被踢活会拿磁盘值当真,
|
|
73
|
-
* job 实际还拨旧 v6
|
|
74
|
+
* job 实际还拨旧 v6 拨到失败休眠。 */
|
|
74
75
|
function rollbackEdgeRegistration(ctx, edge, prevV6) {
|
|
75
76
|
if (prevV6 === null) removeEdgeRegistration(ctx, edge.alias);
|
|
76
77
|
else writeEdgeRegistration(ctx, edge, prevV6);
|
|
@@ -141,9 +142,8 @@ export function runRegister(ctx, args = []) {
|
|
|
141
142
|
|
|
142
143
|
// 脚本对账无条件先行(幂等):脚本被删/过期/参数变更时,四个状态分支
|
|
143
144
|
// 里的任何一个都可能成为重建通道,不能只靠首次报名那一次
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
[[edgeLinkName(alias), 'ssh-net-uplink-edge']]);
|
|
145
|
+
const plan = edgeCallbacksPlan([alias]);
|
|
146
|
+
ensureRuntime(ctx, plan.entities, plan.links);
|
|
147
147
|
|
|
148
148
|
// 未注册(首次报名)或已注册但渲染结果与磁盘不符(topology 的 user 等变了):
|
|
149
149
|
// 重新部署。syncPlist 走 bootout→落盘→enable→bootstrap,bootstrap 即首发,
|
|
@@ -168,11 +168,12 @@ export function runRegister(ctx, args = []) {
|
|
|
168
168
|
// 休眠中(disable 睡着)或 enabled(running 且 v6 相同 / v6 变了 /
|
|
169
169
|
// not-running 三种形态合并):写 v6 → enable(休眠唤醒要清 override;
|
|
170
170
|
// enabled 幂等无害)→ kickstart。报名只会由活 edge 上的真实 ssh 触发,
|
|
171
|
-
//
|
|
171
|
+
// 无条件重拨正是人等连接时要的语义,进程重启即重置重试额度(首拨 +
|
|
172
|
+
// 一次重试两次机会重新计)。enable
|
|
172
173
|
// 失败不用回滚(disabled 态没变,下轮报名照走本分支)。两处同写先于
|
|
173
|
-
// kickstart:顺带 reconcile conf.d 片段(uplink-edge
|
|
174
|
+
// kickstart:顺带 reconcile conf.d 片段(uplink-edge 拨号失败后休眠删片段与
|
|
174
175
|
// 报名存在删/写竞态,缺则补)。kickstart 失败回滚 state 到 job 实际在用
|
|
175
|
-
// 的 v6:不回滚则磁盘已是新值,job 仍拿旧 v6
|
|
176
|
+
// 的 v6:不回滚则磁盘已是新值,job 仍拿旧 v6 拨到失败休眠
|
|
176
177
|
writeEdgeRegistration(ctx, edge, v6);
|
|
177
178
|
const en = launchd.enable(ctx.runner, ctx.uid, label);
|
|
178
179
|
if (en.code !== 0) throw new Error(`enable 失败:${en.stderr.trim()}`);
|
|
@@ -5,7 +5,6 @@
|
|
|
5
5
|
// 状态」。
|
|
6
6
|
import fs from 'node:fs';
|
|
7
7
|
import path from 'node:path';
|
|
8
|
-
import * as launchd from '../lib/launchd.mjs';
|
|
9
8
|
import * as systemd from '../lib/systemd.mjs';
|
|
10
9
|
import { ensureLoaded } from '../lib/manage.mjs';
|
|
11
10
|
import { assertUnitSwitchArgs } from '../lib/cliargs.mjs';
|
|
@@ -7,7 +7,6 @@
|
|
|
7
7
|
// bin/ 体检(2026-09-30 软链方案):实体逐字节比对 + 服务软链指向/悬空检查。
|
|
8
8
|
// 退出码:健康(含 edge 关闭态)0;有可行动问题 1。
|
|
9
9
|
import fs from 'node:fs';
|
|
10
|
-
import path from 'node:path';
|
|
11
10
|
import {
|
|
12
11
|
auditScript, auditLink, runtimeVersionIssue,
|
|
13
12
|
} from '../lib/runtime.mjs';
|
|
@@ -16,11 +15,14 @@ import * as launchd from '../lib/launchd.mjs';
|
|
|
16
15
|
import * as systemd from '../lib/systemd.mjs';
|
|
17
16
|
import { peerProbeOk, tcpOpen } from '../lib/probe.mjs';
|
|
18
17
|
import { readFileOrNull } from '../lib/fsutil.mjs';
|
|
19
|
-
import {
|
|
18
|
+
import {
|
|
19
|
+
SN_ROUTE, SN_IPV6, UPLINK_EDGE,
|
|
20
|
+
edgeRuntimePlan, relayRuntimePlan, edgeCallbacksPlan,
|
|
21
|
+
} from '../lib/manifest.mjs';
|
|
20
22
|
import { edgeFragmentContent, edgeFragmentShouldExist } from '../lib/registration.mjs';
|
|
21
23
|
import {
|
|
22
24
|
linkLabel, relayLabel, relayUnitName,
|
|
23
|
-
edgeLabel,
|
|
25
|
+
edgeLabel,
|
|
24
26
|
renderRoutes, coreViaRelayArgs,
|
|
25
27
|
} from '../lib/render.mjs';
|
|
26
28
|
|
|
@@ -34,13 +36,17 @@ function auditConfFragment(file, expected) {
|
|
|
34
36
|
return text === expected ? 'ok' : 'stale';
|
|
35
37
|
}
|
|
36
38
|
|
|
37
|
-
/**
|
|
39
|
+
/**
|
|
40
|
+
* 各角色共用的实体+软链体检。
|
|
41
|
+
* @returns {{issues:string[], entityAudits:{name:string,audit:string}[]}}
|
|
42
|
+
* issues 是人话问题列表(空=健康)
|
|
43
|
+
*/
|
|
38
44
|
function runtimeIssues(ctx, { entities, links }) {
|
|
39
45
|
const issues = [];
|
|
40
46
|
// 实体逐字节比对(name 是 bin 实体名,不是软链)
|
|
41
47
|
const entityAudits = entities.map(([name, script]) => ({
|
|
42
48
|
name,
|
|
43
|
-
audit: auditScript(
|
|
49
|
+
audit: auditScript(ctx.paths.binScript(name), packageScriptPath(script)),
|
|
44
50
|
}));
|
|
45
51
|
if (entityAudits.some((a) => a.audit === 'stale'))
|
|
46
52
|
issues.push('隧道组件与所装版本不一致——跑一次 ssh-net install 刷新');
|
|
@@ -50,13 +56,23 @@ function runtimeIssues(ctx, { entities, links }) {
|
|
|
50
56
|
// 部分缺失:探针丢了会让开着的隧道恒判中继,不能只显示不报
|
|
51
57
|
issues.push(`隧道组件不完整(缺 ${entityAudits.filter((a) => a.audit === 'missing').map((a) => a.name).join('、')})——跑一次 ssh-net install 修复`);
|
|
52
58
|
for (const [linkName, entityName] of links) {
|
|
53
|
-
const r = auditLink(
|
|
59
|
+
const r = auditLink(ctx.paths.binScript(linkName), entityName);
|
|
54
60
|
if (r !== 'ok')
|
|
55
61
|
issues.push(`隧道组件异常(${linkName}:${r})——跑一次 ssh-net install 修复`);
|
|
56
62
|
}
|
|
57
63
|
return { issues, entityAudits };
|
|
58
64
|
}
|
|
59
65
|
|
|
66
|
+
/** 三个 collector 的共同前奏:版本戳问题 + 实体/软链体检。 */
|
|
67
|
+
function baseAudit(ctx, plan) {
|
|
68
|
+
const issues = [];
|
|
69
|
+
const vIssue = runtimeVersionIssue(ctx.paths);
|
|
70
|
+
if (vIssue) issues.push(vIssue);
|
|
71
|
+
const { issues: rtIssues, entityAudits } = runtimeIssues(ctx, plan);
|
|
72
|
+
issues.push(...rtIssues);
|
|
73
|
+
return { issues, entityAudits };
|
|
74
|
+
}
|
|
75
|
+
|
|
60
76
|
/** forward 的人话标签:写出目标(loopback 是落地机环回)。 */
|
|
61
77
|
function forwardLabel(f) {
|
|
62
78
|
const target = f.targetAlias === 'loopback'
|
|
@@ -104,20 +120,16 @@ async function relayPathOk(ctx) {
|
|
|
104
120
|
|
|
105
121
|
async function collectEdge(ctx) {
|
|
106
122
|
const { topo, paths, state } = ctx;
|
|
107
|
-
const issues = [];
|
|
108
|
-
const vIssue = runtimeVersionIssue(ctx.paths);
|
|
109
|
-
if (vIssue) issues.push(vIssue);
|
|
110
123
|
// link 附加件以 marker 为准:默认只有 3 实体、无服务软链;装了才加
|
|
111
124
|
// link-core(转发口只取决于装没装,不是运行时开关)
|
|
112
125
|
const linkInstalled = fs.existsSync(paths.linkEnabledFile);
|
|
113
126
|
const plan = edgeRuntimePlan(topo, linkInstalled);
|
|
114
|
-
const { issues
|
|
115
|
-
issues.push(...rtIssues);
|
|
127
|
+
const { issues, entityAudits } = baseAudit(ctx, plan);
|
|
116
128
|
|
|
117
129
|
// routes 体检(sn-route 实体在才查:没装时 rtIssues 已报「尚未安装」):
|
|
118
130
|
// routes 被手删/过期会让 ssh 静默退回 lan 直连,是故障、进 issues。文件尾部
|
|
119
131
|
// 的 VS Code 打标块同属这份内容,逐字节对账一并覆盖,不用单独体检
|
|
120
|
-
if (entityAudits.some((a) => a.name ===
|
|
132
|
+
if (entityAudits.some((a) => a.name === SN_ROUTE && a.audit === 'ok')) {
|
|
121
133
|
const routeAudit = auditConfFragment(paths.routesFile, renderRoutes(topo, ctx.self.alias));
|
|
122
134
|
if (routeAudit !== 'ok')
|
|
123
135
|
issues.push(`选路片段 conf.d/routes ${routeAudit === 'missing' ? '缺失' : '与 topology 不一致'}——跑一次 ssh-net install 刷新`);
|
|
@@ -139,9 +151,9 @@ async function collectEdge(ctx) {
|
|
|
139
151
|
// 探测没跑成(超时/非零退出/实体缺失)时不能谎称「没有 IPv6」——那个
|
|
140
152
|
// 结论只在 sn-ipv6 正常跑完且无输出时才成立
|
|
141
153
|
const needV6 = (switches.relay || switches.peer)
|
|
142
|
-
&& entityAudits.some((a) => a.name ===
|
|
154
|
+
&& entityAudits.some((a) => a.name === SN_IPV6 && a.audit === 'ok');
|
|
143
155
|
const v6P = needV6
|
|
144
|
-
? ctx.runner.runRawAsync(
|
|
156
|
+
? ctx.runner.runRawAsync(paths.binScript(SN_IPV6), [], { timeout: 5000 })
|
|
145
157
|
: Promise.resolve(null);
|
|
146
158
|
|
|
147
159
|
// 就绪实测并发(On 是意图、ready 是事实;开关关的线路不探测=null)。
|
|
@@ -204,11 +216,7 @@ async function collectEdge(ctx) {
|
|
|
204
216
|
|
|
205
217
|
async function collectCore(ctx) {
|
|
206
218
|
const { topo, paths } = ctx;
|
|
207
|
-
const issues =
|
|
208
|
-
const vIssue = runtimeVersionIssue(ctx.paths);
|
|
209
|
-
if (vIssue) issues.push(vIssue);
|
|
210
|
-
const { issues: rtIssues } = runtimeIssues(ctx, relayRuntimePlan(topo));
|
|
211
|
-
issues.push(...rtIssues);
|
|
219
|
+
const { issues } = baseAudit(ctx, relayRuntimePlan(topo));
|
|
212
220
|
|
|
213
221
|
// disabled override 是整域同一份字典:relay job 与各 edge job 共用一次拉取
|
|
214
222
|
const dmap = launchd.disabledMap(ctx.runner, ctx.uid);
|
|
@@ -217,11 +225,11 @@ async function collectCore(ctx) {
|
|
|
217
225
|
// relay 口实测是 8s 超时的真 ssh:先发起来,边等边做下面的注册态体检
|
|
218
226
|
const relayPortP = relayPortListening(ctx, topo.core.relayPort);
|
|
219
227
|
|
|
220
|
-
//
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
packageScriptPath(
|
|
224
|
-
|
|
228
|
+
// 回拨组件清单与 install/报名同源(manifest):实体与别名无关,循环外体检
|
|
229
|
+
// 一次,各已注册 edge 只检各自的带别名软链。topology 无 edge 时不审
|
|
230
|
+
const callbackEntityAudit = topo.edges.length > 0
|
|
231
|
+
? auditScript(paths.binScript(UPLINK_EDGE), packageScriptPath(UPLINK_EDGE))
|
|
232
|
+
: null;
|
|
225
233
|
|
|
226
234
|
const edges = [];
|
|
227
235
|
for (const e of topo.edges) {
|
|
@@ -230,12 +238,10 @@ async function collectCore(ctx) {
|
|
|
230
238
|
// 已连接过的随身机,回拨组件一并体检:回拨脚本/软链只有 _register
|
|
231
239
|
// (报名)这条刷新通道,漏刷会拿旧脚本拨号
|
|
232
240
|
if (st.registered) {
|
|
241
|
+
const [linkName, entityName] = edgeCallbacksPlan([e.alias]).links[0];
|
|
233
242
|
const bad = [];
|
|
234
243
|
if (callbackEntityAudit !== 'ok') bad.push('回拨脚本');
|
|
235
|
-
const linkState = auditLink(
|
|
236
|
-
path.join(paths.binDir, edgeLinkName(e.alias)),
|
|
237
|
-
'ssh-net-uplink-edge',
|
|
238
|
-
);
|
|
244
|
+
const linkState = auditLink(paths.binScript(linkName), entityName);
|
|
239
245
|
if (linkState !== 'ok') bad.push(`软链(${linkState})`);
|
|
240
246
|
if (bad.length)
|
|
241
247
|
// 已注册 edge 的实体/软链其实会被 install 刷新(26.9.19 起),但
|
|
@@ -247,7 +253,7 @@ async function collectCore(ctx) {
|
|
|
247
253
|
// edge 入口片段体检:job 在册且未休眠(enabled)+ 有 v6 时,片段必须
|
|
248
254
|
// 与 state 一致——缺失(手删/磁盘丢失)或过期(短名/user 变更后没
|
|
249
255
|
// install)都会让 `ssh <别名>` 失败或解析错。休眠(disabled)时片段
|
|
250
|
-
//
|
|
256
|
+
// 本就该删(拨号失败收尸),不报;未注册无 v6 也不报
|
|
251
257
|
let fragmentAudit = null;
|
|
252
258
|
if (edgeFragmentShouldExist(st, v6)) {
|
|
253
259
|
fragmentAudit = auditConfFragment(paths.edgeHostFile(e.alias), edgeFragmentContent(e, v6));
|
|
@@ -271,12 +277,12 @@ async function collectCore(ctx) {
|
|
|
271
277
|
issues.push(relayPortIssue(topo.relay.alias, topo.core.relayPort, relayPort));
|
|
272
278
|
|
|
273
279
|
// topology 已删(edge 改名/退役)但磁盘上还有回拨 job:不渲染成 edges 行
|
|
274
|
-
//(state/conf.d
|
|
280
|
+
//(state/conf.d 都按新别名走),单独点破。残留无害——拨号失败后自休眠、
|
|
275
281
|
// 端口争抢由胜者持有且监听功能等价——但不可见会让改名现场少一条线索
|
|
276
282
|
const knownAliases = new Set(topo.edges.map((e) => e.alias));
|
|
277
283
|
for (const alias of launchd.edgeAliasesOnDisk(paths.launchAgentsDir)) {
|
|
278
284
|
if (!knownAliases.has(alias))
|
|
279
|
-
issues.push(`${alias} 的回拨 job 还在但 topology 已没有这个 edge(改名/退役残留,无害)——清理:launchctl bootout gui/${ctx.uid}/${edgeLabel(alias)} 后删该 plist
|
|
285
|
+
issues.push(`${alias} 的回拨 job 还在但 topology 已没有这个 edge(改名/退役残留,无害)——清理:launchctl bootout gui/${ctx.uid}/${edgeLabel(alias)} 后删该 plist;或无视(它拨号失败后会自休眠)`);
|
|
280
286
|
}
|
|
281
287
|
|
|
282
288
|
return {
|
|
@@ -287,11 +293,7 @@ async function collectCore(ctx) {
|
|
|
287
293
|
|
|
288
294
|
async function collectNode(ctx) {
|
|
289
295
|
const { topo } = ctx;
|
|
290
|
-
const issues =
|
|
291
|
-
const vIssue = runtimeVersionIssue(ctx.paths);
|
|
292
|
-
if (vIssue) issues.push(vIssue);
|
|
293
|
-
const { issues: rtIssues } = runtimeIssues(ctx, relayRuntimePlan(topo));
|
|
294
|
-
issues.push(...rtIssues);
|
|
296
|
+
const { issues } = baseAudit(ctx, relayRuntimePlan(topo));
|
|
295
297
|
|
|
296
298
|
const unit = relayUnitName(topo.relay.alias);
|
|
297
299
|
const active = systemd.isActive(ctx.runner, unit);
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
// node stop+disable。
|
|
7
7
|
import * as launchd from '../lib/launchd.mjs';
|
|
8
8
|
import * as systemd from '../lib/systemd.mjs';
|
|
9
|
-
import {
|
|
9
|
+
import { reapEdgeLink } from '../lib/jobctl.mjs';
|
|
10
10
|
import { assertUnitSwitchArgs } from '../lib/cliargs.mjs';
|
|
11
11
|
import { relayLabel, relayUnitName } from '../lib/render.mjs';
|
|
12
12
|
|
|
@@ -45,9 +45,11 @@ export function run(ctx, args = []) {
|
|
|
45
45
|
// 时 sn-route 不报名,自然只走中继
|
|
46
46
|
return 'peer 已关闭(软生效:停止报名。已建立的连接继续用,断了重连就只走中继)';
|
|
47
47
|
}
|
|
48
|
-
// 全关 / 只关 relay
|
|
48
|
+
// 全关 / 只关 relay:级联删两开关(peer 依赖 relay,见 design §5),
|
|
49
|
+
// 硬停 link job(PathState 见 RELAY_ON 消失不再重拉;未装附加件直接 gone)
|
|
49
50
|
const alreadyAllOff = !before.relay && !before.peer;
|
|
50
|
-
|
|
51
|
+
ctx.state.removeOn(['relay', 'peer']);
|
|
52
|
+
const { gone, label } = reapEdgeLink(ctx);
|
|
51
53
|
if (!gone)
|
|
52
54
|
throw new Error(
|
|
53
55
|
`${label} 仍在运行(TERM 已发两次)。手工排查:launchctl print gui/${ctx.uid}/${label}`,
|
|
@@ -13,16 +13,22 @@ import {
|
|
|
13
13
|
|
|
14
14
|
const rm = (p) => fs.rmSync(p, { recursive: true, force: true });
|
|
15
15
|
|
|
16
|
+
/** 三角色共同产物:整棵 bin/state/conf.d + 版本戳 + link 意图 marker
|
|
17
|
+
* (marker 只 edge 可能有,其余角色 force rm 不存在的文件无害)。 */
|
|
18
|
+
function removeArtifacts(ctx) {
|
|
19
|
+
rm(ctx.paths.binDir);
|
|
20
|
+
rm(ctx.paths.stateDir);
|
|
21
|
+
rm(ctx.paths.confDir);
|
|
22
|
+
rm(ctx.paths.runtimeVersionFile);
|
|
23
|
+
rm(ctx.paths.linkEnabledFile);
|
|
24
|
+
}
|
|
25
|
+
|
|
16
26
|
function uninstallEdge(ctx) {
|
|
17
27
|
const label = linkLabel(ctx.topo.core.alias);
|
|
18
28
|
removeJob(ctx, label, path.join(
|
|
19
29
|
ctx.paths.launchAgentsDir, plistFileName(label),
|
|
20
30
|
));
|
|
21
|
-
|
|
22
|
-
rm(ctx.paths.stateDir);
|
|
23
|
-
rm(ctx.paths.confDir); // routes(edge 装的);node 上不存在无害
|
|
24
|
-
rm(ctx.paths.runtimeVersionFile);
|
|
25
|
-
rm(ctx.paths.linkEnabledFile); // link 附加件意图
|
|
31
|
+
removeArtifacts(ctx);
|
|
26
32
|
rm(path.join(ctx.paths.logsDir, `${label}.log`));
|
|
27
33
|
return '已卸载。最后一步:npm uninstall -g ssh-net';
|
|
28
34
|
}
|
|
@@ -45,10 +51,7 @@ function uninstallCore(ctx) {
|
|
|
45
51
|
const relay = relayLabel(ctx.topo.relay.alias);
|
|
46
52
|
removeJob(ctx, relay, path.join(dir, plistFileName(relay)));
|
|
47
53
|
|
|
48
|
-
|
|
49
|
-
rm(ctx.paths.stateDir);
|
|
50
|
-
rm(ctx.paths.confDir); // 全部 edge-<别名> 动态片段
|
|
51
|
-
rm(ctx.paths.runtimeVersionFile);
|
|
54
|
+
removeArtifacts(ctx); // 全部 edge-<别名> 动态片段随 conf.d 整棵删
|
|
52
55
|
for (const label of [...edgeJobs.keys(), relay])
|
|
53
56
|
rm(path.join(ctx.paths.logsDir, `${label}.log`));
|
|
54
57
|
return `已卸载(relay 通道与 ${edgeJobs.size} 个 edge 的回拨作业)。最后一步:npm uninstall -g ssh-net`;
|
|
@@ -64,10 +67,7 @@ function uninstallNode(ctx) {
|
|
|
64
67
|
systemd.disableUnit(ctx.runner, unit); // 已禁无害
|
|
65
68
|
rm(path.join(ctx.paths.systemdUnitDir, unit));
|
|
66
69
|
systemd.daemonReload(ctx.runner);
|
|
67
|
-
|
|
68
|
-
rm(ctx.paths.stateDir);
|
|
69
|
-
rm(ctx.paths.confDir);
|
|
70
|
-
rm(ctx.paths.runtimeVersionFile);
|
|
70
|
+
removeArtifacts(ctx);
|
|
71
71
|
return '已卸载。最后一步:npm uninstall -g ssh-net';
|
|
72
72
|
}
|
|
73
73
|
|
package/libexec/lib/jobctl.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
// edge
|
|
2
|
-
//
|
|
3
|
-
//
|
|
1
|
+
// edge link job 的进程收束(design §5):纯机制——在册才 launchctl kill TERM
|
|
2
|
+
// 并轮询校验纯 ssh job 真退出(信号直达 ssh),不碰开关;删哪些开关是 stop
|
|
3
|
+
// 的命令语义,由命令层显式做。未装 link(label 不在册)直接 gone。预算内
|
|
4
|
+
// 补发一次 TERM,仍在则报错给手工排障命令,绝不自动 KILL。
|
|
4
5
|
// 同步 CLI 用 Atomics.wait 节流。
|
|
5
6
|
import * as launchd from './launchd.mjs';
|
|
6
7
|
import { linkLabel } from './render.mjs';
|
|
@@ -24,11 +25,8 @@ function isGone(ctx, label) {
|
|
|
24
25
|
* @param {object} ctx
|
|
25
26
|
* @returns {{gone:boolean, label:string}}
|
|
26
27
|
*/
|
|
27
|
-
export function
|
|
28
|
+
export function reapEdgeLink(ctx) {
|
|
28
29
|
const label = linkLabel(ctx.topo.core.alias);
|
|
29
|
-
// 删两开关(peer 依赖 relay,见 design §5)。PathState:rm RELAY_ON 不杀
|
|
30
|
-
// 进程,但阻止退出后重拉
|
|
31
|
-
ctx.state.removeOn(['relay', 'peer']);
|
|
32
30
|
if (!launchd.printState(ctx.runner, ctx.uid, label).registered)
|
|
33
31
|
return { gone: true, label };
|
|
34
32
|
|
package/libexec/lib/launchd.mjs
CHANGED
|
@@ -12,6 +12,12 @@ export const guiDomain = (uid) => `gui/${uid}`;
|
|
|
12
12
|
|
|
13
13
|
const EDGE_PLIST_PREFIX = edgeLabel(''); // 'local.ssh-net-uplink-edge-'
|
|
14
14
|
|
|
15
|
+
/** 未注册状态的固定形状(parsePrint 与两个 inspect 的未注册返回共用)。 */
|
|
16
|
+
const NOT_REGISTERED_PRINT = Object.freeze(
|
|
17
|
+
{ registered: false, running: false, pid: null, runs: null, lastExitCode: null },
|
|
18
|
+
);
|
|
19
|
+
const NOT_REGISTERED = Object.freeze({ disabled: false, ...NOT_REGISTERED_PRINT });
|
|
20
|
+
|
|
15
21
|
/**
|
|
16
22
|
* 磁盘 LaunchAgents 里 uplink-edge job plist 的别名清单(install/status/
|
|
17
23
|
* uninstall 共用;topology 已删的机器只能靠这个 glob 兜底发现,目录不存在
|
|
@@ -42,9 +48,7 @@ export function parsePrint(result) {
|
|
|
42
48
|
// enable+bootstrap 会以真实报错浮出(2026-09-29 真机确认 113 语义)
|
|
43
49
|
const registered =
|
|
44
50
|
result.code === 0 && !/Could not find service/.test(result.stderr ?? '');
|
|
45
|
-
if (!registered)
|
|
46
|
-
return { registered: false, running: false, pid: null, runs: null, lastExitCode: null };
|
|
47
|
-
}
|
|
51
|
+
if (!registered) return NOT_REGISTERED_PRINT;
|
|
48
52
|
const out = result.stdout ?? '';
|
|
49
53
|
const pidMatch = out.match(/^\s*pid\s*=\s*(\d+)/m);
|
|
50
54
|
const runsMatch = out.match(/^\s*runs\s*=\s*(\d+)/m);
|
|
@@ -105,9 +109,7 @@ export function disabledMap(runner, uid) {
|
|
|
105
109
|
*/
|
|
106
110
|
export function inspectWith(runner, uid, label, map) {
|
|
107
111
|
const printed = printState(runner, uid, label);
|
|
108
|
-
if (!printed.registered)
|
|
109
|
-
return { registered: false, disabled: false, running: false, pid: null, runs: null, lastExitCode: null };
|
|
110
|
-
}
|
|
112
|
+
if (!printed.registered) return NOT_REGISTERED;
|
|
111
113
|
return { ...printed, disabled: map.get(label) === true };
|
|
112
114
|
}
|
|
113
115
|
|
|
@@ -121,9 +123,7 @@ export function inspectWith(runner, uid, label, map) {
|
|
|
121
123
|
*/
|
|
122
124
|
export function inspect(runner, uid, label) {
|
|
123
125
|
const printed = printState(runner, uid, label);
|
|
124
|
-
if (!printed.registered)
|
|
125
|
-
return { registered: false, disabled: false, running: false, pid: null, runs: null, lastExitCode: null };
|
|
126
|
-
}
|
|
126
|
+
if (!printed.registered) return NOT_REGISTERED;
|
|
127
127
|
return { ...printed, disabled: disabledMap(runner, uid).get(label) === true };
|
|
128
128
|
}
|
|
129
129
|
|
package/libexec/lib/manifest.mjs
CHANGED
|
@@ -1,11 +1,29 @@
|
|
|
1
|
-
// 运行时实体/服务软链清单的单一来源(install
|
|
2
|
-
// design §2.3
|
|
3
|
-
//
|
|
1
|
+
// 运行时实体/服务软链清单的单一来源(install 对账、报名对账与 status 体检
|
|
2
|
+
// 共用,design §2.3)。清单即契约:新增包脚本只改这里,漏装与假报「组件
|
|
3
|
+
// 异常」不会再分叉。
|
|
4
4
|
//
|
|
5
5
|
// entities:[bin 名, 包脚本名]——原名实体;links:[带机器名软链, 实体名]。
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
import { edgeLinkName, linkLinkName, relayLinkName } from './render.mjs';
|
|
7
|
+
|
|
8
|
+
// bin 脚本名:plan 清单与按名判断(status 体检、install 预检)共用,改名一处
|
|
9
|
+
export const SN_PROBE = 'sn-probe';
|
|
10
|
+
export const SN_ROUTE = 'sn-route';
|
|
11
|
+
export const SN_IPV6 = 'sn-ipv6';
|
|
12
|
+
export const LINK_CORE = 'ssh-net-link-core';
|
|
13
|
+
export const UPLINK_RELAY = 'ssh-net-uplink-relay';
|
|
14
|
+
export const UPLINK_EDGE = 'ssh-net-uplink-edge';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* link 附加件专属产物:第 4 实体 + 带机器名软链。edgeRuntimePlan 装了 link
|
|
18
|
+
* 时并入;install --without-link 按这份清单删。
|
|
19
|
+
* @param {import('./config.mjs').Topology} topo
|
|
20
|
+
*/
|
|
21
|
+
export function linkAddonPlan(topo) {
|
|
22
|
+
return {
|
|
23
|
+
entities: [[LINK_CORE, LINK_CORE]],
|
|
24
|
+
links: [[linkLinkName(topo.core.alias), LINK_CORE]],
|
|
25
|
+
};
|
|
26
|
+
}
|
|
9
27
|
|
|
10
28
|
/**
|
|
11
29
|
* edge 形态:基础三件套恒装;装了 link 附加件才加第 4 实体与带机器名软链。
|
|
@@ -15,27 +33,44 @@ import { linkLinkName, relayLinkName } from './render.mjs';
|
|
|
15
33
|
export function edgeRuntimePlan(topo, linkInstalled) {
|
|
16
34
|
/** @type {[string,string][]} */
|
|
17
35
|
const entities = [
|
|
18
|
-
[
|
|
19
|
-
[
|
|
20
|
-
[
|
|
36
|
+
[SN_PROBE, SN_PROBE],
|
|
37
|
+
[SN_ROUTE, SN_ROUTE], // Match exec 按需选路 + 成功后本地通知/秒切(design §2.2)
|
|
38
|
+
[SN_IPV6, SN_IPV6],
|
|
21
39
|
];
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
entities.push(['ssh-net-link-core', 'ssh-net-link-core']);
|
|
26
|
-
links.push([linkLinkName(topo.core.alias), 'ssh-net-link-core']);
|
|
27
|
-
}
|
|
28
|
-
return { entities, links };
|
|
40
|
+
return linkInstalled
|
|
41
|
+
? mergePlans({ entities, links: [] }, linkAddonPlan(topo))
|
|
42
|
+
: { entities, links: [] };
|
|
29
43
|
}
|
|
30
44
|
|
|
31
45
|
/**
|
|
32
|
-
* relay 一件套:core/node 常驻服务的基础形态(installCore
|
|
46
|
+
* relay 一件套:core/node 常驻服务的基础形态(installCore 在其上并入在册
|
|
33
47
|
* edge 的回拨组件)。
|
|
34
48
|
* @param {import('./config.mjs').Topology} topo
|
|
35
49
|
*/
|
|
36
50
|
export function relayRuntimePlan(topo) {
|
|
37
51
|
return {
|
|
38
|
-
entities: [[
|
|
39
|
-
links: [[relayLinkName(topo.relay.alias),
|
|
52
|
+
entities: [[UPLINK_RELAY, UPLINK_RELAY]],
|
|
53
|
+
links: [[relayLinkName(topo.relay.alias), UPLINK_RELAY]],
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* core 侧在册 edge 的回拨组件并集:uplink-edge 全 core 一份实体,每台 edge
|
|
59
|
+
* 一条带别名软链(install 按磁盘在册、_register 传单台、status 按 topology
|
|
60
|
+
* 在册,三方同源)。
|
|
61
|
+
* @param {readonly string[]} aliases
|
|
62
|
+
*/
|
|
63
|
+
export function edgeCallbacksPlan(aliases) {
|
|
64
|
+
return {
|
|
65
|
+
entities: aliases.length ? [[UPLINK_EDGE, UPLINK_EDGE]] : [],
|
|
66
|
+
links: aliases.map((alias) => [edgeLinkName(alias), UPLINK_EDGE]),
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** 多份计划并集(顺序拼接;调用方保证各份之间实体/软链不重复)。 */
|
|
71
|
+
export function mergePlans(...plans) {
|
|
72
|
+
return {
|
|
73
|
+
entities: plans.flatMap((p) => p.entities),
|
|
74
|
+
links: plans.flatMap((p) => p.links),
|
|
40
75
|
};
|
|
41
76
|
}
|
package/libexec/lib/paths.mjs
CHANGED
|
@@ -49,6 +49,8 @@ export function machinePaths(env = process.env) {
|
|
|
49
49
|
sshNetDir,
|
|
50
50
|
topologyFile: path.join(sshNetDir, 'topology'),
|
|
51
51
|
binDir: path.join(sshNetDir, 'bin'),
|
|
52
|
+
/** @param {string} name bin/ 下产物名(实体或服务软链) */
|
|
53
|
+
binScript: (name) => path.join(sshNetDir, 'bin', name),
|
|
52
54
|
runtimeVersionFile: path.join(sshNetDir, 'runtime-version'),
|
|
53
55
|
stateDir,
|
|
54
56
|
// ssh Include 片段目录(design §4.2,2026-10-01):routes 仅 edge install
|
package/libexec/lib/probe.mjs
CHANGED
|
@@ -47,24 +47,39 @@ export function listListeners(runner, port) {
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
/**
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
* 本地转发——与本服务不同地址族、可共存,不算冲突。用全输出 lsof 按
|
|
53
|
-
* `[::1]:<port>` 行过滤(lsof 的 -iTCP@ 过滤对 v6 字面量支持不稳)。
|
|
50
|
+
* 全输出 lsof 查某端口 LISTEN 持有者(无监听返回 '')。预检报冲突与僵尸
|
|
51
|
+
* 判定都用它:一次调用同时拿 pid 与给人看的持有者文本。
|
|
54
52
|
* @param {import('./exec.mjs').Runner} runner
|
|
55
53
|
* @param {number} port
|
|
56
|
-
* @returns {
|
|
54
|
+
* @returns {string}
|
|
57
55
|
*/
|
|
58
|
-
export function
|
|
56
|
+
export function lsofListen(runner, port) {
|
|
59
57
|
const r = runner.run('lsof', ['-nP', `-iTCP:${port}`, '-sTCP:LISTEN']);
|
|
60
|
-
|
|
61
|
-
|
|
58
|
+
return r.code === 0 ? r.stdout.trim() : '';
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 从 lsof 全输出解析 v6 环回([::1])监听者 pid(lsof 的 -iTCP@ 过滤对 v6
|
|
63
|
+
* 字面量支持不稳,按行过滤)。v4 环回(127.0.0.1)上的同名端口持有者——如
|
|
64
|
+
* VS Code 本地转发——与本服务不同地址族、可共存,不算冲突。
|
|
65
|
+
* @param {string} text lsofListen 的输出
|
|
66
|
+
* @param {number} port
|
|
67
|
+
* @returns {number[]}
|
|
68
|
+
*/
|
|
69
|
+
export function v6LoopbackPids(text, port) {
|
|
70
|
+
return text
|
|
62
71
|
.split('\n')
|
|
63
72
|
.filter((l) => l.includes(`[::1]:${port}`))
|
|
64
73
|
.map((l) => Number(l.match(/\s(\d+)\s/)?.[1]))
|
|
65
74
|
.filter((n) => Number.isInteger(n));
|
|
66
75
|
}
|
|
67
76
|
|
|
77
|
+
/** 端口的 v6 环回([::1])监听者 pid 列表(peer 回拨口专用,2026-09-30 起
|
|
78
|
+
* 10022 只听 [::1])。 */
|
|
79
|
+
export function listV6LoopbackListeners(runner, port) {
|
|
80
|
+
return v6LoopbackPids(lsofListen(runner, port), port);
|
|
81
|
+
}
|
|
82
|
+
|
|
68
83
|
/**
|
|
69
84
|
* 全机 pid→ppid 表。
|
|
70
85
|
* @param {import('./exec.mjs').Runner} runner
|
package/libexec/lib/render.mjs
CHANGED
|
@@ -167,7 +167,7 @@ export function renderRoutes(topo, selfAlias) {
|
|
|
167
167
|
*/
|
|
168
168
|
export function renderEdgeHost({ alias, aliases, user, v6 }) {
|
|
169
169
|
return `# 本文件由 ssh-net 维护:core 收到 ${alias} 的报名时写入,回拨 job 休眠
|
|
170
|
-
# (edge
|
|
170
|
+
# (edge 关机/不可达,拨号失败两次)或 uninstall 时删除。存在即 edge 当前在线、
|
|
171
171
|
# 其上报 v6 可拨;勿手改(改了下次报名也会被覆盖)。
|
|
172
172
|
Host ${aliases.join(' ')}
|
|
173
173
|
User ${user}
|
|
@@ -237,7 +237,7 @@ export function renderEdgeLinkPlist(opts) {
|
|
|
237
237
|
'-o', 'ExitOnForwardFailure=yes', '-o', 'BatchMode=yes',
|
|
238
238
|
'-o', 'IdentitiesOnly=yes', '-o', 'ControlMaster=no', '-o', 'ControlPath=none',
|
|
239
239
|
'-o', 'ServerAliveInterval=35', '-o', 'ServerAliveCountMax=3',
|
|
240
|
-
'-o', 'ConnectTimeout=
|
|
240
|
+
'-o', 'ConnectTimeout=5', '-o', 'StrictHostKeyChecking=accept-new',
|
|
241
241
|
coreAlias,
|
|
242
242
|
];
|
|
243
243
|
return `${XML_HEAD} <key>Label</key>
|
|
@@ -279,8 +279,8 @@ ${logPaths({ label, logsDir })}${XML_TAIL}`;
|
|
|
279
279
|
}
|
|
280
280
|
|
|
281
281
|
/**
|
|
282
|
-
* core 回拨 edge
|
|
283
|
-
* 无 RunAtLoad
|
|
282
|
+
* core 回拨 edge:两拨重试与休眠在脚本里,launchd 只在非自愿退出时重拉。
|
|
283
|
+
* 无 RunAtLoad;二连败(首拨失败 5s 后重试一次仍败)由脚本 disable + exit 0。
|
|
284
284
|
* @param {{binDir:string, edgeAlias:string, user:string, logsDir:string}} opts
|
|
285
285
|
*/
|
|
286
286
|
export function renderUplinkEdgePlist(opts) {
|
package/libexec/lib/runtime.mjs
CHANGED
|
@@ -62,17 +62,23 @@ export function ensureLink(linkPath, entityName) {
|
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
/**
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
65
|
+
* 已退役产物清单:bin/state/conf.d 都是本工具独占位置,install 对账时静默
|
|
66
|
+
* 删除,三种产物的退役清理只有这一个入口。条目带退役版本,保留约一个月后
|
|
67
|
+
* 随版本删除(见 CLAUDE.md「约定」),别让一次性兜底变成永久代码。
|
|
68
|
+
* dir 是 ctx.paths 的目录键。
|
|
69
|
+
* @type {{dir:'binDir'|'stateDir'|'confDir', name:string, retired:string}[]}
|
|
68
70
|
*/
|
|
69
|
-
const
|
|
70
|
-
'sn-notify',
|
|
71
|
-
'sn-signal',
|
|
71
|
+
const STALE_FILES = [
|
|
72
|
+
{ dir: 'binDir', name: 'sn-notify', retired: '26.10.31' }, // 通知内联 sn-route
|
|
73
|
+
{ dir: 'binDir', name: 'sn-signal', retired: '26.10.31' }, // 跨机反拨通道取消
|
|
74
|
+
{ dir: 'stateDir', name: 'ROUTE_PEER', retired: '26.10.31' }, // 开关收敛为 relay/peer
|
|
75
|
+
{ dir: 'stateDir', name: 'LINK_ON', retired: '26.10.31' }, // link 退化为纯 ssh
|
|
76
|
+
{ dir: 'confDir', name: 'vscode-mark', retired: '26.10.32' }, // 打标块并入 routes
|
|
72
77
|
];
|
|
73
78
|
|
|
74
79
|
/**
|
|
75
|
-
* 幂等对账 bin
|
|
80
|
+
* 幂等对账 bin/:退役产物清理 + 实体拷贝 + 服务软链 + 版本戳。所有 install
|
|
81
|
+
* 路径(edge/core/node,含 link 形态切换分支)都先经这里,退役清理无死角。
|
|
76
82
|
* @param {object} ctx
|
|
77
83
|
* @param {readonly (readonly [string, string])[]} entries [bin 名, 包脚本名] 实体
|
|
78
84
|
* @param {readonly (readonly [string, string])[]} links [链名, 实体 bin 名]
|
|
@@ -80,8 +86,8 @@ const STALE_SCRIPTS = [
|
|
|
80
86
|
*/
|
|
81
87
|
export function ensureRuntime(ctx, entries, links = []) {
|
|
82
88
|
fs.mkdirSync(ctx.paths.binDir, { recursive: true });
|
|
83
|
-
for (const name of
|
|
84
|
-
fs.rmSync(path.join(ctx.paths
|
|
89
|
+
for (const { dir, name } of STALE_FILES)
|
|
90
|
+
fs.rmSync(path.join(ctx.paths[dir], name), { force: true });
|
|
85
91
|
const results = entries.map(([name, scriptName]) =>
|
|
86
92
|
ensureScript(path.join(ctx.paths.binDir, name), packageScriptPath(scriptName)));
|
|
87
93
|
for (const [name, entityName] of links) {
|
package/libexec/lib/systemd.mjs
CHANGED
|
@@ -20,14 +20,13 @@ export const isActive = (runner, unit) =>
|
|
|
20
20
|
sc(runner, ['is-active', unit]).stdout.trim();
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
|
-
* unit 文件校验;systemd-analyze
|
|
24
|
-
*
|
|
23
|
+
* unit 文件校验;systemd-analyze 不存在(ENOENT)视为通过——lint 是尽力而为,
|
|
24
|
+
* 不该让装不了 systemd-analyze 的机器无法安装。
|
|
25
|
+
* @returns {{ok:boolean, stdout:string, stderr:string}}
|
|
25
26
|
*/
|
|
26
27
|
export function verifyUnit(runner, unitPath) {
|
|
27
28
|
const r = runner.run('systemdAnalyze', ['verify', unitPath]);
|
|
28
|
-
if (r.error && r.error.code === 'ENOENT') {
|
|
29
|
-
return { ok: true, skipped: true, stdout: r.stdout, stderr: r.stderr };
|
|
30
|
-
}
|
|
29
|
+
if (r.error && r.error.code === 'ENOENT') return { ok: true, stdout: r.stdout, stderr: r.stderr };
|
|
31
30
|
// verify 对只告警也可能非零;这里把非零视为失败,命令层打印 stderr
|
|
32
31
|
return { ok: r.code === 0, stdout: r.stdout, stderr: r.stderr };
|
|
33
32
|
}
|
|
@@ -65,7 +64,7 @@ export function syncUnit(runner, { unitPath, unitName, content }) {
|
|
|
65
64
|
|
|
66
65
|
writeFileAtomic(unitPath, content, { mode: 0o644 });
|
|
67
66
|
const verify = verifyUnit(runner, unitPath);
|
|
68
|
-
if (!verify.ok
|
|
67
|
+
if (!verify.ok)
|
|
69
68
|
throw new Error(`systemd-analyze verify 失败:\n${verify.stderr}`);
|
|
70
69
|
for (const r of [
|
|
71
70
|
daemonReload(runner),
|
package/libexec/sn-route
CHANGED
|
@@ -13,7 +13,9 @@
|
|
|
13
13
|
# ——软关 peer 后仍允许用已建立的回拨)
|
|
14
14
|
# 3. PEER_ON 不存在 → 失败,不报名(软关语义)
|
|
15
15
|
# 4. PROBE_FAIL 印记新鲜(15s) → 失败,不报名
|
|
16
|
-
# 5.
|
|
16
|
+
# 5. 僵尸口(probe=2:TCP 通但无 banner)在本层先本地清尸(lsof 找持口的
|
|
17
|
+
# sshd 会话、SIGTERM),再查 sn-ipv6——清尸与 v6 同属报名前本地准备,
|
|
18
|
+
# 不吃第二次探测;sn-ipv6 为空 → 失败,写印记
|
|
17
19
|
# 6. single-flight 按需确保(state/ENSURE 锁):leader 经 relay 报名、
|
|
18
20
|
# follower 只跟 probe;预算 DEADLINE=6s 覆盖全程。leader 等回新回拨
|
|
19
21
|
# 后在本地做一次副作用(桌面通知 + 装了 link 才 kickstart,peer_ready)
|
|
@@ -31,6 +33,7 @@ CAT=${CAT:-/bin/cat}
|
|
|
31
33
|
SLEEP=${SLEEP:-/bin/sleep}
|
|
32
34
|
LAUNCHCTL=${LAUNCHCTL:-/bin/launchctl}
|
|
33
35
|
ID=${ID:-/usr/bin/id}
|
|
36
|
+
LSOF=${LSOF:-/usr/sbin/lsof}
|
|
34
37
|
STATE_DIR=${SSH_NET_STATE_DIR:-$HOME/.ssh/ssh-net/state}
|
|
35
38
|
SSH_NET_DIR=${SSH_NET_DIR:-$HOME/.ssh/ssh-net}
|
|
36
39
|
BIN_DIR=${SSH_NET_BIN_DIR:-$SSH_NET_DIR/bin}
|
|
@@ -81,14 +84,19 @@ stamp_fresh() {
|
|
|
81
84
|
|
|
82
85
|
# banner probe 在子 shell 里做:stderr 重定向只在子内生效(主 shell 的 stderr
|
|
83
86
|
# 要留给 say)。fd3 开 /dev/tcp,读 SSH banner 首行,健康 ~10ms、僵尸吃满
|
|
84
|
-
# PROBE_W
|
|
87
|
+
# PROBE_W、拒连即时。退出码三态:
|
|
88
|
+
# 0 活(读到 SSH- banner);1 口没开(拒连即时);
|
|
89
|
+
# 2 僵尸口(TCP 连得上但 PROBE_W 内无 banner——断了的 -R 会话仍被
|
|
90
|
+
# sshd 持着,短睡开盖后最常见,触发层 5 的本地清尸)
|
|
85
91
|
probe() (
|
|
86
92
|
exec 2>/dev/null
|
|
87
93
|
exec 3<>"/dev/tcp/$HOST/$PORT" || exit 1
|
|
88
|
-
# read 必须显式 <&3:fd0 在 Match exec 下是 /dev/null,漏了会立即 EOF
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
94
|
+
# read 必须显式 <&3:fd0 在 Match exec 下是 /dev/null,漏了会立即 EOF。
|
|
95
|
+
# 读到非 SSH- banner 与读超时同为僵尸态,落到函数尾 exit 2
|
|
96
|
+
if IFS= read -t "$PROBE_W" -r line <&3; then
|
|
97
|
+
case "$line" in SSH-*) exit 0 ;; esac
|
|
98
|
+
fi
|
|
99
|
+
exit 2
|
|
92
100
|
)
|
|
93
101
|
|
|
94
102
|
# 失败印记:tmp+rename,epoch 在写盘当下现取——回填探测前时刻会让僵尸吃满
|
|
@@ -103,6 +111,33 @@ mark_fail() {
|
|
|
103
111
|
|| "$RM" -f "$STATE_DIR/PROBE_FAIL.tmp.$$" 2>/dev/null
|
|
104
112
|
}
|
|
105
113
|
|
|
114
|
+
# 僵尸口本地清尸:短睡开盖后 edge sshd 要等 ClientAlive 30×3≈90s 才回收
|
|
115
|
+
# 断了的 -R 会话,这期间口在但无 banner(probe=2),报名唤醒的 core 新拨号
|
|
116
|
+
# 必撞 ExitOnForwardFailure 瞬败,6s 预算怎么都赶不上、本次必落 relay。
|
|
117
|
+
# 实测(2026-10-03,macOS 27 / OpenSSH 10.3):-R 监听口归登录用户自己的
|
|
118
|
+
# sshd-session(root 的 [priv] monitor 不持转发口),本地直接 SIGTERM 会话
|
|
119
|
+
# 进程、监听毫秒级释放;core 旧 client 同连断开,由本次报名的 kickstart
|
|
120
|
+
# 换新拨。三重限定防误杀:LISTEN 状态、本端口、可执行 basename 必须 sshd*
|
|
121
|
+
# (sshd / sshd-session;杀不动 root 属主时 EPERM 静默跳过)。
|
|
122
|
+
# 调用纪律(改顺序必须同时改这里):只许在层 2 probe=2 之后立刻调用,
|
|
123
|
+
# 中间只能过 PEER_ON/印记的本地 stat/cat/date(几十毫秒),sn-ipv6 等网络
|
|
124
|
+
# 命令一律排在清尸之后——不复查探针,复查多吃 1s 读超时,挤爆 6s 预算;
|
|
125
|
+
# 几十毫秒内隧道自行复活的极端窗口即便误杀,本次报名也会立刻重建回拨。
|
|
126
|
+
# 任何意外(lsof 缺失/失败、无持有者、comm 不符)静默:等 sshd 90s
|
|
127
|
+
# 自行回收,之后 edge 下一条连接(受 15s 印记节流)再报名唤醒
|
|
128
|
+
reap_zombie() {
|
|
129
|
+
# 一条 lsof 同时取 pid 与 comm(-F pc:每个持口进程一对 p/c 行,
|
|
130
|
+
# c 给全名不截断),不再为每个 pid 另起一次 ps;进程替换喂 while
|
|
131
|
+
local pid= line
|
|
132
|
+
while IFS= read -r line; do
|
|
133
|
+
case "$line" in
|
|
134
|
+
p[0-9]*) pid=${line#p} ;;
|
|
135
|
+
csshd*) [ -n "$pid" ] && { kill "$pid" 2>/dev/null || true; pid=; } ;;
|
|
136
|
+
c*) pid= ;; # 持口者非 sshd:本进程跳过
|
|
137
|
+
esac
|
|
138
|
+
done < <("$LSOF" -F pc -i "TCP:$PORT" -sTCP:LISTEN 2>/dev/null)
|
|
139
|
+
}
|
|
140
|
+
|
|
106
141
|
# ── 层 1
|
|
107
142
|
[ -e "$STATE_DIR/RELAY_ON" ] || exit 1
|
|
108
143
|
|
|
@@ -110,8 +145,11 @@ say "正在尝试与 $peer 的直连…"
|
|
|
110
145
|
peer_on=0
|
|
111
146
|
[ -e "$STATE_DIR/PEER_ON" ] && peer_on=1
|
|
112
147
|
|
|
113
|
-
# ── 层 2:先探活(顺序锚:probe
|
|
114
|
-
|
|
148
|
+
# ── 层 2:先探活(顺序锚:probe 在一切印记前面)。rc=2 僵尸态记下,
|
|
149
|
+
# 过 PEER_ON/印记两关后由层 5 动手清——软关与印记期绝不杀进程
|
|
150
|
+
probe
|
|
151
|
+
probe_rc=$?
|
|
152
|
+
[ "$probe_rc" = 0 ] && exit 0
|
|
115
153
|
|
|
116
154
|
# ── 层 3:PEER_ON 不在 → 不报名
|
|
117
155
|
if [ "$peer_on" = 0 ]; then
|
|
@@ -125,7 +163,8 @@ if stamp_fresh "$STATE_DIR/PROBE_FAIL" "$FAIL_TTL"; then
|
|
|
125
163
|
exit 1
|
|
126
164
|
fi
|
|
127
165
|
|
|
128
|
-
# ── 层 5
|
|
166
|
+
# ── 层 5:僵尸口先本地清尸(调用纪律见 reap_zombie 注释),再查本机 v6
|
|
167
|
+
[ "$probe_rc" = 2 ] && reap_zombie
|
|
129
168
|
v6=$("$BIN_DIR/sn-ipv6" 2>/dev/null)
|
|
130
169
|
if [ -z "$v6" ]; then
|
|
131
170
|
mark_fail
|
|
@@ -252,8 +291,15 @@ while time_left; do
|
|
|
252
291
|
if ! stamp_fresh "$lock" "$LOCK_STALE" "$now"; then
|
|
253
292
|
"$RM" -f "$lock" 2>/dev/null
|
|
254
293
|
if trylock; then
|
|
255
|
-
|
|
256
|
-
|
|
294
|
+
# 抢空锁后复查失败印记:锁也可能是 leader 秒败刚删的——双
|
|
295
|
+
# Match 并发的晚到者可能正夹在「印记已写、锁已删」之间跳过
|
|
296
|
+
# 上面那条检查。新鲜印记下认 follower、释放空锁,不重复报名
|
|
297
|
+
if stamp_fresh "$STATE_DIR/PROBE_FAIL" "$FAIL_TTL"; then
|
|
298
|
+
"$RM" -f "$lock" 2>/dev/null
|
|
299
|
+
else
|
|
300
|
+
role=leader
|
|
301
|
+
announce_now
|
|
302
|
+
fi
|
|
257
303
|
fi
|
|
258
304
|
fi
|
|
259
305
|
fi
|
|
@@ -1,11 +1,20 @@
|
|
|
1
1
|
#!/bin/bash
|
|
2
|
-
# ssh-net-uplink-edge:core 回拨某台 edge 的 -R 10022
|
|
3
|
-
#
|
|
4
|
-
#
|
|
5
|
-
#
|
|
6
|
-
#
|
|
7
|
-
#
|
|
8
|
-
#
|
|
2
|
+
# ssh-net-uplink-edge:core 回拨某台 edge 的 -R 10022 隧道。argv: <edge 别名>
|
|
3
|
+
# <edge 登录用户>,v6 读 state/edges/<别名>。plist 一生成一次长期保留
|
|
4
|
+
# (脚本经带别名的软链引用,design §2.3)。
|
|
5
|
+
#
|
|
6
|
+
# 生命周期(2026-10-03,取代「6 次失败计数 + 30s 退避」模型):进程一
|
|
7
|
+
# 辈子最多两次拨号——首拨 ssh 自然退出后 5s 短退避再拨一次,第二次退出
|
|
8
|
+
# 即 launchctl disable + exit 0 休眠,等 edge 下次报名(enable +
|
|
9
|
+
# kickstart -k)唤醒。按需架构下唤醒唯一通道是报名(design §6.3):
|
|
10
|
+
# 需要回拨的连接本身就是报名源,循环重试只在合盖后制造噪声拨号(旧模型
|
|
11
|
+
# 105s 判死 + 5 轮 ≈ 5.5 分钟、6 次必败拨号)。唯一保留的一次重试覆盖
|
|
12
|
+
# 「报名瞬间 core→edge 网络还没收敛几秒」的竞态(sn-ipv6 能取到 v6 不
|
|
13
|
+
# 等于上游已可拨通),以及长连断开后的瞬时网络抖动。
|
|
14
|
+
#
|
|
15
|
+
# 关键区分:TERM/INT(kickstart -k 换 v6/重拨、uninstall)绝不 disable
|
|
16
|
+
# ——外部正要重启本 job,trap 转发给 ssh 与退避 sleep 后 exit 0;只有
|
|
17
|
+
# ssh 自然退出才走重试与休眠。
|
|
9
18
|
set -u
|
|
10
19
|
LC_ALL=C
|
|
11
20
|
export LC_ALL
|
|
@@ -16,9 +25,7 @@ STATE_DIR=${SSH_NET_STATE_DIR:-$HOME/.ssh/ssh-net/state}
|
|
|
16
25
|
# core 上给人用的 edge 入口片段目录(design §4.2):休眠即删,喂参测试可注入
|
|
17
26
|
CONF_DIR=${SSH_NET_CONF_DIR:-$HOME/.ssh/ssh-net/conf.d}
|
|
18
27
|
RM=${RM:-/bin/rm}
|
|
19
|
-
|
|
20
|
-
FAIL_LIMIT=${SSH_NET_FAIL_LIMIT:-6}
|
|
21
|
-
RETRY_SLEEP=${SSH_NET_RETRY_SLEEP:-30}
|
|
28
|
+
RETRY_SLEEP=${SSH_NET_RETRY_SLEEP:-5} # 唯一一次重试的退避秒数
|
|
22
29
|
|
|
23
30
|
alias_name=$1
|
|
24
31
|
user=$2
|
|
@@ -33,31 +40,17 @@ log() { echo "$(/bin/date '+%F %T') $*" >&2; }
|
|
|
33
40
|
log "uplink-edge 启动(alias=$alias_name user=$user)"
|
|
34
41
|
|
|
35
42
|
ssh_pid=
|
|
36
|
-
|
|
43
|
+
sleep_pid=
|
|
37
44
|
|
|
38
|
-
#
|
|
45
|
+
# TERM/INT 杀父进程时转给 ssh 子进程与退避 sleep,不留孤儿;绝不能在
|
|
46
|
+
# 本路径 disable(见文件头)。kickstart -k 之后新实例立即起,旧实例
|
|
47
|
+
# disable 会与之竞态
|
|
39
48
|
on_term() {
|
|
40
49
|
[ -n "$ssh_pid" ] && kill "$ssh_pid" 2>/dev/null
|
|
41
|
-
[ -n "$
|
|
50
|
+
[ -n "$sleep_pid" ] && kill "$sleep_pid" 2>/dev/null
|
|
42
51
|
exit 0
|
|
43
52
|
}
|
|
44
53
|
trap on_term TERM INT
|
|
45
|
-
trap 'fails=0; log "存活满 ${CLEAR_TTL}s:失败计数清零"' USR1 # 清零信号也留痕
|
|
46
|
-
|
|
47
|
-
# 内层 trap 负责把未睡完的 sleep 一起收掉,否则 kill 子 shell 会留下孤儿 sleep
|
|
48
|
-
start_clear_timer() {
|
|
49
|
-
[ -n "$timer_pid" ] && kill "$timer_pid" 2>/dev/null # 先杀上一轮残留
|
|
50
|
-
(
|
|
51
|
-
trap 'kill "$sleep_pid" 2>/dev/null; exit 0' TERM
|
|
52
|
-
/bin/sleep "$CLEAR_TTL" & sleep_pid=$!
|
|
53
|
-
wait "$sleep_pid"
|
|
54
|
-
kill -USR1 "$$" 2>/dev/null
|
|
55
|
-
) &
|
|
56
|
-
timer_pid=$!
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
fails=0
|
|
60
|
-
rounds=0
|
|
61
54
|
|
|
62
55
|
# 休眠 = edge 当前不在线:删掉给人用的 conf.d/edge-<别名> 入口,免得
|
|
63
56
|
# `ssh <别名>` 拨向必然失败的旧 v6;下次报名 _register 连同 state 写回。
|
|
@@ -66,20 +59,21 @@ rounds=0
|
|
|
66
59
|
retire_host_fragment() { "$RM" -f "$CONF_DIR/edge-$alias_name" 2>/dev/null || true; }
|
|
67
60
|
|
|
68
61
|
# 休眠 = edge 当前不在线:删入口片段 + disable + exit 0(launchd 见自愿退出
|
|
69
|
-
# 不重拉)。state 无 v6
|
|
62
|
+
# 不重拉)。state 无 v6 与二连败两条路径同构,收在这一处
|
|
70
63
|
hibernate() {
|
|
71
64
|
log "$1"
|
|
72
65
|
retire_host_fragment
|
|
73
66
|
"$LAUNCHCTL" disable "$gui_target"
|
|
74
67
|
exit 0
|
|
75
68
|
}
|
|
76
|
-
while :; do
|
|
77
|
-
if [ -n "${SSH_NET_MAX_ROUNDS:-}" ]; then
|
|
78
|
-
rounds=$((rounds + 1))
|
|
79
|
-
[ "$rounds" -gt "$SSH_NET_MAX_ROUNDS" ] && exit 0
|
|
80
|
-
fi
|
|
81
69
|
|
|
82
|
-
|
|
70
|
+
# 一次拨号:读当下 state 里的 v6(无 v6 直接休眠)→ ssh 到其自然退出。
|
|
71
|
+
# 一辈子调两次:首拨与唯一一次重试;报名 kickstart 杀旧起新即开启新进程
|
|
72
|
+
# 的两次额度。不做「活满 N 秒清零」——那要 USR1 定时器整套(残留 timer、
|
|
73
|
+
# 迟到信号都踩过),而第二次拨号再断时 edge 侧的连接也会感知断线并立刻
|
|
74
|
+
# 再报名,休眠成本为零。v6 每次调用现读:退避期间被报名改写,重试即拨新值
|
|
75
|
+
dial() {
|
|
76
|
+
local v6=
|
|
83
77
|
if [ -r "$state_file" ]; then
|
|
84
78
|
# 纯 bash 内建消空白(launchd 最小环境,不依赖 PATH 里有 tr)
|
|
85
79
|
v6=$(<"$state_file")
|
|
@@ -93,7 +87,7 @@ while :; do
|
|
|
93
87
|
|
|
94
88
|
# host key 走默认 known_hosts + accept-new(2026-09-29 弃专用信任本;
|
|
95
89
|
# 对面不是本尊时 user key 认证处即失败,行为与 pinning 失败一致)
|
|
96
|
-
log "拨号 $user@$v6
|
|
90
|
+
log "拨号 $user@$v6"
|
|
97
91
|
# -R 两端全 v6 环回(2026-09-30 定):edge 监听 [::1]:10022,core 落点 [::1]:22。
|
|
98
92
|
# 不变量「peer 落地客户端必为 ::1、relay 必为 127.0.0.1」由此构造保证,
|
|
99
93
|
# statusline 链路前缀(peer `>` / relay `>>`)只按客户端地址族判级。
|
|
@@ -103,33 +97,29 @@ while :; do
|
|
|
103
97
|
-o ExitOnForwardFailure=yes -o BatchMode=yes -o IdentitiesOnly=yes \
|
|
104
98
|
-o ControlMaster=no -o ControlPath=none \
|
|
105
99
|
-o ServerAliveInterval=35 -o ServerAliveCountMax=3 \
|
|
106
|
-
-o ConnectTimeout=
|
|
100
|
+
-o ConnectTimeout=5 \
|
|
107
101
|
-o StrictHostKeyChecking=accept-new \
|
|
108
102
|
"$user@$v6" &
|
|
109
103
|
ssh_pid=$!
|
|
110
|
-
start_clear_timer
|
|
111
104
|
|
|
112
|
-
#
|
|
113
|
-
#
|
|
105
|
+
# wait 被信号中断会返回非零:循环到 kill -0 兜住,活连接被信号打断后
|
|
106
|
+
# 不能让函数返回、误走下方重试与休眠
|
|
114
107
|
while kill -0 "$ssh_pid" 2>/dev/null; do
|
|
115
108
|
wait "$ssh_pid" 2>/dev/null
|
|
116
109
|
done
|
|
117
|
-
kill "$timer_pid" 2>/dev/null
|
|
118
|
-
timer_pid=
|
|
119
110
|
ssh_pid=
|
|
111
|
+
}
|
|
120
112
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
/bin/sleep "$RETRY_SLEEP" & wait $!
|
|
135
|
-
done
|
|
113
|
+
dial
|
|
114
|
+
|
|
115
|
+
# 唯一一次短退避:覆盖报名瞬间网络未收敛与瞬态抖动。后台 sleep + wait
|
|
116
|
+
# (不是前台 sleep)——wait 可被 TERM trap 立即打断:报名 kickstart 在
|
|
117
|
+
# 退避窗口到达时绝不能多等 5s;前台命令跑完才处理 TERM 会把 launchd
|
|
118
|
+
# 停 job 扣到超时被 SIGKILL(收尾整个跳过)
|
|
119
|
+
log "回拨退出,${RETRY_SLEEP}s 后重试一次(覆盖网络瞬态;再败即休眠等报名)"
|
|
120
|
+
/bin/sleep "$RETRY_SLEEP" & sleep_pid=$!
|
|
121
|
+
wait "$sleep_pid" 2>/dev/null
|
|
122
|
+
sleep_pid=
|
|
123
|
+
|
|
124
|
+
dial
|
|
125
|
+
hibernate "回拨再次退出 → disable 休眠(正常路径:edge 关机/合盖/不可达;等下次报名唤醒)"
|
|
@@ -15,5 +15,5 @@ echo "$(/bin/date '+%F %T') uplink-relay 启动:注册口 $1 → relay $2" >&2
|
|
|
15
15
|
exec "$SSH" -N -R "127.0.0.1:$1:127.0.0.1:22" \
|
|
16
16
|
-o ExitOnForwardFailure=yes -o BatchMode=yes -o IdentitiesOnly=yes \
|
|
17
17
|
-o ControlMaster=no -o ControlPath=none -o StrictHostKeyChecking=yes \
|
|
18
|
-
-o ServerAliveInterval=35 -o ServerAliveCountMax=3 -o ConnectTimeout=
|
|
18
|
+
-o ServerAliveInterval=35 -o ServerAliveCountMax=3 -o ConnectTimeout=5 \
|
|
19
19
|
"$2"
|