ssh-net 26.10.22 → 26.10.24
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 +66 -0
- package/README.md +3 -1
- package/libexec/cli.mjs +31 -8
- package/libexec/commands/checkin.mjs +53 -15
- package/libexec/commands/install.mjs +60 -8
- package/libexec/commands/status.mjs +56 -11
- package/libexec/commands/uninstall.mjs +3 -0
- package/libexec/lib/conf.mjs +44 -0
- package/libexec/lib/config.mjs +134 -66
- package/libexec/lib/identity.mjs +0 -1
- package/libexec/lib/paths.mjs +20 -1
- package/libexec/lib/render.mjs +96 -5
- package/libexec/lib/sshconfig.mjs +73 -0
- package/libexec/ssh-net-link-core +61 -16
- package/libexec/ssh-net-uplink-edge +11 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -9,6 +9,72 @@
|
|
|
9
9
|
故 26.9.12 之后跳号发 26.9.17(第 17 个包),13–16 永久空缺——序号从此与
|
|
10
10
|
第几个包严格一致,看到版本号即知发布总数
|
|
11
11
|
|
|
12
|
+
## [26.10.24] - 2026-10-01
|
|
13
|
+
|
|
14
|
+
### 变更(topology 正名去 IP;Match 收归 install 渲染;edge 在线入口)
|
|
15
|
+
|
|
16
|
+
- **topology 文件改名 `~/.ssh/ssh-net/config` → `topology`,删掉 LAN_IP
|
|
17
|
+
列**:从此只管名称(角色/主机名/别名/relay 口/用户),静态 IP 唯一来源
|
|
18
|
+
是 `~/.ssh/config` 的 Host 块。host 行 7 列变 6 列
|
|
19
|
+
(`host core mac-mini-m4 m4 12022 aex`);install 校验名称↔地址契约
|
|
20
|
+
(relay/core/node 必须有 Host 块和 HostName,edge 豁免)
|
|
21
|
+
- **Match 选路块不再手写在 ~/.ssh/config**:`ssh-net install` 从 topology
|
|
22
|
+
渲染成 `~/.ssh/ssh-net/conf.d/routes`(edge 专属;值全来自 topology,
|
|
23
|
+
不含真实 IP),主 config 顶部加一行 `Include
|
|
24
|
+
~/.ssh/ssh-net/conf.d/*` 接入。旧 `ON` 兼容判据同时收紧为只认 RELAY_ON
|
|
25
|
+
- **`m4-via-relay` 内部别名删除**:报名/status 实测 ssh 改命令行显式
|
|
26
|
+
`ssh -J s100 -p 12022 -o HostKeyAlias=m4 aex@127.0.0.1`(字面环回目标
|
|
27
|
+
不命中 Match,绕开自动选路;跳板仍读 Host s100 块)。link-core argv
|
|
28
|
+
相应变为 `<self> <peer> <relay别名> <relay端口> <peer用户> <转发规格…>`
|
|
29
|
+
- **core 新增 edge 在线入口**:edge 报名即写 `conf.d/edge-<别名>`,m4 上
|
|
30
|
+
直接 `ssh mb5` 经公网 v6 一跳连上笔记本;六连败休眠/state 缺失/uninstall
|
|
31
|
+
自动删除;v4-only 网络不报名、无入口(不提供经 relay 回连)
|
|
32
|
+
- **别名列支持附加 ssh 短名**:topology 写 `mb5,m5`——规范名 mb5 仍是
|
|
33
|
+
ssh-net 内部唯一名字(文件/state/job/Match),短名 m5 只进 core 入口
|
|
34
|
+
的 Host 模式,`ssh mb5` 与 `ssh m5` 等价;mb1 同理配 m1
|
|
35
|
+
- **edge 入口片段可由 install 重建**:core install 把 conf.d/edge-* 对账
|
|
36
|
+
成 state+topology 的视图(peer 稳态循环不报名,手删片段原本等不到自然
|
|
37
|
+
恢复);无 state 支撑的假入口与 topology 已删 edge 的残留一并清。
|
|
38
|
+
status 新增片段体检:edge 的 routes、core 各在线 edge 的入口片段缺失/
|
|
39
|
+
过期都报「跑 install 修复」(休眠态删片段是正确行为,不报)
|
|
40
|
+
|
|
41
|
+
**迁移窗口内置旧文件兜底**:`topology` 缺席时新包仍读旧名 `config`(7 列
|
|
42
|
+
含 LAN_IP 也认,IP 列忽略)——因为 `dot apply ssh` 按整份 md 还原,
|
|
43
|
+
topology 与 Include 版 config 必然同时落地,无法分阶段。
|
|
44
|
+
|
|
45
|
+
顺序是硬约束(详见 docs/migration.md 2026-10-01 段):
|
|
46
|
+
1. 五台全部先 `npm i -g ssh-net@26.10.24 && ssh-net install`(吃旧文件,
|
|
47
|
+
routes 落盘但无 Include 不生效;install 提示「仍读旧文件兜底」属正常)
|
|
48
|
+
2. dotfiles push,各机 `dot apply ssh`(topology 与 Include 版 config
|
|
49
|
+
同时生效,兜底提示消失)
|
|
50
|
+
3. edge `ssh-net checkin`,m4 上验证 `ssh mb5` / `ssh m5`
|
|
51
|
+
4. 全部正常后手动删旧文件 `~/.ssh/ssh-net/config`
|
|
52
|
+
|
|
53
|
+
另:core install 会清理 topology 已删 edge 的残留 conf.d 片段。
|
|
54
|
+
|
|
55
|
+
## [26.10.23] - 2026-10-01
|
|
56
|
+
|
|
57
|
+
### 变更(转发规格化;loopback6 目标按 peer/relay 选 v6/v4 环回)
|
|
58
|
+
|
|
59
|
+
- topology 新增环回目标别名 **`loopback6`**:`forward local 15900
|
|
60
|
+
loopback6 5900`。与固定 v4 的 `loopback` 不同,它由 link-core 按当前
|
|
61
|
+
mode 渲染——**peer 态远程目标 `[::1]:5900`、relay 态自动退
|
|
62
|
+
`127.0.0.1:5900`**(ps 看 link 的 ssh `-L` 即可辨别当前路径;两态都
|
|
63
|
+
落地 core,要求 core 服务两族环回都听——macOS 屏幕共享实测都听)。
|
|
64
|
+
1090 microsocks 只听 v4,继续用 `loopback`
|
|
65
|
+
- plist 传给 link-core 的转发参数从「裸 ssh 片段」改为**转发规格**
|
|
66
|
+
`D:<本地口>` / `L:<本地口>:<目标口>:<目标>`(buildForwardArgs →
|
|
67
|
+
buildForwardSpecs),由 link-core 在 mode 确定后渲染;peer↔relay
|
|
68
|
+
翻转杀 ssh 重连时目标地址随 mode 切换,无需额外动作
|
|
69
|
+
|
|
70
|
+
**需 dotfiles 同步发布(15900 改 loopback6),edge 顺序不能乱**:
|
|
71
|
+
1. `npm i -g ssh-net@26.10.23`(先装能解析 loopback6、能读规格的包)
|
|
72
|
+
2. `dot apply ssh`(落新 topology)
|
|
73
|
+
3. `ssh-net install`(渲染规格 plist 并重建隧道)
|
|
74
|
+
|
|
75
|
+
顺序反了:旧包解析 `loopback6` 会按「未知别名」报配置错;旧 link 收到
|
|
76
|
+
规格当裸 ssh 参数会起不来。core/node 无 forward 行,升包无行为变化。
|
|
77
|
+
|
|
12
78
|
## [26.10.22] - 2026-10-01
|
|
13
79
|
|
|
14
80
|
### 变更(选路判决集中到 link-core;修 sn-probe 健康路径白等 2s)
|
package/README.md
CHANGED
|
@@ -31,7 +31,9 @@
|
|
|
31
31
|
fnm 切版本不影响常驻服务;包升级 `npm i -g` 后重跑 install 刷新,
|
|
32
32
|
runtime-version 戳由 status 对账
|
|
33
33
|
- **npm 包分发**:消费机 `npm i -g ssh-net`,不 clone;个人配置全部在 dotfiles
|
|
34
|
-
托管(`~/.ssh/ssh-net/
|
|
34
|
+
托管(`~/.ssh/ssh-net/topology` 与 ssh config 的静态 Host 块),包内零个人数据;
|
|
35
|
+
选路 Match 与 core 上的 edge 入口由 install/报名渲染进
|
|
36
|
+
`~/.ssh/ssh-net/conf.d/`(生成物,ssh config 一行 Include 接入)
|
|
35
37
|
|
|
36
38
|
## edge 的三个开关
|
|
37
39
|
|
package/libexec/cli.mjs
CHANGED
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
// 本文件只做装配、帮助与错误边界。
|
|
4
4
|
import fs from 'node:fs';
|
|
5
5
|
import os from 'node:os';
|
|
6
|
-
import { parseTopology } from './lib/config.mjs';
|
|
6
|
+
import { parseTopology, validateHostContract } from './lib/config.mjs';
|
|
7
|
+
import { parseStaticHosts } from './lib/sshconfig.mjs';
|
|
7
8
|
import { localHostname, matchIdentity } from './lib/identity.mjs';
|
|
8
9
|
import { machinePaths } from './lib/paths.mjs';
|
|
9
10
|
import { createStateStore } from './lib/state.mjs';
|
|
@@ -41,24 +42,44 @@ function help() {
|
|
|
41
42
|
.filter(([, c]) => !c.internal)
|
|
42
43
|
.map(([name, c]) => ` ${name.padEnd(10)} ${c.desc}`),
|
|
43
44
|
'',
|
|
44
|
-
'配置: ~/.ssh/ssh-net/
|
|
45
|
+
'配置: ~/.ssh/ssh-net/topology(dotfiles 托管);运行时/状态: ~/.ssh/ssh-net/{bin,state,conf.d}',
|
|
45
46
|
];
|
|
46
47
|
return lines.join('\n');
|
|
47
48
|
}
|
|
48
49
|
|
|
49
|
-
/** 装配本机上下文;config 缺失或主机名不匹配在此硬失败。 */
|
|
50
|
+
/** 装配本机上下文;topology/ssh config 缺失或主机名不匹配在此硬失败。 */
|
|
50
51
|
function buildContext() {
|
|
51
52
|
const env = process.env;
|
|
52
53
|
const paths = machinePaths(env);
|
|
54
|
+
// 新名 topology 优先;缺席时兜底读旧名 config(2026-10-01 改名的迁移窗口,
|
|
55
|
+
// 旧文件 7 列含 LAN_IP——解析但忽略该列;五台 dot apply 完即可删兜底)
|
|
53
56
|
let text;
|
|
57
|
+
let legacyTopology = false;
|
|
54
58
|
try {
|
|
55
|
-
text = fs.readFileSync(paths.
|
|
59
|
+
text = fs.readFileSync(paths.topologyFile, 'utf8');
|
|
56
60
|
} catch (err) {
|
|
57
|
-
if (err
|
|
58
|
-
|
|
59
|
-
|
|
61
|
+
if (!err || err.code !== 'ENOENT') throw err;
|
|
62
|
+
try {
|
|
63
|
+
text = fs.readFileSync(paths.legacyConfigFile, 'utf8');
|
|
64
|
+
legacyTopology = true;
|
|
65
|
+
} catch (err2) {
|
|
66
|
+
if (err2 && err2.code === 'ENOENT')
|
|
67
|
+
throw new Error(`缺配置 ${paths.topologyFile}(dotfiles apply ssh 后再 install;旧名 config 已改名 topology)`);
|
|
68
|
+
throw err2;
|
|
69
|
+
}
|
|
60
70
|
}
|
|
61
|
-
const topo = parseTopology(text);
|
|
71
|
+
const topo = parseTopology(text, { allowLegacyLanIp: legacyTopology });
|
|
72
|
+
|
|
73
|
+
// 静态主机表(IP 唯一来源);~/.ssh/config 缺失时给空表,契约校验报具体缺口
|
|
74
|
+
let sshConfigText = '';
|
|
75
|
+
try {
|
|
76
|
+
sshConfigText = fs.readFileSync(paths.sshConfigFile, 'utf8');
|
|
77
|
+
} catch (err) {
|
|
78
|
+
if (!err || err.code !== 'ENOENT') throw err;
|
|
79
|
+
}
|
|
80
|
+
const hostTable = parseStaticHosts(sshConfigText);
|
|
81
|
+
validateHostContract(topo, hostTable);
|
|
82
|
+
|
|
62
83
|
const hostname = localHostname(os.hostname());
|
|
63
84
|
const self = matchIdentity(topo, hostname);
|
|
64
85
|
if (!self)
|
|
@@ -69,6 +90,8 @@ function buildContext() {
|
|
|
69
90
|
env,
|
|
70
91
|
paths,
|
|
71
92
|
topo,
|
|
93
|
+
hostTable,
|
|
94
|
+
legacyTopology,
|
|
72
95
|
self,
|
|
73
96
|
role: self.role,
|
|
74
97
|
runner: createRunner(env),
|
|
@@ -16,9 +16,10 @@ import path from 'node:path';
|
|
|
16
16
|
import { explainV6Error } from '../lib/ipv6.mjs';
|
|
17
17
|
import * as launchd from '../lib/launchd.mjs';
|
|
18
18
|
import { syncPlist } from '../lib/manage.mjs';
|
|
19
|
+
import { syncConfFile, removeConfFile } from '../lib/conf.mjs';
|
|
19
20
|
import { ensureRuntime } from '../lib/runtime.mjs';
|
|
20
21
|
import {
|
|
21
|
-
edgeLabel, edgeLinkName, plistFileName, renderUplinkEdgePlist,
|
|
22
|
+
edgeLabel, edgeLinkName, plistFileName, renderUplinkEdgePlist, renderEdgeHost,
|
|
22
23
|
} from '../lib/render.mjs';
|
|
23
24
|
|
|
24
25
|
/** 手动报名(edge):与 link-core 循环里的报名 ssh 同一命令形态与选项集。
|
|
@@ -34,11 +35,19 @@ export function runEdgeAnnounce(ctx) {
|
|
|
34
35
|
const v6 = probe.stdout.trim();
|
|
35
36
|
if (!v6)
|
|
36
37
|
throw new Error('当前网络没有可用 IPv6,没有可报名的地址(中继不受影响)');
|
|
38
|
+
// 报名固定走 relay(relay 7×24 在;peer 僵尸时自动选路会把报名一起挂死)。
|
|
39
|
+
// 命令行显式 -J,不用 ~/.ssh/config 内部别名:字面目标 127.0.0.1 不命中
|
|
40
|
+
// Match originalhost <core>,绕开自动选路;跳板(relay.alias)的地址/用户
|
|
41
|
+
// 仍读 ssh config 的 Host 块;HostKeyAlias 保证三路径同一条 known_hosts。
|
|
42
|
+
// 与 ssh-net-link-core 循环里的报名 ssh 同一形态(design §6.1)。
|
|
37
43
|
const r = ctx.runner.run('ssh', [
|
|
44
|
+
'-J', ctx.topo.relay.alias,
|
|
45
|
+
'-p', String(ctx.topo.core.relayPort),
|
|
46
|
+
'-o', `HostKeyAlias=${ctx.topo.core.alias}`,
|
|
38
47
|
'-o', 'BatchMode=yes', '-o', 'ConnectTimeout=15',
|
|
39
48
|
'-o', 'ServerAliveInterval=10', '-o', 'ServerAliveCountMax=2',
|
|
40
49
|
'-o', 'ControlMaster=no', '-o', 'ControlPath=none',
|
|
41
|
-
`${ctx.topo.core.
|
|
50
|
+
`${ctx.topo.core.user}@127.0.0.1`,
|
|
42
51
|
`ssh-net _register ${ctx.self.alias} ${v6}`,
|
|
43
52
|
], { timeout: 60_000 });
|
|
44
53
|
if (r.code !== 0)
|
|
@@ -47,12 +56,33 @@ export function runEdgeAnnounce(ctx) {
|
|
|
47
56
|
return `已报名(本机 v6 ${v6},core 返回 ${core})。core 几秒内回拨建立 peer;ssh-net status 查看结果`;
|
|
48
57
|
}
|
|
49
58
|
|
|
50
|
-
/**
|
|
51
|
-
*
|
|
59
|
+
/**
|
|
60
|
+
* edge 登记两处同写(唯一写者 _register):state/edges/<别名> 给
|
|
61
|
+
* uplink-edge 脚本机读,conf.d/edge-<别名> 是 core 上 `ssh <别名>` 的入口。
|
|
62
|
+
* @param {object} ctx
|
|
63
|
+
* @param {{alias:string, user:string}} edge topology edge 行
|
|
64
|
+
* @param {string} v6
|
|
65
|
+
*/
|
|
66
|
+
function writeEdgeRegistration(ctx, edge, v6) {
|
|
67
|
+
ctx.state.writeEdgeV6(edge.alias, v6);
|
|
68
|
+
syncConfFile(
|
|
69
|
+
ctx.paths.edgeHostFile(edge.alias),
|
|
70
|
+
renderEdgeHost({ alias: edge.alias, aliases: edge.aliases, user: edge.user, v6 }),
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** 两处同删(未报名过的失败路径)。 */
|
|
75
|
+
function removeEdgeRegistration(ctx, alias) {
|
|
76
|
+
ctx.state.removeEdgeV6(alias);
|
|
77
|
+
removeConfFile(ctx.paths.edgeHostFile(alias));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** kickstart 失败时把登记回滚到 job 实际在拨的 v6(null=两处都删)。
|
|
81
|
+
* 不回滚的话磁盘已是新 v6,下轮「running + v6 相同」会被 noop 分支
|
|
52
82
|
* 误吞,job 拿旧 v6 拨到六连败休眠、再等下轮 checkin 唤醒才纠正。 */
|
|
53
|
-
function
|
|
54
|
-
if (prevV6 === null) ctx.
|
|
55
|
-
else ctx
|
|
83
|
+
function rollbackEdgeRegistration(ctx, edge, prevV6) {
|
|
84
|
+
if (prevV6 === null) removeEdgeRegistration(ctx, edge.alias);
|
|
85
|
+
else writeEdgeRegistration(ctx, edge, prevV6);
|
|
56
86
|
}
|
|
57
87
|
|
|
58
88
|
/** start peer 与 checkin 共享的实现:置 relay+peer 开关 + 控制面立即报名。
|
|
@@ -122,7 +152,7 @@ export function runRegister(ctx, args = []) {
|
|
|
122
152
|
|
|
123
153
|
// 未注册:首次报名,plist 后 enable + bootstrap
|
|
124
154
|
if (!st.registered) {
|
|
125
|
-
ctx
|
|
155
|
+
writeEdgeRegistration(ctx, edge, v6);
|
|
126
156
|
const r = syncPlist(ctx, { label, plistPath, content });
|
|
127
157
|
if (r.lintError) throw new Error(`uplink-edge plist 校验失败:${r.lintError}`);
|
|
128
158
|
return `registered ${alias}`;
|
|
@@ -131,7 +161,7 @@ export function runRegister(ctx, args = []) {
|
|
|
131
161
|
// 已注册但渲染结果与磁盘不符(topology 的 user 等变了):重新部署。
|
|
132
162
|
// syncPlist 走 bootout→落盘→enable→bootstrap,bootstrap 即首发,无需 kickstart
|
|
133
163
|
if (existing !== content) {
|
|
134
|
-
ctx
|
|
164
|
+
writeEdgeRegistration(ctx, edge, v6);
|
|
135
165
|
const r = syncPlist(ctx, { label, plistPath, content });
|
|
136
166
|
if (r.lintError) throw new Error(`uplink-edge plist 校验失败:${r.lintError}`);
|
|
137
167
|
return `redeployed ${alias}`;
|
|
@@ -140,25 +170,33 @@ export function runRegister(ctx, args = []) {
|
|
|
140
170
|
// 休眠中:写 v6 → enable 清 override → kickstart 唤醒(enable 本身不首发)。
|
|
141
171
|
// enable 失败不用回滚:disabled 分支排在 no-op 之前,下轮照走本分支
|
|
142
172
|
if (st.disabled) {
|
|
143
|
-
ctx
|
|
173
|
+
writeEdgeRegistration(ctx, edge, v6);
|
|
144
174
|
let r = launchd.enable(ctx.runner, ctx.uid, label);
|
|
145
175
|
if (r.code !== 0) throw new Error(`enable 失败:${r.stderr.trim()}`);
|
|
146
176
|
r = launchd.kickstart(ctx.runner, ctx.uid, label);
|
|
147
177
|
if (r.code !== 0) {
|
|
148
|
-
|
|
178
|
+
rollbackEdgeRegistration(ctx, edge, prevV6);
|
|
149
179
|
throw new Error(`kickstart 失败:${r.stderr.trim()}`);
|
|
150
180
|
}
|
|
151
181
|
return `woken ${alias}`;
|
|
152
182
|
}
|
|
153
183
|
|
|
154
|
-
// enabled + running 且 v6 相同:刻意 no-op(不动进程即不清零 fails
|
|
155
|
-
|
|
184
|
+
// enabled + running 且 v6 相同:刻意 no-op(不动进程即不清零 fails)。
|
|
185
|
+
// 但片段仍 reconcile:uplink-edge 六连败休眠会删 conf.d/edge-<别名>,
|
|
186
|
+
// 与报名存在删/写竞态;TTL 重报周期经过本分支,缺则补、被改则还原
|
|
187
|
+
if (st.running && prevV6 === v6) {
|
|
188
|
+
syncConfFile(
|
|
189
|
+
ctx.paths.edgeHostFile(alias),
|
|
190
|
+
renderEdgeHost({ alias, aliases: edge.aliases, user: edge.user, v6 }),
|
|
191
|
+
);
|
|
192
|
+
return `noop ${alias}`;
|
|
193
|
+
}
|
|
156
194
|
|
|
157
195
|
// enabled 但 v6 变了,或 enabled 未运行:写 v6 → kickstart
|
|
158
|
-
ctx
|
|
196
|
+
writeEdgeRegistration(ctx, edge, v6);
|
|
159
197
|
const r = launchd.kickstart(ctx.runner, ctx.uid, label);
|
|
160
198
|
if (r.code !== 0) {
|
|
161
|
-
|
|
199
|
+
rollbackEdgeRegistration(ctx, edge, prevV6);
|
|
162
200
|
throw new Error(`kickstart 失败:${r.stderr.trim()}`);
|
|
163
201
|
}
|
|
164
202
|
return `kickstarted ${alias}`;
|
|
@@ -4,9 +4,10 @@
|
|
|
4
4
|
// enable --now。
|
|
5
5
|
import fs from 'node:fs';
|
|
6
6
|
import path from 'node:path';
|
|
7
|
-
import {
|
|
7
|
+
import { buildForwardSpecs } from '../lib/config.mjs';
|
|
8
8
|
import { CALLBACK_PORT } from '../lib/constants.mjs';
|
|
9
9
|
import { syncPlist, writeFileAtomic } from '../lib/manage.mjs';
|
|
10
|
+
import { syncConfFile } from '../lib/conf.mjs';
|
|
10
11
|
import { ensureRuntime } from '../lib/runtime.mjs';
|
|
11
12
|
import * as launchd from '../lib/launchd.mjs';
|
|
12
13
|
import * as systemd from '../lib/systemd.mjs';
|
|
@@ -19,7 +20,8 @@ import {
|
|
|
19
20
|
relayLabel, relayLinkName, relayUnitName,
|
|
20
21
|
edgeLinkName,
|
|
21
22
|
plistFileName,
|
|
22
|
-
renderEdgeLinkPlist, renderRelayPlist, renderRelayUnit,
|
|
23
|
+
renderEdgeLinkPlist, renderRelayPlist, renderRelayUnit, renderRoutes,
|
|
24
|
+
renderEdgeHost,
|
|
23
25
|
} from '../lib/render.mjs';
|
|
24
26
|
|
|
25
27
|
/** edge 端口预检(纯读 + banner 判定),冲突抛错并报持有者。 */
|
|
@@ -87,7 +89,10 @@ async function installEdge(ctx) {
|
|
|
87
89
|
binDir: paths.binDir,
|
|
88
90
|
coreAlias: topo.core.alias,
|
|
89
91
|
selfAlias: ctx.self.alias,
|
|
90
|
-
|
|
92
|
+
relayAlias: topo.relay.alias,
|
|
93
|
+
relayPort: topo.core.relayPort,
|
|
94
|
+
coreUser: topo.core.user,
|
|
95
|
+
forwardArgs: buildForwardSpecs(topo.forwards, ctx.hostTable),
|
|
91
96
|
relayOnFile: paths.switchFiles.relay,
|
|
92
97
|
logsDir: paths.logsDir,
|
|
93
98
|
});
|
|
@@ -96,9 +101,13 @@ async function installEdge(ctx) {
|
|
|
96
101
|
plistPath: path.join(paths.launchAgentsDir, plistFileName(linkLabel(topo.core.alias))),
|
|
97
102
|
content,
|
|
98
103
|
});
|
|
104
|
+
// 选路片段(design §4.2):edge 是 routes 的唯一安装角色。RELAY_ON 只在
|
|
105
|
+
// edge 存在,其他机器写了也恒不命中;core/node 的 conf.d 没有本文件
|
|
106
|
+
const routesChanged = syncConfFile(paths.routesFile, renderRoutes(topo));
|
|
107
|
+
const routesMsg = routesChanged ? '(选路片段已更新)' : '';
|
|
99
108
|
return r.unchanged
|
|
100
|
-
? `已安装,无需改动${notifyNote}`
|
|
101
|
-
: `安装完成。隧道默认关闭——at home(或 ssh-net start)开启${notifyNote}`;
|
|
109
|
+
? `已安装,无需改动${routesMsg}${notifyNote}`
|
|
110
|
+
: `安装完成。隧道默认关闭——at home(或 ssh-net start)开启${routesMsg}${notifyNote}`;
|
|
102
111
|
}
|
|
103
112
|
|
|
104
113
|
/** 磁盘上已注册的 uplink-edge 别名(plist glob;topology 已删的机器靠它兜底)。 */
|
|
@@ -113,6 +122,8 @@ function registeredEdgeAliases(ctx) {
|
|
|
113
122
|
|
|
114
123
|
function installCore(ctx) {
|
|
115
124
|
const { topo, paths } = ctx;
|
|
125
|
+
reconcileEdgeFragments(ctx);
|
|
126
|
+
// conf.d/edge-<别名> 不在此写:那是 _register(edge 报名)的产物,见 checkin.mjs。
|
|
116
127
|
// 已注册的回拨组件在 install 时一并迁移/对账(旧版带机器名实体拷贝 →
|
|
117
128
|
// 软链):否则 upgrade 后 core status 要对每台未联系的 edge 报组件异常,
|
|
118
129
|
// 直到它下次报名才自愈(2026-09-30 补)
|
|
@@ -173,12 +184,53 @@ function installNode(ctx) {
|
|
|
173
184
|
return `安装完成,relay 已开启(s100 入口 ${ctx.self.relayPort})`;
|
|
174
185
|
}
|
|
175
186
|
|
|
187
|
+
/**
|
|
188
|
+
* core:把 conf.d/edge-* 严格对账成「topology 名单 + state 最新 v6」的视图——
|
|
189
|
+
* 文件是否存在完全由 state 有没有该 edge 的 v6 决定:
|
|
190
|
+
* - 在册且 state 有 v6:从 state 纯渲染(缺失补、过期改、相同不动)。这是
|
|
191
|
+
* 手删/磁盘丢失的恢复通道——peer 稳态的 link 循环不报名,等不到
|
|
192
|
+
* _register 自然补写
|
|
193
|
+
* - 在册但 state 无 v6(从没报名成功/state 被删):删掉残留片段——没有
|
|
194
|
+
* state 支撑的入口指向旧 v6,是必然失败的假入口
|
|
195
|
+
* - topology 已删 edge(改名/退役):删片段;state 同名残留不动(既有
|
|
196
|
+
* 现象,uninstall 才整棵清)
|
|
197
|
+
*/
|
|
198
|
+
function reconcileEdgeFragments(ctx) {
|
|
199
|
+
const { topo, paths } = ctx;
|
|
200
|
+
let files = [];
|
|
201
|
+
try {
|
|
202
|
+
files = fs.readdirSync(paths.confDir).filter((f) => f.startsWith('edge-'));
|
|
203
|
+
} catch { files = []; }
|
|
204
|
+
|
|
205
|
+
for (const e of topo.edges) {
|
|
206
|
+
const file = paths.edgeHostFile(e.alias);
|
|
207
|
+
const v6 = ctx.state.readEdgeV6(e.alias);
|
|
208
|
+
if (v6) {
|
|
209
|
+
syncConfFile(
|
|
210
|
+
file,
|
|
211
|
+
renderEdgeHost({ alias: e.alias, aliases: e.aliases, user: e.user, v6 }),
|
|
212
|
+
);
|
|
213
|
+
} else {
|
|
214
|
+
fs.rmSync(file, { force: true });
|
|
215
|
+
}
|
|
216
|
+
files = files.filter((f) => f !== `edge-${e.alias}`);
|
|
217
|
+
}
|
|
218
|
+
// 剩下的都是 topology 不认识的孤儿
|
|
219
|
+
for (const f of files)
|
|
220
|
+
fs.rmSync(path.join(paths.confDir, f), { force: true });
|
|
221
|
+
}
|
|
222
|
+
|
|
176
223
|
/** @param {object} ctx */
|
|
177
224
|
export async function run(ctx) {
|
|
225
|
+
let result;
|
|
178
226
|
switch (ctx.role) {
|
|
179
|
-
case 'edge':
|
|
180
|
-
case 'core':
|
|
181
|
-
case 'node':
|
|
227
|
+
case 'edge': result = await installEdge(ctx); break;
|
|
228
|
+
case 'core': result = installCore(ctx); break;
|
|
229
|
+
case 'node': result = installNode(ctx); break;
|
|
182
230
|
default: throw new Error(`未知角色:${ctx.role}`);
|
|
183
231
|
}
|
|
232
|
+
// 迁移窗口提示:还在吃旧名 config,dot apply 出新 topology 后旧文件可手删
|
|
233
|
+
if (ctx.legacyTopology)
|
|
234
|
+
result += '(当前仍读旧文件 ~/.ssh/ssh-net/config 兜底——dotfiles apply ssh 后可删它)';
|
|
235
|
+
return result;
|
|
184
236
|
}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
// status(design §6.7):只报本机角色,判据是 banner/端口/注册态真实探测。
|
|
2
2
|
// collect 返回结构化对象(测试断言对象,不锁文本);renderText 给人看——
|
|
3
3
|
// 2026-09-29 三开关起按「开关(意图)与就绪(实测)分开」组织:开关行报
|
|
4
|
-
// RELAY_ON/PEER_ON/LINK_ON,线路行逐条实测(relay
|
|
5
|
-
//
|
|
4
|
+
// RELAY_ON/PEER_ON/LINK_ON,线路行逐条实测(relay 就绪=命令行 -J 经 relay
|
|
5
|
+
// 真跑一条 ssh,不再从「probe 不通」推断),开关关的线路报「关」不探测。
|
|
6
6
|
// bin/ 体检(2026-09-30 软链方案):实体逐字节比对 + 服务软链指向/悬空检查。
|
|
7
7
|
// 退出码:健康(含 edge 关闭态)0;有可行动问题 1。
|
|
8
|
+
import fs from 'node:fs';
|
|
8
9
|
import path from 'node:path';
|
|
9
10
|
import {
|
|
10
11
|
auditScript, auditLink, readRuntimeVersion, runtimeVersionIssue,
|
|
@@ -15,9 +16,21 @@ import * as systemd from '../lib/systemd.mjs';
|
|
|
15
16
|
import { peerProbeOk, tcpOpen, detectTerminalNotifier } from '../lib/probe.mjs';
|
|
16
17
|
import {
|
|
17
18
|
linkLabel, linkLinkName, relayLabel, relayLinkName, relayUnitName,
|
|
18
|
-
edgeLabel, edgeLinkName,
|
|
19
|
+
edgeLabel, edgeLinkName, renderRoutes, renderEdgeHost,
|
|
19
20
|
} from '../lib/render.mjs';
|
|
20
21
|
|
|
22
|
+
/**
|
|
23
|
+
* 读 conf.d 片段并与期望内容比对。
|
|
24
|
+
* @returns {'ok'|'stale'|'missing'}
|
|
25
|
+
*/
|
|
26
|
+
function auditConfFragment(file, expected) {
|
|
27
|
+
let text = null;
|
|
28
|
+
try {
|
|
29
|
+
text = fs.readFileSync(file, 'utf8');
|
|
30
|
+
} catch { return 'missing'; }
|
|
31
|
+
return text === expected ? 'ok' : 'stale';
|
|
32
|
+
}
|
|
33
|
+
|
|
21
34
|
/** 实体脚本体检列表。 */
|
|
22
35
|
function auditRequired(ctx, entries) {
|
|
23
36
|
return entries.map(([name, script]) => ({
|
|
@@ -71,11 +84,15 @@ async function relayPortListening(ctx, port) {
|
|
|
71
84
|
return { port, reachable: true, listening: new RegExp(`127\\.0\\.0\\.1[:.]${port}\\b`).test(r.stdout) };
|
|
72
85
|
}
|
|
73
86
|
|
|
74
|
-
/** edge 的 relay
|
|
87
|
+
/** edge 的 relay 线路实测:命令行显式 -J 经 relay 跑一条真 ssh(报名/中继同
|
|
88
|
+
* 信道;字面 127.0.0.1 不命中 Match,绕开自动选路,design §6.7)。 */
|
|
75
89
|
async function relayPathOk(ctx) {
|
|
76
90
|
const r = await ctx.runner.runAsync('ssh', [
|
|
91
|
+
'-J', ctx.topo.relay.alias,
|
|
92
|
+
'-p', String(ctx.topo.core.relayPort),
|
|
93
|
+
'-o', `HostKeyAlias=${ctx.topo.core.alias}`,
|
|
77
94
|
'-o', 'BatchMode=yes', '-o', 'ConnectTimeout=5',
|
|
78
|
-
`${ctx.topo.core.
|
|
95
|
+
`${ctx.topo.core.user}@127.0.0.1`, 'true',
|
|
79
96
|
], { timeout: 8000 });
|
|
80
97
|
return r.code === 0;
|
|
81
98
|
}
|
|
@@ -98,6 +115,14 @@ async function collectEdge(ctx) {
|
|
|
98
115
|
});
|
|
99
116
|
issues.push(...rtIssues);
|
|
100
117
|
|
|
118
|
+
// 选路片段体检(已安装才查:没装时 rtIssues 已报「尚未安装」,别重复):
|
|
119
|
+
// routes 被手删/过期会让 ssh 静默退回 lan 直连,必须显眼
|
|
120
|
+
if (links.find((l) => l.link === 'ssh-net-link-core')?.audit === 'ok') {
|
|
121
|
+
const routeAudit = auditConfFragment(paths.routesFile, renderRoutes(topo));
|
|
122
|
+
if (routeAudit !== 'ok')
|
|
123
|
+
issues.push(`选路片段 conf.d/routes ${routeAudit === 'missing' ? '缺失' : '与 topology 不一致'}——跑一次 ssh-net install 刷新`);
|
|
124
|
+
}
|
|
125
|
+
|
|
101
126
|
const label = linkLabel(topo.core.alias);
|
|
102
127
|
const job = launchd.inspect(ctx.runner, ctx.uid, label);
|
|
103
128
|
const switches = state.readSwitches();
|
|
@@ -114,9 +139,12 @@ async function collectEdge(ctx) {
|
|
|
114
139
|
// status 要能区分「现有连接保持中」与「彻底没了」
|
|
115
140
|
...topo.forwards.map((f) => openTcp('127.0.0.1', f.listenPort)),
|
|
116
141
|
];
|
|
117
|
-
// relay 开但 core 的 LAN 22 可达 =
|
|
118
|
-
|
|
119
|
-
|
|
142
|
+
// relay 开但 core 的 LAN 22 可达 = 人在办公室忘了关(只提示不纠偏)。
|
|
143
|
+
// core 的 LAN IP 不进 topology,从 ~/.ssh/config 的静态 Host 块取(契约校验
|
|
144
|
+
// 已保证存在),不读 routes——Match 会把地址改写成环回口
|
|
145
|
+
const coreLanIp = ctx.hostTable.get(topo.core.alias)?.hostname;
|
|
146
|
+
const lanIdx = switches.relay && coreLanIp ? probes.length : -1;
|
|
147
|
+
if (lanIdx >= 0) probes.push(openTcp(coreLanIp, 22, 1200));
|
|
120
148
|
const got = await Promise.all(probes);
|
|
121
149
|
const relayReady = got[0];
|
|
122
150
|
const peerReady = got[1];
|
|
@@ -125,7 +153,7 @@ async function collectEdge(ctx) {
|
|
|
125
153
|
const lanReachable = lanIdx >= 0 ? got[lanIdx] : false;
|
|
126
154
|
|
|
127
155
|
if (lanReachable)
|
|
128
|
-
issues.push(`检测到 ${topo.core.alias}(${
|
|
156
|
+
issues.push(`检测到 ${topo.core.alias}(${coreLanIp})局域网直连可达——隧道可能在空转绕中继;人在办公室可 ssh-net stop`);
|
|
129
157
|
|
|
130
158
|
// relay 关(全关/级联)但转发口仍在听 = 孤儿进程;link 软关的口在听不报
|
|
131
159
|
if (!switches.relay)
|
|
@@ -200,13 +228,28 @@ async function collectCore(ctx) {
|
|
|
200
228
|
// ——别引导「重跑 install」,白跑;只能等该 edge 下次报名自动修复
|
|
201
229
|
issues.push(`${e.alias} 的回拨组件异常(${bad.join('、')})——它下次报名时会自动刷新`);
|
|
202
230
|
}
|
|
231
|
+
const v6 = ctx.state.readEdgeV6(e.alias);
|
|
232
|
+
// edge 入口片段体检:job 在册且未休眠(enabled)+ 有 v6 时,片段必须
|
|
233
|
+
// 与 state 一致——缺失(手删/磁盘丢失)或过期(短名/user 变更后没
|
|
234
|
+
// install)都会让 `ssh <别名>` 失败或解析错。休眠(disabled)时片段
|
|
235
|
+
// 本就该删(六连败收尸),不报;未注册无 v6 也不报
|
|
236
|
+
let fragmentAudit = null;
|
|
237
|
+
if (st.registered && !st.disabled && v6) {
|
|
238
|
+
fragmentAudit = auditConfFragment(
|
|
239
|
+
paths.edgeHostFile(e.alias),
|
|
240
|
+
renderEdgeHost({ alias: e.alias, aliases: e.aliases, user: e.user, v6 }),
|
|
241
|
+
);
|
|
242
|
+
if (fragmentAudit !== 'ok')
|
|
243
|
+
issues.push(`${e.alias} 的 ssh 入口片段${fragmentAudit === 'missing' ? '缺失' : '过期'}(state 有 v6、job 未休眠)——跑一次 ssh-net install 重建`);
|
|
244
|
+
}
|
|
203
245
|
edges.push({
|
|
204
246
|
alias: e.alias,
|
|
205
247
|
registered: st.registered,
|
|
206
248
|
disabled: st.disabled,
|
|
207
249
|
running: st.running,
|
|
208
250
|
lastExitCode: st.lastExitCode,
|
|
209
|
-
v6
|
|
251
|
+
v6,
|
|
252
|
+
fragment: fragmentAudit,
|
|
210
253
|
});
|
|
211
254
|
}
|
|
212
255
|
|
|
@@ -327,7 +370,9 @@ export function renderText(s) {
|
|
|
327
370
|
: e.disabled ? '休眠中(它下次开机报名/TTL 到点重报时自动唤醒)'
|
|
328
371
|
: e.running ? `已连接(peer ${e.v6 ?? '?'})${exitNote(e.lastExitCode)}`
|
|
329
372
|
: '已断开(自动重试中)';
|
|
330
|
-
|
|
373
|
+
// 入口片段态只在异常时显形(正常时无需噪音;issues 区也会报)
|
|
374
|
+
const frag = e.fragment && e.fragment !== 'ok' ? ` · ssh 入口${e.fragment === 'missing' ? '缺失' : '过期'}` : '';
|
|
375
|
+
lines.push(` ${e.alias} ${state}${frag}`);
|
|
331
376
|
}
|
|
332
377
|
} else {
|
|
333
378
|
lines.push(s.relayPort.listening
|
|
@@ -30,6 +30,7 @@ function uninstallEdge(ctx) {
|
|
|
30
30
|
));
|
|
31
31
|
rm(ctx.paths.binDir);
|
|
32
32
|
rm(ctx.paths.stateDir);
|
|
33
|
+
rm(ctx.paths.confDir); // routes(edge 装的);node 上不存在无害
|
|
33
34
|
rm(ctx.paths.runtimeVersionFile);
|
|
34
35
|
rm(path.join(ctx.paths.logsDir, `${label}.log`));
|
|
35
36
|
return '已卸载。最后一步:npm uninstall -g ssh-net';
|
|
@@ -54,6 +55,7 @@ function uninstallCore(ctx) {
|
|
|
54
55
|
|
|
55
56
|
rm(ctx.paths.binDir);
|
|
56
57
|
rm(ctx.paths.stateDir);
|
|
58
|
+
rm(ctx.paths.confDir); // 全部 edge-<别名> 动态片段
|
|
57
59
|
rm(ctx.paths.runtimeVersionFile);
|
|
58
60
|
for (const label of [...edgeJobs.keys(), relay])
|
|
59
61
|
rm(path.join(ctx.paths.logsDir, `${label}.log`));
|
|
@@ -74,6 +76,7 @@ function uninstallNode(ctx) {
|
|
|
74
76
|
systemd.daemonReload(ctx.runner);
|
|
75
77
|
rm(ctx.paths.binDir);
|
|
76
78
|
rm(ctx.paths.stateDir);
|
|
79
|
+
rm(ctx.paths.confDir);
|
|
77
80
|
rm(ctx.paths.runtimeVersionFile);
|
|
78
81
|
return '已卸载。最后一步:npm uninstall -g ssh-net';
|
|
79
82
|
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// conf.d 片段的落盘原语(design §4.2,2026-10-01)。~/.ssh/config 顶部
|
|
2
|
+
// `Include ~/.ssh/ssh-net/conf.d/*` 会 glob 进本目录每个文件,所以:
|
|
3
|
+
// - tmp 文件绝不写在 conf.d/ 内(写入中的半截文件会被某次 ssh 扫到)——
|
|
4
|
+
// 写在上级目录 ~/.ssh/ssh-net/.<名>.tmp-<pid> 再原子 rename 进来
|
|
5
|
+
// - 目录 0700、片段 0600(与 ~/.ssh 权限纪律一致)
|
|
6
|
+
// - sync 幂等:内容字节相同不动盘(routes 每次 install 调,不产生无意义变更)
|
|
7
|
+
import fs from 'node:fs';
|
|
8
|
+
import path from 'node:path';
|
|
9
|
+
|
|
10
|
+
/** 确保 conf.d 存在且 0700(~/.ssh/ssh-net 已由 state 侧建为 0700)。 */
|
|
11
|
+
export function ensureConfDir(confDir) {
|
|
12
|
+
fs.mkdirSync(confDir, { recursive: true });
|
|
13
|
+
fs.chmodSync(confDir, 0o700);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 原子同步一个 Include 片段。
|
|
18
|
+
* @param {string} file 片段绝对路径(在 conf.d 内)
|
|
19
|
+
* @param {string} content 完整内容
|
|
20
|
+
* @returns {boolean} 是否实际重写
|
|
21
|
+
*/
|
|
22
|
+
export function syncConfFile(file, content) {
|
|
23
|
+
let existing = null;
|
|
24
|
+
try {
|
|
25
|
+
existing = fs.readFileSync(file, 'utf8');
|
|
26
|
+
} catch {
|
|
27
|
+
existing = null;
|
|
28
|
+
}
|
|
29
|
+
if (existing === content) return false;
|
|
30
|
+
|
|
31
|
+
const dir = path.dirname(file);
|
|
32
|
+
ensureConfDir(dir);
|
|
33
|
+
// tmp 落 conf.d 的**上级**目录:glob conf.d/* 扫不到它
|
|
34
|
+
const tmp = path.join(path.dirname(dir), `.${path.basename(file)}.tmp-${process.pid}`);
|
|
35
|
+
fs.writeFileSync(tmp, content, { mode: 0o600 });
|
|
36
|
+
fs.renameSync(tmp, file);
|
|
37
|
+
fs.chmodSync(file, 0o600);
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** 删一个片段(不存在静默)。 */
|
|
42
|
+
export function removeConfFile(file) {
|
|
43
|
+
fs.rmSync(file, { force: true });
|
|
44
|
+
}
|