ssh-net 26.10.34 → 26.10.35
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 +21 -0
- package/libexec/cli.mjs +4 -13
- package/libexec/commands/doctor.mjs +6 -4
- package/libexec/commands/install.mjs +48 -80
- package/libexec/commands/register.mjs +24 -51
- package/libexec/commands/start.mjs +12 -23
- package/libexec/commands/status.mjs +76 -87
- package/libexec/commands/stop.mjs +3 -7
- package/libexec/commands/uninstall.mjs +3 -5
- package/libexec/lib/cliargs.mjs +12 -0
- package/libexec/lib/conf.mjs +3 -11
- package/libexec/lib/config.mjs +2 -3
- package/libexec/lib/fsutil.mjs +34 -0
- package/libexec/lib/ipv6.mjs +17 -20
- package/libexec/lib/jobctl.mjs +8 -7
- package/libexec/lib/launchd.mjs +60 -6
- package/libexec/lib/manage.mjs +51 -34
- package/libexec/lib/manifest.mjs +41 -0
- package/libexec/lib/probe.mjs +4 -2
- package/libexec/lib/registration.mjs +47 -0
- package/libexec/lib/render.mjs +79 -24
- package/libexec/lib/runtime.mjs +9 -26
- package/libexec/lib/state.mjs +12 -20
- package/libexec/lib/systemd.mjs +48 -0
- package/libexec/sn-route +26 -15
- package/libexec/ssh-net-uplink-edge +12 -8
- package/package.json +1 -1
package/libexec/lib/manage.mjs
CHANGED
|
@@ -3,13 +3,47 @@
|
|
|
3
3
|
// 渲染结果与现文件字节相同则不 bootout——install 幂等不断连。
|
|
4
4
|
import fs from 'node:fs';
|
|
5
5
|
import * as launchd from './launchd.mjs';
|
|
6
|
+
import { readFileOrNull } from './fsutil.mjs';
|
|
6
7
|
|
|
7
|
-
/**
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
/**
|
|
9
|
+
* 确保一个在册 job 已加载可用(launchctl 四态状态机的唯一实现,实测矩阵见
|
|
10
|
+
* launchd.mjs 头注;syncPlist 的「文件没变」分支与 `start core` 共用)。
|
|
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
|
+
* @returns {'bootstrapped'|'kickstarted'|'enabled'|'unchanged'}
|
|
19
|
+
*/
|
|
20
|
+
export function ensureLoaded(ctx, { label, plistPath }, {
|
|
21
|
+
kickWhenStopped = false,
|
|
22
|
+
reviveRunningDisabled = false,
|
|
23
|
+
} = {}) {
|
|
24
|
+
const { runner, uid } = ctx;
|
|
25
|
+
const st = launchd.inspect(runner, uid, label);
|
|
26
|
+
if (!st.registered) {
|
|
27
|
+
launchd.enableBootstrap(runner, uid, label, plistPath);
|
|
28
|
+
return 'bootstrapped';
|
|
29
|
+
}
|
|
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 'kickstarted';
|
|
34
|
+
}
|
|
35
|
+
if (st.disabled) {
|
|
36
|
+
// 在跑但 override 残留:只 enable 清掉,防退出后不重拉,不打断现连接
|
|
37
|
+
const en = launchd.enable(runner, uid, label);
|
|
38
|
+
if (en.code !== 0) throw new Error(`enable 失败(${label}):${en.stderr.trim()}`);
|
|
39
|
+
return 'enabled';
|
|
40
|
+
}
|
|
41
|
+
if (kickWhenStopped && !st.running) {
|
|
42
|
+
const k = launchd.kickstart(runner, uid, label);
|
|
43
|
+
if (k.code !== 0) throw new Error(`kickstart 失败(${label}):${k.stderr.trim()}`);
|
|
44
|
+
return 'kickstarted';
|
|
45
|
+
}
|
|
46
|
+
return 'unchanged';
|
|
13
47
|
}
|
|
14
48
|
|
|
15
49
|
/**
|
|
@@ -24,29 +58,16 @@ export function syncPlist(ctx, { label, plistPath, content }) {
|
|
|
24
58
|
fs.mkdirSync(paths.launchAgentsDir, { recursive: true });
|
|
25
59
|
fs.mkdirSync(paths.logsDir, { recursive: true });
|
|
26
60
|
|
|
27
|
-
|
|
28
|
-
try {
|
|
29
|
-
existing = fs.readFileSync(plistPath, 'utf8');
|
|
30
|
-
} catch {
|
|
31
|
-
existing = null;
|
|
32
|
-
}
|
|
61
|
+
const existing = readFileOrNull(plistPath);
|
|
33
62
|
|
|
34
63
|
if (existing === content) {
|
|
35
|
-
const st = launchd.inspect(runner, uid, label);
|
|
36
64
|
// registered+disabled(stop 中途失败/手工 disable)不是「已部署」:
|
|
37
65
|
// enable 后拉起,否则 install 报 unchanged 却没在跑、start 的 kickstart 被拒
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
if (
|
|
43
|
-
if (!st.registered) {
|
|
44
|
-
const b = launchd.bootstrap(runner, uid, plistPath);
|
|
45
|
-
if (b.code !== 0) throw new Error(`bootstrap 失败(${label}):${b.stderr.trim()}`);
|
|
46
|
-
return { registered: true };
|
|
47
|
-
}
|
|
48
|
-
const k = launchd.kickstart(runner, uid, label);
|
|
49
|
-
if (k.code !== 0) throw new Error(`kickstart 失败(${label}):${k.stderr.trim()}`);
|
|
66
|
+
const action = ensureLoaded(ctx, { label, plistPath }, {
|
|
67
|
+
reviveRunningDisabled: true,
|
|
68
|
+
});
|
|
69
|
+
if (action === 'unchanged') return { unchanged: true };
|
|
70
|
+
if (action === 'bootstrapped') return { registered: true };
|
|
50
71
|
return { revived: true };
|
|
51
72
|
}
|
|
52
73
|
|
|
@@ -67,15 +88,11 @@ export function syncPlist(ctx, { label, plistPath, content }) {
|
|
|
67
88
|
}
|
|
68
89
|
fs.renameSync(tmp, plistPath);
|
|
69
90
|
fs.chmodSync(plistPath, 0o644);
|
|
70
|
-
// enable
|
|
71
|
-
//
|
|
72
|
-
|
|
73
|
-
//
|
|
74
|
-
|
|
75
|
-
const en = launchd.enable(runner, uid, label); // 无条件先 enable(override 残留时 bootstrap error 5)
|
|
76
|
-
if (en.code !== 0) throw new Error(`enable 失败(${label}):${en.stderr.trim()}`);
|
|
77
|
-
const b = launchd.bootstrap(runner, uid, plistPath);
|
|
78
|
-
if (b.code !== 0) throw new Error(`bootstrap 失败(${label}):${b.stderr.trim()}`);
|
|
91
|
+
// 无条件先 enable(override 残留时 bootstrap error 5;launchd.mjs 宣言
|
|
92
|
+
// 「勿在此吞错」)。取舍:此处 job 已 bootout、enable 失败即不 bootstrap
|
|
93
|
+
//(停机态)——真失败(override 数据库损坏)下 bootstrap 也过不去(error 5),
|
|
94
|
+
// 可见性优先于「也许 bootstrap 能侥幸成功」
|
|
95
|
+
launchd.enableBootstrap(runner, uid, label, plistPath);
|
|
79
96
|
return { deployed: true };
|
|
80
97
|
}
|
|
81
98
|
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// 运行时实体/服务软链清单的单一来源(install 对账与 status 体检共用,
|
|
2
|
+
// design §2.3)。清单即契约:新增包脚本只改这里,漏装与假报「组件异常」
|
|
3
|
+
// 不会再分叉。
|
|
4
|
+
//
|
|
5
|
+
// entities:[bin 名, 包脚本名]——原名实体;links:[带机器名软链, 实体名]。
|
|
6
|
+
// core 安装时按磁盘在册 edge 追加 uplink-edge(见 installCore),单台 edge
|
|
7
|
+
// 报名时的一对固定对账直接写在 register.mjs——都只有一个消费方,不进这里。
|
|
8
|
+
import { linkLinkName, relayLinkName } from './render.mjs';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* edge 形态:基础三件套恒装;装了 link 附加件才加第 4 实体与带机器名软链。
|
|
12
|
+
* @param {import('./config.mjs').Topology} topo
|
|
13
|
+
* @param {boolean} linkInstalled
|
|
14
|
+
*/
|
|
15
|
+
export function edgeRuntimePlan(topo, linkInstalled) {
|
|
16
|
+
/** @type {[string,string][]} */
|
|
17
|
+
const entities = [
|
|
18
|
+
['sn-probe', 'sn-probe'],
|
|
19
|
+
['sn-route', 'sn-route'], // Match exec 按需选路 + 成功后本地通知/秒切(design §2.2)
|
|
20
|
+
['sn-ipv6', 'sn-ipv6'],
|
|
21
|
+
];
|
|
22
|
+
/** @type {[string,string][]} */
|
|
23
|
+
const links = [];
|
|
24
|
+
if (linkInstalled) {
|
|
25
|
+
entities.push(['ssh-net-link-core', 'ssh-net-link-core']);
|
|
26
|
+
links.push([linkLinkName(topo.core.alias), 'ssh-net-link-core']);
|
|
27
|
+
}
|
|
28
|
+
return { entities, links };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* relay 一件套:core/node 常驻服务的基础形态(installCore 在其上追加在册
|
|
33
|
+
* edge 的回拨组件)。
|
|
34
|
+
* @param {import('./config.mjs').Topology} topo
|
|
35
|
+
*/
|
|
36
|
+
export function relayRuntimePlan(topo) {
|
|
37
|
+
return {
|
|
38
|
+
entities: [['ssh-net-uplink-relay', 'ssh-net-uplink-relay']],
|
|
39
|
+
links: [[relayLinkName(topo.relay.alias), 'ssh-net-uplink-relay']],
|
|
40
|
+
};
|
|
41
|
+
}
|
package/libexec/lib/probe.mjs
CHANGED
|
@@ -110,13 +110,15 @@ export function descendantsOf(tree, root) {
|
|
|
110
110
|
* @param {import('./exec.mjs').Runner} runner
|
|
111
111
|
* @param {number} port
|
|
112
112
|
* @param {number|null} jobPid link job 的 pid(未注册给 null)
|
|
113
|
+
* @param {Map<number,number>} [tree] 预检全程进程树不变:多端口预检时调用方
|
|
114
|
+
* 建一次传入复用,缺省本函数自取
|
|
113
115
|
* @returns {{kind:'free'|'ours'|'foreign', pids:number[]}}
|
|
114
116
|
*/
|
|
115
|
-
export function classifyLocalPort(runner, port, jobPid) {
|
|
117
|
+
export function classifyLocalPort(runner, port, jobPid, tree) {
|
|
116
118
|
const pids = listListeners(runner, port);
|
|
117
119
|
if (pids.length === 0) return { kind: 'free', pids: [] };
|
|
118
120
|
if (jobPid === null) return { kind: 'foreign', pids };
|
|
119
|
-
const ok = new Set([jobPid, ...descendantsOf(processTree(runner), jobPid)]);
|
|
121
|
+
const ok = new Set([jobPid, ...descendantsOf(tree ?? processTree(runner), jobPid)]);
|
|
120
122
|
return pids.every((p) => ok.has(p))
|
|
121
123
|
? { kind: 'ours', pids }
|
|
122
124
|
: { kind: 'foreign', pids };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// core 侧 edge 登记的不变式(install reconcile、status 体检、报名 _register
|
|
2
|
+
// 三处共同遵守):
|
|
3
|
+
// conf.d/edge-<别名> 存在 ⟺ 回拨 job 在册且未休眠 ⋀ state/edges/<别名> 有 v6
|
|
4
|
+
// 片段内容恒为 edge 行 + 该 v6 的渲染。state 与片段永远两处同写/同删——
|
|
5
|
+
// 没有 job 兜底的片段是指向陈旧 v6 的假入口。
|
|
6
|
+
import { syncConfFile, removeConfFile } from './conf.mjs';
|
|
7
|
+
import { renderEdgeHost } from './render.mjs';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* edge 在线入口片段的期望内容。
|
|
11
|
+
* @param {{alias:string, aliases:string[], user:string}} edge topology edge 行
|
|
12
|
+
* @param {string} v6
|
|
13
|
+
* @returns {string}
|
|
14
|
+
*/
|
|
15
|
+
export function edgeFragmentContent(edge, v6) {
|
|
16
|
+
return renderEdgeHost({
|
|
17
|
+
alias: edge.alias, aliases: edge.aliases, user: edge.user, v6,
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 片段此刻是否应当存在:job 在册、未休眠(enabled)、state 有 v6 三者齐备。
|
|
23
|
+
* @param {{registered:boolean, disabled:boolean}} jobState launchd.inspect 的结果
|
|
24
|
+
* @param {string|null} v6
|
|
25
|
+
* @returns {boolean}
|
|
26
|
+
*/
|
|
27
|
+
export function edgeFragmentShouldExist(jobState, v6) {
|
|
28
|
+
return jobState.registered && !jobState.disabled && v6 !== null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 两处同写(唯一写者 _register;install reconcile 用 syncConfFile 只补片段):
|
|
33
|
+
* state/edges/<别名> 给 uplink-edge 脚本机读,conf.d/edge-<别名> 给人当入口。
|
|
34
|
+
* @param {object} ctx
|
|
35
|
+
* @param {{alias:string, aliases:string[], user:string}} edge
|
|
36
|
+
* @param {string} v6
|
|
37
|
+
*/
|
|
38
|
+
export function writeEdgeRegistration(ctx, edge, v6) {
|
|
39
|
+
ctx.state.writeEdgeV6(edge.alias, v6);
|
|
40
|
+
syncConfFile(ctx.paths.edgeHostFile(edge.alias), edgeFragmentContent(edge, v6));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** 两处同删(未报名过的失败路径、休眠收尸)。 */
|
|
44
|
+
export function removeEdgeRegistration(ctx, alias) {
|
|
45
|
+
ctx.state.removeEdgeV6(alias);
|
|
46
|
+
removeConfFile(ctx.paths.edgeHostFile(alias));
|
|
47
|
+
}
|
package/libexec/lib/render.mjs
CHANGED
|
@@ -75,6 +75,22 @@ const ROUTES_HEADER = `# 本文件由 \`ssh-net install\` 从 ~/.ssh/ssh-net/top
|
|
|
75
75
|
# relay 兜底。User 不写——由主 config 的 Host 块补。
|
|
76
76
|
`;
|
|
77
77
|
|
|
78
|
+
/**
|
|
79
|
+
* sn-route 的冻结位置参数(design §2.3 argv 契约):
|
|
80
|
+
* <self> <core> <relay> <core 注册口> <core 用户> <show|quiet>
|
|
81
|
+
* routes 渲染与 doctor 喂参的唯一生产点,两边改一处即可。
|
|
82
|
+
* @param {import('./config.mjs').Topology} topo
|
|
83
|
+
* @param {string} selfAlias 本机 edge 规范别名(sn-route 报名 RPC 的自称)
|
|
84
|
+
* @param {'show'|'quiet'} ui
|
|
85
|
+
* @returns {string[]}
|
|
86
|
+
*/
|
|
87
|
+
export function snRouteArgv(topo, selfAlias, ui) {
|
|
88
|
+
return [
|
|
89
|
+
selfAlias, topo.core.alias, topo.relay.alias,
|
|
90
|
+
String(topo.core.relayPort), topo.core.user, ui,
|
|
91
|
+
];
|
|
92
|
+
}
|
|
93
|
+
|
|
78
94
|
/**
|
|
79
95
|
* edge 选路片段 conf.d/routes 的唯一渲染来源。值全部来自 topology + 冻结常量,
|
|
80
96
|
* 不出现任何真实 IP(环回口 + relay 别名)。
|
|
@@ -89,11 +105,25 @@ export function renderRoutes(topo, selfAlias) {
|
|
|
89
105
|
const blocks = [];
|
|
90
106
|
|
|
91
107
|
// sn-route argv 直接渲染进 Match(无配置文件中转);peer 块 show(终端可见
|
|
92
|
-
// 进度)、relay 块 quiet
|
|
93
|
-
|
|
94
|
-
|
|
108
|
+
// 进度)、relay 块 quiet(第二次调用零输出)。端口加引号是 shell 渲染层,
|
|
109
|
+
// argv 契约本身在 snRouteArgv
|
|
110
|
+
const routeArgs = (ui) => {
|
|
111
|
+
const argv = snRouteArgv(topo, selfAlias, ui);
|
|
112
|
+
argv[3] = `'${argv[3]}'`;
|
|
113
|
+
return `${snRoute} ${argv.join(' ')}`;
|
|
114
|
+
};
|
|
95
115
|
const match = (alias, cond) =>
|
|
96
116
|
`Match originalhost ${alias} exec "test -e ${state}/RELAY_ON${cond}"`;
|
|
117
|
+
// 经 relay 的兜底 Host 块(core 失败兜底与 node 常态同构):环回口 + 注册口
|
|
118
|
+
// + ProxyJump,差异只在注释与 Match 条件
|
|
119
|
+
const relayBlock = (alias, port, comment, cond) => [
|
|
120
|
+
`# ${alias}:${comment}`,
|
|
121
|
+
match(alias, cond),
|
|
122
|
+
' HostName 127.0.0.1',
|
|
123
|
+
` Port ${port}`,
|
|
124
|
+
` ProxyJump ${topo.relay.alias}`,
|
|
125
|
+
` HostKeyAlias ${alias}`,
|
|
126
|
+
].join('\n');
|
|
97
127
|
|
|
98
128
|
// core:peer 一跳(sn-route 按需确保成功:回拨落在本机 [::1]:10022)
|
|
99
129
|
blocks.push([
|
|
@@ -104,26 +134,15 @@ export function renderRoutes(topo, selfAlias) {
|
|
|
104
134
|
` HostKeyAlias ${topo.core.alias}`,
|
|
105
135
|
].join('\n'));
|
|
106
136
|
// core:relay 兜底(与上块互斥:sn-route 失败本块才成立)
|
|
107
|
-
blocks.push(
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
` ProxyJump ${topo.relay.alias}`,
|
|
113
|
-
` HostKeyAlias ${topo.core.alias}`,
|
|
114
|
-
].join('\n'));
|
|
137
|
+
blocks.push(relayBlock(
|
|
138
|
+
topo.core.alias, topo.core.relayPort,
|
|
139
|
+
'relay 兜底(无 v6/确保失败/预算超时)',
|
|
140
|
+
` && ! ${routeArgs('quiet')}`,
|
|
141
|
+
));
|
|
115
142
|
|
|
116
143
|
// node:只有 relay(无公网入站)
|
|
117
|
-
for (const n of topo.nodes)
|
|
118
|
-
blocks.push(
|
|
119
|
-
`# ${n.alias}:relay(node 无公网入站)`,
|
|
120
|
-
match(n.alias, ''),
|
|
121
|
-
' HostName 127.0.0.1',
|
|
122
|
-
` Port ${n.relayPort}`,
|
|
123
|
-
` ProxyJump ${topo.relay.alias}`,
|
|
124
|
-
` HostKeyAlias ${n.alias}`,
|
|
125
|
-
].join('\n'));
|
|
126
|
-
}
|
|
144
|
+
for (const n of topo.nodes)
|
|
145
|
+
blocks.push(relayBlock(n.alias, n.relayPort, 'relay(node 无公网入站)', ''));
|
|
127
146
|
|
|
128
147
|
// VS Code 打标块(与目标无关,对所有 VS Code 发起的连接生效):扩展宿主
|
|
129
148
|
// 进程有 VSCODE_IPC_HOOK;集成终端里手动 ssh 只有 VSCODE_IPC_HOOK_CLI、
|
|
@@ -143,20 +162,56 @@ export function renderRoutes(topo, selfAlias) {
|
|
|
143
162
|
* core 上的 edge 在线入口片段 conf.d/edge-<规范别名>。Host 模式给全部别名:
|
|
144
163
|
* 规范名(ssh-net 内部用,如 edge1)在前,给人用的短名(如 e1)在后,
|
|
145
164
|
* `ssh edge1` 与 `ssh e1` 等价;文件/state/job 命名只用规范名。
|
|
146
|
-
* @param {{alias:string, aliases
|
|
165
|
+
* @param {{alias:string, aliases:string[], user:string, v6:string}} opts
|
|
147
166
|
* @returns {string}
|
|
148
167
|
*/
|
|
149
168
|
export function renderEdgeHost({ alias, aliases, user, v6 }) {
|
|
150
|
-
const patterns = aliases && aliases.length > 0 ? aliases.join(' ') : alias;
|
|
151
169
|
return `# 本文件由 ssh-net 维护:core 收到 ${alias} 的报名时写入,回拨 job 休眠
|
|
152
170
|
# (edge 关机/不可达,六连败)或 uninstall 时删除。存在即 edge 当前在线、
|
|
153
171
|
# 其上报 v6 可拨;勿手改(改了下次报名也会被覆盖)。
|
|
154
|
-
Host ${
|
|
172
|
+
Host ${aliases.join(' ')}
|
|
155
173
|
User ${user}
|
|
156
174
|
HostName ${v6}
|
|
157
175
|
`;
|
|
158
176
|
}
|
|
159
177
|
|
|
178
|
+
// ---- 经 relay 拨 core 环回口的 ssh argv(报名 RPC 与 status 探测共用)----
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* 命令行显式 -J 经 relay 拨 core 注册口的公共 argv(design §4.3):字面目标
|
|
182
|
+
* 127.0.0.1 不命中 Match 选路、跳板地址/用户仍读 ssh config 的 Host 块、
|
|
183
|
+
* HostKeyAlias 保证多路径同一条 known_hosts。两个调用方只在分歧项上显式传参:
|
|
184
|
+
* 报名无人值守首连要 accept-new(探测侧不接受新 host key)、各自超时与远端命令。
|
|
185
|
+
* @param {import('./config.mjs').Topology} topo
|
|
186
|
+
* @param {object} [opts]
|
|
187
|
+
* @param {number} [opts.connectTimeout=5]
|
|
188
|
+
* @param {boolean} [opts.acceptHost=false] 加 StrictHostKeyChecking=accept-new
|
|
189
|
+
* @param {string} [opts.remoteCommand] 给了就追加为远端命令
|
|
190
|
+
* @param {readonly string[]} [opts.extraOpts] 追加的额外 -o 选项(成对追加)
|
|
191
|
+
* @returns {string[]}
|
|
192
|
+
*/
|
|
193
|
+
export function coreViaRelayArgs(topo, {
|
|
194
|
+
connectTimeout = 5,
|
|
195
|
+
acceptHost = false,
|
|
196
|
+
remoteCommand,
|
|
197
|
+
extraOpts = [],
|
|
198
|
+
} = {}) {
|
|
199
|
+
const args = [
|
|
200
|
+
'-J', topo.relay.alias,
|
|
201
|
+
'-p', String(topo.core.relayPort),
|
|
202
|
+
'-o', `HostKeyAlias=${topo.core.alias}`,
|
|
203
|
+
'-o', 'BatchMode=yes',
|
|
204
|
+
'-o', `ConnectTimeout=${connectTimeout}`,
|
|
205
|
+
'-o', 'ControlMaster=no', '-o', 'ControlPath=none',
|
|
206
|
+
'-o', 'IdentitiesOnly=yes',
|
|
207
|
+
];
|
|
208
|
+
if (acceptHost) args.push('-o', 'StrictHostKeyChecking=accept-new');
|
|
209
|
+
args.push(...extraOpts);
|
|
210
|
+
args.push(`${topo.core.user}@127.0.0.1`);
|
|
211
|
+
if (remoteCommand !== undefined) args.push(remoteCommand);
|
|
212
|
+
return args;
|
|
213
|
+
}
|
|
214
|
+
|
|
160
215
|
// ---- 三种 LaunchAgent plist ----
|
|
161
216
|
|
|
162
217
|
// link job 的 launchd 重连间隔(2026-10-02):纯 ssh 形态下 ssh 退出即 job
|
package/libexec/lib/runtime.mjs
CHANGED
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
import fs from 'node:fs';
|
|
11
11
|
import path from 'node:path';
|
|
12
12
|
import { packageScriptPath } from './paths.mjs';
|
|
13
|
+
import { readFileOrNull, writeFileAtomic } from './fsutil.mjs';
|
|
13
14
|
import pkg from '../../package.json' with { type: 'json' };
|
|
14
15
|
|
|
15
16
|
export const RUNTIME_VERSION = pkg.version;
|
|
@@ -22,12 +23,7 @@ export const RUNTIME_VERSION = pkg.version;
|
|
|
22
23
|
*/
|
|
23
24
|
export function ensureScript(dstPath, srcPath) {
|
|
24
25
|
const src = fs.readFileSync(srcPath);
|
|
25
|
-
|
|
26
|
-
try {
|
|
27
|
-
cur = fs.readFileSync(dstPath);
|
|
28
|
-
} catch (err) {
|
|
29
|
-
if (err.code !== 'ENOENT') throw err;
|
|
30
|
-
}
|
|
26
|
+
const cur = readFileOrNull(dstPath, null);
|
|
31
27
|
if (cur !== null && cur.equals(src)) {
|
|
32
28
|
// 内容对但可执行位丢了(cp/同步工具吃掉 mode):补上
|
|
33
29
|
if (fs.statSync(dstPath).mode & 0o111) return 'unchanged';
|
|
@@ -35,10 +31,7 @@ export function ensureScript(dstPath, srcPath) {
|
|
|
35
31
|
return 'repaired';
|
|
36
32
|
}
|
|
37
33
|
// 缺失或旧内容:tmp+rename 原子换(rename 直接替换目录项)
|
|
38
|
-
|
|
39
|
-
fs.writeFileSync(tmp, src, { mode: 0o755 });
|
|
40
|
-
fs.renameSync(tmp, dstPath);
|
|
41
|
-
fs.chmodSync(dstPath, 0o755);
|
|
34
|
+
writeFileAtomic(dstPath, src, { mode: 0o755 });
|
|
42
35
|
return cur !== null ? 'updated' : 'created';
|
|
43
36
|
}
|
|
44
37
|
|
|
@@ -100,17 +93,9 @@ export function ensureRuntime(ctx, entries, links = []) {
|
|
|
100
93
|
|
|
101
94
|
/** 刷新版本戳(ensureRuntime 尾部) */
|
|
102
95
|
function refreshStamp(ctx) {
|
|
103
|
-
const stamp = `${RUNTIME_VERSION}\n`;
|
|
104
96
|
const stampPath = ctx.paths.runtimeVersionFile;
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
cur = fs.readFileSync(stampPath, 'utf8');
|
|
108
|
-
} catch { cur = null; }
|
|
109
|
-
if (cur !== stamp) {
|
|
110
|
-
const tmp = `${stampPath}.tmp-${process.pid}`;
|
|
111
|
-
fs.writeFileSync(tmp, stamp, { mode: 0o644 });
|
|
112
|
-
fs.renameSync(tmp, stampPath);
|
|
113
|
-
}
|
|
97
|
+
if (readFileOrNull(stampPath) !== `${RUNTIME_VERSION}\n`)
|
|
98
|
+
writeFileAtomic(stampPath, `${RUNTIME_VERSION}\n`);
|
|
114
99
|
}
|
|
115
100
|
|
|
116
101
|
/**
|
|
@@ -162,12 +147,10 @@ export function auditLink(linkPath, entityName) {
|
|
|
162
147
|
* @returns {string | null}
|
|
163
148
|
*/
|
|
164
149
|
export function readRuntimeVersion(paths) {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
return null;
|
|
170
|
-
}
|
|
150
|
+
const v = readFileOrNull(paths.runtimeVersionFile);
|
|
151
|
+
if (v === null) return null;
|
|
152
|
+
const trimmed = v.trim();
|
|
153
|
+
return trimmed.length > 0 ? trimmed : null;
|
|
171
154
|
}
|
|
172
155
|
|
|
173
156
|
/**
|
package/libexec/lib/state.mjs
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
// 目录 0700、文件 0600,写 v6 走 tmp+rename 防报名写入与 uplink-edge 读到半截。
|
|
7
7
|
import fs from 'node:fs';
|
|
8
8
|
import path from 'node:path';
|
|
9
|
+
import { readFileOrNull, writeFileAtomic } from './fsutil.mjs';
|
|
9
10
|
|
|
10
11
|
const SWITCH_NAMES = ['relay', 'peer'];
|
|
11
12
|
|
|
@@ -28,8 +29,6 @@ const SWITCH_NAMES = ['relay', 'peer'];
|
|
|
28
29
|
* @returns {StateStore}
|
|
29
30
|
*/
|
|
30
31
|
export function createStateStore({ stateDir, switchFiles, edgesDir }) {
|
|
31
|
-
const files = switchFiles;
|
|
32
|
-
|
|
33
32
|
const ensureState = () => {
|
|
34
33
|
fs.mkdirSync(stateDir, { recursive: true, mode: 0o700 });
|
|
35
34
|
};
|
|
@@ -42,7 +41,7 @@ export function createStateStore({ stateDir, switchFiles, edgesDir }) {
|
|
|
42
41
|
return {
|
|
43
42
|
isOn(name) {
|
|
44
43
|
assertNames([name]);
|
|
45
|
-
return fs.existsSync(
|
|
44
|
+
return fs.existsSync(switchFiles[name]);
|
|
46
45
|
},
|
|
47
46
|
|
|
48
47
|
setOn(names) {
|
|
@@ -50,9 +49,9 @@ export function createStateStore({ stateDir, switchFiles, edgesDir }) {
|
|
|
50
49
|
ensureState();
|
|
51
50
|
for (const n of names) {
|
|
52
51
|
// 'a':不截断、已存在不报错(touch 语义)
|
|
53
|
-
const fd = fs.openSync(
|
|
52
|
+
const fd = fs.openSync(switchFiles[n], 'a');
|
|
54
53
|
fs.closeSync(fd);
|
|
55
|
-
fs.chmodSync(
|
|
54
|
+
fs.chmodSync(switchFiles[n], 0o600);
|
|
56
55
|
}
|
|
57
56
|
},
|
|
58
57
|
|
|
@@ -60,7 +59,7 @@ export function createStateStore({ stateDir, switchFiles, edgesDir }) {
|
|
|
60
59
|
assertNames(names);
|
|
61
60
|
for (const n of names) {
|
|
62
61
|
try {
|
|
63
|
-
fs.unlinkSync(
|
|
62
|
+
fs.unlinkSync(switchFiles[n]);
|
|
64
63
|
} catch (err) {
|
|
65
64
|
if (err && err.code === 'ENOENT') continue;
|
|
66
65
|
throw err;
|
|
@@ -70,29 +69,22 @@ export function createStateStore({ stateDir, switchFiles, edgesDir }) {
|
|
|
70
69
|
|
|
71
70
|
readSwitches() {
|
|
72
71
|
return {
|
|
73
|
-
relay: fs.existsSync(
|
|
74
|
-
peer: fs.existsSync(
|
|
72
|
+
relay: fs.existsSync(switchFiles.relay),
|
|
73
|
+
peer: fs.existsSync(switchFiles.peer),
|
|
75
74
|
};
|
|
76
75
|
},
|
|
77
76
|
|
|
78
77
|
readEdgeV6(alias) {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
if (err && err.code === 'ENOENT') return null;
|
|
84
|
-
throw err;
|
|
85
|
-
}
|
|
78
|
+
const v6 = readFileOrNull(path.join(edgesDir, alias));
|
|
79
|
+
if (v6 === null) return null;
|
|
80
|
+
const trimmed = v6.trim();
|
|
81
|
+
return trimmed.length > 0 ? trimmed : null;
|
|
86
82
|
},
|
|
87
83
|
|
|
88
84
|
writeEdgeV6(alias, v6) {
|
|
89
85
|
ensureState();
|
|
90
86
|
fs.mkdirSync(edgesDir, { recursive: true, mode: 0o700 });
|
|
91
|
-
|
|
92
|
-
const tmp = path.join(edgesDir, `.${alias}.tmp-${process.pid}`);
|
|
93
|
-
fs.writeFileSync(tmp, `${v6}\n`, { mode: 0o600 });
|
|
94
|
-
fs.renameSync(tmp, final);
|
|
95
|
-
fs.chmodSync(final, 0o600);
|
|
87
|
+
writeFileAtomic(path.join(edgesDir, alias), `${v6}\n`, { mode: 0o600 });
|
|
96
88
|
},
|
|
97
89
|
|
|
98
90
|
removeEdgeV6(alias) {
|
package/libexec/lib/systemd.mjs
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
// node 机 systemd 编排。node 登录为 root(migration 实测前提),直接
|
|
2
2
|
// systemctl、直接写 /etc/systemd/system,不包 sudo;非 root 调用由命令层拒绝。
|
|
3
3
|
// Restart=always/RestartSec/StartLimitIntervalSec 在渲染的 unit 里定死。
|
|
4
|
+
import fs from 'node:fs';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { readFileOrNull, writeFileAtomic } from './fsutil.mjs';
|
|
4
7
|
|
|
5
8
|
/** @param {import('./exec.mjs').Runner} runner @param {string} unit 含 .service */
|
|
6
9
|
const sc = (runner, args) => runner.run('systemctl', args);
|
|
@@ -28,3 +31,48 @@ export function verifyUnit(runner, unitPath) {
|
|
|
28
31
|
// verify 对只告警也可能非零;这里把非零视为失败,命令层打印 stderr
|
|
29
32
|
return { ok: r.code === 0, stdout: r.stdout, stderr: r.stderr };
|
|
30
33
|
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 停一个 unit:未加载(未装/已卸)的 not-loaded 失败放行,其余抛出。
|
|
37
|
+
* stop 与 uninstall 共用这条规则——真失败必须报,否则「已关闭」是谎报、
|
|
38
|
+
* 删了 unit 文件还会留下在跑的 ghost 进程。
|
|
39
|
+
* @param {import('./exec.mjs').Runner} runner
|
|
40
|
+
* @param {string} unit
|
|
41
|
+
*/
|
|
42
|
+
export function stopIfLoaded(runner, unit) {
|
|
43
|
+
const r = stopUnit(runner, unit);
|
|
44
|
+
if (r.code !== 0 && !/not loaded/.test(r.stderr))
|
|
45
|
+
throw new Error(`systemctl stop 失败:${r.stderr.trim()}`);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 对账一个 unit 到磁盘并确保服务在跑(与 mac 侧 syncPlist 同构、对齐 design §5
|
|
50
|
+
* 「install 幂等不断连」):字节相同只 enable+start,不重写不 restart;内容
|
|
51
|
+
* 变了才 verify → 原子落盘 → daemon-reload → enable → restart。
|
|
52
|
+
* @param {import('./exec.mjs').Runner} runner
|
|
53
|
+
* @param {{unitPath:string, unitName:string, content:string}} job
|
|
54
|
+
* @returns {'unchanged'|'deployed'}
|
|
55
|
+
*/
|
|
56
|
+
export function syncUnit(runner, { unitPath, unitName, content }) {
|
|
57
|
+
fs.mkdirSync(path.dirname(unitPath), { recursive: true });
|
|
58
|
+
|
|
59
|
+
if (readFileOrNull(unitPath) === content) {
|
|
60
|
+
for (const r of [enableUnit(runner, unitName), startUnit(runner, unitName)]) {
|
|
61
|
+
if (r.code !== 0) throw new Error(`systemctl 失败:${r.stderr.trim()}`);
|
|
62
|
+
}
|
|
63
|
+
return 'unchanged';
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
writeFileAtomic(unitPath, content, { mode: 0o644 });
|
|
67
|
+
const verify = verifyUnit(runner, unitPath);
|
|
68
|
+
if (!verify.ok && !verify.skipped)
|
|
69
|
+
throw new Error(`systemd-analyze verify 失败:\n${verify.stderr}`);
|
|
70
|
+
for (const r of [
|
|
71
|
+
daemonReload(runner),
|
|
72
|
+
enableUnit(runner, unitName),
|
|
73
|
+
restartUnit(runner, unitName),
|
|
74
|
+
]) {
|
|
75
|
+
if (r.code !== 0) throw new Error(`systemctl 失败:${r.stderr.trim()}`);
|
|
76
|
+
}
|
|
77
|
+
return 'deployed';
|
|
78
|
+
}
|