ssh-net 26.10.34 → 26.10.36
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 +42 -0
- package/libexec/cli.mjs +5 -14
- package/libexec/commands/doctor.mjs +7 -5
- package/libexec/commands/install.mjs +58 -95
- package/libexec/commands/register.mjs +28 -55
- package/libexec/commands/start.mjs +12 -24
- package/libexec/commands/status.mjs +97 -106
- package/libexec/commands/stop.mjs +8 -10
- package/libexec/commands/uninstall.mjs +16 -18
- 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 +13 -14
- package/libexec/lib/launchd.mjs +66 -12
- package/libexec/lib/manage.mjs +51 -34
- package/libexec/lib/manifest.mjs +76 -0
- package/libexec/lib/paths.mjs +2 -0
- package/libexec/lib/probe.mjs +27 -10
- package/libexec/lib/registration.mjs +47 -0
- package/libexec/lib/render.mjs +79 -24
- package/libexec/lib/runtime.mjs +24 -35
- package/libexec/lib/state.mjs +12 -20
- package/libexec/lib/systemd.mjs +52 -5
- package/libexec/sn-route +36 -19
- package/libexec/ssh-net-uplink-edge +12 -8
- package/package.json +1 -1
package/libexec/lib/jobctl.mjs
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
// edge
|
|
2
|
-
//
|
|
3
|
-
//
|
|
1
|
+
// edge link job 的进程收束(design §5):纯机制——在册才 launchctl kill TERM
|
|
2
|
+
// 并轮询校验纯 ssh job 真退出(信号直达 ssh),不碰开关;删哪些开关是 stop
|
|
3
|
+
// 的命令语义,由命令层显式做。未装 link(label 不在册)直接 gone。预算内
|
|
4
|
+
// 补发一次 TERM,仍在则报错给手工排障命令,绝不自动 KILL。
|
|
4
5
|
// 同步 CLI 用 Atomics.wait 节流。
|
|
5
6
|
import * as launchd from './launchd.mjs';
|
|
6
7
|
import { linkLabel } from './render.mjs';
|
|
7
8
|
|
|
8
|
-
const
|
|
9
|
+
const TERM_ROUNDS = 2; // 每轮最多等 20s,两轮共覆盖最坏 ~35s 的前台报名 ssh
|
|
10
|
+
const ROUND_MS = 20_000;
|
|
9
11
|
const POLL_MS = 500;
|
|
10
12
|
|
|
11
13
|
function sleepMs(ms) {
|
|
@@ -14,7 +16,8 @@ function sleepMs(ms) {
|
|
|
14
16
|
}
|
|
15
17
|
|
|
16
18
|
function isGone(ctx, label) {
|
|
17
|
-
|
|
19
|
+
// 只要 print 的注册/运行/pid:轮询每 500ms 一次,不跑全域 print-disabled
|
|
20
|
+
const st = launchd.printState(ctx.runner, ctx.uid, label);
|
|
18
21
|
return !st.registered || (!st.running && st.pid === null);
|
|
19
22
|
}
|
|
20
23
|
|
|
@@ -22,22 +25,18 @@ function isGone(ctx, label) {
|
|
|
22
25
|
* @param {object} ctx
|
|
23
26
|
* @returns {{gone:boolean, label:string}}
|
|
24
27
|
*/
|
|
25
|
-
export function
|
|
28
|
+
export function reapEdgeLink(ctx) {
|
|
26
29
|
const label = linkLabel(ctx.topo.core.alias);
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
ctx.state.removeOn(['relay', 'peer']);
|
|
30
|
-
if (!launchd.inspect(ctx.runner, ctx.uid, label).registered) return { gone: true, label };
|
|
30
|
+
if (!launchd.printState(ctx.runner, ctx.uid, label).registered)
|
|
31
|
+
return { gone: true, label };
|
|
31
32
|
|
|
32
|
-
let
|
|
33
|
-
for (let attempt = 0; attempt < 2; attempt += 1) {
|
|
33
|
+
for (let attempt = 0; attempt < TERM_ROUNDS; attempt += 1) {
|
|
34
34
|
launchd.killJob(ctx.runner, ctx.uid, label, 'TERM');
|
|
35
|
-
const end = Date.now() +
|
|
35
|
+
const end = Date.now() + ROUND_MS;
|
|
36
36
|
while (Date.now() < end) {
|
|
37
37
|
if (isGone(ctx, label)) return { gone: true, label };
|
|
38
38
|
sleepMs(POLL_MS);
|
|
39
39
|
}
|
|
40
|
-
remaining -= 20_000;
|
|
41
40
|
}
|
|
42
41
|
return { gone: isGone(ctx, label), label };
|
|
43
42
|
}
|
package/libexec/lib/launchd.mjs
CHANGED
|
@@ -12,6 +12,12 @@ export const guiDomain = (uid) => `gui/${uid}`;
|
|
|
12
12
|
|
|
13
13
|
const EDGE_PLIST_PREFIX = edgeLabel(''); // 'local.ssh-net-uplink-edge-'
|
|
14
14
|
|
|
15
|
+
/** 未注册状态的固定形状(parsePrint 与两个 inspect 的未注册返回共用)。 */
|
|
16
|
+
const NOT_REGISTERED_PRINT = Object.freeze(
|
|
17
|
+
{ registered: false, running: false, pid: null, runs: null, lastExitCode: null },
|
|
18
|
+
);
|
|
19
|
+
const NOT_REGISTERED = Object.freeze({ disabled: false, ...NOT_REGISTERED_PRINT });
|
|
20
|
+
|
|
15
21
|
/**
|
|
16
22
|
* 磁盘 LaunchAgents 里 uplink-edge job plist 的别名清单(install/status/
|
|
17
23
|
* uninstall 共用;topology 已删的机器只能靠这个 glob 兜底发现,目录不存在
|
|
@@ -42,9 +48,7 @@ export function parsePrint(result) {
|
|
|
42
48
|
// enable+bootstrap 会以真实报错浮出(2026-09-29 真机确认 113 语义)
|
|
43
49
|
const registered =
|
|
44
50
|
result.code === 0 && !/Could not find service/.test(result.stderr ?? '');
|
|
45
|
-
if (!registered)
|
|
46
|
-
return { registered: false, running: false, pid: null, runs: null, lastExitCode: null };
|
|
47
|
-
}
|
|
51
|
+
if (!registered) return NOT_REGISTERED_PRINT;
|
|
48
52
|
const out = result.stdout ?? '';
|
|
49
53
|
const pidMatch = out.match(/^\s*pid\s*=\s*(\d+)/m);
|
|
50
54
|
const runsMatch = out.match(/^\s*runs\s*=\s*(\d+)/m);
|
|
@@ -82,22 +86,58 @@ export function parseDisabled(stdout) {
|
|
|
82
86
|
*/
|
|
83
87
|
|
|
84
88
|
/**
|
|
85
|
-
*
|
|
89
|
+
* 整个 gui 域的 disabled override 字典(一次 print-disabled)。同一域多个
|
|
90
|
+
* label 体检时循环外取一次、inspectWith 共用——全域 dump 与查哪个 label 无关。
|
|
91
|
+
* @param {import('./exec.mjs').Runner} runner
|
|
92
|
+
* @param {number} uid
|
|
93
|
+
* @returns {Map<string, boolean>}
|
|
94
|
+
*/
|
|
95
|
+
export function disabledMap(runner, uid) {
|
|
96
|
+
return parseDisabled(
|
|
97
|
+
runner.run('launchctl', ['print-disabled', guiDomain(uid)]).stdout,
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* print + 预取字典合成四态(inspect 的 map 注入版;字典由调用方循环外取一次
|
|
103
|
+
* 共用)。print 先行:未注册不碰传入 map。
|
|
104
|
+
* @param {import('./exec.mjs').Runner} runner
|
|
105
|
+
* @param {number} uid
|
|
106
|
+
* @param {string} label
|
|
107
|
+
* @param {Map<string, boolean>} map disabledMap 的产物
|
|
108
|
+
* @returns {JobState}
|
|
109
|
+
*/
|
|
110
|
+
export function inspectWith(runner, uid, label, map) {
|
|
111
|
+
const printed = printState(runner, uid, label);
|
|
112
|
+
if (!printed.registered) return NOT_REGISTERED;
|
|
113
|
+
return { ...printed, disabled: map.get(label) === true };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* print + print-disabled 合成四态所需的全部事实(单 label 便捷封装)。
|
|
118
|
+
* print 在前、未注册即返回(不白跑全域 dump)。
|
|
86
119
|
* @param {import('./exec.mjs').Runner} runner
|
|
87
120
|
* @param {number} uid
|
|
88
121
|
* @param {string} label
|
|
89
122
|
* @returns {JobState}
|
|
90
123
|
*/
|
|
91
124
|
export function inspect(runner, uid, label) {
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
125
|
+
const printed = printState(runner, uid, label);
|
|
126
|
+
if (!printed.registered) return NOT_REGISTERED;
|
|
127
|
+
return { ...printed, disabled: disabledMap(runner, uid).get(label) === true };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* 只要注册/运行/pid 事实(stop 轮询专用):不跑全域 print-disabled。
|
|
132
|
+
* @param {import('./exec.mjs').Runner} runner
|
|
133
|
+
* @param {number} uid
|
|
134
|
+
* @param {string} label
|
|
135
|
+
* @returns {ReturnType<typeof parsePrint>}
|
|
136
|
+
*/
|
|
137
|
+
export function printState(runner, uid, label) {
|
|
138
|
+
return parsePrint(
|
|
139
|
+
runner.run('launchctl', ['print', `${guiDomain(uid)}/${label}`]),
|
|
99
140
|
);
|
|
100
|
-
return { ...printed, disabled: disabledMap.get(label) === true };
|
|
101
141
|
}
|
|
102
142
|
|
|
103
143
|
/** 以下均返回原始 RunResult,由命令层判 code(编排顺序是冻结行为,勿在此吞错)。 */
|
|
@@ -109,6 +149,20 @@ export const bootstrap = (runner, uid, plistPath) =>
|
|
|
109
149
|
runner.run('launchctl', ['bootstrap', guiDomain(uid), plistPath]);
|
|
110
150
|
export const kickstart = (runner, uid, label) =>
|
|
111
151
|
runner.run('launchctl', ['kickstart', '-k', `${guiDomain(uid)}/${label}`]);
|
|
152
|
+
/**
|
|
153
|
+
* enable + bootstrap 连发(未注册 job 的加载序列)。任一步失败抛错——
|
|
154
|
+
* enable 失败若吞掉,只会以 bootstrap error 5 的间接形式浮出,排障被带偏。
|
|
155
|
+
* @param {import('./exec.mjs').Runner} runner
|
|
156
|
+
* @param {number} uid
|
|
157
|
+
* @param {string} label
|
|
158
|
+
* @param {string} plistPath
|
|
159
|
+
*/
|
|
160
|
+
export function enableBootstrap(runner, uid, label, plistPath) {
|
|
161
|
+
const en = enable(runner, uid, label);
|
|
162
|
+
if (en.code !== 0) throw new Error(`enable 失败(${label}):${en.stderr.trim()}`);
|
|
163
|
+
const b = bootstrap(runner, uid, plistPath);
|
|
164
|
+
if (b.code !== 0) throw new Error(`bootstrap 失败(${label}):${b.stderr.trim()}`);
|
|
165
|
+
}
|
|
112
166
|
/** kickstart 对 disabled job 被拒(实测矩阵),kickstart 前一律幂等 enable。
|
|
113
167
|
* enable 失败要报(本文件宣言「勿在此吞错」):否则以 kickstart 被拒的
|
|
114
168
|
* 间接形式浮出,排障方向被带偏。 */
|
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,76 @@
|
|
|
1
|
+
// 运行时实体/服务软链清单的单一来源(install 对账、报名对账与 status 体检
|
|
2
|
+
// 共用,design §2.3)。清单即契约:新增包脚本只改这里,漏装与假报「组件
|
|
3
|
+
// 异常」不会再分叉。
|
|
4
|
+
//
|
|
5
|
+
// entities:[bin 名, 包脚本名]——原名实体;links:[带机器名软链, 实体名]。
|
|
6
|
+
import { edgeLinkName, linkLinkName, relayLinkName } from './render.mjs';
|
|
7
|
+
|
|
8
|
+
// bin 脚本名:plan 清单与按名判断(status 体检、install 预检)共用,改名一处
|
|
9
|
+
export const SN_PROBE = 'sn-probe';
|
|
10
|
+
export const SN_ROUTE = 'sn-route';
|
|
11
|
+
export const SN_IPV6 = 'sn-ipv6';
|
|
12
|
+
export const LINK_CORE = 'ssh-net-link-core';
|
|
13
|
+
export const UPLINK_RELAY = 'ssh-net-uplink-relay';
|
|
14
|
+
export const UPLINK_EDGE = 'ssh-net-uplink-edge';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* link 附加件专属产物:第 4 实体 + 带机器名软链。edgeRuntimePlan 装了 link
|
|
18
|
+
* 时并入;install --without-link 按这份清单删。
|
|
19
|
+
* @param {import('./config.mjs').Topology} topo
|
|
20
|
+
*/
|
|
21
|
+
export function linkAddonPlan(topo) {
|
|
22
|
+
return {
|
|
23
|
+
entities: [[LINK_CORE, LINK_CORE]],
|
|
24
|
+
links: [[linkLinkName(topo.core.alias), LINK_CORE]],
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* edge 形态:基础三件套恒装;装了 link 附加件才加第 4 实体与带机器名软链。
|
|
30
|
+
* @param {import('./config.mjs').Topology} topo
|
|
31
|
+
* @param {boolean} linkInstalled
|
|
32
|
+
*/
|
|
33
|
+
export function edgeRuntimePlan(topo, linkInstalled) {
|
|
34
|
+
/** @type {[string,string][]} */
|
|
35
|
+
const entities = [
|
|
36
|
+
[SN_PROBE, SN_PROBE],
|
|
37
|
+
[SN_ROUTE, SN_ROUTE], // Match exec 按需选路 + 成功后本地通知/秒切(design §2.2)
|
|
38
|
+
[SN_IPV6, SN_IPV6],
|
|
39
|
+
];
|
|
40
|
+
return linkInstalled
|
|
41
|
+
? mergePlans({ entities, links: [] }, linkAddonPlan(topo))
|
|
42
|
+
: { entities, links: [] };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* relay 一件套:core/node 常驻服务的基础形态(installCore 在其上并入在册
|
|
47
|
+
* edge 的回拨组件)。
|
|
48
|
+
* @param {import('./config.mjs').Topology} topo
|
|
49
|
+
*/
|
|
50
|
+
export function relayRuntimePlan(topo) {
|
|
51
|
+
return {
|
|
52
|
+
entities: [[UPLINK_RELAY, UPLINK_RELAY]],
|
|
53
|
+
links: [[relayLinkName(topo.relay.alias), UPLINK_RELAY]],
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* core 侧在册 edge 的回拨组件并集:uplink-edge 全 core 一份实体,每台 edge
|
|
59
|
+
* 一条带别名软链(install 按磁盘在册、_register 传单台、status 按 topology
|
|
60
|
+
* 在册,三方同源)。
|
|
61
|
+
* @param {readonly string[]} aliases
|
|
62
|
+
*/
|
|
63
|
+
export function edgeCallbacksPlan(aliases) {
|
|
64
|
+
return {
|
|
65
|
+
entities: aliases.length ? [[UPLINK_EDGE, UPLINK_EDGE]] : [],
|
|
66
|
+
links: aliases.map((alias) => [edgeLinkName(alias), UPLINK_EDGE]),
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** 多份计划并集(顺序拼接;调用方保证各份之间实体/软链不重复)。 */
|
|
71
|
+
export function mergePlans(...plans) {
|
|
72
|
+
return {
|
|
73
|
+
entities: plans.flatMap((p) => p.entities),
|
|
74
|
+
links: plans.flatMap((p) => p.links),
|
|
75
|
+
};
|
|
76
|
+
}
|
package/libexec/lib/paths.mjs
CHANGED
|
@@ -49,6 +49,8 @@ export function machinePaths(env = process.env) {
|
|
|
49
49
|
sshNetDir,
|
|
50
50
|
topologyFile: path.join(sshNetDir, 'topology'),
|
|
51
51
|
binDir: path.join(sshNetDir, 'bin'),
|
|
52
|
+
/** @param {string} name bin/ 下产物名(实体或服务软链) */
|
|
53
|
+
binScript: (name) => path.join(sshNetDir, 'bin', name),
|
|
52
54
|
runtimeVersionFile: path.join(sshNetDir, 'runtime-version'),
|
|
53
55
|
stateDir,
|
|
54
56
|
// ssh Include 片段目录(design §4.2,2026-10-01):routes 仅 edge install
|
package/libexec/lib/probe.mjs
CHANGED
|
@@ -47,24 +47,39 @@ export function listListeners(runner, port) {
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
/**
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
* 本地转发——与本服务不同地址族、可共存,不算冲突。用全输出 lsof 按
|
|
53
|
-
* `[::1]:<port>` 行过滤(lsof 的 -iTCP@ 过滤对 v6 字面量支持不稳)。
|
|
50
|
+
* 全输出 lsof 查某端口 LISTEN 持有者(无监听返回 '')。预检报冲突与僵尸
|
|
51
|
+
* 判定都用它:一次调用同时拿 pid 与给人看的持有者文本。
|
|
54
52
|
* @param {import('./exec.mjs').Runner} runner
|
|
55
53
|
* @param {number} port
|
|
56
|
-
* @returns {
|
|
54
|
+
* @returns {string}
|
|
57
55
|
*/
|
|
58
|
-
export function
|
|
56
|
+
export function lsofListen(runner, port) {
|
|
59
57
|
const r = runner.run('lsof', ['-nP', `-iTCP:${port}`, '-sTCP:LISTEN']);
|
|
60
|
-
|
|
61
|
-
|
|
58
|
+
return r.code === 0 ? r.stdout.trim() : '';
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 从 lsof 全输出解析 v6 环回([::1])监听者 pid(lsof 的 -iTCP@ 过滤对 v6
|
|
63
|
+
* 字面量支持不稳,按行过滤)。v4 环回(127.0.0.1)上的同名端口持有者——如
|
|
64
|
+
* VS Code 本地转发——与本服务不同地址族、可共存,不算冲突。
|
|
65
|
+
* @param {string} text lsofListen 的输出
|
|
66
|
+
* @param {number} port
|
|
67
|
+
* @returns {number[]}
|
|
68
|
+
*/
|
|
69
|
+
export function v6LoopbackPids(text, port) {
|
|
70
|
+
return text
|
|
62
71
|
.split('\n')
|
|
63
72
|
.filter((l) => l.includes(`[::1]:${port}`))
|
|
64
73
|
.map((l) => Number(l.match(/\s(\d+)\s/)?.[1]))
|
|
65
74
|
.filter((n) => Number.isInteger(n));
|
|
66
75
|
}
|
|
67
76
|
|
|
77
|
+
/** 端口的 v6 环回([::1])监听者 pid 列表(peer 回拨口专用,2026-09-30 起
|
|
78
|
+
* 10022 只听 [::1])。 */
|
|
79
|
+
export function listV6LoopbackListeners(runner, port) {
|
|
80
|
+
return v6LoopbackPids(lsofListen(runner, port), port);
|
|
81
|
+
}
|
|
82
|
+
|
|
68
83
|
/**
|
|
69
84
|
* 全机 pid→ppid 表。
|
|
70
85
|
* @param {import('./exec.mjs').Runner} runner
|
|
@@ -110,13 +125,15 @@ export function descendantsOf(tree, root) {
|
|
|
110
125
|
* @param {import('./exec.mjs').Runner} runner
|
|
111
126
|
* @param {number} port
|
|
112
127
|
* @param {number|null} jobPid link job 的 pid(未注册给 null)
|
|
128
|
+
* @param {Map<number,number>} [tree] 预检全程进程树不变:多端口预检时调用方
|
|
129
|
+
* 建一次传入复用,缺省本函数自取
|
|
113
130
|
* @returns {{kind:'free'|'ours'|'foreign', pids:number[]}}
|
|
114
131
|
*/
|
|
115
|
-
export function classifyLocalPort(runner, port, jobPid) {
|
|
132
|
+
export function classifyLocalPort(runner, port, jobPid, tree) {
|
|
116
133
|
const pids = listListeners(runner, port);
|
|
117
134
|
if (pids.length === 0) return { kind: 'free', pids: [] };
|
|
118
135
|
if (jobPid === null) return { kind: 'foreign', pids };
|
|
119
|
-
const ok = new Set([jobPid, ...descendantsOf(processTree(runner), jobPid)]);
|
|
136
|
+
const ok = new Set([jobPid, ...descendantsOf(tree ?? processTree(runner), jobPid)]);
|
|
120
137
|
return pids.every((p) => ok.has(p))
|
|
121
138
|
? { kind: 'ours', pids }
|
|
122
139
|
: { 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
|