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
package/libexec/lib/jobs.mjs
CHANGED
|
@@ -1,27 +1,31 @@
|
|
|
1
1
|
// install 管理的服务配置装配:写盘与 status 体检共用同一份渲染输入,
|
|
2
2
|
// 避免只改 plist/unit 模板时,status 因运行时脚本未变而漏报未同步升级。
|
|
3
|
-
import os from 'node:os';
|
|
4
3
|
import path from 'node:path';
|
|
5
4
|
import { renderForwardArgs } from './config.mjs';
|
|
5
|
+
import { nodeAccountHome } from './identity.mjs';
|
|
6
6
|
import {
|
|
7
7
|
linkLabel, relayLabel, relayUnitName,
|
|
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
|
|
|
@@ -41,17 +45,13 @@ export function coreRelayJob(ctx) {
|
|
|
41
45
|
};
|
|
42
46
|
}
|
|
43
47
|
|
|
44
|
-
/** node 中继 systemd unit
|
|
45
|
-
export function nodeRelayJob(ctx) {
|
|
48
|
+
/** node 中继 systemd unit。status 只读取预期配置,不要求 root */
|
|
49
|
+
export function nodeRelayJob(ctx, home = nodeAccountHome(ctx)) {
|
|
46
50
|
const { topo, paths } = ctx;
|
|
47
51
|
const unitName = relayUnitName(topo.relay.alias);
|
|
48
|
-
// HOME 用 root 的真实家目录(getpwuid),不取 $HOME:sudo 保留 HOME 会把
|
|
49
|
-
// 调用者的家目录写进 unit,root 的 ssh 随之读错 config
|
|
50
|
-
const home = ctx.nodeHome ?? os.userInfo().homedir;
|
|
51
52
|
return {
|
|
52
53
|
unitName,
|
|
53
54
|
unitPath: path.join(paths.systemdUnitDir, unitName),
|
|
54
|
-
home,
|
|
55
55
|
content: renderRelayUnit({
|
|
56
56
|
binDir: paths.binDir,
|
|
57
57
|
relayAlias: topo.relay.alias,
|
package/libexec/lib/launchd.mjs
CHANGED
|
@@ -5,42 +5,28 @@
|
|
|
5
5
|
// - 任何 bootstrap 前无条件先 enable(override 残留时 bootstrap error 5)
|
|
6
6
|
// - enable 本身不触发首发,唤醒必须 kickstart -k
|
|
7
7
|
import { readDirOrEmpty } from './fsutil.mjs';
|
|
8
|
-
import { edgeLabel } from './render.mjs';
|
|
9
8
|
|
|
10
9
|
/** @param {number} uid */
|
|
11
10
|
export const guiDomain = (uid) => `gui/${uid}`;
|
|
12
11
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
/** 未注册状态的固定形状(parsePrint 与两个 inspect 的未注册返回共用)。 */
|
|
12
|
+
/** 未注册时的查询结果,只有 inspect 额外返回禁用状态 */
|
|
16
13
|
const NOT_REGISTERED_PRINT = Object.freeze(
|
|
17
14
|
{ registered: false, running: false, pid: null, lastExitCode: null },
|
|
18
15
|
);
|
|
19
16
|
const NOT_REGISTERED = Object.freeze({ disabled: false, ...NOT_REGISTERED_PRINT });
|
|
20
17
|
|
|
21
18
|
/**
|
|
22
|
-
*
|
|
19
|
+
* 磁盘上的 job label,可按前缀筛选;目录不存在时返回空清单
|
|
23
20
|
* @param {string} launchAgentsDir
|
|
24
|
-
* @param {string} prefix
|
|
21
|
+
* @param {string} [prefix]
|
|
25
22
|
* @returns {string[]}
|
|
26
23
|
*/
|
|
27
|
-
export function labelsOnDisk(launchAgentsDir, prefix) {
|
|
24
|
+
export function labelsOnDisk(launchAgentsDir, prefix = '') {
|
|
28
25
|
return readDirOrEmpty(launchAgentsDir)
|
|
29
26
|
.filter((f) => f.startsWith(prefix) && f.endsWith('.plist'))
|
|
30
27
|
.map((f) => f.slice(0, -'.plist'.length));
|
|
31
28
|
}
|
|
32
29
|
|
|
33
|
-
/**
|
|
34
|
-
* 磁盘 LaunchAgents 里 uplink-edge job plist 的别名清单(install/status/
|
|
35
|
-
* uninstall 共用;topology 已删的机器只能靠这个 glob 兜底发现)。
|
|
36
|
-
* @param {string} launchAgentsDir
|
|
37
|
-
* @returns {string[]}
|
|
38
|
-
*/
|
|
39
|
-
export function edgeAliasesOnDisk(launchAgentsDir) {
|
|
40
|
-
return labelsOnDisk(launchAgentsDir, EDGE_PLIST_PREFIX)
|
|
41
|
-
.map((label) => label.slice(EDGE_PLIST_PREFIX.length));
|
|
42
|
-
}
|
|
43
|
-
|
|
44
30
|
/**
|
|
45
31
|
* print 正文解析。
|
|
46
32
|
* @param {{code:number|null, stderr:string, stdout:string}} result
|
|
@@ -48,9 +34,7 @@ export function edgeAliasesOnDisk(launchAgentsDir) {
|
|
|
48
34
|
* lastExitCode:string|null}}
|
|
49
35
|
*/
|
|
50
36
|
export function parsePrint(result) {
|
|
51
|
-
//
|
|
52
|
-
// 误判成「已注册」会让 syncPlist 报 unchanged 却没在跑;按未注册走
|
|
53
|
-
// enable+bootstrap 会以真实报错浮出
|
|
37
|
+
// 只有成功的查询能确认任务在册。需要区分系统故障的调用方先由 printState 检查退出码
|
|
54
38
|
const registered =
|
|
55
39
|
result.code === 0 && !/Could not find service/.test(result.stderr ?? '');
|
|
56
40
|
if (!registered) return NOT_REGISTERED_PRINT;
|
|
@@ -94,7 +78,7 @@ export function parseDisabled(stdout) {
|
|
|
94
78
|
|
|
95
79
|
/**
|
|
96
80
|
* 整个 gui 域的 disabled override 字典(一次 print-disabled)。同一域多个
|
|
97
|
-
* label
|
|
81
|
+
* label 体检时可在循环外取一次,交给各次 inspect 共用
|
|
98
82
|
* @param {import('./exec.mjs').Runner} runner
|
|
99
83
|
* @param {number} uid
|
|
100
84
|
* @returns {Map<string, boolean>}
|
|
@@ -110,47 +94,29 @@ export function disabledMap(runner, uid) {
|
|
|
110
94
|
}
|
|
111
95
|
|
|
112
96
|
/**
|
|
113
|
-
*
|
|
114
|
-
* 共用)。print 先行:未注册不碰传入 map。
|
|
115
|
-
* @param {import('./exec.mjs').Runner} runner
|
|
116
|
-
* @param {number} uid
|
|
117
|
-
* @param {string} label
|
|
118
|
-
* @param {Map<string, boolean>} map disabledMap 的产物
|
|
119
|
-
* @returns {JobState}
|
|
120
|
-
*/
|
|
121
|
-
export function inspectWith(runner, uid, label, map) {
|
|
122
|
-
const printed = printState(runner, uid, label);
|
|
123
|
-
if (!printed.registered) return NOT_REGISTERED;
|
|
124
|
-
return { ...printed, disabled: map.get(label) === true };
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* print + print-disabled 合成四态所需的全部事实(单 label 便捷封装)。
|
|
129
|
-
* print 在前、未注册即返回(不白跑全域 dump)。
|
|
97
|
+
* 查询一个任务的注册、运行与禁用状态。批量查询可共用同一份禁用字典
|
|
130
98
|
* @param {import('./exec.mjs').Runner} runner
|
|
131
99
|
* @param {number} uid
|
|
132
100
|
* @param {string} label
|
|
101
|
+
* @param {Map<string, boolean>} [map] disabledMap 的结果
|
|
133
102
|
* @returns {JobState}
|
|
134
103
|
*/
|
|
135
|
-
export function inspect(runner, uid, label) {
|
|
104
|
+
export function inspect(runner, uid, label, map) {
|
|
136
105
|
const printed = printState(runner, uid, label);
|
|
137
106
|
if (!printed.registered) return NOT_REGISTERED;
|
|
138
|
-
return { ...printed, disabled: disabledMap(runner, uid).get(label) === true };
|
|
107
|
+
return { ...printed, disabled: (map ?? disabledMap(runner, uid)).get(label) === true };
|
|
139
108
|
}
|
|
140
109
|
|
|
141
110
|
/**
|
|
142
|
-
*
|
|
143
|
-
* 删除路径必须传 strict:除明确的 113/not-found 外,查询失败都当场报错,
|
|
144
|
-
* 不能把「不知道是否注册」当成「未注册」后继续删 plist。
|
|
111
|
+
* 单查注册状态、运行状态与 PID。除明确未注册外,系统查询失败即报错
|
|
145
112
|
* @param {import('./exec.mjs').Runner} runner
|
|
146
113
|
* @param {number} uid
|
|
147
114
|
* @param {string} label
|
|
148
|
-
* @param {boolean} [strict=false]
|
|
149
115
|
* @returns {ReturnType<typeof parsePrint>}
|
|
150
116
|
*/
|
|
151
|
-
export function printState(runner, uid, label
|
|
117
|
+
export function printState(runner, uid, label) {
|
|
152
118
|
const result = runner.run('launchctl', ['print', `${guiDomain(uid)}/${label}`]);
|
|
153
|
-
if (
|
|
119
|
+
if (result.code !== 0 && result.code !== 113
|
|
154
120
|
&& !/Could not find service/.test(result.stderr ?? ''))
|
|
155
121
|
throw new Error(`launchctl print 失败(${label}):${result.stderr.trim() || `exit ${result.code}`}`);
|
|
156
122
|
return parsePrint(result);
|
|
@@ -163,8 +129,9 @@ export const disable = (runner, uid, label) =>
|
|
|
163
129
|
runner.run('launchctl', ['disable', `${guiDomain(uid)}/${label}`]);
|
|
164
130
|
export const bootstrap = (runner, uid, plistPath) =>
|
|
165
131
|
runner.run('launchctl', ['bootstrap', guiDomain(uid), plistPath]);
|
|
132
|
+
// 不加 -k:若查询后任务已自行启动,绝不能打断刚建好的连接
|
|
166
133
|
export const kickstart = (runner, uid, label) =>
|
|
167
|
-
runner.run('launchctl', ['kickstart',
|
|
134
|
+
runner.run('launchctl', ['kickstart', `${guiDomain(uid)}/${label}`]);
|
|
168
135
|
/**
|
|
169
136
|
* enable + bootstrap 连发(未注册 job 的加载序列)。任一步失败抛错——
|
|
170
137
|
* enable 失败若吞掉,只会以 bootstrap error 5 的间接形式浮出,排障被带偏。
|
|
@@ -185,7 +152,8 @@ export function enableBootstrap(runner, uid, label, plistPath) {
|
|
|
185
152
|
export const kickstartEnabled = (runner, uid, label) => {
|
|
186
153
|
const en = enable(runner, uid, label);
|
|
187
154
|
if (en.code !== 0) throw new Error(`enable 失败(${label}):${en.stderr.trim()}`);
|
|
188
|
-
|
|
155
|
+
// 只有收到报名时才强制换掉旧回拨,使用 edge 刚上报的地址
|
|
156
|
+
return runner.run('launchctl', ['kickstart', '-k', `${guiDomain(uid)}/${label}`]);
|
|
189
157
|
};
|
|
190
158
|
export const killJob = (runner, uid, label, signal = 'TERM') =>
|
|
191
159
|
runner.run('launchctl', ['kill', signal, `${guiDomain(uid)}/${label}`]);
|
package/libexec/lib/manage.mjs
CHANGED
|
@@ -3,116 +3,157 @@
|
|
|
3
3
|
// 渲染结果与现文件字节相同则不 bootout——install 幂等不断连。
|
|
4
4
|
import fs from 'node:fs';
|
|
5
5
|
import * as launchd from './launchd.mjs';
|
|
6
|
-
import
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import { readFileOrNull, removeObsoleteNames, withTempFile, writeFileAtomic } from './fsutil.mjs';
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
|
-
*
|
|
10
|
-
* launchd.mjs 头注;syncPlist 的「文件没变」分支与 `start core` 共用)。
|
|
10
|
+
* install 与 start 共用的任务恢复逻辑。带 PathState 的任务由 launchd 按文件状态启动
|
|
11
11
|
* @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 另行判断
|
|
12
|
+
* @param {{label:string, plistPath:string, pathStateFile?:string}} job
|
|
19
13
|
*/
|
|
20
|
-
export function ensureLoaded(
|
|
21
|
-
kickWhenStopped = false,
|
|
22
|
-
reviveRunningDisabled = false,
|
|
23
|
-
} = {}) {
|
|
24
|
-
const { runner, uid } = ctx;
|
|
14
|
+
export function ensureLoaded({ runner, uid }, { label, plistPath, pathStateFile }) {
|
|
25
15
|
const st = launchd.inspect(runner, uid, label);
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
16
|
+
// enable 本身不会首发。重新注册让 launchd 按当下开关决定,避免与 stop 抢启动
|
|
17
|
+
const reloadPathState = pathStateFile && st.disabled && !st.running && fs.existsSync(pathStateFile);
|
|
18
|
+
if (!st.registered || reloadPathState) {
|
|
19
|
+
const wasDisabled = st.disabled || launchd.disabledMap(runner, uid).get(label) === true;
|
|
20
|
+
if (reloadPathState) bootoutJob(runner, uid, label);
|
|
21
|
+
try {
|
|
22
|
+
launchd.enableBootstrap(runner, uid, label, plistPath);
|
|
23
|
+
} catch (err) {
|
|
24
|
+
// enable 成功、bootstrap 失败时,下一次登录也要继续服从之前的禁用标记
|
|
25
|
+
if (wasDisabled && !launchd.printState(runner, uid, label).registered) {
|
|
26
|
+
const r = launchd.disable(runner, uid, label);
|
|
27
|
+
if (r.code !== 0) throw new Error(`${err.message};恢复 ${label} 的禁用状态失败:${r.stderr.trim()}`, { cause: err });
|
|
28
|
+
}
|
|
29
|
+
throw err;
|
|
30
|
+
}
|
|
33
31
|
return;
|
|
34
32
|
}
|
|
35
33
|
if (st.disabled) {
|
|
36
|
-
//
|
|
34
|
+
// disable 不会停掉已在跑的进程;清除标记就够了,不必断开现有连接
|
|
37
35
|
const en = launchd.enable(runner, uid, label);
|
|
38
36
|
if (en.code !== 0) throw new Error(`enable 失败(${label}):${en.stderr.trim()}`);
|
|
39
|
-
return;
|
|
40
37
|
}
|
|
41
|
-
if (
|
|
38
|
+
if (!pathStateFile && !st.running) {
|
|
42
39
|
const k = launchd.kickstart(runner, uid, label);
|
|
43
40
|
if (k.code !== 0) throw new Error(`kickstart 失败(${label}):${k.stderr.trim()}`);
|
|
44
|
-
return;
|
|
45
41
|
}
|
|
46
42
|
}
|
|
47
43
|
|
|
48
44
|
/**
|
|
49
|
-
* 同步一个 plist
|
|
45
|
+
* 同步一个 plist 到磁盘并按需注册。改名时等新配置就绪再撤旧任务,避免争用同一端口
|
|
50
46
|
* @param {object} ctx
|
|
51
|
-
* @param {{label:string, plistPath:string, content:string}} job
|
|
47
|
+
* @param {{label:string, plistPath:string, content:string, pathStateFile?:string}} job
|
|
48
|
+
* @param {string[]} [obsolete] 本次安装要撤掉的旧任务名
|
|
49
|
+
* @param {{apply:Function, restore:Function}} [routes] edge 转发须与对应版本的选路片段一起启用
|
|
52
50
|
* @returns {{changed:boolean, restarted?:boolean, lintError?:string}}
|
|
53
51
|
* changed 只表示 plist 内容变化,服务恢复不计入;restarted 只在替换已注册
|
|
54
52
|
* job 的配置时为真,用于提示可能断连
|
|
55
53
|
*/
|
|
56
|
-
export function syncPlist(ctx,
|
|
54
|
+
export function syncPlist(ctx, job, obsolete = [], routes) {
|
|
55
|
+
const { label, plistPath, content } = job;
|
|
57
56
|
const { runner, uid, paths } = ctx;
|
|
58
57
|
fs.mkdirSync(paths.launchAgentsDir, { recursive: true });
|
|
59
58
|
fs.mkdirSync(paths.logsDir, { recursive: true });
|
|
60
59
|
|
|
61
60
|
const existing = readFileOrNull(plistPath);
|
|
61
|
+
const changed = existing !== content;
|
|
62
62
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
// 内容变化(或首次):先 lint tmp,过了再动现文件;tmp 生命周期与原子写共用
|
|
73
|
-
return withTempFile(plistPath, {}, (tmp) => {
|
|
74
|
-
fs.writeFileSync(tmp, content, { mode: 0o644 });
|
|
75
|
-
const lint = launchd.lintPlist(runner, tmp);
|
|
76
|
-
if (lint.code !== 0)
|
|
77
|
-
return { changed: false, lintError: lint.stderr.trim() || `plutil exit ${lint.code}` };
|
|
63
|
+
// plist 相同时也可能换了选路,加载失败要走同一条恢复路径
|
|
64
|
+
function deploy(tmp) {
|
|
65
|
+
if (changed) {
|
|
66
|
+
fs.writeFileSync(tmp, content, { mode: 0o644 });
|
|
67
|
+
fs.chmodSync(tmp, 0o644);
|
|
68
|
+
const lint = launchd.lintPlist(runner, tmp);
|
|
69
|
+
if (lint.code !== 0)
|
|
70
|
+
return { changed: false, lintError: lint.stderr.trim() || `plutil exit ${lint.code}` };
|
|
71
|
+
}
|
|
78
72
|
|
|
79
|
-
//
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
73
|
+
// 只改大小写时,新旧路径在默认 macOS 卷上是同一项;回滚要还原磁盘上的旧拼写
|
|
74
|
+
const diskNames = obsolete.length && existing !== null ? fs.readdirSync(paths.launchAgentsDir) : [];
|
|
75
|
+
const oldSpelling = !diskNames.includes(path.basename(plistPath)) && obsolete.find((old) =>
|
|
76
|
+
old.toLowerCase() === label.toLowerCase() && diskNames.includes(path.basename(paths.plistFile(old))));
|
|
77
|
+
const existingPath = oldSpelling ? paths.plistFile(oldSpelling) : plistPath;
|
|
78
|
+
const stopped = [];
|
|
79
|
+
let replaced = false;
|
|
80
|
+
let candidate;
|
|
81
|
+
let targetWasDisabled = false;
|
|
82
|
+
try {
|
|
83
|
+
for (const old of changed ? [...obsolete, label] : obsolete) {
|
|
84
|
+
const st = launchd.inspect(runner, uid, old);
|
|
85
|
+
if (!st.registered) {
|
|
86
|
+
if (old === label) targetWasDisabled = launchd.disabledMap(runner, uid).get(label) === true;
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (bootoutJob(runner, uid, old)) stopped.push({ label: old, disabled: st.disabled });
|
|
90
|
+
}
|
|
91
|
+
routes?.apply();
|
|
92
|
+
if (changed) {
|
|
93
|
+
candidate = fs.statSync(tmp);
|
|
94
|
+
fs.renameSync(tmp, plistPath);
|
|
95
|
+
replaced = true;
|
|
96
|
+
launchd.enableBootstrap(runner, uid, label, plistPath);
|
|
97
|
+
} else {
|
|
98
|
+
ensureLoaded(ctx, job);
|
|
99
|
+
}
|
|
100
|
+
} catch (err) {
|
|
101
|
+
// 另一条安装或报名可能已经替换文件或注册任务,失败的调用不能再撤销对方的部署
|
|
102
|
+
if (replaced) {
|
|
103
|
+
const disk = fs.lstatSync(plistPath, { throwIfNoEntry: false });
|
|
104
|
+
let registered;
|
|
105
|
+
try { registered = launchd.printState(runner, uid, label).registered; } catch (query) {
|
|
106
|
+
throw new AggregateError([err, query], `${err.message};回滚前查询任务失败:${query.message}`);
|
|
107
|
+
}
|
|
108
|
+
if (!disk || disk.ino !== candidate.ino || disk.dev !== candidate.dev || registered)
|
|
109
|
+
throw new Error(`${err.message};部署期间任务状态已改变,请用 ssh-net status 核对`, { cause: err });
|
|
110
|
+
}
|
|
111
|
+
const failures = [];
|
|
112
|
+
try { routes?.restore(); } catch (restore) { failures.push(restore); }
|
|
113
|
+
// 在册任务的 plist 可能曾被手删;这时刚校验过的新文件是唯一可用于恢复的定义
|
|
114
|
+
let fileReady = true;
|
|
115
|
+
try {
|
|
116
|
+
if (replaced && existing !== null) writeFileAtomic(existingPath, existing);
|
|
117
|
+
else if (replaced && !stopped.some(old => old.label === label)) fs.rmSync(plistPath, { force: true });
|
|
118
|
+
} catch (restore) { fileReady = false; failures.push(restore); }
|
|
119
|
+
for (const old of stopped) {
|
|
120
|
+
if (!fileReady && (old.label === label || old.label === oldSpelling)) continue;
|
|
121
|
+
try {
|
|
122
|
+
launchd.enableBootstrap(runner, uid, old.label, paths.plistFile(old.label));
|
|
123
|
+
} catch (restore) { failures.push(restore); }
|
|
124
|
+
}
|
|
125
|
+
const disabled = stopped.filter(old => old.disabled).map(old => old.label);
|
|
126
|
+
if (replaced && targetWasDisabled) disabled.push(label);
|
|
127
|
+
for (const name of disabled) {
|
|
128
|
+
const r = launchd.disable(runner, uid, name);
|
|
129
|
+
if (r.code !== 0) failures.push(new Error(`disable 失败(${name}):${r.stderr.trim()}`));
|
|
130
|
+
}
|
|
131
|
+
if (failures.length)
|
|
132
|
+
throw new AggregateError([err, ...failures], `${err.message};恢复旧任务失败:${failures.map(e => e.message).join(';')}`);
|
|
133
|
+
throw err;
|
|
85
134
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
//(停机态)——真失败(override 数据库损坏)下 bootstrap 也过不去(error 5),
|
|
91
|
-
// 可见性优先于「也许 bootstrap 能侥幸成功」
|
|
92
|
-
launchd.enableBootstrap(runner, uid, label, plistPath);
|
|
93
|
-
return { changed: true, restarted: st.registered };
|
|
94
|
-
});
|
|
135
|
+
removeObsoleteNames(paths.launchAgentsDir, obsolete.map((old) => path.basename(paths.plistFile(old))), [path.basename(plistPath)]);
|
|
136
|
+
return { changed, restarted: stopped.some((old) => old.label === label) };
|
|
137
|
+
}
|
|
138
|
+
return changed ? withTempFile(plistPath, {}, deploy) : deploy();
|
|
95
139
|
}
|
|
96
140
|
|
|
97
141
|
/**
|
|
98
|
-
*
|
|
142
|
+
* 逐个卸载磁盘任务,在册的先 bootout,再删 plist。查询失败时保留文件
|
|
99
143
|
* @param {object} ctx
|
|
100
|
-
* @param {string}
|
|
101
|
-
* @param {string} plistPath
|
|
102
|
-
* @returns {boolean} 是否在册任务或磁盘 plist 确有清理
|
|
144
|
+
* @param {Iterable<string>} labels
|
|
103
145
|
*/
|
|
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()}`);
|
|
146
|
+
export function removeJobs({ runner, uid, paths }, labels) {
|
|
147
|
+
for (const label of labels) {
|
|
148
|
+
const st = launchd.printState(runner, uid, label);
|
|
149
|
+
if (st.registered) bootoutJob(runner, uid, label);
|
|
150
|
+
fs.rmSync(paths.plistFile(label), { force: true });
|
|
115
151
|
}
|
|
116
|
-
|
|
117
|
-
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function bootoutJob(runner, uid, label) {
|
|
155
|
+
const bo = launchd.bootout(runner, uid, label);
|
|
156
|
+
if (bo.code !== 0 && bo.code !== 113)
|
|
157
|
+
throw new Error(`bootout 失败(${label}):${bo.stderr.trim()}`);
|
|
158
|
+
return bo.code === 0;
|
|
118
159
|
}
|
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'),
|