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/libexec/lib/config.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
// topology
|
|
1
|
+
// topology(~/.ssh/ssh-net/topology,dotfiles 托管)的解析与严格校验。
|
|
2
2
|
// 格式见 design §4.1:空格分隔、`#` 注释、空行忽略、无项填 `-`。
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
|
|
3
|
+
// 只管名称:IP 不进 topology——静态主机地址的唯一来源是 ~/.ssh/config 的
|
|
4
|
+
// Host 块(lib/sshconfig.mjs 解析成 hostTable 传入),relay 地址/用户同理
|
|
5
|
+
// 由 Host s100 块提供。本文件只做语法解析与拓扑内部校验;别名→IP 的翻译
|
|
6
|
+
// 在 buildForwardSpecs 渲染时查 hostTable(install 时做,缺块即报错)。
|
|
7
7
|
import net from 'node:net';
|
|
8
8
|
|
|
9
9
|
export class ConfigError extends Error {
|
|
@@ -12,7 +12,7 @@ export class ConfigError extends Error {
|
|
|
12
12
|
* @param {string} message
|
|
13
13
|
*/
|
|
14
14
|
constructor(line, message) {
|
|
15
|
-
super(line > 0 ? `
|
|
15
|
+
super(line > 0 ? `topology 第 ${line} 行:${message}` : message);
|
|
16
16
|
this.name = 'ConfigError';
|
|
17
17
|
this.line = line;
|
|
18
18
|
}
|
|
@@ -29,8 +29,9 @@ const BIND = '127.0.0.1'; // 所有转发强制只绑环回(design §6.5)
|
|
|
29
29
|
* @typedef {Object} HostRow
|
|
30
30
|
* @property {'core'|'edge'|'node'} role
|
|
31
31
|
* @property {string} hostname DOT_HOST 短主机名
|
|
32
|
-
* @property {string} alias
|
|
33
|
-
* @property {string
|
|
32
|
+
* @property {string} alias 规范别名(ssh-net 内部一切命名:job/state/Match)
|
|
33
|
+
* @property {string[]} aliases 规范别名 + 给人用的附加 ssh 别名(逗号列声明,
|
|
34
|
+
* 如 ['mb5','m5'];目前只有 core 的 edge 入口片段渲染附加名)
|
|
34
35
|
* @property {number|null} relayPort
|
|
35
36
|
* @property {string} user
|
|
36
37
|
*
|
|
@@ -38,8 +39,9 @@ const BIND = '127.0.0.1'; // 所有转发强制只绑环回(design §6.5)
|
|
|
38
39
|
* @property {'dynamic'|'local'} kind
|
|
39
40
|
* @property {string} bind 恒 127.0.0.1
|
|
40
41
|
* @property {number} listenPort
|
|
41
|
-
* @property {string} [targetAlias]
|
|
42
|
-
*
|
|
42
|
+
* @property {string} [targetAlias] 目标别名:'loopback'=落地机 v4 环回、
|
|
43
|
+
* 'loopback6'=v6 环回偏好(peer 走 ::1、非 peer 退 127.0.0.1),
|
|
44
|
+
* 其余=主机别名(渲染时查 ~/.ssh/config Host 块的 HostName)
|
|
43
45
|
* @property {number} [targetPort]
|
|
44
46
|
*
|
|
45
47
|
* @typedef {Object} Topology
|
|
@@ -58,13 +60,34 @@ function parsePort(tok) {
|
|
|
58
60
|
return Number(tok);
|
|
59
61
|
}
|
|
60
62
|
|
|
63
|
+
/**
|
|
64
|
+
* 别名列:单个规范名,或逗号分隔的「规范名,附加名…」(无空格,文件本身
|
|
65
|
+
* 空白分列)。规范名是第一个,ssh-net 内部一切命名用它;附加名是给人用的
|
|
66
|
+
* ssh 别名(如 mb5,m5——内部都叫 mb5,人在 core 上 ssh m5/mb5 等价)。
|
|
67
|
+
* @param {string} tok
|
|
68
|
+
* @param {number} lineNo
|
|
69
|
+
* @returns {string[]}
|
|
70
|
+
*/
|
|
71
|
+
function parseAliasList(tok, lineNo) {
|
|
72
|
+
const names = tok.split(',');
|
|
73
|
+
if (names.some((n) => !ALIAS_RE.test(n)))
|
|
74
|
+
throw new ConfigError(lineNo, `别名非法:${tok}`);
|
|
75
|
+
if (new Set(names).size !== names.length)
|
|
76
|
+
throw new ConfigError(lineNo, `别名列内重复:${tok}`);
|
|
77
|
+
return names;
|
|
78
|
+
}
|
|
79
|
+
|
|
61
80
|
/**
|
|
62
81
|
* @param {string} text
|
|
82
|
+
* @param {{allowLegacyLanIp?: boolean}} [opts] 兼容 2026-10-01 前的旧 7 列
|
|
83
|
+
* 格式(多一列 LAN_IP):仅在读旧名 `config` 兜底时传 true;该列只校验、
|
|
84
|
+
* 不进 HostRow。五台切换完成后连同文件名兜底一起删
|
|
63
85
|
* @returns {Topology}
|
|
64
86
|
*/
|
|
65
|
-
export function parseTopology(text) {
|
|
87
|
+
export function parseTopology(text, opts = {}) {
|
|
66
88
|
if (typeof text !== 'string' || text.trim().length === 0)
|
|
67
|
-
throw new ConfigError(0, '
|
|
89
|
+
throw new ConfigError(0, 'topology 为空');
|
|
90
|
+
const allowLegacy = opts.allowLegacyLanIp === true;
|
|
68
91
|
|
|
69
92
|
/** @type {RelayRow|null} */
|
|
70
93
|
let relay = null;
|
|
@@ -92,35 +115,49 @@ export function parseTopology(text) {
|
|
|
92
115
|
}
|
|
93
116
|
|
|
94
117
|
if (head === 'host') {
|
|
95
|
-
|
|
118
|
+
let role;
|
|
119
|
+
let hostname;
|
|
120
|
+
let aliasTok;
|
|
121
|
+
let lanTok = null;
|
|
122
|
+
let portTok;
|
|
123
|
+
let user;
|
|
124
|
+
if (cols.length === 6) {
|
|
125
|
+
[, role, hostname, aliasTok, portTok, user] = cols;
|
|
126
|
+
} else if (cols.length === 7 && allowLegacy) {
|
|
127
|
+
// 旧 7 列:…<别名> <LAN_IP|-> <relay端口|-> <用户>。LAN_IP 只校验
|
|
128
|
+
// 不落对象(新格式里它归 ~/.ssh/config)
|
|
129
|
+
[, role, hostname, aliasTok, lanTok, portTok, user] = cols;
|
|
130
|
+
} else {
|
|
96
131
|
throw new ConfigError(
|
|
97
132
|
lineNo,
|
|
98
|
-
'host 需要
|
|
133
|
+
'host 需要 6 列:host <角色> <主机名> <别名[,附加名…]> <relay端口|-> <用户>',
|
|
99
134
|
);
|
|
100
|
-
|
|
135
|
+
}
|
|
101
136
|
if (!ROLES.has(role)) throw new ConfigError(lineNo, `未知角色:${role}(只允许 core/edge/node)`);
|
|
102
137
|
if (!ALIAS_RE.test(hostname)) throw new ConfigError(lineNo, `主机名非法:${hostname}`);
|
|
103
|
-
|
|
138
|
+
const aliases = parseAliasList(aliasTok, lineNo);
|
|
139
|
+
const alias = aliases[0];
|
|
104
140
|
if (!user || user === '-') throw new ConfigError(lineNo, '用户不能为空');
|
|
105
|
-
|
|
106
|
-
|
|
141
|
+
if (lanTok !== null) {
|
|
142
|
+
if (role === 'edge') {
|
|
143
|
+
if (lanTok !== '-')
|
|
144
|
+
throw new ConfigError(lineNo, 'edge 行的 LAN_IP 必须填 -');
|
|
145
|
+
} else if (net.isIP(lanTok) === 0) {
|
|
146
|
+
throw new ConfigError(lineNo, `LAN_IP 不是 IP 字面值:${lanTok}`);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
107
149
|
/** @type {number|null} */
|
|
108
150
|
let relayPort = null;
|
|
109
151
|
if (role === 'edge') {
|
|
110
|
-
if (
|
|
111
|
-
throw new ConfigError(lineNo, 'edge 行的
|
|
152
|
+
if (portTok !== '-')
|
|
153
|
+
throw new ConfigError(lineNo, 'edge 行的 relay 端口必须填 -(edge 不注册口)');
|
|
112
154
|
} else {
|
|
113
|
-
if (ipTok === '-' || !ipTok)
|
|
114
|
-
throw new ConfigError(lineNo, `${role} 行必须有 LAN_IP`);
|
|
115
|
-
if (net.isIP(ipTok) === 0)
|
|
116
|
-
throw new ConfigError(lineNo, `LAN_IP 不是 IP 字面值:${ipTok}`);
|
|
117
155
|
const p = parsePort(portTok);
|
|
118
156
|
if (!Number.isInteger(p) || p < 1 || p > 65535)
|
|
119
157
|
throw new ConfigError(lineNo, `relay 端口非法:${portTok}`);
|
|
120
|
-
lanIp = ipTok;
|
|
121
158
|
relayPort = p;
|
|
122
159
|
}
|
|
123
|
-
hosts.push({ role, hostname, alias,
|
|
160
|
+
hosts.push({ role, hostname, alias, aliases: Object.freeze(aliases), relayPort, user });
|
|
124
161
|
return;
|
|
125
162
|
}
|
|
126
163
|
|
|
@@ -143,18 +180,17 @@ export function parseTopology(text) {
|
|
|
143
180
|
|
|
144
181
|
const byAlias = new Map();
|
|
145
182
|
const byHostname = new Map();
|
|
146
|
-
// relay 别名、各 host
|
|
147
|
-
|
|
183
|
+
// relay 别名、各 host 的全部别名(含给人用的短名)与主机名同处一个命名空间
|
|
184
|
+
//(ssh Host/HostKeyAlias 共用),任一重复都报错。
|
|
148
185
|
const names = new Set([relay.alias]);
|
|
149
186
|
for (const h of hosts) {
|
|
150
|
-
for (const
|
|
151
|
-
|
|
152
|
-
[h.hostname, '主机名'],
|
|
153
|
-
]) {
|
|
154
|
-
if (names.has(name)) throw new ConfigError(0, `${what}重复:${name}`);
|
|
187
|
+
for (const name of h.aliases) {
|
|
188
|
+
if (names.has(name)) throw new ConfigError(0, `别名重复:${name}`);
|
|
155
189
|
names.add(name);
|
|
190
|
+
byAlias.set(name, h);
|
|
156
191
|
}
|
|
157
|
-
|
|
192
|
+
if (names.has(h.hostname)) throw new ConfigError(0, `主机名重复:${h.hostname}`);
|
|
193
|
+
names.add(h.hostname);
|
|
158
194
|
byHostname.set(h.hostname, h);
|
|
159
195
|
}
|
|
160
196
|
|
|
@@ -181,33 +217,20 @@ export function parseTopology(text) {
|
|
|
181
217
|
const targetPort = parsePort(cols[4]);
|
|
182
218
|
if (!Number.isInteger(targetPort) || targetPort < 1 || targetPort > 65535)
|
|
183
219
|
throw new ConfigError(line, `目标端口非法:${cols[4]}`);
|
|
184
|
-
//
|
|
185
|
-
//
|
|
186
|
-
//
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
continue;
|
|
220
|
+
// 环回别名:目标服务在 ssh 落地机的本机环回上,不是主机名、无 Host 块:
|
|
221
|
+
// loopback = 127.0.0.1(microsocks 只听 v4)
|
|
222
|
+
// loopback6 = ::1(服务同听 v6 环回时用;link-core 渲染 ssh 参数时
|
|
223
|
+
// 按 mode 选族带方括号,见 buildForwardSpecs)
|
|
224
|
+
// 具名别名(core/node)的 HostName 来自 ~/.ssh/config Host 块,
|
|
225
|
+
// buildForwardSpecs 渲染时查 hostTable——topology 不存任何 IP。
|
|
226
|
+
if (targetAlias !== 'loopback' && targetAlias !== 'loopback6') {
|
|
227
|
+
const target = byAlias.get(targetAlias);
|
|
228
|
+
if (!target)
|
|
229
|
+
throw new ConfigError(line, `forward local 指向未知别名:${targetAlias}`);
|
|
230
|
+
if (target.role === 'edge')
|
|
231
|
+
throw new ConfigError(line, `forward local 目标 ${targetAlias} 是 edge(edge 无注册地址,不能做转发目标)`);
|
|
197
232
|
}
|
|
198
|
-
|
|
199
|
-
if (!target)
|
|
200
|
-
throw new ConfigError(line, `forward local 指向未知别名:${targetAlias}`);
|
|
201
|
-
if (target.lanIp === null)
|
|
202
|
-
throw new ConfigError(line, `forward local 目标 ${targetAlias} 没有 LAN_IP(不能指向 edge)`);
|
|
203
|
-
forwards.push({
|
|
204
|
-
kind,
|
|
205
|
-
bind: BIND,
|
|
206
|
-
listenPort,
|
|
207
|
-
targetAlias,
|
|
208
|
-
targetIp: target.lanIp,
|
|
209
|
-
targetPort,
|
|
210
|
-
});
|
|
233
|
+
forwards.push({ kind, bind: BIND, listenPort, targetAlias, targetPort });
|
|
211
234
|
}
|
|
212
235
|
}
|
|
213
236
|
|
|
@@ -223,19 +246,64 @@ export function parseTopology(text) {
|
|
|
223
246
|
}
|
|
224
247
|
|
|
225
248
|
/**
|
|
226
|
-
*
|
|
249
|
+
* 名称↔地址契约校验(buildContext 装配时跑):topology 里凡需要被 ssh 拨到的
|
|
250
|
+
* 静态主机(relay/core/nodes)与具名转发目标,都必须在 ~/.ssh/config 有 Host
|
|
251
|
+
* 块且声明 HostName。edge 豁免(其入口是 core 上动态渲染的 conf.d/edge-*)。
|
|
252
|
+
* @param {Topology} topo
|
|
253
|
+
* @param {Map<string, {hostname?: string}>} hostTable parseStaticHosts 的产物
|
|
254
|
+
*/
|
|
255
|
+
export function validateHostContract(topo, hostTable) {
|
|
256
|
+
/** @type {string[]} */
|
|
257
|
+
const missing = [];
|
|
258
|
+
const check = (alias) => {
|
|
259
|
+
const host = hostTable.get(alias);
|
|
260
|
+
if (!host || !host.hostname) missing.push(alias);
|
|
261
|
+
};
|
|
262
|
+
check(topo.relay.alias);
|
|
263
|
+
check(topo.core.alias);
|
|
264
|
+
for (const n of topo.nodes) check(n.alias);
|
|
265
|
+
for (const f of topo.forwards) {
|
|
266
|
+
const a = f.targetAlias;
|
|
267
|
+
if (a && a !== 'loopback' && a !== 'loopback6') check(a);
|
|
268
|
+
}
|
|
269
|
+
if (missing.length > 0)
|
|
270
|
+
throw new ConfigError(
|
|
271
|
+
0,
|
|
272
|
+
`${missing.join('、')} 在 topology 有名称,但 ~/.ssh/config 缺 Host 块或 HostName`
|
|
273
|
+
+ '(名称归 topology、静态地址归 ssh config,两处要对上;edge 豁免)',
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* 转发渲染成 link-core 的**转发规格**(plist argv 传它,不是裸 ssh 片段):
|
|
279
|
+
* dynamic → `D:<listenPort>`
|
|
280
|
+
* local → `L:<listenPort>:<targetPort>:<target>`
|
|
281
|
+
* target:loopback=127.0.0.1;loopback6 保留关键字(link-core 按 mode 选族);
|
|
282
|
+
* 具名别名查 hostTable(~/.ssh/config Host 块的 HostName,IP 唯一来源)。
|
|
283
|
+
* bind 不进规格(link-core 恒用 127.0.0.1)。规格冒号分隔,target 不支持
|
|
284
|
+
* v6 字面量(v6 环回走 loopback6 关键字)。
|
|
227
285
|
* @param {readonly Forward[]} forwards
|
|
286
|
+
* @param {Map<string, {hostname?: string}>} hostTable parseStaticHosts 的产物
|
|
228
287
|
* @returns {string[]}
|
|
229
288
|
*/
|
|
230
|
-
export function
|
|
289
|
+
export function buildForwardSpecs(forwards, hostTable) {
|
|
231
290
|
/** @type {string[]} */
|
|
232
|
-
const
|
|
291
|
+
const specs = [];
|
|
233
292
|
for (const f of forwards) {
|
|
234
293
|
if (f.kind === 'dynamic') {
|
|
235
|
-
|
|
294
|
+
specs.push(`D:${f.listenPort}`);
|
|
295
|
+
} else if (f.targetAlias === 'loopback6') {
|
|
296
|
+
specs.push(`L:${f.listenPort}:${f.targetPort}:loopback6`);
|
|
297
|
+
} else if (f.targetAlias === 'loopback') {
|
|
298
|
+
specs.push(`L:${f.listenPort}:${f.targetPort}:127.0.0.1`);
|
|
236
299
|
} else {
|
|
237
|
-
|
|
300
|
+
const host = hostTable.get(f.targetAlias);
|
|
301
|
+
// 语法解析时只校验别名在 topology;此处才查 ssh config——install 时
|
|
302
|
+
// 暴露,错误直指缺 Host 块的那台机器
|
|
303
|
+
if (!host || !host.hostname)
|
|
304
|
+
throw new Error(`forward local 目标 ${f.targetAlias} 在 ~/.ssh/config 缺 Host 块或 HostName`);
|
|
305
|
+
specs.push(`L:${f.listenPort}:${f.targetPort}:${host.hostname}`);
|
|
238
306
|
}
|
|
239
307
|
}
|
|
240
|
-
return
|
|
308
|
+
return specs;
|
|
241
309
|
}
|
package/libexec/lib/identity.mjs
CHANGED
package/libexec/lib/paths.mjs
CHANGED
|
@@ -4,6 +4,15 @@
|
|
|
4
4
|
// 运行时分发(design §2.3,2026-09-29 定):install 把包脚本**拷贝**进
|
|
5
5
|
// ~/.ssh/ssh-net/bin/(不再软链 fnm alias)——运行时零 npm/fnm 依赖;
|
|
6
6
|
// 状态与产物同住 ~/.ssh/ssh-net/ 一棵树(state/ 子目录)。
|
|
7
|
+
//
|
|
8
|
+
// 目录分工(2026-10-01 定):
|
|
9
|
+
// topology dotfiles 托管的拓扑(名/角色/relay 口/用户;旧名 config 退役)
|
|
10
|
+
// bin/ 包脚本实体 + 服务软链(install/checkin 对账)
|
|
11
|
+
// state/ 易变状态(开关、edges v6、ROUTE_PEER)
|
|
12
|
+
// conf.d/ 渲染给 ssh Include 的配置片段(生成物,不进 dotfiles):
|
|
13
|
+
// routes edge:Match 选路块(install 渲染)
|
|
14
|
+
// edge-<别名> core:edge 在线入口(_register 写/休眠删)
|
|
15
|
+
// ~/.ssh/config dotfiles 托管,顶部 Include conf.d/* + 静态主机块(IP 唯一来源)
|
|
7
16
|
import path from 'node:path';
|
|
8
17
|
import fs from 'node:fs';
|
|
9
18
|
import { fileURLToPath } from 'node:url';
|
|
@@ -33,11 +42,21 @@ export function machinePaths(env = process.env) {
|
|
|
33
42
|
return Object.freeze({
|
|
34
43
|
home,
|
|
35
44
|
sshDir: path.join(home, '.ssh'),
|
|
45
|
+
sshConfigFile: path.join(home, '.ssh', 'config'),
|
|
36
46
|
sshNetDir,
|
|
37
|
-
|
|
47
|
+
topologyFile: path.join(sshNetDir, 'topology'),
|
|
48
|
+
// 旧名(2026-10-01 前):topology 缺席时兜底读它,新格式全面铺开后删兼容
|
|
49
|
+
legacyConfigFile: path.join(sshNetDir, 'config'),
|
|
38
50
|
binDir: path.join(sshNetDir, 'bin'),
|
|
39
51
|
runtimeVersionFile: path.join(sshNetDir, 'runtime-version'),
|
|
40
52
|
stateDir,
|
|
53
|
+
// ssh Include 片段目录(design §4.2,2026-10-01):routes 仅 edge install
|
|
54
|
+
// 写;edge-<别名> 仅 core _register/休眠写删;node 不建。Include glob
|
|
55
|
+
// 零匹配静默,主 config 五台可同份
|
|
56
|
+
confDir: path.join(sshNetDir, 'conf.d'),
|
|
57
|
+
routesFile: path.join(sshNetDir, 'conf.d', 'routes'),
|
|
58
|
+
/** @param {string} alias edge 别名 */
|
|
59
|
+
edgeHostFile: (alias) => path.join(sshNetDir, 'conf.d', `edge-${alias}`),
|
|
41
60
|
// 三开关(design §3):relay 是根(PathState 盯它),文件名与 bash 侧
|
|
42
61
|
// link-core、dotfiles Match 三处硬编码互为契约,改则三处同步
|
|
43
62
|
switchFiles: Object.freeze({
|
package/libexec/lib/render.mjs
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
//
|
|
1
|
+
// 渲染唯一来源(design §2.3、§4.2、§6.1、§6.2):
|
|
2
|
+
// - plist/systemd unit:每机差异只以位置参数出现(端口、别名、user、转发列表)
|
|
3
|
+
// - conf.d 片段:routes(edge 选路 Match)与 edge-<别名>(core 上的 edge 入口)
|
|
4
|
+
// 纯字符串函数;渲染快照测试钉死输出;结构性断言(XML 闭合、只绑环回)在测试侧另查。
|
|
5
|
+
import { CALLBACK_PORT } from './constants.mjs';
|
|
4
6
|
|
|
5
7
|
export const THROTTLE = 30; // launchd ThrottleInterval / 对应 systemd RestartSec
|
|
6
8
|
|
|
@@ -59,17 +61,106 @@ export const edgeLabel = (edgeAlias) => `local.${edgeLinkName(edgeAlias)}`;
|
|
|
59
61
|
/** @param {string} label */
|
|
60
62
|
export const plistFileName = (label) => `${label}.plist`;
|
|
61
63
|
|
|
64
|
+
// ---- conf.d 片段(~/.ssh/config 顶部 Include,design §4.2)----
|
|
65
|
+
|
|
66
|
+
const ROUTES_HEADER = `# 本文件由 \`ssh-net install\` 从 ~/.ssh/ssh-net/topology 渲染,勿手改。
|
|
67
|
+
# 改 topology(别名/relay 端口)后重跑 ssh-net install 刷新;卸载会删本文件。
|
|
68
|
+
#
|
|
69
|
+
# Match 必须排在主 config 的 Host 块之前(Include 在文件首行;ssh 首次取到的
|
|
70
|
+
# 值生效)。判据是 edge 根开关 RELAY_ON(只在 edge 存在,core/node 恒不命中)
|
|
71
|
+
# + sn-route 判决(link 发布 ROUTE_PEER 时复核一次 banner,design §2.2):
|
|
72
|
+
# core 两块一正一反互斥(Match 不是 if/elif,PROBE_FAIL 戳让两块只复核一次);
|
|
73
|
+
# node 无 v6 入站,只有 relay 兜底。User 不写——由主 config 的 Host 块补。
|
|
74
|
+
`;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* edge 选路片段 conf.d/routes 的唯一渲染来源。值全部来自 topology + 冻结常量,
|
|
78
|
+
* 不出现任何真实 IP(环回口 + relay 别名)。
|
|
79
|
+
* @param {import('./config.mjs').Topology} topo
|
|
80
|
+
* @returns {string}
|
|
81
|
+
*/
|
|
82
|
+
export function renderRoutes(topo) {
|
|
83
|
+
const state = '$HOME/.ssh/ssh-net/state';
|
|
84
|
+
const snRoute = '$HOME/.ssh/ssh-net/bin/sn-route';
|
|
85
|
+
/** @type {string[]} */
|
|
86
|
+
const blocks = [];
|
|
87
|
+
|
|
88
|
+
const match = (alias, cond) =>
|
|
89
|
+
`Match originalhost ${alias} exec "test -e ${state}/RELAY_ON${cond}"`;
|
|
90
|
+
|
|
91
|
+
// core:peer 一跳(link 判 peer 且 banner 复核通过)
|
|
92
|
+
blocks.push([
|
|
93
|
+
`# ${topo.core.alias}:peer(v6 回拨落在本机 [::1]:${CALLBACK_PORT},全 v6 环回)`,
|
|
94
|
+
match(topo.core.alias, ` && ${snRoute}`),
|
|
95
|
+
' HostName ::1',
|
|
96
|
+
` Port ${CALLBACK_PORT}`,
|
|
97
|
+
` HostKeyAlias ${topo.core.alias}`,
|
|
98
|
+
].join('\n'));
|
|
99
|
+
// core:relay 兜底(与上块互斥:sn-route 失败本块才成立)
|
|
100
|
+
blocks.push([
|
|
101
|
+
`# ${topo.core.alias}:relay 兜底(无判决/复核失败/僵尸窗口)`,
|
|
102
|
+
match(topo.core.alias, ` && ! ${snRoute}`),
|
|
103
|
+
' HostName 127.0.0.1',
|
|
104
|
+
` Port ${topo.core.relayPort}`,
|
|
105
|
+
` ProxyJump ${topo.relay.alias}`,
|
|
106
|
+
` HostKeyAlias ${topo.core.alias}`,
|
|
107
|
+
].join('\n'));
|
|
108
|
+
|
|
109
|
+
// node:只有 relay(无公网入站)
|
|
110
|
+
for (const n of topo.nodes) {
|
|
111
|
+
blocks.push([
|
|
112
|
+
`# ${n.alias}:relay(node 无公网入站)`,
|
|
113
|
+
match(n.alias, ''),
|
|
114
|
+
' HostName 127.0.0.1',
|
|
115
|
+
` Port ${n.relayPort}`,
|
|
116
|
+
` ProxyJump ${topo.relay.alias}`,
|
|
117
|
+
` HostKeyAlias ${n.alias}`,
|
|
118
|
+
].join('\n'));
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return `${ROUTES_HEADER}\n${blocks.join('\n\n')}\n`;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* core 上的 edge 在线入口片段 conf.d/edge-<规范别名>。Host 模式给全部别名:
|
|
126
|
+
* 规范名(ssh-net 内部用,如 mb5)在前,给人用的短名(如 m5)在后,
|
|
127
|
+
* `ssh mb5` 与 `ssh m5` 等价;文件/state/job 命名只用规范名。
|
|
128
|
+
* @param {{alias:string, aliases?:string[], user:string, v6:string}} opts
|
|
129
|
+
* @returns {string}
|
|
130
|
+
*/
|
|
131
|
+
export function renderEdgeHost({ alias, aliases, user, v6 }) {
|
|
132
|
+
const patterns = aliases && aliases.length > 0 ? aliases.join(' ') : alias;
|
|
133
|
+
return `# 本文件由 ssh-net 维护:core 收到 ${alias} 的报名时写入,回拨 job 休眠
|
|
134
|
+
# (edge 关机/不可达,六连败)或 uninstall 时删除。存在即 edge 当前在线、
|
|
135
|
+
# 其上报 v6 可拨;勿手改(改了下次报名也会被覆盖)。
|
|
136
|
+
Host ${patterns}
|
|
137
|
+
User ${user}
|
|
138
|
+
HostName ${v6}
|
|
139
|
+
`;
|
|
140
|
+
}
|
|
141
|
+
|
|
62
142
|
// ---- 三种 LaunchAgent plist ----
|
|
63
143
|
|
|
64
144
|
/**
|
|
65
145
|
* edge link job:PathState 盯根开关 RELAY_ON,不写 RunAtLoad。
|
|
66
146
|
* @param {{binDir:string, coreAlias:string, selfAlias:string,
|
|
147
|
+
* relayAlias:string, relayPort:number, coreUser:string,
|
|
67
148
|
* forwardArgs:readonly string[], relayOnFile:string, logsDir:string}} opts
|
|
149
|
+
* forwardArgs 是 buildForwardSpecs 的转发规格(D:/L:),由 link-core 按 mode 渲染。
|
|
150
|
+
* relay 三参(别名/端口/core 用户)喂给循环的报名 ssh:命令行显式 -J 走中继,
|
|
151
|
+
* 不再依赖 ~/.ssh/config 里的 <core>-via-relay 内部别名(design §6.1)
|
|
68
152
|
*/
|
|
69
153
|
export function renderEdgeLinkPlist(opts) {
|
|
70
|
-
const {
|
|
154
|
+
const {
|
|
155
|
+
binDir, coreAlias, selfAlias, relayAlias, relayPort, coreUser,
|
|
156
|
+
forwardArgs, relayOnFile, logsDir,
|
|
157
|
+
} = opts;
|
|
71
158
|
const label = linkLabel(coreAlias);
|
|
72
|
-
const argv = [
|
|
159
|
+
const argv = [
|
|
160
|
+
`${binDir}/${linkLinkName(coreAlias)}`,
|
|
161
|
+
selfAlias, coreAlias, relayAlias, String(relayPort), coreUser,
|
|
162
|
+
...forwardArgs,
|
|
163
|
+
];
|
|
73
164
|
return `${XML_HEAD} <key>Label</key>
|
|
74
165
|
<string>${label}</string>
|
|
75
166
|
${programArguments(argv)} <key>ProcessType</key>
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// ~/.ssh/config 静态主机块的极简解析(2026-10-01,topology 去 IP 后的配套)。
|
|
2
|
+
// topology 只管名称,静态地址唯一来源是 ~/.ssh/config 的 Host 块:edge install
|
|
3
|
+
// 渲染 forward 目标、edge status 探 LAN、以及「名称↔地址」契约校验都查本表。
|
|
4
|
+
//
|
|
5
|
+
// 刻意不做完整 ssh 配置解析:
|
|
6
|
+
// - **不展开 Include**——conf.d/routes 是渲染产物,里面的 Match 写着
|
|
7
|
+
// HostName ::1/127.0.0.1,展开就会把隧道口当静态地址;本表只认主文件
|
|
8
|
+
// - **跳过 Match 块**(同理,Match 是条件覆盖,不是静态主机定义)
|
|
9
|
+
// - 只收录精确别名(无通配的 Host 模式);`Host *` / `Host n*` 不入表
|
|
10
|
+
// - 关键字大小写不敏感(ssh 规则),同块内同关键字取首次(ssh 首次生效)
|
|
11
|
+
// - 行首 `#` 注释、空行忽略;不处理行尾注释(ssh 不支持行尾注释)
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @typedef {Object} StaticHost
|
|
15
|
+
* @property {string} [hostname] HostName;缺省时 ssh 以别名为地址
|
|
16
|
+
* @property {string} [user] User
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
const GLOB_CHARS = new Set(['*', '?', '!']);
|
|
20
|
+
|
|
21
|
+
/** @param {string} pattern Host 模式 */
|
|
22
|
+
function isExactPattern(pattern) {
|
|
23
|
+
return ![...pattern].some((c) => GLOB_CHARS.has(c));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 解析主 ssh config 的静态 Host 块。
|
|
28
|
+
* @param {string} text ~/.ssh/config 原文
|
|
29
|
+
* @returns {Map<string, StaticHost>} key=精确别名
|
|
30
|
+
*/
|
|
31
|
+
export function parseStaticHosts(text) {
|
|
32
|
+
/** @type {Map<string, StaticHost>} */
|
|
33
|
+
const table = new Map();
|
|
34
|
+
/** @type {string[]} */
|
|
35
|
+
let currentPatterns = [];
|
|
36
|
+
let inMatch = false;
|
|
37
|
+
|
|
38
|
+
const putFirst = (host, key, value) => {
|
|
39
|
+
if (host[key] === undefined) host[key] = value;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
for (const raw of text.split('\n')) {
|
|
43
|
+
const line = raw.trim();
|
|
44
|
+
if (line.length === 0 || line.startsWith('#')) continue;
|
|
45
|
+
const cols = line.split(/\s+/);
|
|
46
|
+
const keyword = cols[0].toLowerCase();
|
|
47
|
+
|
|
48
|
+
if (keyword === 'match') {
|
|
49
|
+
inMatch = true;
|
|
50
|
+
currentPatterns = [];
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (keyword === 'host') {
|
|
54
|
+
inMatch = false;
|
|
55
|
+
// 精确模式入表(多模式逐个);同一别名多个 Host 块时 ssh 取首次值——
|
|
56
|
+
// 已存在的记录对象继续沿用,putFirst 保证不被后块覆盖
|
|
57
|
+
currentPatterns = cols.slice(1).filter(isExactPattern);
|
|
58
|
+
for (const p of currentPatterns) {
|
|
59
|
+
if (!table.has(p)) table.set(p, {});
|
|
60
|
+
}
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
if (inMatch || currentPatterns.length === 0) continue;
|
|
64
|
+
if (keyword === 'hostname' || keyword === 'user') {
|
|
65
|
+
const value = cols.slice(1).join(' ');
|
|
66
|
+
if (value) {
|
|
67
|
+
for (const p of currentPatterns)
|
|
68
|
+
putFirst(table.get(p), keyword === 'hostname' ? 'hostname' : 'user', value);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return table;
|
|
73
|
+
}
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
#!/bin/bash
|
|
2
2
|
# ssh-net-link-core:edge 上到 core 的常驻隧道(-D SOCKS + -L VNC)。
|
|
3
|
-
# argv: <self 别名> <peer 别名>
|
|
3
|
+
# argv: <self 别名> <peer 别名> <relay 别名> <relay 端口> <peer 用户> <转发规格…>
|
|
4
|
+
# 转发规格(buildForwardSpecs 产出,不是裸 ssh 片段):D:<本地口> 或
|
|
5
|
+
# L:<本地口>:<目标口>:<目标>;目标是 IP,或关键字 loopback6——后者按当前
|
|
6
|
+
# mode 渲染:peer 走 [::1]、relay/其他退 127.0.0.1(两态都落地 core,
|
|
7
|
+
# core 的屏幕共享两族环回都听;1090 用普通 loopback 不参与)。规格在
|
|
8
|
+
# 每轮 mode 确定后渲染成 -D/-L,翻转杀 ssh 重连即随新 mode 换目标。
|
|
4
9
|
# 走 peer 还是 relay 由本循环判决(回拨窗口/信令/到点探),并把结论发布到
|
|
5
10
|
# state/ROUTE_PEER(peer=epoch 心跳,relay/退出=删除),ssh config Match 的
|
|
6
11
|
# sn-route 只读它 + 一次廉价 banner 复核(design §2.2/§6.1)。循环本身做
|
|
@@ -33,13 +38,57 @@ RM=${RM:-/bin/rm}
|
|
|
33
38
|
|
|
34
39
|
self=$1
|
|
35
40
|
peer=$2
|
|
36
|
-
|
|
37
|
-
|
|
41
|
+
relay_alias=$3
|
|
42
|
+
relay_port=$4
|
|
43
|
+
peer_user=$5
|
|
44
|
+
shift 5
|
|
45
|
+
fwd=("$@") # 转发规格(D:/L:),mode 确定后经 build_fwd_args 渲染
|
|
46
|
+
|
|
47
|
+
# 转发规格 → ssh -D/-L 参数(结果写全局数组 FWD_SSH)。bind 恒 127.0.0.1
|
|
48
|
+
#(与控制面 BIND 双源,constants.test.mjs 钉)。loopback6 按本轮 mode 选族。
|
|
49
|
+
# bash 3.2:不用关联数组,逐项字符串切分(规格分隔符是冒号,故 target 不
|
|
50
|
+
# 支持 v6 字面量,v6 环回只能用 loopback6 关键字)
|
|
51
|
+
build_fwd_args() {
|
|
52
|
+
FWD_SSH=()
|
|
53
|
+
local spec kind rest lport rport target ip
|
|
54
|
+
for spec in "${fwd[@]}"; do
|
|
55
|
+
kind=${spec%%:*}
|
|
56
|
+
rest=${spec#*:}
|
|
57
|
+
case "$kind" in
|
|
58
|
+
D)
|
|
59
|
+
FWD_SSH+=(-D "127.0.0.1:$rest")
|
|
60
|
+
;;
|
|
61
|
+
L)
|
|
62
|
+
lport=${rest%%:*}; rest=${rest#*:}
|
|
63
|
+
rport=${rest%%:*}; target=${rest#*:}
|
|
64
|
+
ip=$target
|
|
65
|
+
if [ "$target" = loopback6 ]; then
|
|
66
|
+
if [ "$mode" = peer ]; then ip='[::1]'; else ip=127.0.0.1; fi
|
|
67
|
+
fi
|
|
68
|
+
FWD_SSH+=(-L "127.0.0.1:$lport:$ip:$rport")
|
|
69
|
+
;;
|
|
70
|
+
esac
|
|
71
|
+
done
|
|
72
|
+
}
|
|
38
73
|
|
|
39
74
|
# 事件日志:一行一事件(date 绝对路径,launchd 无交互 PATH)。ssh 自身的
|
|
40
75
|
# stderr 照旧落同一文件但无时间戳——事件行负责锚定时刻(design §6.1)
|
|
41
76
|
log() { echo "$(/bin/date '+%F %T') $*" >&2; }
|
|
42
|
-
log "link 启动(self=$self peer=$peer 转发 ${#fwd[@]} 组)"
|
|
77
|
+
log "link 启动(self=$self peer=$peer relay=$relay_alias:$relay_port 转发 ${#fwd[@]} 组)"
|
|
78
|
+
|
|
79
|
+
# 报名 ssh(循环顶与 TTL 到点重报共用):固定经 relay,命令行显式 -J——
|
|
80
|
+
# 字面目标 127.0.0.1 不命中 Match originalhost "$peer",绕开自动选路
|
|
81
|
+
#(peer 僵尸/冷启动时自动选路会把报名一起挂死,故不碰 <peer>-via-relay 类
|
|
82
|
+
# 别名);跳板 relay_alias 的地址/用户由 ssh config Host 块提供;
|
|
83
|
+
# HostKeyAlias 让三路径共用同一条 known_hosts。参数 = 本次上报的 v6
|
|
84
|
+
announce() {
|
|
85
|
+
"$SSH" -J "$relay_alias" -p "$relay_port" \
|
|
86
|
+
-o HostKeyAlias="$peer" \
|
|
87
|
+
-o BatchMode=yes -o ConnectTimeout=15 \
|
|
88
|
+
-o ServerAliveInterval=10 -o ServerAliveCountMax=2 \
|
|
89
|
+
-o ControlMaster=no -o ControlPath=none \
|
|
90
|
+
"$peer_user@127.0.0.1" "ssh-net _register $self $1" >/dev/null 2>&1
|
|
91
|
+
}
|
|
43
92
|
|
|
44
93
|
relay_on_file="$STATE_DIR/RELAY_ON"
|
|
45
94
|
peer_on_file="$STATE_DIR/PEER_ON"
|
|
@@ -146,17 +195,14 @@ while :; do
|
|
|
146
195
|
[ -z "$v6" ] && log "冷启动宽限:${V6_GRACE}s 内无 v6(v4-only 网络),落中继"
|
|
147
196
|
fi
|
|
148
197
|
|
|
149
|
-
# 报名(PEER_ON
|
|
198
|
+
# 报名(PEER_ON 控)固定经 relay(announce 封装,peer 僵尸/冷启动都可达);
|
|
150
199
|
# 失败不阻断,只在日志留痕——报名长期失败(如 relay 别名漂移)是静默
|
|
151
200
|
# 降级:中继照常工作、peer 永远建不起来,status 看不出区别,唯一的
|
|
152
201
|
# 线索在这里。失败用短周期重试(下方 timer 的 RETRY_TTL),不再等
|
|
153
202
|
# 600s 寿命
|
|
154
203
|
announce_ok=1
|
|
155
204
|
if [ -n "$v6" ] && [ "$peer_on" = 1 ]; then
|
|
156
|
-
if ! "$
|
|
157
|
-
-o ServerAliveInterval=10 -o ServerAliveCountMax=2 \
|
|
158
|
-
-o ControlMaster=no -o ControlPath=none \
|
|
159
|
-
"$peer-via-relay" "ssh-net _register $self $v6" >/dev/null 2>&1; then
|
|
205
|
+
if ! announce "$v6"; then
|
|
160
206
|
announce_ok=0
|
|
161
207
|
echo "$(/bin/date '+%F %T') 报名失败($self $v6):中继不受影响,${RETRY_TTL}s 后重试" >&2
|
|
162
208
|
fi
|
|
@@ -220,12 +266,14 @@ while :; do
|
|
|
220
266
|
last_notice=$mode
|
|
221
267
|
fi
|
|
222
268
|
|
|
223
|
-
# 唯一一条转发 ssh
|
|
224
|
-
# ssh——「只要被连接、不要本地转发口」的形态;bash 3.2
|
|
269
|
+
# 唯一一条转发 ssh(规格按本轮 mode 渲染成 -D/-L;LINK_ON 不在=纯报名
|
|
270
|
+
# 维护,不起 ssh——「只要被连接、不要本地转发口」的形态;bash 3.2 空
|
|
271
|
+
# 数组展开写法)
|
|
225
272
|
ssh_pid=
|
|
226
273
|
if [ "$link_on" = 1 ]; then
|
|
274
|
+
build_fwd_args
|
|
227
275
|
log "转发 ssh 起(mode=$mode${v6:+ v6=$v6})"
|
|
228
|
-
"$SSH" -N ${
|
|
276
|
+
"$SSH" -N ${FWD_SSH[@]+"${FWD_SSH[@]}"} \
|
|
229
277
|
-o ExitOnForwardFailure=yes -o BatchMode=yes -o IdentitiesOnly=yes \
|
|
230
278
|
-o ControlMaster=no -o ControlPath=none \
|
|
231
279
|
-o ServerAliveInterval=35 -o ServerAliveCountMax=3 \
|
|
@@ -282,10 +330,7 @@ while :; do
|
|
|
282
330
|
# 两边互等;PEER_ON 关时不报,那是开关语义)
|
|
283
331
|
if [ "$peer_on" = 1 ]; then
|
|
284
332
|
fresh_v6=$("$BIN_DIR/sn-ipv6" 2>/dev/null)
|
|
285
|
-
if [ -n "$fresh_v6" ] && "$
|
|
286
|
-
-o ServerAliveInterval=10 -o ServerAliveCountMax=2 \
|
|
287
|
-
-o ControlMaster=no -o ControlPath=none \
|
|
288
|
-
"$peer-via-relay" "ssh-net _register $self $fresh_v6" >/dev/null 2>&1; then
|
|
333
|
+
if [ -n "$fresh_v6" ] && announce "$fresh_v6"; then
|
|
289
334
|
log "到点探不中:已用实时 v6 重报($fresh_v6)"
|
|
290
335
|
# 重报成功给回拨一个短窗口,命中即收编(core 唤醒/
|
|
291
336
|
# kickstart 通常几秒内);不中落回循环头续睡
|