ssh-net 26.10.34 → 26.10.35
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 +21 -0
- package/libexec/cli.mjs +4 -13
- package/libexec/commands/doctor.mjs +6 -4
- package/libexec/commands/install.mjs +48 -80
- package/libexec/commands/register.mjs +24 -51
- package/libexec/commands/start.mjs +12 -23
- package/libexec/commands/status.mjs +76 -87
- package/libexec/commands/stop.mjs +3 -7
- package/libexec/commands/uninstall.mjs +3 -5
- 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 +8 -7
- package/libexec/lib/launchd.mjs +60 -6
- package/libexec/lib/manage.mjs +51 -34
- package/libexec/lib/manifest.mjs +41 -0
- package/libexec/lib/probe.mjs +4 -2
- package/libexec/lib/registration.mjs +47 -0
- package/libexec/lib/render.mjs +79 -24
- package/libexec/lib/runtime.mjs +9 -26
- package/libexec/lib/state.mjs +12 -20
- package/libexec/lib/systemd.mjs +48 -0
- package/libexec/sn-route +26 -15
- package/libexec/ssh-net-uplink-edge +12 -8
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,27 @@
|
|
|
7
7
|
GitHub Release(Actions 自动跑测试后 npm publish;跳号与 0.x 的完整
|
|
8
8
|
考据见 git 史)。
|
|
9
9
|
|
|
10
|
+
## [26.10.35] - 2026-10-03
|
|
11
|
+
|
|
12
|
+
### 精简:控制面重复收敛(纯内部重构,对外行为不变)
|
|
13
|
+
|
|
14
|
+
- **清单/原语立单一来源**:新增 `fsutil`(原子写 + 缺失即 null 读取,
|
|
15
|
+
替掉 10 处手抄 tmp+rename/ENOENT 模板)、`manifest`(各角色该装/该检
|
|
16
|
+
哪些运行时实体与软链,install/status 共用,增删脚本不再同步改四处)、
|
|
17
|
+
`registration`(edge 登记三态不变式与两处同写同删,install reconcile/
|
|
18
|
+
status/报名三方共用)、`cliargs`(start/stop 公共参数守卫)
|
|
19
|
+
- **编排下沉对位**:launchctl 四态状态机收成 `manage.ensureLoaded`
|
|
20
|
+
(syncPlist 与 start 此前各抄一份);node 侧补与 syncPlist 同构的
|
|
21
|
+
`systemd.syncUnit` 与 `stopIfLoaded`;sn-route 冻结 argv、经 relay 拨
|
|
22
|
+
core 的 ssh argv 各立唯一生产点(`snRouteArgv`/`coreViaRelayArgs`)
|
|
23
|
+
- **效率**:edge status 的 sn-ipv6 探测改与其他实测并发(最坏 13s→8s);
|
|
24
|
+
stop 轮询、edge status、install 预检不再重复跑全域 print-disabled 与
|
|
25
|
+
全机 ps;sn-route 轮询计时改内建 `$SECONDS`、follower 每 tick 只取一次
|
|
26
|
+
时间(等满 6s 省约百个子进程)
|
|
27
|
+
- **杂项**:删零消费的 Forward.bind 死字段;ipv6 可拨号判定收成单一
|
|
28
|
+
来源;uplink-edge 两处休眠序列合并为 hibernate、sn-route 抢锁咒语合并;
|
|
29
|
+
enablePeer 重复开启的措辞不再靠调用方做字符串替换
|
|
30
|
+
|
|
10
31
|
## [26.10.34] - 2026-10-03
|
|
11
32
|
|
|
12
33
|
### 精简:冗余清理(代码/测试/文档三面,对外行为仅删 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
|
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
// doctor 只负责关态分诊与结果转述。不新增状态、不踢常驻服务(默认形态没有
|
|
4
4
|
// 常驻)。仅 edge 有动作:core 的恢复靠 edge 报名,node 由 systemd 自动重启。
|
|
5
5
|
import path from 'node:path';
|
|
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
|
+
path.join(paths.binDir, '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,24 @@ 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, listV6LoopbackListeners,
|
|
16
|
+
classifyLocalPort, listV6LoopbackListeners, processTree,
|
|
17
17
|
peerProbeOk,
|
|
18
18
|
} from '../lib/probe.mjs';
|
|
19
|
+
import { writeFileAtomic } from '../lib/fsutil.mjs';
|
|
20
|
+
import { edgeRuntimePlan, relayRuntimePlan } from '../lib/manifest.mjs';
|
|
21
|
+
import { edgeFragmentContent, edgeFragmentShouldExist } from '../lib/registration.mjs';
|
|
19
22
|
import {
|
|
20
23
|
linkLabel, linkLinkName,
|
|
21
|
-
relayLabel,
|
|
24
|
+
relayLabel, relayUnitName,
|
|
22
25
|
edgeLabel, edgeLinkName,
|
|
23
26
|
plistFileName,
|
|
24
27
|
renderEdgeLinkPlist, renderRelayPlist, renderRelayUnit, renderRoutes,
|
|
25
|
-
renderEdgeHost,
|
|
26
28
|
} from '../lib/render.mjs';
|
|
27
29
|
|
|
28
30
|
/** ensureRuntime 结果的人话摘要('' = 组件零变更)。install/upgrade 后
|
|
@@ -48,13 +50,19 @@ function compNote(results) {
|
|
|
48
50
|
async function precheckEdgePorts(ctx, linkOn) {
|
|
49
51
|
|
|
50
52
|
const { runner, paths, uid, topo } = ctx;
|
|
51
|
-
|
|
53
|
+
// 只要 pid 判持口者归属,不跑全域 print-disabled
|
|
54
|
+
const jobPid = launchd.printState(runner, uid, linkLabel(topo.core.alias)).pid;
|
|
52
55
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
56
|
+
// 进程树预检全程不变:有转发口时建一次复用,别每个口重跑一遍 ps
|
|
57
|
+
const forwardPorts = topo.forwards.map((f) => f.listenPort);
|
|
58
|
+
if (linkOn && forwardPorts.length > 0) {
|
|
59
|
+
const tree = processTree(runner);
|
|
60
|
+
for (const port of forwardPorts) {
|
|
61
|
+
const c = classifyLocalPort(runner, port, jobPid, tree);
|
|
62
|
+
if (c.kind === 'foreign') {
|
|
63
|
+
const who = runner.run('lsof', ['-nP', `-iTCP:${port}`, '-sTCP:LISTEN']).stdout.trim();
|
|
64
|
+
throw new Error(`端口 ${port} 被非本服务进程占用(pid ${c.pids.join(',')}):\n${who}`);
|
|
65
|
+
}
|
|
58
66
|
}
|
|
59
67
|
}
|
|
60
68
|
|
|
@@ -107,26 +115,15 @@ async function installEdge(ctx, args) {
|
|
|
107
115
|
// 显式意图先落/删 marker;无参 install(含 upgrade 内置)不触碰它
|
|
108
116
|
if (flag === true) {
|
|
109
117
|
fs.mkdirSync(paths.sshNetDir, { recursive: true });
|
|
110
|
-
writeFileAtomic(paths.linkEnabledFile, '1\n', 0o600);
|
|
118
|
+
writeFileAtomic(paths.linkEnabledFile, '1\n', { mode: 0o600 });
|
|
111
119
|
}
|
|
112
120
|
if (flag === false) fs.rmSync(paths.linkEnabledFile, { force: true });
|
|
113
121
|
|
|
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));
|
|
122
|
+
// 实体/软链清单唯一来源在 manifest:基础 3 实体,linkOn 才加第 4 实体与
|
|
123
|
+
// 带机器名软链。回拨通知与 link 秒切已内联在 sn-route 成功点(本地进程
|
|
124
|
+
// 调用),不再有独立信令脚本与跨机反拨
|
|
125
|
+
const plan = edgeRuntimePlan(topo, linkOn);
|
|
126
|
+
const comp = compNote(ensureRuntime(ctx, plan.entities, plan.links));
|
|
130
127
|
|
|
131
128
|
if (linkOn) {
|
|
132
129
|
// 附加件:渲染纯 ssh plist 并对账(RELAY_ON 在 syncPlist 按 stopped 语义
|
|
@@ -173,14 +170,12 @@ function installCore(ctx) {
|
|
|
173
170
|
// 软链):否则 upgrade 后 core status 要对每台未联系的 edge 报组件异常,
|
|
174
171
|
// 直到它下次报名才自愈(2026-09-30 补)
|
|
175
172
|
const edges = launchd.edgeAliasesOnDisk(paths.launchAgentsDir);
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
...edges.map((alias) => [edgeLinkName(alias), 'ssh-net-uplink-edge']),
|
|
183
|
-
]));
|
|
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']));
|
|
178
|
+
const comp = compNote(ensureRuntime(ctx, plan.entities, plan.links));
|
|
184
179
|
const label = relayLabel(topo.relay.alias);
|
|
185
180
|
const content = renderRelayPlist({
|
|
186
181
|
binDir: paths.binDir,
|
|
@@ -205,11 +200,9 @@ function installNode(ctx) {
|
|
|
205
200
|
const { topo, paths, runner } = ctx;
|
|
206
201
|
if (ctx.uid !== 0)
|
|
207
202
|
throw new Error('node 安装写 /etc/systemd/system 需要 root,请以 root 运行');
|
|
208
|
-
const
|
|
209
|
-
|
|
210
|
-
[[relayLinkName(topo.relay.alias), 'ssh-net-uplink-relay']]));
|
|
203
|
+
const plan = relayRuntimePlan(topo);
|
|
204
|
+
const comp = compNote(ensureRuntime(ctx, plan.entities, plan.links));
|
|
211
205
|
const unitName = relayUnitName(topo.relay.alias);
|
|
212
|
-
const unitPath = path.join(paths.systemdUnitDir, unitName);
|
|
213
206
|
// HOME 用 root 的真实家目录(getpwuid),不取 $HOME:sudo 保留 HOME 的环境
|
|
214
207
|
// (sudo -E,或部分发行版的默认值)会把调用者的家目录写进 unit——root
|
|
215
208
|
// 运行的 ssh 将去读别人的 ~/.ssh/config,relay 别名解析错配且无声
|
|
@@ -218,44 +211,21 @@ function installNode(ctx) {
|
|
|
218
211
|
// (plist 侧是 XML 数组无此问题)——当场拒绝比装出一个起不来的 unit 诚实
|
|
219
212
|
if (/\s/.test(home))
|
|
220
213
|
throw new Error(`node 的 HOME(${home})含空白字符:systemd ExecStart 无法转义,拒绝安装`);
|
|
221
|
-
const
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
214
|
+
const r = systemd.syncUnit(runner, {
|
|
215
|
+
unitPath: path.join(paths.systemdUnitDir, unitName),
|
|
216
|
+
unitName,
|
|
217
|
+
content: renderRelayUnit({
|
|
218
|
+
binDir: paths.binDir,
|
|
219
|
+
relayAlias: topo.relay.alias,
|
|
220
|
+
relayPort: ctx.self.relayPort,
|
|
221
|
+
home,
|
|
222
|
+
}),
|
|
226
223
|
});
|
|
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
|
-
}
|
|
224
|
+
if (r === 'unchanged') {
|
|
243
225
|
return comp
|
|
244
226
|
? `组件已刷新${comp};unit 无需改动`
|
|
245
227
|
: '已安装,无需改动';
|
|
246
228
|
}
|
|
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
229
|
return `安装完成${comp},relay 已开启(${ctx.topo.relay.alias} 入口 ${ctx.self.relayPort})`;
|
|
260
230
|
}
|
|
261
231
|
|
|
@@ -277,18 +247,16 @@ function reconcileEdgeFragments(ctx) {
|
|
|
277
247
|
files = fs.readdirSync(paths.confDir).filter((f) => f.startsWith('edge-'));
|
|
278
248
|
} catch { files = []; }
|
|
279
249
|
|
|
250
|
+
// disabled 字典是整域同一份:循环外取一次,各 edge 的 inspect 共用
|
|
251
|
+
const dmap = launchd.disabledMap(ctx.runner, ctx.uid);
|
|
280
252
|
for (const e of topo.edges) {
|
|
281
253
|
const file = paths.edgeHostFile(e.alias);
|
|
282
|
-
const st = launchd.
|
|
254
|
+
const st = launchd.inspectWith(ctx.runner, ctx.uid, edgeLabel(e.alias), dmap);
|
|
283
255
|
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 {
|
|
256
|
+
if (edgeFragmentShouldExist(st, v6))
|
|
257
|
+
syncConfFile(file, edgeFragmentContent(e, v6));
|
|
258
|
+
else
|
|
290
259
|
fs.rmSync(file, { force: true });
|
|
291
|
-
}
|
|
292
260
|
files = files.filter((f) => f !== `edge-${e.alias}`);
|
|
293
261
|
}
|
|
294
262
|
// 剩下的都是 topology 不认识的孤儿
|
|
@@ -11,15 +11,18 @@
|
|
|
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 {
|
|
25
|
+
edgeLabel, edgeLinkName, plistFileName, renderUplinkEdgePlist, coreViaRelayArgs,
|
|
23
26
|
} from '../lib/render.mjs';
|
|
24
27
|
|
|
25
28
|
/** edge 侧报名:取 v6 + 命令行 -J 经 relay 调 core 的 _register。
|
|
@@ -51,50 +54,20 @@ function runEdgeAnnounce(ctx) {
|
|
|
51
54
|
const v6Err = explainV6Error(v6);
|
|
52
55
|
if (v6Err) throw new Error(`本机 v6 选取异常(sn-ipv6 输出不是可报名地址:${v6Err})`);
|
|
53
56
|
// 报名固定走 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 });
|
|
57
|
+
// 无人值守首连信任 accept-new(探测侧 status 不带它,差异在 coreViaRelayArgs
|
|
58
|
+
// 显式)+ IdentitiesOnly 防多 key 试错撞 MaxAuthTries(design §4.3)。
|
|
59
|
+
const r = ctx.runner.run('ssh', coreViaRelayArgs(ctx.topo, {
|
|
60
|
+
connectTimeout: 15,
|
|
61
|
+
acceptHost: true,
|
|
62
|
+
extraOpts: ['-o', 'ServerAliveInterval=10', '-o', 'ServerAliveCountMax=2'],
|
|
63
|
+
remoteCommand: `ssh-net _register ${ctx.self.alias} ${v6}`,
|
|
64
|
+
}), { timeout: 60_000 });
|
|
71
65
|
if (r.code !== 0)
|
|
72
66
|
throw new Error(`报名失败(${r.stderr.trim() || `exit ${r.code}`}):检查 ${ctx.topo.relay.alias} 与 core 侧 relay 是否可用,稍后重试`);
|
|
73
67
|
const core = r.stdout.trim();
|
|
74
68
|
return `已报名(本机 v6 ${v6},core 返回 ${core})。core 几秒内回拨建立 peer;ssh-net status 查看结果`;
|
|
75
69
|
}
|
|
76
70
|
|
|
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
71
|
/** kickstart 失败时把登记回滚到 job 实际在拨的 v6(null=两处都删)。
|
|
99
72
|
* 不回滚的话磁盘已是新 v6,下次报名若进程已被踢活会拿磁盘值当真,
|
|
100
73
|
* job 实际还拨旧 v6 拨到六连败休眠。 */
|
|
@@ -117,16 +90,20 @@ function rollbackRegistration(ctx, edge, prevV6) {
|
|
|
117
90
|
* 文案(带上报名详情:v6 与 core 的响应)。先报名、成功后才落开关——
|
|
118
91
|
* 未安装(fatal)零副作用;可重试的报名失败(relay 不通、当场无 v6)
|
|
119
92
|
* 不阻断用户意图,开关照置:下次发起连接时 sn-route 会再报,也可再跑
|
|
120
|
-
* 一次 ssh-net start peer
|
|
121
|
-
|
|
93
|
+
* 一次 ssh-net start peer。
|
|
94
|
+
* @param {object} ctx
|
|
95
|
+
* @param {boolean} [alreadyOn] 开关此前已全开:措辞用「已是开启状态」(重复
|
|
96
|
+
* start 是幂等确认)——由 start 在调用前传,文案选词不留给调用方替换字符串 */
|
|
97
|
+
export function enablePeer(ctx, alreadyOn = false) {
|
|
98
|
+
const prefix = alreadyOn ? '隧道已是开启状态' : 'peer 已开启';
|
|
122
99
|
try {
|
|
123
100
|
const detail = runEdgeAnnounce(ctx);
|
|
124
101
|
ctx.state.setOn(['relay', 'peer']); // relay 是根:peer 依赖它(design §5 级联)
|
|
125
|
-
return
|
|
102
|
+
return `${prefix}(relay 一并开启;${detail})`;
|
|
126
103
|
} catch (err) {
|
|
127
104
|
if (err && err.fatal) throw err;
|
|
128
105
|
ctx.state.setOn(['relay', 'peer']);
|
|
129
|
-
return
|
|
106
|
+
return `${prefix}(relay 一并开启;本次报名没成功——不影响开关,下次发起连接时会自动再报,也可再跑一次 ssh-net start peer)`;
|
|
130
107
|
}
|
|
131
108
|
}
|
|
132
109
|
|
|
@@ -157,12 +134,8 @@ export function runRegister(ctx, args = []) {
|
|
|
157
134
|
binDir: ctx.paths.binDir, edgeAlias: alias, user: edge.user,
|
|
158
135
|
logsDir: ctx.paths.logsDir,
|
|
159
136
|
});
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
existing = fs.readFileSync(plistPath, 'utf8');
|
|
163
|
-
} catch {
|
|
164
|
-
existing = null; // 未注册(从未落盘)或被手删
|
|
165
|
-
}
|
|
137
|
+
// 未注册(从未落盘)或被手删 → null
|
|
138
|
+
const existing = readFileOrNull(plistPath);
|
|
166
139
|
const prevV6 = ctx.state.readEdgeV6(alias);
|
|
167
140
|
const st = launchd.inspect(ctx.runner, ctx.uid, label);
|
|
168
141
|
|
|
@@ -7,6 +7,8 @@ import fs from 'node:fs';
|
|
|
7
7
|
import path from 'node:path';
|
|
8
8
|
import * as launchd from '../lib/launchd.mjs';
|
|
9
9
|
import * as systemd from '../lib/systemd.mjs';
|
|
10
|
+
import { ensureLoaded } from '../lib/manage.mjs';
|
|
11
|
+
import { assertUnitSwitchArgs } from '../lib/cliargs.mjs';
|
|
10
12
|
import { relayLabel, relayUnitName, plistFileName } from '../lib/render.mjs';
|
|
11
13
|
import { enablePeer } from './register.mjs';
|
|
12
14
|
|
|
@@ -26,22 +28,12 @@ function startCore(ctx) {
|
|
|
26
28
|
const label = relayLabel(ctx.topo.relay.alias);
|
|
27
29
|
const plist = path.join(ctx.paths.launchAgentsDir, plistFileName(label));
|
|
28
30
|
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 残留:清掉防退出后不重拉
|
|
31
|
+
// start 语义:在册但停着要踢活;在跑但 disabled 只清 override、不闪断
|
|
32
|
+
const action = ensureLoaded(ctx, { label, plistPath: plist }, {
|
|
33
|
+
kickWhenStopped: true,
|
|
34
|
+
});
|
|
35
|
+
if (action === 'bootstrapped') return 'relay 已开启';
|
|
36
|
+
if (action === 'kickstarted') return 'relay 已重新拉起';
|
|
45
37
|
return 'relay 已在运行';
|
|
46
38
|
}
|
|
47
39
|
|
|
@@ -51,18 +43,15 @@ export function run(ctx, args = []) {
|
|
|
51
43
|
const targets = parseTargets(args); // ['relay','peer'] 或 ['relay'];link 抛错
|
|
52
44
|
|
|
53
45
|
if (targets.includes('peer')) {
|
|
54
|
-
// 默认/-a/peer:enablePeer 置两开关(幂等)+
|
|
55
|
-
|
|
56
|
-
return before.relay && before.peer
|
|
57
|
-
? msg.replace('peer 已开启', '隧道已是开启状态')
|
|
58
|
-
: msg;
|
|
46
|
+
// 默认/-a/peer:enablePeer 置两开关(幂等)+ 立即报名。开关此前已
|
|
47
|
+
// 全开时措辞由 enablePeer 内部一处选词
|
|
48
|
+
return enablePeer(ctx, before.relay && before.peer);
|
|
59
49
|
}
|
|
60
50
|
ctx.state.setOn(['relay']);
|
|
61
51
|
return `relay ${before.relay ? '已是开启状态' : '已开启'}(隧道基础,不报名——要被 core 连到再 ssh-net start peer)`;
|
|
62
52
|
}
|
|
63
53
|
// core/node 是单总开关(等同 --all):显式 -a/--all 收下,其余参数拒绝
|
|
64
|
-
|
|
65
|
-
throw new Error('本机是单总开关,start 不带参数(-a/--all 亦可,等同缺省)');
|
|
54
|
+
assertUnitSwitchArgs(args, 'start');
|
|
66
55
|
if (ctx.role === 'core') return startCore(ctx);
|
|
67
56
|
if (ctx.role === 'node') {
|
|
68
57
|
const r = systemd.startUnit(ctx.runner, relayUnitName(ctx.topo.relay.alias));
|