ssh-net 26.10.45 → 26.10.46
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 +49 -3
- package/libexec/commands/doctor.mjs +3 -3
- package/libexec/commands/install.mjs +69 -80
- package/libexec/commands/register.mjs +34 -112
- package/libexec/commands/restart.mjs +1 -1
- package/libexec/commands/start.mjs +5 -7
- package/libexec/commands/status.mjs +155 -215
- package/libexec/commands/stop.mjs +8 -9
- package/libexec/commands/uninstall.mjs +1 -1
- package/libexec/commands/update.mjs +7 -10
- package/libexec/lib/conf.mjs +1 -6
- package/libexec/lib/constants.mjs +26 -0
- package/libexec/lib/identity.mjs +1 -1
- package/libexec/lib/ipv6.mjs +1 -1
- package/libexec/lib/jobctl.mjs +11 -16
- package/libexec/lib/launchd.mjs +5 -6
- package/libexec/lib/manage.mjs +1 -1
- package/libexec/lib/paths.mjs +20 -20
- package/libexec/lib/probe.mjs +7 -50
- package/libexec/lib/render.mjs +57 -29
- package/libexec/lib/runtime.mjs +11 -16
- package/libexec/lib/sshconfig.mjs +2 -2
- package/libexec/lib/state.mjs +50 -16
- package/libexec/lib/systemd.mjs +26 -16
- package/libexec/sn-probe +11 -17
- package/libexec/sn-route +15 -20
- package/libexec/ssh-net-link-core +6 -11
- package/libexec/ssh-net-uplink-edge +27 -50
- package/libexec/ssh-net-uplink-relay +5 -6
- package/package.json +1 -1
- package/libexec/lib/registration.mjs +0 -47
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,52 @@
|
|
|
7
7
|
GitHub Release(Actions 自动跑测试后 npm publish;跳号与 0.x 的完整
|
|
8
8
|
考据见 git 史)。
|
|
9
9
|
|
|
10
|
+
## [26.10.46] - 2026-10-13
|
|
11
|
+
|
|
12
|
+
### 收敛:删掉旧架构残留与低收益的一致性维护
|
|
13
|
+
|
|
14
|
+
**libexec 有改动,下次发版后各机跑 `ssh-net update` 刷新**
|
|
15
|
+
|
|
16
|
+
- core 的 `conf.d/edge-<别名>` 不再随休眠删除、报名失败不再回滚(`lib/registration.mjs`
|
|
17
|
+
整个移除;该片段随后改为 install 渲染,见下方「新增」)
|
|
18
|
+
- status 只报开关、线路就绪、安装产物一致性三类;去掉形态行、peer/relay 非法
|
|
19
|
+
组合、转发口孤儿、topology 残留 job、出口代理与「人在办公室忘关」等专项诊断。
|
|
20
|
+
转发口未就绪改为 link 线路行显示,不再进「需要处理」
|
|
21
|
+
- install 端口预检按 pid 相等认领 link job(link-core 是 exec ssh,job pid 即
|
|
22
|
+
持口者),删掉进程树遍历;stop 收 link job 改为单次 TERM + 5s 等待
|
|
23
|
+
- node 的 systemd unit 改为先在临时目录用同名文件 verify,通过才写入
|
|
24
|
+
/etc/systemd/system——坏 unit 不再留在系统目录
|
|
25
|
+
- 删除无人使用的 `coreViaRelayArgs` 的 `extraOpts` 参数
|
|
26
|
+
- 运行时脚本与控制面注释去掉改动编年史,失效的 design 章节引用一并修正
|
|
27
|
+
|
|
28
|
+
### 新增:在办公室时 `ssh m5` 自动走内网(core 与 node 都支持)
|
|
29
|
+
|
|
30
|
+
**需配套 dotfiles 的 `~/.ssh/rc` 新版(`dot apply ssh`),三角色都要 `ssh-net update`**
|
|
31
|
+
|
|
32
|
+
- edge 的 routes 给 core/node 的连接加 `SetEnv LC_SSH_NET_EDGE=<本机别名>`;
|
|
33
|
+
m4/n007/n061 的 rc 见来源是 10.x 且带此标识,就把来源地址登记为该 edge 的入口
|
|
34
|
+
- `conf.d/edge-<别名>` 改为 core/node install 按 topology 渲染(Host 规范名与短名、
|
|
35
|
+
User、`HostKeyAlias <规范名>`、`Include state/addr/<别名>`),地址拆进单独文件:
|
|
36
|
+
中继报名写公网 v6,内网直连登录写 10.x,谁后写听谁的。node 上也能 `ssh m5` 了
|
|
37
|
+
- HostKeyAlias 固定为规范名:旧内网 IP 被别的设备接手时 ssh 直接拒绝,不会连错
|
|
38
|
+
- 入口地址 8 小时没刷新就不再采用(按地址文件修改时间、连接发起时判定,文件不删):
|
|
39
|
+
`ssh m5` 立即报找不到主机,不再拨旧地址;入口另加 `ConnectTimeout 5`
|
|
40
|
+
- core/node 的 status 列出每台 edge 当前的 ssh 入口地址与新旧(几小时前 / 已过期),
|
|
41
|
+
并体检入口片段
|
|
42
|
+
|
|
43
|
+
### 打磨:连接门控少做功、status 入口标签判据单源
|
|
44
|
+
|
|
45
|
+
- edge 入口的过期门控从 `find … | grep -q .` 换成 `find … | read -r _`(read 是
|
|
46
|
+
bash/zsh/dash 内建):Match exec 每条连接都跑,少 fork 一个外部进程;更省的
|
|
47
|
+
`test -n $(find …)` 写法撞 Ubuntu OpenSSH 9.x 分词器被否,考据记进 notes
|
|
48
|
+
- routes 尾部全局 VS Code 打标块加 originalhost 否定列表排除 core/node:
|
|
49
|
+
连 core/node 时打标 shell 只跑一次,不再白跑第二遍
|
|
50
|
+
- core/node status 的「内网」标签只认 10/8,与 rc 的登记白名单同一判据(单源
|
|
51
|
+
`constants.LAN_ADDR_RE`,跨仓测试锚住);其余 IPv4 按字面标「IPv4」,不替
|
|
52
|
+
rc 的网段策略背书。link 未安装时 status 不再空跑一次 launchctl print
|
|
53
|
+
- 删掉无调用方的 `state.removeEdgeV6`;`readEdgeAddr`/`edgeAddrAgeMin` 合并为
|
|
54
|
+
`readEdgeEntry`,有效期判定只剩一处(与 ssh 门控同边界)
|
|
55
|
+
|
|
10
56
|
## [26.10.45] - 2026-10-11
|
|
11
57
|
|
|
12
58
|
### 文档降级为「只记为什么」,无运行时变化
|
|
@@ -22,7 +68,7 @@ GitHub Release(Actions 自动跑测试后 npm publish;跳号与 0.x 的完
|
|
|
22
68
|
实体拷贝旧 bin 形态(现为原名实体+软链)、microsocks 1080(现 1090)、
|
|
23
69
|
VNC 旧「渲染成 LAN IP」(现 loopback6 固定 [::1])、两处已失效的交叉引用
|
|
24
70
|
- notes.md 删掉自己明令不做的编年史(已完成迁移、缺陷代码已删除的复盘条目),
|
|
25
|
-
真机实测矩阵、被否方案、装机/排障步骤全部保留;补了
|
|
71
|
+
真机实测矩阵、被否方案、装机/排障步骤全部保留;补了 relay host key 预置步骤
|
|
26
72
|
- CLAUDE.md 的 argv 契约指针随 design §2.3 改写更新
|
|
27
73
|
|
|
28
74
|
## [26.10.44] - 2026-10-08
|
|
@@ -66,7 +112,7 @@ GitHub Release(Actions 自动跑测试后 npm publish;跳号与 0.x 的完
|
|
|
66
112
|
|
|
67
113
|
**修复过程中钉死的 first-wins 语义(逐 keyword 取首值,块命中不是整体
|
|
68
114
|
替换)**
|
|
69
|
-
- 判决块不设 ProxyJump 时,兜底块的 `ProxyJump
|
|
115
|
+
- 判决块不设 ProxyJump 时,兜底块的 `ProxyJump <relay>` 会成为该 keyword
|
|
70
116
|
的首值——直连路径被强加中继。`ProxyJump none` 占位挡住;判决块不
|
|
71
117
|
命中时占位随之失效、兜底块正常生效(ssh -G 实测三态)
|
|
72
118
|
- 教训进测试:渲染字符串快照测不出配置语义,render.test 新增 ssh -G
|
|
@@ -345,7 +391,7 @@ core 侧 uplink-edge 脚本在下次 edge 报名时自动对账,或在 core
|
|
|
345
391
|
### 修复:edge 端口预检按 link 形态收窄
|
|
346
392
|
|
|
347
393
|
- **默认零常驻形态不再预检转发口**:install 只有装 link 附加件时才绑
|
|
348
|
-
topology forwards 的端口;默认形态下他进程占用这些口(
|
|
394
|
+
topology forwards 的端口;默认形态下他进程占用这些口(edge 实案:
|
|
349
395
|
VS Code Remote 的自动端口转发在本机占着 1090/1080/5900)与本服务无关,
|
|
350
396
|
旧版预检却拒绝 install、挡住升级。`--with-link` 或 marker 在时预检照旧;
|
|
351
397
|
`[::1]:10022` 的 banner 检查不受影响(peer 按需路径仍由它保护)
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
// doctor
|
|
1
|
+
// doctor:人在机器前的「立即按需确保」——直接跑一次 sn-route:
|
|
2
2
|
// 它会 probe、经 relay 报名、等回拨(6s 预算),进度直接显示在终端。
|
|
3
3
|
// doctor 只负责关态分诊与结果转述。不新增状态、不踢常驻服务(默认形态没有
|
|
4
4
|
// 常驻)。仅 edge 有动作:core 的恢复靠 edge 报名,node 由 systemd 自动重启。
|
|
5
5
|
import { SN_ROUTE } from '../lib/manifest.mjs';
|
|
6
6
|
import { snRouteArgv } from '../lib/render.mjs';
|
|
7
7
|
|
|
8
|
-
// 返回 {text, issues}:cli 见 issues 非空即退出 1
|
|
9
|
-
//
|
|
8
|
+
// 返回 {text, issues}:cli 见 issues 非空即退出 1——doctor 退出 0=peer 直达,
|
|
9
|
+
// 非零=本次经中继,供脚本串接判定
|
|
10
10
|
export async function run(ctx, args = []) {
|
|
11
11
|
if (args.length)
|
|
12
12
|
throw new Error('用法:doctor 不带参数');
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
// install
|
|
2
|
-
// bootout
|
|
3
|
-
// uplink-relay、不预建
|
|
4
|
-
// enable --now。
|
|
1
|
+
// install:对账到 topology 描述的稳态(会把 stop 掉的服务重新拉起)。幂等:
|
|
2
|
+
// 运行时脚本内容相同不重写、plist/unit 字节相同不 bootout(不断连)。edge 只
|
|
3
|
+
// 注册 PathState job(RELAY_ON 不在不启动);core 装完即启 uplink-relay、不预建
|
|
4
|
+
// uplink-edge(首次报名才生成);node 写 unit 并 enable --now。
|
|
5
5
|
import fs from 'node:fs';
|
|
6
6
|
import os from 'node:os';
|
|
7
7
|
import path from 'node:path';
|
|
@@ -10,29 +10,25 @@ import {
|
|
|
10
10
|
CALLBACK_PORT, TERMINAL_NOTIFIER_PATHS, OSASCRIPT_PATH,
|
|
11
11
|
} from '../lib/constants.mjs';
|
|
12
12
|
import { syncPlist, removeJob } from '../lib/manage.mjs';
|
|
13
|
-
import { syncConfFile
|
|
13
|
+
import { syncConfFile } from '../lib/conf.mjs';
|
|
14
14
|
import { ensureRuntime } from '../lib/runtime.mjs';
|
|
15
15
|
import * as launchd from '../lib/launchd.mjs';
|
|
16
16
|
import * as systemd from '../lib/systemd.mjs';
|
|
17
17
|
import {
|
|
18
|
-
classifyLocalPort, lsofListen, v6LoopbackPids,
|
|
19
|
-
peerProbeOk,
|
|
18
|
+
classifyLocalPort, lsofListen, v6LoopbackPids, peerProbeOk,
|
|
20
19
|
} from '../lib/probe.mjs';
|
|
21
20
|
import { writeFileAtomic } from '../lib/fsutil.mjs';
|
|
22
21
|
import {
|
|
23
22
|
SN_PROBE, edgeRuntimePlan, relayRuntimePlan,
|
|
24
23
|
edgeCallbacksPlan, linkAddonPlan, mergePlans,
|
|
25
24
|
} from '../lib/manifest.mjs';
|
|
26
|
-
import { edgeFragmentContent, edgeFragmentShouldExist } from '../lib/registration.mjs';
|
|
27
25
|
import {
|
|
28
|
-
linkLabel,
|
|
29
|
-
|
|
30
|
-
edgeLabel,
|
|
31
|
-
renderEdgeLinkPlist, renderRelayPlist, renderRelayUnit, renderRoutes,
|
|
26
|
+
linkLabel, relayLabel, relayUnitName,
|
|
27
|
+
renderEdgeHost, renderEdgeLinkPlist, renderRelayPlist, renderRelayUnit, renderRoutes,
|
|
32
28
|
} from '../lib/render.mjs';
|
|
33
29
|
|
|
34
|
-
/** ensureRuntime 结果的人话摘要('' =
|
|
35
|
-
*
|
|
30
|
+
/** ensureRuntime 结果的人话摘要('' = 组件零变更;非零自带「本次更新:」前缀,
|
|
31
|
+
* 直接交给 notesPart),让用户看得见后台脚本刷没刷。 */
|
|
36
32
|
function compNote(results) {
|
|
37
33
|
const LABELS = {
|
|
38
34
|
created: '新拷脚本', updated: '更新脚本', repaired: '补执行权限',
|
|
@@ -44,8 +40,21 @@ function compNote(results) {
|
|
|
44
40
|
counts.set(s, (counts.get(s) ?? 0) + 1);
|
|
45
41
|
}
|
|
46
42
|
if (counts.size === 0) return '';
|
|
47
|
-
|
|
48
|
-
|
|
43
|
+
return `本次更新:${[...counts].map(([k, n]) => `${LABELS[k] ?? k} ${n} 个`).join('、')}`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** 安装结果文案:组件刷新/配置更新若干条 note 合并进一对括号(''=零附加信息)。 */
|
|
47
|
+
function notesPart(...notes) {
|
|
48
|
+
const parts = notes.filter(Boolean);
|
|
49
|
+
return parts.length ? `(${parts.join(';')})` : '';
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** core/node 安装结果同构:服务配置未变只报刷新项,变了报 relay 已开启。 */
|
|
53
|
+
function coreNodeInstallMsg(comp, entryChanged, unchanged, relayLine) {
|
|
54
|
+
const note = notesPart(comp, entryChanged ? 'edge 入口已更新' : '');
|
|
55
|
+
if (unchanged)
|
|
56
|
+
return note ? `已刷新${note};服务配置无需改动` : '已安装,无需改动';
|
|
57
|
+
return `安装完成${note},relay 已开启(${relayLine})`;
|
|
49
58
|
}
|
|
50
59
|
|
|
51
60
|
/** edge 端口预检(纯读 + banner 判定),冲突抛错并报持有者。转发口只在
|
|
@@ -54,15 +63,12 @@ function compNote(results) {
|
|
|
54
63
|
async function precheckEdgePorts(ctx, linkOn) {
|
|
55
64
|
|
|
56
65
|
const { runner, paths, uid, topo } = ctx;
|
|
57
|
-
// 进程树预检全程不变:有转发口时建一次复用,别每个口重跑一遍 ps
|
|
58
66
|
const forwardPorts = topo.forwards.map((f) => f.listenPort);
|
|
59
67
|
if (linkOn && forwardPorts.length > 0) {
|
|
60
|
-
// 只要 pid
|
|
61
|
-
// link job,这次 print 纯浪费(update 内置的无参 install 也走这里)
|
|
68
|
+
// 只要 pid 判持口者归属(link-core 是 exec ssh,job pid 即持口者)
|
|
62
69
|
const jobPid = launchd.printState(runner, uid, linkLabel(topo.core.alias)).pid;
|
|
63
|
-
const tree = processTree(runner);
|
|
64
70
|
for (const port of forwardPorts) {
|
|
65
|
-
const c = classifyLocalPort(runner, port, jobPid
|
|
71
|
+
const c = classifyLocalPort(runner, port, jobPid);
|
|
66
72
|
if (c.kind === 'foreign') {
|
|
67
73
|
throw new Error(`端口 ${port} 被别的程序占用(pid ${c.pids.join(',')}):\n${lsofListen(runner, port)}`);
|
|
68
74
|
}
|
|
@@ -70,8 +76,8 @@ async function precheckEdgePorts(ctx, linkOn) {
|
|
|
70
76
|
}
|
|
71
77
|
|
|
72
78
|
// 10022 持口者永远是本机 sshd,无法按 pid 溯源:读到 SSH banner 才认。
|
|
73
|
-
// 只盯 [::1]
|
|
74
|
-
//
|
|
79
|
+
// 只盯 [::1](本服务的监听面);127.0.0.1:10022 的持有者如 VS Code 本地
|
|
80
|
+
// 转发与本服务不同地址族、可共存,不判冲突。
|
|
75
81
|
// 一次 lsof 同时拿持有者 pid 与报错展示文本;首装时 bin/sn-probe 还没拷上,
|
|
76
82
|
// 探针缺失不判僵尸(install 随后就拷)
|
|
77
83
|
const listeners = lsofListen(runner, CALLBACK_PORT);
|
|
@@ -165,8 +171,8 @@ async function installEdge(ctx, args) {
|
|
|
165
171
|
const r = syncPlist(ctx, { label, plistPath, content });
|
|
166
172
|
if (r.lintError) throw new Error(`link plist 校验失败:${r.lintError}`);
|
|
167
173
|
} else {
|
|
168
|
-
//
|
|
169
|
-
//
|
|
174
|
+
// 默认零常驻:清掉加装过的 link 全部产物。plist 必须先 bootout,否则
|
|
175
|
+
// 删了实体后 launchd 每 ThrottleInterval 刷 ENOENT
|
|
170
176
|
removeJob(ctx, label, plistPath);
|
|
171
177
|
const addon = linkAddonPlan(topo);
|
|
172
178
|
for (const [name] of [...addon.entities, ...addon.links])
|
|
@@ -174,9 +180,8 @@ async function installEdge(ctx, args) {
|
|
|
174
180
|
}
|
|
175
181
|
|
|
176
182
|
// conf.d 片段(design §4.2):edge 是 routes 的唯一安装角色——routes 含
|
|
177
|
-
// 选路 Match
|
|
183
|
+
// 选路 Match 与尾部打标块(VS Code 标识 + 本机别名)
|
|
178
184
|
const routesChanged = syncConfFile(paths.routesFile, renderRoutes(topo, ctx.self.alias));
|
|
179
|
-
const fragMsg = routesChanged ? '(ssh 配置片段已更新)' : '';
|
|
180
185
|
const depNote = localNotifyDepNote();
|
|
181
186
|
const head = linkOn
|
|
182
187
|
? '安装完成(含 link 本地转发)'
|
|
@@ -185,17 +190,43 @@ async function installEdge(ctx, args) {
|
|
|
185
190
|
? (linkOn
|
|
186
191
|
? '已安装(link 本地转发已启用),无需改动'
|
|
187
192
|
: '已安装,无需改动(默认不驻留后台)')
|
|
188
|
-
: `${head}${comp}
|
|
193
|
+
: `${head}${notesPart(comp, routesChanged ? 'ssh 配置片段已更新' : '')}`;
|
|
189
194
|
return `${base}${depNote}`;
|
|
190
195
|
}
|
|
191
196
|
|
|
197
|
+
/**
|
|
198
|
+
* core/node:按 topology 渲染每台 edge 的入口片段 conf.d/edge-<别名>(内容只随
|
|
199
|
+
* topology 变,地址在 Include 的 state/addr 文件里),并删掉 topology 已没有的
|
|
200
|
+
* 旧片段——conf.d/edge-* 全由本函数管。
|
|
201
|
+
* @returns {boolean} 有无改动
|
|
202
|
+
*/
|
|
203
|
+
function syncEdgeEntries(ctx) {
|
|
204
|
+
const { topo, paths } = ctx;
|
|
205
|
+
let changed = false;
|
|
206
|
+
const keep = new Set();
|
|
207
|
+
for (const e of topo.edges) {
|
|
208
|
+
const file = paths.edgeHostFile(e.alias);
|
|
209
|
+
keep.add(path.basename(file));
|
|
210
|
+
if (syncConfFile(file, renderEdgeHost(e))) changed = true;
|
|
211
|
+
}
|
|
212
|
+
let existing = [];
|
|
213
|
+
try { existing = fs.readdirSync(paths.confDir); } catch { /* 目录不在即无旧片段 */ }
|
|
214
|
+
// 片段名公式唯一来源是 edgeHostFile(含 '' 即前缀),不抄 'edge-' 字面量
|
|
215
|
+
const edgePrefix = path.basename(paths.edgeHostFile(''));
|
|
216
|
+
for (const f of existing) {
|
|
217
|
+
if (f.startsWith(edgePrefix) && !keep.has(f)) {
|
|
218
|
+
fs.rmSync(path.join(paths.confDir, f), { force: true });
|
|
219
|
+
changed = true;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return changed;
|
|
223
|
+
}
|
|
224
|
+
|
|
192
225
|
function installCore(ctx) {
|
|
193
226
|
const { topo, paths } = ctx;
|
|
194
|
-
|
|
195
|
-
//
|
|
196
|
-
//
|
|
197
|
-
// 软链):否则 update 后 core status 要对每台未联系的 edge 报组件异常,
|
|
198
|
-
// 直到它下次报名才自愈(2026-09-30 补)
|
|
227
|
+
const entryChanged = syncEdgeEntries(ctx);
|
|
228
|
+
// 磁盘在册 edge 的回拨组件在 install 时一并对账:否则 update 后 core
|
|
229
|
+
// status 要对每台未联系的 edge 报组件异常,直到它下次报名才自愈
|
|
199
230
|
const edges = launchd.edgeAliasesOnDisk(paths.launchAgentsDir);
|
|
200
231
|
// relay 基础形态 + 磁盘在册 edge 的回拨组件(清单与报名/status 体检同源)
|
|
201
232
|
const plan = mergePlans(relayRuntimePlan(topo), edgeCallbacksPlan(edges));
|
|
@@ -210,18 +241,15 @@ function installCore(ctx) {
|
|
|
210
241
|
const r = syncPlist(ctx, { label, plistPath: paths.plistFile(label), content });
|
|
211
242
|
// lint 失败不能静默报「安装完成」:syncPlist 以返回值交错误(register 同判)
|
|
212
243
|
if (r.lintError) throw new Error(`relay plist 校验失败:${r.lintError}`);
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
? `后台脚本已刷新${comp};服务配置无需改动`
|
|
216
|
-
: '已安装,无需改动';
|
|
217
|
-
}
|
|
218
|
-
return `安装完成${comp},relay 已开启(${topo.relay.alias} 入口 ${topo.core.relayPort})`;
|
|
244
|
+
return coreNodeInstallMsg(comp, entryChanged, r.unchanged,
|
|
245
|
+
`${topo.relay.alias} 入口 ${topo.core.relayPort}`);
|
|
219
246
|
}
|
|
220
247
|
|
|
221
248
|
function installNode(ctx) {
|
|
222
249
|
const { topo, paths, runner } = ctx;
|
|
223
250
|
if (ctx.uid !== 0)
|
|
224
251
|
throw new Error('node 安装写 /etc/systemd/system 需要 root,请以 root 运行');
|
|
252
|
+
const entryChanged = syncEdgeEntries(ctx);
|
|
225
253
|
const plan = relayRuntimePlan(topo);
|
|
226
254
|
const comp = compNote(ensureRuntime(ctx, plan.entities, plan.links));
|
|
227
255
|
const unitName = relayUnitName(topo.relay.alias);
|
|
@@ -243,47 +271,8 @@ function installNode(ctx) {
|
|
|
243
271
|
home,
|
|
244
272
|
}),
|
|
245
273
|
});
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
? `后台脚本已刷新${comp};服务配置无需改动`
|
|
249
|
-
: '已安装,无需改动';
|
|
250
|
-
}
|
|
251
|
-
return `安装完成${comp},relay 已开启(${ctx.topo.relay.alias} 入口 ${ctx.self.relayPort})`;
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
/**
|
|
255
|
-
* core:把 conf.d/edge-* 严格对账成「在册 + job 已注册未休眠 + state 有
|
|
256
|
-
* v6」三者同时成立的视图——片段存在即「这个地址此刻有回拨 job 兜着」:
|
|
257
|
-
* - 三者齐备:从 state 纯渲染(缺失补、过期改、相同不动)。这是手删/磁盘
|
|
258
|
-
* 丢失的恢复通道——peer 稳态的 link 循环不报名,等不到 _register 自然补写
|
|
259
|
-
* - job 未注册(state 只有历史 v6,机器从这台 core 没建成过回拨)或处于
|
|
260
|
-
* 休眠(拨号失败收尸)或 state 无 v6:删片段——没有 job 兜底的入口是指向
|
|
261
|
-
* 陈旧 v6 的假入口,且永远等不到休眠收尸
|
|
262
|
-
* - topology 已删 edge(改名/退役):删片段;state 同名残留不动(既有
|
|
263
|
-
* 现象,uninstall 才整棵清)
|
|
264
|
-
*/
|
|
265
|
-
function reconcileEdgeFragments(ctx) {
|
|
266
|
-
const { topo, paths } = ctx;
|
|
267
|
-
let files = [];
|
|
268
|
-
try {
|
|
269
|
-
files = fs.readdirSync(paths.confDir).filter((f) => f.startsWith('edge-'));
|
|
270
|
-
} catch { files = []; }
|
|
271
|
-
|
|
272
|
-
// disabled 字典是整域同一份:循环外取一次,各 edge 的 inspect 共用
|
|
273
|
-
const dmap = launchd.disabledMap(ctx.runner, ctx.uid);
|
|
274
|
-
for (const e of topo.edges) {
|
|
275
|
-
const file = paths.edgeHostFile(e.alias);
|
|
276
|
-
const st = launchd.inspectWith(ctx.runner, ctx.uid, edgeLabel(e.alias), dmap);
|
|
277
|
-
const v6 = ctx.state.readEdgeV6(e.alias);
|
|
278
|
-
if (edgeFragmentShouldExist(st, v6))
|
|
279
|
-
syncConfFile(file, edgeFragmentContent(e, v6));
|
|
280
|
-
else
|
|
281
|
-
removeConfFile(file);
|
|
282
|
-
files = files.filter((f) => f !== `edge-${e.alias}`);
|
|
283
|
-
}
|
|
284
|
-
// 剩下的都是 topology 不认识的孤儿
|
|
285
|
-
for (const f of files)
|
|
286
|
-
removeConfFile(path.join(paths.confDir, f));
|
|
274
|
+
return coreNodeInstallMsg(comp, entryChanged, r === 'unchanged',
|
|
275
|
+
`${ctx.topo.relay.alias} 入口 ${ctx.self.relayPort}`);
|
|
287
276
|
}
|
|
288
277
|
|
|
289
278
|
/**
|
|
@@ -1,51 +1,39 @@
|
|
|
1
|
-
// 报名:edge 侧发起(
|
|
2
|
-
//
|
|
1
|
+
// 报名:edge 侧发起(enablePeer,供 `start peer` 使用;sn-route 现场报名走
|
|
2
|
+
// bash 同形态 argv)与 core 侧接收端内部命令 `_register`(下划线前缀不进 help)。
|
|
3
3
|
//
|
|
4
|
-
// edge 侧:取本机 v6,经 relay 发一条 ssh 调 core 的 _register
|
|
5
|
-
//
|
|
4
|
+
// edge 侧:取本机 v6,经 relay 发一条 ssh 调 core 的 _register。没有常驻循环:
|
|
5
|
+
// 报名失败后由下次发起连接时 sn-route 再报,或重跑 start peer。
|
|
6
6
|
//
|
|
7
|
-
// core
|
|
8
|
-
//
|
|
9
|
-
//
|
|
10
|
-
//
|
|
11
|
-
// 先对账运行时脚本与 plist——uplink-edge 的脚本与 job 参数(user 等)变更
|
|
12
|
-
// 只有这条更新通道,缺了会永远用旧参数/旧目标拨号、拨号失败后休眠。
|
|
13
|
-
// launchctl 真返回成功才结束;失败 edge 下次连接自然重试。
|
|
7
|
+
// core 侧:报名只会由活 edge 上的真实连接触发(sn-route 现场、start peer),
|
|
8
|
+
// 所以任何报名都带「人正在等连接」语义——同 v6 也无条件 enable+kickstart,
|
|
9
|
+
// 回拨立即重拨、重试额度随进程重启重置。先对账运行时脚本与 plist:uplink-edge
|
|
10
|
+
// 的脚本与 job 参数(user 等)只有这条更新通道。
|
|
14
11
|
import { explainV6Error } from '../lib/ipv6.mjs';
|
|
15
12
|
import * as launchd from '../lib/launchd.mjs';
|
|
16
13
|
import { syncPlist } from '../lib/manage.mjs';
|
|
17
|
-
import { syncConfFile, removeConfFile } from '../lib/conf.mjs';
|
|
18
14
|
import { ensureRuntime } from '../lib/runtime.mjs';
|
|
19
15
|
import { readFileOrNull } from '../lib/fsutil.mjs';
|
|
20
16
|
import { classifyV6Probe } from '../lib/probe.mjs';
|
|
21
|
-
import {
|
|
22
|
-
writeEdgeRegistration, removeEdgeRegistration,
|
|
23
|
-
edgeFragmentContent, edgeFragmentShouldExist,
|
|
24
|
-
} from '../lib/registration.mjs';
|
|
25
17
|
import { SN_IPV6, edgeCallbacksPlan } from '../lib/manifest.mjs';
|
|
26
18
|
import {
|
|
27
19
|
edgeLabel, renderUplinkEdgePlist, coreViaRelayArgs,
|
|
28
20
|
} from '../lib/render.mjs';
|
|
29
21
|
|
|
30
|
-
/** edge 侧报名:取 v6 + 命令行 -J 经 relay 调 core 的 _register。
|
|
31
|
-
* start peer 用——控制面直接报一次名让 core 马上回拨。 */
|
|
22
|
+
/** edge 侧报名:取 v6 + 命令行 -J 经 relay 调 core 的 _register。 */
|
|
32
23
|
function runEdgeAnnounce(ctx) {
|
|
33
24
|
const probe = ctx.runner.runRaw(
|
|
34
25
|
ctx.paths.binScript(SN_IPV6), [], { timeout: 5000 },
|
|
35
26
|
);
|
|
36
27
|
// bin/sn-ipv6 缺失(未 install)别误报成「无 IPv6」。fatal 哨兵属性:
|
|
37
|
-
// enablePeer
|
|
38
|
-
// 不拿 stderr 文本做字符串匹配(远端报错文本不可控)
|
|
28
|
+
// enablePeer 靠它区分致命错与可重试的报名失败,不拿 stderr 文本做匹配
|
|
39
29
|
if (probe.error && probe.error.code === 'ENOENT') {
|
|
40
30
|
const e = new Error('未安装(缺运行时脚本)——先跑 ssh-net install');
|
|
41
31
|
e.fatal = true;
|
|
42
32
|
throw e;
|
|
43
33
|
}
|
|
44
|
-
//
|
|
45
|
-
// 结果是两回事,failed 绝不能谎称「网络没有 IPv6」
|
|
34
|
+
// 「探测没跑成」与「网络没有 IPv6」是两回事,failed 绝不能谎称没有 v6
|
|
46
35
|
const { state, v6 } = classifyV6Probe(probe);
|
|
47
36
|
if (state === 'failed') {
|
|
48
|
-
// code null = 被信号杀(spawnSync 此时 error 为 null);ENOENT 已在上面拦走
|
|
49
37
|
const why = probe.error?.message
|
|
50
38
|
?? (probe.signal !== null && probe.signal !== undefined
|
|
51
39
|
? `signal ${probe.signal}` : `exit ${probe.code}`);
|
|
@@ -53,16 +41,12 @@ function runEdgeAnnounce(ctx) {
|
|
|
53
41
|
}
|
|
54
42
|
if (state === 'none')
|
|
55
43
|
throw new Error('当前网络没有可用 IPv6,没法告知 core 来连(中继不受影响)');
|
|
56
|
-
//
|
|
57
|
-
//
|
|
58
|
-
// 报清晰错误,而不是拼出一条让 core 侧 shell 莫名解析失败的命令
|
|
44
|
+
// 拼进远端命令串前本地校验:输出被污染(多行/元字符)时报清晰错误,
|
|
45
|
+
// 而不是拼出一条让 core 侧 shell 莫名解析失败的命令
|
|
59
46
|
const v6Err = explainV6Error(v6);
|
|
60
47
|
if (v6Err) throw new Error(`本机 v6 选取异常(sn-ipv6 输出不是可用地址:${v6Err})`);
|
|
61
|
-
// 报名固定走 relay
|
|
62
|
-
//
|
|
63
|
-
// 5,design §3 冻结):无人值守首连信任 accept-new(探测侧 status 不带它,
|
|
64
|
-
// 差异在 coreViaRelayArgs 显式)+ IdentitiesOnly 防多 key 试错撞 MaxAuthTries
|
|
65
|
-
//(design §4.3)。挂死由外层 60s timeout 兜底
|
|
48
|
+
// 报名固定走 relay:peer 僵尸时自动选路会把报名一起挂死。无人值守首连
|
|
49
|
+
// 信任 accept-new(status 探测不带,见 design §4.3)。挂死由 60s timeout 兜底
|
|
66
50
|
const r = ctx.runner.run('ssh', coreViaRelayArgs(ctx.topo, {
|
|
67
51
|
acceptHost: true,
|
|
68
52
|
remoteCommand: `ssh-net _register ${ctx.self.alias} ${v6}`,
|
|
@@ -73,52 +57,16 @@ function runEdgeAnnounce(ctx) {
|
|
|
73
57
|
return `已通知 core(本机地址 ${v6};core 回应:${core}),它几秒内会主动连过来建立直连;ssh-net status 查看结果`;
|
|
74
58
|
}
|
|
75
59
|
|
|
76
|
-
/**
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
* 真实 job 态重判:休眠中的 edge(registered+disabled)唤醒失败,旧 v6
|
|
80
|
-
* 没有未休眠 job 兜底,重写 conf.d 片段就是注册不变式禁止的假入口
|
|
81
|
-
* (2026-10-03:旧实现无条件重写,hibernate 唤醒失败会留假入口) */
|
|
82
|
-
function rollbackEdgeRegistration(ctx, edge, prevV6, label) {
|
|
83
|
-
if (prevV6 === null) {
|
|
84
|
-
removeEdgeRegistration(ctx, edge.alias);
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
ctx.state.writeEdgeV6(edge.alias, prevV6);
|
|
88
|
-
const st = launchd.inspect(ctx.runner, ctx.uid, label);
|
|
89
|
-
const file = ctx.paths.edgeHostFile(edge.alias);
|
|
90
|
-
if (edgeFragmentShouldExist(st, prevV6))
|
|
91
|
-
syncConfFile(file, edgeFragmentContent(edge, prevV6));
|
|
92
|
-
else
|
|
93
|
-
removeConfFile(file);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/** 未注册/重部署分支的部署失败回滚:state 恢复旧 v6(无旧值则两处同删,
|
|
97
|
-
* 与 rollbackEdgeRegistration 的 null 分支同一原语)、片段删除——没有
|
|
98
|
-
* job 兜底的片段就是假入口。回滚失败不遮蔽原始错误。 */
|
|
99
|
-
function rollbackRegistration(ctx, edge, prevV6) {
|
|
100
|
-
try {
|
|
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
|
-
}
|
|
106
|
-
} catch { /* 回滚失败不遮蔽原始错误 */ }
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/** start peer 的实现:控制面立即报名 + 置 relay+peer 开关。返回给人看的
|
|
110
|
-
* 文案(带上报名详情:v6 与 core 的响应)。先报名、成功后才落开关——
|
|
111
|
-
* 未安装(fatal)零副作用;可重试的报名失败(relay 不通、当场无 v6)
|
|
112
|
-
* 不阻断用户意图,开关照置:下次发起连接时 sn-route 会再报,也可再跑
|
|
113
|
-
* 一次 ssh-net start peer。
|
|
60
|
+
/** start peer 的实现:立即报名 + 置 relay+peer 开关。未安装(fatal)零副作用;
|
|
61
|
+
* 可重试的报名失败(relay 不通、当场无 v6)不阻断用户意图,开关照置——下次
|
|
62
|
+
* 发起连接时 sn-route 会再报。
|
|
114
63
|
* @param {object} ctx
|
|
115
|
-
* @param {boolean} [alreadyOn]
|
|
116
|
-
* start 是幂等确认)——由 start 在调用前传,文案选词不留给调用方替换字符串 */
|
|
64
|
+
* @param {boolean} [alreadyOn] 开关此前已全开:措辞用「已是开启状态」 */
|
|
117
65
|
export function enablePeer(ctx, alreadyOn = false) {
|
|
118
66
|
const prefix = alreadyOn ? '隧道已是开启状态' : 'peer 已开启';
|
|
119
67
|
try {
|
|
120
68
|
const detail = runEdgeAnnounce(ctx);
|
|
121
|
-
ctx.state.setOn(['relay', 'peer']); // relay 是根:peer
|
|
69
|
+
ctx.state.setOn(['relay', 'peer']); // relay 是根:peer 依赖它
|
|
122
70
|
return `${prefix}(relay 一并开启;${detail})`;
|
|
123
71
|
} catch (err) {
|
|
124
72
|
if (err && err.fatal) throw err;
|
|
@@ -128,8 +76,11 @@ export function enablePeer(ctx, alreadyOn = false) {
|
|
|
128
76
|
}
|
|
129
77
|
|
|
130
78
|
/**
|
|
131
|
-
* 内部 RPC(core):`ssh-net _register <edge 别名> <v6
|
|
132
|
-
*
|
|
79
|
+
* 内部 RPC(core):`ssh-net _register <edge 别名> <v6>`。
|
|
80
|
+
* 报名即两处同写:state/edges/<别名>(uplink-edge 每次拨号现读)与
|
|
81
|
+
* state/addr/<别名>(`ssh <别名>` 入口 Include 的地址行;内网直连时 dotfiles 的
|
|
82
|
+
* ~/.ssh/rc 也写它,谁后写听谁的)。入口片段本身由 install 按 topology 渲染。
|
|
83
|
+
* 部署/唤醒失败不回滚:写下的就是 edge 刚上报的真实地址,下次报名照样重试。
|
|
133
84
|
* @param {object} ctx
|
|
134
85
|
* @param {string[]} args [别名, v6]
|
|
135
86
|
* @returns {string} 给 edge 的简短结果
|
|
@@ -152,54 +103,25 @@ export function runRegister(ctx, args = []) {
|
|
|
152
103
|
binDir: ctx.paths.binDir, edgeAlias: alias, user: edge.user,
|
|
153
104
|
logsDir: ctx.paths.logsDir,
|
|
154
105
|
});
|
|
155
|
-
// 未注册(从未落盘)或被手删 → null
|
|
156
|
-
const existing = readFileOrNull(plistPath);
|
|
157
|
-
const prevV6 = ctx.state.readEdgeV6(alias);
|
|
158
106
|
const st = launchd.inspect(ctx.runner, ctx.uid, label);
|
|
159
107
|
|
|
160
|
-
// 脚本对账无条件先行(幂等):脚本被删/过期/参数变更时,四个状态分支
|
|
161
|
-
// 里的任何一个都可能成为重建通道,不能只靠首次报名那一次
|
|
162
108
|
const plan = edgeCallbacksPlan([alias]);
|
|
163
109
|
ensureRuntime(ctx, plan.entities, plan.links);
|
|
110
|
+
ctx.state.writeEdgeV6(alias, v6);
|
|
111
|
+
ctx.state.writeEdgeAddr(alias, v6);
|
|
164
112
|
|
|
165
|
-
//
|
|
166
|
-
//
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
// (而非重写)——未注册态的片段按 reconcile 不变式本就不该在(休眠/会话
|
|
171
|
-
// 重建时已被 retire 清掉),内容变化路径 syncPlist 先 bootout、失败时 job
|
|
172
|
-
// 已不在册,重写同样造出假入口。下轮报名自愈;回滚失败不遮蔽原始错误
|
|
173
|
-
if (!st.registered || existing !== content) {
|
|
174
|
-
writeEdgeRegistration(ctx, edge, v6);
|
|
175
|
-
try {
|
|
176
|
-
const r = syncPlist(ctx, { label, plistPath, content });
|
|
177
|
-
if (r.lintError) throw new Error(`uplink-edge plist 校验失败:${r.lintError}`);
|
|
178
|
-
} catch (err) {
|
|
179
|
-
rollbackRegistration(ctx, edge, prevV6);
|
|
180
|
-
throw err;
|
|
181
|
-
}
|
|
113
|
+
// 未注册(首次报名)或 plist 与 topology 渲染不符(user 等变了):重新部署,
|
|
114
|
+
// syncPlist 的 bootstrap 即首发
|
|
115
|
+
if (!st.registered || readFileOrNull(plistPath) !== content) {
|
|
116
|
+
const r = syncPlist(ctx, { label, plistPath, content });
|
|
117
|
+
if (r.lintError) throw new Error(`uplink-edge plist 校验失败:${r.lintError}`);
|
|
182
118
|
return st.registered ? `已更新登记 ${alias}` : `已登记 ${alias}`;
|
|
183
119
|
}
|
|
184
120
|
|
|
185
|
-
//
|
|
186
|
-
// not-running 三种形态合并):写 v6 → enable(休眠唤醒要清 override;
|
|
187
|
-
// enabled 幂等无害)→ kickstart。报名只会由活 edge 上的真实 ssh 触发,
|
|
188
|
-
// 无条件重拨正是人等连接时要的语义,进程重启即重置重试额度(首拨 +
|
|
189
|
-
// 一次重试两次机会重新计)。enable
|
|
190
|
-
// 失败不用回滚(disabled 态没变,下轮报名照走本分支)。两处同写先于
|
|
191
|
-
// kickstart:顺带 reconcile conf.d 片段(uplink-edge 拨号失败后休眠删片段与
|
|
192
|
-
// 报名存在删/写竞态,缺则补)。kickstart 失败回滚 state 到 job 实际在用
|
|
193
|
-
// 的 v6:不回滚则磁盘已是新值,job 仍拿旧 v6 拨到失败休眠
|
|
194
|
-
writeEdgeRegistration(ctx, edge, v6);
|
|
121
|
+
// 已注册:休眠中要先 enable 清 override,enabled 时幂等;kickstart -k 杀旧起新
|
|
195
122
|
const en = launchd.enable(ctx.runner, ctx.uid, label);
|
|
196
123
|
if (en.code !== 0) throw new Error(`enable 失败:${en.stderr.trim()}`);
|
|
197
124
|
const r = launchd.kickstart(ctx.runner, ctx.uid, label);
|
|
198
|
-
if (r.code !== 0) {
|
|
199
|
-
try {
|
|
200
|
-
rollbackEdgeRegistration(ctx, edge, prevV6, label);
|
|
201
|
-
} catch { /* 回滚失败不遮蔽原始错误 */ }
|
|
202
|
-
throw new Error(`kickstart 失败:${r.stderr.trim()}`);
|
|
203
|
-
}
|
|
125
|
+
if (r.code !== 0) throw new Error(`kickstart 失败:${r.stderr.trim()}`);
|
|
204
126
|
return st.disabled ? `已唤醒 ${alias}` : `已开始拨号 ${alias}`;
|
|
205
127
|
}
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
// start
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
// 状态」。
|
|
1
|
+
// start:edge 两开关(缺省或 -a/--all=relay+peer,peer 自动补 relay + 立即
|
|
2
|
+
// 报名);core 重新 enable+bootstrap uplink-relay;node systemctl。link 不是
|
|
3
|
+
// 运行时开关(转发口只取决于装没装附加件)。重复 start 是幂等确认:动作照做
|
|
4
|
+
// (重新报名让恢复立即发生),措辞区分「已开启」与「已是开启状态」。
|
|
6
5
|
import fs from 'node:fs';
|
|
7
6
|
import * as systemd from '../lib/systemd.mjs';
|
|
8
7
|
import { ensureLoaded } from '../lib/manage.mjs';
|
|
@@ -40,8 +39,7 @@ export function run(ctx, args = []) {
|
|
|
40
39
|
const targets = parseTargets(args); // ['relay','peer'] 或 ['relay'];link 抛错
|
|
41
40
|
|
|
42
41
|
if (targets.includes('peer')) {
|
|
43
|
-
// 默认/-a/peer:enablePeer 置两开关(幂等)+
|
|
44
|
-
// 全开时措辞由 enablePeer 内部一处选词
|
|
42
|
+
// 默认/-a/peer:enablePeer 置两开关(幂等)+ 立即报名
|
|
45
43
|
return enablePeer(ctx, before.relay && before.peer);
|
|
46
44
|
}
|
|
47
45
|
ctx.state.setOn(['relay']);
|