ssh-net 26.10.39 → 26.10.41
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 +73 -0
- package/libexec/commands/doctor.mjs +3 -1
- package/libexec/commands/install.mjs +11 -5
- package/libexec/commands/register.mjs +22 -15
- package/libexec/commands/restart.mjs +8 -10
- package/libexec/commands/start.mjs +3 -11
- package/libexec/commands/status.mjs +24 -16
- package/libexec/commands/stop.mjs +2 -3
- package/libexec/commands/uninstall.mjs +3 -1
- package/libexec/commands/upgrade.mjs +10 -4
- package/libexec/lib/config.mjs +6 -5
- package/libexec/lib/fsutil.mjs +13 -0
- package/libexec/lib/ipv6.mjs +4 -12
- package/libexec/lib/manage.mjs +4 -2
- package/libexec/lib/manifest.mjs +4 -3
- package/libexec/lib/paths.mjs +5 -4
- package/libexec/lib/probe.mjs +16 -1
- package/libexec/lib/render.mjs +44 -33
- package/libexec/lib/runtime.mjs +2 -5
- package/libexec/lib/state.mjs +8 -20
- package/libexec/lib/systemd.mjs +23 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,79 @@
|
|
|
7
7
|
GitHub Release(Actions 自动跑测试后 npm publish;跳号与 0.x 的完整
|
|
8
8
|
考据见 git 史)。
|
|
9
9
|
|
|
10
|
+
## [26.10.41] - 2026-10-04
|
|
11
|
+
|
|
12
|
+
### 全仓存量复核:参数校验补齐与失败文案修正
|
|
13
|
+
|
|
14
|
+
**修正(3 处,存量代码复核产出)**
|
|
15
|
+
- status:装了 link 但 topology 没配 forward 行时,link 行不再渲染残缺的
|
|
16
|
+
「✗ 未就绪( 没在听…)」——那是配置空,不是线路故障;改为「已安装
|
|
17
|
+
(topology 没配转发口:要转发口加 forward local 行后重跑 install)」
|
|
18
|
+
- 参数校验对齐 start/stop 口径:restart / doctor / status / uninstall /
|
|
19
|
+
upgrade 拒绝多余参数(原先静默吞掉,`ssh-net restart peer` 会被当成
|
|
20
|
+
无参 restart);install 在 core/node 上拒绝参数并点名 `--with-link`
|
|
21
|
+
是 edge 附加件——错打的 flag 不再看起来被接受
|
|
22
|
+
- upgrade 失败详情单源 `failWhy`(npm 与 install 两处共用):被信号杀
|
|
23
|
+
(code null,典型是超时)报「被信号杀死(SIGTERM,多半是超时)」
|
|
24
|
+
而不是没信息的「exit null」;⚠️ 版本戳警告前补换行,不与 install
|
|
25
|
+
输出挤成一句
|
|
26
|
+
|
|
27
|
+
**测试**
|
|
28
|
+
- 新行为全部锚上用例(9 个新增 + 1 处强化):各命令参数拒绝(含
|
|
29
|
+
「拒绝发生在 npm 之前」「已装产物不动」的顺序锚)、零转发口的
|
|
30
|
+
link 行渲染、两处信号死亡文案、⚠️ 换行
|
|
31
|
+
- 测试:全套 239(237 过、2 跳过)
|
|
32
|
+
|
|
33
|
+
升级:libexec 有变化,五台都升(`ssh-net upgrade` 自带 install 对账)。
|
|
34
|
+
|
|
35
|
+
## [26.10.40] - 2026-10-03
|
|
36
|
+
|
|
37
|
+
### 全仓简化收敛:单源化与测试样板收敛(四路审查:复用/简化/效率/层次)
|
|
38
|
+
|
|
39
|
+
**单源收敛(行为等价,渲染快照测试钉死字节不变)**
|
|
40
|
+
- 三个 plist 渲染函数抽公共骨架 `renderPlist`(KeepAlive 形态/throttle 是
|
|
41
|
+
仅有的差异项,此前骨架手抄三份)
|
|
42
|
+
- 控制面 ssh 公共 `-o` 选项单源 `probeSshOpts`:`coreViaRelayArgs` 与
|
|
43
|
+
status 的 relay 口探测共用(此前两处各手拼一份五选项)
|
|
44
|
+
- sn-ipv6 三态口径单源 `classifyV6Probe`(register 报名与 status 诊断
|
|
45
|
+
共用「探测没跑成 ≠ 网络没有 IPv6」)。**本版唯一行为修正**:register
|
|
46
|
+
此前对非零退出且空输出的探测会谎报「网络没有 IPv6」,现在如实报
|
|
47
|
+
「探测没跑成」(sn-ipv6 契约退出码恒 0,非零即异常)
|
|
48
|
+
- 开关名单 mjs 侧单源:state 导出 `SWITCH_NAMES`,start/stop 的目标解析
|
|
49
|
+
消费(此前四处各写一份)
|
|
50
|
+
- relay 行别名校验走 host 行同一份 `parseAliasList`(保留字/合法性规则
|
|
51
|
+
单源;带逗号从「别名非法」改为显式报「relay 别名不支持附加名」)
|
|
52
|
+
- `paths.plistFile` 目录与命名公式单源(`launchAgentsDir` 变量 +
|
|
53
|
+
`render.plistFileName`,此前双嵌 join + 字面量二抄)
|
|
54
|
+
- 「读文件 trim 空即 null」下沉 `fsutil.readTrimmedOrNull`(edges v6 与
|
|
55
|
+
runtime 版本戳共用)
|
|
56
|
+
|
|
57
|
+
**省掉没人消费的工作**
|
|
58
|
+
- manage/restart 四处只要 `registered` 的调用点 `inspect`→`printState`:
|
|
59
|
+
uninstall(逐 job)、_register 重部署、restart 不再每次白跑一遍全域
|
|
60
|
+
`print-disabled`(省一次 launchctl spawn)
|
|
61
|
+
|
|
62
|
+
**惯用法对齐**
|
|
63
|
+
- node 侧 start/restart 的 enable→start/restart 序列下沉
|
|
64
|
+
`systemd.enableStart/enableRestart`(「enable 先行」纪律不再靠注释
|
|
65
|
+
跨文件同步)
|
|
66
|
+
- register 部署失败回滚的 null 分支统一走 `removeEdgeRegistration`;
|
|
67
|
+
install reconcile 删片段统一 `removeConfFile`(不再裸 rmSync)
|
|
68
|
+
- state 两处手写吞 ENOENT 的 unlink 改 `fs.rmSync(force)`;删生产零调用
|
|
69
|
+
的 `isDialableV6` 与 manifest 两个无外部消费者的 export;register/probe
|
|
70
|
+
的 bin 脚本名改用 manifest 常量
|
|
71
|
+
|
|
72
|
+
**测试样板收敛**
|
|
73
|
+
- fake-system 补 `patchJob`/`subsOf`/`linesOf`,status 的 `runRawByFile`
|
|
74
|
+
提升 helpers,新增 `tmpDir(t)`:五份手抄 job 形状、六份局部 calls 过滤
|
|
75
|
+
helper、九处 runRaw 按脚本分发、约 34 块手写临时目录样板全部收敛;
|
|
76
|
+
删 17 处与 harness `t.after` 重复的手动 rmSync(home)
|
|
77
|
+
- 补 renderRoutes 布局三源锚:渲染字面量 ↔ `machinePaths` ↔ sn-route
|
|
78
|
+
默认值(树根迁移时不再有无声漂移点)
|
|
79
|
+
- 测试:全套 230(228 过、2 跳过)
|
|
80
|
+
|
|
81
|
+
升级:libexec 有变化,五台都升(`ssh-net upgrade` 自带 install 对账)。
|
|
82
|
+
|
|
10
83
|
## [26.10.39] - 2026-10-03
|
|
11
84
|
|
|
12
85
|
### 文档:与两拨模型/锁推导/Release 发布对齐,去重瘦身(libexec 无变化)
|
|
@@ -7,7 +7,9 @@ import { snRouteArgv } from '../lib/render.mjs';
|
|
|
7
7
|
|
|
8
8
|
// 返回 {text, issues}:cli 见 issues 非空即退出 1(design §5:doctor
|
|
9
9
|
// 退出 0=peer 直达,非零=本次经中继,供脚本串接判定)
|
|
10
|
-
export async function run(ctx) {
|
|
10
|
+
export async function run(ctx, args = []) {
|
|
11
|
+
if (args.length)
|
|
12
|
+
throw new Error('用法:doctor 不带参数');
|
|
11
13
|
if (ctx.role !== 'edge')
|
|
12
14
|
return {
|
|
13
15
|
text: '本机不是 edge,无需处理:core 等对应笔记本报名自动恢复,node 由 systemd 自动重启',
|
|
@@ -8,7 +8,7 @@ import path from 'node:path';
|
|
|
8
8
|
import { renderForwardArgs } from '../lib/config.mjs';
|
|
9
9
|
import { CALLBACK_PORT } from '../lib/constants.mjs';
|
|
10
10
|
import { syncPlist, removeJob } from '../lib/manage.mjs';
|
|
11
|
-
import { syncConfFile } from '../lib/conf.mjs';
|
|
11
|
+
import { syncConfFile, removeConfFile } 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';
|
|
@@ -249,12 +249,12 @@ function reconcileEdgeFragments(ctx) {
|
|
|
249
249
|
if (edgeFragmentShouldExist(st, v6))
|
|
250
250
|
syncConfFile(file, edgeFragmentContent(e, v6));
|
|
251
251
|
else
|
|
252
|
-
|
|
252
|
+
removeConfFile(file);
|
|
253
253
|
files = files.filter((f) => f !== `edge-${e.alias}`);
|
|
254
254
|
}
|
|
255
255
|
// 剩下的都是 topology 不认识的孤儿
|
|
256
256
|
for (const f of files)
|
|
257
|
-
|
|
257
|
+
removeConfFile(path.join(paths.confDir, f));
|
|
258
258
|
}
|
|
259
259
|
|
|
260
260
|
/**
|
|
@@ -265,8 +265,14 @@ export async function run(ctx, args = []) {
|
|
|
265
265
|
let result;
|
|
266
266
|
switch (ctx.role) {
|
|
267
267
|
case 'edge': result = await installEdge(ctx, args); break;
|
|
268
|
-
case 'core':
|
|
269
|
-
case 'node':
|
|
268
|
+
case 'core':
|
|
269
|
+
case 'node':
|
|
270
|
+
// --with-link 是 edge 附加件:core/node 上静默吞掉会让错打的 flag
|
|
271
|
+
// 看起来被接受了
|
|
272
|
+
if (args.length)
|
|
273
|
+
throw new Error(`本机是 ${ctx.role},install 不接受参数(--with-link/--without-link 是 edge 附加件的开关)`);
|
|
274
|
+
result = ctx.role === 'core' ? installCore(ctx) : installNode(ctx);
|
|
275
|
+
break;
|
|
270
276
|
default: throw new Error(`未知角色:${ctx.role}`);
|
|
271
277
|
}
|
|
272
278
|
return result;
|
|
@@ -17,11 +17,12 @@ import { syncPlist } from '../lib/manage.mjs';
|
|
|
17
17
|
import { syncConfFile, removeConfFile } from '../lib/conf.mjs';
|
|
18
18
|
import { ensureRuntime } from '../lib/runtime.mjs';
|
|
19
19
|
import { readFileOrNull } from '../lib/fsutil.mjs';
|
|
20
|
+
import { classifyV6Probe } from '../lib/probe.mjs';
|
|
20
21
|
import {
|
|
21
22
|
writeEdgeRegistration, removeEdgeRegistration,
|
|
22
23
|
edgeFragmentContent, edgeFragmentShouldExist,
|
|
23
24
|
} from '../lib/registration.mjs';
|
|
24
|
-
import { edgeCallbacksPlan } from '../lib/manifest.mjs';
|
|
25
|
+
import { SN_IPV6, edgeCallbacksPlan } from '../lib/manifest.mjs';
|
|
25
26
|
import {
|
|
26
27
|
edgeLabel, renderUplinkEdgePlist, coreViaRelayArgs,
|
|
27
28
|
} from '../lib/render.mjs';
|
|
@@ -30,7 +31,7 @@ import {
|
|
|
30
31
|
* start peer 用——控制面直接报一次名让 core 马上回拨。 */
|
|
31
32
|
function runEdgeAnnounce(ctx) {
|
|
32
33
|
const probe = ctx.runner.runRaw(
|
|
33
|
-
ctx.paths.binScript(
|
|
34
|
+
ctx.paths.binScript(SN_IPV6), [], { timeout: 5000 },
|
|
34
35
|
);
|
|
35
36
|
// bin/sn-ipv6 缺失(未 install)别误报成「无 IPv6」。fatal 哨兵属性:
|
|
36
37
|
// enablePeer 靠它区分「没有现场选路可指望」的致命错与可重试的报名失败,
|
|
@@ -40,14 +41,17 @@ function runEdgeAnnounce(ctx) {
|
|
|
40
41
|
e.fatal = true;
|
|
41
42
|
throw e;
|
|
42
43
|
}
|
|
43
|
-
//
|
|
44
|
-
//
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
44
|
+
// 三态口径单源在 classifyV6Probe(与 status 共用):探测没跑成和探测
|
|
45
|
+
// 结果是两回事,failed 绝不能谎称「网络没有 IPv6」
|
|
46
|
+
const { state, v6 } = classifyV6Probe(probe);
|
|
47
|
+
if (state === 'failed') {
|
|
48
|
+
// code null = 被信号杀(spawnSync 此时 error 为 null);ENOENT 已在上面拦走
|
|
49
|
+
const why = probe.error?.message
|
|
50
|
+
?? (probe.signal !== null && probe.signal !== undefined
|
|
51
|
+
? `signal ${probe.signal}` : `exit ${probe.code}`);
|
|
52
|
+
throw new Error(`sn-ipv6 探测没跑成(${why})——检查 ~/.ssh/ssh-net/bin/sn-ipv6 后重试,别据此断言网络没有 IPv6`);
|
|
53
|
+
}
|
|
54
|
+
if (state === 'none')
|
|
51
55
|
throw new Error('当前网络没有可用 IPv6,没有可报名的地址(中继不受影响)');
|
|
52
56
|
// 拼进远端命令串前本地校验(纵深防御,与 _register 侧 explainV6Error 同源):
|
|
53
57
|
// v6 来自本机 sn-ipv6,正常必合法;输出被污染(多行/元字符)时在这里
|
|
@@ -89,13 +93,16 @@ function rollbackEdgeRegistration(ctx, edge, prevV6, label) {
|
|
|
89
93
|
removeConfFile(file);
|
|
90
94
|
}
|
|
91
95
|
|
|
92
|
-
/** 未注册/重部署分支的部署失败回滚:state 恢复旧 v6
|
|
93
|
-
*
|
|
96
|
+
/** 未注册/重部署分支的部署失败回滚:state 恢复旧 v6(无旧值则两处同删,
|
|
97
|
+
* 与 rollbackEdgeRegistration 的 null 分支同一原语)、片段删除——没有
|
|
98
|
+
* job 兜底的片段就是假入口。回滚失败不遮蔽原始错误。 */
|
|
94
99
|
function rollbackRegistration(ctx, edge, prevV6) {
|
|
95
100
|
try {
|
|
96
|
-
if (prevV6 === null) ctx
|
|
97
|
-
else
|
|
98
|
-
|
|
101
|
+
if (prevV6 === null) removeEdgeRegistration(ctx, edge.alias);
|
|
102
|
+
else {
|
|
103
|
+
ctx.state.writeEdgeV6(edge.alias, prevV6);
|
|
104
|
+
removeConfFile(ctx.paths.edgeHostFile(edge.alias));
|
|
105
|
+
}
|
|
99
106
|
} catch { /* 回滚失败不遮蔽原始错误 */ }
|
|
100
107
|
}
|
|
101
108
|
|
|
@@ -5,11 +5,14 @@ import * as launchd from '../lib/launchd.mjs';
|
|
|
5
5
|
import * as systemd from '../lib/systemd.mjs';
|
|
6
6
|
import { linkLabel, relayLabel, relayUnitName } from '../lib/render.mjs';
|
|
7
7
|
|
|
8
|
-
export function run(ctx) {
|
|
8
|
+
export function run(ctx, args = []) {
|
|
9
|
+
if (args.length)
|
|
10
|
+
throw new Error('用法:restart 不带参数');
|
|
9
11
|
if (ctx.role === 'edge') {
|
|
10
12
|
if (!ctx.state.isOn('relay')) throw new Error('隧道未开启——先 ssh-net start');
|
|
11
13
|
const label = linkLabel(ctx.topo.core.alias);
|
|
12
|
-
|
|
14
|
+
// 只要 registered(printState),不跑全域 print-disabled
|
|
15
|
+
if (!launchd.printState(ctx.runner, ctx.uid, label).registered) {
|
|
13
16
|
return '默认形态没有常驻服务,没有可重启的对象——人在机器前要立即选路跑 ssh-net doctor;要 SOCKS/VNC 转发口跑 ssh-net install --with-link';
|
|
14
17
|
}
|
|
15
18
|
// kickstartEnabled:与 core 同规则,残留 disabled override 时不被拒
|
|
@@ -19,20 +22,15 @@ export function run(ctx) {
|
|
|
19
22
|
}
|
|
20
23
|
if (ctx.role === 'core') {
|
|
21
24
|
const label = relayLabel(ctx.topo.relay.alias);
|
|
22
|
-
|
|
25
|
+
// 只要 registered(printState),不跑全域 print-disabled
|
|
26
|
+
if (!launchd.printState(ctx.runner, ctx.uid, label).registered)
|
|
23
27
|
throw new Error('uplink-relay 未注册(已 stop?先 ssh-net start)');
|
|
24
28
|
const r = launchd.kickstartEnabled(ctx.runner, ctx.uid, label);
|
|
25
29
|
if (r.code !== 0) throw new Error(`重启失败:${r.stderr.trim()}`);
|
|
26
30
|
return 'relay 已重启';
|
|
27
31
|
}
|
|
28
32
|
if (ctx.role === 'node') {
|
|
29
|
-
|
|
30
|
-
// 先 enable 清 stop 的 disable(同 mac kickstartEnabled 先清 override),
|
|
31
|
-
// 否则重启只这次在跑、重启后依旧关闭
|
|
32
|
-
const e = systemd.enableUnit(ctx.runner, unit);
|
|
33
|
-
if (e.code !== 0) throw new Error(`systemctl enable 失败:${e.stderr.trim()}`);
|
|
34
|
-
const r = systemd.restartUnit(ctx.runner, unit);
|
|
35
|
-
if (r.code !== 0) throw new Error(`systemctl restart 失败:${r.stderr.trim()}`);
|
|
33
|
+
systemd.enableRestart(ctx.runner, relayUnitName(ctx.topo.relay.alias));
|
|
36
34
|
return 'relay 已重启';
|
|
37
35
|
}
|
|
38
36
|
throw new Error(`未知角色:${ctx.role}`);
|
|
@@ -7,15 +7,14 @@ import fs from 'node:fs';
|
|
|
7
7
|
import * as systemd from '../lib/systemd.mjs';
|
|
8
8
|
import { ensureLoaded } from '../lib/manage.mjs';
|
|
9
9
|
import { assertUnitSwitchArgs } from '../lib/cliargs.mjs';
|
|
10
|
+
import { SWITCH_NAMES } from '../lib/state.mjs';
|
|
10
11
|
import { relayLabel, relayUnitName } from '../lib/render.mjs';
|
|
11
12
|
import { enablePeer } from './register.mjs';
|
|
12
13
|
|
|
13
|
-
const TARGETS = ['relay', 'peer'];
|
|
14
|
-
|
|
15
14
|
function parseTargets(args) {
|
|
16
15
|
if (args.length === 0 || (args.length === 1 && (args[0] === '-a' || args[0] === '--all')))
|
|
17
16
|
return ['relay', 'peer'];
|
|
18
|
-
if (args.length === 1 &&
|
|
17
|
+
if (args.length === 1 && SWITCH_NAMES.includes(args[0]))
|
|
19
18
|
return [args[0]];
|
|
20
19
|
if (args.length === 1 && args[0] === 'link')
|
|
21
20
|
throw new Error('link 不再是运行时开关:转发口只取决于装没装附加件——加装 ssh-net install --with-link,移除 ssh-net install --without-link');
|
|
@@ -52,14 +51,7 @@ export function run(ctx, args = []) {
|
|
|
52
51
|
assertUnitSwitchArgs(args, 'start');
|
|
53
52
|
if (ctx.role === 'core') return startCore(ctx);
|
|
54
53
|
if (ctx.role === 'node') {
|
|
55
|
-
|
|
56
|
-
// 与 mac startCore 的 enable→start 同序:stop 停服时做了 disable,只
|
|
57
|
-
// start 会让服务这次在跑、重启后依旧不起来(enable 幂等;unit 缺失报
|
|
58
|
-
// 真错,提示先 install,同 mac 侧 plist 检查)
|
|
59
|
-
const e = systemd.enableUnit(ctx.runner, unit);
|
|
60
|
-
if (e.code !== 0) throw new Error(`systemctl enable 失败:${e.stderr.trim()}`);
|
|
61
|
-
const r = systemd.startUnit(ctx.runner, unit);
|
|
62
|
-
if (r.code !== 0) throw new Error(`systemctl start 失败:${r.stderr.trim()}`);
|
|
54
|
+
systemd.enableStart(ctx.runner, relayUnitName(ctx.topo.relay.alias));
|
|
63
55
|
return 'relay 已开启';
|
|
64
56
|
}
|
|
65
57
|
throw new Error(`未知角色:${ctx.role}`);
|
|
@@ -13,7 +13,7 @@ import {
|
|
|
13
13
|
import { packageScriptPath } from '../lib/paths.mjs';
|
|
14
14
|
import * as launchd from '../lib/launchd.mjs';
|
|
15
15
|
import * as systemd from '../lib/systemd.mjs';
|
|
16
|
-
import { peerProbeOk, tcpOpen } from '../lib/probe.mjs';
|
|
16
|
+
import { peerProbeOk, tcpOpen, classifyV6Probe } from '../lib/probe.mjs';
|
|
17
17
|
import { readFileOrNull } from '../lib/fsutil.mjs';
|
|
18
18
|
import {
|
|
19
19
|
SN_ROUTE, SN_IPV6, UPLINK_EDGE,
|
|
@@ -23,7 +23,7 @@ import { edgeFragmentContent, edgeFragmentShouldExist } from '../lib/registratio
|
|
|
23
23
|
import {
|
|
24
24
|
linkLabel, relayLabel, relayUnitName,
|
|
25
25
|
edgeLabel,
|
|
26
|
-
renderRoutes, coreViaRelayArgs,
|
|
26
|
+
renderRoutes, coreViaRelayArgs, probeSshOpts,
|
|
27
27
|
} from '../lib/render.mjs';
|
|
28
28
|
|
|
29
29
|
/**
|
|
@@ -91,14 +91,11 @@ function relayPortIssue(relayAlias, port, r) {
|
|
|
91
91
|
}
|
|
92
92
|
|
|
93
93
|
async function relayPortListening(ctx, port) {
|
|
94
|
-
// relay 别名的 User/HostName 由 ssh config Host
|
|
95
|
-
|
|
96
|
-
//
|
|
97
|
-
// 同约定,status 是探测、结果必须诚实)
|
|
94
|
+
// relay 别名的 User/HostName 由 ssh config Host 块提供;公共 -o 纪律
|
|
95
|
+
//(BatchMode/IdentitiesOnly/ControlMaster)单源在 render.probeSshOpts,
|
|
96
|
+
// 与 coreViaRelayArgs 同一份(运行时五个 bash ssh 则由 constants.test 锚)
|
|
98
97
|
const r = await ctx.runner.runAsync('ssh', [
|
|
99
|
-
|
|
100
|
-
'-o', 'IdentitiesOnly=yes',
|
|
101
|
-
'-o', 'ControlMaster=no', '-o', 'ControlPath=none',
|
|
98
|
+
...probeSshOpts(5),
|
|
102
99
|
ctx.topo.relay.alias, 'ss', '-tlnH',
|
|
103
100
|
], { timeout: 8000 });
|
|
104
101
|
if (r.code !== 0) return { port, reachable: false, listening: false, detail: r.stderr.trim() };
|
|
@@ -183,8 +180,11 @@ async function collectEdge(ctx) {
|
|
|
183
180
|
let v6 = '';
|
|
184
181
|
let v6ProbeOk = false;
|
|
185
182
|
if (v6Probe) {
|
|
186
|
-
|
|
187
|
-
|
|
183
|
+
// 三态口径单源在 classifyV6Probe(与 register 报名共用):v6ProbeOk
|
|
184
|
+
// 的语义是「探测正常跑完」(含「确认无 v6」),不是「有 v6」
|
|
185
|
+
const c = classifyV6Probe(v6Probe);
|
|
186
|
+
v6 = c.v6;
|
|
187
|
+
v6ProbeOk = c.state !== 'failed';
|
|
188
188
|
}
|
|
189
189
|
|
|
190
190
|
if (lanReachable)
|
|
@@ -376,10 +376,16 @@ export function renderText(s) {
|
|
|
376
376
|
lines.push(' link: 未安装(默认零常驻;要 SOCKS/VNC 转发口:ssh-net install --with-link)');
|
|
377
377
|
} else {
|
|
378
378
|
const views = s.forwardViews;
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
379
|
+
// 装了 link 但 topology 没配 forward 行:没有该监听的口,别把空列表
|
|
380
|
+
// 渲染成「✗ 未就绪( 没在听…)」——那是配置空,不是线路故障
|
|
381
|
+
if (views.length === 0) {
|
|
382
|
+
lines.push(' link: 已安装(topology 没配转发口:要转发口加 forward local 行后重跑 ssh-net install)');
|
|
383
|
+
} else {
|
|
384
|
+
const allOk = views.every(([, ok]) => ok);
|
|
385
|
+
lines.push(allOk
|
|
386
|
+
? ` link: ✓ 就绪(${views.map(([l]) => l).join(' · ')})`
|
|
387
|
+
: ` link: ✗ 未就绪(${views.filter(([, ok]) => !ok).map(([l]) => l).join('、')} 没在听;隧道建立中)`);
|
|
388
|
+
}
|
|
383
389
|
}
|
|
384
390
|
lines.push(s.v6 !== ''
|
|
385
391
|
? ` 对外地址: ${s.v6}`
|
|
@@ -419,7 +425,9 @@ export function renderText(s) {
|
|
|
419
425
|
return lines.join('\n');
|
|
420
426
|
}
|
|
421
427
|
|
|
422
|
-
export async function run(ctx) {
|
|
428
|
+
export async function run(ctx, args = []) {
|
|
429
|
+
if (args.length)
|
|
430
|
+
throw new Error('用法:status 不带参数');
|
|
423
431
|
const s = await collect(ctx);
|
|
424
432
|
return { text: renderText(s), issues: s.issues };
|
|
425
433
|
}
|
|
@@ -8,14 +8,13 @@ import * as launchd from '../lib/launchd.mjs';
|
|
|
8
8
|
import * as systemd from '../lib/systemd.mjs';
|
|
9
9
|
import { reapEdgeLink } from '../lib/jobctl.mjs';
|
|
10
10
|
import { assertUnitSwitchArgs } from '../lib/cliargs.mjs';
|
|
11
|
+
import { SWITCH_NAMES } from '../lib/state.mjs';
|
|
11
12
|
import { relayLabel, relayUnitName } from '../lib/render.mjs';
|
|
12
13
|
|
|
13
|
-
const TARGETS = ['relay', 'peer'];
|
|
14
|
-
|
|
15
14
|
function parseTarget(args) {
|
|
16
15
|
if (args.length === 0 || args.length === 1 && (args[0] === '-a' || args[0] === '--all'))
|
|
17
16
|
return null; // 全关
|
|
18
|
-
if (args.length === 1 &&
|
|
17
|
+
if (args.length === 1 && SWITCH_NAMES.includes(args[0]))
|
|
19
18
|
return args[0];
|
|
20
19
|
if (args.length === 1 && args[0] === 'link')
|
|
21
20
|
throw new Error('link 不是运行时目标:移除附加件跑 ssh-net install --without-link(卸载会一并清掉)');
|
|
@@ -67,7 +67,9 @@ function uninstallNode(ctx) {
|
|
|
67
67
|
return '已卸载。最后一步:npm uninstall -g ssh-net';
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
-
export function run(ctx) {
|
|
70
|
+
export function run(ctx, args = []) {
|
|
71
|
+
if (args.length)
|
|
72
|
+
throw new Error('用法:uninstall 不带参数');
|
|
71
73
|
switch (ctx.role) {
|
|
72
74
|
case 'edge': return uninstallEdge(ctx);
|
|
73
75
|
case 'core': return uninstallCore(ctx);
|
|
@@ -18,7 +18,13 @@ import { readRuntimeVersion, RUNTIME_VERSION } from '../lib/runtime.mjs';
|
|
|
18
18
|
const NPM_TIMEOUT = 180_000; // 慢网络下全局装包的余量
|
|
19
19
|
const INSTALL_TIMEOUT = 120_000;
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
/** 失败详情:stderr 优先;被信号杀(code null,典型是超时)别说成 exit null */
|
|
22
|
+
const failWhy = (r) => r.stderr.trim()
|
|
23
|
+
|| (r.code !== null ? `exit ${r.code}` : `被信号杀死(${r.signal ?? '未知'},多半是超时)`);
|
|
24
|
+
|
|
25
|
+
export async function run(ctx, args = []) {
|
|
26
|
+
if (args.length)
|
|
27
|
+
throw new Error('用法:upgrade 不带参数');
|
|
22
28
|
if (ctx.role === 'node' && ctx.uid !== 0)
|
|
23
29
|
throw new Error('node 上升级要写 /etc/systemd/system(install 步骤),请以 root 运行(sudo ssh-net upgrade)');
|
|
24
30
|
|
|
@@ -41,7 +47,7 @@ export async function run(ctx) {
|
|
|
41
47
|
);
|
|
42
48
|
}
|
|
43
49
|
if (r.code !== 0)
|
|
44
|
-
throw new Error(`npm 升级失败:${r
|
|
50
|
+
throw new Error(`npm 升级失败:${failWhy(r)}(网络或 registry 问题;已装的版本不受影响,稍后重试)`);
|
|
45
51
|
|
|
46
52
|
// 换装后用新 bin 跑 install:对账运行时实体/软链/plist/unit 并落
|
|
47
53
|
// runtime-version 戳(幂等,健康的 job 不断连)。PATH 上找 ssh-net:
|
|
@@ -51,7 +57,7 @@ export async function run(ctx) {
|
|
|
51
57
|
if (inst.error && inst.error.code === 'ENOENT')
|
|
52
58
|
throw new Error('npm 换装后 PATH 上找不到 ssh-net(全局 bin 未生效)——新开一个 shell 或手动跑一次 ssh-net install;若反复出现,检查 fnm 的 global 包路径');
|
|
53
59
|
if (inst.code !== 0)
|
|
54
|
-
throw new Error(`包已升级,但 install 刷新没成功:${inst
|
|
60
|
+
throw new Error(`包已升级,但 install 刷新没成功:${failWhy(inst)}——手动跑一次 ssh-net install 修复`);
|
|
55
61
|
|
|
56
62
|
// 后置校验:真换装后新 bin 的 install 必把版本戳刷到新版本(ensureRuntime
|
|
57
63
|
// 无条件 refreshStamp);戳仍是本进程——即旧包——的版本,说明 PATH 解析到
|
|
@@ -60,7 +66,7 @@ export async function run(ctx) {
|
|
|
60
66
|
// 版本恒等,该校验无意义,跳过
|
|
61
67
|
const stamp = readRuntimeVersion(ctx.paths);
|
|
62
68
|
if (!upToDate && stamp !== null && stamp === RUNTIME_VERSION)
|
|
63
|
-
return `升级完成。${inst.stdout.trim()}⚠️ 但运行时版本戳仍是 ${stamp}——install 跑的不是新装的 ssh-net(PATH 可能解析到旧安装,如 fnm 多版本残留):which -a ssh-net 排查,再手动跑一次 ssh-net install 完成对账`;
|
|
69
|
+
return `升级完成。${inst.stdout.trim()}\n⚠️ 但运行时版本戳仍是 ${stamp}——install 跑的不是新装的 ssh-net(PATH 可能解析到旧安装,如 fnm 多版本残留):which -a ssh-net 排查,再手动跑一次 ssh-net install 完成对账`;
|
|
64
70
|
// 文件已全部对账,但**在跑的 job 进程仍是旧脚本**(install 幂等不断连,
|
|
65
71
|
// 不重启健康服务)——新行为要等进程自然重建(断线/重启)后生效;多数
|
|
66
72
|
// 运行时改动不影响已建立的隧道,不主动 restart(那会把连接断掉)
|
package/libexec/lib/config.mjs
CHANGED
|
@@ -110,12 +110,13 @@ export function parseTopology(text) {
|
|
|
110
110
|
if (head === 'relay') {
|
|
111
111
|
if (cols.length !== 2)
|
|
112
112
|
throw new ConfigError(lineNo, 'relay 需要 2 列:relay <别名>');
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
113
|
+
// 别名合法性/保留字检查与 host 行同一份(parseAliasList);relay 是
|
|
114
|
+
// 单名,逗号附加名列不支持
|
|
115
|
+
const names = parseAliasList(cols[1], lineNo);
|
|
116
|
+
if (names.length !== 1)
|
|
117
|
+
throw new ConfigError(lineNo, `relay 别名不支持附加名:${cols[1]}`);
|
|
117
118
|
if (relay) throw new ConfigError(lineNo, 'relay 只能有一行');
|
|
118
|
-
relay = { alias };
|
|
119
|
+
relay = { alias: names[0] };
|
|
119
120
|
return;
|
|
120
121
|
}
|
|
121
122
|
|
package/libexec/lib/fsutil.mjs
CHANGED
|
@@ -18,6 +18,19 @@ export function readFileOrNull(file, encoding = 'utf8') {
|
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
+
/**
|
|
22
|
+
* 读文本并 trim:不存在或只剩空白返回 null(「文件里得有值才算数」,
|
|
23
|
+
* state 的 edges v6 与 runtime 的版本戳共用)。
|
|
24
|
+
* @param {string} file
|
|
25
|
+
* @returns {string|null}
|
|
26
|
+
*/
|
|
27
|
+
export function readTrimmedOrNull(file) {
|
|
28
|
+
const text = readFileOrNull(file);
|
|
29
|
+
if (text === null) return null;
|
|
30
|
+
const trimmed = text.trim();
|
|
31
|
+
return trimmed.length > 0 ? trimmed : null;
|
|
32
|
+
}
|
|
33
|
+
|
|
21
34
|
/**
|
|
22
35
|
* 原子落盘:tmp 写好再 rename 落定,落定后 chmod 兜底(怪 umask/拷贝工具
|
|
23
36
|
* 吃位都修正到 mode)。
|
package/libexec/lib/ipv6.mjs
CHANGED
|
@@ -9,7 +9,7 @@ import net from 'node:net';
|
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* 分类一个候选 v6:合法(无 scope、非内嵌 v4 的 IPv6 字面值)给首 hextet
|
|
12
|
-
* 数值;否则给中文拒收原因。
|
|
12
|
+
* 数值;否则给中文拒收原因。explainV6Error 的唯一事实来源,
|
|
13
13
|
* 拒收规则不抄第二份。
|
|
14
14
|
* @param {unknown} value
|
|
15
15
|
* @returns {{g0:number}|{reason:string}}
|
|
@@ -27,7 +27,7 @@ function classifyV6(value) {
|
|
|
27
27
|
|
|
28
28
|
/**
|
|
29
29
|
* 可拨号段:2000::/3 公网单播或 fc00::/7 ULA(首组 0xfc00–0xfdff)。
|
|
30
|
-
* 段判据唯一来源,
|
|
30
|
+
* 段判据唯一来源,explainV6Error 从分类结果走它,不抄第二遍。
|
|
31
31
|
* @param {number} g0
|
|
32
32
|
* @returns {boolean}
|
|
33
33
|
*/
|
|
@@ -36,16 +36,8 @@ function inDialableRange(g0) {
|
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
/**
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*/
|
|
42
|
-
export function isDialableV6(value) {
|
|
43
|
-
const c = classifyV6(value);
|
|
44
|
-
return 'g0' in c && inDialableRange(c.g0);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* 给报名 RPC 报错用的中文原因;合法可拨号返回 null。
|
|
39
|
+
* 给报名 RPC 报错用的中文原因;合法可拨号返回 null(生产侧唯一出口,
|
|
40
|
+
* 布尔判定就是它 === null)。
|
|
49
41
|
* @param {unknown} value
|
|
50
42
|
* @returns {string | null}
|
|
51
43
|
*/
|
package/libexec/lib/manage.mjs
CHANGED
|
@@ -80,7 +80,8 @@ export function syncPlist(ctx, { label, plistPath, content }) {
|
|
|
80
80
|
return { lintError: lint.stderr.trim() || `plutil exit ${lint.code}` };
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
-
|
|
83
|
+
// 只要 registered 一个事实:printState 即可,不跑全域 print-disabled
|
|
84
|
+
const st = launchd.printState(runner, uid, label);
|
|
84
85
|
if (st.registered) {
|
|
85
86
|
const bo = launchd.bootout(runner, uid, label);
|
|
86
87
|
if (bo.code !== 0 && bo.code !== 113)
|
|
@@ -104,7 +105,8 @@ export function syncPlist(ctx, { label, plistPath, content }) {
|
|
|
104
105
|
*/
|
|
105
106
|
export function removeJob(ctx, label, plistPath) {
|
|
106
107
|
const { runner, uid } = ctx;
|
|
107
|
-
|
|
108
|
+
// 只要 registered(同上,不跑全域 print-disabled)
|
|
109
|
+
const st = launchd.printState(runner, uid, label);
|
|
108
110
|
if (st.registered) {
|
|
109
111
|
const bo = launchd.bootout(runner, uid, label);
|
|
110
112
|
// 113 = 未注册(竞态下消失),其余失败必须报:job 还在注册表里,删了
|
package/libexec/lib/manifest.mjs
CHANGED
|
@@ -5,12 +5,13 @@
|
|
|
5
5
|
// entities:[bin 名, 包脚本名]——原名实体;links:[带机器名软链, 实体名]。
|
|
6
6
|
import { edgeLinkName, linkLinkName, relayLinkName } from './render.mjs';
|
|
7
7
|
|
|
8
|
-
// bin 脚本名:plan 清单与按名判断(status 体检、install
|
|
8
|
+
// bin 脚本名:plan 清单与按名判断(status 体检、install/报名调用)共用,
|
|
9
|
+
// 改名一处。跨模块消费的只有 SN_* 与 UPLINK_EDGE,另两个仅本文件 plan 用
|
|
9
10
|
export const SN_PROBE = 'sn-probe';
|
|
10
11
|
export const SN_ROUTE = 'sn-route';
|
|
11
12
|
export const SN_IPV6 = 'sn-ipv6';
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
const LINK_CORE = 'ssh-net-link-core';
|
|
14
|
+
const UPLINK_RELAY = 'ssh-net-uplink-relay';
|
|
14
15
|
export const UPLINK_EDGE = 'ssh-net-uplink-edge';
|
|
15
16
|
|
|
16
17
|
/**
|
package/libexec/lib/paths.mjs
CHANGED
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
import path from 'node:path';
|
|
18
18
|
import fs from 'node:fs';
|
|
19
19
|
import { fileURLToPath } from 'node:url';
|
|
20
|
+
import { plistFileName } from './render.mjs';
|
|
20
21
|
|
|
21
22
|
/** 包内 libexec 实体所在的真实目录(本文件经 realpath 后位于 libexec/lib)。 */
|
|
22
23
|
const LIB_REAL = fs.realpathSync(fileURLToPath(import.meta.url));
|
|
@@ -43,6 +44,7 @@ export function machinePaths(env = process.env) {
|
|
|
43
44
|
if (!home) throw new Error('HOME 环境变量未设置——ssh-net 的全部产物/配置路径都挂在 HOME 下,拒绝在相对路径上盲跑');
|
|
44
45
|
const sshNetDir = path.join(home, '.ssh', 'ssh-net');
|
|
45
46
|
const stateDir = path.join(sshNetDir, 'state');
|
|
47
|
+
const launchAgentsDir = path.join(home, 'Library', 'LaunchAgents');
|
|
46
48
|
return Object.freeze({
|
|
47
49
|
sshDir: path.join(home, '.ssh'),
|
|
48
50
|
sshConfigFile: path.join(home, '.ssh', 'config'),
|
|
@@ -77,11 +79,10 @@ export function machinePaths(env = process.env) {
|
|
|
77
79
|
// ENSURE:sn-route 按需确保的 single-flight 锁(内容 epoch)
|
|
78
80
|
ensureLockFile: path.join(stateDir, 'ENSURE'),
|
|
79
81
|
edgesDir: path.join(stateDir, 'edges'),
|
|
80
|
-
launchAgentsDir
|
|
81
|
-
/** label → plist
|
|
82
|
+
launchAgentsDir,
|
|
83
|
+
/** label → plist 绝对路径(命名公式单源 = render.plistFileName)。
|
|
82
84
|
* @param {string} label local.<...> job label */
|
|
83
|
-
plistFile: (label) => path.join(
|
|
84
|
-
path.join(home, 'Library', 'LaunchAgents'), `${label}.plist`),
|
|
85
|
+
plistFile: (label) => path.join(launchAgentsDir, plistFileName(label)),
|
|
85
86
|
logsDir: path.join(home, 'Library', 'Logs'),
|
|
86
87
|
systemdUnitDir: '/etc/systemd/system',
|
|
87
88
|
});
|
package/libexec/lib/probe.mjs
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
// 只能以 sn-probe 是否读到 SSH banner 认定是不是本服务
|
|
5
5
|
import net from 'node:net';
|
|
6
6
|
import path from 'node:path';
|
|
7
|
+
import { SN_PROBE } from './manifest.mjs';
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* TCP 能否在超时内连上(status 用,mjs 侧不依赖 nc)。
|
|
@@ -144,6 +145,20 @@ export function classifyLocalPort(runner, port, jobPid, tree) {
|
|
|
144
145
|
* @returns {Promise<boolean>}
|
|
145
146
|
*/
|
|
146
147
|
export async function peerProbeOk(runner, binDir, timeoutMs = 5000) {
|
|
147
|
-
const r = await runner.runRawAsync(path.join(binDir,
|
|
148
|
+
const r = await runner.runRawAsync(path.join(binDir, SN_PROBE), [], { timeout: timeoutMs });
|
|
148
149
|
return r.code === 0;
|
|
149
150
|
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* sn-ipv6 运行结果的三态分类——「探测没跑成」与「网络没有 IPv6」是两回事,
|
|
154
|
+
* register 报名与 status 诊断共用这一份口径(各自只做呈现)。failed 盖启动
|
|
155
|
+
* 失败、被信号杀(code null)与非零退出(sn-ipv6 契约退出码恒 0,非零即异常)。
|
|
156
|
+
* @param {{code:number|null, signal?:string|null, stdout:string,
|
|
157
|
+
* error?:NodeJS.ErrnoException|null}} r Runner 的 RunResult
|
|
158
|
+
* @returns {{state:'failed'|'none'|'ok', v6:string}}
|
|
159
|
+
*/
|
|
160
|
+
export function classifyV6Probe(r) {
|
|
161
|
+
if (r.error || r.code !== 0) return { state: 'failed', v6: '' };
|
|
162
|
+
const v6 = r.stdout.trim();
|
|
163
|
+
return v6 === '' ? { state: 'none', v6: '' } : { state: 'ok', v6 };
|
|
164
|
+
}
|
package/libexec/lib/render.mjs
CHANGED
|
@@ -40,6 +40,19 @@ function logPaths({ label, logsDir }) {
|
|
|
40
40
|
`;
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
+
/** 三个 job plist 的公共骨架;keepAlive 传 KeepAlive 键的值片段(含尾换行),
|
|
44
|
+
* runAtLoad 只有 relay 要,throttle 缺省 30、link 传 2。 */
|
|
45
|
+
function renderPlist({ label, argv, keepAlive, runAtLoad = false, throttle = THROTTLE, logsDir }) {
|
|
46
|
+
return `${XML_HEAD} <key>Label</key>
|
|
47
|
+
<string>${label}</string>
|
|
48
|
+
${programArguments(argv)} <key>ProcessType</key>
|
|
49
|
+
<string>Background</string>
|
|
50
|
+
${runAtLoad ? ' <key>RunAtLoad</key>\n <true/>\n' : ''} <key>KeepAlive</key>
|
|
51
|
+
${keepAlive} <key>ThrottleInterval</key>
|
|
52
|
+
<integer>${throttle}</integer>
|
|
53
|
+
${logPaths({ label, logsDir })}${XML_TAIL}`;
|
|
54
|
+
}
|
|
55
|
+
|
|
43
56
|
// ---- 命名(design §2.3 公式 ssh-net-<link|uplink>-<对端角色>-<对端别名>)----
|
|
44
57
|
|
|
45
58
|
/** @param {string} coreAlias */
|
|
@@ -177,6 +190,23 @@ Host ${aliases.join(' ')}
|
|
|
177
190
|
|
|
178
191
|
// ---- 经 relay 拨 core 环回口的 ssh argv(报名 RPC 与 status 探测共用)----
|
|
179
192
|
|
|
193
|
+
/**
|
|
194
|
+
* 控制面 ssh 的公共 -o 基础选项(design §4.3 探测纪律的 mjs 侧单源):
|
|
195
|
+
* BatchMode 防终端闪密码提示、IdentitiesOnly 防多 key 试错撞 MaxAuthTries、
|
|
196
|
+
* ControlMaster/Path=none 防探测搭便车。coreViaRelayArgs 与 status 的
|
|
197
|
+
* relay 口探测共用;三个 bash 运行时脚本的选项集不在此列(constants.test 锚)。
|
|
198
|
+
* @param {number} connectTimeout
|
|
199
|
+
* @returns {string[]}
|
|
200
|
+
*/
|
|
201
|
+
export function probeSshOpts(connectTimeout) {
|
|
202
|
+
return [
|
|
203
|
+
'-o', 'BatchMode=yes',
|
|
204
|
+
'-o', `ConnectTimeout=${connectTimeout}`,
|
|
205
|
+
'-o', 'ControlMaster=no', '-o', 'ControlPath=none',
|
|
206
|
+
'-o', 'IdentitiesOnly=yes',
|
|
207
|
+
];
|
|
208
|
+
}
|
|
209
|
+
|
|
180
210
|
/**
|
|
181
211
|
* 命令行显式 -J 经 relay 拨 core 注册口的公共 argv(design §4.3):字面目标
|
|
182
212
|
* 127.0.0.1 不命中 Match 选路、跳板地址/用户仍读 ssh config 的 Host 块、
|
|
@@ -200,10 +230,7 @@ export function coreViaRelayArgs(topo, {
|
|
|
200
230
|
'-J', topo.relay.alias,
|
|
201
231
|
'-p', String(topo.core.relayPort),
|
|
202
232
|
'-o', `HostKeyAlias=${topo.core.alias}`,
|
|
203
|
-
|
|
204
|
-
'-o', `ConnectTimeout=${connectTimeout}`,
|
|
205
|
-
'-o', 'ControlMaster=no', '-o', 'ControlPath=none',
|
|
206
|
-
'-o', 'IdentitiesOnly=yes',
|
|
233
|
+
...probeSshOpts(connectTimeout),
|
|
207
234
|
];
|
|
208
235
|
if (acceptHost) args.push('-o', 'StrictHostKeyChecking=accept-new');
|
|
209
236
|
args.push(...extraOpts);
|
|
@@ -240,21 +267,17 @@ export function renderEdgeLinkPlist(opts) {
|
|
|
240
267
|
'-o', 'ConnectTimeout=5', '-o', 'StrictHostKeyChecking=accept-new',
|
|
241
268
|
coreAlias,
|
|
242
269
|
];
|
|
243
|
-
return
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
<string>Background</string>
|
|
247
|
-
<key>KeepAlive</key>
|
|
248
|
-
<dict>
|
|
270
|
+
return renderPlist({
|
|
271
|
+
label, argv, logsDir, throttle: LINK_THROTTLE,
|
|
272
|
+
keepAlive: ` <dict>
|
|
249
273
|
<key>PathState</key>
|
|
250
274
|
<dict>
|
|
251
275
|
<key>${xmlEscape(relayOnFile)}</key>
|
|
252
276
|
<true/>
|
|
253
277
|
</dict>
|
|
254
278
|
</dict>
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
${logPaths({ label, logsDir })}${XML_TAIL}`;
|
|
279
|
+
`,
|
|
280
|
+
});
|
|
258
281
|
}
|
|
259
282
|
|
|
260
283
|
/**
|
|
@@ -265,17 +288,9 @@ export function renderRelayPlist(opts) {
|
|
|
265
288
|
const { binDir, relayAlias, relayPort, logsDir } = opts;
|
|
266
289
|
const label = relayLabel(relayAlias);
|
|
267
290
|
const argv = [`${binDir}/${relayLinkName(relayAlias)}`, String(relayPort), relayAlias];
|
|
268
|
-
return
|
|
269
|
-
<
|
|
270
|
-
|
|
271
|
-
<string>Background</string>
|
|
272
|
-
<key>RunAtLoad</key>
|
|
273
|
-
<true/>
|
|
274
|
-
<key>KeepAlive</key>
|
|
275
|
-
<true/>
|
|
276
|
-
<key>ThrottleInterval</key>
|
|
277
|
-
<integer>${THROTTLE}</integer>
|
|
278
|
-
${logPaths({ label, logsDir })}${XML_TAIL}`;
|
|
291
|
+
return renderPlist({
|
|
292
|
+
label, argv, logsDir, runAtLoad: true, keepAlive: ' <true/>\n',
|
|
293
|
+
});
|
|
279
294
|
}
|
|
280
295
|
|
|
281
296
|
/**
|
|
@@ -287,18 +302,14 @@ export function renderUplinkEdgePlist(opts) {
|
|
|
287
302
|
const { binDir, edgeAlias, user, logsDir } = opts;
|
|
288
303
|
const label = edgeLabel(edgeAlias);
|
|
289
304
|
const argv = [`${binDir}/${edgeLinkName(edgeAlias)}`, edgeAlias, user];
|
|
290
|
-
return
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
<string>Background</string>
|
|
294
|
-
<key>KeepAlive</key>
|
|
295
|
-
<dict>
|
|
305
|
+
return renderPlist({
|
|
306
|
+
label, argv, logsDir,
|
|
307
|
+
keepAlive: ` <dict>
|
|
296
308
|
<key>SuccessfulExit</key>
|
|
297
309
|
<false/>
|
|
298
310
|
</dict>
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
${logPaths({ label, logsDir })}${XML_TAIL}`;
|
|
311
|
+
`,
|
|
312
|
+
});
|
|
302
313
|
}
|
|
303
314
|
|
|
304
315
|
// ---- node 的 systemd unit(与 relay plist 同构,Restart=always)----
|
package/libexec/lib/runtime.mjs
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
import fs from 'node:fs';
|
|
11
11
|
import path from 'node:path';
|
|
12
12
|
import { packageScriptPath } from './paths.mjs';
|
|
13
|
-
import { readFileOrNull, writeFileAtomic } from './fsutil.mjs';
|
|
13
|
+
import { readFileOrNull, readTrimmedOrNull, writeFileAtomic } from './fsutil.mjs';
|
|
14
14
|
import pkg from '../../package.json' with { type: 'json' };
|
|
15
15
|
|
|
16
16
|
export const RUNTIME_VERSION = pkg.version;
|
|
@@ -158,10 +158,7 @@ export function auditLink(linkPath, entityName) {
|
|
|
158
158
|
* @returns {string | null}
|
|
159
159
|
*/
|
|
160
160
|
export function readRuntimeVersion(paths) {
|
|
161
|
-
|
|
162
|
-
if (v === null) return null;
|
|
163
|
-
const trimmed = v.trim();
|
|
164
|
-
return trimmed.length > 0 ? trimmed : null;
|
|
161
|
+
return readTrimmedOrNull(paths.runtimeVersionFile);
|
|
165
162
|
}
|
|
166
163
|
|
|
167
164
|
/**
|
package/libexec/lib/state.mjs
CHANGED
|
@@ -6,9 +6,11 @@
|
|
|
6
6
|
// 目录 0700、文件 0600,写 v6 走 tmp+rename 防报名写入与 uplink-edge 读到半截。
|
|
7
7
|
import fs from 'node:fs';
|
|
8
8
|
import path from 'node:path';
|
|
9
|
-
import {
|
|
9
|
+
import { readTrimmedOrNull, writeFileAtomic } from './fsutil.mjs';
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
// 开关名单的 mjs 侧单源(isOn/setOn 的合法参数域):start/stop 的目标解析
|
|
12
|
+
// 也消费它。文件名与 bash 侧 sn-route、dotfiles Match 互为契约(见 paths)
|
|
13
|
+
export const SWITCH_NAMES = ['relay', 'peer'];
|
|
12
14
|
|
|
13
15
|
/**
|
|
14
16
|
* @typedef {Object} StateStore
|
|
@@ -57,14 +59,8 @@ export function createStateStore({ stateDir, switchFiles, edgesDir }) {
|
|
|
57
59
|
|
|
58
60
|
removeOn(names) {
|
|
59
61
|
assertNames(names);
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
fs.unlinkSync(switchFiles[n]);
|
|
63
|
-
} catch (err) {
|
|
64
|
-
if (err && err.code === 'ENOENT') continue;
|
|
65
|
-
throw err;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
62
|
+
// force = 不存在静默(与全仓其余幂等删除同一写法)
|
|
63
|
+
for (const n of names) fs.rmSync(switchFiles[n], { force: true });
|
|
68
64
|
},
|
|
69
65
|
|
|
70
66
|
readSwitches() {
|
|
@@ -75,10 +71,7 @@ export function createStateStore({ stateDir, switchFiles, edgesDir }) {
|
|
|
75
71
|
},
|
|
76
72
|
|
|
77
73
|
readEdgeV6(alias) {
|
|
78
|
-
|
|
79
|
-
if (v6 === null) return null;
|
|
80
|
-
const trimmed = v6.trim();
|
|
81
|
-
return trimmed.length > 0 ? trimmed : null;
|
|
74
|
+
return readTrimmedOrNull(path.join(edgesDir, alias));
|
|
82
75
|
},
|
|
83
76
|
|
|
84
77
|
writeEdgeV6(alias, v6) {
|
|
@@ -88,12 +81,7 @@ export function createStateStore({ stateDir, switchFiles, edgesDir }) {
|
|
|
88
81
|
},
|
|
89
82
|
|
|
90
83
|
removeEdgeV6(alias) {
|
|
91
|
-
|
|
92
|
-
fs.unlinkSync(path.join(edgesDir, alias));
|
|
93
|
-
} catch (err) {
|
|
94
|
-
if (err && err.code === 'ENOENT') return;
|
|
95
|
-
throw err;
|
|
96
|
-
}
|
|
84
|
+
fs.rmSync(path.join(edgesDir, alias), { force: true });
|
|
97
85
|
},
|
|
98
86
|
};
|
|
99
87
|
}
|
package/libexec/lib/systemd.mjs
CHANGED
|
@@ -25,6 +25,29 @@ export function disableIfInstalled(runner, unit) {
|
|
|
25
25
|
throw new Error(`systemctl disable 失败:${r.stderr.trim()}`);
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
/**
|
|
29
|
+
* enable 先行再 start(start/restart 共用的加载序列,镜像 mac 侧
|
|
30
|
+
* ensureLoaded/kickstartEnabled 的地位):stop 停服时做了 disable,只 start
|
|
31
|
+
* 会让服务这次在跑、重启后依旧不起来(enable 幂等;unit 缺失报真错,
|
|
32
|
+
* 提示先 install,同 mac 侧 plist 检查)。
|
|
33
|
+
* @param {import('./exec.mjs').Runner} runner
|
|
34
|
+
* @param {string} unit
|
|
35
|
+
*/
|
|
36
|
+
export function enableStart(runner, unit) {
|
|
37
|
+
const e = enableUnit(runner, unit);
|
|
38
|
+
if (e.code !== 0) throw new Error(`systemctl enable 失败:${e.stderr.trim()}`);
|
|
39
|
+
const r = startUnit(runner, unit);
|
|
40
|
+
if (r.code !== 0) throw new Error(`systemctl start 失败:${r.stderr.trim()}`);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** enable 先行再 restart(同 enableStart 的理由:先清 stop 残留的 disable)。 */
|
|
44
|
+
export function enableRestart(runner, unit) {
|
|
45
|
+
const e = enableUnit(runner, unit);
|
|
46
|
+
if (e.code !== 0) throw new Error(`systemctl enable 失败:${e.stderr.trim()}`);
|
|
47
|
+
const r = restartUnit(runner, unit);
|
|
48
|
+
if (r.code !== 0) throw new Error(`systemctl restart 失败:${r.stderr.trim()}`);
|
|
49
|
+
}
|
|
50
|
+
|
|
28
51
|
/** @returns {string} active|inactive|failed|... */
|
|
29
52
|
export const isActive = (runner, unit) =>
|
|
30
53
|
sc(runner, ['is-active', unit]).stdout.trim();
|