ssh-net 26.10.28 → 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 +188 -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 +102 -52
- package/libexec/commands/restart.mjs +7 -3
- package/libexec/commands/start.mjs +23 -53
- package/libexec/commands/status.mjs +79 -63
- 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 +17 -12
- package/libexec/lib/render.mjs +36 -23
- 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
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
// status(design §6.7):只报本机角色,判据是 banner/端口/注册态真实探测。
|
|
2
2
|
// collect 返回结构化对象(测试断言对象,不锁文本);renderText 给人看——
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
// 真跑一条 ssh
|
|
3
|
+
// 按「开关(意图)与就绪(实测)分开」组织:开关行报 RELAY_ON/PEER_ON
|
|
4
|
+
// 两开关 + link 附加件(装没装,不是运行时开关),线路行逐条实测
|
|
5
|
+
// (relay 就绪=命令行 -J 经 relay 真跑一条 ssh,不从「probe 不通」推断),
|
|
6
|
+
// 开关关的线路报「关」不探测。
|
|
6
7
|
// bin/ 体检(2026-09-30 软链方案):实体逐字节比对 + 服务软链指向/悬空检查。
|
|
7
8
|
// 退出码:健康(含 edge 关闭态)0;有可行动问题 1。
|
|
8
9
|
import fs from 'node:fs';
|
|
@@ -81,6 +82,7 @@ async function relayPortListening(ctx, port) {
|
|
|
81
82
|
const r = await ctx.runner.runAsync('ssh', [
|
|
82
83
|
'-o', 'BatchMode=yes', '-o', 'ConnectTimeout=5',
|
|
83
84
|
'-o', 'IdentitiesOnly=yes',
|
|
85
|
+
'-o', 'ControlMaster=no', '-o', 'ControlPath=none',
|
|
84
86
|
ctx.topo.relay.alias, 'ss', '-tlnH',
|
|
85
87
|
], { timeout: 8000 });
|
|
86
88
|
if (r.code !== 0) return { port, reachable: false, listening: false, detail: r.stderr.trim() };
|
|
@@ -96,6 +98,7 @@ async function relayPathOk(ctx) {
|
|
|
96
98
|
'-o', `HostKeyAlias=${ctx.topo.core.alias}`,
|
|
97
99
|
'-o', 'BatchMode=yes', '-o', 'ConnectTimeout=5',
|
|
98
100
|
'-o', 'IdentitiesOnly=yes',
|
|
101
|
+
'-o', 'ControlMaster=no', '-o', 'ControlPath=none',
|
|
99
102
|
`${ctx.topo.core.user}@127.0.0.1`, 'true',
|
|
100
103
|
], { timeout: 8000 });
|
|
101
104
|
return r.code === 0;
|
|
@@ -106,24 +109,31 @@ async function collectEdge(ctx) {
|
|
|
106
109
|
const issues = [];
|
|
107
110
|
const { issue: vIssue, runtimeVersion } = versionInfo(ctx);
|
|
108
111
|
if (vIssue) issues.push(vIssue);
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
],
|
|
118
|
-
|
|
119
|
-
|
|
112
|
+
// link 附加件以 marker 为准:默认只有 5 实体、无服务软链;装了才加
|
|
113
|
+
// link-core(转发口只取决于装没装,不是运行时开关)
|
|
114
|
+
const linkInstalled = fs.existsSync(paths.linkEnabledFile);
|
|
115
|
+
/** @type {[string,string][]} */
|
|
116
|
+
const entities = [
|
|
117
|
+
['sn-probe', 'sn-probe'],
|
|
118
|
+
['sn-route', 'sn-route'],
|
|
119
|
+
['sn-ipv6', 'sn-ipv6'],
|
|
120
|
+
['sn-signal', 'sn-signal'],
|
|
121
|
+
['sn-notify', 'sn-notify'],
|
|
122
|
+
];
|
|
123
|
+
/** @type {[string,string][]} */
|
|
124
|
+
const serviceLinks = [];
|
|
125
|
+
if (linkInstalled) {
|
|
126
|
+
entities.push(['ssh-net-link-core', 'ssh-net-link-core']);
|
|
127
|
+
serviceLinks.push([linkLinkName(topo.core.alias), 'ssh-net-link-core']);
|
|
128
|
+
}
|
|
129
|
+
const { issues: rtIssues, links } = runtimeIssues(ctx, { entities, serviceLinks });
|
|
120
130
|
issues.push(...rtIssues);
|
|
121
131
|
|
|
122
|
-
//
|
|
132
|
+
// 片段体检(sn-route 实体在才查:没装时 rtIssues 已报「尚未安装」):
|
|
123
133
|
// routes 被手删/过期会让 ssh 静默退回 lan 直连;vscode-mark 缺失则 VS Code
|
|
124
134
|
// 连接不打标、dot.via 不刷新——都必须显眼
|
|
125
|
-
if (links.find((l) => l.link === '
|
|
126
|
-
const routeAudit = auditConfFragment(paths.routesFile, renderRoutes(topo));
|
|
135
|
+
if (links.find((l) => l.link === 'sn-route')?.audit === 'ok') {
|
|
136
|
+
const routeAudit = auditConfFragment(paths.routesFile, renderRoutes(topo, ctx.self.alias));
|
|
127
137
|
if (routeAudit !== 'ok')
|
|
128
138
|
issues.push(`选路片段 conf.d/routes ${routeAudit === 'missing' ? '缺失' : '与 topology 不一致'}——跑一次 ssh-net install 刷新`);
|
|
129
139
|
const markAudit = auditConfFragment(paths.vscodeMarkFile, renderVscodeMark());
|
|
@@ -135,26 +145,22 @@ async function collectEdge(ctx) {
|
|
|
135
145
|
const job = launchd.inspect(ctx.runner, ctx.uid, label);
|
|
136
146
|
const switches = state.readSwitches();
|
|
137
147
|
|
|
138
|
-
// 非法组合:peer
|
|
139
|
-
//
|
|
140
|
-
//
|
|
148
|
+
// 非法组合:peer 依赖 relay。开关是「文件即接口」——手 touch/rm state 里
|
|
149
|
+
// 的标记可造出组合(设计接受手改,这里负责显眼指出,而不是让线路行各说
|
|
150
|
+
// 各话)
|
|
141
151
|
if (switches.peer && !switches.relay)
|
|
142
|
-
issues.push('开关组合异常:peer 开但 relay 关(peer 依赖 relay,多半是 state
|
|
143
|
-
if (switches.link && !switches.relay)
|
|
144
|
-
issues.push('开关组合异常:link 开但 relay 关(link 依赖 relay,多半是 state 里的开关文件被手动改动)——跑一次 ssh-net stop 清零后再按需要的 start');
|
|
152
|
+
issues.push('开关组合异常:peer 开但 relay 关(peer 依赖 relay,多半是 state 里的开关被手动改动)——跑一次 ssh-net stop 后再按需要 start');
|
|
145
153
|
|
|
146
|
-
//
|
|
147
|
-
//
|
|
148
|
-
// TCP 探测随其一起发起,最坏耗时取决于最慢一条
|
|
154
|
+
// 就绪实测并发(On 是意图、ready 是事实;开关关的线路不探测=null)。
|
|
155
|
+
// 转发口只有装了 link 才可能存在——默认形态不探
|
|
149
156
|
const relayOk = ctx.relayOk ?? relayPathOk; // 测试可注入
|
|
150
157
|
const openTcp = ctx.openTcp ?? tcpOpen;
|
|
151
158
|
const probes = [
|
|
152
159
|
switches.relay ? relayOk(ctx) : null,
|
|
153
160
|
switches.relay && switches.peer ? peerProbeOk(ctx.runner, paths.binDir) : null,
|
|
154
|
-
// link 关也探:软生效窗口里旧转发连接的口还在听(「关」不等于口已消失),
|
|
155
|
-
// status 要能区分「现有连接保持中」与「彻底没了」
|
|
156
|
-
...topo.forwards.map((f) => openTcp('127.0.0.1', f.listenPort)),
|
|
157
161
|
];
|
|
162
|
+
if (linkInstalled)
|
|
163
|
+
for (const f of topo.forwards) probes.push(openTcp('127.0.0.1', f.listenPort));
|
|
158
164
|
// relay 开但 core 的 LAN 22 可达 = 人在办公室忘了关(只提示不纠偏)。
|
|
159
165
|
// core 的 LAN IP 不进 topology,从 ~/.ssh/config 的静态 Host 块取(契约校验
|
|
160
166
|
// 已保证存在),不读 routes——Match 会把地址改写成环回口
|
|
@@ -165,21 +171,22 @@ async function collectEdge(ctx) {
|
|
|
165
171
|
const relayReady = got[0];
|
|
166
172
|
const peerReady = got[1];
|
|
167
173
|
const ports = {};
|
|
168
|
-
|
|
174
|
+
if (linkInstalled)
|
|
175
|
+
topo.forwards.forEach((f, i) => { ports[f.listenPort] = got[2 + i]; });
|
|
169
176
|
const lanReachable = lanIdx >= 0 ? got[lanIdx] : false;
|
|
170
177
|
|
|
171
178
|
if (lanReachable)
|
|
172
179
|
issues.push(`检测到 ${topo.core.alias}(${coreLanIp})局域网直连可达——隧道可能在空转绕中继;人在办公室可 ssh-net stop`);
|
|
173
180
|
|
|
174
|
-
// relay
|
|
175
|
-
if (!switches.relay)
|
|
181
|
+
// relay 关但转发口仍在听 = 孤儿进程(仅装了 link 才可能)
|
|
182
|
+
if (!switches.relay && linkInstalled)
|
|
176
183
|
for (const f of topo.forwards)
|
|
177
184
|
if (ports[f.listenPort])
|
|
178
185
|
issues.push(`隧道已关但 ${forwardLabel(f)} 仍被占用——疑似残留进程:lsof -nP -iTCP:${f.listenPort} -sTCP:LISTEN 查占用者`);
|
|
179
186
|
|
|
180
187
|
// loopback 转发的出口在 core 本机环回(microsocks),ssh-net 管辖外:
|
|
181
|
-
//
|
|
182
|
-
if (
|
|
188
|
+
// 未就绪给根因提示,别让排障只在 edge 侧打转
|
|
189
|
+
if (linkInstalled)
|
|
183
190
|
for (const f of topo.forwards)
|
|
184
191
|
if (f.kind === 'local' && f.targetAlias === 'loopback' && !ports[f.listenPort])
|
|
185
192
|
issues.push(`${forwardLabel(f)} 未就绪:edge 侧正常时多半是 core 上的出口代理(microsocks)没跑——到 core 上查 local.microsocks`);
|
|
@@ -196,13 +203,15 @@ async function collectEdge(ctx) {
|
|
|
196
203
|
v6ProbeOk = !probe.error && probe.code === 0;
|
|
197
204
|
}
|
|
198
205
|
|
|
199
|
-
const forwardViews =
|
|
200
|
-
|
|
201
|
-
|
|
206
|
+
const forwardViews = linkInstalled
|
|
207
|
+
? topo.forwards.map((f) => [forwardLabel(f), ports[f.listenPort]])
|
|
208
|
+
: [];
|
|
209
|
+
// 线路通知组件:未装不算故障(sn-notify 静默回退 osascript/notify-send)
|
|
202
210
|
const notify = ctx.tnPath === undefined ? detectTerminalNotifier() : ctx.tnPath;
|
|
203
211
|
return {
|
|
204
212
|
role: 'edge', alias: ctx.self.alias, switches, relayReady, peerReady,
|
|
205
|
-
job, links, ports, forwardViews,
|
|
213
|
+
linkInstalled, job, links, ports, forwardViews,
|
|
214
|
+
coreAlias: topo.core.alias, relayAlias: topo.relay.alias,
|
|
206
215
|
v6, v6ProbeOk, runtimeVersion, issues, notify,
|
|
207
216
|
};
|
|
208
217
|
}
|
|
@@ -248,9 +257,10 @@ async function collectCore(ctx) {
|
|
|
248
257
|
);
|
|
249
258
|
if (linkState !== 'ok') bad.push(`软链(${linkState})`);
|
|
250
259
|
if (bad.length)
|
|
251
|
-
//
|
|
252
|
-
//
|
|
253
|
-
|
|
260
|
+
// 已注册 edge 的实体/软链其实会被 install 刷新(26.9.19 起),但
|
|
261
|
+
// 报名是自愈通道且不用人手动跑——按「等下次报名」引导,想立即修
|
|
262
|
+
// 也可跑 ssh-net install
|
|
263
|
+
issues.push(`${e.alias} 的回拨组件异常(${bad.join('、')})——它下次报名时会自动刷新(或跑一次 ssh-net install 手动修复)`);
|
|
254
264
|
}
|
|
255
265
|
const v6 = ctx.state.readEdgeV6(e.alias);
|
|
256
266
|
// edge 入口片段体检:job 在册且未休眠(enabled)+ 有 v6 时,片段必须
|
|
@@ -279,8 +289,14 @@ async function collectCore(ctx) {
|
|
|
279
289
|
|
|
280
290
|
const relayPort = await relayPortP;
|
|
281
291
|
if (!relayJob.running) issues.push('relay 未在运行——ssh-net start 可拉起');
|
|
282
|
-
if (!relayPort.listening)
|
|
283
|
-
|
|
292
|
+
if (!relayPort.listening) {
|
|
293
|
+
// 连不上 relay 时入口状态未知,别断言成「没在监听」——那是把故障点
|
|
294
|
+
// 指向本机转发,实际更可能是 relay 不可达(排障方向完全不同)
|
|
295
|
+
if (relayPort.reachable === false)
|
|
296
|
+
issues.push(`连不上 ${topo.relay.alias}(${relayPort.detail || 'ssh 探测失败'}),入口 ${topo.core.relayPort} 状态未知——查 ${topo.relay.alias} 可达性或本机网络`);
|
|
297
|
+
else
|
|
298
|
+
issues.push(`${topo.relay.alias} 入口 ${topo.core.relayPort} 没在监听:在外面暂时进不来(断线会自动重连;持续出现请查 ${topo.relay.alias} 或本机网络)`);
|
|
299
|
+
}
|
|
284
300
|
|
|
285
301
|
// topology 已删(edge 改名/退役)但磁盘上还有回拨 job:不渲染成 edges 行
|
|
286
302
|
//(state/conf.d 都按新别名走),单独点破。残留无害——六连败后自休眠、
|
|
@@ -317,8 +333,13 @@ async function collectNode(ctx) {
|
|
|
317
333
|
const relayPort = await relayPortListening(ctx, ctx.self.relayPort);
|
|
318
334
|
if (active !== 'active')
|
|
319
335
|
issues.push(`系统服务 ${unit} 状态 ${active}——systemctl status ${unit} 查看`);
|
|
320
|
-
if (!relayPort.listening)
|
|
321
|
-
|
|
336
|
+
if (!relayPort.listening) {
|
|
337
|
+
// 与 core 侧同款区分:relay 不可达 ≠ 入口没在监听,排障方向不同
|
|
338
|
+
if (relayPort.reachable === false)
|
|
339
|
+
issues.push(`连不上 ${topo.relay.alias}(${relayPort.detail || 'ssh 探测失败'}),入口 ${ctx.self.relayPort} 状态未知——查 ${topo.relay.alias} 可达性或本机网络`);
|
|
340
|
+
else
|
|
341
|
+
issues.push(`${ctx.topo.relay.alias} 入口 ${ctx.self.relayPort} 没在监听:在外面暂时进不来(断线会自动重连;持续出现请查 ${ctx.topo.relay.alias} 或本机网络)`);
|
|
342
|
+
}
|
|
322
343
|
|
|
323
344
|
return {
|
|
324
345
|
role: 'node', alias: ctx.self.alias, runtimeVersion,
|
|
@@ -344,12 +365,12 @@ function lineOf(name, on, ready, okText, failText, offText) {
|
|
|
344
365
|
: ` ${name}: ✗ ${failText}`;
|
|
345
366
|
}
|
|
346
367
|
|
|
347
|
-
/**
|
|
348
|
-
function profileOf(sw) {
|
|
368
|
+
/** 开关+附加件 → 一行人话总结(回答「我现在是什么形态」)。 */
|
|
369
|
+
function profileOf(sw, linkInstalled) {
|
|
349
370
|
if (!sw.relay) return null; // 全关有独立文案,不加形态行
|
|
350
|
-
if (sw.relay && sw.peer &&
|
|
371
|
+
if (sw.relay && sw.peer && linkInstalled) return '全功能:core 直连优先,回拨与本地转发口都开';
|
|
351
372
|
if (sw.relay && sw.peer) return '被连接形态:core 可回拨进来,无本地转发口';
|
|
352
|
-
if (sw.relay &&
|
|
373
|
+
if (sw.relay && linkInstalled) return '仅本地转发:经中继出去,core 不回拨';
|
|
353
374
|
return '仅中继信道(待命:不报名、无本地转发口)';
|
|
354
375
|
}
|
|
355
376
|
|
|
@@ -359,11 +380,11 @@ export function renderText(s) {
|
|
|
359
380
|
|
|
360
381
|
if (s.role === 'edge') {
|
|
361
382
|
const sw = s.switches;
|
|
362
|
-
if (!sw.relay && !sw.peer
|
|
383
|
+
if (!sw.relay && !sw.peer) {
|
|
363
384
|
lines.push(' 开关: 全关(在办公室,局域网直连即可;需要时 ssh-net start)');
|
|
364
385
|
} else {
|
|
365
|
-
lines.push(` 开关: relay ${sw.relay ? '开' : '关'} · peer ${sw.peer ? '开' : '关'}
|
|
366
|
-
lines.push(` 形态: ${profileOf(sw)}`);
|
|
386
|
+
lines.push(` 开关: relay ${sw.relay ? '开' : '关'} · peer ${sw.peer ? '开' : '关'}`);
|
|
387
|
+
lines.push(` 形态: ${profileOf(sw, s.linkInstalled)}`);
|
|
367
388
|
lines.push(lineOf('relay', sw.relay, s.relayReady,
|
|
368
389
|
`可用(经 ${s.coreAlias} 的中继信道)`,
|
|
369
390
|
`不可用(经 ${s.relayAlias} 连不上 core;刚开启请等几秒再看,持续出现查 ${s.relayAlias} 或 core 侧 ssh-net status)`,
|
|
@@ -378,20 +399,15 @@ export function renderText(s) {
|
|
|
378
399
|
`已就绪(v6 直达 ${s.coreAlias},一条 ssh)`,
|
|
379
400
|
peerUnready,
|
|
380
401
|
'按开关:不报名,core 不会回拨'));
|
|
381
|
-
if (!
|
|
382
|
-
//
|
|
383
|
-
|
|
384
|
-
// 都属非预期,文案直接说破而不是按软关常态描述
|
|
385
|
-
const lingering = s.forwardViews.some(([, ok]) => ok);
|
|
386
|
-
lines.push(lingering
|
|
387
|
-
? ' link: 关(按开关;转发口仍在听属非预期——stop link 本应立即使其断开:开关文件被手改过,或当时断开请求失败。要立即断开跑 ssh-net restart)'
|
|
388
|
-
: ' link: 关(按开关,本地无转发口;core 仍可经回拨进入本机)');
|
|
402
|
+
if (!s.linkInstalled) {
|
|
403
|
+
// 默认形态不探转发口(没有生产者会去占它们)
|
|
404
|
+
lines.push(' link: 未安装(默认零常驻;要 SOCKS/VNC 转发口:ssh-net install --with-link)');
|
|
389
405
|
} else {
|
|
390
406
|
const views = s.forwardViews;
|
|
391
407
|
const allOk = views.length > 0 && views.every(([, ok]) => ok);
|
|
392
408
|
lines.push(allOk
|
|
393
409
|
? ` link: ✓ 就绪(${views.map(([l]) => l).join(' · ')})`
|
|
394
|
-
: ` link: ✗ 未就绪(${views.filter(([, ok]) => !ok).map(([l]) => l).join('、')}
|
|
410
|
+
: ` link: ✗ 未就绪(${views.filter(([, ok]) => !ok).map(([l]) => l).join('、')} 没在听;隧道建立中)`);
|
|
395
411
|
}
|
|
396
412
|
lines.push(s.v6 !== ''
|
|
397
413
|
? ` 对外地址: ${s.v6}`
|
|
@@ -399,7 +415,7 @@ export function renderText(s) {
|
|
|
399
415
|
? ' 对外地址: 未知(v6 探测没跑成:sn-ipv6 超时或异常——vpn 重环境下 route 查询可能拖过探测时限,稍后再试)'
|
|
400
416
|
: ` 对外地址: 无 v6${sw.peer ? '(peer 建不起来,只能走 relay)' : ''}`);
|
|
401
417
|
}
|
|
402
|
-
lines.push(` 后台任务: ${!s.job.registered ?
|
|
418
|
+
lines.push(` 后台任务: ${!s.job.registered ? `未装 link(默认零常驻)` : s.job.running ? 'link 运行中' : 'link 已停止'}`);
|
|
403
419
|
// 通知行按平台分文案:terminal-notifier/osascript/brew 是 mac 世界的
|
|
404
420
|
// 东西;Linux(node 角色)的兜底是 notify-send,照实说,别引导 brew
|
|
405
421
|
if (s.notify)
|
|
@@ -432,7 +448,7 @@ export function renderText(s) {
|
|
|
432
448
|
} else {
|
|
433
449
|
lines.push(s.relayPort.listening
|
|
434
450
|
? ` relay: ✓ 开着(在外面经 ${s.relayAlias} 进入 ${s.alias} · 入口 ${s.relayPort.port})`
|
|
435
|
-
: ` relay: ✗
|
|
451
|
+
: ` relay: ✗ ${s.relayPort.reachable === false ? `连不上 ${s.relayAlias},入口状态未知` : `入口 ${s.relayPort.port} 没在监听`}`);
|
|
436
452
|
lines.push(` relay 进程: ${s.active === 'active' ? '运行中(systemd)' : `${s.active}(systemd)`}`);
|
|
437
453
|
}
|
|
438
454
|
|
|
@@ -1,21 +1,24 @@
|
|
|
1
|
-
// stop(design §5):edge
|
|
2
|
-
//
|
|
3
|
-
// design §5
|
|
1
|
+
// stop(design §5):edge 关两开关——缺省或 -a/--all(或带 relay)=级联全关
|
|
2
|
+
// (删两开关 + TERM 纯 ssh job 并校验真退出);只关 peer 是软关(只删标记,
|
|
3
|
+
// 零闪断,design §5「软生效」)。link 不是运行时目标(移除附加件用
|
|
4
|
+
// install --without-link)。重复 stop 是幂等确认,措辞区分「已关闭」与「已是
|
|
4
5
|
// 关闭状态」。core disable+bootout uplink-relay(不动 uplink-edge);
|
|
5
6
|
// node stop+disable。
|
|
6
7
|
import * as launchd from '../lib/launchd.mjs';
|
|
7
8
|
import * as systemd from '../lib/systemd.mjs';
|
|
8
9
|
import { stopEdgeLink } from '../lib/jobctl.mjs';
|
|
9
|
-
import {
|
|
10
|
+
import { relayLabel, relayUnitName } from '../lib/render.mjs';
|
|
10
11
|
|
|
11
|
-
const TARGETS = ['relay', 'peer'
|
|
12
|
+
const TARGETS = ['relay', 'peer'];
|
|
12
13
|
|
|
13
14
|
function parseTarget(args) {
|
|
14
15
|
if (args.length === 0 || args.length === 1 && (args[0] === '-a' || args[0] === '--all'))
|
|
15
16
|
return null; // 全关
|
|
16
|
-
if (args.length
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
if (args.length === 1 && TARGETS.includes(args[0]))
|
|
18
|
+
return args[0];
|
|
19
|
+
if (args.length === 1 && args[0] === 'link')
|
|
20
|
+
throw new Error('link 不是运行时目标:移除附加件跑 ssh-net install --without-link(卸载会一并清掉)');
|
|
21
|
+
throw new Error('用法:stop [-a|--all|relay|peer](不带参数或 -a=全部关闭)');
|
|
19
22
|
}
|
|
20
23
|
|
|
21
24
|
function stopCore(ctx) {
|
|
@@ -37,29 +40,12 @@ export function run(ctx, args = []) {
|
|
|
37
40
|
if (target === 'peer') {
|
|
38
41
|
ctx.state.removeOn(['peer']);
|
|
39
42
|
if (!before.peer) return 'peer 已是关闭状态(无需改动)';
|
|
40
|
-
//
|
|
41
|
-
|
|
42
|
-
return
|
|
43
|
-
? 'peer 已关闭(软生效:停止报名,已建立的回拨随网络变化自然消亡;下一轮循环起不再报名)'
|
|
44
|
-
: 'peer 已关闭(软生效:停止报名;后台任务未在运行,开启时会读取本开关)';
|
|
43
|
+
// 软关:只停止报名。已建立的连接继续用;装了 link,其转发 ssh 重连
|
|
44
|
+
// 时 sn-route 不报名,自然只走中继
|
|
45
|
+
return 'peer 已关闭(软生效:停止报名。已建立的连接继续用,断了重连就只走中继)';
|
|
45
46
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
if (!before.link) return 'link 已是关闭状态(无需改动)';
|
|
49
|
-
// 立即生效:kickstart 断当前转发 ssh(承载的正是用户要关的东西,不算
|
|
50
|
-
// 闪断),循环重来后不再起。job 没跑就不用动(本来也没转发口)
|
|
51
|
-
let off = '本地转发口已断开';
|
|
52
|
-
if (launchd.inspect(ctx.runner, ctx.uid, linkLabel(ctx.topo.core.alias)).registered) {
|
|
53
|
-
const r = launchd.kickstartEnabled(ctx.runner, ctx.uid, linkLabel(ctx.topo.core.alias));
|
|
54
|
-
if (r.code !== 0)
|
|
55
|
-
off = '断开请求未生效(现有连接会随其自然断开而消失;要立即断开跑 ssh-net restart)';
|
|
56
|
-
} else {
|
|
57
|
-
off = '后台任务未在运行,本就无转发口';
|
|
58
|
-
}
|
|
59
|
-
return `link 已关闭(${off};被连接方向不受影响——core 仍可经回拨进入本机)`;
|
|
60
|
-
}
|
|
61
|
-
// 全关 / 只关 relay:级联删三个(peer/link 依赖 relay),硬停 job
|
|
62
|
-
const alreadyAllOff = !before.relay && !before.peer && !before.link;
|
|
47
|
+
// 全关 / 只关 relay:级联删两开关,硬停 link job(未装附加件则直接 gone)
|
|
48
|
+
const alreadyAllOff = !before.relay && !before.peer;
|
|
63
49
|
const { gone, label } = stopEdgeLink(ctx);
|
|
64
50
|
if (!gone)
|
|
65
51
|
throw new Error(
|
|
@@ -68,11 +54,11 @@ export function run(ctx, args = []) {
|
|
|
68
54
|
if (target === null) {
|
|
69
55
|
return alreadyAllOff
|
|
70
56
|
? '隧道已是关闭状态(无需改动)'
|
|
71
|
-
: '
|
|
57
|
+
: '隧道已关闭。几分钟后 core 侧会停止回拨并静默(正常现象,下次开启自动恢复)';
|
|
72
58
|
}
|
|
73
59
|
return alreadyAllOff
|
|
74
|
-
? 'relay 已是关闭状态(peer
|
|
75
|
-
: 'relay 已关闭(peer
|
|
60
|
+
? 'relay 已是关闭状态(peer 本就没开)'
|
|
61
|
+
: 'relay 已关闭(peer 一并关闭——它依赖 relay)。几分钟后 core 侧会停止回拨并静默';
|
|
76
62
|
}
|
|
77
63
|
// core/node 是单总开关(等同 --all):显式 -a/--all 收下,其余参数拒绝
|
|
78
64
|
if (args.length && !(args.length === 1 && (args[0] === '-a' || args[0] === '--all')))
|
|
@@ -32,6 +32,7 @@ function uninstallEdge(ctx) {
|
|
|
32
32
|
rm(ctx.paths.stateDir);
|
|
33
33
|
rm(ctx.paths.confDir); // routes(edge 装的);node 上不存在无害
|
|
34
34
|
rm(ctx.paths.runtimeVersionFile);
|
|
35
|
+
rm(ctx.paths.linkEnabledFile); // link 附加件意图
|
|
35
36
|
rm(path.join(ctx.paths.logsDir, `${label}.log`));
|
|
36
37
|
return '已卸载。最后一步:npm uninstall -g ssh-net';
|
|
37
38
|
}
|
|
@@ -10,8 +10,9 @@
|
|
|
10
10
|
// 跑 install 会盖一个旧版本戳、越对账越乱。
|
|
11
11
|
//
|
|
12
12
|
// 边界:node 角色需要 root(install 写 /etc/systemd/system),先检查再动
|
|
13
|
-
// npm,避免「包装上了、install 被拒」的半截态;core
|
|
14
|
-
//
|
|
13
|
+
// npm,避免「包装上了、install 被拒」的半截态;core 侧**已注册** edge 的
|
|
14
|
+
// uplink-edge 实体随 install 刷新(26.9.19 起),job 参数仍等各 edge 下次
|
|
15
|
+
// 报名对账(design §5)。
|
|
15
16
|
import { readRuntimeVersion, RUNTIME_VERSION } from '../lib/runtime.mjs';
|
|
16
17
|
|
|
17
18
|
const NPM_TIMEOUT = 180_000; // 慢网络下全局装包的余量
|
|
@@ -21,14 +22,19 @@ export async function run(ctx) {
|
|
|
21
22
|
if (ctx.role === 'node' && ctx.uid !== 0)
|
|
22
23
|
throw new Error('node 上升级要写 /etc/systemd/system(install 步骤),请以 root 运行(sudo ssh-net upgrade)');
|
|
23
24
|
|
|
24
|
-
|
|
25
|
+
// 首跑就带 --prefer-online(notes #10,2026-10-02 落地):npm 默认读本地
|
|
26
|
+
// packument 缓存的 dist-tags,刚发布后 latest 仍旧值时不报 ETARGET 而是
|
|
27
|
+
// 「成功」装回旧版——ETARGET 分支只盖「显式版本比元数据新」,@latest 解析
|
|
28
|
+
// 完全绕过它。跳过缓存读 dist-tags 一劳永逸,慢个几秒可接受
|
|
29
|
+
const npmArgs = ['install', '-g', 'ssh-net@latest', '--prefer-online'];
|
|
25
30
|
let r = ctx.runner.run('npm', npmArgs, { timeout: NPM_TIMEOUT });
|
|
26
31
|
if (r.error && r.error.code === 'ENOENT')
|
|
27
32
|
throw new Error('PATH 上找不到 npm(非交互环境的 node/npm 由 dotfiles 保证,见 design §7.5)——检查 ~/.zshenv 或 .bashrc 登录链后重试');
|
|
28
33
|
if (r.code !== 0 && /ETARGET/.test(r.stderr)) {
|
|
29
|
-
//
|
|
30
|
-
//
|
|
31
|
-
|
|
34
|
+
// 剩余传播窗口:本机 --prefer-online 也绕不开 registry 各边缘节点间的
|
|
35
|
+
// 元数据不一致(m5 实踩:另一台已能 view 到新版、这台仍报无此版本)。
|
|
36
|
+
// 再试一次(覆盖另一台边缘节点/瞬时网络),仍失败才让人等
|
|
37
|
+
r = ctx.runner.run('npm', npmArgs, { timeout: NPM_TIMEOUT });
|
|
32
38
|
if (r.code !== 0)
|
|
33
39
|
throw new Error(
|
|
34
40
|
`安装最新版失败(ETARGET:registry 元数据还没传播过来,常见于刚发布几分钟内)——等几分钟再跑 ssh-net upgrade,或手动 npm i -g ssh-net@<版本> --prefer-online。npm 输出:${r.stderr.trim()}`,
|
|
@@ -58,6 +64,6 @@ export async function run(ctx) {
|
|
|
58
64
|
// 文件已全部对账,但**在跑的 job 进程仍是旧脚本**(install 幂等不断连,
|
|
59
65
|
// 不重启健康服务)——新行为要等进程自然重建(断线/重启)后生效;多数
|
|
60
66
|
// 运行时改动不影响已建立的隧道,不主动 restart(那会把连接断掉)
|
|
61
|
-
const rollover = '(在跑的隧道进程继续用旧脚本直到下次重建;如需立刻加载新行为,ssh-net restart 会断当前连接。core
|
|
67
|
+
const rollover = '(在跑的隧道进程继续用旧脚本直到下次重建;如需立刻加载新行为,ssh-net restart 会断当前连接。core 侧的回拨组件:已注册的随本次 install 刷新、job 参数随各 edge 下次报名对账)';
|
|
62
68
|
return `${upToDate ? '已是最新版本' : '升级完成'}。${inst.stdout.trim()}${rollover}`;
|
|
63
69
|
}
|
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') =>
|