ssh-net 26.10.53 → 26.10.55
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 +63 -0
- package/README.md +157 -105
- package/libexec/cli.mjs +51 -16
- package/libexec/commands/doctor.mjs +16 -13
- package/libexec/commands/install.mjs +12 -9
- package/libexec/commands/register.mjs +23 -18
- package/libexec/commands/restart.mjs +5 -5
- package/libexec/commands/start.mjs +8 -5
- package/libexec/commands/status.mjs +62 -45
- package/libexec/commands/stop.mjs +56 -20
- package/libexec/commands/uninstall.mjs +76 -21
- package/libexec/commands/update.mjs +44 -65
- package/libexec/lib/cliargs.mjs +10 -4
- package/libexec/lib/config.mjs +10 -0
- package/libexec/lib/jobctl.mjs +5 -4
- package/libexec/lib/launchd.mjs +29 -12
- package/libexec/lib/manage.mjs +2 -2
- package/libexec/lib/manifest.mjs +3 -0
- package/libexec/lib/paths.mjs +2 -1
- package/libexec/lib/render.mjs +2 -2
- package/libexec/lib/runtime.mjs +45 -8
- package/libexec/lib/systemd.mjs +35 -6
- package/libexec/lib/version.mjs +12 -0
- package/libexec/sn-route +142 -91
- package/libexec/ssh-net-uplink-edge +3 -3
- package/package.json +3 -3
|
@@ -19,32 +19,36 @@ import {
|
|
|
19
19
|
edgeLabel, renderUplinkEdgePlist, coreViaRelayArgs,
|
|
20
20
|
} from '../lib/render.mjs';
|
|
21
21
|
|
|
22
|
+
/** 可重试的即时通知失败:开关仍可保存,但必须把具体原因带给用户。 */
|
|
23
|
+
function retryableError(message) {
|
|
24
|
+
const err = new Error(message);
|
|
25
|
+
err.retryable = true;
|
|
26
|
+
return err;
|
|
27
|
+
}
|
|
28
|
+
|
|
22
29
|
/** edge 侧报名:取 v6 + 命令行 -J 经 relay 调 core 的 _register。 */
|
|
23
30
|
function runEdgeAnnounce(ctx) {
|
|
24
31
|
const probe = ctx.runner.runRaw(
|
|
25
32
|
ctx.paths.binScript(SN_IPV6), [], { timeout: 5000 },
|
|
26
33
|
);
|
|
27
|
-
// bin/sn-ipv6 缺失(未 install)别误报成「无 IPv6
|
|
28
|
-
// enablePeer
|
|
29
|
-
if (probe.error && probe.error.code === 'ENOENT')
|
|
30
|
-
|
|
31
|
-
e.fatal = true;
|
|
32
|
-
throw e;
|
|
33
|
-
}
|
|
34
|
+
// bin/sn-ipv6 缺失(未 install)别误报成「无 IPv6」;未标记 retryable,
|
|
35
|
+
// enablePeer 会原样抛出且不保存开关
|
|
36
|
+
if (probe.error && probe.error.code === 'ENOENT')
|
|
37
|
+
throw new Error('未安装(缺运行时脚本)——先跑 ssh-net install');
|
|
34
38
|
// 「探测没跑成」与「网络没有 IPv6」是两回事,failed 绝不能谎称没有 v6
|
|
35
39
|
const { state, v6 } = classifyV6Probe(probe);
|
|
36
40
|
if (state === 'failed') {
|
|
37
41
|
const why = probe.error?.message
|
|
38
42
|
?? (probe.signal !== null && probe.signal !== undefined
|
|
39
43
|
? `signal ${probe.signal}` : `exit ${probe.code}`);
|
|
40
|
-
throw
|
|
44
|
+
throw retryableError(`本机 IPv6 探测失败(${why});这不代表网络没有 IPv6`);
|
|
41
45
|
}
|
|
42
46
|
if (state === 'none')
|
|
43
|
-
throw
|
|
47
|
+
throw retryableError('当前网络没有可用 IPv6');
|
|
44
48
|
// 拼进远端命令串前本地校验:输出被污染(多行/元字符)时报清晰错误,
|
|
45
49
|
// 而不是拼出一条让 core 侧 shell 莫名解析失败的命令
|
|
46
50
|
const v6Err = explainV6Error(v6);
|
|
47
|
-
if (v6Err) throw
|
|
51
|
+
if (v6Err) throw retryableError(`本机 IPv6 选取异常(sn-ipv6 输出不是可用地址:${v6Err})`);
|
|
48
52
|
// 报名固定走 relay:peer 僵尸时自动选路会把报名一起挂死。无人值守首连
|
|
49
53
|
// 信任 accept-new(status 探测不带,见 design §4.3)。挂死由 60s timeout 兜底
|
|
50
54
|
const r = ctx.runner.run('ssh', coreViaRelayArgs(ctx.topo, {
|
|
@@ -52,26 +56,27 @@ function runEdgeAnnounce(ctx) {
|
|
|
52
56
|
remoteCommand: `ssh-net _register ${ctx.self.alias} ${v6}`,
|
|
53
57
|
}), { timeout: 60_000 });
|
|
54
58
|
if (r.code !== 0)
|
|
55
|
-
throw
|
|
59
|
+
throw retryableError(`经 ${ctx.topo.relay.alias} 通知失败(${r.stderr.trim() || `exit ${r.code}`})`);
|
|
56
60
|
const core = r.stdout.trim();
|
|
57
|
-
return `已通知 core(本机地址 ${v6};core 回应:${core}
|
|
61
|
+
return `已通知 core(本机地址 ${v6};core 回应:${core});core 已受理,接下来会尝试回连,直连是否就绪用 ssh-net status 查看`;
|
|
58
62
|
}
|
|
59
63
|
|
|
60
|
-
/** start peer 的实现:立即报名 + 置 relay+peer
|
|
61
|
-
*
|
|
64
|
+
/** start peer 的实现:立即报名 + 置 relay+peer 开关。未安装等非预期错误零副作用;
|
|
65
|
+
* 标记为 retryable 的报名失败不阻断用户意图,开关照置并展示具体原因——下次
|
|
62
66
|
* 发起连接时 sn-route 会再报。
|
|
63
67
|
* @param {object} ctx
|
|
64
68
|
* @param {boolean} [alreadyOn] 开关此前已全开:措辞用「已是开启状态」 */
|
|
65
69
|
export function enablePeer(ctx, alreadyOn = false) {
|
|
66
|
-
const prefix = alreadyOn ? '隧道已是开启状态' : 'peer
|
|
70
|
+
const prefix = alreadyOn ? '隧道已是开启状态' : 'peer 和 relay 开关已开启';
|
|
67
71
|
try {
|
|
68
72
|
const detail = runEdgeAnnounce(ctx);
|
|
69
73
|
ctx.state.setOn(['relay', 'peer']); // relay 是根:peer 依赖它
|
|
70
|
-
return `${prefix}
|
|
74
|
+
return `${prefix};${detail}`;
|
|
71
75
|
} catch (err) {
|
|
72
|
-
|
|
76
|
+
// 只有明确的网络/探测失败才降级为「开关已开、稍后再试」;程序错误照抛
|
|
77
|
+
if (!err?.retryable) throw err;
|
|
73
78
|
ctx.state.setOn(['relay', 'peer']);
|
|
74
|
-
return `${prefix}
|
|
79
|
+
return `${prefix};本次未能通知 core:${err.message}。开关已保存,下次 ssh 会自动重试,也可再跑 ssh-net start peer`;
|
|
75
80
|
}
|
|
76
81
|
}
|
|
77
82
|
|
|
@@ -13,25 +13,25 @@ export function run(ctx, args = []) {
|
|
|
13
13
|
const label = linkLabel(ctx.topo.core.alias);
|
|
14
14
|
// 只要 registered(printState),不跑全域 print-disabled
|
|
15
15
|
if (!launchd.printState(ctx.runner, ctx.uid, label).registered) {
|
|
16
|
-
return '
|
|
16
|
+
return '默认未安装本地端口转发,没有可重启的对象;立即尝试 IPv6 直连用 ssh-net doctor,需要 SOCKS/VNC 端口用 ssh-net install --with-link';
|
|
17
17
|
}
|
|
18
18
|
// kickstartEnabled:与 core 同规则,残留 disabled override 时不被拒
|
|
19
19
|
const r = launchd.kickstartEnabled(ctx.runner, ctx.uid, label);
|
|
20
20
|
if (r.code !== 0) throw new Error(`重启失败:${r.stderr.trim()}`);
|
|
21
|
-
return '
|
|
21
|
+
return '本地端口转发已重启(正在重新连接)';
|
|
22
22
|
}
|
|
23
23
|
if (ctx.role === 'core') {
|
|
24
24
|
const label = relayLabel(ctx.topo.relay.alias);
|
|
25
25
|
// 只要 registered(printState),不跑全域 print-disabled
|
|
26
26
|
if (!launchd.printState(ctx.runner, ctx.uid, label).registered)
|
|
27
|
-
throw new Error('
|
|
27
|
+
throw new Error('中继任务尚未加载(已 stop?先 ssh-net start)');
|
|
28
28
|
const r = launchd.kickstartEnabled(ctx.runner, ctx.uid, label);
|
|
29
29
|
if (r.code !== 0) throw new Error(`重启失败:${r.stderr.trim()}`);
|
|
30
|
-
return '
|
|
30
|
+
return '中继通道已重启';
|
|
31
31
|
}
|
|
32
32
|
if (ctx.role === 'node') {
|
|
33
33
|
systemd.enableRestart(ctx.runner, relayUnitName(ctx.topo.relay.alias));
|
|
34
|
-
return '
|
|
34
|
+
return '中继通道已重启';
|
|
35
35
|
}
|
|
36
36
|
throw new Error(`未知角色:${ctx.role}`);
|
|
37
37
|
}
|
|
@@ -28,9 +28,9 @@ function startCore(ctx) {
|
|
|
28
28
|
const action = ensureLoaded(ctx, { label, plistPath: plist }, {
|
|
29
29
|
kickWhenStopped: true,
|
|
30
30
|
});
|
|
31
|
-
if (action === 'bootstrapped') return '
|
|
32
|
-
if (action === 'kickstarted') return '
|
|
33
|
-
return '
|
|
31
|
+
if (action === 'bootstrapped') return '中继通道已开启';
|
|
32
|
+
if (action === 'kickstarted') return '中继通道已重新拉起';
|
|
33
|
+
return '中继通道已在运行';
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
export function run(ctx, args = []) {
|
|
@@ -43,14 +43,17 @@ export function run(ctx, args = []) {
|
|
|
43
43
|
return enablePeer(ctx, before.relay && before.peer);
|
|
44
44
|
}
|
|
45
45
|
ctx.state.setOn(['relay']);
|
|
46
|
-
|
|
46
|
+
const peerNote = before.peer
|
|
47
|
+
? 'peer 保持开启,仍会主动恢复 IPv6 直连'
|
|
48
|
+
: 'peer 仍关闭;需要自动恢复时运行 ssh-net start peer';
|
|
49
|
+
return `relay ${before.relay ? '已是开启状态' : '已开启'}(${peerNote})`;
|
|
47
50
|
}
|
|
48
51
|
// core/node 是单总开关(等同 --all):显式 -a/--all 收下,其余参数拒绝
|
|
49
52
|
assertUnitSwitchArgs(args, 'start');
|
|
50
53
|
if (ctx.role === 'core') return startCore(ctx);
|
|
51
54
|
if (ctx.role === 'node') {
|
|
52
55
|
systemd.enableStart(ctx.runner, relayUnitName(ctx.topo.relay.alias));
|
|
53
|
-
return '
|
|
56
|
+
return '中继通道已开启';
|
|
54
57
|
}
|
|
55
58
|
throw new Error(`未知角色:${ctx.role}`);
|
|
56
59
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
// status
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
1
|
+
// status:分开显示开关意图、线路探测和后台任务状态。relay 用真实 ssh/端口探测;
|
|
2
|
+
// edge 的 peer 用 banner 探测,即使 peer 软关闭也报告已有直连;core 的 edge 行
|
|
3
|
+
// 只表示 launchd 连接任务,不把进程 running 误写成线路已连接。
|
|
4
|
+
// 另检查安装产物一致性(bin/ 实体、服务软链、版本戳、ssh 配置片段)。
|
|
5
|
+
// 退出码:无「需要处理」事项 0;有可行动问题 1。线路关闭或 peer 暂不可用不等于
|
|
6
|
+
// 安装故障;中继不可用会进入处理项。
|
|
7
7
|
import fs from 'node:fs';
|
|
8
8
|
import {
|
|
9
9
|
auditScript, auditLink, runtimeVersionIssue,
|
|
@@ -120,6 +120,7 @@ async function relayPortListening(ctx, port) {
|
|
|
120
120
|
// relay 别名的 User/HostName 由 ssh config Host 块提供
|
|
121
121
|
const r = await ctx.runner.runAsync('ssh', [
|
|
122
122
|
...probeSshOpts(5),
|
|
123
|
+
'-o', 'StrictHostKeyChecking=yes',
|
|
123
124
|
ctx.topo.relay.alias, 'ss', '-tlnH',
|
|
124
125
|
], { timeout: 8000 });
|
|
125
126
|
if (r.code !== 0) return { port, reachable: false, listening: false, detail: r.stderr.trim() };
|
|
@@ -148,7 +149,7 @@ async function collectEdge(ctx) {
|
|
|
148
149
|
// 上面已报「尚未安装」,不重复
|
|
149
150
|
if (entityAudits.some((a) => a.name === SN_ROUTE && a.audit === 'ok')) {
|
|
150
151
|
const issue = confFragmentIssue(
|
|
151
|
-
paths.routesFile, renderRoutes(topo, ctx.self.alias), 'ssh
|
|
152
|
+
paths.routesFile, renderRoutes(topo, ctx.self.alias), 'ssh 自动选路配置',
|
|
152
153
|
);
|
|
153
154
|
if (issue) issues.push(issue);
|
|
154
155
|
}
|
|
@@ -168,14 +169,14 @@ async function collectEdge(ctx) {
|
|
|
168
169
|
? ctx.runner.runRawAsync(paths.binScript(SN_IPV6), [], { timeout: 5000 })
|
|
169
170
|
: Promise.resolve(null);
|
|
170
171
|
|
|
171
|
-
//
|
|
172
|
-
//
|
|
172
|
+
// 就绪实测并发。relay 关闭时不探测;peer 软关闭只停止主动恢复,已有直连
|
|
173
|
+
// 仍可能被新 ssh 使用,所以只要 relay 开着就继续做 banner 探测
|
|
173
174
|
const relayOk = ctx.relayOk ?? relayPathOk; // 测试可注入
|
|
174
175
|
const openTcp = ctx.openTcp ?? tcpOpen;
|
|
175
176
|
const forwards = linkInstalled && switches.relay ? topo.forwards : [];
|
|
176
177
|
const [relayReady, peerReady, portsOk, v6Probe] = await Promise.all([
|
|
177
178
|
switches.relay ? relayOk(ctx) : null,
|
|
178
|
-
switches.relay
|
|
179
|
+
switches.relay ? peerProbeOk(ctx.runner, paths.binDir) : null,
|
|
179
180
|
Promise.all(forwards.map((f) => openTcp('127.0.0.1', f.listenPort))),
|
|
180
181
|
v6P,
|
|
181
182
|
]);
|
|
@@ -188,6 +189,9 @@ async function collectEdge(ctx) {
|
|
|
188
189
|
v6ProbeOk = c.state !== 'failed';
|
|
189
190
|
}
|
|
190
191
|
|
|
192
|
+
if (switches.relay && relayReady === false)
|
|
193
|
+
issues.push(`中继线路不可用——检查 ${topo.relay.alias} 可达性,以及 core 上的 ssh-net status`);
|
|
194
|
+
|
|
191
195
|
return {
|
|
192
196
|
role: 'edge', alias: ctx.self.alias, switches, relayReady, peerReady,
|
|
193
197
|
linkInstalled, job,
|
|
@@ -283,68 +287,81 @@ const exitNote = (code) => (code && code !== '0' ? `(上次退出 ${code})`
|
|
|
283
287
|
/** 线路行的统一形状:✓ 就绪 / ✗ 未就绪。「关」只有 peer 有,由调用方自写。 */
|
|
284
288
|
function lineOf(name, ready, okText, failText) {
|
|
285
289
|
return ready
|
|
286
|
-
? ` ${name}
|
|
287
|
-
: ` ${name}
|
|
290
|
+
? ` ${name}:✓ ${okText}`
|
|
291
|
+
: ` ${name}:✗ ${failText}`;
|
|
288
292
|
}
|
|
289
293
|
|
|
290
294
|
/** core/node 共用的 relay 状态行。 */
|
|
291
295
|
function relayStatusLine(s) {
|
|
292
296
|
return s.relayPort.listening
|
|
293
|
-
? ` relay
|
|
294
|
-
: ` relay
|
|
297
|
+
? ` relay:✓ 可用(外部可经 ${s.relayAlias} 进入 ${s.alias} · 入口 ${s.relayPort.port})`
|
|
298
|
+
: ` relay:✗ ${s.relayPort.reachable === false ? `连不上 ${s.relayAlias},入口状态未知` : `入口 ${s.relayPort.port} 没在监听`}`;
|
|
295
299
|
}
|
|
296
300
|
|
|
297
301
|
function renderEdgeLines(s, lines) {
|
|
298
302
|
const sw = s.switches;
|
|
299
303
|
if (!sw.relay) {
|
|
300
|
-
lines.push('
|
|
304
|
+
lines.push(' 开关:全关(ssh 使用静态 Host 地址;恢复自动选路:ssh-net start)');
|
|
301
305
|
} else {
|
|
302
|
-
lines.push(`
|
|
306
|
+
lines.push(` 开关:relay 开 · peer ${sw.peer ? '开(允许主动恢复直连)' : '关(只停止主动恢复)'}`);
|
|
303
307
|
lines.push(lineOf('relay', s.relayReady,
|
|
304
308
|
`可用(经 ${s.relayAlias} 连到 ${s.coreAlias})`,
|
|
305
|
-
`不可用(经 ${s.relayAlias} 连不上 ${s.coreAlias}
|
|
309
|
+
`不可用(经 ${s.relayAlias} 连不上 ${s.coreAlias};刚开启请等几秒,持续出现查 ${s.relayAlias} 或 core 侧 ssh-net status)`));
|
|
306
310
|
const peerUnready = !s.v6ProbeOk
|
|
307
|
-
? '未就绪(本机
|
|
311
|
+
? '未就绪(本机 IPv6 探测失败,直连状态未知)'
|
|
308
312
|
: s.v6 === ''
|
|
309
|
-
? '
|
|
310
|
-
: '
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
`已就绪(v6 直达 ${s.coreAlias},一条 ssh)`,
|
|
313
|
+
? '未就绪(当前网络没有可用 IPv6)'
|
|
314
|
+
: '未就绪(core 尚未连过来;可运行 ssh-net doctor 立即重试)';
|
|
315
|
+
if (sw.peer) {
|
|
316
|
+
lines.push(lineOf('peer', s.peerReady,
|
|
317
|
+
`IPv6 直连已就绪(到 ${s.coreAlias},一条 ssh)`,
|
|
315
318
|
peerUnready));
|
|
319
|
+
} else if (s.peerReady) {
|
|
320
|
+
lines.push(' peer:✓ 现有 IPv6 直连仍可用(不主动恢复)');
|
|
321
|
+
} else {
|
|
322
|
+
lines.push(' peer:关(当前无可用直连;不主动恢复)');
|
|
323
|
+
}
|
|
316
324
|
if (s.linkInstalled && s.forwardViews.length > 0) {
|
|
317
325
|
const bad = s.forwardViews.filter(([, ok]) => !ok).map(([l]) => l);
|
|
318
326
|
lines.push(lineOf('link', bad.length === 0,
|
|
319
327
|
`就绪(${s.forwardViews.map(([l]) => l).join(' · ')})`,
|
|
320
|
-
`未就绪(${bad.join('、')}
|
|
328
|
+
`未就绪(${bad.join('、')} 没在听;检查本地转发任务、中继线路与 ${s.coreAlias} 上的目标服务)`));
|
|
321
329
|
}
|
|
322
330
|
lines.push(s.v6 !== ''
|
|
323
|
-
? `
|
|
331
|
+
? ` 对外地址:${s.v6}`
|
|
324
332
|
: !s.v6ProbeOk
|
|
325
|
-
? '
|
|
326
|
-
:
|
|
333
|
+
? ' 对外地址:未知(IPv6 探测失败,稍后再试)'
|
|
334
|
+
: ' 对外地址:当前没有可用 IPv6');
|
|
327
335
|
}
|
|
328
336
|
lines.push(s.linkInstalled
|
|
329
|
-
? ` link
|
|
330
|
-
: ' link
|
|
337
|
+
? ` 本地转发(link):已安装(后台任务${s.job.running ? '运行中' : '未运行'})`
|
|
338
|
+
: ' 本地转发(link):未安装(需要 SOCKS/VNC 端口:ssh-net install --with-link)');
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function edgeTaskState(e) {
|
|
342
|
+
const failed = Boolean(e.lastExitCode && e.lastExitCode !== '0');
|
|
343
|
+
if (!e.registered) {
|
|
344
|
+
return e.v6
|
|
345
|
+
? `当前未加载(曾收到地址 ${e.v6};它下次连接时自动恢复)`
|
|
346
|
+
: '尚未建立(等这台笔记本第一次发起连接)';
|
|
347
|
+
}
|
|
348
|
+
if (e.disabled) {
|
|
349
|
+
if (e.running) return '已标记休眠,进程尚未退出';
|
|
350
|
+
if (failed) return `已标记休眠,但上次退出 ${e.lastExitCode};launchd 仍可能自动重拉`;
|
|
351
|
+
return '休眠中(这台笔记本下次 ssh 或 start peer 时自动唤醒)';
|
|
352
|
+
}
|
|
353
|
+
if (e.running) return `运行中(仅表示任务进程,不代表线路已通)${exitNote(e.lastExitCode)}`;
|
|
354
|
+
if (failed) return `未运行(上次退出 ${e.lastExitCode},launchd 会自动重拉)`;
|
|
355
|
+
return '未运行(对端下次通知时会重新拉起)';
|
|
331
356
|
}
|
|
332
357
|
|
|
333
358
|
function renderCoreLines(s, lines) {
|
|
334
359
|
lines.push(relayStatusLine(s));
|
|
335
|
-
lines.push(` relay
|
|
336
|
-
lines.push(' edge
|
|
360
|
+
lines.push(` relay 进程:${s.relayJob.running ? `运行中${exitNote(s.relayJob.lastExitCode)}` : '未运行'}`);
|
|
361
|
+
lines.push(' edge 自动连接任务:');
|
|
337
362
|
for (const e of s.edges) {
|
|
338
|
-
//
|
|
339
|
-
|
|
340
|
-
const state = !e.registered
|
|
341
|
-
? (e.v6
|
|
342
|
-
? `曾连接过(地址 ${e.v6}),现在没在拨——休眠/重启后的正常态,它下次来连时自动恢复`
|
|
343
|
-
: '从未连接(到场装机后自动出现)')
|
|
344
|
-
: e.disabled ? '休眠中(这台笔记本下次 ssh 或 start peer 时自动唤醒)'
|
|
345
|
-
: e.running ? `已连接(地址 ${e.v6 ?? '?'})${exitNote(e.lastExitCode)}`
|
|
346
|
-
: '已断开(自动重试中)';
|
|
347
|
-
lines.push(` ${e.alias} ${state}`);
|
|
363
|
+
// 这里只报告 launchd 任务,不把进程 running 当作 SSH 线路已连通
|
|
364
|
+
lines.push(` ${e.alias} ${edgeTaskState(e)}`);
|
|
348
365
|
lines.push(` ${addrNote(e.entry)}`);
|
|
349
366
|
}
|
|
350
367
|
}
|
|
@@ -355,12 +372,12 @@ export function renderText(s) {
|
|
|
355
372
|
else if (s.role === 'core') renderCoreLines(s, lines);
|
|
356
373
|
else {
|
|
357
374
|
lines.push(relayStatusLine(s));
|
|
358
|
-
lines.push(` relay
|
|
359
|
-
lines.push(' edge
|
|
375
|
+
lines.push(` relay 进程:${s.active === 'active' ? '运行中(systemd)' : `${s.active}(systemd)`}`);
|
|
376
|
+
lines.push(' edge 的 ssh 入口:');
|
|
360
377
|
for (const e of s.entries) lines.push(` ${e.alias} ${addrNote(e)}`);
|
|
361
378
|
}
|
|
362
379
|
if (s.issues.length) {
|
|
363
|
-
lines.push('
|
|
380
|
+
lines.push('需要处理:');
|
|
364
381
|
for (const i of s.issues) lines.push(` - ${i}`);
|
|
365
382
|
}
|
|
366
383
|
return lines.join('\n');
|
|
@@ -6,9 +6,9 @@
|
|
|
6
6
|
import * as launchd from '../lib/launchd.mjs';
|
|
7
7
|
import * as systemd from '../lib/systemd.mjs';
|
|
8
8
|
import { reapEdgeLink } from '../lib/jobctl.mjs';
|
|
9
|
-
import { assertUnitSwitchArgs } from '../lib/cliargs.mjs';
|
|
9
|
+
import { assertUnitSwitchArgs, recoveryResult } from '../lib/cliargs.mjs';
|
|
10
10
|
import { SWITCH_NAMES } from '../lib/state.mjs';
|
|
11
|
-
import { relayLabel, relayUnitName } from '../lib/render.mjs';
|
|
11
|
+
import { linkLabel, relayLabel, relayLinkName, relayUnitName } from '../lib/render.mjs';
|
|
12
12
|
|
|
13
13
|
function parseTarget(args) {
|
|
14
14
|
if (args.length === 0 || args.length === 1 && (args[0] === '-a' || args[0] === '--all'))
|
|
@@ -20,18 +20,57 @@ function parseTarget(args) {
|
|
|
20
20
|
throw new Error('用法:stop [-a|--all|relay|peer](不带参数或 -a=全部关闭)');
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
/** 三类停止原语:正常与恢复路径只负责发现 label/unit,顺序和失败处理共用。 */
|
|
24
|
+
function stopRelayJob(ctx, label) {
|
|
25
25
|
const d = launchd.disable(ctx.runner, ctx.uid, label);
|
|
26
|
-
if (d.code !== 0) throw new Error(`disable
|
|
26
|
+
if (d.code !== 0) throw new Error(`disable 失败(${label}):${d.stderr.trim()}`);
|
|
27
27
|
const bo = launchd.bootout(ctx.runner, ctx.uid, label);
|
|
28
28
|
// 113 = 未注册(竞态下消失);其余失败必须报,否则 stop 谎报成功、隧道还活着
|
|
29
29
|
if (bo.code !== 0 && bo.code !== 113)
|
|
30
|
-
throw new Error(`bootout
|
|
31
|
-
|
|
30
|
+
throw new Error(`bootout 失败(${label} 仍在注册表):${bo.stderr.trim()}`);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function stopNodeUnit(ctx, unit) {
|
|
34
|
+
systemd.stopIfLoaded(ctx.runner, unit); // not-loaded(未装/已卸)无害,真失败抛出
|
|
35
|
+
systemd.disableIfInstalled(ctx.runner, unit); // 未装/已卸幂等,与 mac bootout 113 同口径
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function reapLinkOrThrow(ctx, label) {
|
|
39
|
+
const { gone } = reapEdgeLink(ctx, label);
|
|
40
|
+
if (!gone)
|
|
41
|
+
throw new Error(`${label} 发 TERM 后仍在运行。手工排查:launchctl print gui/${ctx.uid}/${label}`);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** 配置不可用:只按磁盘上的 link/relay 任务全停,不猜 topology,也不动 edge 回拨任务。 */
|
|
45
|
+
function stopRecoveryResult(ctx, action) {
|
|
46
|
+
return `${recoveryResult(ctx, action)}\n修复配置:dot apply ssh;之后运行 ssh-net status 检查`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function stopRecovery(ctx, args) {
|
|
50
|
+
assertUnitSwitchArgs(args, 'stop',
|
|
51
|
+
'配置不可用时 stop 只支持不带参数或 -a/--all;修复配置后再按 relay/peer 分别操作');
|
|
52
|
+
if ((ctx.platform ?? process.platform) === 'linux') {
|
|
53
|
+
const units = systemd.unitNamesOnDisk(ctx.paths.systemdUnitDir, relayLinkName(''));
|
|
54
|
+
if (units.length > 0 && ctx.uid !== 0)
|
|
55
|
+
throw new Error('停止 node 的中继服务需要 root,请运行 sudo -i ssh-net stop');
|
|
56
|
+
ctx.state.removeOn(['relay', 'peer']);
|
|
57
|
+
for (const unit of units) stopNodeUnit(ctx, unit);
|
|
58
|
+
return stopRecoveryResult(ctx, units.length
|
|
59
|
+
? `已停止并禁用 ${units.length} 个中继服务`
|
|
60
|
+
: '未发现中继服务;已清除本机开关');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const links = launchd.labelsOnDisk(ctx.paths.launchAgentsDir, linkLabel(''));
|
|
64
|
+
const relays = launchd.labelsOnDisk(ctx.paths.launchAgentsDir, relayLabel(''));
|
|
65
|
+
ctx.state.removeOn(['relay', 'peer']);
|
|
66
|
+
for (const label of links) reapLinkOrThrow(ctx, label);
|
|
67
|
+
for (const label of relays) stopRelayJob(ctx, label);
|
|
68
|
+
return stopRecoveryResult(ctx,
|
|
69
|
+
`已清除 relay/peer 开关,停止 ${links.length} 个本地转发任务和 ${relays.length} 个中继任务`);
|
|
32
70
|
}
|
|
33
71
|
|
|
34
72
|
export function run(ctx, args = []) {
|
|
73
|
+
if (ctx.configError) return stopRecovery(ctx, args);
|
|
35
74
|
if (ctx.role === 'edge') {
|
|
36
75
|
const target = parseTarget(args);
|
|
37
76
|
const before = ctx.state.readSwitches();
|
|
@@ -41,34 +80,31 @@ export function run(ctx, args = []) {
|
|
|
41
80
|
if (!before.peer) return 'peer 已是关闭状态(无需改动)';
|
|
42
81
|
// 软关:不再通知 core 来连。已建立的连接继续用;装了 link,其转发 ssh
|
|
43
82
|
// 重连时也不会通知,自然只走中继
|
|
44
|
-
return 'peer
|
|
83
|
+
return 'peer 已关闭:不再主动通知 core 恢复 IPv6 直连;现有直连断开前仍可供新 ssh 使用,断开后走中继';
|
|
45
84
|
}
|
|
46
85
|
// 全关 / 只关 relay:级联删两开关(peer 依赖 relay),硬停 link job
|
|
47
86
|
// (PathState 见 RELAY_ON 消失不再重拉;未装附加件直接 gone)
|
|
48
87
|
const alreadyAllOff = !before.relay && !before.peer;
|
|
49
88
|
ctx.state.removeOn(['relay', 'peer']);
|
|
50
|
-
|
|
51
|
-
if (!gone)
|
|
52
|
-
throw new Error(
|
|
53
|
-
`${label} 发 TERM 后仍在运行。手工排查:launchctl print gui/${ctx.uid}/${label}`,
|
|
54
|
-
);
|
|
89
|
+
reapLinkOrThrow(ctx, linkLabel(ctx.topo.core.alias));
|
|
55
90
|
if (target === null) {
|
|
56
91
|
return alreadyAllOff
|
|
57
92
|
? '隧道已是关闭状态(无需改动)'
|
|
58
|
-
: '
|
|
93
|
+
: '隧道已关闭;core 会在现有连接退出后停止重试,下次开启自动恢复';
|
|
59
94
|
}
|
|
60
95
|
return alreadyAllOff
|
|
61
96
|
? 'relay 已是关闭状态(peer 本就没开)'
|
|
62
|
-
: 'relay
|
|
97
|
+
: 'relay 和 peer 已一并关闭;core 会在现有连接退出后停止重试,下次开启自动恢复';
|
|
63
98
|
}
|
|
64
99
|
// core/node 是单总开关(等同 --all):显式 -a/--all 收下,其余参数拒绝
|
|
65
100
|
assertUnitSwitchArgs(args, 'stop');
|
|
66
|
-
if (ctx.role === 'core')
|
|
101
|
+
if (ctx.role === 'core') {
|
|
102
|
+
stopRelayJob(ctx, relayLabel(ctx.topo.relay.alias));
|
|
103
|
+
return '中继通道已关闭(现有 IPv6 直连不受影响)';
|
|
104
|
+
}
|
|
67
105
|
if (ctx.role === 'node') {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
systemd.disableIfInstalled(ctx.runner, unit); // 未装/已卸幂等,与 mac bootout 113 同口径
|
|
71
|
-
return 'relay 已关闭(重启后保持关闭)';
|
|
106
|
+
stopNodeUnit(ctx, relayUnitName(ctx.topo.relay.alias));
|
|
107
|
+
return '中继通道已关闭(重启后保持关闭)';
|
|
72
108
|
}
|
|
73
109
|
throw new Error(`未知角色:${ctx.role}`);
|
|
74
110
|
}
|
|
@@ -8,8 +8,9 @@ import * as launchd from '../lib/launchd.mjs';
|
|
|
8
8
|
import * as systemd from '../lib/systemd.mjs';
|
|
9
9
|
import {
|
|
10
10
|
linkLabel,
|
|
11
|
-
relayLabel, relayUnitName, edgeLabel,
|
|
11
|
+
relayLabel, relayLinkName, relayUnitName, edgeLabel,
|
|
12
12
|
} from '../lib/render.mjs';
|
|
13
|
+
import { recoveryResult } from '../lib/cliargs.mjs';
|
|
13
14
|
|
|
14
15
|
const rm = (p) => fs.rmSync(p, { recursive: true, force: true });
|
|
15
16
|
|
|
@@ -23,11 +24,46 @@ function removeArtifacts(ctx) {
|
|
|
23
24
|
rm(ctx.paths.linkEnabledFile);
|
|
24
25
|
}
|
|
25
26
|
|
|
27
|
+
/** 日志也按工具独占前缀清理,覆盖 plist 已丢但日志仍在的恢复场景。 */
|
|
28
|
+
function removeSshNetLogs(ctx) {
|
|
29
|
+
let files = [];
|
|
30
|
+
try { files = fs.readdirSync(ctx.paths.logsDir); } catch (err) {
|
|
31
|
+
if (err.code !== 'ENOENT') throw err;
|
|
32
|
+
}
|
|
33
|
+
for (const file of files) {
|
|
34
|
+
if (file.startsWith('local.ssh-net-') && file.endsWith('.log'))
|
|
35
|
+
rm(path.join(ctx.paths.logsDir, file));
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** 删除原语由正常与恢复路径共用;目标清单的发现方式留在各入口。 */
|
|
40
|
+
function removeJobs(ctx, labels) {
|
|
41
|
+
for (const label of labels) removeJob(ctx, label, ctx.paths.plistFile(label));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function removeNodeUnits(ctx, units) {
|
|
45
|
+
for (const unit of units) {
|
|
46
|
+
// not-loaded(未装/已卸)无害,真失败必须报——否则删了 unit 文件、
|
|
47
|
+
// systemd 里留下 ghost 态的在跑进程
|
|
48
|
+
systemd.stopIfLoaded(ctx.runner, unit);
|
|
49
|
+
systemd.disableIfInstalled(ctx.runner, unit);
|
|
50
|
+
rm(path.join(ctx.paths.systemdUnitDir, unit));
|
|
51
|
+
}
|
|
52
|
+
// daemon-reload 失败不能吞:systemd 仍持有已删 unit,Restart=always 会
|
|
53
|
+
// 留着/重拉 ghost 进程(与 stopIfLoaded 注释防的是同一个谎报成功)
|
|
54
|
+
const dr = systemd.daemonReload(ctx.runner);
|
|
55
|
+
if (dr.code !== 0) throw new Error(`systemctl daemon-reload 失败:${dr.stderr.trim()}`);
|
|
56
|
+
}
|
|
57
|
+
|
|
26
58
|
function uninstallEdge(ctx) {
|
|
27
|
-
|
|
28
|
-
|
|
59
|
+
// 当前 topology 只提供现名;磁盘 glob 同时覆盖 core 改名前的 link job
|
|
60
|
+
const labels = new Set([
|
|
61
|
+
linkLabel(ctx.topo.core.alias),
|
|
62
|
+
...launchd.labelsOnDisk(ctx.paths.launchAgentsDir, linkLabel('')),
|
|
63
|
+
]);
|
|
64
|
+
removeJobs(ctx, labels);
|
|
29
65
|
removeArtifacts(ctx);
|
|
30
|
-
|
|
66
|
+
removeSshNetLogs(ctx);
|
|
31
67
|
return '已卸载。最后一步:npm uninstall -g ssh-net';
|
|
32
68
|
}
|
|
33
69
|
|
|
@@ -38,38 +74,57 @@ function uninstallCore(ctx) {
|
|
|
38
74
|
edgeJobs.set(edgeLabel(alias), alias);
|
|
39
75
|
for (const e of ctx.topo.edges)
|
|
40
76
|
edgeJobs.set(edgeLabel(e.alias), e.alias);
|
|
41
|
-
|
|
42
|
-
removeJob(ctx, label, ctx.paths.plistFile(label));
|
|
77
|
+
removeJobs(ctx, edgeJobs.keys());
|
|
43
78
|
|
|
44
|
-
const
|
|
45
|
-
|
|
79
|
+
const relays = new Set([
|
|
80
|
+
relayLabel(ctx.topo.relay.alias),
|
|
81
|
+
...launchd.labelsOnDisk(ctx.paths.launchAgentsDir, relayLabel('')),
|
|
82
|
+
]);
|
|
83
|
+
removeJobs(ctx, relays);
|
|
46
84
|
|
|
47
85
|
removeArtifacts(ctx); // 全部 edge-<别名> 动态片段随 conf.d 整棵删
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
return `已卸载(relay 通道与 ${edgeJobs.size} 台笔记本的自动连接任务)。最后一步:npm uninstall -g ssh-net`;
|
|
86
|
+
removeSshNetLogs(ctx);
|
|
87
|
+
return `已卸载(中继通道与 ${edgeJobs.size} 台笔记本的自动连接任务)。最后一步:npm uninstall -g ssh-net`;
|
|
51
88
|
}
|
|
52
89
|
|
|
53
90
|
function uninstallNode(ctx) {
|
|
54
91
|
if (ctx.uid !== 0)
|
|
55
92
|
throw new Error('node 卸载写 /etc/systemd/system 需要 root,请以 root 运行');
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
rm(path.join(ctx.paths.systemdUnitDir, unit));
|
|
62
|
-
// daemon-reload 失败不能吞:systemd 仍持有已删 unit,Restart=always 会
|
|
63
|
-
// 留着/重拉 ghost 进程(与 stopIfLoaded 注释防的是同一个谎报成功)
|
|
64
|
-
const dr = systemd.daemonReload(ctx.runner);
|
|
65
|
-
if (dr.code !== 0) throw new Error(`systemctl daemon-reload 失败:${dr.stderr.trim()}`);
|
|
93
|
+
const units = new Set([
|
|
94
|
+
relayUnitName(ctx.topo.relay.alias),
|
|
95
|
+
...systemd.unitNamesOnDisk(ctx.paths.systemdUnitDir, relayLinkName('')),
|
|
96
|
+
]);
|
|
97
|
+
removeNodeUnits(ctx, units);
|
|
66
98
|
removeArtifacts(ctx);
|
|
67
99
|
return '已卸载。最后一步:npm uninstall -g ssh-net';
|
|
68
100
|
}
|
|
69
101
|
|
|
102
|
+
/** 配置不可用:按磁盘上的全部 ssh-net 任务卸载,不需要猜本机角色或别名。 */
|
|
103
|
+
function uninstallRecovery(ctx) {
|
|
104
|
+
if ((ctx.platform ?? process.platform) === 'linux') {
|
|
105
|
+
const units = systemd.unitNamesOnDisk(ctx.paths.systemdUnitDir, relayLinkName(''));
|
|
106
|
+
if (units.length > 0 && ctx.uid !== 0)
|
|
107
|
+
throw new Error('卸载 node 的中继服务需要 root,请运行 sudo -i ssh-net uninstall');
|
|
108
|
+
if (units.length > 0) removeNodeUnits(ctx, units);
|
|
109
|
+
removeArtifacts(ctx);
|
|
110
|
+
return recoveryResult(ctx, `已删除 ${units.length} 个中继服务及本机 ssh-net 产物。最后一步:npm uninstall -g ssh-net`);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const labels = new Set([
|
|
114
|
+
...launchd.labelsOnDisk(ctx.paths.launchAgentsDir, linkLabel('')),
|
|
115
|
+
...launchd.labelsOnDisk(ctx.paths.launchAgentsDir, relayLabel('')),
|
|
116
|
+
...launchd.labelsOnDisk(ctx.paths.launchAgentsDir, edgeLabel('')),
|
|
117
|
+
]);
|
|
118
|
+
removeJobs(ctx, labels);
|
|
119
|
+
removeArtifacts(ctx);
|
|
120
|
+
removeSshNetLogs(ctx);
|
|
121
|
+
return recoveryResult(ctx, `已停止并删除 ${labels.size} 个后台任务及本机 ssh-net 产物。最后一步:npm uninstall -g ssh-net`);
|
|
122
|
+
}
|
|
123
|
+
|
|
70
124
|
export function run(ctx, args = []) {
|
|
71
125
|
if (args.length)
|
|
72
126
|
throw new Error('用法:uninstall 不带参数');
|
|
127
|
+
if (ctx.configError) return uninstallRecovery(ctx);
|
|
73
128
|
switch (ctx.role) {
|
|
74
129
|
case 'edge': return uninstallEdge(ctx);
|
|
75
130
|
case 'core': return uninstallCore(ctx);
|