ssh-net 26.10.55 → 26.10.57
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 +36 -0
- package/README.md +32 -21
- package/libexec/cli.mjs +5 -7
- package/libexec/commands/install.mjs +56 -31
- package/libexec/commands/register.mjs +14 -82
- package/libexec/commands/start.mjs +37 -11
- package/libexec/commands/status.mjs +86 -71
- package/libexec/commands/update.mjs +18 -24
- package/libexec/lib/manage.mjs +5 -2
- package/libexec/lib/probe.mjs +3 -3
- package/libexec/lib/render.mjs +6 -8
- package/libexec/lib/runtime.mjs +0 -35
- package/libexec/lib/systemd.mjs +5 -5
- package/libexec/sn-register-lan +3 -3
- package/libexec/sn-route +68 -38
- package/package.json +1 -1
- package/libexec/commands/doctor.mjs +0 -44
- package/libexec/commands/restart.mjs +0 -37
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
// status
|
|
1
|
+
// status:默认只查本机开关、后台任务与安装产物;--probe 增加线路实测。
|
|
2
2
|
// edge 的 peer 用 banner 探测,即使 peer 软关闭也报告已有直连;core 的 edge 行
|
|
3
3
|
// 只表示 launchd 连接任务,不把进程 running 误写成线路已连接。
|
|
4
|
-
// 另检查安装产物一致性(bin/ 实体、服务软链、版本戳、ssh 配置片段)。
|
|
5
4
|
// 退出码:无「需要处理」事项 0;有可行动问题 1。线路关闭或 peer 暂不可用不等于
|
|
6
|
-
//
|
|
5
|
+
// 安装故障;实测发现中继不可用会进入处理项。
|
|
7
6
|
import fs from 'node:fs';
|
|
8
7
|
import {
|
|
9
8
|
auditScript, auditLink, runtimeVersionIssue,
|
|
@@ -24,8 +23,8 @@ import {
|
|
|
24
23
|
} from '../lib/render.mjs';
|
|
25
24
|
|
|
26
25
|
/**
|
|
27
|
-
*
|
|
28
|
-
* @returns {{issues:string[], entityAudits:{name:string,audit:string}[]}}
|
|
26
|
+
* 各角色共用的实体+软链体检;完全未安装只报根因,不级联报版本戳和悬链。
|
|
27
|
+
* @returns {{issues:string[], entityAudits:{name:string,audit:string}[], allMissing:boolean}}
|
|
29
28
|
*/
|
|
30
29
|
function runtimeIssues(ctx, { entities, links }) {
|
|
31
30
|
const issues = [];
|
|
@@ -33,27 +32,31 @@ function runtimeIssues(ctx, { entities, links }) {
|
|
|
33
32
|
name,
|
|
34
33
|
audit: auditScript(ctx.paths.binScript(name), packageScriptPath(script)),
|
|
35
34
|
}));
|
|
35
|
+
const allMissing = entityAudits.length > 0
|
|
36
|
+
&& entityAudits.every((a) => a.audit === 'missing');
|
|
36
37
|
if (entityAudits.some((a) => a.audit === 'stale'))
|
|
37
38
|
issues.push('后台脚本与所装版本不一致——跑一次 ssh-net install 刷新');
|
|
38
|
-
if (
|
|
39
|
+
if (allMissing) {
|
|
39
40
|
issues.push('尚未安装(先跑 ssh-net install)');
|
|
40
|
-
else
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
41
|
+
} else {
|
|
42
|
+
if (entityAudits.some((a) => a.audit === 'missing'))
|
|
43
|
+
// 部分缺失:探针丢了会让开着的隧道恒判中继,不能只显示不报
|
|
44
|
+
issues.push(`后台脚本不完整(缺 ${entityAudits.filter((a) => a.audit === 'missing').map((a) => a.name).join('、')})——跑一次 ssh-net install 修复`);
|
|
45
|
+
for (const [linkName, entityName] of links) {
|
|
46
|
+
const r = auditLink(ctx.paths.binScript(linkName), entityName);
|
|
47
|
+
if (r !== 'ok')
|
|
48
|
+
issues.push(`后台脚本异常(${linkName}:${r})——跑一次 ssh-net install 修复`);
|
|
49
|
+
}
|
|
47
50
|
}
|
|
48
|
-
return { issues, entityAudits };
|
|
51
|
+
return { issues, entityAudits, allMissing };
|
|
49
52
|
}
|
|
50
53
|
|
|
51
54
|
/** 三个 collector 的共同前奏:版本戳问题 + 实体/软链体检。 */
|
|
52
55
|
function baseAudit(ctx, plan) {
|
|
53
56
|
const issues = [];
|
|
57
|
+
const { issues: rtIssues, entityAudits, allMissing } = runtimeIssues(ctx, plan);
|
|
54
58
|
const vIssue = runtimeVersionIssue(ctx.paths);
|
|
55
|
-
if (vIssue) issues.push(vIssue);
|
|
56
|
-
const { issues: rtIssues, entityAudits } = runtimeIssues(ctx, plan);
|
|
59
|
+
if (vIssue && !allMissing) issues.push(vIssue);
|
|
57
60
|
issues.push(...rtIssues);
|
|
58
61
|
return { issues, entityAudits };
|
|
59
62
|
}
|
|
@@ -130,16 +133,16 @@ async function relayPortListening(ctx, port) {
|
|
|
130
133
|
/** edge 的 relay 线路实测:命令行显式 -J 经 relay 跑一条真 ssh(字面
|
|
131
134
|
* 127.0.0.1 不命中 Match,绕开自动选路)。探测不接受新 host key——与无人
|
|
132
135
|
* 值守报名的 accept-new 是刻意分级(design §4.3)。 */
|
|
133
|
-
async function
|
|
136
|
+
async function relayPathProbe(ctx) {
|
|
134
137
|
const r = await ctx.runner.runAsync(
|
|
135
138
|
'ssh',
|
|
136
139
|
coreViaRelayArgs(ctx.topo, { connectTimeout: 5, remoteCommand: 'true' }),
|
|
137
140
|
{ timeout: 8000 },
|
|
138
141
|
);
|
|
139
|
-
return r.code === 0;
|
|
142
|
+
return { ready: r.code === 0, detail: r.stderr.trim() };
|
|
140
143
|
}
|
|
141
144
|
|
|
142
|
-
async function collectEdge(ctx) {
|
|
145
|
+
async function collectEdge(ctx, probe) {
|
|
143
146
|
const { topo, paths, state } = ctx;
|
|
144
147
|
const linkInstalled = fs.existsSync(paths.linkEnabledFile);
|
|
145
148
|
const plan = edgeRuntimePlan(topo, linkInstalled);
|
|
@@ -163,7 +166,7 @@ async function collectEdge(ctx) {
|
|
|
163
166
|
|
|
164
167
|
// v6 只是诊断线索不是判决,与就绪实测并发。探测没跑成时不能谎称「没有
|
|
165
168
|
// IPv6」——那个结论只在 sn-ipv6 正常跑完且无输出时才成立
|
|
166
|
-
const needV6 = switches.relay
|
|
169
|
+
const needV6 = probe && switches.relay
|
|
167
170
|
&& entityAudits.some((a) => a.name === SN_IPV6 && a.audit === 'ok');
|
|
168
171
|
const v6P = needV6
|
|
169
172
|
? ctx.runner.runRawAsync(paths.binScript(SN_IPV6), [], { timeout: 5000 })
|
|
@@ -171,15 +174,17 @@ async function collectEdge(ctx) {
|
|
|
171
174
|
|
|
172
175
|
// 就绪实测并发。relay 关闭时不探测;peer 软关闭只停止主动恢复,已有直连
|
|
173
176
|
// 仍可能被新 ssh 使用,所以只要 relay 开着就继续做 banner 探测
|
|
174
|
-
const
|
|
177
|
+
const relayProbe = ctx.relayOk ?? relayPathProbe; // 测试可注入同形结果
|
|
175
178
|
const openTcp = ctx.openTcp ?? tcpOpen;
|
|
176
|
-
const forwards = linkInstalled && switches.relay ? topo.forwards : [];
|
|
177
|
-
const [
|
|
178
|
-
switches.relay ?
|
|
179
|
-
switches.relay ? peerProbeOk(ctx.runner, paths.binDir) : null,
|
|
179
|
+
const forwards = probe && linkInstalled && switches.relay ? topo.forwards : [];
|
|
180
|
+
const [relayResult, peerReady, portsOk, v6Probe] = await Promise.all([
|
|
181
|
+
probe && switches.relay ? relayProbe(ctx) : null,
|
|
182
|
+
probe && switches.relay ? peerProbeOk(ctx.runner, paths.binDir) : null,
|
|
180
183
|
Promise.all(forwards.map((f) => openTcp('127.0.0.1', f.listenPort))),
|
|
181
184
|
v6P,
|
|
182
185
|
]);
|
|
186
|
+
const relayReady = relayResult?.ready ?? null;
|
|
187
|
+
const relayDetail = relayResult?.detail ?? '';
|
|
183
188
|
|
|
184
189
|
let v6 = '';
|
|
185
190
|
let v6ProbeOk = false;
|
|
@@ -189,11 +194,13 @@ async function collectEdge(ctx) {
|
|
|
189
194
|
v6ProbeOk = c.state !== 'failed';
|
|
190
195
|
}
|
|
191
196
|
|
|
192
|
-
if (switches.relay && relayReady === false)
|
|
193
|
-
|
|
197
|
+
if (probe && switches.relay && relayReady === false) {
|
|
198
|
+
const detail = relayDetail ? `(${relayDetail.replace(/\s*\n\s*/g, ';')})` : '';
|
|
199
|
+
issues.push(`中继线路不可用${detail}——检查 ${topo.relay.alias} 可达性,以及 core 上的 ssh-net status --probe`);
|
|
200
|
+
}
|
|
194
201
|
|
|
195
202
|
return {
|
|
196
|
-
role: 'edge', alias: ctx.self.alias, switches, relayReady, peerReady,
|
|
203
|
+
role: 'edge', alias: ctx.self.alias, switches, probed: probe, relayReady, peerReady,
|
|
197
204
|
linkInstalled, job,
|
|
198
205
|
forwardViews: forwards.map((f, i) => [forwardLabel(f), portsOk[i]]),
|
|
199
206
|
coreAlias: topo.core.alias, relayAlias: topo.relay.alias,
|
|
@@ -201,15 +208,17 @@ async function collectEdge(ctx) {
|
|
|
201
208
|
};
|
|
202
209
|
}
|
|
203
210
|
|
|
204
|
-
async function collectCore(ctx) {
|
|
211
|
+
async function collectCore(ctx, probe) {
|
|
205
212
|
const { topo, paths } = ctx;
|
|
206
213
|
const { issues } = baseAudit(ctx, coreNodeRuntimePlan(topo));
|
|
207
214
|
|
|
208
215
|
// disabled override 是整域同一份字典:relay job 与各 edge job 共用一次拉取
|
|
209
216
|
const dmap = launchd.disabledMap(ctx.runner, ctx.uid);
|
|
210
217
|
const relayJob = launchd.inspectWith(ctx.runner, ctx.uid, relayLabel(topo.relay.alias), dmap);
|
|
211
|
-
//
|
|
212
|
-
const relayPortP =
|
|
218
|
+
// 实测时先发起来,边等边做注册态体检;默认快查不发起 ssh
|
|
219
|
+
const relayPortP = probe
|
|
220
|
+
? relayPortListening(ctx, topo.core.relayPort)
|
|
221
|
+
: Promise.resolve(null);
|
|
213
222
|
|
|
214
223
|
// 回拨实体全 core 一份:循环外体检一次,各已注册 edge 只检各自的软链
|
|
215
224
|
const callbackEntityAudit = topo.edges.length > 0
|
|
@@ -246,38 +255,39 @@ async function collectCore(ctx) {
|
|
|
246
255
|
|
|
247
256
|
const relayPort = await relayPortP;
|
|
248
257
|
if (!relayJob.running) issues.push('relay 未在运行——ssh-net start 可拉起');
|
|
249
|
-
if (!relayPort.listening)
|
|
258
|
+
if (relayPort && !relayPort.listening)
|
|
250
259
|
issues.push(relayPortIssue(topo.relay.alias, topo.core.relayPort, relayPort));
|
|
251
260
|
|
|
252
261
|
return {
|
|
253
|
-
role: 'core', alias: ctx.self.alias,
|
|
262
|
+
role: 'core', alias: ctx.self.alias, probed: probe,
|
|
254
263
|
relayAlias: topo.relay.alias, relayJob, relayPort, edges, issues,
|
|
255
264
|
};
|
|
256
265
|
}
|
|
257
266
|
|
|
258
|
-
async function collectNode(ctx) {
|
|
267
|
+
async function collectNode(ctx, probe) {
|
|
259
268
|
const { topo } = ctx;
|
|
260
269
|
const { issues } = baseAudit(ctx, coreNodeRuntimePlan(topo));
|
|
261
270
|
const entries = edgeEntries(ctx, issues);
|
|
262
271
|
|
|
263
272
|
const unit = relayUnitName(topo.relay.alias);
|
|
264
273
|
const active = systemd.isActive(ctx.runner, unit);
|
|
265
|
-
const relayPort = await relayPortListening(ctx, ctx.self.relayPort);
|
|
274
|
+
const relayPort = probe ? await relayPortListening(ctx, ctx.self.relayPort) : null;
|
|
266
275
|
if (active !== 'active')
|
|
267
276
|
issues.push(`系统服务 ${unit} 状态 ${active}——systemctl status ${unit} 查看`);
|
|
268
|
-
if (!relayPort.listening)
|
|
277
|
+
if (relayPort && !relayPort.listening)
|
|
269
278
|
issues.push(relayPortIssue(topo.relay.alias, ctx.self.relayPort, relayPort));
|
|
270
279
|
|
|
271
280
|
return {
|
|
272
|
-
role: 'node', alias: ctx.self.alias,
|
|
281
|
+
role: 'node', alias: ctx.self.alias, probed: probe,
|
|
273
282
|
relayAlias: topo.relay.alias, active, relayPort, entries, issues,
|
|
274
283
|
};
|
|
275
284
|
}
|
|
276
285
|
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
if (ctx.role === '
|
|
280
|
-
if (ctx.role === '
|
|
286
|
+
/** 默认只做本地采集;需要线路实测的调用方显式传 probe:true。 */
|
|
287
|
+
export async function collect(ctx, { probe = false } = {}) {
|
|
288
|
+
if (ctx.role === 'edge') return collectEdge(ctx, probe);
|
|
289
|
+
if (ctx.role === 'core') return collectCore(ctx, probe);
|
|
290
|
+
if (ctx.role === 'node') return collectNode(ctx, probe);
|
|
281
291
|
throw new Error(`未知角色:${ctx.role}`);
|
|
282
292
|
}
|
|
283
293
|
|
|
@@ -291,8 +301,9 @@ function lineOf(name, ready, okText, failText) {
|
|
|
291
301
|
: ` ${name}:✗ ${failText}`;
|
|
292
302
|
}
|
|
293
303
|
|
|
294
|
-
/** core/node 共用的 relay
|
|
304
|
+
/** core/node 共用的 relay 状态行,未实测不猜端口状态。 */
|
|
295
305
|
function relayStatusLine(s) {
|
|
306
|
+
if (!s.probed) return ' relay:未实测(需要时运行 ssh-net status --probe)';
|
|
296
307
|
return s.relayPort.listening
|
|
297
308
|
? ` relay:✓ 可用(外部可经 ${s.relayAlias} 进入 ${s.alias} · 入口 ${s.relayPort.port})`
|
|
298
309
|
: ` relay:✗ ${s.relayPort.reachable === false ? `连不上 ${s.relayAlias},入口状态未知` : `入口 ${s.relayPort.port} 没在监听`}`;
|
|
@@ -304,34 +315,38 @@ function renderEdgeLines(s, lines) {
|
|
|
304
315
|
lines.push(' 开关:全关(ssh 使用静态 Host 地址;恢复自动选路:ssh-net start)');
|
|
305
316
|
} else {
|
|
306
317
|
lines.push(` 开关:relay 开 · peer ${sw.peer ? '开(允许主动恢复直连)' : '关(只停止主动恢复)'}`);
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
`不可用(经 ${s.relayAlias} 连不上 ${s.coreAlias};刚开启请等几秒,持续出现查 ${s.relayAlias} 或 core 侧 ssh-net status)`));
|
|
310
|
-
const peerUnready = !s.v6ProbeOk
|
|
311
|
-
? '未就绪(本机 IPv6 探测失败,直连状态未知)'
|
|
312
|
-
: s.v6 === ''
|
|
313
|
-
? '未就绪(当前网络没有可用 IPv6)'
|
|
314
|
-
: '未就绪(core 尚未连过来;可运行 ssh-net doctor 立即重试)';
|
|
315
|
-
if (sw.peer) {
|
|
316
|
-
lines.push(lineOf('peer', s.peerReady,
|
|
317
|
-
`IPv6 直连已就绪(到 ${s.coreAlias},一条 ssh)`,
|
|
318
|
-
peerUnready));
|
|
319
|
-
} else if (s.peerReady) {
|
|
320
|
-
lines.push(' peer:✓ 现有 IPv6 直连仍可用(不主动恢复)');
|
|
318
|
+
if (!s.probed) {
|
|
319
|
+
lines.push(' 线路:未实测(需要时运行 ssh-net status --probe)');
|
|
321
320
|
} else {
|
|
322
|
-
lines.push('
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
const
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
321
|
+
lines.push(lineOf('relay', s.relayReady,
|
|
322
|
+
`可用(经 ${s.relayAlias} 连到 ${s.coreAlias})`,
|
|
323
|
+
`不可用(经 ${s.relayAlias} 连不上 ${s.coreAlias};刚开启请等几秒,持续出现查 ${s.relayAlias} 或 core 侧 ssh-net status --probe)`));
|
|
324
|
+
const peerUnready = !s.v6ProbeOk
|
|
325
|
+
? '未就绪(本机 IPv6 探测失败,直连状态未知)'
|
|
326
|
+
: s.v6 === ''
|
|
327
|
+
? '未就绪(当前网络没有可用 IPv6)'
|
|
328
|
+
: '未就绪(core 尚未连过来;可运行 ssh-net start peer 立即重试)';
|
|
329
|
+
if (sw.peer) {
|
|
330
|
+
lines.push(lineOf('peer', s.peerReady,
|
|
331
|
+
`IPv6 直连已就绪(到 ${s.coreAlias},一条 ssh)`,
|
|
332
|
+
peerUnready));
|
|
333
|
+
} else if (s.peerReady) {
|
|
334
|
+
lines.push(' peer:✓ 现有 IPv6 直连仍可用(不主动恢复)');
|
|
335
|
+
} else {
|
|
336
|
+
lines.push(' peer:关(当前无可用直连;不主动恢复)');
|
|
337
|
+
}
|
|
338
|
+
if (s.linkInstalled && s.forwardViews.length > 0) {
|
|
339
|
+
const bad = s.forwardViews.filter(([, ok]) => !ok).map(([l]) => l);
|
|
340
|
+
lines.push(lineOf('link', bad.length === 0,
|
|
341
|
+
`本地端口在听(${s.forwardViews.map(([l]) => l).join(' · ')})`,
|
|
342
|
+
`本地端口未在听(${bad.join('、')};检查本地转发任务与中继线路)`));
|
|
343
|
+
}
|
|
344
|
+
lines.push(s.v6 !== ''
|
|
345
|
+
? ` 对外地址:${s.v6}`
|
|
346
|
+
: !s.v6ProbeOk
|
|
347
|
+
? ' 对外地址:未知(IPv6 探测失败,稍后再试)'
|
|
348
|
+
: ' 对外地址:当前没有可用 IPv6');
|
|
329
349
|
}
|
|
330
|
-
lines.push(s.v6 !== ''
|
|
331
|
-
? ` 对外地址:${s.v6}`
|
|
332
|
-
: !s.v6ProbeOk
|
|
333
|
-
? ' 对外地址:未知(IPv6 探测失败,稍后再试)'
|
|
334
|
-
: ' 对外地址:当前没有可用 IPv6');
|
|
335
350
|
}
|
|
336
351
|
lines.push(s.linkInstalled
|
|
337
352
|
? ` 本地转发(link):已安装(后台任务${s.job.running ? '运行中' : '未运行'})`
|
|
@@ -384,8 +399,8 @@ export function renderText(s) {
|
|
|
384
399
|
}
|
|
385
400
|
|
|
386
401
|
export async function run(ctx, args = []) {
|
|
387
|
-
if (args.length)
|
|
388
|
-
throw new Error('用法:status
|
|
389
|
-
const s = await collect(ctx);
|
|
402
|
+
if (args.length > 1 || (args.length === 1 && args[0] !== '--probe'))
|
|
403
|
+
throw new Error('用法:status [--probe]');
|
|
404
|
+
const s = await collect(ctx, { probe: args[0] === '--probe' });
|
|
390
405
|
return { text: renderText(s), issues: s.issues };
|
|
391
406
|
}
|
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
// update:一键升级——npm i -g 后重跑 install 刷新运行时。
|
|
2
2
|
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
// 只做一轮、不轮询等待:操作者即发版者,知道版号直接 update <版号> 最稳,
|
|
8
|
-
// 真被「显式版号也装不上」实锤后再加等待机制。
|
|
3
|
+
// 版本选择一条直线:显式版号用于切换、回滚或同版修复;无参先 `npm view`
|
|
4
|
+
// 问最新版号,更旧则停止、相同直接返回、更新才装 exact 版本。查询失败也不
|
|
5
|
+
// 退回 @latest:标签与 view 看同一份 registry 元数据,先装再 ls 无法阻止降级。
|
|
6
|
+
// 只做一轮、不轮询等待;知道目标版号时直接 update <版号>。
|
|
9
7
|
//
|
|
10
8
|
// 刻意「换装后重新 exec 新 bin」而不是在当前进程里直接调 installRun:
|
|
11
9
|
// 当前进程的 pkg 版本常量与包内路径都属于旧包(模块已加载),npm 换装后
|
|
@@ -16,7 +14,7 @@
|
|
|
16
14
|
// 边界:node 角色需要 root(install 写 /etc/systemd/system),先检查再动
|
|
17
15
|
// npm,避免「包装上了、install 被拒」的半截态;core 侧**已注册** edge 的
|
|
18
16
|
// uplink-edge 实体随 install 刷新,job 参数仍等各 edge 下次报名对账。
|
|
19
|
-
import { readRuntimeVersion, RUNTIME_VERSION
|
|
17
|
+
import { readRuntimeVersion, RUNTIME_VERSION } from '../lib/runtime.mjs';
|
|
20
18
|
import { cmpVersion, VERSION_RE } from '../lib/version.mjs';
|
|
21
19
|
|
|
22
20
|
const NPM_TIMEOUT = 180_000; // 慢网络下全局装包的余量
|
|
@@ -63,20 +61,23 @@ export async function run(ctx, args = []) {
|
|
|
63
61
|
if (ctx.role === 'node' && ctx.uid !== 0)
|
|
64
62
|
throw new Error('node 上升级需要 root(install 要写 /etc/systemd/system),请运行 sudo -i ssh-net update');
|
|
65
63
|
|
|
66
|
-
const { runner } = ctx;
|
|
67
|
-
const residentBefore = residentRuntimeFingerprint(ctx.paths);
|
|
64
|
+
const { runner, progress } = ctx;
|
|
68
65
|
|
|
69
|
-
// 无参只装 view 确认的 exact
|
|
66
|
+
// 无参只装 view 确认的 exact 新版;查询失败/旧版都不能拿 @latest 再赌一次
|
|
70
67
|
let resolved = target;
|
|
71
68
|
if (!resolved) {
|
|
69
|
+
progress('正在查询 npm 最新版本…');
|
|
72
70
|
const latest = npmLatest(runner);
|
|
73
71
|
if (latest === null)
|
|
74
72
|
throw new Error('没能从 npm view 确认最新版,未执行安装——稍后重试;知道目标版号可直接 ssh-net update <版本号>');
|
|
75
|
-
|
|
73
|
+
const relation = cmpVersion(latest, RUNTIME_VERSION);
|
|
74
|
+
if (relation < 0)
|
|
76
75
|
throw new Error(`npm 返回的最新版 ${latest} 比当前版本 ${RUNTIME_VERSION} 更旧,已停止且未执行安装——稍后重试;知道目标版号可直接 ssh-net update <版本号>`);
|
|
76
|
+
if (relation === 0) return '已是最新版本';
|
|
77
77
|
resolved = latest;
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
+
progress(`正在安装 ssh-net@${resolved}…`);
|
|
80
81
|
const r = npmInstall(runner, resolved);
|
|
81
82
|
if (r.error && r.error.code === 'ENOENT')
|
|
82
83
|
throw new Error(NPM_NOT_FOUND);
|
|
@@ -90,10 +91,9 @@ export async function run(ctx, args = []) {
|
|
|
90
91
|
throw new Error(`npm 安装 ${resolved} 失败:${failWhy(r)}(网络或 registry 问题;已装的版本不受影响,稍后重试)`);
|
|
91
92
|
}
|
|
92
93
|
|
|
93
|
-
// 版号地址 code 0 即装上该版,无需再解析 npm 人类文案或 ls
|
|
94
|
-
// 换装后用新 bin 跑 install
|
|
95
|
-
|
|
96
|
-
// 非交互环境由 dotfiles 保证(同报名 RPC 的前提,design §7 第 5 条)
|
|
94
|
+
// 版号地址 code 0 即装上该版,无需再解析 npm 人类文案或 ls 猜版本。
|
|
95
|
+
// 换装后用新 bin 跑 install;变更摘要由 install 统一产出,update 只转述
|
|
96
|
+
progress('正在同步本机组件…');
|
|
97
97
|
const inst = runner.run('ssh-net', ['install'], { timeout: INSTALL_TIMEOUT });
|
|
98
98
|
if (inst.error && inst.error.code === 'ENOENT')
|
|
99
99
|
throw new Error('npm 换装后 PATH 上找不到 ssh-net(全局 bin 未生效)——新开一个 shell 或手动跑一次 ssh-net install;若反复出现,检查 fnm 的 global 包路径');
|
|
@@ -101,17 +101,11 @@ export async function run(ctx, args = []) {
|
|
|
101
101
|
throw new Error(`npm 包已切换,但 install 刷新没成功:${failWhy(inst)}——手动跑一次 ssh-net install 修复`);
|
|
102
102
|
|
|
103
103
|
const rel = cmpVersion(resolved, RUNTIME_VERSION);
|
|
104
|
-
const residentChanged = residentRuntimeFingerprint(ctx.paths) !== residentBefore;
|
|
105
|
-
// 提示依据 install 前后的常驻脚本,而不是包版本号或整个 bin/:同版修复也会
|
|
106
|
-
// 换脚本,按需工具与文档-only 更新不需要后台重连提示
|
|
107
|
-
const rollover = residentChanged
|
|
108
|
-
? '提示:常驻脚本已更新;已在运行的后台进程到下次重连才使用新版,服务配置变化或手动 restart 可能断开当前连接'
|
|
109
|
-
: '';
|
|
110
104
|
|
|
111
105
|
// 后置校验:真换装后新 bin 的 install 必把版本戳刷到刚装上的版本
|
|
112
106
|
// (ensureRuntime 无条件 refreshStamp);戳没跟上说明 PATH 解析到了旧
|
|
113
107
|
// ssh-net,install 跑在旧代码上、升级静默没生效(status 的 runtime-version
|
|
114
|
-
//
|
|
108
|
+
// 对账能查到,但当场报比事后对账快)。同版修复新旧版本恒等,校验无意义,跳过
|
|
115
109
|
const stamp = readRuntimeVersion(ctx.paths);
|
|
116
110
|
const stampWarning = rel !== 0 && stamp !== null && stamp !== resolved
|
|
117
111
|
? `⚠️ 后台脚本版本仍为 ${stamp}——刚才的 install 可能跑了旧 ssh-net;用 which -a ssh-net 检查 PATH,再手动运行 ssh-net install`
|
|
@@ -120,7 +114,7 @@ export async function run(ctx, args = []) {
|
|
|
120
114
|
const head = target !== null && rel < 0
|
|
121
115
|
? `已切换到指定版本 ${resolved}(原版本 ${RUNTIME_VERSION},这是降级)`
|
|
122
116
|
: rel === 0
|
|
123
|
-
?
|
|
117
|
+
? `当前已是指定版本 ${resolved}`
|
|
124
118
|
: `已升级到 ${resolved}`;
|
|
125
|
-
return [head, inst.stdout.trim(), stampWarning
|
|
119
|
+
return [head, inst.stdout.trim(), stampWarning].filter(Boolean).join('\n');
|
|
126
120
|
}
|
package/libexec/lib/manage.mjs
CHANGED
|
@@ -51,7 +51,7 @@ export function ensureLoaded(ctx, { label, plistPath }, {
|
|
|
51
51
|
* @param {object} ctx
|
|
52
52
|
* @param {{label:string, plistPath:string, content:string}} job
|
|
53
53
|
* @returns {{unchanged?:boolean, registered?:boolean, revived?:boolean,
|
|
54
|
-
* deployed?:boolean, lintError?:string}}
|
|
54
|
+
* deployed?:boolean, restarted?:boolean, lintError?:string}}
|
|
55
55
|
*/
|
|
56
56
|
export function syncPlist(ctx, { label, plistPath, content }) {
|
|
57
57
|
const { runner, uid, paths } = ctx;
|
|
@@ -94,7 +94,7 @@ export function syncPlist(ctx, { label, plistPath, content }) {
|
|
|
94
94
|
//(停机态)——真失败(override 数据库损坏)下 bootstrap 也过不去(error 5),
|
|
95
95
|
// 可见性优先于「也许 bootstrap 能侥幸成功」
|
|
96
96
|
launchd.enableBootstrap(runner, uid, label, plistPath);
|
|
97
|
-
return { deployed: true };
|
|
97
|
+
return { deployed: true, restarted: st.registered };
|
|
98
98
|
}
|
|
99
99
|
|
|
100
100
|
/**
|
|
@@ -102,9 +102,11 @@ export function syncPlist(ctx, { label, plistPath, content }) {
|
|
|
102
102
|
* @param {object} ctx
|
|
103
103
|
* @param {string} label
|
|
104
104
|
* @param {string} plistPath
|
|
105
|
+
* @returns {boolean} 是否在册任务或磁盘 plist 确有清理
|
|
105
106
|
*/
|
|
106
107
|
export function removeJob(ctx, label, plistPath) {
|
|
107
108
|
const { runner, uid } = ctx;
|
|
109
|
+
const existed = fs.existsSync(plistPath);
|
|
108
110
|
// 删除路径按查询失败中止,只有明确的未注册才允许直接删 plist
|
|
109
111
|
const st = launchd.printState(runner, uid, label, true);
|
|
110
112
|
if (st.registered) {
|
|
@@ -115,4 +117,5 @@ export function removeJob(ctx, label, plistPath) {
|
|
|
115
117
|
throw new Error(`bootout 失败(${label}):${bo.stderr.trim()}`);
|
|
116
118
|
}
|
|
117
119
|
fs.rmSync(plistPath, { force: true });
|
|
120
|
+
return st.registered || existed;
|
|
118
121
|
}
|
package/libexec/lib/probe.mjs
CHANGED
|
@@ -107,9 +107,9 @@ export async function peerProbeOk(runner, binDir, timeoutMs = 5000) {
|
|
|
107
107
|
}
|
|
108
108
|
|
|
109
109
|
/**
|
|
110
|
-
* sn-ipv6 运行结果的三态分类——「探测没跑成」与「网络没有 IPv6
|
|
111
|
-
*
|
|
112
|
-
*
|
|
110
|
+
* status 对 sn-ipv6 运行结果的三态分类——「探测没跑成」与「网络没有 IPv6」
|
|
111
|
+
* 是两回事。failed 盖启动失败、被信号杀(code null)与非零退出
|
|
112
|
+
* (sn-ipv6 契约退出码恒 0,非零即异常)。
|
|
113
113
|
* @param {{code:number|null, signal?:string|null, stdout:string,
|
|
114
114
|
* error?:NodeJS.ErrnoException|null}} r Runner 的 RunResult
|
|
115
115
|
* @returns {{state:'failed'|'none'|'ok', v6:string}}
|
package/libexec/lib/render.mjs
CHANGED
|
@@ -94,7 +94,7 @@ const ROUTES_HEADER = `# 本文件由 \`ssh-net install\` 从 ~/.ssh/ssh-net/top
|
|
|
94
94
|
/**
|
|
95
95
|
* sn-route 的冻结位置参数(design §2.3 argv 契约):
|
|
96
96
|
* <self> <core> <relay> <core 注册口> <core 用户>
|
|
97
|
-
* routes 渲染与
|
|
97
|
+
* routes 渲染与 start 喂参的唯一生产点,两边改一处即可。
|
|
98
98
|
* @param {import('./config.mjs').Topology} topo
|
|
99
99
|
* @param {string} selfAlias 本机 edge 规范别名(sn-route 报名 RPC 的自称)
|
|
100
100
|
* @returns {string[]}
|
|
@@ -162,7 +162,7 @@ export function renderRoutes(topo, selfAlias) {
|
|
|
162
162
|
// 尾部打标与选路无关。SetEnv 借 LC_* 通道、对端 sshd 默认 AcceptEnv LC_*、
|
|
163
163
|
// 不用改;变量名都是跨仓契约(接收方是 dotfiles 的 ~/.ssh/rc,改名两处同步):
|
|
164
164
|
// - LC_VSCODE_SSH:VS Code 扩展宿主进程有 VSCODE_IPC_HOOK(集成终端手动 ssh
|
|
165
|
-
// 只有 _CLI 变体、不命中),rc 据此维护 dot.via
|
|
165
|
+
// 只有 _CLI 变体、不命中),rc 据此维护 dot.via;对所有目标生效
|
|
166
166
|
// - EDGE_ENV:本机规范别名,只发给 core/node(rc 在内网直连时把它传给
|
|
167
167
|
// sn-register-lan 登记本机地址)——不外发给 GitHub 等无关主机
|
|
168
168
|
// **SetEnv 不跨块累加**:first-wins 下第一个命中块的 SetEnv 整行生效、后面的
|
|
@@ -240,20 +240,18 @@ export function probeSshOpts(connectTimeout) {
|
|
|
240
240
|
}
|
|
241
241
|
|
|
242
242
|
/**
|
|
243
|
-
* 命令行显式 -J 经 relay 拨 core
|
|
243
|
+
* status 命令行显式 -J 经 relay 拨 core 注册口的 argv(design §4.3):字面目标
|
|
244
244
|
* 127.0.0.1 不命中 Match 选路、跳板地址/用户仍读 ssh config 的 Host 块、
|
|
245
|
-
* HostKeyAlias 保证多路径同一条 known_hosts
|
|
246
|
-
*
|
|
245
|
+
* HostKeyAlias 保证多路径同一条 known_hosts。探测不接受新 host key,固定
|
|
246
|
+
* StrictHostKeyChecking=yes;超时与远端命令由调用方传。
|
|
247
247
|
* @param {import('./config.mjs').Topology} topo
|
|
248
248
|
* @param {object} [opts]
|
|
249
249
|
* @param {number} [opts.connectTimeout=5]
|
|
250
|
-
* @param {boolean} [opts.acceptHost=false] true 允许首连 accept-new;false 固定 yes
|
|
251
250
|
* @param {string} [opts.remoteCommand] 给了就追加为远端命令
|
|
252
251
|
* @returns {string[]}
|
|
253
252
|
*/
|
|
254
253
|
export function coreViaRelayArgs(topo, {
|
|
255
254
|
connectTimeout = 5,
|
|
256
|
-
acceptHost = false,
|
|
257
255
|
remoteCommand,
|
|
258
256
|
} = {}) {
|
|
259
257
|
const args = [
|
|
@@ -262,7 +260,7 @@ export function coreViaRelayArgs(topo, {
|
|
|
262
260
|
'-o', `HostKeyAlias=${topo.core.alias}`,
|
|
263
261
|
...probeSshOpts(connectTimeout),
|
|
264
262
|
];
|
|
265
|
-
args.push('-o',
|
|
263
|
+
args.push('-o', 'StrictHostKeyChecking=yes');
|
|
266
264
|
args.push(`${topo.core.user}@127.0.0.1`);
|
|
267
265
|
if (remoteCommand !== undefined) args.push(remoteCommand);
|
|
268
266
|
return args;
|
package/libexec/lib/runtime.mjs
CHANGED
|
@@ -8,47 +8,12 @@
|
|
|
8
8
|
// 版本差即报
|
|
9
9
|
import fs from 'node:fs';
|
|
10
10
|
import path from 'node:path';
|
|
11
|
-
import { createHash } from 'node:crypto';
|
|
12
11
|
import { packageScriptPath } from './paths.mjs';
|
|
13
|
-
import { RESIDENT_SCRIPTS } from './manifest.mjs';
|
|
14
12
|
import { readFileOrNull, readTrimmedOrNull, writeFileAtomic } from './fsutil.mjs';
|
|
15
13
|
import pkg from '../../package.json' with { type: 'json' };
|
|
16
14
|
|
|
17
15
|
export const RUNTIME_VERSION = pkg.version;
|
|
18
16
|
|
|
19
|
-
/**
|
|
20
|
-
* 常驻服务脚本及其服务软链的实际内容指纹。update 用它比较 install 前后是否
|
|
21
|
-
* 换了后台进程需要重连的脚本;sn-* 按需工具更新不参与 rollover 判断。
|
|
22
|
-
* @param {{binDir:string}} paths
|
|
23
|
-
* @returns {string}
|
|
24
|
-
*/
|
|
25
|
-
export function residentRuntimeFingerprint(paths) {
|
|
26
|
-
const hash = createHash('sha256');
|
|
27
|
-
let entries = [];
|
|
28
|
-
try { entries = fs.readdirSync(paths.binDir, { withFileTypes: true }); } catch (err) {
|
|
29
|
-
if (err.code === 'ENOENT') return hash.digest('hex');
|
|
30
|
-
throw err;
|
|
31
|
-
}
|
|
32
|
-
for (const entry of entries.sort((a, b) => a.name.localeCompare(b.name))) {
|
|
33
|
-
const file = path.join(paths.binDir, entry.name);
|
|
34
|
-
const st = fs.lstatSync(file);
|
|
35
|
-
const target = st.isSymbolicLink() ? fs.readlinkSync(file) : entry.name;
|
|
36
|
-
if (!RESIDENT_SCRIPTS.includes(target)) continue;
|
|
37
|
-
hash.update(`${entry.name}\0${st.mode & 0o777}\0`);
|
|
38
|
-
if (st.isSymbolicLink()) {
|
|
39
|
-
hash.update(`link\0${target}\0`);
|
|
40
|
-
} else if (st.isFile()) {
|
|
41
|
-
hash.update('file\0');
|
|
42
|
-
hash.update(fs.readFileSync(file));
|
|
43
|
-
hash.update('\0');
|
|
44
|
-
} else {
|
|
45
|
-
// bin/ 契约只有实体和软链;异常类型也进入指纹,不能被当成无变化
|
|
46
|
-
hash.update(`other\0${st.mode}\0`);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
return hash.digest('hex');
|
|
50
|
-
}
|
|
51
|
-
|
|
52
17
|
/**
|
|
53
18
|
* 拷贝单脚本到 bin/(实体)。
|
|
54
19
|
* @param {string} dstPath
|
package/libexec/lib/systemd.mjs
CHANGED
|
@@ -35,8 +35,8 @@ export function unitNamesOnDisk(unitDir, prefix) {
|
|
|
35
35
|
|
|
36
36
|
/** 没装/已卸的 unit 放行(stop 的 not-loaded 与 disable 的 does not exist
|
|
37
37
|
* 两种真机文案都认),其余失败抛出——stop/uninstall 对不存在的 unit 幂等,
|
|
38
|
-
* 但真失败不能谎报成功。start
|
|
39
|
-
* 的真错误,与 mac 侧 startCore 查 plist
|
|
38
|
+
* 但真失败不能谎报成功。start 不经这里:unit 缺失是「先 install」
|
|
39
|
+
* 的真错误,与 mac 侧 startCore 查 plist 同口径 */
|
|
40
40
|
export function disableIfInstalled(runner, unit) {
|
|
41
41
|
const r = disableUnit(runner, unit);
|
|
42
42
|
if (r.code !== 0 && !/(not loaded|does not exist)/.test(r.stderr))
|
|
@@ -111,7 +111,7 @@ export function stopIfLoaded(runner, unit) {
|
|
|
111
111
|
* 旧文件与 manager,首装撤掉 enable/进程,更新用旧 unit 恢复启动。
|
|
112
112
|
* @param {import('./exec.mjs').Runner} runner
|
|
113
113
|
* @param {{unitPath:string, unitName:string, content:string}} job
|
|
114
|
-
* @returns {
|
|
114
|
+
* @returns {{unchanged?:boolean, deployed?:boolean, restarted?:boolean}}
|
|
115
115
|
*/
|
|
116
116
|
export function syncUnit(runner, { unitPath, unitName, content }) {
|
|
117
117
|
fs.mkdirSync(path.dirname(unitPath), { recursive: true });
|
|
@@ -119,7 +119,7 @@ export function syncUnit(runner, { unitPath, unitName, content }) {
|
|
|
119
119
|
const previous = readFileOrNull(unitPath);
|
|
120
120
|
if (previous === content) {
|
|
121
121
|
enableStart(runner, unitName);
|
|
122
|
-
return
|
|
122
|
+
return { unchanged: true };
|
|
123
123
|
}
|
|
124
124
|
|
|
125
125
|
const verify = verifyUnitContent(runner, unitName, content);
|
|
@@ -153,5 +153,5 @@ export function syncUnit(runner, { unitPath, unitName, content }) {
|
|
|
153
153
|
}
|
|
154
154
|
throw err;
|
|
155
155
|
}
|
|
156
|
-
return
|
|
156
|
+
return { deployed: true, restarted: previous !== null };
|
|
157
157
|
}
|
package/libexec/sn-register-lan
CHANGED
|
@@ -3,14 +3,14 @@
|
|
|
3
3
|
# 调起——来源是 10/8 内网直连时,把来源地址登记为该 edge 的入口地址
|
|
4
4
|
# (state/addr/<别名>,一行 HostName;conf.d/edge-<别名> 门控 Include 它,之后
|
|
5
5
|
# 本机 ssh <别名> 走内网)。公网侧对应物是控制面 _register(中继报名写 v6):
|
|
6
|
-
# 两个写者谁后写听谁的,文件 mtime 即「最后一次见到」,入口按
|
|
6
|
+
# 两个写者谁后写听谁的,文件 mtime 即「最后一次见到」,入口按 mtime 判有效期。
|
|
7
7
|
#
|
|
8
8
|
# 登记判定(任一不满足即静默退出——rc 是登录关键路径,本脚本零输出、恒 exit 0):
|
|
9
9
|
# 来源非 10.*(公网/环回登录不登记)/ 别名为空或含 [A-Za-z0-9._-] 外字符
|
|
10
10
|
# (防路径穿越,先于任何路径拼接)/ conf.d/edge-<别名> 不存在(install 按
|
|
11
11
|
# topology 渲染的入口片段即白名单,未在册别名直接忽略,脚本不解析 topology)
|
|
12
|
-
# 全过则 tmp+mv
|
|
13
|
-
#
|
|
12
|
+
# 全过则 tmp+mv 原子重写整个文件:每次无条件重写都会续期,「内容没变就跳过」
|
|
13
|
+
# 会把在线 edge 误判过期。tmp 与目标同目录(同文件系统
|
|
14
14
|
# rename,读者要么见旧文件要么见新文件)。
|
|
15
15
|
#
|
|
16
16
|
# argv:<别名> <来源地址>(冻结;调用方只有 dotfiles 的 rc,次参即其现成的
|