ssh-net 26.10.34 → 26.10.36
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 +42 -0
- package/libexec/cli.mjs +5 -14
- package/libexec/commands/doctor.mjs +7 -5
- package/libexec/commands/install.mjs +58 -95
- package/libexec/commands/register.mjs +28 -55
- package/libexec/commands/start.mjs +12 -24
- package/libexec/commands/status.mjs +97 -106
- package/libexec/commands/stop.mjs +8 -10
- package/libexec/commands/uninstall.mjs +16 -18
- package/libexec/lib/cliargs.mjs +12 -0
- package/libexec/lib/conf.mjs +3 -11
- package/libexec/lib/config.mjs +2 -3
- package/libexec/lib/fsutil.mjs +34 -0
- package/libexec/lib/ipv6.mjs +17 -20
- package/libexec/lib/jobctl.mjs +13 -14
- package/libexec/lib/launchd.mjs +66 -12
- package/libexec/lib/manage.mjs +51 -34
- package/libexec/lib/manifest.mjs +76 -0
- package/libexec/lib/paths.mjs +2 -0
- package/libexec/lib/probe.mjs +27 -10
- package/libexec/lib/registration.mjs +47 -0
- package/libexec/lib/render.mjs +79 -24
- package/libexec/lib/runtime.mjs +24 -35
- package/libexec/lib/state.mjs +12 -20
- package/libexec/lib/systemd.mjs +52 -5
- package/libexec/sn-route +36 -19
- package/libexec/ssh-net-uplink-edge +12 -8
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,48 @@
|
|
|
7
7
|
GitHub Release(Actions 自动跑测试后 npm publish;跳号与 0.x 的完整
|
|
8
8
|
考据见 git 史)。
|
|
9
9
|
|
|
10
|
+
## [26.10.36] - 2026-10-03
|
|
11
|
+
|
|
12
|
+
### 精简:清单单一来源补齐 + 一个报名竞态修复
|
|
13
|
+
|
|
14
|
+
- **uplink-edge 组件清单三处合一**:core install、`_register` 报名对账、
|
|
15
|
+
status 体检此前各抄一份实体/软链知识,收敛进 manifest
|
|
16
|
+
(`edgeCallbacksPlan`/`linkAddonPlan`),`install --without-link` 的
|
|
17
|
+
删除也由清单驱动;脚本名改常量引用,改名一处即可
|
|
18
|
+
- **报名 ssh 两源对齐**:`start peer` 的 ConnectTimeout 15 与额外保活
|
|
19
|
+
选项是相对 design 冻结表(5s)的静默漂移,改为与 sn-route 现场报名
|
|
20
|
+
完全同形(外层 60s timeout 仍兜底挂死)
|
|
21
|
+
- **sn-route 单飞竞态修复**:leader 报名秒败删锁的瞬间,晚启动的
|
|
22
|
+
follower 可能夹在「印记已写、锁已删」之间误接管、重复报名;抢空锁后
|
|
23
|
+
复查 PROBE_FAIL,新鲜则认 follower 不报名
|
|
24
|
+
- **退役产物清理统一**:bin/state/conf.d 三类退役清单合并为 STALE_FILES;
|
|
25
|
+
stop 的进程收束不再偷删开关(策略回命令层);lsof 预检两跑合一;
|
|
26
|
+
launchd/systemd 若干死代码清理
|
|
27
|
+
- **测试**:命令测试沙箱收敛为 makeCommandHarness(t.after 自动回收,
|
|
28
|
+
失败不泄漏临时目录)、fixture 加载单一入口、镜像脚手架合并;新增报名
|
|
29
|
+
argv/label 公式/片段名三处跨语言锚测试;全套 9.6s→7.5s
|
|
30
|
+
|
|
31
|
+
## [26.10.35] - 2026-10-03
|
|
32
|
+
|
|
33
|
+
### 精简:控制面重复收敛(纯内部重构,对外行为不变)
|
|
34
|
+
|
|
35
|
+
- **清单/原语立单一来源**:新增 `fsutil`(原子写 + 缺失即 null 读取,
|
|
36
|
+
替掉 10 处手抄 tmp+rename/ENOENT 模板)、`manifest`(各角色该装/该检
|
|
37
|
+
哪些运行时实体与软链,install/status 共用,增删脚本不再同步改四处)、
|
|
38
|
+
`registration`(edge 登记三态不变式与两处同写同删,install reconcile/
|
|
39
|
+
status/报名三方共用)、`cliargs`(start/stop 公共参数守卫)
|
|
40
|
+
- **编排下沉对位**:launchctl 四态状态机收成 `manage.ensureLoaded`
|
|
41
|
+
(syncPlist 与 start 此前各抄一份);node 侧补与 syncPlist 同构的
|
|
42
|
+
`systemd.syncUnit` 与 `stopIfLoaded`;sn-route 冻结 argv、经 relay 拨
|
|
43
|
+
core 的 ssh argv 各立唯一生产点(`snRouteArgv`/`coreViaRelayArgs`)
|
|
44
|
+
- **效率**:edge status 的 sn-ipv6 探测改与其他实测并发(最坏 13s→8s);
|
|
45
|
+
stop 轮询、edge status、install 预检不再重复跑全域 print-disabled 与
|
|
46
|
+
全机 ps;sn-route 轮询计时改内建 `$SECONDS`、follower 每 tick 只取一次
|
|
47
|
+
时间(等满 6s 省约百个子进程)
|
|
48
|
+
- **杂项**:删零消费的 Forward.bind 死字段;ipv6 可拨号判定收成单一
|
|
49
|
+
来源;uplink-edge 两处休眠序列合并为 hibernate、sn-route 抢锁咒语合并;
|
|
50
|
+
enablePeer 重复开启的措辞不再靠调用方做字符串替换
|
|
51
|
+
|
|
10
52
|
## [26.10.34] - 2026-10-03
|
|
11
53
|
|
|
12
54
|
### 精简:冗余清理(代码/测试/文档三面,对外行为仅删 update 别名)
|
package/libexec/cli.mjs
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
// ssh-net 控制面入口:参数路由 + buildContext。业务都在 lib/ 与 commands/,
|
|
3
3
|
// 本文件只做装配、帮助与错误边界。
|
|
4
|
-
import fs from 'node:fs';
|
|
5
4
|
import os from 'node:os';
|
|
6
5
|
import { parseTopology, validateHostContract } from './lib/config.mjs';
|
|
7
6
|
import { parseStaticHosts } from './lib/sshconfig.mjs';
|
|
@@ -9,6 +8,7 @@ import { localHostname, matchIdentity } from './lib/identity.mjs';
|
|
|
9
8
|
import { machinePaths } from './lib/paths.mjs';
|
|
10
9
|
import { createStateStore } from './lib/state.mjs';
|
|
11
10
|
import { createRunner } from './lib/exec.mjs';
|
|
11
|
+
import { readFileOrNull } from './lib/fsutil.mjs';
|
|
12
12
|
import pkg from '../package.json' with { type: 'json' };
|
|
13
13
|
|
|
14
14
|
import { run as installRun } from './commands/install.mjs';
|
|
@@ -52,22 +52,13 @@ function help() {
|
|
|
52
52
|
function buildContext() {
|
|
53
53
|
const env = process.env;
|
|
54
54
|
const paths = machinePaths(env);
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
text = fs.readFileSync(paths.topologyFile, 'utf8');
|
|
58
|
-
} catch (err) {
|
|
59
|
-
if (err.code !== 'ENOENT') throw err;
|
|
55
|
+
const text = readFileOrNull(paths.topologyFile);
|
|
56
|
+
if (text === null)
|
|
60
57
|
throw new Error(`缺配置 ${paths.topologyFile}(dotfiles apply ssh 后再 install)`);
|
|
61
|
-
}
|
|
62
58
|
const topo = parseTopology(text);
|
|
63
59
|
|
|
64
60
|
// 静态主机表(IP 唯一来源);~/.ssh/config 缺失时给空表,契约校验报具体缺口
|
|
65
|
-
|
|
66
|
-
try {
|
|
67
|
-
sshConfigText = fs.readFileSync(paths.sshConfigFile, 'utf8');
|
|
68
|
-
} catch (err) {
|
|
69
|
-
if (err.code !== 'ENOENT') throw err;
|
|
70
|
-
}
|
|
61
|
+
const sshConfigText = readFileOrNull(paths.sshConfigFile) ?? '';
|
|
71
62
|
const hostTable = parseStaticHosts(sshConfigText);
|
|
72
63
|
validateHostContract(topo, hostTable);
|
|
73
64
|
|
|
@@ -86,7 +77,7 @@ function buildContext() {
|
|
|
86
77
|
role: self.role,
|
|
87
78
|
runner: createRunner(env),
|
|
88
79
|
state: createStateStore(paths),
|
|
89
|
-
uid:
|
|
80
|
+
uid: process.getuid(),
|
|
90
81
|
};
|
|
91
82
|
}
|
|
92
83
|
|
|
@@ -2,7 +2,8 @@
|
|
|
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
|
+
import { snRouteArgv } from '../lib/render.mjs';
|
|
6
7
|
|
|
7
8
|
export async function run(ctx) {
|
|
8
9
|
if (ctx.role !== 'edge')
|
|
@@ -17,10 +18,11 @@ export async function run(ctx) {
|
|
|
17
18
|
const { topo, paths, self } = ctx;
|
|
18
19
|
// stdio:'inherit':sn-route 进度直达终端,同时其 [ -t 2 ] 才成立;
|
|
19
20
|
// timeout 略大于 6s 预算
|
|
20
|
-
const r = ctx.runner.runRaw(
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
const r = ctx.runner.runRaw(
|
|
22
|
+
paths.binScript(SN_ROUTE),
|
|
23
|
+
snRouteArgv(topo, self.alias, 'show'),
|
|
24
|
+
{ stdio: 'inherit', timeout: 8000 },
|
|
25
|
+
);
|
|
24
26
|
|
|
25
27
|
if (r.error && r.error.code === 'ENOENT')
|
|
26
28
|
throw new Error('未安装(缺 sn-route)——先跑 ssh-net install');
|
|
@@ -7,22 +7,27 @@ import os from 'node:os';
|
|
|
7
7
|
import path from 'node:path';
|
|
8
8
|
import { renderForwardArgs } from '../lib/config.mjs';
|
|
9
9
|
import { CALLBACK_PORT } from '../lib/constants.mjs';
|
|
10
|
-
import { syncPlist,
|
|
10
|
+
import { syncPlist, removeJob } from '../lib/manage.mjs';
|
|
11
11
|
import { syncConfFile } from '../lib/conf.mjs';
|
|
12
12
|
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
|
+
import { writeFileAtomic } from '../lib/fsutil.mjs';
|
|
19
20
|
import {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
SN_PROBE, edgeRuntimePlan, relayRuntimePlan,
|
|
22
|
+
edgeCallbacksPlan, linkAddonPlan, mergePlans,
|
|
23
|
+
} from '../lib/manifest.mjs';
|
|
24
|
+
import { edgeFragmentContent, edgeFragmentShouldExist } from '../lib/registration.mjs';
|
|
25
|
+
import {
|
|
26
|
+
linkLabel,
|
|
27
|
+
relayLabel, relayUnitName,
|
|
28
|
+
edgeLabel,
|
|
23
29
|
plistFileName,
|
|
24
30
|
renderEdgeLinkPlist, renderRelayPlist, renderRelayUnit, renderRoutes,
|
|
25
|
-
renderEdgeHost,
|
|
26
31
|
} from '../lib/render.mjs';
|
|
27
32
|
|
|
28
33
|
/** ensureRuntime 结果的人话摘要('' = 组件零变更)。install/upgrade 后
|
|
@@ -48,29 +53,34 @@ function compNote(results) {
|
|
|
48
53
|
async function precheckEdgePorts(ctx, linkOn) {
|
|
49
54
|
|
|
50
55
|
const { runner, paths, uid, topo } = ctx;
|
|
51
|
-
|
|
56
|
+
// 只要 pid 判持口者归属,不跑全域 print-disabled
|
|
57
|
+
const jobPid = launchd.printState(runner, uid, linkLabel(topo.core.alias)).pid;
|
|
52
58
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
59
|
+
// 进程树预检全程不变:有转发口时建一次复用,别每个口重跑一遍 ps
|
|
60
|
+
const forwardPorts = topo.forwards.map((f) => f.listenPort);
|
|
61
|
+
if (linkOn && forwardPorts.length > 0) {
|
|
62
|
+
const tree = processTree(runner);
|
|
63
|
+
for (const port of forwardPorts) {
|
|
64
|
+
const c = classifyLocalPort(runner, port, jobPid, tree);
|
|
65
|
+
if (c.kind === 'foreign') {
|
|
66
|
+
throw new Error(`端口 ${port} 被非本服务进程占用(pid ${c.pids.join(',')}):\n${lsofListen(runner, port)}`);
|
|
67
|
+
}
|
|
58
68
|
}
|
|
59
69
|
}
|
|
60
70
|
|
|
61
71
|
// 10022 持口者永远是本机 sshd,无法按 pid 溯源:读到 SSH banner 才认。
|
|
62
72
|
// 只盯 [::1](peer 全 v6 环回后的本服务监听面;127.0.0.1:10022 的持有
|
|
63
73
|
// 者如 VS Code 本地转发与本服务可共存,2026-09-30 起不判冲突——edge 实案)。
|
|
64
|
-
//
|
|
65
|
-
|
|
74
|
+
// 一次 lsof 同时拿持有者 pid 与报错展示文本;首装时 bin/sn-probe 还没拷上,
|
|
75
|
+
// 探针缺失不判僵尸(install 随后就拷)
|
|
76
|
+
const listeners = lsofListen(runner, CALLBACK_PORT);
|
|
66
77
|
if (
|
|
67
|
-
|
|
68
|
-
&& fs.existsSync(
|
|
78
|
+
v6LoopbackPids(listeners, CALLBACK_PORT).length > 0
|
|
79
|
+
&& fs.existsSync(paths.binScript(SN_PROBE))
|
|
69
80
|
&& !(await peerProbeOk(runner, paths.binDir))
|
|
70
81
|
) {
|
|
71
|
-
const who = runner.run('lsof', ['-nP', `-iTCP:${CALLBACK_PORT}`, '-sTCP:LISTEN']).stdout.trim();
|
|
72
82
|
throw new Error(
|
|
73
|
-
`端口 ${CALLBACK_PORT} 在听但读不到 SSH banner(僵尸监听或他进程占用):\n${
|
|
83
|
+
`端口 ${CALLBACK_PORT} 在听但读不到 SSH banner(僵尸监听或他进程占用):\n${listeners}`,
|
|
74
84
|
);
|
|
75
85
|
}
|
|
76
86
|
}
|
|
@@ -107,26 +117,15 @@ async function installEdge(ctx, args) {
|
|
|
107
117
|
// 显式意图先落/删 marker;无参 install(含 upgrade 内置)不触碰它
|
|
108
118
|
if (flag === true) {
|
|
109
119
|
fs.mkdirSync(paths.sshNetDir, { recursive: true });
|
|
110
|
-
writeFileAtomic(paths.linkEnabledFile, '1\n', 0o600);
|
|
120
|
+
writeFileAtomic(paths.linkEnabledFile, '1\n', { mode: 0o600 });
|
|
111
121
|
}
|
|
112
122
|
if (flag === false) fs.rmSync(paths.linkEnabledFile, { force: true });
|
|
113
123
|
|
|
114
|
-
//
|
|
115
|
-
//
|
|
116
|
-
//
|
|
117
|
-
|
|
118
|
-
const
|
|
119
|
-
['sn-probe', 'sn-probe'],
|
|
120
|
-
['sn-route', 'sn-route'], // Match exec 按需选路 + 成功后本地通知/秒切(design §2.2)
|
|
121
|
-
['sn-ipv6', 'sn-ipv6'],
|
|
122
|
-
];
|
|
123
|
-
/** @type {[string,string][]} */
|
|
124
|
-
const links = [];
|
|
125
|
-
if (linkOn) {
|
|
126
|
-
entities.push(['ssh-net-link-core', 'ssh-net-link-core']);
|
|
127
|
-
links.push([linkLinkName(topo.core.alias), 'ssh-net-link-core']);
|
|
128
|
-
}
|
|
129
|
-
const comp = compNote(ensureRuntime(ctx, entities, links));
|
|
124
|
+
// 实体/软链清单唯一来源在 manifest:基础 3 实体,linkOn 才加第 4 实体与
|
|
125
|
+
// 带机器名软链。回拨通知与 link 秒切已内联在 sn-route 成功点(本地进程
|
|
126
|
+
// 调用),不再有独立信令脚本与跨机反拨
|
|
127
|
+
const plan = edgeRuntimePlan(topo, linkOn);
|
|
128
|
+
const comp = compNote(ensureRuntime(ctx, plan.entities, plan.links));
|
|
130
129
|
|
|
131
130
|
if (linkOn) {
|
|
132
131
|
// 附加件:渲染纯 ssh plist 并对账(RELAY_ON 在 syncPlist 按 stopped 语义
|
|
@@ -143,18 +142,14 @@ async function installEdge(ctx, args) {
|
|
|
143
142
|
// 默认零常驻:清掉旧版/加装过的 link 全部产物。升级时旧 plist 必须
|
|
144
143
|
// bootout,否则 launchd 每 ThrottleInterval 刷 ENOENT
|
|
145
144
|
removeJob(ctx, label, plistPath);
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
fs.rmSync(path.join(paths.stateDir, 'ROUTE_PEER'), { force: true });
|
|
150
|
-
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 });
|
|
151
148
|
}
|
|
152
149
|
|
|
153
150
|
// conf.d 片段(design §4.2):edge 是 routes 的唯一安装角色——routes 含
|
|
154
151
|
// 选路 Match 与尾部 VS Code 打标块;新签名带本机别名
|
|
155
152
|
const routesChanged = syncConfFile(paths.routesFile, renderRoutes(topo, ctx.self.alias));
|
|
156
|
-
// 打标块并入 routes 后的旧文件清理(26.10.32 退役;按约定一个月后删本行)
|
|
157
|
-
fs.rmSync(path.join(paths.confDir, 'vscode-mark'), { force: true });
|
|
158
153
|
const fragMsg = routesChanged ? '(选路片段已更新)' : '';
|
|
159
154
|
if (!comp && !routesChanged && flag === undefined) {
|
|
160
155
|
return linkOn
|
|
@@ -173,14 +168,9 @@ function installCore(ctx) {
|
|
|
173
168
|
// 软链):否则 upgrade 后 core status 要对每台未联系的 edge 报组件异常,
|
|
174
169
|
// 直到它下次报名才自愈(2026-09-30 补)
|
|
175
170
|
const edges = launchd.edgeAliasesOnDisk(paths.launchAgentsDir);
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
if (edges.length) entities.push(['ssh-net-uplink-edge', 'ssh-net-uplink-edge']);
|
|
180
|
-
const comp = compNote(ensureRuntime(ctx, entities, [
|
|
181
|
-
[relayLinkName(topo.relay.alias), 'ssh-net-uplink-relay'],
|
|
182
|
-
...edges.map((alias) => [edgeLinkName(alias), 'ssh-net-uplink-edge']),
|
|
183
|
-
]));
|
|
171
|
+
// relay 基础形态 + 磁盘在册 edge 的回拨组件(清单与报名/status 体检同源)
|
|
172
|
+
const plan = mergePlans(relayRuntimePlan(topo), edgeCallbacksPlan(edges));
|
|
173
|
+
const comp = compNote(ensureRuntime(ctx, plan.entities, plan.links));
|
|
184
174
|
const label = relayLabel(topo.relay.alias);
|
|
185
175
|
const content = renderRelayPlist({
|
|
186
176
|
binDir: paths.binDir,
|
|
@@ -205,11 +195,9 @@ function installNode(ctx) {
|
|
|
205
195
|
const { topo, paths, runner } = ctx;
|
|
206
196
|
if (ctx.uid !== 0)
|
|
207
197
|
throw new Error('node 安装写 /etc/systemd/system 需要 root,请以 root 运行');
|
|
208
|
-
const
|
|
209
|
-
|
|
210
|
-
[[relayLinkName(topo.relay.alias), 'ssh-net-uplink-relay']]));
|
|
198
|
+
const plan = relayRuntimePlan(topo);
|
|
199
|
+
const comp = compNote(ensureRuntime(ctx, plan.entities, plan.links));
|
|
211
200
|
const unitName = relayUnitName(topo.relay.alias);
|
|
212
|
-
const unitPath = path.join(paths.systemdUnitDir, unitName);
|
|
213
201
|
// HOME 用 root 的真实家目录(getpwuid),不取 $HOME:sudo 保留 HOME 的环境
|
|
214
202
|
// (sudo -E,或部分发行版的默认值)会把调用者的家目录写进 unit——root
|
|
215
203
|
// 运行的 ssh 将去读别人的 ~/.ssh/config,relay 别名解析错配且无声
|
|
@@ -218,44 +206,21 @@ function installNode(ctx) {
|
|
|
218
206
|
// (plist 侧是 XML 数组无此问题)——当场拒绝比装出一个起不来的 unit 诚实
|
|
219
207
|
if (/\s/.test(home))
|
|
220
208
|
throw new Error(`node 的 HOME(${home})含空白字符:systemd ExecStart 无法转义,拒绝安装`);
|
|
221
|
-
const
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
209
|
+
const r = systemd.syncUnit(runner, {
|
|
210
|
+
unitPath: path.join(paths.systemdUnitDir, unitName),
|
|
211
|
+
unitName,
|
|
212
|
+
content: renderRelayUnit({
|
|
213
|
+
binDir: paths.binDir,
|
|
214
|
+
relayAlias: topo.relay.alias,
|
|
215
|
+
relayPort: ctx.self.relayPort,
|
|
216
|
+
home,
|
|
217
|
+
}),
|
|
226
218
|
});
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
// 幂等(对齐 mac 侧 syncPlist 的「字节相同不动盘」与 design §5「install
|
|
230
|
-
// 幂等不断连」):unit 内容一致时只确保 enable + 在跑(start 对 active
|
|
231
|
-
// 单元是 no-op),不重写、不 restart——重复 install 不再把健康隧道
|
|
232
|
-
// 白断一次;内容变了才走写 + verify + reload + restart 全序列
|
|
233
|
-
let existing = null;
|
|
234
|
-
try {
|
|
235
|
-
existing = fs.readFileSync(unitPath, 'utf8');
|
|
236
|
-
} catch {
|
|
237
|
-
existing = null;
|
|
238
|
-
}
|
|
239
|
-
if (existing === content) {
|
|
240
|
-
for (const r of [systemd.enableUnit(runner, unitName), systemd.startUnit(runner, unitName)]) {
|
|
241
|
-
if (r.code !== 0) throw new Error(`systemctl 失败:${r.stderr.trim()}`);
|
|
242
|
-
}
|
|
219
|
+
if (r === 'unchanged') {
|
|
243
220
|
return comp
|
|
244
221
|
? `组件已刷新${comp};unit 无需改动`
|
|
245
222
|
: '已安装,无需改动';
|
|
246
223
|
}
|
|
247
|
-
|
|
248
|
-
writeFileAtomic(unitPath, content, 0o644);
|
|
249
|
-
const verify = systemd.verifyUnit(runner, unitPath);
|
|
250
|
-
if (!verify.ok && !verify.skipped)
|
|
251
|
-
throw new Error(`systemd-analyze verify 失败:\n${verify.stderr}`);
|
|
252
|
-
for (const r of [
|
|
253
|
-
systemd.daemonReload(runner),
|
|
254
|
-
systemd.enableUnit(runner, unitName),
|
|
255
|
-
systemd.restartUnit(runner, unitName),
|
|
256
|
-
]) {
|
|
257
|
-
if (r.code !== 0) throw new Error(`systemctl 失败:${r.stderr.trim()}`);
|
|
258
|
-
}
|
|
259
224
|
return `安装完成${comp},relay 已开启(${ctx.topo.relay.alias} 入口 ${ctx.self.relayPort})`;
|
|
260
225
|
}
|
|
261
226
|
|
|
@@ -277,18 +242,16 @@ function reconcileEdgeFragments(ctx) {
|
|
|
277
242
|
files = fs.readdirSync(paths.confDir).filter((f) => f.startsWith('edge-'));
|
|
278
243
|
} catch { files = []; }
|
|
279
244
|
|
|
245
|
+
// disabled 字典是整域同一份:循环外取一次,各 edge 的 inspect 共用
|
|
246
|
+
const dmap = launchd.disabledMap(ctx.runner, ctx.uid);
|
|
280
247
|
for (const e of topo.edges) {
|
|
281
248
|
const file = paths.edgeHostFile(e.alias);
|
|
282
|
-
const st = launchd.
|
|
249
|
+
const st = launchd.inspectWith(ctx.runner, ctx.uid, edgeLabel(e.alias), dmap);
|
|
283
250
|
const v6 = ctx.state.readEdgeV6(e.alias);
|
|
284
|
-
if (st
|
|
285
|
-
syncConfFile(
|
|
286
|
-
|
|
287
|
-
renderEdgeHost({ alias: e.alias, aliases: e.aliases, user: e.user, v6 }),
|
|
288
|
-
);
|
|
289
|
-
} else {
|
|
251
|
+
if (edgeFragmentShouldExist(st, v6))
|
|
252
|
+
syncConfFile(file, edgeFragmentContent(e, v6));
|
|
253
|
+
else
|
|
290
254
|
fs.rmSync(file, { force: true });
|
|
291
|
-
}
|
|
292
255
|
files = files.filter((f) => f !== `edge-${e.alias}`);
|
|
293
256
|
}
|
|
294
257
|
// 剩下的都是 topology 不认识的孤儿
|
|
@@ -11,22 +11,26 @@
|
|
|
11
11
|
// 先对账运行时脚本与 plist——uplink-edge 的脚本与 job 参数(user 等)变更
|
|
12
12
|
// 只有这条更新通道,缺了会永远用旧参数/旧目标拨号、六连败休眠。
|
|
13
13
|
// launchctl 真返回成功才结束;失败 edge 下次连接自然重试。
|
|
14
|
-
import fs from 'node:fs';
|
|
15
14
|
import path from 'node:path';
|
|
16
15
|
import { explainV6Error } from '../lib/ipv6.mjs';
|
|
17
16
|
import * as launchd from '../lib/launchd.mjs';
|
|
18
17
|
import { syncPlist } from '../lib/manage.mjs';
|
|
19
|
-
import {
|
|
18
|
+
import { removeConfFile } from '../lib/conf.mjs';
|
|
20
19
|
import { ensureRuntime } from '../lib/runtime.mjs';
|
|
20
|
+
import { readFileOrNull } from '../lib/fsutil.mjs';
|
|
21
21
|
import {
|
|
22
|
-
|
|
22
|
+
writeEdgeRegistration, removeEdgeRegistration,
|
|
23
|
+
} from '../lib/registration.mjs';
|
|
24
|
+
import { edgeCallbacksPlan } from '../lib/manifest.mjs';
|
|
25
|
+
import {
|
|
26
|
+
edgeLabel, plistFileName, renderUplinkEdgePlist, coreViaRelayArgs,
|
|
23
27
|
} from '../lib/render.mjs';
|
|
24
28
|
|
|
25
29
|
/** edge 侧报名:取 v6 + 命令行 -J 经 relay 调 core 的 _register。
|
|
26
30
|
* start peer 用——控制面直接报一次名让 core 马上回拨。 */
|
|
27
31
|
function runEdgeAnnounce(ctx) {
|
|
28
32
|
const probe = ctx.runner.runRaw(
|
|
29
|
-
|
|
33
|
+
ctx.paths.binScript('sn-ipv6'), [], { timeout: 5000 },
|
|
30
34
|
);
|
|
31
35
|
// bin/sn-ipv6 缺失(未 install)别误报成「无 IPv6」。fatal 哨兵属性:
|
|
32
36
|
// enablePeer 靠它区分「没有现场选路可指望」的致命错与可重试的报名失败,
|
|
@@ -51,50 +55,20 @@ function runEdgeAnnounce(ctx) {
|
|
|
51
55
|
const v6Err = explainV6Error(v6);
|
|
52
56
|
if (v6Err) throw new Error(`本机 v6 选取异常(sn-ipv6 输出不是可报名地址:${v6Err})`);
|
|
53
57
|
// 报名固定走 relay(relay 7×24 在;peer 僵尸时自动选路会把报名一起挂死)。
|
|
54
|
-
//
|
|
55
|
-
//
|
|
56
|
-
//
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
'-o', `HostKeyAlias=${ctx.topo.core.alias}`,
|
|
63
|
-
'-o', 'BatchMode=yes', '-o', 'ConnectTimeout=15',
|
|
64
|
-
'-o', 'ServerAliveInterval=10', '-o', 'ServerAliveCountMax=2',
|
|
65
|
-
'-o', 'ControlMaster=no', '-o', 'ControlPath=none',
|
|
66
|
-
'-o', 'IdentitiesOnly=yes',
|
|
67
|
-
'-o', 'StrictHostKeyChecking=accept-new',
|
|
68
|
-
`${ctx.topo.core.user}@127.0.0.1`,
|
|
69
|
-
`ssh-net _register ${ctx.self.alias} ${v6}`,
|
|
70
|
-
], { timeout: 60_000 });
|
|
58
|
+
// argv 与 sn-route 的现场报名完全同一形态(coreViaRelayArgs 默认 ConnectTimeout
|
|
59
|
+
// 5,design §3 冻结):无人值守首连信任 accept-new(探测侧 status 不带它,
|
|
60
|
+
// 差异在 coreViaRelayArgs 显式)+ IdentitiesOnly 防多 key 试错撞 MaxAuthTries
|
|
61
|
+
//(design §4.3)。挂死由外层 60s timeout 兜底
|
|
62
|
+
const r = ctx.runner.run('ssh', coreViaRelayArgs(ctx.topo, {
|
|
63
|
+
acceptHost: true,
|
|
64
|
+
remoteCommand: `ssh-net _register ${ctx.self.alias} ${v6}`,
|
|
65
|
+
}), { timeout: 60_000 });
|
|
71
66
|
if (r.code !== 0)
|
|
72
67
|
throw new Error(`报名失败(${r.stderr.trim() || `exit ${r.code}`}):检查 ${ctx.topo.relay.alias} 与 core 侧 relay 是否可用,稍后重试`);
|
|
73
68
|
const core = r.stdout.trim();
|
|
74
69
|
return `已报名(本机 v6 ${v6},core 返回 ${core})。core 几秒内回拨建立 peer;ssh-net status 查看结果`;
|
|
75
70
|
}
|
|
76
71
|
|
|
77
|
-
/**
|
|
78
|
-
* edge 登记两处同写(唯一写者 _register):state/edges/<别名> 给
|
|
79
|
-
* uplink-edge 脚本机读,conf.d/edge-<别名> 是 core 上 `ssh <别名>` 的入口。
|
|
80
|
-
* @param {object} ctx
|
|
81
|
-
* @param {{alias:string, user:string}} edge topology edge 行
|
|
82
|
-
* @param {string} v6
|
|
83
|
-
*/
|
|
84
|
-
function writeEdgeRegistration(ctx, edge, v6) {
|
|
85
|
-
ctx.state.writeEdgeV6(edge.alias, v6);
|
|
86
|
-
syncConfFile(
|
|
87
|
-
ctx.paths.edgeHostFile(edge.alias),
|
|
88
|
-
renderEdgeHost({ alias: edge.alias, aliases: edge.aliases, user: edge.user, v6 }),
|
|
89
|
-
);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/** 两处同删(未报名过的失败路径)。 */
|
|
93
|
-
function removeEdgeRegistration(ctx, alias) {
|
|
94
|
-
ctx.state.removeEdgeV6(alias);
|
|
95
|
-
removeConfFile(ctx.paths.edgeHostFile(alias));
|
|
96
|
-
}
|
|
97
|
-
|
|
98
72
|
/** kickstart 失败时把登记回滚到 job 实际在拨的 v6(null=两处都删)。
|
|
99
73
|
* 不回滚的话磁盘已是新 v6,下次报名若进程已被踢活会拿磁盘值当真,
|
|
100
74
|
* job 实际还拨旧 v6 拨到六连败休眠。 */
|
|
@@ -117,16 +91,20 @@ function rollbackRegistration(ctx, edge, prevV6) {
|
|
|
117
91
|
* 文案(带上报名详情:v6 与 core 的响应)。先报名、成功后才落开关——
|
|
118
92
|
* 未安装(fatal)零副作用;可重试的报名失败(relay 不通、当场无 v6)
|
|
119
93
|
* 不阻断用户意图,开关照置:下次发起连接时 sn-route 会再报,也可再跑
|
|
120
|
-
* 一次 ssh-net start peer
|
|
121
|
-
|
|
94
|
+
* 一次 ssh-net start peer。
|
|
95
|
+
* @param {object} ctx
|
|
96
|
+
* @param {boolean} [alreadyOn] 开关此前已全开:措辞用「已是开启状态」(重复
|
|
97
|
+
* start 是幂等确认)——由 start 在调用前传,文案选词不留给调用方替换字符串 */
|
|
98
|
+
export function enablePeer(ctx, alreadyOn = false) {
|
|
99
|
+
const prefix = alreadyOn ? '隧道已是开启状态' : 'peer 已开启';
|
|
122
100
|
try {
|
|
123
101
|
const detail = runEdgeAnnounce(ctx);
|
|
124
102
|
ctx.state.setOn(['relay', 'peer']); // relay 是根:peer 依赖它(design §5 级联)
|
|
125
|
-
return
|
|
103
|
+
return `${prefix}(relay 一并开启;${detail})`;
|
|
126
104
|
} catch (err) {
|
|
127
105
|
if (err && err.fatal) throw err;
|
|
128
106
|
ctx.state.setOn(['relay', 'peer']);
|
|
129
|
-
return
|
|
107
|
+
return `${prefix}(relay 一并开启;本次报名没成功——不影响开关,下次发起连接时会自动再报,也可再跑一次 ssh-net start peer)`;
|
|
130
108
|
}
|
|
131
109
|
}
|
|
132
110
|
|
|
@@ -157,20 +135,15 @@ export function runRegister(ctx, args = []) {
|
|
|
157
135
|
binDir: ctx.paths.binDir, edgeAlias: alias, user: edge.user,
|
|
158
136
|
logsDir: ctx.paths.logsDir,
|
|
159
137
|
});
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
existing = fs.readFileSync(plistPath, 'utf8');
|
|
163
|
-
} catch {
|
|
164
|
-
existing = null; // 未注册(从未落盘)或被手删
|
|
165
|
-
}
|
|
138
|
+
// 未注册(从未落盘)或被手删 → null
|
|
139
|
+
const existing = readFileOrNull(plistPath);
|
|
166
140
|
const prevV6 = ctx.state.readEdgeV6(alias);
|
|
167
141
|
const st = launchd.inspect(ctx.runner, ctx.uid, label);
|
|
168
142
|
|
|
169
143
|
// 脚本对账无条件先行(幂等):脚本被删/过期/参数变更时,四个状态分支
|
|
170
144
|
// 里的任何一个都可能成为重建通道,不能只靠首次报名那一次
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
[[edgeLinkName(alias), 'ssh-net-uplink-edge']]);
|
|
145
|
+
const plan = edgeCallbacksPlan([alias]);
|
|
146
|
+
ensureRuntime(ctx, plan.entities, plan.links);
|
|
174
147
|
|
|
175
148
|
// 未注册(首次报名)或已注册但渲染结果与磁盘不符(topology 的 user 等变了):
|
|
176
149
|
// 重新部署。syncPlist 走 bootout→落盘→enable→bootstrap,bootstrap 即首发,
|
|
@@ -5,8 +5,9 @@
|
|
|
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';
|
|
9
|
+
import { ensureLoaded } from '../lib/manage.mjs';
|
|
10
|
+
import { assertUnitSwitchArgs } from '../lib/cliargs.mjs';
|
|
10
11
|
import { relayLabel, relayUnitName, plistFileName } from '../lib/render.mjs';
|
|
11
12
|
import { enablePeer } from './register.mjs';
|
|
12
13
|
|
|
@@ -26,22 +27,12 @@ function startCore(ctx) {
|
|
|
26
27
|
const label = relayLabel(ctx.topo.relay.alias);
|
|
27
28
|
const plist = path.join(ctx.paths.launchAgentsDir, plistFileName(label));
|
|
28
29
|
if (!fs.existsSync(plist)) throw new Error(`尚未安装(缺 ${plist}),先跑 ssh-net install`);
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
return 'relay 已开启';
|
|
36
|
-
}
|
|
37
|
-
if (!st.running) {
|
|
38
|
-
// kickstartEnabled:kickstart 对 disabled job 被拒(stop 中途失败/手工
|
|
39
|
-
// disable 的残留态),先幂等 enable
|
|
40
|
-
const r = launchd.kickstartEnabled(ctx.runner, ctx.uid, label);
|
|
41
|
-
if (r.code !== 0) throw new Error(`启动失败:${r.stderr.trim()}`);
|
|
42
|
-
return 'relay 已重新拉起';
|
|
43
|
-
}
|
|
44
|
-
if (st.disabled) launchd.enable(ctx.runner, ctx.uid, label); // 在跑但 override 残留:清掉防退出后不重拉
|
|
30
|
+
// start 语义:在册但停着要踢活;在跑但 disabled 只清 override、不闪断
|
|
31
|
+
const action = ensureLoaded(ctx, { label, plistPath: plist }, {
|
|
32
|
+
kickWhenStopped: true,
|
|
33
|
+
});
|
|
34
|
+
if (action === 'bootstrapped') return 'relay 已开启';
|
|
35
|
+
if (action === 'kickstarted') return 'relay 已重新拉起';
|
|
45
36
|
return 'relay 已在运行';
|
|
46
37
|
}
|
|
47
38
|
|
|
@@ -51,18 +42,15 @@ export function run(ctx, args = []) {
|
|
|
51
42
|
const targets = parseTargets(args); // ['relay','peer'] 或 ['relay'];link 抛错
|
|
52
43
|
|
|
53
44
|
if (targets.includes('peer')) {
|
|
54
|
-
// 默认/-a/peer:enablePeer 置两开关(幂等)+
|
|
55
|
-
|
|
56
|
-
return before.relay && before.peer
|
|
57
|
-
? msg.replace('peer 已开启', '隧道已是开启状态')
|
|
58
|
-
: msg;
|
|
45
|
+
// 默认/-a/peer:enablePeer 置两开关(幂等)+ 立即报名。开关此前已
|
|
46
|
+
// 全开时措辞由 enablePeer 内部一处选词
|
|
47
|
+
return enablePeer(ctx, before.relay && before.peer);
|
|
59
48
|
}
|
|
60
49
|
ctx.state.setOn(['relay']);
|
|
61
50
|
return `relay ${before.relay ? '已是开启状态' : '已开启'}(隧道基础,不报名——要被 core 连到再 ssh-net start peer)`;
|
|
62
51
|
}
|
|
63
52
|
// core/node 是单总开关(等同 --all):显式 -a/--all 收下,其余参数拒绝
|
|
64
|
-
|
|
65
|
-
throw new Error('本机是单总开关,start 不带参数(-a/--all 亦可,等同缺省)');
|
|
53
|
+
assertUnitSwitchArgs(args, 'start');
|
|
66
54
|
if (ctx.role === 'core') return startCore(ctx);
|
|
67
55
|
if (ctx.role === 'node') {
|
|
68
56
|
const r = systemd.startUnit(ctx.runner, relayUnitName(ctx.topo.relay.alias));
|