ssh-net 26.11.2 → 26.11.3
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 +14 -0
- package/README.md +13 -14
- package/libexec/cli.mjs +20 -22
- package/libexec/commands/install.mjs +58 -143
- package/libexec/commands/register.mjs +1 -2
- package/libexec/commands/start.mjs +18 -17
- package/libexec/commands/status.mjs +30 -29
- package/libexec/commands/stop.mjs +0 -2
- package/libexec/commands/uninstall.mjs +2 -6
- package/libexec/lib/fsutil.mjs +4 -5
- package/libexec/lib/jobctl.mjs +1 -1
- package/libexec/lib/jobs.mjs +14 -10
- package/libexec/lib/launchd.mjs +9 -11
- package/libexec/lib/manage.mjs +27 -53
- package/libexec/lib/manifest.mjs +14 -57
- package/libexec/lib/paths.mjs +2 -4
- package/libexec/lib/probe.mjs +14 -30
- package/libexec/lib/runtime.mjs +29 -37
- package/libexec/sn-route +34 -24
- package/libexec/ssh-net-link-core +4 -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 } 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, 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');
|
|
@@ -130,15 +130,23 @@ async function relayPathProbe(ctx) {
|
|
|
130
130
|
|
|
131
131
|
async function collectEdge(ctx, probe) {
|
|
132
132
|
const { topo, paths, state } = ctx;
|
|
133
|
-
const
|
|
134
|
-
const
|
|
135
|
-
const
|
|
136
|
-
const
|
|
137
|
-
if (
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
133
|
+
const { issues, entityAudits } = runtimeIssues(ctx, edgeRuntimePlan(topo));
|
|
134
|
+
const switches = state.readSwitches();
|
|
135
|
+
const link = edgeLinkJob(ctx);
|
|
136
|
+
const onDisk = launchd.labelsOnDisk(paths.launchAgentsDir, linkLabel(''));
|
|
137
|
+
if (onDisk.some((label) => label !== link?.label))
|
|
138
|
+
issues.push('有旧的本地转发任务——跑一次 ssh-net install 清理');
|
|
139
|
+
let job = null;
|
|
140
|
+
if (link && (entityAudits.some((a) => a.audit !== 'missing') || onDisk.includes(link.label))) {
|
|
141
|
+
const issue = configFileIssue(link.plistPath, link.content, '本地转发配置', '需要更新');
|
|
141
142
|
if (issue) issues.push(issue);
|
|
143
|
+
job = launchd.inspect(ctx.runner, ctx.uid, link.label);
|
|
144
|
+
if (!job.registered || job.disabled)
|
|
145
|
+
issues.push(`本地转发任务${job.registered ? '已禁用' : '未注册'}——跑一次 ssh-net install 修复`);
|
|
146
|
+
else if (switches.relay && !job.running)
|
|
147
|
+
issues.push('本地转发任务未在运行——先 stop 再按原开关 start,或跑 ssh-net install 修复');
|
|
148
|
+
if (!switches.relay && job.running)
|
|
149
|
+
issues.push('relay 已关但本地转发仍在运行——再跑一次 ssh-net stop');
|
|
142
150
|
}
|
|
143
151
|
|
|
144
152
|
// routes 被手删/过期会让 ssh 静默退回 lan 直连,是故障。sn-route 没装时
|
|
@@ -150,15 +158,6 @@ async function collectEdge(ctx, probe) {
|
|
|
150
158
|
if (issue) issues.push(issue);
|
|
151
159
|
}
|
|
152
160
|
|
|
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
161
|
// v6 只是诊断线索不是判决,与就绪实测并发。探测没跑成时不能谎称「没有
|
|
163
162
|
// IPv6」——那个结论只在 sn-ipv6 正常跑完且无输出时才成立
|
|
164
163
|
const needV6 = probe && switches.relay
|
|
@@ -171,11 +170,15 @@ async function collectEdge(ctx, probe) {
|
|
|
171
170
|
// 仍可能被新 ssh 使用,所以只要 relay 开着就继续做 banner 探测
|
|
172
171
|
const relayProbe = ctx.relayOk ?? relayPathProbe; // 测试可注入同形结果
|
|
173
172
|
const openTcp = ctx.openTcp ?? tcpOpen;
|
|
174
|
-
const forwards = probe &&
|
|
173
|
+
const forwards = probe && switches.relay ? topo.forwards : [];
|
|
175
174
|
const [relayResult, peerReady, portsOk, v6Probe] = await Promise.all([
|
|
176
175
|
probe && switches.relay ? relayProbe(ctx) : null,
|
|
177
176
|
probe && switches.relay ? peerProbeOk(ctx.runner, paths.binDir) : null,
|
|
178
|
-
Promise.all(forwards.map((
|
|
177
|
+
Promise.all(forwards.map(async ({ listenPort }) => {
|
|
178
|
+
if (!job?.pid || !await openTcp('127.0.0.1', listenPort)) return false;
|
|
179
|
+
const owners = await forwardListenerPids(ctx.runner, listenPort);
|
|
180
|
+
return owners.includes(job.pid);
|
|
181
|
+
})),
|
|
179
182
|
v6P,
|
|
180
183
|
]);
|
|
181
184
|
const relayReady = relayResult?.ready ?? null;
|
|
@@ -196,7 +199,7 @@ async function collectEdge(ctx, probe) {
|
|
|
196
199
|
|
|
197
200
|
return {
|
|
198
201
|
role: 'edge', alias: ctx.self.alias, switches, probed: probe, relayReady, peerReady,
|
|
199
|
-
|
|
202
|
+
job,
|
|
200
203
|
forwardViews: forwards.map((f, i) => [forwardLabel(f), portsOk[i]]),
|
|
201
204
|
coreAlias: topo.core.alias, relayAlias: topo.relay.alias,
|
|
202
205
|
v6, v6ProbeOk, issues,
|
|
@@ -339,11 +342,11 @@ function renderEdgeLines(s, lines) {
|
|
|
339
342
|
} else {
|
|
340
343
|
lines.push(' peer:关(当前无可用直连;不主动恢复)');
|
|
341
344
|
}
|
|
342
|
-
if (s.
|
|
345
|
+
if (s.forwardViews.length > 0) {
|
|
343
346
|
const bad = s.forwardViews.filter(([, ok]) => !ok).map(([l]) => l);
|
|
344
347
|
lines.push(lineOf('link', bad.length === 0,
|
|
345
348
|
`本地端口在听(${s.forwardViews.map(([l]) => l).join(' · ')})`,
|
|
346
|
-
|
|
349
|
+
`转发任务未监听这些端口(${bad.join('、')};检查本地转发任务与中继线路)`));
|
|
347
350
|
}
|
|
348
351
|
lines.push(s.v6 !== ''
|
|
349
352
|
? ` 对外地址:${s.v6}`
|
|
@@ -352,9 +355,7 @@ function renderEdgeLines(s, lines) {
|
|
|
352
355
|
: ' 对外地址:当前没有可用 IPv6');
|
|
353
356
|
}
|
|
354
357
|
}
|
|
355
|
-
lines.push(s.
|
|
356
|
-
? ` 本地转发(link):已安装(后台任务${s.job.running ? '运行中' : '未运行'})`
|
|
357
|
-
: ' 本地转发(link):未安装(需要 SOCKS/VNC 端口:ssh-net install --with-link)');
|
|
358
|
+
if (s.job) lines.push(` 本地转发(link):后台任务${s.job.running ? '运行中' : '未运行'}`);
|
|
358
359
|
}
|
|
359
360
|
|
|
360
361
|
function edgeTaskState(e) {
|
|
@@ -11,8 +11,6 @@ function parseTarget(args) {
|
|
|
11
11
|
if (args.length === 0 || args.length === 1 && ['-a', '--all'].includes(args[0]))
|
|
12
12
|
return 'relay';
|
|
13
13
|
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
14
|
throw new Error('用法:stop [-a|--all|relay|peer](不带参数或 -a=全部关闭)');
|
|
17
15
|
}
|
|
18
16
|
|
|
@@ -3,7 +3,7 @@
|
|
|
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
7
|
import * as launchd from '../lib/launchd.mjs';
|
|
8
8
|
import * as systemd from '../lib/systemd.mjs';
|
|
9
9
|
import {
|
|
@@ -18,7 +18,7 @@ function removeArtifacts(ctx) {
|
|
|
18
18
|
rm(ctx.paths.stateDir);
|
|
19
19
|
rm(ctx.paths.confDir);
|
|
20
20
|
rm(ctx.paths.legacyRuntimeVersionFile);
|
|
21
|
-
rm(ctx.paths.
|
|
21
|
+
rm(ctx.paths.legacyLinkEnabledFile);
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
function removeSshNetLogs(ctx) {
|
|
@@ -28,10 +28,6 @@ function removeSshNetLogs(ctx) {
|
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
function removeJobs(ctx, labels) {
|
|
32
|
-
for (const label of labels) removeJob(ctx, label, ctx.paths.plistFile(label));
|
|
33
|
-
}
|
|
34
|
-
|
|
35
31
|
function removeNodeUnits(ctx, units) {
|
|
36
32
|
for (const unit of units) {
|
|
37
33
|
// not-loaded(未装/已卸)无害,真失败必须报——否则删了 unit 文件、
|
package/libexec/lib/fsutil.mjs
CHANGED
|
@@ -4,14 +4,13 @@ import fs from 'node:fs';
|
|
|
4
4
|
import path from 'node:path';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
7
|
+
* 读取 UTF-8 文本,文件不存在时返回 null,其他错误照常抛出
|
|
8
8
|
* @param {string} file
|
|
9
|
-
* @
|
|
10
|
-
* @returns {string|Buffer|null}
|
|
9
|
+
* @returns {string|null}
|
|
11
10
|
*/
|
|
12
|
-
export function readFileOrNull(file
|
|
11
|
+
export function readFileOrNull(file) {
|
|
13
12
|
try {
|
|
14
|
-
return fs.readFileSync(file,
|
|
13
|
+
return fs.readFileSync(file, 'utf8');
|
|
15
14
|
} catch (err) {
|
|
16
15
|
if (err && err.code === 'ENOENT') return null;
|
|
17
16
|
throw err;
|
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
|
|
package/libexec/lib/jobs.mjs
CHANGED
|
@@ -8,20 +8,24 @@ import {
|
|
|
8
8
|
renderEdgeLinkPlist, renderRelayPlist, renderRelayUnit,
|
|
9
9
|
} from './render.mjs';
|
|
10
10
|
|
|
11
|
-
/**
|
|
12
|
-
export function edgeLinkJob(
|
|
13
|
-
|
|
11
|
+
/** 有转发端口才需要 LaunchAgent。启动只用任务路径,安装和体检才读取 plist 内容 */
|
|
12
|
+
export function edgeLinkJob({ topo, paths, hostTable }) {
|
|
13
|
+
if (!topo.forwards.length) return null;
|
|
14
14
|
const label = linkLabel(topo.core.alias);
|
|
15
|
+
const pathStateFile = paths.switchFiles.relay;
|
|
15
16
|
return {
|
|
16
17
|
label,
|
|
17
18
|
plistPath: paths.plistFile(label),
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
19
|
+
pathStateFile,
|
|
20
|
+
get content() {
|
|
21
|
+
return renderEdgeLinkPlist({
|
|
22
|
+
binDir: paths.binDir,
|
|
23
|
+
coreAlias: topo.core.alias,
|
|
24
|
+
forwardArgs: renderForwardArgs(topo.forwards, hostTable),
|
|
25
|
+
relayOnFile: pathStateFile,
|
|
26
|
+
logsDir: paths.logsDir,
|
|
27
|
+
});
|
|
28
|
+
},
|
|
25
29
|
};
|
|
26
30
|
}
|
|
27
31
|
|
package/libexec/lib/launchd.mjs
CHANGED
|
@@ -48,9 +48,7 @@ export function edgeAliasesOnDisk(launchAgentsDir) {
|
|
|
48
48
|
* lastExitCode:string|null}}
|
|
49
49
|
*/
|
|
50
50
|
export function parsePrint(result) {
|
|
51
|
-
//
|
|
52
|
-
// 误判成「已注册」会让 syncPlist 报 unchanged 却没在跑;按未注册走
|
|
53
|
-
// enable+bootstrap 会以真实报错浮出
|
|
51
|
+
// 只有成功的查询能确认任务在册。需要区分系统故障的调用方先由 printState 检查退出码
|
|
54
52
|
const registered =
|
|
55
53
|
result.code === 0 && !/Could not find service/.test(result.stderr ?? '');
|
|
56
54
|
if (!registered) return NOT_REGISTERED_PRINT;
|
|
@@ -119,7 +117,7 @@ export function disabledMap(runner, uid) {
|
|
|
119
117
|
* @returns {JobState}
|
|
120
118
|
*/
|
|
121
119
|
export function inspectWith(runner, uid, label, map) {
|
|
122
|
-
const printed = printState(runner, uid, label);
|
|
120
|
+
const printed = printState(runner, uid, label, false);
|
|
123
121
|
if (!printed.registered) return NOT_REGISTERED;
|
|
124
122
|
return { ...printed, disabled: map.get(label) === true };
|
|
125
123
|
}
|
|
@@ -139,16 +137,14 @@ export function inspect(runner, uid, label) {
|
|
|
139
137
|
}
|
|
140
138
|
|
|
141
139
|
/**
|
|
142
|
-
*
|
|
143
|
-
* 删除路径必须传 strict:除明确的 113/not-found 外,查询失败都当场报错,
|
|
144
|
-
* 不能把「不知道是否注册」当成「未注册」后继续删 plist。
|
|
140
|
+
* 单查注册状态、运行状态与 PID。除明确未注册外,系统查询失败即报错
|
|
145
141
|
* @param {import('./exec.mjs').Runner} runner
|
|
146
142
|
* @param {number} uid
|
|
147
143
|
* @param {string} label
|
|
148
|
-
* @param {boolean} [strict=
|
|
144
|
+
* @param {boolean} [strict=true] 只有批量展示任务状态时沿用宽松解析
|
|
149
145
|
* @returns {ReturnType<typeof parsePrint>}
|
|
150
146
|
*/
|
|
151
|
-
export function printState(runner, uid, label, strict =
|
|
147
|
+
export function printState(runner, uid, label, strict = true) {
|
|
152
148
|
const result = runner.run('launchctl', ['print', `${guiDomain(uid)}/${label}`]);
|
|
153
149
|
if (strict && result.code !== 0 && result.code !== 113
|
|
154
150
|
&& !/Could not find service/.test(result.stderr ?? ''))
|
|
@@ -163,8 +159,9 @@ export const disable = (runner, uid, label) =>
|
|
|
163
159
|
runner.run('launchctl', ['disable', `${guiDomain(uid)}/${label}`]);
|
|
164
160
|
export const bootstrap = (runner, uid, plistPath) =>
|
|
165
161
|
runner.run('launchctl', ['bootstrap', guiDomain(uid), plistPath]);
|
|
162
|
+
// 不加 -k:若查询后任务已自行启动,绝不能打断刚建好的连接
|
|
166
163
|
export const kickstart = (runner, uid, label) =>
|
|
167
|
-
runner.run('launchctl', ['kickstart',
|
|
164
|
+
runner.run('launchctl', ['kickstart', `${guiDomain(uid)}/${label}`]);
|
|
168
165
|
/**
|
|
169
166
|
* enable + bootstrap 连发(未注册 job 的加载序列)。任一步失败抛错——
|
|
170
167
|
* enable 失败若吞掉,只会以 bootstrap error 5 的间接形式浮出,排障被带偏。
|
|
@@ -185,7 +182,8 @@ export function enableBootstrap(runner, uid, label, plistPath) {
|
|
|
185
182
|
export const kickstartEnabled = (runner, uid, label) => {
|
|
186
183
|
const en = enable(runner, uid, label);
|
|
187
184
|
if (en.code !== 0) throw new Error(`enable 失败(${label}):${en.stderr.trim()}`);
|
|
188
|
-
|
|
185
|
+
// 只有收到报名时才强制换掉旧回拨,使用 edge 刚上报的地址
|
|
186
|
+
return runner.run('launchctl', ['kickstart', '-k', `${guiDomain(uid)}/${label}`]);
|
|
189
187
|
};
|
|
190
188
|
export const killJob = (runner, uid, label, signal = 'TERM') =>
|
|
191
189
|
runner.run('launchctl', ['kill', signal, `${guiDomain(uid)}/${label}`]);
|
package/libexec/lib/manage.mjs
CHANGED
|
@@ -6,54 +6,40 @@ import * as launchd from './launchd.mjs';
|
|
|
6
6
|
import { readFileOrNull, withTempFile } from './fsutil.mjs';
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
10
|
-
* launchd.mjs 头注;syncPlist 的「文件没变」分支与 `start core` 共用)。
|
|
9
|
+
* install 与 start 共用的任务恢复逻辑。带 PathState 的任务由 launchd 按文件状态启动
|
|
11
10
|
* @param {object} ctx
|
|
12
|
-
* @param {{label:string, plistPath:string}} job
|
|
13
|
-
* @param {object} [opts]
|
|
14
|
-
* @param {boolean} [opts.kickWhenStopped=false] registered+enabled 但没在跑时
|
|
15
|
-
* 是否踢活:install 对账不踢(不断连),start 要踢
|
|
16
|
-
* @param {boolean} [opts.reviveRunningDisabled=false] registered+disabled 但
|
|
17
|
-
* 还在跑时是否踢重启:install 要 revive,start 只清 override、不闪断
|
|
18
|
-
* 只执行必要的加载/恢复动作;是否有配置变化由 syncPlist 另行判断
|
|
11
|
+
* @param {{label:string, plistPath:string, pathStateFile?:string}} job
|
|
19
12
|
*/
|
|
20
|
-
export function ensureLoaded(
|
|
21
|
-
kickWhenStopped = false,
|
|
22
|
-
reviveRunningDisabled = false,
|
|
23
|
-
} = {}) {
|
|
24
|
-
const { runner, uid } = ctx;
|
|
13
|
+
export function ensureLoaded({ runner, uid }, { label, plistPath, pathStateFile }) {
|
|
25
14
|
const st = launchd.inspect(runner, uid, label);
|
|
26
|
-
|
|
15
|
+
// enable 本身不会首发。重新注册让 launchd 按当下开关决定,避免与 stop 抢启动
|
|
16
|
+
const reloadPathState = pathStateFile && st.disabled && !st.running && fs.existsSync(pathStateFile);
|
|
17
|
+
if (reloadPathState) bootoutJob(runner, uid, label);
|
|
18
|
+
if (!st.registered || reloadPathState) {
|
|
27
19
|
launchd.enableBootstrap(runner, uid, label, plistPath);
|
|
28
20
|
return;
|
|
29
21
|
}
|
|
30
|
-
if (st.disabled && (!st.running || reviveRunningDisabled)) {
|
|
31
|
-
const k = launchd.kickstartEnabled(runner, uid, label);
|
|
32
|
-
if (k.code !== 0) throw new Error(`kickstart 失败(${label}):${k.stderr.trim()}`);
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
35
22
|
if (st.disabled) {
|
|
36
|
-
//
|
|
23
|
+
// disable 不会停掉已在跑的进程;清除标记就够了,不必断开现有连接
|
|
37
24
|
const en = launchd.enable(runner, uid, label);
|
|
38
25
|
if (en.code !== 0) throw new Error(`enable 失败(${label}):${en.stderr.trim()}`);
|
|
39
|
-
return;
|
|
40
26
|
}
|
|
41
|
-
if (
|
|
27
|
+
if (!pathStateFile && !st.running) {
|
|
42
28
|
const k = launchd.kickstart(runner, uid, label);
|
|
43
29
|
if (k.code !== 0) throw new Error(`kickstart 失败(${label}):${k.stderr.trim()}`);
|
|
44
|
-
return;
|
|
45
30
|
}
|
|
46
31
|
}
|
|
47
32
|
|
|
48
33
|
/**
|
|
49
34
|
* 同步一个 plist 到磁盘并按需注册(幂等、不断连)。
|
|
50
35
|
* @param {object} ctx
|
|
51
|
-
* @param {{label:string, plistPath:string, content:string}} job
|
|
36
|
+
* @param {{label:string, plistPath:string, content:string, pathStateFile?:string}} job
|
|
52
37
|
* @returns {{changed:boolean, restarted?:boolean, lintError?:string}}
|
|
53
38
|
* changed 只表示 plist 内容变化,服务恢复不计入;restarted 只在替换已注册
|
|
54
39
|
* job 的配置时为真,用于提示可能断连
|
|
55
40
|
*/
|
|
56
|
-
export function syncPlist(ctx,
|
|
41
|
+
export function syncPlist(ctx, job) {
|
|
42
|
+
const { label, plistPath, content } = job;
|
|
57
43
|
const { runner, uid, paths } = ctx;
|
|
58
44
|
fs.mkdirSync(paths.launchAgentsDir, { recursive: true });
|
|
59
45
|
fs.mkdirSync(paths.logsDir, { recursive: true });
|
|
@@ -61,11 +47,7 @@ export function syncPlist(ctx, { label, plistPath, content }) {
|
|
|
61
47
|
const existing = readFileOrNull(plistPath);
|
|
62
48
|
|
|
63
49
|
if (existing === content) {
|
|
64
|
-
|
|
65
|
-
// enable 后拉起,否则 install 报配置未变却没在跑、start 的 kickstart 被拒
|
|
66
|
-
ensureLoaded(ctx, { label, plistPath }, {
|
|
67
|
-
reviveRunningDisabled: true,
|
|
68
|
-
});
|
|
50
|
+
ensureLoaded(ctx, job);
|
|
69
51
|
return { changed: false };
|
|
70
52
|
}
|
|
71
53
|
|
|
@@ -78,11 +60,7 @@ export function syncPlist(ctx, { label, plistPath, content }) {
|
|
|
78
60
|
|
|
79
61
|
// 只要 registered 一个事实:printState 即可,不跑全域 print-disabled
|
|
80
62
|
const st = launchd.printState(runner, uid, label);
|
|
81
|
-
if (st.registered)
|
|
82
|
-
const bo = launchd.bootout(runner, uid, label);
|
|
83
|
-
if (bo.code !== 0 && bo.code !== 113)
|
|
84
|
-
throw new Error(`bootout 失败(${label}):${bo.stderr.trim()}`);
|
|
85
|
-
}
|
|
63
|
+
if (st.registered) bootoutJob(runner, uid, label);
|
|
86
64
|
fs.renameSync(tmp, plistPath);
|
|
87
65
|
fs.chmodSync(plistPath, 0o644);
|
|
88
66
|
// 无条件先 enable(override 残留时 bootstrap error 5;launchd.mjs 宣言
|
|
@@ -95,24 +73,20 @@ export function syncPlist(ctx, { label, plistPath, content }) {
|
|
|
95
73
|
}
|
|
96
74
|
|
|
97
75
|
/**
|
|
98
|
-
*
|
|
76
|
+
* 逐个卸载磁盘任务,在册的先 bootout,再删 plist。查询失败时保留文件
|
|
99
77
|
* @param {object} ctx
|
|
100
|
-
* @param {string}
|
|
101
|
-
* @param {string} plistPath
|
|
102
|
-
* @returns {boolean} 是否在册任务或磁盘 plist 确有清理
|
|
78
|
+
* @param {Iterable<string>} labels
|
|
103
79
|
*/
|
|
104
|
-
export function
|
|
105
|
-
const
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
if (st.registered) {
|
|
110
|
-
const bo = launchd.bootout(runner, uid, label);
|
|
111
|
-
// 113 = 未注册(竞态下消失),其余失败必须报:job 还在注册表里,删了
|
|
112
|
-
// plist 会留下内存态孤儿
|
|
113
|
-
if (bo.code !== 0 && bo.code !== 113)
|
|
114
|
-
throw new Error(`bootout 失败(${label}):${bo.stderr.trim()}`);
|
|
80
|
+
export function removeJobs({ runner, uid, paths }, labels) {
|
|
81
|
+
for (const label of labels) {
|
|
82
|
+
const st = launchd.printState(runner, uid, label);
|
|
83
|
+
if (st.registered) bootoutJob(runner, uid, label);
|
|
84
|
+
fs.rmSync(paths.plistFile(label), { force: true });
|
|
115
85
|
}
|
|
116
|
-
|
|
117
|
-
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function bootoutJob(runner, uid, label) {
|
|
89
|
+
const bo = launchd.bootout(runner, uid, label);
|
|
90
|
+
if (bo.code !== 0 && bo.code !== 113)
|
|
91
|
+
throw new Error(`bootout 失败(${label}):${bo.stderr.trim()}`);
|
|
118
92
|
}
|
package/libexec/lib/manifest.mjs
CHANGED
|
@@ -1,12 +1,7 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
// 异常」不会再分叉。
|
|
4
|
-
//
|
|
5
|
-
// entities:[bin 名, 包脚本名]——原名实体;links:[带机器名软链, 实体名]。
|
|
1
|
+
// install、报名与 status 共用的运行时清单。实体按包内原名安装;只有服务软链
|
|
2
|
+
// 需要两列:[带机器名的链名, 实体名](design §2.3)
|
|
6
3
|
import { edgeLinkName, linkLinkName, relayLinkName } from './render.mjs';
|
|
7
4
|
|
|
8
|
-
// bin 脚本名:plan 清单与按名判断(status 体检、install/报名调用)共用,
|
|
9
|
-
// 改名一处。跨模块消费的只有 SN_* 与 UPLINK_EDGE,另两个仅本文件 plan 用
|
|
10
5
|
export const SN_PROBE = 'sn-probe';
|
|
11
6
|
export const SN_ROUTE = 'sn-route';
|
|
12
7
|
export const SN_IPV6 = 'sn-ipv6';
|
|
@@ -14,71 +9,33 @@ export const SN_REGISTER_LAN = 'sn-register-lan';
|
|
|
14
9
|
const LINK_CORE = 'ssh-net-link-core';
|
|
15
10
|
const UPLINK_RELAY = 'ssh-net-uplink-relay';
|
|
16
11
|
export const UPLINK_EDGE = 'ssh-net-uplink-edge';
|
|
17
|
-
//
|
|
18
|
-
// 不影响 update 的重连提示
|
|
12
|
+
// 只有常驻脚本的更新需要提示「下次重连后生效」
|
|
19
13
|
export const RESIDENT_SCRIPTS = Object.freeze([LINK_CORE, UPLINK_RELAY, UPLINK_EDGE]);
|
|
20
14
|
|
|
21
|
-
/**
|
|
22
|
-
|
|
23
|
-
* 时并入;install --without-link 按这份清单删。
|
|
24
|
-
* @param {import('./config.mjs').Topology} topo
|
|
25
|
-
*/
|
|
26
|
-
export function linkAddonPlan(topo) {
|
|
15
|
+
/** @param {import('./config.mjs').Topology} topo */
|
|
16
|
+
export function edgeRuntimePlan(topo) {
|
|
27
17
|
return {
|
|
28
|
-
entities: [
|
|
18
|
+
entities: [SN_PROBE, SN_ROUTE, SN_IPV6, LINK_CORE],
|
|
29
19
|
links: [[linkLinkName(topo.core.alias), LINK_CORE]],
|
|
30
20
|
};
|
|
31
21
|
}
|
|
32
22
|
|
|
33
|
-
/**
|
|
34
|
-
* edge 形态:基础三件套恒装;装了 link 附加件才加第 4 实体与带机器名软链。
|
|
35
|
-
* @param {import('./config.mjs').Topology} topo
|
|
36
|
-
* @param {boolean} linkInstalled
|
|
37
|
-
*/
|
|
38
|
-
export function edgeRuntimePlan(topo, linkInstalled) {
|
|
39
|
-
/** @type {[string,string][]} */
|
|
40
|
-
const entities = [
|
|
41
|
-
[SN_PROBE, SN_PROBE],
|
|
42
|
-
[SN_ROUTE, SN_ROUTE], // Match exec 按需选路 + 成功后本地通知/秒切(design §2.2)
|
|
43
|
-
[SN_IPV6, SN_IPV6],
|
|
44
|
-
];
|
|
45
|
-
return linkInstalled
|
|
46
|
-
? mergePlans({ entities, links: [] }, linkAddonPlan(topo))
|
|
47
|
-
: { entities, links: [] };
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* core/node 基础形态:常驻 relay 一件套 + sn-register-lan(rc 登录调起的
|
|
52
|
-
* edge 内网地址登记,零常驻)。installCore 在其上并入在册 edge 的回拨组件。
|
|
23
|
+
/** core 安装时一并刷新已登记的回拨脚本;node 只有中继与内网地址登记脚本
|
|
53
24
|
* @param {import('./config.mjs').Topology} topo
|
|
25
|
+
* @param {readonly string[]} [registeredEdges]
|
|
54
26
|
*/
|
|
55
|
-
export function coreNodeRuntimePlan(topo) {
|
|
27
|
+
export function coreNodeRuntimePlan(topo, registeredEdges = []) {
|
|
28
|
+
const callbacks = edgeCallbacksPlan(registeredEdges);
|
|
56
29
|
return {
|
|
57
|
-
entities: [
|
|
58
|
-
|
|
59
|
-
[SN_REGISTER_LAN, SN_REGISTER_LAN],
|
|
60
|
-
],
|
|
61
|
-
links: [[relayLinkName(topo.relay.alias), UPLINK_RELAY]],
|
|
30
|
+
entities: [UPLINK_RELAY, SN_REGISTER_LAN, ...callbacks.entities],
|
|
31
|
+
links: [[relayLinkName(topo.relay.alias), UPLINK_RELAY], ...callbacks.links],
|
|
62
32
|
};
|
|
63
33
|
}
|
|
64
34
|
|
|
65
|
-
/**
|
|
66
|
-
* core 侧在册 edge 的回拨组件并集:uplink-edge 全 core 一份实体,每台 edge
|
|
67
|
-
* 一条带别名软链(install 按磁盘在册、_register 传单台、status 按 topology
|
|
68
|
-
* 在册,三方同源)。
|
|
69
|
-
* @param {readonly string[]} aliases
|
|
70
|
-
*/
|
|
35
|
+
/** core 上已登记的 edge 共用一份回拨脚本,每台各有一条软链。@param {readonly string[]} aliases */
|
|
71
36
|
export function edgeCallbacksPlan(aliases) {
|
|
72
37
|
return {
|
|
73
|
-
entities: aliases.length ? [
|
|
38
|
+
entities: aliases.length ? [UPLINK_EDGE] : [],
|
|
74
39
|
links: aliases.map((alias) => [edgeLinkName(alias), UPLINK_EDGE]),
|
|
75
40
|
};
|
|
76
41
|
}
|
|
77
|
-
|
|
78
|
-
/** 多份计划并集(顺序拼接;调用方保证各份之间实体/软链不重复)。 */
|
|
79
|
-
export function mergePlans(...plans) {
|
|
80
|
-
return {
|
|
81
|
-
entities: plans.flatMap((p) => p.entities),
|
|
82
|
-
links: plans.flatMap((p) => p.links),
|
|
83
|
-
};
|
|
84
|
-
}
|
package/libexec/lib/paths.mjs
CHANGED
|
@@ -5,7 +5,6 @@
|
|
|
5
5
|
// topology dotfiles 托管的拓扑(名/角色/relay 口/用户)
|
|
6
6
|
// bin/ 包脚本实体 + 服务软链(install/报名对账;运行时零 npm/fnm 依赖)
|
|
7
7
|
// state/ 易变状态(两开关、edges v6、addr 入口地址、PROBE_FAIL、ENSURE)
|
|
8
|
-
// link.enabled link 附加件的持久安装意图(树根,不放 state/)
|
|
9
8
|
// conf.d/ 渲染给 ssh Include 的配置片段(生成物,不进 dotfiles):
|
|
10
9
|
// routes edge:Match 选路块 + 尾部打标块(install 渲染)
|
|
11
10
|
// edge-<别名> core/node:edge 入口(install 渲染,地址 Include state/addr)
|
|
@@ -72,9 +71,8 @@ export function machinePaths(env = process.env) {
|
|
|
72
71
|
relay: path.join(stateDir, 'RELAY_ON'),
|
|
73
72
|
peer: path.join(stateDir, 'PEER_ON'),
|
|
74
73
|
}),
|
|
75
|
-
//
|
|
76
|
-
|
|
77
|
-
linkEnabledFile: path.join(sshNetDir, 'link.enabled'),
|
|
74
|
+
// 旧版 edge 的选装标记(2026-11 退役),仅供安装与卸载清理,不再参与判断
|
|
75
|
+
legacyLinkEnabledFile: path.join(sshNetDir, 'link.enabled'),
|
|
78
76
|
// PROBE_FAIL:sn-route 的失败印记(内容 epoch,15s TTL)。文件名是
|
|
79
77
|
// paths ↔ sn-route 两处契约(constants.test 锚)
|
|
80
78
|
probeFailFile: path.join(stateDir, 'PROBE_FAIL'),
|
package/libexec/lib/probe.mjs
CHANGED
|
@@ -44,21 +44,20 @@ function runLsof(runner, args, port) {
|
|
|
44
44
|
throw new Error(`lsof 查询端口 ${port} 失败:${detail}`);
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
/**
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
.
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
.filter((n) => Number.isInteger(n));
|
|
47
|
+
/** 只认覆盖 127.0.0.1 的监听。同号的 IPv6 通配监听可能只收 IPv6,单独探测 */
|
|
48
|
+
export async function forwardListenerPids(runner, port) {
|
|
49
|
+
const r = runLsof(runner, ['-nP', '-Fpftn', `-iTCP:${port}`, '-sTCP:LISTEN'], port);
|
|
50
|
+
const ipv4 = new Set();
|
|
51
|
+
const wildcard6 = new Set();
|
|
52
|
+
let pid, type;
|
|
53
|
+
for (const field of r.stdout.split('\n')) {
|
|
54
|
+
if (field.startsWith('p')) pid = Number(field.slice(1));
|
|
55
|
+
if (field.startsWith('t')) type = field.slice(1);
|
|
56
|
+
if (field === `n127.0.0.1:${port}` || field === `n[::ffff:127.0.0.1]:${port}`) ipv4.add(pid);
|
|
57
|
+
if (field === `n*:${port}`) (type === 'IPv6' ? wildcard6 : ipv4).add(pid);
|
|
58
|
+
}
|
|
59
|
+
if (ipv4.size === 0 && wildcard6.size && await tcpOpen('127.0.0.1', port)) return [...wildcard6];
|
|
60
|
+
return [...ipv4];
|
|
62
61
|
}
|
|
63
62
|
|
|
64
63
|
/**
|
|
@@ -91,21 +90,6 @@ export function v6LoopbackPids(text, port) {
|
|
|
91
90
|
.filter((n) => Number.isInteger(n));
|
|
92
91
|
}
|
|
93
92
|
|
|
94
|
-
/**
|
|
95
|
-
* 本地转发口预检:无监听=空闲;持口者全是 link job 进程本身=本服务;否则冲突。
|
|
96
|
-
* @param {import('./exec.mjs').Runner} runner
|
|
97
|
-
* @param {number} port
|
|
98
|
-
* @param {number|null} jobPid link job 的 pid(未注册或没在跑给 null)
|
|
99
|
-
* @returns {{kind:'free'|'ours'|'foreign', pids:number[]}}
|
|
100
|
-
*/
|
|
101
|
-
export function classifyLocalPort(runner, port, jobPid) {
|
|
102
|
-
const pids = listListeners(runner, port);
|
|
103
|
-
if (pids.length === 0) return { kind: 'free', pids: [] };
|
|
104
|
-
return jobPid !== null && pids.every((p) => p === jobPid)
|
|
105
|
-
? { kind: 'ours', pids }
|
|
106
|
-
: { kind: 'foreign', pids };
|
|
107
|
-
}
|
|
108
|
-
|
|
109
93
|
/**
|
|
110
94
|
* 跑 bin/sn-probe(install 预检与 status 共用同一实现)。异步:status 把
|
|
111
95
|
* 它与 relay 实测、端口探测并发,避免串行累加超时。
|