ssh-net 26.10.27 → 26.10.30
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 +201 -9
- package/README.md +50 -34
- package/libexec/cli.mjs +10 -21
- package/libexec/commands/checkin.mjs +101 -39
- package/libexec/commands/doctor.mjs +19 -34
- package/libexec/commands/install.mjs +105 -53
- package/libexec/commands/restart.mjs +7 -3
- package/libexec/commands/start.mjs +23 -53
- package/libexec/commands/status.mjs +85 -65
- package/libexec/commands/stop.mjs +19 -33
- package/libexec/commands/uninstall.mjs +1 -0
- package/libexec/commands/upgrade.mjs +13 -7
- package/libexec/lib/config.mjs +28 -48
- package/libexec/lib/ipv6.mjs +1 -1
- package/libexec/lib/jobctl.mjs +7 -6
- package/libexec/lib/launchd.mjs +5 -2
- package/libexec/lib/manage.mjs +11 -3
- package/libexec/lib/paths.mjs +22 -15
- package/libexec/lib/render.mjs +59 -24
- package/libexec/lib/runtime.mjs +3 -11
- package/libexec/lib/state.mjs +10 -12
- package/libexec/sn-route +187 -65
- package/libexec/sn-signal +35 -28
- package/libexec/ssh-net-link-core +13 -474
- package/libexec/ssh-net-uplink-edge +16 -9
- package/package.json +1 -1
package/libexec/lib/config.mjs
CHANGED
|
@@ -3,8 +3,7 @@
|
|
|
3
3
|
// 只管名称:IP 不进 topology——静态主机地址的唯一来源是 ~/.ssh/config 的
|
|
4
4
|
// Host 块(lib/sshconfig.mjs 解析成 hostTable 传入),relay 地址/用户同理
|
|
5
5
|
// 由 Host <relay 别名> 块提供。本文件只做语法解析与拓扑内部校验;别名→IP 的翻译
|
|
6
|
-
// 在
|
|
7
|
-
import net from 'node:net';
|
|
6
|
+
// 在 renderForwardArgs 渲染时查 hostTable(install 时做,缺块即报错)。
|
|
8
7
|
|
|
9
8
|
export class ConfigError extends Error {
|
|
10
9
|
/**
|
|
@@ -83,15 +82,11 @@ function parseAliasList(tok, lineNo) {
|
|
|
83
82
|
|
|
84
83
|
/**
|
|
85
84
|
* @param {string} text
|
|
86
|
-
* @param {{allowLegacyLanIp?: boolean}} [opts] 兼容 2026-10-01 前的旧 7 列
|
|
87
|
-
* 格式(多一列 LAN_IP):仅在读旧名 `config` 兜底时传 true;该列只校验、
|
|
88
|
-
* 不进 HostRow。五台切换完成后连同文件名兜底一起删
|
|
89
85
|
* @returns {Topology}
|
|
90
86
|
*/
|
|
91
|
-
export function parseTopology(text
|
|
87
|
+
export function parseTopology(text) {
|
|
92
88
|
if (typeof text !== 'string' || text.trim().length === 0)
|
|
93
89
|
throw new ConfigError(0, 'topology 为空');
|
|
94
|
-
const allowLegacy = opts.allowLegacyLanIp === true;
|
|
95
90
|
|
|
96
91
|
/** @type {RelayRow|null} */
|
|
97
92
|
let relay = null;
|
|
@@ -121,24 +116,12 @@ export function parseTopology(text, opts = {}) {
|
|
|
121
116
|
}
|
|
122
117
|
|
|
123
118
|
if (head === 'host') {
|
|
124
|
-
|
|
125
|
-
let hostname;
|
|
126
|
-
let aliasTok;
|
|
127
|
-
let lanTok = null;
|
|
128
|
-
let portTok;
|
|
129
|
-
let user;
|
|
130
|
-
if (cols.length === 6) {
|
|
131
|
-
[, role, hostname, aliasTok, portTok, user] = cols;
|
|
132
|
-
} else if (cols.length === 7 && allowLegacy) {
|
|
133
|
-
// 旧 7 列:…<别名> <LAN_IP|-> <relay端口|-> <用户>。LAN_IP 只校验
|
|
134
|
-
// 不落对象(新格式里它归 ~/.ssh/config)
|
|
135
|
-
[, role, hostname, aliasTok, lanTok, portTok, user] = cols;
|
|
136
|
-
} else {
|
|
119
|
+
if (cols.length !== 6)
|
|
137
120
|
throw new ConfigError(
|
|
138
121
|
lineNo,
|
|
139
122
|
'host 需要 6 列:host <角色> <主机名> <别名[,附加名…]> <relay端口|-> <用户>',
|
|
140
123
|
);
|
|
141
|
-
|
|
124
|
+
const [, role, hostname, aliasTok, portTok, user] = cols;
|
|
142
125
|
if (!ROLES.has(role)) throw new ConfigError(lineNo, `未知角色:${role}(只允许 core/edge/node)`);
|
|
143
126
|
if (!ALIAS_RE.test(hostname)) throw new ConfigError(lineNo, `主机名非法:${hostname}`);
|
|
144
127
|
const aliases = parseAliasList(aliasTok, lineNo);
|
|
@@ -149,14 +132,6 @@ export function parseTopology(text, opts = {}) {
|
|
|
149
132
|
if (role !== 'edge' && aliases.length > 1)
|
|
150
133
|
throw new ConfigError(lineNo, `只有 edge 行支持附加名(core 侧入口渲染用):${aliasTok}`);
|
|
151
134
|
if (!user || user === '-') throw new ConfigError(lineNo, '用户不能为空');
|
|
152
|
-
if (lanTok !== null) {
|
|
153
|
-
if (role === 'edge') {
|
|
154
|
-
if (lanTok !== '-')
|
|
155
|
-
throw new ConfigError(lineNo, 'edge 行的 LAN_IP 必须填 -');
|
|
156
|
-
} else if (net.isIP(lanTok) === 0) {
|
|
157
|
-
throw new ConfigError(lineNo, `LAN_IP 不是 IP 字面值:${lanTok}`);
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
135
|
/** @type {number|null} */
|
|
161
136
|
let relayPort = null;
|
|
162
137
|
if (role === 'edge') {
|
|
@@ -230,10 +205,10 @@ export function parseTopology(text, opts = {}) {
|
|
|
230
205
|
throw new ConfigError(line, `目标端口非法:${cols[4]}`);
|
|
231
206
|
// 环回别名:目标服务在 ssh 落地机的本机环回上,不是主机名、无 Host 块:
|
|
232
207
|
// loopback = 127.0.0.1(microsocks 只听 v4)
|
|
233
|
-
// loopback6 = ::1
|
|
234
|
-
//
|
|
208
|
+
// loopback6 = [::1](renderForwardArgs 固定 v6——relay 是 ProxyJump
|
|
209
|
+
// 对 -L 透明,两路径转发都落地 core 本机)
|
|
235
210
|
// 具名别名(core/node)的 HostName 来自 ~/.ssh/config Host 块,
|
|
236
|
-
//
|
|
211
|
+
// renderForwardArgs 渲染时查 hostTable——topology 不存任何 IP。
|
|
237
212
|
if (targetAlias !== 'loopback' && targetAlias !== 'loopback6') {
|
|
238
213
|
const target = byAlias.get(targetAlias);
|
|
239
214
|
if (!target)
|
|
@@ -286,35 +261,40 @@ export function validateHostContract(topo, hostTable) {
|
|
|
286
261
|
}
|
|
287
262
|
|
|
288
263
|
/**
|
|
289
|
-
*
|
|
290
|
-
* dynamic →
|
|
291
|
-
* local →
|
|
292
|
-
* target:loopback=127.0.0.1;loopback6
|
|
293
|
-
*
|
|
294
|
-
*
|
|
295
|
-
*
|
|
264
|
+
* 转发渲染成最终 ssh argv(2026-10-02 起,link 是纯 ssh 通道、无脚本端解析):
|
|
265
|
+
* dynamic → `-D 127.0.0.1:<listenPort>`
|
|
266
|
+
* local → `-L 127.0.0.1:<listenPort>:<target>:<targetPort>`
|
|
267
|
+
* target:loopback=127.0.0.1;loopback6 固定 `[::1]`——relay 是 ProxyJump、
|
|
268
|
+
* 对 -L 透明,peer/relay 两路径的转发都由 core sshd 在 core 本机发起,不需要
|
|
269
|
+
* 按 mode 选族;具名别名查 hostTable(~/.ssh/config Host 块的 HostName,
|
|
270
|
+
* IP 唯一来源)。
|
|
296
271
|
* @param {readonly Forward[]} forwards
|
|
297
272
|
* @param {Map<string, {hostname?: string}>} hostTable parseStaticHosts 的产物
|
|
298
|
-
* @returns {string[]}
|
|
273
|
+
* @returns {string[]} 扁平 argv(['-D','127.0.0.1:..','-L',...])
|
|
299
274
|
*/
|
|
300
|
-
export function
|
|
275
|
+
export function renderForwardArgs(forwards, hostTable) {
|
|
301
276
|
/** @type {string[]} */
|
|
302
|
-
const
|
|
277
|
+
const args = [];
|
|
303
278
|
for (const f of forwards) {
|
|
304
279
|
if (f.kind === 'dynamic') {
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
280
|
+
args.push('-D', `127.0.0.1:${f.listenPort}`);
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
/** @type {string} */
|
|
284
|
+
let target;
|
|
285
|
+
if (f.targetAlias === 'loopback6') {
|
|
286
|
+
target = '[::1]';
|
|
308
287
|
} else if (f.targetAlias === 'loopback') {
|
|
309
|
-
|
|
288
|
+
target = '127.0.0.1';
|
|
310
289
|
} else {
|
|
311
290
|
const host = hostTable.get(f.targetAlias);
|
|
312
291
|
// 语法解析时只校验别名在 topology;此处才查 ssh config——install 时
|
|
313
292
|
// 暴露,错误直指缺 Host 块的那台机器
|
|
314
293
|
if (!host || !host.hostname)
|
|
315
294
|
throw new Error(`forward local 目标 ${f.targetAlias} 在 ~/.ssh/config 缺 Host 块或 HostName`);
|
|
316
|
-
|
|
295
|
+
target = host.hostname;
|
|
317
296
|
}
|
|
297
|
+
args.push('-L', `127.0.0.1:${f.listenPort}:${target}:${f.targetPort}`);
|
|
318
298
|
}
|
|
319
|
-
return
|
|
299
|
+
return args;
|
|
320
300
|
}
|
package/libexec/lib/ipv6.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// checkin v6 入参校验(design §6.3)。唯一外部输入面:合法 IPv6 字面值 +
|
|
2
2
|
// 可拨号段——2000::/3 公网单播,或 fc00::/7 ULA(2026-09-29 起收 ULA:
|
|
3
3
|
// 办公室内网彩排 peer 路径 / 纯内网部署场景。「报不报 ULA」的严格性在
|
|
4
|
-
// edge 侧
|
|
4
|
+
// edge 侧 sn-ipv6 的选取,checkin 只管「这个地址能不能拨」)。
|
|
5
5
|
// 仍拒绝 scope id、内嵌 v4(含 ::ffff: v4-mapped)、link-local、unspecified、
|
|
6
6
|
// 组播等。依赖 net.isIP 做字面值合法性,首 hextet 判段。
|
|
7
7
|
import net from 'node:net';
|
package/libexec/lib/jobctl.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
// edge stop 的进程收束(design §5):rm
|
|
2
|
-
//
|
|
3
|
-
//
|
|
1
|
+
// edge stop 的进程收束(design §5):rm 两开关后 launchctl kill TERM,轮询
|
|
2
|
+
// 校验纯 ssh job 真退出(信号直达 ssh)。未装 link(label 不在册)直接
|
|
3
|
+
// gone。预算内补发一次 TERM,仍在则报错给手工排障命令,绝不自动 KILL。
|
|
4
|
+
// 同步 CLI 用 Atomics.wait 节流。
|
|
4
5
|
import * as launchd from './launchd.mjs';
|
|
5
6
|
import { linkLabel } from './render.mjs';
|
|
6
7
|
|
|
@@ -23,9 +24,9 @@ function isGone(ctx, label) {
|
|
|
23
24
|
*/
|
|
24
25
|
export function stopEdgeLink(ctx) {
|
|
25
26
|
const label = linkLabel(ctx.topo.core.alias);
|
|
26
|
-
//
|
|
27
|
-
//
|
|
28
|
-
ctx.state.removeOn(['relay', 'peer'
|
|
27
|
+
// 删两开关(peer 依赖 relay,见 design §5)。PathState:rm RELAY_ON 不杀
|
|
28
|
+
// 进程,但阻止退出后重拉
|
|
29
|
+
ctx.state.removeOn(['relay', 'peer']);
|
|
29
30
|
if (!launchd.inspect(ctx.runner, ctx.uid, label).registered) return { gone: true, label };
|
|
30
31
|
|
|
31
32
|
let remaining = TERM_BUDGET_MS;
|
package/libexec/lib/launchd.mjs
CHANGED
|
@@ -86,9 +86,12 @@ export const bootstrap = (runner, uid, plistPath) =>
|
|
|
86
86
|
runner.run('launchctl', ['bootstrap', guiDomain(uid), plistPath]);
|
|
87
87
|
export const kickstart = (runner, uid, label) =>
|
|
88
88
|
runner.run('launchctl', ['kickstart', '-k', `${guiDomain(uid)}/${label}`]);
|
|
89
|
-
/** kickstart 对 disabled job 被拒(实测矩阵),kickstart 前一律幂等 enable。
|
|
89
|
+
/** kickstart 对 disabled job 被拒(实测矩阵),kickstart 前一律幂等 enable。
|
|
90
|
+
* enable 失败要报(本文件宣言「勿在此吞错」):否则以 kickstart 被拒的
|
|
91
|
+
* 间接形式浮出,排障方向被带偏。 */
|
|
90
92
|
export const kickstartEnabled = (runner, uid, label) => {
|
|
91
|
-
enable(runner, uid, label);
|
|
93
|
+
const en = enable(runner, uid, label);
|
|
94
|
+
if (en.code !== 0) throw new Error(`enable 失败(${label}):${en.stderr.trim()}`);
|
|
92
95
|
return kickstart(runner, uid, label);
|
|
93
96
|
};
|
|
94
97
|
export const killJob = (runner, uid, label, signal = 'TERM') =>
|
package/libexec/lib/manage.mjs
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
// (bin 运行时脚本的拷贝对账在 runtime.mjs)。核心承诺(design §5):
|
|
3
3
|
// 渲染结果与现文件字节相同则不 bootout——install 幂等不断连。
|
|
4
4
|
import fs from 'node:fs';
|
|
5
|
-
import path from 'node:path';
|
|
6
5
|
import * as launchd from './launchd.mjs';
|
|
7
6
|
|
|
8
7
|
/** 同目录 tmp + rename 原子落盘。 */
|
|
@@ -37,7 +36,10 @@ export function syncPlist(ctx, { label, plistPath, content }) {
|
|
|
37
36
|
// registered+disabled(stop 中途失败/手工 disable)不是「已部署」:
|
|
38
37
|
// enable 后拉起,否则 install 报 unchanged 却没在跑、start 的 kickstart 被拒
|
|
39
38
|
if (st.registered && !st.disabled) return { unchanged: true };
|
|
40
|
-
launchd.enable(runner, uid, label);
|
|
39
|
+
const en = launchd.enable(runner, uid, label);
|
|
40
|
+
// enable 失败要报(launchd.mjs 宣言「勿在此吞错」):否则错误以
|
|
41
|
+
// bootstrap error 5 / kickstart 被拒的间接形式浮出,排障方向被带偏
|
|
42
|
+
if (en.code !== 0) throw new Error(`enable 失败(${label}):${en.stderr.trim()}`);
|
|
41
43
|
if (!st.registered) {
|
|
42
44
|
const b = launchd.bootstrap(runner, uid, plistPath);
|
|
43
45
|
if (b.code !== 0) throw new Error(`bootstrap 失败(${label}):${b.stderr.trim()}`);
|
|
@@ -65,7 +67,13 @@ export function syncPlist(ctx, { label, plistPath, content }) {
|
|
|
65
67
|
}
|
|
66
68
|
fs.renameSync(tmp, plistPath);
|
|
67
69
|
fs.chmodSync(plistPath, 0o644);
|
|
68
|
-
|
|
70
|
+
// enable 失败要报(launchd.mjs 宣言「勿在此吞错」):否则错误以 bootstrap
|
|
71
|
+
// error 5 的间接形式浮出,排障方向被带偏。取舍:此处 job 已 bootout、
|
|
72
|
+
// enable 失败即不 bootstrap(停机态)——launchctl enable 对已 enable 的
|
|
73
|
+
// job 幂等返回 0,真失败(override 数据库损坏)下 bootstrap 也过不去
|
|
74
|
+
// (error 5),可见性优先于「也许 bootstrap 能侥幸成功」
|
|
75
|
+
const en = launchd.enable(runner, uid, label); // 无条件先 enable(override 残留时 bootstrap error 5)
|
|
76
|
+
if (en.code !== 0) throw new Error(`enable 失败(${label}):${en.stderr.trim()}`);
|
|
69
77
|
const b = launchd.bootstrap(runner, uid, plistPath);
|
|
70
78
|
if (b.code !== 0) throw new Error(`bootstrap 失败(${label}):${b.stderr.trim()}`);
|
|
71
79
|
return { deployed: true };
|
package/libexec/lib/paths.mjs
CHANGED
|
@@ -5,12 +5,14 @@
|
|
|
5
5
|
// ~/.ssh/ssh-net/bin/(不再软链 fnm alias)——运行时零 npm/fnm 依赖;
|
|
6
6
|
// 状态与产物同住 ~/.ssh/ssh-net/ 一棵树(state/ 子目录)。
|
|
7
7
|
//
|
|
8
|
-
// 目录分工(2026-10-
|
|
8
|
+
// 目录分工(2026-10-02 改):
|
|
9
9
|
// topology dotfiles 托管的拓扑(名/角色/relay 口/用户;旧名 config 退役)
|
|
10
10
|
// bin/ 包脚本实体 + 服务软链(install/checkin 对账)
|
|
11
|
-
// state/
|
|
11
|
+
// state/ 易变状态(两开关、edges v6、PROBE_FAIL、ENSURE)
|
|
12
|
+
// link.enabled link 附加件的持久安装意图(树根,不放 state/)
|
|
12
13
|
// conf.d/ 渲染给 ssh Include 的配置片段(生成物,不进 dotfiles):
|
|
13
14
|
// routes edge:Match 选路块(install 渲染)
|
|
15
|
+
// vscode-mark edge:VS Code 连接打标块(install 渲染常量)
|
|
14
16
|
// edge-<别名> core:edge 在线入口(_register 写/休眠删)
|
|
15
17
|
// ~/.ssh/config dotfiles 托管,顶部 Include conf.d/* + 静态主机块(IP 唯一来源)
|
|
16
18
|
import path from 'node:path';
|
|
@@ -37,6 +39,9 @@ export function packageScriptPath(scriptName) {
|
|
|
37
39
|
*/
|
|
38
40
|
export function machinePaths(env = process.env) {
|
|
39
41
|
const home = env.HOME ?? '';
|
|
42
|
+
// HOME 缺失当场硬失败:path.join('', ...) 会把全部路径相对化到 cwd,
|
|
43
|
+
// 报错文案指向一个毫不相关的目录,排障被带偏
|
|
44
|
+
if (!home) throw new Error('HOME 环境变量未设置——ssh-net 的全部产物/配置路径都挂在 HOME 下,拒绝在相对路径上盲跑');
|
|
40
45
|
const sshNetDir = path.join(home, '.ssh', 'ssh-net');
|
|
41
46
|
const stateDir = path.join(sshNetDir, 'state');
|
|
42
47
|
return Object.freeze({
|
|
@@ -45,31 +50,33 @@ export function machinePaths(env = process.env) {
|
|
|
45
50
|
sshConfigFile: path.join(home, '.ssh', 'config'),
|
|
46
51
|
sshNetDir,
|
|
47
52
|
topologyFile: path.join(sshNetDir, 'topology'),
|
|
48
|
-
// 旧名(2026-10-01 前):topology 缺席时兜底读它,新格式全面铺开后删兼容
|
|
49
|
-
legacyConfigFile: path.join(sshNetDir, 'config'),
|
|
50
53
|
binDir: path.join(sshNetDir, 'bin'),
|
|
51
54
|
runtimeVersionFile: path.join(sshNetDir, 'runtime-version'),
|
|
52
55
|
stateDir,
|
|
53
|
-
// ssh Include 片段目录(design §4.2,2026-10-01):routes
|
|
54
|
-
// 写;edge-<别名> 仅 core _register/休眠写删;node 不建。
|
|
55
|
-
// 零匹配静默,主 config 五台可同份
|
|
56
|
+
// ssh Include 片段目录(design §4.2,2026-10-01):routes、vscode-mark
|
|
57
|
+
// 仅 edge install 写;edge-<别名> 仅 core _register/休眠写删;node 不建。
|
|
58
|
+
// Include glob 零匹配静默,主 config 五台可同份
|
|
56
59
|
confDir: path.join(sshNetDir, 'conf.d'),
|
|
57
60
|
routesFile: path.join(sshNetDir, 'conf.d', 'routes'),
|
|
61
|
+
vscodeMarkFile: path.join(sshNetDir, 'conf.d', 'vscode-mark'),
|
|
58
62
|
/** @param {string} alias edge 别名 */
|
|
59
63
|
edgeHostFile: (alias) => path.join(sshNetDir, 'conf.d', `edge-${alias}`),
|
|
60
|
-
//
|
|
61
|
-
//
|
|
64
|
+
// 两开关(2026-10-02 起,LINK_ON 随 link 退化为纯 ssh 而退役):
|
|
65
|
+
// relay 是根(PathState 盯它)、peer 控报名;文件名与 bash 侧 sn-route、
|
|
66
|
+
// dotfiles Match 硬编码互为契约,改则同步
|
|
62
67
|
switchFiles: Object.freeze({
|
|
63
68
|
relay: path.join(stateDir, 'RELAY_ON'),
|
|
64
69
|
peer: path.join(stateDir, 'PEER_ON'),
|
|
65
|
-
link: path.join(stateDir, 'LINK_ON'),
|
|
66
70
|
}),
|
|
67
|
-
// link
|
|
68
|
-
//
|
|
69
|
-
//
|
|
70
|
-
|
|
71
|
-
|
|
71
|
+
// link 附加件的持久安装意图(2026-10-02):存在=install 对账 link 实体/
|
|
72
|
+
// plist。放树根不放 state/——upgrade 的无参 install 与任何 state 维护都
|
|
73
|
+
// 不能误伤它;只读存在性
|
|
74
|
+
linkEnabledFile: path.join(sshNetDir, 'link.enabled'),
|
|
75
|
+
// PROBE_FAIL:sn-route 的失败印记(内容 epoch,15s TTL)。文件名是
|
|
76
|
+
// paths ↔ sn-route 两处契约(constants.test 锚)
|
|
72
77
|
probeFailFile: path.join(stateDir, 'PROBE_FAIL'),
|
|
78
|
+
// ENSURE:sn-route 按需确保的 single-flight 锁(内容 epoch)
|
|
79
|
+
ensureLockFile: path.join(stateDir, 'ENSURE'),
|
|
73
80
|
edgesDir: path.join(stateDir, 'edges'),
|
|
74
81
|
launchAgentsDir: path.join(home, 'Library', 'LaunchAgents'),
|
|
75
82
|
logsDir: path.join(home, 'Library', 'Logs'),
|
package/libexec/lib/render.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// 渲染唯一来源(design §2.3、§4.2、§6.1、§6.2):
|
|
2
2
|
// - plist/systemd unit:每机差异只以位置参数出现(端口、别名、user、转发列表)
|
|
3
|
-
// - conf.d 片段:routes(edge 选路 Match
|
|
3
|
+
// - conf.d 片段:routes(edge 选路 Match)、vscode-mark(edge 上 VS Code 连接打标)
|
|
4
|
+
// 与 edge-<别名>(core 上的 edge 入口)
|
|
4
5
|
// 纯字符串函数;渲染快照测试钉死输出;结构性断言(XML 闭合、只绑环回)在测试侧另查。
|
|
5
6
|
import { CALLBACK_PORT } from './constants.mjs';
|
|
6
7
|
|
|
@@ -68,38 +69,44 @@ const ROUTES_HEADER = `# 本文件由 \`ssh-net install\` 从 ~/.ssh/ssh-net/top
|
|
|
68
69
|
#
|
|
69
70
|
# Match 必须排在主 config 的 Host 块之前(Include 在文件首行;ssh 首次取到的
|
|
70
71
|
# 值生效)。判据是 edge 根开关 RELAY_ON(只在 edge 存在,core/node 恒不命中)
|
|
71
|
-
# + sn-route
|
|
72
|
-
# core 两块一正一反互斥(Match 不是 if/elif
|
|
73
|
-
# node 无 v6 入站,只有
|
|
72
|
+
# + sn-route 判决:sn-route 在连接发起时按需确保 peer(报名+等回拨,6s 预算,
|
|
73
|
+
# design §2.2)。core 两块一正一反互斥(Match 不是 if/elif;peer 块 show、
|
|
74
|
+
# relay 块 quiet,PROBE_FAIL 印记让两块只做一次确保);node 无 v6 入站,只有
|
|
75
|
+
# relay 兜底。User 不写——由主 config 的 Host 块补。
|
|
74
76
|
`;
|
|
75
77
|
|
|
76
78
|
/**
|
|
77
79
|
* edge 选路片段 conf.d/routes 的唯一渲染来源。值全部来自 topology + 冻结常量,
|
|
78
80
|
* 不出现任何真实 IP(环回口 + relay 别名)。
|
|
79
81
|
* @param {import('./config.mjs').Topology} topo
|
|
82
|
+
* @param {string} selfAlias 本机 edge 规范别名(sn-route 报名 RPC 的自称)
|
|
80
83
|
* @returns {string}
|
|
81
84
|
*/
|
|
82
|
-
export function renderRoutes(topo) {
|
|
85
|
+
export function renderRoutes(topo, selfAlias) {
|
|
83
86
|
const state = '$HOME/.ssh/ssh-net/state';
|
|
84
87
|
const snRoute = '$HOME/.ssh/ssh-net/bin/sn-route';
|
|
85
88
|
/** @type {string[]} */
|
|
86
89
|
const blocks = [];
|
|
87
90
|
|
|
91
|
+
// sn-route argv 直接渲染进 Match(无配置文件中转);peer 块 show(终端可见
|
|
92
|
+
// 进度)、relay 块 quiet(第二次调用零输出)
|
|
93
|
+
const routeArgs = (ui) =>
|
|
94
|
+
`${snRoute} ${selfAlias} ${topo.core.alias} ${topo.relay.alias} '${topo.core.relayPort}' ${topo.core.user} ${ui}`;
|
|
88
95
|
const match = (alias, cond) =>
|
|
89
96
|
`Match originalhost ${alias} exec "test -e ${state}/RELAY_ON${cond}"`;
|
|
90
97
|
|
|
91
|
-
// core:peer 一跳(
|
|
98
|
+
// core:peer 一跳(sn-route 按需确保成功:回拨落在本机 [::1]:10022)
|
|
92
99
|
blocks.push([
|
|
93
100
|
`# ${topo.core.alias}:peer(v6 回拨落在本机 [::1]:${CALLBACK_PORT},全 v6 环回)`,
|
|
94
|
-
match(topo.core.alias, ` && ${
|
|
101
|
+
match(topo.core.alias, ` && ${routeArgs('show')}`),
|
|
95
102
|
' HostName ::1',
|
|
96
103
|
` Port ${CALLBACK_PORT}`,
|
|
97
104
|
` HostKeyAlias ${topo.core.alias}`,
|
|
98
105
|
].join('\n'));
|
|
99
106
|
// core:relay 兜底(与上块互斥:sn-route 失败本块才成立)
|
|
100
107
|
blocks.push([
|
|
101
|
-
`# ${topo.core.alias}:relay
|
|
102
|
-
match(topo.core.alias, ` && ! ${
|
|
108
|
+
`# ${topo.core.alias}:relay 兜底(无 v6/确保失败/预算超时)`,
|
|
109
|
+
match(topo.core.alias, ` && ! ${routeArgs('quiet')}`),
|
|
103
110
|
' HostName 127.0.0.1',
|
|
104
111
|
` Port ${topo.core.relayPort}`,
|
|
105
112
|
` ProxyJump ${topo.relay.alias}`,
|
|
@@ -121,6 +128,27 @@ export function renderRoutes(topo) {
|
|
|
121
128
|
return `${ROUTES_HEADER}\n${blocks.join('\n\n')}\n`;
|
|
122
129
|
}
|
|
123
130
|
|
|
131
|
+
/**
|
|
132
|
+
* VS Code 打标片段 conf.d/vscode-mark 的内容(常量,与 topology 无关):
|
|
133
|
+
* 扩展宿主进程环境有 VSCODE_IPC_HOOK——集成终端里手动 ssh 只有
|
|
134
|
+
* VSCODE_IPC_HOOK_CLI、不命中。SetEnv 借 LC_* 发送通道(服务端默认放行,
|
|
135
|
+
* sshd 不用改)把标识交给落地端 ~/.ssh/rc 维护 dot.via。只在 edge 安装:
|
|
136
|
+
* 其余角色没有「本机发起 VS Code 远程连接」的场景,写了恒不命中。
|
|
137
|
+
* @returns {string}
|
|
138
|
+
*/
|
|
139
|
+
export function renderVscodeMark() {
|
|
140
|
+
return [
|
|
141
|
+
'# 本文件由 `ssh-net install` 维护,勿手改;卸载会删本文件。',
|
|
142
|
+
'#',
|
|
143
|
+
'# 仅 VS Code 发起的连接命中:扩展宿主进程环境有 VSCODE_IPC_HOOK,集成终端里',
|
|
144
|
+
'# 手动 ssh 只有 VSCODE_IPC_HOOK_CLI。SetEnv 借 LC_* 发送通道(服务端默认',
|
|
145
|
+
'# 放行,sshd 不用改)把标识交给落地端 ~/.ssh/rc 维护 dot.via。',
|
|
146
|
+
'Match exec "test -n \\"${VSCODE_IPC_HOOK:-}\\""',
|
|
147
|
+
' SetEnv LC_VSCODE_SSH=1',
|
|
148
|
+
'',
|
|
149
|
+
].join('\n');
|
|
150
|
+
}
|
|
151
|
+
|
|
124
152
|
/**
|
|
125
153
|
* core 上的 edge 在线入口片段 conf.d/edge-<规范别名>。Host 模式给全部别名:
|
|
126
154
|
* 规范名(ssh-net 内部用,如 edge1)在前,给人用的短名(如 e1)在后,
|
|
@@ -141,25 +169,31 @@ Host ${patterns}
|
|
|
141
169
|
|
|
142
170
|
// ---- 三种 LaunchAgent plist ----
|
|
143
171
|
|
|
172
|
+
// link job 的 launchd 重连间隔(2026-10-02):纯 ssh 形态下 ssh 退出即 job
|
|
173
|
+
// 退出,转发口要 2s 内重拉,不用公共的 30s。edge 开着时 relay 恒通、无失败
|
|
174
|
+
// 风暴(关机时整冻结)
|
|
175
|
+
const LINK_THROTTLE = 2;
|
|
176
|
+
|
|
144
177
|
/**
|
|
145
|
-
* edge link job
|
|
146
|
-
* @param {{binDir:string, coreAlias:string,
|
|
147
|
-
* relayAlias:string, relayPort:number, coreUser:string,
|
|
178
|
+
* edge link job(纯 ssh 转发通道):PathState 盯根开关 RELAY_ON,不写 RunAtLoad。
|
|
179
|
+
* @param {{binDir:string, coreAlias:string,
|
|
148
180
|
* forwardArgs:readonly string[], relayOnFile:string, logsDir:string}} opts
|
|
149
|
-
* forwardArgs 是
|
|
150
|
-
*
|
|
151
|
-
* 不再依赖 ~/.ssh/config 里的 <core>-via-relay 内部别名(design §6.1)
|
|
181
|
+
* forwardArgs 是 renderForwardArgs 的最终 ssh 转发参数(-D/-L,目标固定,
|
|
182
|
+
* 无 mode 选族);末尾一个 argv 是 peer 别名,User/HostName 读 Host 块
|
|
152
183
|
*/
|
|
153
184
|
export function renderEdgeLinkPlist(opts) {
|
|
154
|
-
const {
|
|
155
|
-
binDir, coreAlias, selfAlias, relayAlias, relayPort, coreUser,
|
|
156
|
-
forwardArgs, relayOnFile, logsDir,
|
|
157
|
-
} = opts;
|
|
185
|
+
const { binDir, coreAlias, forwardArgs, relayOnFile, logsDir } = opts;
|
|
158
186
|
const label = linkLabel(coreAlias);
|
|
187
|
+
// argv 完全渲染:软链 → -N(无远程命令)→ 转发口 → 选项 → peer
|
|
159
188
|
const argv = [
|
|
160
189
|
`${binDir}/${linkLinkName(coreAlias)}`,
|
|
161
|
-
|
|
190
|
+
'-N',
|
|
162
191
|
...forwardArgs,
|
|
192
|
+
'-o', 'ExitOnForwardFailure=yes', '-o', 'BatchMode=yes',
|
|
193
|
+
'-o', 'IdentitiesOnly=yes', '-o', 'ControlMaster=no', '-o', 'ControlPath=none',
|
|
194
|
+
'-o', 'ServerAliveInterval=35', '-o', 'ServerAliveCountMax=3',
|
|
195
|
+
'-o', 'ConnectTimeout=15', '-o', 'StrictHostKeyChecking=accept-new',
|
|
196
|
+
coreAlias,
|
|
163
197
|
];
|
|
164
198
|
return `${XML_HEAD} <key>Label</key>
|
|
165
199
|
<string>${label}</string>
|
|
@@ -174,7 +208,7 @@ ${programArguments(argv)} <key>ProcessType</key>
|
|
|
174
208
|
</dict>
|
|
175
209
|
</dict>
|
|
176
210
|
<key>ThrottleInterval</key>
|
|
177
|
-
<integer>${
|
|
211
|
+
<integer>${LINK_THROTTLE}</integer>
|
|
178
212
|
${logPaths({ label, logsDir })}${XML_TAIL}`;
|
|
179
213
|
}
|
|
180
214
|
|
|
@@ -202,12 +236,13 @@ ${logPaths({ label, logsDir })}${XML_TAIL}`;
|
|
|
202
236
|
/**
|
|
203
237
|
* core 回拨 edge:失败计数与休眠在脚本里,launchd 只在非自愿退出时重拉。
|
|
204
238
|
* 无 RunAtLoad;6 连败由脚本 disable + exit 0。
|
|
205
|
-
* @param {{binDir:string, edgeAlias:string, user:string, logsDir:string}} opts
|
|
239
|
+
* @param {{binDir:string, edgeAlias:string, user:string, coreAlias:string, logsDir:string}} opts
|
|
240
|
+
* coreAlias 透传给脚本→LocalCommand→sn-signal→edge 的通知/link kickstart
|
|
206
241
|
*/
|
|
207
242
|
export function renderUplinkEdgePlist(opts) {
|
|
208
|
-
const { binDir, edgeAlias, user, logsDir } = opts;
|
|
243
|
+
const { binDir, edgeAlias, user, coreAlias, logsDir } = opts;
|
|
209
244
|
const label = edgeLabel(edgeAlias);
|
|
210
|
-
const argv = [`${binDir}/${edgeLinkName(edgeAlias)}`, edgeAlias, user];
|
|
245
|
+
const argv = [`${binDir}/${edgeLinkName(edgeAlias)}`, edgeAlias, user, coreAlias];
|
|
211
246
|
return `${XML_HEAD} <key>Label</key>
|
|
212
247
|
<string>${label}</string>
|
|
213
248
|
${programArguments(argv)} <key>ProcessType</key>
|
package/libexec/lib/runtime.mjs
CHANGED
|
@@ -7,7 +7,6 @@
|
|
|
7
7
|
// - 运行时零 npm/fnm 依赖;包升级后重跑 install 刷新(core 侧 uplink-edge
|
|
8
8
|
// 由 checkin 每次报名自动对账),runtime-version 戳记安装版本,status 见
|
|
9
9
|
// 版本差即报
|
|
10
|
-
// - 旧版带机器名实体/旧工具名(peer-probe 等)在 ensureRuntime 时静默清理
|
|
11
10
|
import fs from 'node:fs';
|
|
12
11
|
import path from 'node:path';
|
|
13
12
|
import { packageScriptPath } from './paths.mjs';
|
|
@@ -15,10 +14,6 @@ import pkg from '../../package.json' with { type: 'json' };
|
|
|
15
14
|
|
|
16
15
|
export const RUNTIME_VERSION = pkg.version;
|
|
17
16
|
|
|
18
|
-
// 26.9.30 改名前的 bin/ 遗留(旧版实体拷贝,新版永不生成;见一个删一个,
|
|
19
|
-
// 两版之后可移除本清单)
|
|
20
|
-
const LEGACY_BIN = ['peer-probe', 'v6-address', 'peer-up', 'notify-edge', 'notify-user', 'sn-v6'];
|
|
21
|
-
|
|
22
17
|
/**
|
|
23
18
|
* 拷贝单脚本到 bin/(实体)。
|
|
24
19
|
* @param {string} dstPath
|
|
@@ -31,7 +26,7 @@ export function ensureScript(dstPath, srcPath) {
|
|
|
31
26
|
try {
|
|
32
27
|
cur = fs.readFileSync(dstPath);
|
|
33
28
|
} catch (err) {
|
|
34
|
-
if (
|
|
29
|
+
if (err.code !== 'ENOENT') throw err;
|
|
35
30
|
}
|
|
36
31
|
if (cur !== null && cur.equals(src)) {
|
|
37
32
|
// 内容对但可执行位丢了(cp/同步工具吃掉 mode):补上
|
|
@@ -59,7 +54,7 @@ export function ensureLink(linkPath, entityName) {
|
|
|
59
54
|
try {
|
|
60
55
|
st = fs.lstatSync(linkPath);
|
|
61
56
|
} catch (err) {
|
|
62
|
-
if (
|
|
57
|
+
if (err.code !== 'ENOENT') throw err;
|
|
63
58
|
}
|
|
64
59
|
if (st !== null && st.isSymbolicLink()) {
|
|
65
60
|
if (fs.readlinkSync(linkPath) === entityName) return 'unchanged';
|
|
@@ -87,9 +82,6 @@ export function ensureRuntime(ctx, entries, links = []) {
|
|
|
87
82
|
for (const [name, entityName] of links) {
|
|
88
83
|
results.push(ensureLink(path.join(ctx.paths.binDir, name), entityName));
|
|
89
84
|
}
|
|
90
|
-
for (const legacy of LEGACY_BIN) {
|
|
91
|
-
fs.rmSync(path.join(ctx.paths.binDir, legacy), { force: true });
|
|
92
|
-
}
|
|
93
85
|
refreshStamp(ctx);
|
|
94
86
|
return results;
|
|
95
87
|
}
|
|
@@ -120,7 +112,7 @@ export function auditScript(dstPath, srcPath) {
|
|
|
120
112
|
try {
|
|
121
113
|
cur = fs.readFileSync(dstPath);
|
|
122
114
|
} catch (err) {
|
|
123
|
-
if (
|
|
115
|
+
if (err.code !== 'ENOENT') throw err;
|
|
124
116
|
return 'missing';
|
|
125
117
|
}
|
|
126
118
|
return cur.equals(fs.readFileSync(srcPath)) ? 'ok' : 'stale';
|
package/libexec/lib/state.mjs
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
// 易变状态的全部 IO(
|
|
2
|
-
//(RELAY_ON 是根、PEER_ON
|
|
3
|
-
//(最近上报的 v6),以及
|
|
4
|
-
//
|
|
5
|
-
//
|
|
1
|
+
// 易变状态的全部 IO(2026-10-02 改):state 目录放两开关空标记
|
|
2
|
+
//(RELAY_ON 是根、PEER_ON 控报名;LINK_ON 已退役)、edges/<别名>
|
|
3
|
+
//(最近上报的 v6),以及 sn-route 的失败印记 PROBE_FAIL(epoch、15s TTL)
|
|
4
|
+
// 与按需确保锁 ENSURE(design §2.2)。开关由控制面置删,印记/锁由 sn-route
|
|
5
|
+
// 维护,mjs 侧只读。
|
|
6
6
|
// 目录 0700、文件 0600,写 v6 走 tmp+rename 防 checkin 与 uplink-edge 读到半截。
|
|
7
7
|
import fs from 'node:fs';
|
|
8
8
|
import path from 'node:path';
|
|
9
9
|
|
|
10
|
-
const SWITCH_NAMES = ['relay', 'peer'
|
|
10
|
+
const SWITCH_NAMES = ['relay', 'peer'];
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* @typedef {Object} StateStore
|
|
14
|
-
* @property {(name: 'relay'|'peer'
|
|
15
|
-
* @property {(names: Array<'relay'|'peer'
|
|
16
|
-
* @property {(names: Array<'relay'|'peer'
|
|
17
|
-
* @property {() => {relay: boolean, peer: boolean
|
|
14
|
+
* @property {(name: 'relay'|'peer') => boolean} isOn
|
|
15
|
+
* @property {(names: Array<'relay'|'peer'>) => void} setOn 幂等 touch
|
|
16
|
+
* @property {(names: Array<'relay'|'peer'>) => void} removeOn 幂等删除
|
|
17
|
+
* @property {() => {relay: boolean, peer: boolean}} readSwitches
|
|
18
18
|
* @property {(alias: string) => string | null} readEdgeV6
|
|
19
19
|
* @property {(alias: string, v6: string) => void} writeEdgeV6
|
|
20
20
|
* @property {(alias: string) => void} removeEdgeV6 不存在时静默
|
|
@@ -30,7 +30,6 @@ export function createStateStore(stateDir) {
|
|
|
30
30
|
const files = {
|
|
31
31
|
relay: path.join(stateDir, 'RELAY_ON'),
|
|
32
32
|
peer: path.join(stateDir, 'PEER_ON'),
|
|
33
|
-
link: path.join(stateDir, 'LINK_ON'),
|
|
34
33
|
};
|
|
35
34
|
|
|
36
35
|
const ensureState = () => {
|
|
@@ -75,7 +74,6 @@ export function createStateStore(stateDir) {
|
|
|
75
74
|
return {
|
|
76
75
|
relay: fs.existsSync(files.relay),
|
|
77
76
|
peer: fs.existsSync(files.peer),
|
|
78
|
-
link: fs.existsSync(files.link),
|
|
79
77
|
};
|
|
80
78
|
},
|
|
81
79
|
|