ssh-net 26.10.28 → 26.10.31
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 +246 -9
- package/README.md +56 -35
- package/libexec/cli.mjs +12 -24
- package/libexec/commands/doctor.mjs +19 -34
- package/libexec/commands/install.mjs +90 -61
- package/libexec/commands/register.mjs +241 -0
- package/libexec/commands/restart.mjs +7 -3
- package/libexec/commands/start.mjs +23 -53
- package/libexec/commands/status.mjs +94 -93
- package/libexec/commands/stop.mjs +19 -33
- package/libexec/commands/uninstall.mjs +1 -0
- package/libexec/commands/upgrade.mjs +14 -8
- package/libexec/lib/conf.mjs +1 -1
- package/libexec/lib/config.mjs +49 -81
- package/libexec/lib/exec.mjs +2 -8
- package/libexec/lib/identity.mjs +1 -1
- package/libexec/lib/ipv6.mjs +6 -5
- package/libexec/lib/jobctl.mjs +8 -7
- package/libexec/lib/launchd.mjs +5 -2
- package/libexec/lib/manage.mjs +12 -4
- package/libexec/lib/paths.mjs +19 -14
- package/libexec/lib/probe.mjs +0 -17
- package/libexec/lib/render.mjs +33 -21
- package/libexec/lib/runtime.mjs +22 -14
- package/libexec/lib/state.mjs +17 -20
- package/libexec/sn-ipv6 +6 -6
- package/libexec/sn-route +229 -65
- package/libexec/ssh-net-link-core +13 -474
- package/libexec/ssh-net-uplink-edge +15 -14
- package/package.json +1 -1
- package/libexec/commands/checkin.mjs +0 -210
- package/libexec/sn-notify +0 -45
- package/libexec/sn-signal +0 -57
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
|
/**
|
|
@@ -36,13 +35,12 @@ const BIND = '127.0.0.1'; // 所有转发强制只绑环回(design §6.5)
|
|
|
36
35
|
* @property {string} user
|
|
37
36
|
*
|
|
38
37
|
* @typedef {Object} Forward
|
|
39
|
-
* @property {'dynamic'|'local'} kind
|
|
40
38
|
* @property {string} bind 恒 127.0.0.1
|
|
41
39
|
* @property {number} listenPort
|
|
42
|
-
* @property {string}
|
|
43
|
-
* 'loopback6'=v6
|
|
44
|
-
*
|
|
45
|
-
* @property {number}
|
|
40
|
+
* @property {string} targetAlias 目标别名:'loopback'=落地机 v4 环回、
|
|
41
|
+
* 'loopback6'=v6 环回(固定 [::1]),其余=主机别名(渲染时查
|
|
42
|
+
* ~/.ssh/config Host 块的 HostName)
|
|
43
|
+
* @property {number} targetPort
|
|
46
44
|
*
|
|
47
45
|
* @typedef {Object} Topology
|
|
48
46
|
* @property {RelayRow} relay
|
|
@@ -83,21 +81,17 @@ function parseAliasList(tok, lineNo) {
|
|
|
83
81
|
|
|
84
82
|
/**
|
|
85
83
|
* @param {string} text
|
|
86
|
-
* @param {{allowLegacyLanIp?: boolean}} [opts] 兼容 2026-10-01 前的旧 7 列
|
|
87
|
-
* 格式(多一列 LAN_IP):仅在读旧名 `config` 兜底时传 true;该列只校验、
|
|
88
|
-
* 不进 HostRow。五台切换完成后连同文件名兜底一起删
|
|
89
84
|
* @returns {Topology}
|
|
90
85
|
*/
|
|
91
|
-
export function parseTopology(text
|
|
86
|
+
export function parseTopology(text) {
|
|
92
87
|
if (typeof text !== 'string' || text.trim().length === 0)
|
|
93
88
|
throw new ConfigError(0, 'topology 为空');
|
|
94
|
-
const allowLegacy = opts.allowLegacyLanIp === true;
|
|
95
89
|
|
|
96
90
|
/** @type {RelayRow|null} */
|
|
97
91
|
let relay = null;
|
|
98
92
|
/** @type {HostRow[]} */
|
|
99
93
|
const hosts = [];
|
|
100
|
-
/** @type {{
|
|
94
|
+
/** @type {{cols:string[], line:number}[]} */
|
|
101
95
|
const rawForwards = [];
|
|
102
96
|
|
|
103
97
|
const lines = text.split('\n');
|
|
@@ -121,24 +115,12 @@ export function parseTopology(text, opts = {}) {
|
|
|
121
115
|
}
|
|
122
116
|
|
|
123
117
|
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 {
|
|
118
|
+
if (cols.length !== 6)
|
|
137
119
|
throw new ConfigError(
|
|
138
120
|
lineNo,
|
|
139
121
|
'host 需要 6 列:host <角色> <主机名> <别名[,附加名…]> <relay端口|-> <用户>',
|
|
140
122
|
);
|
|
141
|
-
|
|
123
|
+
const [, role, hostname, aliasTok, portTok, user] = cols;
|
|
142
124
|
if (!ROLES.has(role)) throw new ConfigError(lineNo, `未知角色:${role}(只允许 core/edge/node)`);
|
|
143
125
|
if (!ALIAS_RE.test(hostname)) throw new ConfigError(lineNo, `主机名非法:${hostname}`);
|
|
144
126
|
const aliases = parseAliasList(aliasTok, lineNo);
|
|
@@ -149,14 +131,6 @@ export function parseTopology(text, opts = {}) {
|
|
|
149
131
|
if (role !== 'edge' && aliases.length > 1)
|
|
150
132
|
throw new ConfigError(lineNo, `只有 edge 行支持附加名(core 侧入口渲染用):${aliasTok}`);
|
|
151
133
|
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
134
|
/** @type {number|null} */
|
|
161
135
|
let relayPort = null;
|
|
162
136
|
if (role === 'edge') {
|
|
@@ -173,14 +147,13 @@ export function parseTopology(text, opts = {}) {
|
|
|
173
147
|
}
|
|
174
148
|
|
|
175
149
|
if (head === 'forward') {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
if (kind === 'local' && cols.length !== 5)
|
|
150
|
+
if (cols[1] === 'dynamic')
|
|
151
|
+
throw new ConfigError(lineNo, 'forward dynamic 已弃用(macOS 27 崩 sshd-session,见 notes):用 forward local 配 microsocks');
|
|
152
|
+
if (cols[1] !== 'local')
|
|
153
|
+
throw new ConfigError(lineNo, `forward 类型只允许 local:${cols[1] ?? '(缺)'}`);
|
|
154
|
+
if (cols.length !== 5)
|
|
182
155
|
throw new ConfigError(lineNo, 'forward local 需要 5 列:forward local <本地端口> <目标别名> <目标端口>');
|
|
183
|
-
rawForwards.push({
|
|
156
|
+
rawForwards.push({ cols, line: lineNo });
|
|
184
157
|
return;
|
|
185
158
|
}
|
|
186
159
|
|
|
@@ -213,7 +186,7 @@ export function parseTopology(text, opts = {}) {
|
|
|
213
186
|
const forwards = [];
|
|
214
187
|
/** @type {Set<number>} */
|
|
215
188
|
const listenPorts = new Set();
|
|
216
|
-
for (const {
|
|
189
|
+
for (const { cols, line } of rawForwards) {
|
|
217
190
|
const listenPort = parsePort(cols[2]);
|
|
218
191
|
if (!Number.isInteger(listenPort) || listenPort < 1 || listenPort > 65535)
|
|
219
192
|
throw new ConfigError(line, `本地端口非法:${cols[2]}`);
|
|
@@ -221,28 +194,24 @@ export function parseTopology(text, opts = {}) {
|
|
|
221
194
|
throw new ConfigError(line, `转发本地端口重复:${listenPort}`);
|
|
222
195
|
listenPorts.add(listenPort);
|
|
223
196
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
throw new ConfigError(line, `forward local 指向未知别名:${targetAlias}`);
|
|
241
|
-
if (target.role === 'edge')
|
|
242
|
-
throw new ConfigError(line, `forward local 目标 ${targetAlias} 是 edge(edge 无注册地址,不能做转发目标)`);
|
|
243
|
-
}
|
|
244
|
-
forwards.push({ kind, bind: BIND, listenPort, targetAlias, targetPort });
|
|
197
|
+
const targetAlias = cols[3];
|
|
198
|
+
const targetPort = parsePort(cols[4]);
|
|
199
|
+
if (!Number.isInteger(targetPort) || targetPort < 1 || targetPort > 65535)
|
|
200
|
+
throw new ConfigError(line, `目标端口非法:${cols[4]}`);
|
|
201
|
+
// 环回别名:目标服务在 ssh 落地机的本机环回上,不是主机名、无 Host 块:
|
|
202
|
+
// loopback = 127.0.0.1(microsocks 只听 v4)
|
|
203
|
+
// loopback6 = [::1](renderForwardArgs 固定 v6——relay 是 ProxyJump
|
|
204
|
+
// 对 -L 透明,转发始终落地 core 本机)
|
|
205
|
+
// 具名别名(core/node)的 HostName 来自 ~/.ssh/config Host 块,
|
|
206
|
+
// renderForwardArgs 渲染时查 hostTable——topology 不存任何 IP。
|
|
207
|
+
if (targetAlias !== 'loopback' && targetAlias !== 'loopback6') {
|
|
208
|
+
const target = byAlias.get(targetAlias);
|
|
209
|
+
if (!target)
|
|
210
|
+
throw new ConfigError(line, `forward local 指向未知别名:${targetAlias}`);
|
|
211
|
+
if (target.role === 'edge')
|
|
212
|
+
throw new ConfigError(line, `forward local 目标 ${targetAlias} 是 edge(edge 无注册地址,不能做转发目标)`);
|
|
245
213
|
}
|
|
214
|
+
forwards.push({ bind: BIND, listenPort, targetAlias, targetPort });
|
|
246
215
|
}
|
|
247
216
|
|
|
248
217
|
return Object.freeze({
|
|
@@ -286,35 +255,34 @@ export function validateHostContract(topo, hostTable) {
|
|
|
286
255
|
}
|
|
287
256
|
|
|
288
257
|
/**
|
|
289
|
-
*
|
|
290
|
-
*
|
|
291
|
-
*
|
|
292
|
-
*
|
|
293
|
-
|
|
294
|
-
* bind 不进规格(link-core 恒用 127.0.0.1)。规格冒号分隔,target 不支持
|
|
295
|
-
* v6 字面量(v6 环回走 loopback6 关键字)。
|
|
258
|
+
* 转发渲染成最终 ssh argv(link 是纯 ssh 通道、无脚本端解析):
|
|
259
|
+
* `-L 127.0.0.1:<listenPort>:<target>:<targetPort>`
|
|
260
|
+
* target:loopback=127.0.0.1;loopback6 固定 `[::1]`——relay 是 ProxyJump、
|
|
261
|
+
* 对 -L 透明,转发始终由 core sshd 在 core 本机发起;具名别名查 hostTable
|
|
262
|
+
*(~/.ssh/config Host 块的 HostName,IP 唯一来源)。
|
|
296
263
|
* @param {readonly Forward[]} forwards
|
|
297
264
|
* @param {Map<string, {hostname?: string}>} hostTable parseStaticHosts 的产物
|
|
298
|
-
* @returns {string[]}
|
|
265
|
+
* @returns {string[]} 扁平 argv(['-L','127.0.0.1:..', ...])
|
|
299
266
|
*/
|
|
300
|
-
export function
|
|
267
|
+
export function renderForwardArgs(forwards, hostTable) {
|
|
301
268
|
/** @type {string[]} */
|
|
302
|
-
const
|
|
269
|
+
const args = [];
|
|
303
270
|
for (const f of forwards) {
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
271
|
+
/** @type {string} */
|
|
272
|
+
let target;
|
|
273
|
+
if (f.targetAlias === 'loopback6') {
|
|
274
|
+
target = '[::1]';
|
|
308
275
|
} else if (f.targetAlias === 'loopback') {
|
|
309
|
-
|
|
276
|
+
target = '127.0.0.1';
|
|
310
277
|
} else {
|
|
311
278
|
const host = hostTable.get(f.targetAlias);
|
|
312
279
|
// 语法解析时只校验别名在 topology;此处才查 ssh config——install 时
|
|
313
280
|
// 暴露,错误直指缺 Host 块的那台机器
|
|
314
281
|
if (!host || !host.hostname)
|
|
315
282
|
throw new Error(`forward local 目标 ${f.targetAlias} 在 ~/.ssh/config 缺 Host 块或 HostName`);
|
|
316
|
-
|
|
283
|
+
target = host.hostname;
|
|
317
284
|
}
|
|
285
|
+
args.push('-L', `127.0.0.1:${f.listenPort}:${target}:${f.targetPort}`);
|
|
318
286
|
}
|
|
319
|
-
return
|
|
287
|
+
return args;
|
|
320
288
|
}
|
package/libexec/lib/exec.mjs
CHANGED
|
@@ -15,12 +15,8 @@ const DEFAULTS = {
|
|
|
15
15
|
ps: '/bin/ps',
|
|
16
16
|
};
|
|
17
17
|
|
|
18
|
-
/**
|
|
19
|
-
|
|
20
|
-
* @param {NodeJS.ProcessEnv} [env]
|
|
21
|
-
* @returns {string}
|
|
22
|
-
*/
|
|
23
|
-
export function commandPath(key, env = process.env) {
|
|
18
|
+
/** 命令绝对路径;测试用 SSH_NET_<KEY> 覆盖(如 SSH_NET_LAUNCHCTL)。 */
|
|
19
|
+
function commandPath(key, env = process.env) {
|
|
24
20
|
const override = env[`SSH_NET_${key.replaceAll(/([a-z])(?=[A-Z])/g, '$1_').toUpperCase()}`];
|
|
25
21
|
return override || DEFAULTS[key] || key;
|
|
26
22
|
}
|
|
@@ -36,7 +32,6 @@ export function commandPath(key, env = process.env) {
|
|
|
36
32
|
|
|
37
33
|
/**
|
|
38
34
|
* @typedef {Object} Runner
|
|
39
|
-
* @property {(key: string) => string} path
|
|
40
35
|
* @property {(key: string, args?: string[], opts?: object) => RunResult} run
|
|
41
36
|
* @property {(file: string, args?: string[], opts?: object) => RunResult} runRaw
|
|
42
37
|
* @property {(key: string, args?: string[], opts?: object) => Promise<RunResult>} runAsync
|
|
@@ -79,7 +74,6 @@ export function createRunner(env = process.env) {
|
|
|
79
74
|
resolve({ code, signal, stdout, stderr, error: null }));
|
|
80
75
|
});
|
|
81
76
|
return {
|
|
82
|
-
path: (key) => commandPath(key, env),
|
|
83
77
|
run: (key, args, opts) => doSpawn(commandPath(key, env), args, opts),
|
|
84
78
|
runRaw: (file, args, opts) => doSpawn(file, args, opts),
|
|
85
79
|
runAsync: (key, args, opts) => doSpawnAsync(commandPath(key, env), args, opts),
|
package/libexec/lib/identity.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// 本机身份自取(design §5):os.hostname() 砍掉首个 `.` 及后缀,
|
|
2
2
|
// 与 dotfiles lib/host.sh 的 DOT_HOST 同一算法,再与 topology 主机名列精确匹配。
|
|
3
|
-
// 不读 DOT_HOST 环境变量——它只在交互 shell 链 export
|
|
3
|
+
// 不读 DOT_HOST 环境变量——它只在交互 shell 链 export,报名 RPC 这类非交互
|
|
4
4
|
// ssh 远程命令里不存在。
|
|
5
5
|
import os from 'node:os';
|
|
6
6
|
|
package/libexec/lib/ipv6.mjs
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
//
|
|
2
|
-
// 可拨号段——2000::/3 公网单播,或 fc00::/7 ULA
|
|
3
|
-
//
|
|
4
|
-
//
|
|
1
|
+
// 报名 v6 入参校验(design §6.3,_register 是唯一外部输入面):合法 IPv6
|
|
2
|
+
// 字面值 + 可拨号段——2000::/3 公网单播,或 fc00::/7 ULA。收 ULA 是给
|
|
3
|
+
// sn-ipv6 的 SSH_NET_V6_ALLOW_ULA 测试开关(模拟纯 ULA 网络的喂参测试)
|
|
4
|
+
// 兜底:校验面不能比选举面更严,否则测试造出的 ULA 报名被 core 拒;
|
|
5
|
+
// 生产 edge 的 sn-ipv6 默认只选公网段,不会报 ULA。
|
|
5
6
|
// 仍拒绝 scope id、内嵌 v4(含 ::ffff: v4-mapped)、link-local、unspecified、
|
|
6
7
|
// 组播等。依赖 net.isIP 做字面值合法性,首 hextet 判段。
|
|
7
8
|
import net from 'node:net';
|
|
@@ -35,7 +36,7 @@ export function isDialableV6(value) {
|
|
|
35
36
|
}
|
|
36
37
|
|
|
37
38
|
/**
|
|
38
|
-
*
|
|
39
|
+
* 给报名 RPC 报错用的中文原因;合法返回 null。
|
|
39
40
|
* @param {unknown} value
|
|
40
41
|
* @returns {string | null}
|
|
41
42
|
*/
|
package/libexec/lib/jobctl.mjs
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
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
|
|
|
7
|
-
const TERM_BUDGET_MS = 40_000; // 覆盖 TERM 落在
|
|
8
|
+
const TERM_BUDGET_MS = 40_000; // 覆盖 TERM 落在 start peer 前台报名 ssh 的最坏 ~35s
|
|
8
9
|
const POLL_MS = 500;
|
|
9
10
|
|
|
10
11
|
function sleepMs(ms) {
|
|
@@ -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
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
// install
|
|
1
|
+
// install 与报名(_register)共享的落地原语:plist 原子落盘 + launchctl 注册态同步
|
|
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,10 +5,11 @@
|
|
|
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
|
-
// bin/ 包脚本实体 + 服务软链(install
|
|
11
|
-
// state/
|
|
10
|
+
// bin/ 包脚本实体 + 服务软链(install/报名对账)
|
|
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 渲染)
|
|
14
15
|
// vscode-mark edge:VS Code 连接打标块(install 渲染常量)
|
|
@@ -23,7 +24,7 @@ const LIB_REAL = fs.realpathSync(fileURLToPath(import.meta.url));
|
|
|
23
24
|
const LIBEXEC_REAL = path.dirname(path.dirname(LIB_REAL));
|
|
24
25
|
|
|
25
26
|
/**
|
|
26
|
-
* 包内脚本的实体路径(install
|
|
27
|
+
* 包内脚本的实体路径(install/报名拷贝源)。
|
|
27
28
|
* @param {string} scriptName libexec 下脚本名,如 ssh-net-link-core
|
|
28
29
|
* @returns {string}
|
|
29
30
|
*/
|
|
@@ -38,6 +39,9 @@ export function packageScriptPath(scriptName) {
|
|
|
38
39
|
*/
|
|
39
40
|
export function machinePaths(env = process.env) {
|
|
40
41
|
const home = env.HOME ?? '';
|
|
42
|
+
// HOME 缺失当场硬失败:path.join('', ...) 会把全部路径相对化到 cwd,
|
|
43
|
+
// 报错文案指向一个毫不相关的目录,排障被带偏
|
|
44
|
+
if (!home) throw new Error('HOME 环境变量未设置——ssh-net 的全部产物/配置路径都挂在 HOME 下,拒绝在相对路径上盲跑');
|
|
41
45
|
const sshNetDir = path.join(home, '.ssh', 'ssh-net');
|
|
42
46
|
const stateDir = path.join(sshNetDir, 'state');
|
|
43
47
|
return Object.freeze({
|
|
@@ -46,8 +50,6 @@ export function machinePaths(env = process.env) {
|
|
|
46
50
|
sshConfigFile: path.join(home, '.ssh', 'config'),
|
|
47
51
|
sshNetDir,
|
|
48
52
|
topologyFile: path.join(sshNetDir, 'topology'),
|
|
49
|
-
// 旧名(2026-10-01 前):topology 缺席时兜底读它,新格式全面铺开后删兼容
|
|
50
|
-
legacyConfigFile: path.join(sshNetDir, 'config'),
|
|
51
53
|
binDir: path.join(sshNetDir, 'bin'),
|
|
52
54
|
runtimeVersionFile: path.join(sshNetDir, 'runtime-version'),
|
|
53
55
|
stateDir,
|
|
@@ -59,19 +61,22 @@ export function machinePaths(env = process.env) {
|
|
|
59
61
|
vscodeMarkFile: path.join(sshNetDir, 'conf.d', 'vscode-mark'),
|
|
60
62
|
/** @param {string} alias edge 别名 */
|
|
61
63
|
edgeHostFile: (alias) => path.join(sshNetDir, 'conf.d', `edge-${alias}`),
|
|
62
|
-
//
|
|
63
|
-
//
|
|
64
|
+
// 两开关(2026-10-02 起,LINK_ON 随 link 退化为纯 ssh 而退役):
|
|
65
|
+
// relay 是根(PathState 盯它)、peer 控报名;文件名与 bash 侧 sn-route、
|
|
66
|
+
// dotfiles Match 硬编码互为契约,改则同步
|
|
64
67
|
switchFiles: Object.freeze({
|
|
65
68
|
relay: path.join(stateDir, 'RELAY_ON'),
|
|
66
69
|
peer: path.join(stateDir, 'PEER_ON'),
|
|
67
|
-
link: path.join(stateDir, 'LINK_ON'),
|
|
68
70
|
}),
|
|
69
|
-
// link
|
|
70
|
-
//
|
|
71
|
-
//
|
|
72
|
-
|
|
73
|
-
|
|
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 锚)
|
|
74
77
|
probeFailFile: path.join(stateDir, 'PROBE_FAIL'),
|
|
78
|
+
// ENSURE:sn-route 按需确保的 single-flight 锁(内容 epoch)
|
|
79
|
+
ensureLockFile: path.join(stateDir, 'ENSURE'),
|
|
75
80
|
edgesDir: path.join(stateDir, 'edges'),
|
|
76
81
|
launchAgentsDir: path.join(home, 'Library', 'LaunchAgents'),
|
|
77
82
|
logsDir: path.join(home, 'Library', 'Logs'),
|
package/libexec/lib/probe.mjs
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
// - 本地转发口(-D/-L)的持口者是 link bash 的 ssh 子进程,要用 ps 父子表判后代
|
|
3
3
|
// - 10022 的持口者永远是本机 sshd(-R 服务端持口),无法按 pid 溯源,
|
|
4
4
|
// 只能以 sn-probe 是否读到 SSH banner 认定是不是本服务
|
|
5
|
-
import fs from 'node:fs';
|
|
6
5
|
import net from 'node:net';
|
|
7
6
|
import path from 'node:path';
|
|
8
7
|
|
|
@@ -135,19 +134,3 @@ export async function peerProbeOk(runner, binDir, timeoutMs = 5000) {
|
|
|
135
134
|
const r = await runner.runRawAsync(path.join(binDir, 'sn-probe'), [], { timeout: timeoutMs });
|
|
136
135
|
return r.code === 0;
|
|
137
136
|
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* 探测 terminal-notifier(线路通知组件,2026-09-30 用户定引入)。brew 的
|
|
141
|
-
* 两个安装前缀都查;返回可执行路径或 null。mac 专属提示用——sn-notify
|
|
142
|
-
* 运行时自己也有同一份探测(bash 侧),两边各自维护不共享。
|
|
143
|
-
* @returns {string|null}
|
|
144
|
-
*/
|
|
145
|
-
export function detectTerminalNotifier() {
|
|
146
|
-
for (const p of ['/opt/homebrew/bin/terminal-notifier', '/usr/local/bin/terminal-notifier']) {
|
|
147
|
-
try {
|
|
148
|
-
fs.accessSync(p, fs.constants.X_OK);
|
|
149
|
-
return p;
|
|
150
|
-
} catch { /* 试下一个 */ }
|
|
151
|
-
}
|
|
152
|
-
return null;
|
|
153
|
-
}
|
package/libexec/lib/render.mjs
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
// 纯字符串函数;渲染快照测试钉死输出;结构性断言(XML 闭合、只绑环回)在测试侧另查。
|
|
6
6
|
import { CALLBACK_PORT } from './constants.mjs';
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
const THROTTLE = 30; // launchd ThrottleInterval / 对应 systemd RestartSec
|
|
9
9
|
|
|
10
10
|
/** @param {string} s */
|
|
11
11
|
function xmlEscape(s) {
|
|
@@ -69,38 +69,44 @@ const ROUTES_HEADER = `# 本文件由 \`ssh-net install\` 从 ~/.ssh/ssh-net/top
|
|
|
69
69
|
#
|
|
70
70
|
# Match 必须排在主 config 的 Host 块之前(Include 在文件首行;ssh 首次取到的
|
|
71
71
|
# 值生效)。判据是 edge 根开关 RELAY_ON(只在 edge 存在,core/node 恒不命中)
|
|
72
|
-
# + sn-route
|
|
73
|
-
# core 两块一正一反互斥(Match 不是 if/elif
|
|
74
|
-
# 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 块补。
|
|
75
76
|
`;
|
|
76
77
|
|
|
77
78
|
/**
|
|
78
79
|
* edge 选路片段 conf.d/routes 的唯一渲染来源。值全部来自 topology + 冻结常量,
|
|
79
80
|
* 不出现任何真实 IP(环回口 + relay 别名)。
|
|
80
81
|
* @param {import('./config.mjs').Topology} topo
|
|
82
|
+
* @param {string} selfAlias 本机 edge 规范别名(sn-route 报名 RPC 的自称)
|
|
81
83
|
* @returns {string}
|
|
82
84
|
*/
|
|
83
|
-
export function renderRoutes(topo) {
|
|
85
|
+
export function renderRoutes(topo, selfAlias) {
|
|
84
86
|
const state = '$HOME/.ssh/ssh-net/state';
|
|
85
87
|
const snRoute = '$HOME/.ssh/ssh-net/bin/sn-route';
|
|
86
88
|
/** @type {string[]} */
|
|
87
89
|
const blocks = [];
|
|
88
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}`;
|
|
89
95
|
const match = (alias, cond) =>
|
|
90
96
|
`Match originalhost ${alias} exec "test -e ${state}/RELAY_ON${cond}"`;
|
|
91
97
|
|
|
92
|
-
// core:peer 一跳(
|
|
98
|
+
// core:peer 一跳(sn-route 按需确保成功:回拨落在本机 [::1]:10022)
|
|
93
99
|
blocks.push([
|
|
94
100
|
`# ${topo.core.alias}:peer(v6 回拨落在本机 [::1]:${CALLBACK_PORT},全 v6 环回)`,
|
|
95
|
-
match(topo.core.alias, ` && ${
|
|
101
|
+
match(topo.core.alias, ` && ${routeArgs('show')}`),
|
|
96
102
|
' HostName ::1',
|
|
97
103
|
` Port ${CALLBACK_PORT}`,
|
|
98
104
|
` HostKeyAlias ${topo.core.alias}`,
|
|
99
105
|
].join('\n'));
|
|
100
106
|
// core:relay 兜底(与上块互斥:sn-route 失败本块才成立)
|
|
101
107
|
blocks.push([
|
|
102
|
-
`# ${topo.core.alias}:relay
|
|
103
|
-
match(topo.core.alias, ` && ! ${
|
|
108
|
+
`# ${topo.core.alias}:relay 兜底(无 v6/确保失败/预算超时)`,
|
|
109
|
+
match(topo.core.alias, ` && ! ${routeArgs('quiet')}`),
|
|
104
110
|
' HostName 127.0.0.1',
|
|
105
111
|
` Port ${topo.core.relayPort}`,
|
|
106
112
|
` ProxyJump ${topo.relay.alias}`,
|
|
@@ -163,25 +169,31 @@ Host ${patterns}
|
|
|
163
169
|
|
|
164
170
|
// ---- 三种 LaunchAgent plist ----
|
|
165
171
|
|
|
172
|
+
// link job 的 launchd 重连间隔(2026-10-02):纯 ssh 形态下 ssh 退出即 job
|
|
173
|
+
// 退出,转发口要 2s 内重拉,不用公共的 30s。edge 开着时 relay 恒通、无失败
|
|
174
|
+
// 风暴(关机时整冻结)
|
|
175
|
+
const LINK_THROTTLE = 2;
|
|
176
|
+
|
|
166
177
|
/**
|
|
167
|
-
* edge link job
|
|
168
|
-
* @param {{binDir:string, coreAlias:string,
|
|
169
|
-
* relayAlias:string, relayPort:number, coreUser:string,
|
|
178
|
+
* edge link job(纯 ssh 转发通道):PathState 盯根开关 RELAY_ON,不写 RunAtLoad。
|
|
179
|
+
* @param {{binDir:string, coreAlias:string,
|
|
170
180
|
* forwardArgs:readonly string[], relayOnFile:string, logsDir:string}} opts
|
|
171
|
-
* forwardArgs 是
|
|
172
|
-
*
|
|
173
|
-
* 不再依赖 ~/.ssh/config 里的 <core>-via-relay 内部别名(design §6.1)
|
|
181
|
+
* forwardArgs 是 renderForwardArgs 的最终 -L 转发参数(目标固定);
|
|
182
|
+
* 末尾一个 argv 是 peer 别名,User/HostName 读 Host 块
|
|
174
183
|
*/
|
|
175
184
|
export function renderEdgeLinkPlist(opts) {
|
|
176
|
-
const {
|
|
177
|
-
binDir, coreAlias, selfAlias, relayAlias, relayPort, coreUser,
|
|
178
|
-
forwardArgs, relayOnFile, logsDir,
|
|
179
|
-
} = opts;
|
|
185
|
+
const { binDir, coreAlias, forwardArgs, relayOnFile, logsDir } = opts;
|
|
180
186
|
const label = linkLabel(coreAlias);
|
|
187
|
+
// argv 完全渲染:软链 → -N(无远程命令)→ 转发口 → 选项 → peer
|
|
181
188
|
const argv = [
|
|
182
189
|
`${binDir}/${linkLinkName(coreAlias)}`,
|
|
183
|
-
|
|
190
|
+
'-N',
|
|
184
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,
|
|
185
197
|
];
|
|
186
198
|
return `${XML_HEAD} <key>Label</key>
|
|
187
199
|
<string>${label}</string>
|
|
@@ -196,7 +208,7 @@ ${programArguments(argv)} <key>ProcessType</key>
|
|
|
196
208
|
</dict>
|
|
197
209
|
</dict>
|
|
198
210
|
<key>ThrottleInterval</key>
|
|
199
|
-
<integer>${
|
|
211
|
+
<integer>${LINK_THROTTLE}</integer>
|
|
200
212
|
${logPaths({ label, logsDir })}${XML_TAIL}`;
|
|
201
213
|
}
|
|
202
214
|
|