ssh-net 26.11.2 → 26.11.66
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 +30 -3
- package/README.md +25 -19
- package/libexec/cli.mjs +23 -23
- package/libexec/commands/install.mjs +112 -182
- package/libexec/commands/register.mjs +1 -2
- package/libexec/commands/start.mjs +25 -18
- package/libexec/commands/status.mjs +59 -39
- package/libexec/commands/stop.mjs +6 -10
- package/libexec/commands/uninstall.mjs +11 -26
- package/libexec/commands/update.mjs +49 -21
- package/libexec/lib/conf.mjs +14 -3
- package/libexec/lib/config.mjs +3 -13
- package/libexec/lib/constants.mjs +3 -0
- package/libexec/lib/exec.mjs +2 -0
- package/libexec/lib/fsutil.mjs +33 -8
- package/libexec/lib/identity.mjs +16 -0
- package/libexec/lib/jobctl.mjs +1 -1
- package/libexec/lib/jobs.mjs +17 -17
- package/libexec/lib/launchd.mjs +17 -49
- package/libexec/lib/manage.mjs +117 -76
- package/libexec/lib/manifest.mjs +14 -57
- package/libexec/lib/paths.mjs +2 -4
- package/libexec/lib/probe.mjs +33 -46
- package/libexec/lib/runtime.mjs +29 -37
- package/libexec/lib/sshconfig.mjs +98 -17
- package/libexec/lib/systemd.mjs +214 -55
- package/libexec/sn-ipv6 +2 -2
- package/libexec/sn-route +110 -57
- package/libexec/ssh-net-link-core +2 -3
- package/package.json +1 -1
|
@@ -8,7 +8,7 @@ import { auditScript, auditLink } from '../lib/runtime.mjs';
|
|
|
8
8
|
import { packageScriptPath } from '../lib/paths.mjs';
|
|
9
9
|
import * as launchd from '../lib/launchd.mjs';
|
|
10
10
|
import * as systemd from '../lib/systemd.mjs';
|
|
11
|
-
import { peerProbeOk, tcpOpen, classifyV6Probe } from '../lib/probe.mjs';
|
|
11
|
+
import { peerProbeOk, tcpOpen, classifyV6Probe, forwardListenerPids, hasRouteTimeout } from '../lib/probe.mjs';
|
|
12
12
|
import { readFileOrNull } from '../lib/fsutil.mjs';
|
|
13
13
|
import { LAN_ADDR_RE } from '../lib/constants.mjs';
|
|
14
14
|
import {
|
|
@@ -17,7 +17,7 @@ import {
|
|
|
17
17
|
} from '../lib/manifest.mjs';
|
|
18
18
|
import { edgeLinkJob, coreRelayJob, nodeRelayJob } from '../lib/jobs.mjs';
|
|
19
19
|
import {
|
|
20
|
-
edgeLabel, renderEdgeHost, renderRoutes, coreViaRelayArgs, probeSshOpts,
|
|
20
|
+
edgeLabel, linkLabel, relayLabel, relayLinkName, renderEdgeHost, renderRoutes, coreViaRelayArgs, probeSshOpts,
|
|
21
21
|
} from '../lib/render.mjs';
|
|
22
22
|
|
|
23
23
|
/**
|
|
@@ -26,9 +26,9 @@ import {
|
|
|
26
26
|
*/
|
|
27
27
|
function runtimeIssues(ctx, { entities, links }) {
|
|
28
28
|
const issues = [];
|
|
29
|
-
const entityAudits = entities.map((
|
|
29
|
+
const entityAudits = entities.map((name) => ({
|
|
30
30
|
name,
|
|
31
|
-
audit: auditScript(ctx.paths.binScript(name), packageScriptPath(
|
|
31
|
+
audit: auditScript(ctx.paths.binScript(name), packageScriptPath(name)),
|
|
32
32
|
}));
|
|
33
33
|
const allMissing = entityAudits.length > 0
|
|
34
34
|
&& entityAudits.every((a) => a.audit === 'missing');
|
|
@@ -128,37 +128,51 @@ async function relayPathProbe(ctx) {
|
|
|
128
128
|
return { ready: r.code === 0, detail: r.stderr.trim() };
|
|
129
129
|
}
|
|
130
130
|
|
|
131
|
+
/** 在本地端口探测结束时确认进程归属,远端中继的响应时间不参与这个判断 */
|
|
132
|
+
async function probeForwardPorts(ctx, forwards, label, initialJob) {
|
|
133
|
+
const openTcp = ctx.openTcp ?? tcpOpen;
|
|
134
|
+
async function listeners({ listenPort }) {
|
|
135
|
+
if (!initialJob || !await openTcp('127.0.0.1', listenPort)) return [];
|
|
136
|
+
return forwardListenerPids(ctx.runner, listenPort);
|
|
137
|
+
}
|
|
138
|
+
const owners = await Promise.all(forwards.map(listeners));
|
|
139
|
+
const firstPid = initialJob?.pid;
|
|
140
|
+
const job = initialJob && owners.some(pids => !pids.includes(firstPid))
|
|
141
|
+
? launchd.inspect(ctx.runner, ctx.uid, label) : initialJob;
|
|
142
|
+
const forwardViews = await Promise.all(forwards.map(async (f, i) => {
|
|
143
|
+
let ok = owners[i].some(pid => pid === firstPid || pid === job?.pid);
|
|
144
|
+
// 探测可能恰好落在两任进程之间。只为换了进程又没认领的端口再查一次
|
|
145
|
+
if (!ok && job?.pid && job.pid !== firstPid) ok = (await listeners(f)).includes(job.pid);
|
|
146
|
+
return [forwardLabel(f), ok];
|
|
147
|
+
}));
|
|
148
|
+
return { job, forwardViews };
|
|
149
|
+
}
|
|
150
|
+
|
|
131
151
|
async function collectEdge(ctx, probe) {
|
|
132
152
|
const { topo, paths, state } = ctx;
|
|
133
|
-
const
|
|
134
|
-
const
|
|
135
|
-
const
|
|
136
|
-
const
|
|
137
|
-
if (
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
153
|
+
const { issues, entityAudits } = runtimeIssues(ctx, edgeRuntimePlan(topo));
|
|
154
|
+
const switches = state.readSwitches();
|
|
155
|
+
const link = edgeLinkJob(ctx);
|
|
156
|
+
const onDisk = launchd.labelsOnDisk(paths.launchAgentsDir, linkLabel(''));
|
|
157
|
+
if (onDisk.some((label) => label !== link?.label))
|
|
158
|
+
issues.push('有旧的本地转发任务——跑一次 ssh-net install 清理');
|
|
159
|
+
let initialJob = null;
|
|
160
|
+
if (link && (entityAudits.some((a) => a.audit !== 'missing') || onDisk.includes(link.label))) {
|
|
161
|
+
const issue = configFileIssue(link.plistPath, link.content, '本地转发配置', '需要更新');
|
|
141
162
|
if (issue) issues.push(issue);
|
|
163
|
+
initialJob = launchd.inspect(ctx.runner, ctx.uid, link.label);
|
|
142
164
|
}
|
|
143
165
|
|
|
144
166
|
// routes 被手删/过期会让 ssh 静默退回 lan 直连,是故障。sn-route 没装时
|
|
145
167
|
// 上面已报「尚未安装」,不重复
|
|
146
168
|
if (entityAudits.some((a) => a.name === SN_ROUTE && a.audit === 'ok')) {
|
|
169
|
+
if (!hasRouteTimeout(ctx.env)) issues.push('缺少可执行的 timeout,无法发起回拨——运行 brew install coreutils');
|
|
147
170
|
const issue = configFileIssue(
|
|
148
171
|
paths.routesFile, renderRoutes(topo, ctx.self.alias), 'ssh 自动选路配置',
|
|
149
172
|
);
|
|
150
173
|
if (issue) issues.push(issue);
|
|
151
174
|
}
|
|
152
175
|
|
|
153
|
-
// link 未装时 job 无消费者(渲染走「未安装」分支)——默认零常驻形态不为它
|
|
154
|
-
// 白跑一次 launchctl print
|
|
155
|
-
const job = linkInstalled
|
|
156
|
-
? launchd.printState(ctx.runner, ctx.uid, linkDeployment.label, true)
|
|
157
|
-
: null;
|
|
158
|
-
const switches = state.readSwitches();
|
|
159
|
-
if (linkInstalled && switches.relay && !job.running)
|
|
160
|
-
issues.push('本地转发任务未在运行——先 stop 再按原开关 start,或跑 ssh-net install 修复');
|
|
161
|
-
|
|
162
176
|
// v6 只是诊断线索不是判决,与就绪实测并发。探测没跑成时不能谎称「没有
|
|
163
177
|
// IPv6」——那个结论只在 sn-ipv6 正常跑完且无输出时才成立
|
|
164
178
|
const needV6 = probe && switches.relay
|
|
@@ -170,14 +184,21 @@ async function collectEdge(ctx, probe) {
|
|
|
170
184
|
// 就绪实测并发。relay 关闭时不探测;peer 软关闭只停止主动恢复,已有直连
|
|
171
185
|
// 仍可能被新 ssh 使用,所以只要 relay 开着就继续做 banner 探测
|
|
172
186
|
const relayProbe = ctx.relayOk ?? relayPathProbe; // 测试可注入同形结果
|
|
173
|
-
const
|
|
174
|
-
const
|
|
175
|
-
const [relayResult, peerReady, portsOk, v6Probe] = await Promise.all([
|
|
187
|
+
const forwards = probe && switches.relay ? topo.forwards : [];
|
|
188
|
+
const [relayResult, peerReady, { job, forwardViews }, v6Probe] = await Promise.all([
|
|
176
189
|
probe && switches.relay ? relayProbe(ctx) : null,
|
|
177
190
|
probe && switches.relay ? peerProbeOk(ctx.runner, paths.binDir) : null,
|
|
178
|
-
|
|
191
|
+
probeForwardPorts(ctx, forwards, link?.label, initialJob),
|
|
179
192
|
v6P,
|
|
180
193
|
]);
|
|
194
|
+
if (job) {
|
|
195
|
+
if (!job.registered || job.disabled)
|
|
196
|
+
issues.push(`本地转发任务${job.registered ? '已禁用' : '未注册'}——跑一次 ssh-net install 修复`);
|
|
197
|
+
else if (switches.relay && !job.running)
|
|
198
|
+
issues.push('本地转发任务未在运行——先 stop 再按原开关 start,或跑 ssh-net install 修复');
|
|
199
|
+
if (!switches.relay && job.running)
|
|
200
|
+
issues.push('relay 已关但本地转发仍在运行——再跑一次 ssh-net stop');
|
|
201
|
+
}
|
|
181
202
|
const relayReady = relayResult?.ready ?? null;
|
|
182
203
|
const relayDetail = relayResult?.detail ?? '';
|
|
183
204
|
|
|
@@ -196,8 +217,7 @@ async function collectEdge(ctx, probe) {
|
|
|
196
217
|
|
|
197
218
|
return {
|
|
198
219
|
role: 'edge', alias: ctx.self.alias, switches, probed: probe, relayReady, peerReady,
|
|
199
|
-
|
|
200
|
-
forwardViews: forwards.map((f, i) => [forwardLabel(f), portsOk[i]]),
|
|
220
|
+
job, forwardViews,
|
|
201
221
|
coreAlias: topo.core.alias, relayAlias: topo.relay.alias,
|
|
202
222
|
v6, v6ProbeOk, issues,
|
|
203
223
|
};
|
|
@@ -210,15 +230,13 @@ async function collectCore(ctx, probe) {
|
|
|
210
230
|
// disabled override 是整域同一份字典:relay job 与各 edge job 共用一次拉取
|
|
211
231
|
const dmap = launchd.disabledMap(ctx.runner, ctx.uid);
|
|
212
232
|
const relayDeployment = coreRelayJob(ctx);
|
|
233
|
+
if (launchd.labelsOnDisk(paths.launchAgentsDir, relayLabel('')).some((name) => name !== relayDeployment.label))
|
|
234
|
+
issues.push('有旧的中继任务——跑一次 ssh-net install 清理');
|
|
213
235
|
const configIssue = configFileIssue(
|
|
214
236
|
relayDeployment.plistPath, relayDeployment.content, '中继服务配置', '需要更新',
|
|
215
237
|
);
|
|
216
238
|
if (configIssue) issues.push(configIssue);
|
|
217
|
-
const relayJob = launchd.
|
|
218
|
-
// 实测时先发起来,边等边做注册态体检;默认快查不发起 ssh
|
|
219
|
-
const relayPortP = probe
|
|
220
|
-
? relayPortListening(ctx, topo.core.relayPort)
|
|
221
|
-
: Promise.resolve(null);
|
|
239
|
+
const relayJob = launchd.inspect(ctx.runner, ctx.uid, relayDeployment.label, dmap);
|
|
222
240
|
|
|
223
241
|
// 回拨实体全 core 一份:循环外体检一次,各已注册 edge 只检各自的软链
|
|
224
242
|
const callbackEntityAudit = topo.edges.length > 0
|
|
@@ -229,7 +247,7 @@ async function collectCore(ctx, probe) {
|
|
|
229
247
|
const edges = [];
|
|
230
248
|
for (const entry of entries) {
|
|
231
249
|
const { alias } = entry;
|
|
232
|
-
const st = launchd.
|
|
250
|
+
const st = launchd.inspect(ctx.runner, ctx.uid, edgeLabel(alias), dmap);
|
|
233
251
|
// 已连接过的 edge 体检回拨脚本/软链:漏刷会拿旧脚本拨号
|
|
234
252
|
if (st.registered) {
|
|
235
253
|
const [linkName, entityName] = edgeCallbacksPlan([alias]).links[0];
|
|
@@ -250,7 +268,8 @@ async function collectCore(ctx, probe) {
|
|
|
250
268
|
});
|
|
251
269
|
}
|
|
252
270
|
|
|
253
|
-
|
|
271
|
+
// 本地任务查询出错时直接返回错误,不留下仍在等待的 SSH 探测
|
|
272
|
+
const relayPort = probe ? await relayPortListening(ctx, topo.core.relayPort) : null;
|
|
254
273
|
if (!relayJob.running) issues.push('relay 未在运行——ssh-net start 可拉起');
|
|
255
274
|
if (relayJob.registered && relayJob.disabled)
|
|
256
275
|
issues.push('relay 已禁用,后续加载可能被拦截——ssh-net start 可重新启用');
|
|
@@ -269,10 +288,13 @@ async function collectNode(ctx, probe) {
|
|
|
269
288
|
const entries = edgeEntries(ctx, issues);
|
|
270
289
|
|
|
271
290
|
const deployment = nodeRelayJob(ctx);
|
|
291
|
+
if (systemd.unitNamesOnDisk(ctx.paths.systemdUnitDir, relayLinkName('')).some((name) => name !== deployment.unitName))
|
|
292
|
+
issues.push('有旧的中继任务——跑一次 ssh-net install 清理');
|
|
272
293
|
const configIssue = configFileIssue(
|
|
273
294
|
deployment.unitPath, deployment.content, '中继服务配置', '需要更新',
|
|
274
295
|
);
|
|
275
296
|
if (configIssue) issues.push(configIssue);
|
|
297
|
+
if (systemd.hasPendingUpdate(deployment.unitPath)) issues.push('中继服务更新尚未完成——跑一次 ssh-net install 完成更新');
|
|
276
298
|
const unit = deployment.unitName;
|
|
277
299
|
const active = systemd.isActive(ctx.runner, unit);
|
|
278
300
|
const relayPort = probe ? await relayPortListening(ctx, ctx.self.relayPort) : null;
|
|
@@ -339,11 +361,11 @@ function renderEdgeLines(s, lines) {
|
|
|
339
361
|
} else {
|
|
340
362
|
lines.push(' peer:关(当前无可用直连;不主动恢复)');
|
|
341
363
|
}
|
|
342
|
-
if (s.
|
|
364
|
+
if (s.forwardViews.length > 0) {
|
|
343
365
|
const bad = s.forwardViews.filter(([, ok]) => !ok).map(([l]) => l);
|
|
344
366
|
lines.push(lineOf('link', bad.length === 0,
|
|
345
367
|
`本地端口在听(${s.forwardViews.map(([l]) => l).join(' · ')})`,
|
|
346
|
-
|
|
368
|
+
`转发任务未监听这些端口(${bad.join('、')};检查本地转发任务与中继线路)`));
|
|
347
369
|
}
|
|
348
370
|
lines.push(s.v6 !== ''
|
|
349
371
|
? ` 对外地址:${s.v6}`
|
|
@@ -352,9 +374,7 @@ function renderEdgeLines(s, lines) {
|
|
|
352
374
|
: ' 对外地址:当前没有可用 IPv6');
|
|
353
375
|
}
|
|
354
376
|
}
|
|
355
|
-
lines.push(s.
|
|
356
|
-
? ` 本地转发(link):已安装(后台任务${s.job.running ? '运行中' : '未运行'})`
|
|
357
|
-
: ' 本地转发(link):未安装(需要 SOCKS/VNC 端口:ssh-net install --with-link)');
|
|
377
|
+
if (s.job) lines.push(` 本地转发(link):后台任务${s.job.running ? '运行中' : '未运行'}`);
|
|
358
378
|
}
|
|
359
379
|
|
|
360
380
|
function edgeTaskState(e) {
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
import * as launchd from '../lib/launchd.mjs';
|
|
5
5
|
import * as systemd from '../lib/systemd.mjs';
|
|
6
6
|
import { reapEdgeLink } from '../lib/jobctl.mjs';
|
|
7
|
+
import { requireNodeHome } from '../lib/identity.mjs';
|
|
7
8
|
import { SWITCH_NAMES } from '../lib/state.mjs';
|
|
8
9
|
import { linkLabel, relayLabel, relayLinkName } from '../lib/render.mjs';
|
|
9
10
|
|
|
@@ -11,8 +12,6 @@ function parseTarget(args) {
|
|
|
11
12
|
if (args.length === 0 || args.length === 1 && ['-a', '--all'].includes(args[0]))
|
|
12
13
|
return 'relay';
|
|
13
14
|
if (args.length === 1 && SWITCH_NAMES.includes(args[0])) return args[0];
|
|
14
|
-
if (args.length === 1 && args[0] === 'link')
|
|
15
|
-
throw new Error('link 不是运行时开关:移除本地转发组件跑 ssh-net install --without-link(卸载会一并清掉)');
|
|
16
15
|
throw new Error('用法:stop [-a|--all|relay|peer](不带参数或 -a=全部关闭)');
|
|
17
16
|
}
|
|
18
17
|
|
|
@@ -25,11 +24,6 @@ function stopRelayJob(ctx, label) {
|
|
|
25
24
|
throw new Error(`bootout 失败(${label} 仍在注册表):${bo.stderr.trim()}`);
|
|
26
25
|
}
|
|
27
26
|
|
|
28
|
-
function stopNodeUnit(ctx, unit) {
|
|
29
|
-
systemd.stopIfLoaded(ctx.runner, unit); // not-loaded(未装/已卸)无害,真失败抛出
|
|
30
|
-
systemd.disableIfInstalled(ctx.runner, unit); // 未装/已卸幂等,与 mac bootout 113 同口径
|
|
31
|
-
}
|
|
32
|
-
|
|
33
27
|
function reapLinkOrThrow(ctx, label) {
|
|
34
28
|
const { gone } = reapEdgeLink(ctx, label);
|
|
35
29
|
if (!gone)
|
|
@@ -39,13 +33,15 @@ function reapLinkOrThrow(ctx, label) {
|
|
|
39
33
|
/** 停止磁盘发现的全部 link/relay 任务;改名前的残留也一并覆盖。 */
|
|
40
34
|
function stopJobs(ctx) {
|
|
41
35
|
if ((ctx.platform ?? process.platform) === 'linux') {
|
|
42
|
-
const units = systemd.unitNamesOnDisk(ctx.paths.systemdUnitDir, relayLinkName(''));
|
|
36
|
+
const units = systemd.unitNamesOnDisk(ctx.paths.systemdUnitDir, relayLinkName(''), true);
|
|
43
37
|
if (units.length > 0 && ctx.uid !== 0)
|
|
44
38
|
throw new Error('停止 node 的中继服务需要 root,请运行 sudo -i ssh-net stop');
|
|
45
|
-
|
|
39
|
+
ctx.state.removeOn(['relay', 'peer']);
|
|
40
|
+
for (const unit of units) systemd.stopInstalledUnit(ctx.runner, ctx.paths.systemdUnitDir, unit);
|
|
46
41
|
return `已停止并禁用 ${units.length} 个中继服务`;
|
|
47
42
|
}
|
|
48
43
|
|
|
44
|
+
ctx.state.removeOn(['relay', 'peer']);
|
|
49
45
|
const links = launchd.labelsOnDisk(ctx.paths.launchAgentsDir, linkLabel(''));
|
|
50
46
|
const relays = launchd.labelsOnDisk(ctx.paths.launchAgentsDir, relayLabel(''));
|
|
51
47
|
for (const label of links) reapLinkOrThrow(ctx, label);
|
|
@@ -55,10 +51,10 @@ function stopJobs(ctx) {
|
|
|
55
51
|
|
|
56
52
|
export function run(ctx, args = []) {
|
|
57
53
|
const target = parseTarget(args);
|
|
54
|
+
if ((ctx.platform ?? process.platform) === 'linux' && ctx.uid === 0) requireNodeHome(ctx, 'stop');
|
|
58
55
|
if (target === 'peer') {
|
|
59
56
|
ctx.state.removeOn(['peer']);
|
|
60
57
|
return 'peer 已关闭:不再主动恢复 IPv6 直连;现有直连断开前仍可供新 ssh 使用,断开后走中继';
|
|
61
58
|
}
|
|
62
|
-
ctx.state.removeOn(['relay', 'peer']);
|
|
63
59
|
return `relay 和 peer 已一并关闭;${stopJobs(ctx)}`;
|
|
64
60
|
}
|
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
// plist/unit、运行时脚本、状态与日志;topology 改名前的残留由 glob 覆盖。
|
|
4
4
|
import fs from 'node:fs';
|
|
5
5
|
import path from 'node:path';
|
|
6
|
-
import {
|
|
6
|
+
import { removeJobs } from '../lib/manage.mjs';
|
|
7
|
+
import { requireNodeHome } from '../lib/identity.mjs';
|
|
7
8
|
import * as launchd from '../lib/launchd.mjs';
|
|
8
9
|
import * as systemd from '../lib/systemd.mjs';
|
|
9
10
|
import {
|
|
@@ -18,7 +19,7 @@ function removeArtifacts(ctx) {
|
|
|
18
19
|
rm(ctx.paths.stateDir);
|
|
19
20
|
rm(ctx.paths.confDir);
|
|
20
21
|
rm(ctx.paths.legacyRuntimeVersionFile);
|
|
21
|
-
rm(ctx.paths.
|
|
22
|
+
rm(ctx.paths.legacyLinkEnabledFile);
|
|
22
23
|
}
|
|
23
24
|
|
|
24
25
|
function removeSshNetLogs(ctx) {
|
|
@@ -28,35 +29,19 @@ function removeSshNetLogs(ctx) {
|
|
|
28
29
|
}
|
|
29
30
|
}
|
|
30
31
|
|
|
31
|
-
function removeJobs(ctx, labels) {
|
|
32
|
-
for (const label of labels) removeJob(ctx, label, ctx.paths.plistFile(label));
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function removeNodeUnits(ctx, units) {
|
|
36
|
-
for (const unit of units) {
|
|
37
|
-
// not-loaded(未装/已卸)无害,真失败必须报——否则删了 unit 文件、
|
|
38
|
-
// systemd 里留下 ghost 态的在跑进程
|
|
39
|
-
systemd.stopIfLoaded(ctx.runner, unit);
|
|
40
|
-
systemd.disableIfInstalled(ctx.runner, unit);
|
|
41
|
-
rm(path.join(ctx.paths.systemdUnitDir, unit));
|
|
42
|
-
}
|
|
43
|
-
// daemon-reload 失败不能吞:systemd 仍持有已删 unit,Restart=always 会
|
|
44
|
-
// 留着/重拉 ghost 进程
|
|
45
|
-
systemd.daemonReload(ctx.runner);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
32
|
export function run(ctx, args = []) {
|
|
49
33
|
if (args.length)
|
|
50
34
|
throw new Error('用法:uninstall 不带参数');
|
|
51
35
|
|
|
52
36
|
if ((ctx.platform ?? process.platform) === 'linux') {
|
|
53
|
-
const units = systemd.unitNamesOnDisk(ctx.paths.systemdUnitDir, relayLinkName(''));
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
37
|
+
const units = systemd.unitNamesOnDisk(ctx.paths.systemdUnitDir, relayLinkName(''), true);
|
|
38
|
+
// 非 root 且没有 unit 时只清理自己的文件。root 即使没有 unit,也不能到别人的 HOME 清理
|
|
39
|
+
if (ctx.uid === 0 || units.length) {
|
|
40
|
+
requireNodeHome(ctx, 'uninstall');
|
|
41
|
+
systemd.removeUnits(ctx.runner, ctx.paths.systemdUnitDir, units);
|
|
42
|
+
// 上次可能在删完文件后中断;root 即使遇到空目录也要补做 reload
|
|
43
|
+
systemd.daemonReload(ctx.runner);
|
|
44
|
+
}
|
|
60
45
|
removeArtifacts(ctx);
|
|
61
46
|
return '已卸载。最后一步:npm uninstall -g ssh-net';
|
|
62
47
|
}
|
|
@@ -5,18 +5,20 @@
|
|
|
5
5
|
// 退回 @latest:标签与 view 看同一份 registry 元数据,先装再 ls 无法阻止降级。
|
|
6
6
|
// 只做一轮、不轮询等待;知道目标版号时直接 update <版号>。
|
|
7
7
|
//
|
|
8
|
-
//
|
|
9
|
-
//
|
|
10
|
-
// 上的版本一致。
|
|
8
|
+
// 安装前确认 npm 会覆盖正在运行的这份包,避免切换 fnm 环境后装出第二份。
|
|
9
|
+
// 当前进程加载的是旧代码,换装后由新进程核对版本并运行 install。
|
|
11
10
|
//
|
|
12
|
-
// 边界:node
|
|
13
|
-
//
|
|
11
|
+
// 边界:node 先按 install 的要求核对 root 身份与 HOME,再动 npm,避免
|
|
12
|
+
// 「包装上了、install 被拒」的半截态;core 侧已注册 edge 的
|
|
14
13
|
// uplink-edge 实体随 install 刷新,job 参数仍等各 edge 下次报名对账。
|
|
14
|
+
import { lstatSync, readFileSync, realpathSync } from 'node:fs';
|
|
15
|
+
import path from 'node:path';
|
|
16
|
+
import { requireNodeHome } from '../lib/identity.mjs';
|
|
15
17
|
import { cmpVersion, VERSION_RE } from '../lib/version.mjs';
|
|
16
18
|
import pkg from '../../package.json' with { type: 'json' };
|
|
17
19
|
|
|
18
20
|
const NPM_TIMEOUT = 180_000; // 慢网络下全局装包的余量
|
|
19
|
-
const QUERY_TIMEOUT = 60_000; //
|
|
21
|
+
const QUERY_TIMEOUT = 60_000; // npm 只读查询
|
|
20
22
|
const INSTALL_TIMEOUT = 120_000;
|
|
21
23
|
|
|
22
24
|
const ARG_HINT = 'update 只接受一个可选的三段数字版本号';
|
|
@@ -24,7 +26,7 @@ const NPM_NOT_FOUND = 'PATH 上找不到 npm——检查 ~/.zshenv(mac)或 .
|
|
|
24
26
|
const BIN_NOT_FOUND = 'npm 换装后 PATH 上找不到 ssh-net(全局 bin 未生效)——新开一个 shell 或手动跑一次 ssh-net install;若反复出现,检查 fnm 的 global 包路径';
|
|
25
27
|
|
|
26
28
|
/** 失败详情:stderr 优先;被信号杀(code null,典型是超时)别说成 exit null */
|
|
27
|
-
const failWhy = (r) => r.stderr.trim()
|
|
29
|
+
const failWhy = (r) => r.stderr.trim() || (!r.signal && r.error?.message)
|
|
28
30
|
|| (r.code !== null ? `exit ${r.code}` : `被信号杀死(${r.signal ?? '未知'},多半是超时)`);
|
|
29
31
|
|
|
30
32
|
/** 参数:零个或一个版号;返回 null(无参)或版号字符串 */
|
|
@@ -42,23 +44,44 @@ function parseArgs(args) {
|
|
|
42
44
|
return version;
|
|
43
45
|
}
|
|
44
46
|
|
|
45
|
-
|
|
46
|
-
const
|
|
47
|
-
|
|
47
|
+
function runNpm(runner, args, timeout = QUERY_TIMEOUT) {
|
|
48
|
+
const r = runner.run('npm', args, { timeout });
|
|
49
|
+
if (r.error?.code === 'ENOENT') throw new Error(NPM_NOT_FOUND);
|
|
50
|
+
return r;
|
|
51
|
+
}
|
|
48
52
|
|
|
49
53
|
/** 问 registry 当前最新版号;查不到或输出不合法返回 null(调用方停止升级) */
|
|
50
54
|
const npmLatest = (runner) => {
|
|
51
|
-
const r = runner
|
|
52
|
-
if (r.error && r.error.code === 'ENOENT')
|
|
53
|
-
throw new Error(NPM_NOT_FOUND);
|
|
55
|
+
const r = runNpm(runner, ['view', 'ssh-net', 'version', '--prefer-online']);
|
|
54
56
|
const v = r.code === 0 ? r.stdout.trim() : '';
|
|
55
57
|
return VERSION_RE.test(v) ? v : null;
|
|
56
58
|
};
|
|
57
59
|
|
|
60
|
+
function checkNpmTarget({ runner, packageDir = new URL('../../', import.meta.url) }) {
|
|
61
|
+
const r = runNpm(runner, ['root', '-g']);
|
|
62
|
+
if (r.code !== 0)
|
|
63
|
+
throw new Error(`npm root -g 查询失败,未执行安装:${failWhy(r)}`);
|
|
64
|
+
const root = r.stdout.trim();
|
|
65
|
+
if (!path.isAbsolute(root))
|
|
66
|
+
throw new Error('npm root -g 未返回全局目录的绝对路径,未执行安装');
|
|
67
|
+
|
|
68
|
+
const target = path.join(root, pkg.name);
|
|
69
|
+
const current = realpathSync.native(packageDir);
|
|
70
|
+
const mismatch = `npm 安装目标 ${target} 与当前运行的 ssh-net 目录 ${current} 不一致,未执行安装——请切换到安装这份 ssh-net 的 Node.js 环境后重试`;
|
|
71
|
+
// macOS 与 Linux 的全局目录都是 <prefix>/lib/node_modules。固定 fnm 当前指向的真实 prefix,
|
|
72
|
+
// 检查与安装都使用它,避免两次 npm 调用之间切版本而换错包
|
|
73
|
+
if (!path.resolve(root).endsWith('/lib/node_modules') || !lstatSync(target, { throwIfNoEntry: false }))
|
|
74
|
+
throw new Error(mismatch);
|
|
75
|
+
const prefix = realpathSync.native(path.resolve(root, '../..'));
|
|
76
|
+
const pinned = path.join(prefix, 'lib', 'node_modules', pkg.name);
|
|
77
|
+
if (!lstatSync(pinned, { throwIfNoEntry: false })?.isDirectory() || realpathSync.native(pinned) !== current)
|
|
78
|
+
throw new Error(mismatch);
|
|
79
|
+
return prefix;
|
|
80
|
+
}
|
|
81
|
+
|
|
58
82
|
export async function run(ctx, args = []) {
|
|
59
83
|
const target = parseArgs(args);
|
|
60
|
-
if (ctx.role === 'node'
|
|
61
|
-
throw new Error('node 上升级需要 root(install 要写 /etc/systemd/system),请运行 sudo -i ssh-net update');
|
|
84
|
+
if (ctx.role === 'node') requireNodeHome(ctx, 'update');
|
|
62
85
|
|
|
63
86
|
const { runner, progress } = ctx;
|
|
64
87
|
|
|
@@ -76,12 +99,11 @@ export async function run(ctx, args = []) {
|
|
|
76
99
|
resolved = latest;
|
|
77
100
|
}
|
|
78
101
|
|
|
102
|
+
const prefix = checkNpmTarget(ctx);
|
|
79
103
|
progress(`正在安装 ssh-net@${resolved}…`);
|
|
80
|
-
const r =
|
|
81
|
-
if (r.error && r.error.code === 'ENOENT')
|
|
82
|
-
throw new Error(NPM_NOT_FOUND);
|
|
104
|
+
const r = runNpm(runner, ['install', '-g', `ssh-net@${resolved}`, '--prefer-online', '--prefix', prefix], NPM_TIMEOUT);
|
|
83
105
|
if (r.code !== 0)
|
|
84
|
-
throw new Error(`npm 安装 ${resolved} 失败:${failWhy(r)}
|
|
106
|
+
throw new Error(`npm 安装 ${resolved} 失败:${failWhy(r)}(请用 ssh-net --version 核对当前安装,网络或 registry 恢复后可重试同一版本)`);
|
|
85
107
|
|
|
86
108
|
// 版号地址 code 0 即装上该版;先核对 PATH 选中的 bin,再让它跑 install。
|
|
87
109
|
// 多全局安装时,npm 更新的目录不一定排在 PATH 首位;旧 bin 成功退出也不代表
|
|
@@ -95,8 +117,14 @@ export async function run(ctx, args = []) {
|
|
|
95
117
|
if (actual !== resolved)
|
|
96
118
|
throw new Error(`npm 包已切换到 ${resolved},但 PATH 上的 ssh-net 版本是 ${actual || '未知'},已停止收尾——用 which -a ssh-net 检查 PATH,再手动运行 ssh-net install`);
|
|
97
119
|
|
|
98
|
-
|
|
99
|
-
|
|
120
|
+
// PATH 上另一份同版包(例如 npm link 的源码)也能通过版号检查。收尾必须使用刚写入的 bin
|
|
121
|
+
const dir = path.join(prefix, 'lib', 'node_modules', pkg.name);
|
|
122
|
+
const installed = JSON.parse(readFileSync(path.join(dir, 'package.json'), 'utf8'));
|
|
123
|
+
if (installed.version !== resolved)
|
|
124
|
+
throw new Error(`npm 安装目录里的版本是 ${installed.version},与目标 ${resolved} 不一致,已停止收尾`);
|
|
125
|
+
const bin = typeof installed.bin === 'string' ? installed.bin : installed.bin?.[pkg.name];
|
|
126
|
+
if (!bin) throw new Error(`ssh-net@${resolved} 缺少命令入口,无法运行 install`);
|
|
127
|
+
const inst = runner.runRaw(path.resolve(dir, bin), ['install'], { timeout: INSTALL_TIMEOUT });
|
|
100
128
|
if (inst.code !== 0)
|
|
101
129
|
throw new Error(`npm 包已切换,但 install 刷新没成功:${failWhy(inst)}——手动跑一次 ssh-net install 修复`);
|
|
102
130
|
|
package/libexec/lib/conf.mjs
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
// - sync 幂等:内容、权限与文件类型都正确就不动盘(不产生无意义变更)
|
|
7
7
|
import fs from 'node:fs';
|
|
8
8
|
import path from 'node:path';
|
|
9
|
-
import { readFileOrNull, writeFileAtomic } from './fsutil.mjs';
|
|
9
|
+
import { readFileOrNull, withTempFile, writeFileAtomic } from './fsutil.mjs';
|
|
10
10
|
|
|
11
11
|
/** 确保 conf.d 存在且 0700(~/.ssh/ssh-net 已由 state 侧建为 0700)。 */
|
|
12
12
|
function ensureConfDir(confDir) {
|
|
@@ -18,9 +18,10 @@ function ensureConfDir(confDir) {
|
|
|
18
18
|
* 原子同步一个 Include 片段。
|
|
19
19
|
* @param {string} file 片段绝对路径(在 conf.d 内)
|
|
20
20
|
* @param {string} content 完整内容
|
|
21
|
+
* @param {(restore:() => void) => void} [onReplace] 关联服务部署失败时恢复原来的目录项
|
|
21
22
|
* @returns {boolean} 是否有变更(重写、权限或文件类型修复)
|
|
22
23
|
*/
|
|
23
|
-
export function syncConfFile(file, content) {
|
|
24
|
+
export function syncConfFile(file, content, onReplace) {
|
|
24
25
|
const dir = path.dirname(file);
|
|
25
26
|
ensureConfDir(dir);
|
|
26
27
|
const st = fs.lstatSync(file, { throwIfNoEntry: false }) ?? null;
|
|
@@ -36,6 +37,16 @@ export function syncConfFile(file, content) {
|
|
|
36
37
|
// 同内容软链不是受管实体,落下原子替换为普通文件。
|
|
37
38
|
|
|
38
39
|
// tmp 落 conf.d 的**上级**目录:glob conf.d/* 扫不到它
|
|
39
|
-
|
|
40
|
+
const tmpDir = path.dirname(dir);
|
|
41
|
+
const link = onReplace && st?.isSymbolicLink() ? fs.readlinkSync(file) : null;
|
|
42
|
+
writeFileAtomic(file, content, { mode: 0o600, tmpDir });
|
|
43
|
+
onReplace?.(() => {
|
|
44
|
+
if (link !== null) withTempFile(file, { tmpDir }, tmp => {
|
|
45
|
+
fs.symlinkSync(link, tmp);
|
|
46
|
+
fs.renameSync(tmp, file);
|
|
47
|
+
});
|
|
48
|
+
else if (existing !== null) syncConfFile(file, existing);
|
|
49
|
+
else fs.rmSync(file, { force: true });
|
|
50
|
+
});
|
|
40
51
|
return true;
|
|
41
52
|
}
|
package/libexec/lib/config.mjs
CHANGED
|
@@ -251,19 +251,9 @@ export function parseTopology(text) {
|
|
|
251
251
|
* @param {Map<string, {hostname?: string}>} hostTable parseStaticHosts 的产物
|
|
252
252
|
*/
|
|
253
253
|
export function validateHostContract(topo, hostTable) {
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
const host = hostTable.get(alias);
|
|
258
|
-
if (!host || !host.hostname) missing.push(alias);
|
|
259
|
-
};
|
|
260
|
-
check(topo.relay.alias);
|
|
261
|
-
check(topo.core.alias);
|
|
262
|
-
for (const n of topo.nodes) check(n.alias);
|
|
263
|
-
for (const f of topo.forwards) {
|
|
264
|
-
const a = f.targetAlias;
|
|
265
|
-
if (a && a !== 'loopback' && a !== 'loopback6') check(a);
|
|
266
|
-
}
|
|
254
|
+
const missing = [topo.relay, topo.core, ...topo.nodes]
|
|
255
|
+
.map((host) => host.alias)
|
|
256
|
+
.filter((alias) => !hostTable.get(alias)?.hostname);
|
|
267
257
|
if (missing.length > 0)
|
|
268
258
|
throw new ConfigError(
|
|
269
259
|
0,
|
|
@@ -5,6 +5,9 @@
|
|
|
5
5
|
/** core 经 -R 回拨、edge 环回上的监听端口。 */
|
|
6
6
|
export const CALLBACK_PORT = 10022;
|
|
7
7
|
|
|
8
|
+
// edge 报名优先用系统 timeout,其次用两种默认 Homebrew 安装位置,不依赖交互 shell 的 PATH
|
|
9
|
+
export const TIMEOUT_SEARCH_PATH = '/usr/bin:/opt/homebrew/opt/coreutils/libexec/gnubin:/usr/local/opt/coreutils/libexec/gnubin';
|
|
10
|
+
|
|
8
11
|
// 回连桌面通知后端(sn-route 的 peer_ready 优先 terminal-notifier、缺它走
|
|
9
12
|
// osascript)。install 的可选依赖提示与 sn-route 的实际探测共用这组默认
|
|
10
13
|
// 路径;shell 读不到本文件,一致性靠 constants.test 锚
|
package/libexec/lib/exec.mjs
CHANGED
package/libexec/lib/fsutil.mjs
CHANGED
|
@@ -3,15 +3,21 @@
|
|
|
3
3
|
import fs from 'node:fs';
|
|
4
4
|
import path from 'node:path';
|
|
5
5
|
|
|
6
|
+
export function isExecutableFile(file) {
|
|
7
|
+
try {
|
|
8
|
+
fs.accessSync(file, fs.constants.X_OK);
|
|
9
|
+
return fs.statSync(file).isFile();
|
|
10
|
+
} catch { return false; }
|
|
11
|
+
}
|
|
12
|
+
|
|
6
13
|
/**
|
|
7
|
-
*
|
|
14
|
+
* 读取 UTF-8 文本,文件不存在时返回 null,其他错误照常抛出
|
|
8
15
|
* @param {string} file
|
|
9
|
-
* @
|
|
10
|
-
* @returns {string|Buffer|null}
|
|
16
|
+
* @returns {string|null}
|
|
11
17
|
*/
|
|
12
|
-
export function readFileOrNull(file
|
|
18
|
+
export function readFileOrNull(file) {
|
|
13
19
|
try {
|
|
14
|
-
return fs.readFileSync(file,
|
|
20
|
+
return fs.readFileSync(file, 'utf8');
|
|
15
21
|
} catch (err) {
|
|
16
22
|
if (err && err.code === 'ENOENT') return null;
|
|
17
23
|
throw err;
|
|
@@ -33,6 +39,26 @@ export function readDirOrEmpty(dir) {
|
|
|
33
39
|
}
|
|
34
40
|
}
|
|
35
41
|
|
|
42
|
+
/** 清理改名前的目录项。大小写不敏感的卷上,旧拼写有时仍是新服务使用的同一项 */
|
|
43
|
+
export function removeObsoleteNames(dir, obsolete, keep) {
|
|
44
|
+
if (!obsolete.length) return false;
|
|
45
|
+
const names = new Set(readDirOrEmpty(dir));
|
|
46
|
+
let changed = false;
|
|
47
|
+
for (const old of obsolete) {
|
|
48
|
+
if (!names.has(old) || keep.includes(old)) continue;
|
|
49
|
+
const file = path.join(dir, old);
|
|
50
|
+
const spelling = keep.find((name) => name.toLowerCase() === old.toLowerCase() && !names.has(name));
|
|
51
|
+
if (spelling && fs.lstatSync(path.join(dir, spelling), { throwIfNoEntry: false })) {
|
|
52
|
+
fs.renameSync(file, path.join(dir, spelling));
|
|
53
|
+
names.add(spelling);
|
|
54
|
+
} else {
|
|
55
|
+
fs.rmSync(file, { force: true });
|
|
56
|
+
}
|
|
57
|
+
changed = true;
|
|
58
|
+
}
|
|
59
|
+
return changed;
|
|
60
|
+
}
|
|
61
|
+
|
|
36
62
|
/**
|
|
37
63
|
* 读文本并 trim:不存在或只剩空白返回 null(「文件里得有值才算数」,
|
|
38
64
|
* state 的 edges v6 与 runtime 的版本戳共用)。
|
|
@@ -69,8 +95,7 @@ export function withTempFile(file, { tmpDir = path.dirname(file) } = {}, fn) {
|
|
|
69
95
|
}
|
|
70
96
|
|
|
71
97
|
/**
|
|
72
|
-
*
|
|
73
|
-
* 吃位都修正到 mode)。
|
|
98
|
+
* 原子落盘:先在临时文件上修正 umask 影响,内容和权限都准备好再替换目标
|
|
74
99
|
* @param {string} file
|
|
75
100
|
* @param {string|Buffer} content
|
|
76
101
|
* @param {{mode?: number, tmpDir?: string}} [opts] tmpDir 给 Include 片段用:
|
|
@@ -79,7 +104,7 @@ export function withTempFile(file, { tmpDir = path.dirname(file) } = {}, fn) {
|
|
|
79
104
|
export function writeFileAtomic(file, content, { mode = 0o644, tmpDir = path.dirname(file) } = {}) {
|
|
80
105
|
return withTempFile(file, { tmpDir }, (tmp) => {
|
|
81
106
|
fs.writeFileSync(tmp, content, { mode });
|
|
107
|
+
fs.chmodSync(tmp, mode);
|
|
82
108
|
fs.renameSync(tmp, file);
|
|
83
|
-
fs.chmodSync(file, mode);
|
|
84
109
|
});
|
|
85
110
|
}
|
package/libexec/lib/identity.mjs
CHANGED
|
@@ -31,3 +31,19 @@ export function localHostname(hostname = os.hostname()) {
|
|
|
31
31
|
export function matchIdentity(topology, hostname = localHostname()) {
|
|
32
32
|
return topology.byHostname.get(hostname) ?? null;
|
|
33
33
|
}
|
|
34
|
+
|
|
35
|
+
// os.userInfo() 从系统账户记录取家目录,不受 sudo 保留的 HOME 影响
|
|
36
|
+
export const nodeAccountHome = (ctx) => ctx.nodeHome ?? os.userInfo().homedir;
|
|
37
|
+
|
|
38
|
+
/** 写入 node 的安装或服务前核对账户,不依赖 topology */
|
|
39
|
+
export function requireNodeHome(ctx, command) {
|
|
40
|
+
const retry = `sudo -i ssh-net ${command}`;
|
|
41
|
+
if (ctx.uid !== 0)
|
|
42
|
+
throw new Error(`操作 node 的系统服务需要 root,请运行 ${retry}`);
|
|
43
|
+
const home = nodeAccountHome(ctx);
|
|
44
|
+
if (!ctx.env.HOME || ctx.env.HOME.replace(/\/+$/, '') !== home.replace(/\/+$/, ''))
|
|
45
|
+
throw new Error(`HOME 与 root 账户的家目录不一致,请运行 ${retry}`);
|
|
46
|
+
if (['install', 'update'].includes(command) && /\s/.test(home))
|
|
47
|
+
throw new Error(`node 的 HOME(${home})含空白字符,当前 systemd ExecStart 无法转义,拒绝${command === 'install' ? '安装' : '升级'}`);
|
|
48
|
+
return home;
|
|
49
|
+
}
|
package/libexec/lib/jobctl.mjs
CHANGED
|
@@ -14,7 +14,7 @@ function sleepMs(ms) {
|
|
|
14
14
|
|
|
15
15
|
function isGone(ctx, label) {
|
|
16
16
|
// 停止路径不能把 launchd 查询失败当成已退出,否则会继续谎报关闭成功
|
|
17
|
-
const st = launchd.printState(ctx.runner, ctx.uid, label
|
|
17
|
+
const st = launchd.printState(ctx.runner, ctx.uid, label);
|
|
18
18
|
return !st.registered || (!st.running && st.pid === null);
|
|
19
19
|
}
|
|
20
20
|
|