ssh-net 26.10.58 → 26.10.60
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 +30 -8
- package/README.md +9 -9
- package/libexec/cli.mjs +26 -43
- package/libexec/commands/install.mjs +20 -71
- package/libexec/commands/start.mjs +9 -15
- package/libexec/commands/status.mjs +43 -49
- package/libexec/commands/stop.mjs +17 -63
- package/libexec/commands/uninstall.mjs +12 -72
- package/libexec/commands/update.mjs +22 -38
- package/libexec/lib/jobctl.mjs +2 -3
- package/libexec/lib/jobs.mjs +62 -0
- package/libexec/lib/manage.mjs +13 -15
- package/libexec/lib/paths.mjs +5 -4
- package/libexec/lib/runtime.mjs +25 -64
- package/libexec/lib/systemd.mjs +4 -3
- package/libexec/sn-route +14 -23
- package/package.json +1 -1
- package/libexec/lib/cliargs.mjs +0 -18
|
@@ -1,26 +1,21 @@
|
|
|
1
|
-
// stop
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
// --without-link)。重复 stop 是幂等确认,措辞区分「已关闭」与「已是关闭状态」。
|
|
5
|
-
// core disable+bootout uplink-relay(不动 uplink-edge);node stop+disable。
|
|
1
|
+
// stop:只按磁盘上的 ssh-net 任务操作,不读 topology。peer 只删开关,
|
|
2
|
+
// 不闪断已有回拨;relay/缺省/-a/--all 级联清两开关,并停止本机全部
|
|
3
|
+
// link/relay 任务。core 的 uplink-edge 不在停止范围内(人工全清用 uninstall)。
|
|
6
4
|
import * as launchd from '../lib/launchd.mjs';
|
|
7
5
|
import * as systemd from '../lib/systemd.mjs';
|
|
8
6
|
import { reapEdgeLink } from '../lib/jobctl.mjs';
|
|
9
|
-
import { assertUnitSwitchArgs, recoveryResult } from '../lib/cliargs.mjs';
|
|
10
7
|
import { SWITCH_NAMES } from '../lib/state.mjs';
|
|
11
|
-
import { linkLabel, relayLabel, relayLinkName
|
|
8
|
+
import { linkLabel, relayLabel, relayLinkName } from '../lib/render.mjs';
|
|
12
9
|
|
|
13
10
|
function parseTarget(args) {
|
|
14
|
-
if (args.length === 0 || args.length === 1 &&
|
|
15
|
-
return
|
|
16
|
-
if (args.length === 1 && SWITCH_NAMES.includes(args[0]))
|
|
17
|
-
return args[0];
|
|
11
|
+
if (args.length === 0 || args.length === 1 && ['-a', '--all'].includes(args[0]))
|
|
12
|
+
return 'relay';
|
|
13
|
+
if (args.length === 1 && SWITCH_NAMES.includes(args[0])) return args[0];
|
|
18
14
|
if (args.length === 1 && args[0] === 'link')
|
|
19
15
|
throw new Error('link 不是运行时开关:移除本地转发组件跑 ssh-net install --without-link(卸载会一并清掉)');
|
|
20
16
|
throw new Error('用法:stop [-a|--all|relay|peer](不带参数或 -a=全部关闭)');
|
|
21
17
|
}
|
|
22
18
|
|
|
23
|
-
/** 三类停止原语:正常与恢复路径只负责发现 label/unit,顺序和失败处理共用。 */
|
|
24
19
|
function stopRelayJob(ctx, label) {
|
|
25
20
|
const d = launchd.disable(ctx.runner, ctx.uid, label);
|
|
26
21
|
if (d.code !== 0) throw new Error(`disable 失败(${label}):${d.stderr.trim()}`);
|
|
@@ -41,70 +36,29 @@ function reapLinkOrThrow(ctx, label) {
|
|
|
41
36
|
throw new Error(`${label} 发 TERM 后仍在运行。手工排查:launchctl print gui/${ctx.uid}/${label}`);
|
|
42
37
|
}
|
|
43
38
|
|
|
44
|
-
/**
|
|
45
|
-
function
|
|
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 分别操作');
|
|
39
|
+
/** 停止磁盘发现的全部 link/relay 任务;改名前的残留也一并覆盖。 */
|
|
40
|
+
function stopJobs(ctx) {
|
|
52
41
|
if ((ctx.platform ?? process.platform) === 'linux') {
|
|
53
42
|
const units = systemd.unitNamesOnDisk(ctx.paths.systemdUnitDir, relayLinkName(''));
|
|
54
43
|
if (units.length > 0 && ctx.uid !== 0)
|
|
55
44
|
throw new Error('停止 node 的中继服务需要 root,请运行 sudo -i ssh-net stop');
|
|
56
|
-
ctx.state.removeOn(['relay', 'peer']);
|
|
57
45
|
for (const unit of units) stopNodeUnit(ctx, unit);
|
|
58
|
-
return
|
|
59
|
-
? `已停止并禁用 ${units.length} 个中继服务`
|
|
60
|
-
: '未发现中继服务;已清除本机开关');
|
|
46
|
+
return `已停止并禁用 ${units.length} 个中继服务`;
|
|
61
47
|
}
|
|
62
48
|
|
|
63
49
|
const links = launchd.labelsOnDisk(ctx.paths.launchAgentsDir, linkLabel(''));
|
|
64
50
|
const relays = launchd.labelsOnDisk(ctx.paths.launchAgentsDir, relayLabel(''));
|
|
65
|
-
ctx.state.removeOn(['relay', 'peer']);
|
|
66
51
|
for (const label of links) reapLinkOrThrow(ctx, label);
|
|
67
52
|
for (const label of relays) stopRelayJob(ctx, label);
|
|
68
|
-
return
|
|
69
|
-
`已清除 relay/peer 开关,停止 ${links.length} 个本地转发任务和 ${relays.length} 个中继任务`);
|
|
53
|
+
return `已停止 ${links.length} 个本地转发任务和 ${relays.length} 个中继任务`;
|
|
70
54
|
}
|
|
71
55
|
|
|
72
56
|
export function run(ctx, args = []) {
|
|
73
|
-
|
|
74
|
-
if (
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
if (target === 'peer') {
|
|
79
|
-
ctx.state.removeOn(['peer']);
|
|
80
|
-
if (!before.peer) return 'peer 已是关闭状态(无需改动)';
|
|
81
|
-
// 软关:不再通知 core 来连。已建立的连接继续用;装了 link,其转发 ssh
|
|
82
|
-
// 重连时也不会通知,自然只走中继
|
|
83
|
-
return 'peer 已关闭:不再主动通知 core 恢复 IPv6 直连;现有直连断开前仍可供新 ssh 使用,断开后走中继';
|
|
84
|
-
}
|
|
85
|
-
// 全关 / 只关 relay:级联删两开关(peer 依赖 relay),硬停 link job
|
|
86
|
-
// (PathState 见 RELAY_ON 消失不再重拉;未装附加件直接 gone)
|
|
87
|
-
const alreadyAllOff = !before.relay && !before.peer;
|
|
88
|
-
ctx.state.removeOn(['relay', 'peer']);
|
|
89
|
-
reapLinkOrThrow(ctx, linkLabel(ctx.topo.core.alias));
|
|
90
|
-
if (target === null) {
|
|
91
|
-
return alreadyAllOff
|
|
92
|
-
? '隧道已是关闭状态(无需改动)'
|
|
93
|
-
: '隧道已关闭;core 会在现有连接退出后停止重试,下次开启自动恢复';
|
|
94
|
-
}
|
|
95
|
-
return alreadyAllOff
|
|
96
|
-
? 'relay 已是关闭状态(peer 本就没开)'
|
|
97
|
-
: 'relay 和 peer 已一并关闭;core 会在现有连接退出后停止重试,下次开启自动恢复';
|
|
57
|
+
const target = parseTarget(args);
|
|
58
|
+
if (target === 'peer') {
|
|
59
|
+
ctx.state.removeOn(['peer']);
|
|
60
|
+
return 'peer 已关闭:不再主动恢复 IPv6 直连;现有直连断开前仍可供新 ssh 使用,断开后走中继';
|
|
98
61
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
if (ctx.role === 'core') {
|
|
102
|
-
stopRelayJob(ctx, relayLabel(ctx.topo.relay.alias));
|
|
103
|
-
return '中继通道已关闭(现有 IPv6 直连不受影响)';
|
|
104
|
-
}
|
|
105
|
-
if (ctx.role === 'node') {
|
|
106
|
-
stopNodeUnit(ctx, relayUnitName(ctx.topo.relay.alias));
|
|
107
|
-
return '中继通道已关闭(重启后保持关闭)';
|
|
108
|
-
}
|
|
109
|
-
throw new Error(`未知角色:${ctx.role}`);
|
|
62
|
+
ctx.state.removeOn(['relay', 'peer']);
|
|
63
|
+
return `relay 和 peer 已一并关闭;${stopJobs(ctx)}`;
|
|
110
64
|
}
|
|
@@ -1,30 +1,25 @@
|
|
|
1
|
-
// uninstall
|
|
2
|
-
//
|
|
3
|
-
//
|
|
1
|
+
// uninstall:只按磁盘上的 ssh-net 任务与产物清理,不读 topology。
|
|
2
|
+
// 先 bootout/stop 全部 job(TERM trap 转发,不留孤儿 ssh),再删
|
|
3
|
+
// plist/unit、运行时脚本、状态与日志;topology 改名前的残留由 glob 覆盖。
|
|
4
4
|
import fs from 'node:fs';
|
|
5
5
|
import path from 'node:path';
|
|
6
6
|
import { removeJob } from '../lib/manage.mjs';
|
|
7
7
|
import * as launchd from '../lib/launchd.mjs';
|
|
8
8
|
import * as systemd from '../lib/systemd.mjs';
|
|
9
9
|
import {
|
|
10
|
-
linkLabel,
|
|
11
|
-
relayLabel, relayLinkName, relayUnitName, edgeLabel,
|
|
10
|
+
linkLabel, relayLabel, relayLinkName, edgeLabel,
|
|
12
11
|
} from '../lib/render.mjs';
|
|
13
|
-
import { recoveryResult } from '../lib/cliargs.mjs';
|
|
14
12
|
|
|
15
13
|
const rm = (p) => fs.rmSync(p, { recursive: true, force: true });
|
|
16
14
|
|
|
17
|
-
/** 三角色共同产物:整棵 bin/state/conf.d + 版本戳 + link 意图 marker
|
|
18
|
-
* (marker 只 edge 可能有,其余角色 force rm 不存在的文件无害)。 */
|
|
19
15
|
function removeArtifacts(ctx) {
|
|
20
16
|
rm(ctx.paths.binDir);
|
|
21
17
|
rm(ctx.paths.stateDir);
|
|
22
18
|
rm(ctx.paths.confDir);
|
|
23
|
-
rm(ctx.paths.
|
|
19
|
+
rm(ctx.paths.legacyRuntimeVersionFile);
|
|
24
20
|
rm(ctx.paths.linkEnabledFile);
|
|
25
21
|
}
|
|
26
22
|
|
|
27
|
-
/** 日志也按工具独占前缀清理,覆盖 plist 已丢但日志仍在的恢复场景。 */
|
|
28
23
|
function removeSshNetLogs(ctx) {
|
|
29
24
|
let files = [];
|
|
30
25
|
try { files = fs.readdirSync(ctx.paths.logsDir); } catch (err) {
|
|
@@ -36,7 +31,6 @@ function removeSshNetLogs(ctx) {
|
|
|
36
31
|
}
|
|
37
32
|
}
|
|
38
33
|
|
|
39
|
-
/** 删除原语由正常与恢复路径共用;目标清单的发现方式留在各入口。 */
|
|
40
34
|
function removeJobs(ctx, labels) {
|
|
41
35
|
for (const label of labels) removeJob(ctx, label, ctx.paths.plistFile(label));
|
|
42
36
|
}
|
|
@@ -50,64 +44,22 @@ function removeNodeUnits(ctx, units) {
|
|
|
50
44
|
rm(path.join(ctx.paths.systemdUnitDir, unit));
|
|
51
45
|
}
|
|
52
46
|
// daemon-reload 失败不能吞:systemd 仍持有已删 unit,Restart=always 会
|
|
53
|
-
// 留着/重拉 ghost
|
|
47
|
+
// 留着/重拉 ghost 进程
|
|
54
48
|
const dr = systemd.daemonReload(ctx.runner);
|
|
55
49
|
if (dr.code !== 0) throw new Error(`systemctl daemon-reload 失败:${dr.stderr.trim()}`);
|
|
56
50
|
}
|
|
57
51
|
|
|
58
|
-
function
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
linkLabel(ctx.topo.core.alias),
|
|
62
|
-
...launchd.labelsOnDisk(ctx.paths.launchAgentsDir, linkLabel('')),
|
|
63
|
-
]);
|
|
64
|
-
removeJobs(ctx, labels);
|
|
65
|
-
removeArtifacts(ctx);
|
|
66
|
-
removeSshNetLogs(ctx);
|
|
67
|
-
return '已卸载。最后一步:npm uninstall -g ssh-net';
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function uninstallCore(ctx) {
|
|
71
|
-
// label → plist 路径:磁盘 glob(topology 已删的残留)∪ topology 在册 edge
|
|
72
|
-
const edgeJobs = new Map();
|
|
73
|
-
for (const alias of launchd.edgeAliasesOnDisk(ctx.paths.launchAgentsDir))
|
|
74
|
-
edgeJobs.set(edgeLabel(alias), alias);
|
|
75
|
-
for (const e of ctx.topo.edges)
|
|
76
|
-
edgeJobs.set(edgeLabel(e.alias), e.alias);
|
|
77
|
-
removeJobs(ctx, edgeJobs.keys());
|
|
78
|
-
|
|
79
|
-
const relays = new Set([
|
|
80
|
-
relayLabel(ctx.topo.relay.alias),
|
|
81
|
-
...launchd.labelsOnDisk(ctx.paths.launchAgentsDir, relayLabel('')),
|
|
82
|
-
]);
|
|
83
|
-
removeJobs(ctx, relays);
|
|
84
|
-
|
|
85
|
-
removeArtifacts(ctx); // 全部 edge-<别名> 动态片段随 conf.d 整棵删
|
|
86
|
-
removeSshNetLogs(ctx);
|
|
87
|
-
return `已卸载(中继通道与 ${edgeJobs.size} 台笔记本的自动连接任务)。最后一步:npm uninstall -g ssh-net`;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
function uninstallNode(ctx) {
|
|
91
|
-
if (ctx.uid !== 0)
|
|
92
|
-
throw new Error('node 卸载写 /etc/systemd/system 需要 root,请运行 sudo -i ssh-net uninstall');
|
|
93
|
-
const units = new Set([
|
|
94
|
-
relayUnitName(ctx.topo.relay.alias),
|
|
95
|
-
...systemd.unitNamesOnDisk(ctx.paths.systemdUnitDir, relayLinkName('')),
|
|
96
|
-
]);
|
|
97
|
-
removeNodeUnits(ctx, units);
|
|
98
|
-
removeArtifacts(ctx);
|
|
99
|
-
return '已卸载。最后一步:npm uninstall -g ssh-net';
|
|
100
|
-
}
|
|
52
|
+
export function run(ctx, args = []) {
|
|
53
|
+
if (args.length)
|
|
54
|
+
throw new Error('用法:uninstall 不带参数');
|
|
101
55
|
|
|
102
|
-
/** 配置不可用:按磁盘上的全部 ssh-net 任务卸载,不需要猜本机角色或别名。 */
|
|
103
|
-
function uninstallRecovery(ctx) {
|
|
104
56
|
if ((ctx.platform ?? process.platform) === 'linux') {
|
|
105
57
|
const units = systemd.unitNamesOnDisk(ctx.paths.systemdUnitDir, relayLinkName(''));
|
|
106
58
|
if (units.length > 0 && ctx.uid !== 0)
|
|
107
|
-
throw new Error('
|
|
59
|
+
throw new Error('node 卸载写 /etc/systemd/system 需要 root,请运行 sudo -i ssh-net uninstall');
|
|
108
60
|
if (units.length > 0) removeNodeUnits(ctx, units);
|
|
109
61
|
removeArtifacts(ctx);
|
|
110
|
-
return
|
|
62
|
+
return '已卸载。最后一步:npm uninstall -g ssh-net';
|
|
111
63
|
}
|
|
112
64
|
|
|
113
65
|
const labels = new Set([
|
|
@@ -118,17 +70,5 @@ function uninstallRecovery(ctx) {
|
|
|
118
70
|
removeJobs(ctx, labels);
|
|
119
71
|
removeArtifacts(ctx);
|
|
120
72
|
removeSshNetLogs(ctx);
|
|
121
|
-
return
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
export function run(ctx, args = []) {
|
|
125
|
-
if (args.length)
|
|
126
|
-
throw new Error('用法:uninstall 不带参数');
|
|
127
|
-
if (ctx.configError) return uninstallRecovery(ctx);
|
|
128
|
-
switch (ctx.role) {
|
|
129
|
-
case 'edge': return uninstallEdge(ctx);
|
|
130
|
-
case 'core': return uninstallCore(ctx);
|
|
131
|
-
case 'node': return uninstallNode(ctx);
|
|
132
|
-
default: throw new Error(`未知角色:${ctx.role}`);
|
|
133
|
-
}
|
|
73
|
+
return '已卸载。最后一步:npm uninstall -g ssh-net';
|
|
134
74
|
}
|
|
@@ -6,16 +6,14 @@
|
|
|
6
6
|
// 只做一轮、不轮询等待;知道目标版号时直接 update <版号>。
|
|
7
7
|
//
|
|
8
8
|
// 刻意「换装后重新 exec 新 bin」而不是在当前进程里直接调 installRun:
|
|
9
|
-
//
|
|
10
|
-
//
|
|
11
|
-
// runtime-version 戳与 status 的对账都依赖新进程里的新常量,用旧进程
|
|
12
|
-
// 跑 install 会盖一个旧版本戳、越对账越乱。
|
|
9
|
+
// 当前进程加载的是旧包代码与包内路径,只有新进程才能保证 install 与刚装
|
|
10
|
+
// 上的版本一致。
|
|
13
11
|
//
|
|
14
12
|
// 边界:node 角色需要 root(install 写 /etc/systemd/system),先检查再动
|
|
15
|
-
// npm,避免「包装上了、install 被拒」的半截态;core
|
|
13
|
+
// npm,避免「包装上了、install 被拒」的半截态;core 侧已注册 edge 的
|
|
16
14
|
// uplink-edge 实体随 install 刷新,job 参数仍等各 edge 下次报名对账。
|
|
17
|
-
import { readRuntimeVersion, RUNTIME_VERSION } from '../lib/runtime.mjs';
|
|
18
15
|
import { cmpVersion, VERSION_RE } from '../lib/version.mjs';
|
|
16
|
+
import pkg from '../../package.json' with { type: 'json' };
|
|
19
17
|
|
|
20
18
|
const NPM_TIMEOUT = 180_000; // 慢网络下全局装包的余量
|
|
21
19
|
const QUERY_TIMEOUT = 60_000; // view 只读查询
|
|
@@ -23,6 +21,7 @@ const INSTALL_TIMEOUT = 120_000;
|
|
|
23
21
|
|
|
24
22
|
const ARG_HINT = 'update 只接受一个可选的三段数字版本号';
|
|
25
23
|
const NPM_NOT_FOUND = 'PATH 上找不到 npm——检查 ~/.zshenv(mac)或 .bashrc(Linux)里 node 的 PATH 配置后重试';
|
|
24
|
+
const BIN_NOT_FOUND = 'npm 换装后 PATH 上找不到 ssh-net(全局 bin 未生效)——新开一个 shell 或手动跑一次 ssh-net install;若反复出现,检查 fnm 的 global 包路径';
|
|
26
25
|
|
|
27
26
|
/** 失败详情:stderr 优先;被信号杀(code null,典型是超时)别说成 exit null */
|
|
28
27
|
const failWhy = (r) => r.stderr.trim()
|
|
@@ -70,9 +69,9 @@ export async function run(ctx, args = []) {
|
|
|
70
69
|
const latest = npmLatest(runner);
|
|
71
70
|
if (latest === null)
|
|
72
71
|
throw new Error('没能从 npm view 确认最新版,未执行安装——稍后重试;知道目标版号可直接 ssh-net update <版本号>');
|
|
73
|
-
const relation = cmpVersion(latest,
|
|
72
|
+
const relation = cmpVersion(latest, pkg.version);
|
|
74
73
|
if (relation < 0)
|
|
75
|
-
throw new Error(`npm 返回的最新版 ${latest} 比当前版本 ${
|
|
74
|
+
throw new Error(`npm 返回的最新版 ${latest} 比当前版本 ${pkg.version} 更旧,已停止且未执行安装——稍后重试;知道目标版号可直接 ssh-net update <版本号>`);
|
|
76
75
|
if (relation === 0) return '已是最新版本';
|
|
77
76
|
resolved = latest;
|
|
78
77
|
}
|
|
@@ -81,40 +80,25 @@ export async function run(ctx, args = []) {
|
|
|
81
80
|
const r = npmInstall(runner, resolved);
|
|
82
81
|
if (r.error && r.error.code === 'ENOENT')
|
|
83
82
|
throw new Error(NPM_NOT_FOUND);
|
|
84
|
-
if (r.code !== 0)
|
|
85
|
-
|
|
86
|
-
const why = '还没传播到本机打到的 registry 节点(常见于刚发布几分钟内)';
|
|
87
|
-
const kind = target !== null ? '指定版本' : '最新版';
|
|
88
|
-
const tail = `${kind} ${resolved} ${why}——等几分钟重跑 ssh-net update ${resolved},不用换命令。`;
|
|
89
|
-
throw new Error(`${tail} npm 输出:${r.stderr.trim()}`);
|
|
90
|
-
}
|
|
91
|
-
throw new Error(`npm 安装 ${resolved} 失败:${failWhy(r)}(网络或 registry 问题;已装的版本不受影响,稍后重试)`);
|
|
92
|
-
}
|
|
83
|
+
if (r.code !== 0)
|
|
84
|
+
throw new Error(`npm 安装 ${resolved} 失败:${failWhy(r)}(已装的版本不受影响;网络或 registry 问题稍后重试,知道目标版号可直接 ssh-net update <版本号>)`);
|
|
93
85
|
|
|
94
|
-
// 版号地址 code 0
|
|
95
|
-
//
|
|
86
|
+
// 版号地址 code 0 即装上该版;先核对 PATH 选中的 bin,再让它跑 install。
|
|
87
|
+
// 多全局安装时,npm 更新的目录不一定排在 PATH 首位;旧 bin 成功退出也不代表
|
|
88
|
+
// 目标版本的组件已经同步。这里只核对进程版本,不恢复持久版本戳
|
|
96
89
|
progress('正在同步本机组件…');
|
|
90
|
+
const ver = runner.run('ssh-net', ['--version'], { timeout: INSTALL_TIMEOUT });
|
|
91
|
+
if (ver.error && ver.error.code === 'ENOENT') throw new Error(BIN_NOT_FOUND);
|
|
92
|
+
if (ver.code !== 0)
|
|
93
|
+
throw new Error(`npm 包已切换,但 ssh-net --version 失败:${failWhy(ver)}——检查 PATH 后手动跑一次 ssh-net install`);
|
|
94
|
+
const actual = ver.stdout.trim();
|
|
95
|
+
if (actual !== resolved)
|
|
96
|
+
throw new Error(`npm 包已切换到 ${resolved},但 PATH 上的 ssh-net 版本是 ${actual || '未知'},已停止收尾——用 which -a ssh-net 检查 PATH,再手动运行 ssh-net install`);
|
|
97
|
+
|
|
97
98
|
const inst = runner.run('ssh-net', ['install'], { timeout: INSTALL_TIMEOUT });
|
|
98
|
-
if (inst.error && inst.error.code === 'ENOENT')
|
|
99
|
-
throw new Error('npm 换装后 PATH 上找不到 ssh-net(全局 bin 未生效)——新开一个 shell 或手动跑一次 ssh-net install;若反复出现,检查 fnm 的 global 包路径');
|
|
99
|
+
if (inst.error && inst.error.code === 'ENOENT') throw new Error(BIN_NOT_FOUND);
|
|
100
100
|
if (inst.code !== 0)
|
|
101
101
|
throw new Error(`npm 包已切换,但 install 刷新没成功:${failWhy(inst)}——手动跑一次 ssh-net install 修复`);
|
|
102
102
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
// 后置校验:真换装后新 bin 的 install 必把版本戳刷到刚装上的版本
|
|
106
|
-
// (ensureRuntime 无条件 refreshStamp);戳没跟上说明 PATH 解析到了旧
|
|
107
|
-
// ssh-net,install 跑在旧代码上、升级静默没生效(status 的 runtime-version
|
|
108
|
-
// 对账能查到,但当场报比事后对账快)。同版修复新旧版本恒等,校验无意义,跳过
|
|
109
|
-
const stamp = readRuntimeVersion(ctx.paths);
|
|
110
|
-
const stampWarning = rel !== 0 && stamp !== null && stamp !== resolved
|
|
111
|
-
? `⚠️ 后台脚本版本仍为 ${stamp}——刚才的 install 可能跑了旧 ssh-net;用 which -a ssh-net 检查 PATH,再手动运行 ssh-net install`
|
|
112
|
-
: '';
|
|
113
|
-
|
|
114
|
-
const head = target !== null && rel < 0
|
|
115
|
-
? `已切换到指定版本 ${resolved}(原版本 ${RUNTIME_VERSION},这是降级)`
|
|
116
|
-
: rel === 0
|
|
117
|
-
? `当前已是指定版本 ${resolved}`
|
|
118
|
-
: `已升级到 ${resolved}`;
|
|
119
|
-
return [head, inst.stdout.trim(), stampWarning].filter(Boolean).join('\n');
|
|
103
|
+
return [`已安装 ssh-net@${resolved}`, inst.stdout.trim()].filter(Boolean).join('\n');
|
|
120
104
|
}
|
package/libexec/lib/jobctl.mjs
CHANGED
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
// 开关是 stop 的命令语义。未装 link(label 不在册)直接 gone。超时报错给手工
|
|
4
4
|
// 排障命令,绝不自动 KILL。同步 CLI 用 Atomics.wait 节流。
|
|
5
5
|
import * as launchd from './launchd.mjs';
|
|
6
|
-
import { linkLabel } from './render.mjs';
|
|
7
6
|
|
|
8
7
|
const WAIT_MS = 5_000;
|
|
9
8
|
const POLL_MS = 200;
|
|
@@ -21,10 +20,10 @@ function isGone(ctx, label) {
|
|
|
21
20
|
|
|
22
21
|
/**
|
|
23
22
|
* @param {object} ctx
|
|
24
|
-
* @param {string}
|
|
23
|
+
* @param {string} label 磁盘发现的 link job label
|
|
25
24
|
* @returns {{gone:boolean, label:string}}
|
|
26
25
|
*/
|
|
27
|
-
export function reapEdgeLink(ctx, label
|
|
26
|
+
export function reapEdgeLink(ctx, label) {
|
|
28
27
|
if (!launchd.printState(ctx.runner, ctx.uid, label, true).registered)
|
|
29
28
|
return { gone: true, label };
|
|
30
29
|
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// install 管理的服务配置装配:写盘与 status 体检共用同一份渲染输入,
|
|
2
|
+
// 避免只改 plist/unit 模板时,status 因运行时脚本未变而漏报未同步升级。
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { renderForwardArgs } from './config.mjs';
|
|
6
|
+
import {
|
|
7
|
+
linkLabel, relayLabel, relayUnitName,
|
|
8
|
+
renderEdgeLinkPlist, renderRelayPlist, renderRelayUnit,
|
|
9
|
+
} from './render.mjs';
|
|
10
|
+
|
|
11
|
+
/** edge 本地转发 LaunchAgent。 */
|
|
12
|
+
export function edgeLinkJob(ctx) {
|
|
13
|
+
const { topo, paths } = ctx;
|
|
14
|
+
const label = linkLabel(topo.core.alias);
|
|
15
|
+
return {
|
|
16
|
+
label,
|
|
17
|
+
plistPath: paths.plistFile(label),
|
|
18
|
+
content: renderEdgeLinkPlist({
|
|
19
|
+
binDir: paths.binDir,
|
|
20
|
+
coreAlias: topo.core.alias,
|
|
21
|
+
forwardArgs: renderForwardArgs(topo.forwards, ctx.hostTable),
|
|
22
|
+
relayOnFile: paths.switchFiles.relay,
|
|
23
|
+
logsDir: paths.logsDir,
|
|
24
|
+
}),
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** core 中继 LaunchAgent。 */
|
|
29
|
+
export function coreRelayJob(ctx) {
|
|
30
|
+
const { topo, paths } = ctx;
|
|
31
|
+
const label = relayLabel(topo.relay.alias);
|
|
32
|
+
return {
|
|
33
|
+
label,
|
|
34
|
+
plistPath: paths.plistFile(label),
|
|
35
|
+
content: renderRelayPlist({
|
|
36
|
+
binDir: paths.binDir,
|
|
37
|
+
relayAlias: topo.relay.alias,
|
|
38
|
+
relayPort: topo.core.relayPort,
|
|
39
|
+
logsDir: paths.logsDir,
|
|
40
|
+
}),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** node 中继 systemd unit;home 一并返回供 install 校验 ExecStart 边界。 */
|
|
45
|
+
export function nodeRelayJob(ctx) {
|
|
46
|
+
const { topo, paths } = ctx;
|
|
47
|
+
const unitName = relayUnitName(topo.relay.alias);
|
|
48
|
+
// HOME 用 root 的真实家目录(getpwuid),不取 $HOME:sudo 保留 HOME 会把
|
|
49
|
+
// 调用者的家目录写进 unit,root 的 ssh 随之读错 config
|
|
50
|
+
const home = ctx.nodeHome ?? os.userInfo().homedir;
|
|
51
|
+
return {
|
|
52
|
+
unitName,
|
|
53
|
+
unitPath: path.join(paths.systemdUnitDir, unitName),
|
|
54
|
+
home,
|
|
55
|
+
content: renderRelayUnit({
|
|
56
|
+
binDir: paths.binDir,
|
|
57
|
+
relayAlias: topo.relay.alias,
|
|
58
|
+
relayPort: ctx.self.relayPort,
|
|
59
|
+
home,
|
|
60
|
+
}),
|
|
61
|
+
};
|
|
62
|
+
}
|
package/libexec/lib/manage.mjs
CHANGED
|
@@ -15,7 +15,7 @@ import { readFileOrNull } from './fsutil.mjs';
|
|
|
15
15
|
* 是否踢活:install 对账不踢(不断连),start 要踢
|
|
16
16
|
* @param {boolean} [opts.reviveRunningDisabled=false] registered+disabled 但
|
|
17
17
|
* 还在跑时是否踢重启:install 要 revive,start 只清 override、不闪断
|
|
18
|
-
*
|
|
18
|
+
* 只执行必要的加载/恢复动作;是否有配置变化由 syncPlist 另行判断
|
|
19
19
|
*/
|
|
20
20
|
export function ensureLoaded(ctx, { label, plistPath }, {
|
|
21
21
|
kickWhenStopped = false,
|
|
@@ -25,33 +25,33 @@ export function ensureLoaded(ctx, { label, plistPath }, {
|
|
|
25
25
|
const st = launchd.inspect(runner, uid, label);
|
|
26
26
|
if (!st.registered) {
|
|
27
27
|
launchd.enableBootstrap(runner, uid, label, plistPath);
|
|
28
|
-
return
|
|
28
|
+
return;
|
|
29
29
|
}
|
|
30
30
|
if (st.disabled && (!st.running || reviveRunningDisabled)) {
|
|
31
31
|
const k = launchd.kickstartEnabled(runner, uid, label);
|
|
32
32
|
if (k.code !== 0) throw new Error(`kickstart 失败(${label}):${k.stderr.trim()}`);
|
|
33
|
-
return
|
|
33
|
+
return;
|
|
34
34
|
}
|
|
35
35
|
if (st.disabled) {
|
|
36
36
|
// 在跑但 override 残留:只 enable 清掉,防退出后不重拉,不打断现连接
|
|
37
37
|
const en = launchd.enable(runner, uid, label);
|
|
38
38
|
if (en.code !== 0) throw new Error(`enable 失败(${label}):${en.stderr.trim()}`);
|
|
39
|
-
return
|
|
39
|
+
return;
|
|
40
40
|
}
|
|
41
41
|
if (kickWhenStopped && !st.running) {
|
|
42
42
|
const k = launchd.kickstart(runner, uid, label);
|
|
43
43
|
if (k.code !== 0) throw new Error(`kickstart 失败(${label}):${k.stderr.trim()}`);
|
|
44
|
-
return
|
|
44
|
+
return;
|
|
45
45
|
}
|
|
46
|
-
return 'unchanged';
|
|
47
46
|
}
|
|
48
47
|
|
|
49
48
|
/**
|
|
50
49
|
* 同步一个 plist 到磁盘并按需注册(幂等、不断连)。
|
|
51
50
|
* @param {object} ctx
|
|
52
51
|
* @param {{label:string, plistPath:string, content:string}} job
|
|
53
|
-
* @returns {{
|
|
54
|
-
*
|
|
52
|
+
* @returns {{changed:boolean, restarted?:boolean, lintError?:string}}
|
|
53
|
+
* changed 只表示 plist 内容变化,服务恢复不计入;restarted 只在替换已注册
|
|
54
|
+
* job 的配置时为真,用于提示可能断连
|
|
55
55
|
*/
|
|
56
56
|
export function syncPlist(ctx, { label, plistPath, content }) {
|
|
57
57
|
const { runner, uid, paths } = ctx;
|
|
@@ -62,13 +62,11 @@ export function syncPlist(ctx, { label, plistPath, content }) {
|
|
|
62
62
|
|
|
63
63
|
if (existing === content) {
|
|
64
64
|
// registered+disabled(stop 中途失败/手工 disable)不是「已部署」:
|
|
65
|
-
// enable 后拉起,否则 install
|
|
66
|
-
|
|
65
|
+
// enable 后拉起,否则 install 报配置未变却没在跑、start 的 kickstart 被拒
|
|
66
|
+
ensureLoaded(ctx, { label, plistPath }, {
|
|
67
67
|
reviveRunningDisabled: true,
|
|
68
68
|
});
|
|
69
|
-
|
|
70
|
-
if (action === 'bootstrapped') return { registered: true };
|
|
71
|
-
return { revived: true };
|
|
69
|
+
return { changed: false };
|
|
72
70
|
}
|
|
73
71
|
|
|
74
72
|
// 内容变化(或首次):先 lint tmp,过了再动现文件
|
|
@@ -77,7 +75,7 @@ export function syncPlist(ctx, { label, plistPath, content }) {
|
|
|
77
75
|
const lint = launchd.lintPlist(runner, tmp);
|
|
78
76
|
if (lint.code !== 0) {
|
|
79
77
|
fs.rmSync(tmp, { force: true });
|
|
80
|
-
return { lintError: lint.stderr.trim() || `plutil exit ${lint.code}` };
|
|
78
|
+
return { changed: false, lintError: lint.stderr.trim() || `plutil exit ${lint.code}` };
|
|
81
79
|
}
|
|
82
80
|
|
|
83
81
|
// 只要 registered 一个事实:printState 即可,不跑全域 print-disabled
|
|
@@ -94,7 +92,7 @@ export function syncPlist(ctx, { label, plistPath, content }) {
|
|
|
94
92
|
//(停机态)——真失败(override 数据库损坏)下 bootstrap 也过不去(error 5),
|
|
95
93
|
// 可见性优先于「也许 bootstrap 能侥幸成功」
|
|
96
94
|
launchd.enableBootstrap(runner, uid, label, plistPath);
|
|
97
|
-
return {
|
|
95
|
+
return { changed: true, restarted: st.registered };
|
|
98
96
|
}
|
|
99
97
|
|
|
100
98
|
/**
|
package/libexec/lib/paths.mjs
CHANGED
|
@@ -30,8 +30,8 @@ export function packageScriptPath(scriptName) {
|
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
/**
|
|
33
|
-
* 本机全部产物与配置路径。config 在 dotfiles;bin/state
|
|
34
|
-
*
|
|
33
|
+
* 本机全部产物与配置路径。config 在 dotfiles;bin/state 是本工具产物,
|
|
34
|
+
* 全部收在 ~/.ssh/ssh-net/ 一棵树。
|
|
35
35
|
* @param {NodeJS.ProcessEnv} [env]
|
|
36
36
|
*/
|
|
37
37
|
export function machinePaths(env = process.env) {
|
|
@@ -50,7 +50,8 @@ export function machinePaths(env = process.env) {
|
|
|
50
50
|
binDir: path.join(sshNetDir, 'bin'),
|
|
51
51
|
/** @param {string} name bin/ 下产物名(实体或服务软链) */
|
|
52
52
|
binScript: (name) => path.join(sshNetDir, 'bin', name),
|
|
53
|
-
|
|
53
|
+
// 旧安装版本戳(26.10.59 退役):只供对账/卸载做迁移清理
|
|
54
|
+
legacyRuntimeVersionFile: path.join(sshNetDir, 'runtime-version'),
|
|
54
55
|
stateDir,
|
|
55
56
|
// ssh Include 片段目录(design §4.2):routes 仅 edge install 写;
|
|
56
57
|
// edge-<别名> 由 core/node install 按 topology 渲染。Include glob 零匹配
|
|
@@ -77,7 +78,7 @@ export function machinePaths(env = process.env) {
|
|
|
77
78
|
// PROBE_FAIL:sn-route 的失败印记(内容 epoch,15s TTL)。文件名是
|
|
78
79
|
// paths ↔ sn-route 两处契约(constants.test 锚)
|
|
79
80
|
probeFailFile: path.join(stateDir, 'PROBE_FAIL'),
|
|
80
|
-
// ENSURE:sn-route 按需确保的 single-flight
|
|
81
|
+
// ENSURE:sn-route 按需确保的 single-flight 锁(内容无意义,互斥在 fd 上)
|
|
81
82
|
ensureLockFile: path.join(stateDir, 'ENSURE'),
|
|
82
83
|
edgesDir: path.join(stateDir, 'edges'),
|
|
83
84
|
launchAgentsDir,
|