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
|
@@ -7,7 +7,6 @@
|
|
|
7
7
|
// bin/ 体检(2026-09-30 软链方案):实体逐字节比对 + 服务软链指向/悬空检查。
|
|
8
8
|
// 退出码:健康(含 edge 关闭态)0;有可行动问题 1。
|
|
9
9
|
import fs from 'node:fs';
|
|
10
|
-
import path from 'node:path';
|
|
11
10
|
import {
|
|
12
11
|
auditScript, auditLink, runtimeVersionIssue,
|
|
13
12
|
} from '../lib/runtime.mjs';
|
|
@@ -15,9 +14,16 @@ import { packageScriptPath } from '../lib/paths.mjs';
|
|
|
15
14
|
import * as launchd from '../lib/launchd.mjs';
|
|
16
15
|
import * as systemd from '../lib/systemd.mjs';
|
|
17
16
|
import { peerProbeOk, tcpOpen } from '../lib/probe.mjs';
|
|
17
|
+
import { readFileOrNull } from '../lib/fsutil.mjs';
|
|
18
18
|
import {
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
SN_ROUTE, SN_IPV6, UPLINK_EDGE,
|
|
20
|
+
edgeRuntimePlan, relayRuntimePlan, edgeCallbacksPlan,
|
|
21
|
+
} from '../lib/manifest.mjs';
|
|
22
|
+
import { edgeFragmentContent, edgeFragmentShouldExist } from '../lib/registration.mjs';
|
|
23
|
+
import {
|
|
24
|
+
linkLabel, relayLabel, relayUnitName,
|
|
25
|
+
edgeLabel,
|
|
26
|
+
renderRoutes, coreViaRelayArgs,
|
|
21
27
|
} from '../lib/render.mjs';
|
|
22
28
|
|
|
23
29
|
/**
|
|
@@ -25,38 +31,46 @@ import {
|
|
|
25
31
|
* @returns {'ok'|'stale'|'missing'}
|
|
26
32
|
*/
|
|
27
33
|
function auditConfFragment(file, expected) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
text = fs.readFileSync(file, 'utf8');
|
|
31
|
-
} catch { return 'missing'; }
|
|
34
|
+
const text = readFileOrNull(file);
|
|
35
|
+
if (text === null) return 'missing';
|
|
32
36
|
return text === expected ? 'ok' : 'stale';
|
|
33
37
|
}
|
|
34
38
|
|
|
35
|
-
/**
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/** 各角色共用的实体+软链体检,返回人话问题列表(空=健康)。 */
|
|
44
|
-
function runtimeIssues(ctx, { entities, serviceLinks }) {
|
|
39
|
+
/**
|
|
40
|
+
* 各角色共用的实体+软链体检。
|
|
41
|
+
* @returns {{issues:string[], entityAudits:{name:string,audit:string}[]}}
|
|
42
|
+
* issues 是人话问题列表(空=健康)
|
|
43
|
+
*/
|
|
44
|
+
function runtimeIssues(ctx, { entities, links }) {
|
|
45
45
|
const issues = [];
|
|
46
|
-
|
|
47
|
-
|
|
46
|
+
// 实体逐字节比对(name 是 bin 实体名,不是软链)
|
|
47
|
+
const entityAudits = entities.map(([name, script]) => ({
|
|
48
|
+
name,
|
|
49
|
+
audit: auditScript(ctx.paths.binScript(name), packageScriptPath(script)),
|
|
50
|
+
}));
|
|
51
|
+
if (entityAudits.some((a) => a.audit === 'stale'))
|
|
48
52
|
issues.push('隧道组件与所装版本不一致——跑一次 ssh-net install 刷新');
|
|
49
|
-
if (
|
|
53
|
+
if (entityAudits.every((a) => a.audit === 'missing'))
|
|
50
54
|
issues.push('尚未安装(先跑 ssh-net install)');
|
|
51
|
-
else if (
|
|
55
|
+
else if (entityAudits.some((a) => a.audit === 'missing'))
|
|
52
56
|
// 部分缺失:探针丢了会让开着的隧道恒判中继,不能只显示不报
|
|
53
|
-
issues.push(`隧道组件不完整(缺 ${
|
|
54
|
-
for (const [linkName, entityName] of
|
|
55
|
-
const r = auditLink(
|
|
57
|
+
issues.push(`隧道组件不完整(缺 ${entityAudits.filter((a) => a.audit === 'missing').map((a) => a.name).join('、')})——跑一次 ssh-net install 修复`);
|
|
58
|
+
for (const [linkName, entityName] of links) {
|
|
59
|
+
const r = auditLink(ctx.paths.binScript(linkName), entityName);
|
|
56
60
|
if (r !== 'ok')
|
|
57
61
|
issues.push(`隧道组件异常(${linkName}:${r})——跑一次 ssh-net install 修复`);
|
|
58
62
|
}
|
|
59
|
-
return { issues,
|
|
63
|
+
return { issues, entityAudits };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** 三个 collector 的共同前奏:版本戳问题 + 实体/软链体检。 */
|
|
67
|
+
function baseAudit(ctx, plan) {
|
|
68
|
+
const issues = [];
|
|
69
|
+
const vIssue = runtimeVersionIssue(ctx.paths);
|
|
70
|
+
if (vIssue) issues.push(vIssue);
|
|
71
|
+
const { issues: rtIssues, entityAudits } = runtimeIssues(ctx, plan);
|
|
72
|
+
issues.push(...rtIssues);
|
|
73
|
+
return { issues, entityAudits };
|
|
60
74
|
}
|
|
61
75
|
|
|
62
76
|
/** forward 的人话标签:写出目标(loopback 是落地机环回)。 */
|
|
@@ -92,54 +106,38 @@ async function relayPortListening(ctx, port) {
|
|
|
92
106
|
}
|
|
93
107
|
|
|
94
108
|
/** edge 的 relay 线路实测:命令行显式 -J 经 relay 跑一条真 ssh(报名/中继同
|
|
95
|
-
* 信道;字面 127.0.0.1 不命中 Match,绕开自动选路,design §6.7)。
|
|
109
|
+
* 信道;字面 127.0.0.1 不命中 Match,绕开自动选路,design §6.7)。
|
|
110
|
+
* 探测不接受新 host key(acceptHost 缺省 false)——与无人值守报名的
|
|
111
|
+
* accept-new 是刻意分级(design §4.3)。 */
|
|
96
112
|
async function relayPathOk(ctx) {
|
|
97
|
-
const r = await ctx.runner.runAsync(
|
|
98
|
-
'
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
'-o', 'IdentitiesOnly=yes',
|
|
103
|
-
'-o', 'ControlMaster=no', '-o', 'ControlPath=none',
|
|
104
|
-
`${ctx.topo.core.user}@127.0.0.1`, 'true',
|
|
105
|
-
], { timeout: 8000 });
|
|
113
|
+
const r = await ctx.runner.runAsync(
|
|
114
|
+
'ssh',
|
|
115
|
+
coreViaRelayArgs(ctx.topo, { connectTimeout: 5, remoteCommand: 'true' }),
|
|
116
|
+
{ timeout: 8000 },
|
|
117
|
+
);
|
|
106
118
|
return r.code === 0;
|
|
107
119
|
}
|
|
108
120
|
|
|
109
121
|
async function collectEdge(ctx) {
|
|
110
122
|
const { topo, paths, state } = ctx;
|
|
111
|
-
|
|
112
|
-
const vIssue = runtimeVersionIssue(ctx.paths);
|
|
113
|
-
if (vIssue) issues.push(vIssue);
|
|
114
|
-
// link 附加件以 marker 为准:默认只有 5 实体、无服务软链;装了才加
|
|
123
|
+
// link 附加件以 marker 为准:默认只有 3 实体、无服务软链;装了才加
|
|
115
124
|
// link-core(转发口只取决于装没装,不是运行时开关)
|
|
116
125
|
const linkInstalled = fs.existsSync(paths.linkEnabledFile);
|
|
117
|
-
|
|
118
|
-
const
|
|
119
|
-
['sn-probe', 'sn-probe'],
|
|
120
|
-
['sn-route', 'sn-route'],
|
|
121
|
-
['sn-ipv6', 'sn-ipv6'],
|
|
122
|
-
];
|
|
123
|
-
/** @type {[string,string][]} */
|
|
124
|
-
const serviceLinks = [];
|
|
125
|
-
if (linkInstalled) {
|
|
126
|
-
entities.push(['ssh-net-link-core', 'ssh-net-link-core']);
|
|
127
|
-
serviceLinks.push([linkLinkName(topo.core.alias), 'ssh-net-link-core']);
|
|
128
|
-
}
|
|
129
|
-
const { issues: rtIssues, links } = runtimeIssues(ctx, { entities, serviceLinks });
|
|
130
|
-
issues.push(...rtIssues);
|
|
126
|
+
const plan = edgeRuntimePlan(topo, linkInstalled);
|
|
127
|
+
const { issues, entityAudits } = baseAudit(ctx, plan);
|
|
131
128
|
|
|
132
129
|
// routes 体检(sn-route 实体在才查:没装时 rtIssues 已报「尚未安装」):
|
|
133
130
|
// routes 被手删/过期会让 ssh 静默退回 lan 直连,是故障、进 issues。文件尾部
|
|
134
131
|
// 的 VS Code 打标块同属这份内容,逐字节对账一并覆盖,不用单独体检
|
|
135
|
-
if (
|
|
132
|
+
if (entityAudits.some((a) => a.name === SN_ROUTE && a.audit === 'ok')) {
|
|
136
133
|
const routeAudit = auditConfFragment(paths.routesFile, renderRoutes(topo, ctx.self.alias));
|
|
137
134
|
if (routeAudit !== 'ok')
|
|
138
135
|
issues.push(`选路片段 conf.d/routes ${routeAudit === 'missing' ? '缺失' : '与 topology 不一致'}——跑一次 ssh-net install 刷新`);
|
|
139
136
|
}
|
|
140
137
|
|
|
138
|
+
// 显示只消费 registered/running:轻量 print 即可,不跑全域 print-disabled
|
|
141
139
|
const label = linkLabel(topo.core.alias);
|
|
142
|
-
const job = launchd.
|
|
140
|
+
const job = launchd.printState(ctx.runner, ctx.uid, label);
|
|
143
141
|
const switches = state.readSwitches();
|
|
144
142
|
|
|
145
143
|
// 非法组合:peer 依赖 relay。开关是「文件即接口」——手 touch/rm state 里
|
|
@@ -148,6 +146,16 @@ async function collectEdge(ctx) {
|
|
|
148
146
|
if (switches.peer && !switches.relay)
|
|
149
147
|
issues.push('开关组合异常:peer 开但 relay 关(peer 依赖 relay,多半是 state 里的开关被手动改动)——跑一次 ssh-net stop 后再按需要 start');
|
|
150
148
|
|
|
149
|
+
// v6 探测与下面的就绪实测互不依赖:同一批并发(5s sn-ipv6 不再串行白等在
|
|
150
|
+
// 8s relay 探测之后)。全关(在办公室)时不跑。v6 只是诊断线索不是判决:
|
|
151
|
+
// 探测没跑成(超时/非零退出/实体缺失)时不能谎称「没有 IPv6」——那个
|
|
152
|
+
// 结论只在 sn-ipv6 正常跑完且无输出时才成立
|
|
153
|
+
const needV6 = (switches.relay || switches.peer)
|
|
154
|
+
&& entityAudits.some((a) => a.name === SN_IPV6 && a.audit === 'ok');
|
|
155
|
+
const v6P = needV6
|
|
156
|
+
? ctx.runner.runRawAsync(paths.binScript(SN_IPV6), [], { timeout: 5000 })
|
|
157
|
+
: Promise.resolve(null);
|
|
158
|
+
|
|
151
159
|
// 就绪实测并发(On 是意图、ready 是事实;开关关的线路不探测=null)。
|
|
152
160
|
// 转发口只有装了 link 才可能存在——默认形态不探
|
|
153
161
|
const relayOk = ctx.relayOk ?? relayPathOk; // 测试可注入
|
|
@@ -164,7 +172,7 @@ async function collectEdge(ctx) {
|
|
|
164
172
|
const coreLanIp = ctx.hostTable.get(topo.core.alias)?.hostname;
|
|
165
173
|
const lanIdx = switches.relay && coreLanIp ? probes.length : -1;
|
|
166
174
|
if (lanIdx >= 0) probes.push(openTcp(coreLanIp, 22, 1200));
|
|
167
|
-
const got = await Promise.all(probes);
|
|
175
|
+
const [got, v6Probe] = await Promise.all([Promise.all(probes), v6P]);
|
|
168
176
|
const relayReady = got[0];
|
|
169
177
|
const peerReady = got[1];
|
|
170
178
|
const ports = {};
|
|
@@ -172,6 +180,13 @@ async function collectEdge(ctx) {
|
|
|
172
180
|
topo.forwards.forEach((f, i) => { ports[f.listenPort] = got[2 + i]; });
|
|
173
181
|
const lanReachable = lanIdx >= 0 ? got[lanIdx] : false;
|
|
174
182
|
|
|
183
|
+
let v6 = '';
|
|
184
|
+
let v6ProbeOk = false;
|
|
185
|
+
if (v6Probe) {
|
|
186
|
+
v6 = v6Probe.stdout.trim();
|
|
187
|
+
v6ProbeOk = !v6Probe.error && v6Probe.code === 0;
|
|
188
|
+
}
|
|
189
|
+
|
|
175
190
|
if (lanReachable)
|
|
176
191
|
issues.push(`检测到 ${topo.core.alias}(${coreLanIp})局域网直连可达——隧道可能在空转绕中继;人在办公室可 ssh-net stop`);
|
|
177
192
|
|
|
@@ -188,19 +203,6 @@ async function collectEdge(ctx) {
|
|
|
188
203
|
if (f.targetAlias === 'loopback' && !ports[f.listenPort])
|
|
189
204
|
issues.push(`${forwardLabel(f)} 未就绪:edge 侧正常时多半是 core 上的出口代理(microsocks)没跑——到 core 上查 local.microsocks`);
|
|
190
205
|
|
|
191
|
-
// v6 只是诊断线索不是判决:探测没跑成(超时被杀/非零退出/实体缺失)时
|
|
192
|
-
// 不能谎称「没有 IPv6」——那个结论只在 sn-ipv6 正常跑完且无输出时才成立
|
|
193
|
-
//(与 doctor 的 probeOk 同判据;sn-ipv6 自身退出码恒 0,code!==0 只会是
|
|
194
|
-
// 超时或崩溃)。全关(在办公室)时 render 早返回不显示 v6 行,不白跑
|
|
195
|
-
let v6 = '';
|
|
196
|
-
let v6ProbeOk = false;
|
|
197
|
-
if ((switches.relay || switches.peer)
|
|
198
|
-
&& links.find((l) => l.link === 'sn-ipv6')?.audit === 'ok') {
|
|
199
|
-
const probe = ctx.runner.runRaw(path.join(paths.binDir, 'sn-ipv6'), [], { timeout: 5000 });
|
|
200
|
-
v6 = probe.stdout.trim();
|
|
201
|
-
v6ProbeOk = !probe.error && probe.code === 0;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
206
|
const forwardViews = linkInstalled
|
|
205
207
|
? topo.forwards.map((f) => [forwardLabel(f), ports[f.listenPort]])
|
|
206
208
|
: [];
|
|
@@ -214,36 +216,32 @@ async function collectEdge(ctx) {
|
|
|
214
216
|
|
|
215
217
|
async function collectCore(ctx) {
|
|
216
218
|
const { topo, paths } = ctx;
|
|
217
|
-
const issues =
|
|
218
|
-
const vIssue = runtimeVersionIssue(ctx.paths);
|
|
219
|
-
if (vIssue) issues.push(vIssue);
|
|
220
|
-
const { issues: rtIssues } = runtimeIssues(ctx, {
|
|
221
|
-
entities: [['ssh-net-uplink-relay', 'ssh-net-uplink-relay']],
|
|
222
|
-
serviceLinks: [[relayLinkName(topo.relay.alias), 'ssh-net-uplink-relay']],
|
|
223
|
-
});
|
|
224
|
-
issues.push(...rtIssues);
|
|
219
|
+
const { issues } = baseAudit(ctx, relayRuntimePlan(topo));
|
|
225
220
|
|
|
221
|
+
// disabled override 是整域同一份字典:relay job 与各 edge job 共用一次拉取
|
|
222
|
+
const dmap = launchd.disabledMap(ctx.runner, ctx.uid);
|
|
226
223
|
const relayLabelName = relayLabel(topo.relay.alias);
|
|
227
|
-
const relayJob = launchd.
|
|
224
|
+
const relayJob = launchd.inspectWith(ctx.runner, ctx.uid, relayLabelName, dmap);
|
|
228
225
|
// relay 口实测是 8s 超时的真 ssh:先发起来,边等边做下面的注册态体检
|
|
229
226
|
const relayPortP = relayPortListening(ctx, topo.core.relayPort);
|
|
230
227
|
|
|
228
|
+
// 回拨组件清单与 install/报名同源(manifest):实体与别名无关,循环外体检
|
|
229
|
+
// 一次,各已注册 edge 只检各自的带别名软链。topology 无 edge 时不审
|
|
230
|
+
const callbackEntityAudit = topo.edges.length > 0
|
|
231
|
+
? auditScript(paths.binScript(UPLINK_EDGE), packageScriptPath(UPLINK_EDGE))
|
|
232
|
+
: null;
|
|
233
|
+
|
|
231
234
|
const edges = [];
|
|
232
235
|
for (const e of topo.edges) {
|
|
233
236
|
const label = edgeLabel(e.alias);
|
|
234
|
-
const st = launchd.
|
|
237
|
+
const st = launchd.inspectWith(ctx.runner, ctx.uid, label, dmap);
|
|
235
238
|
// 已连接过的随身机,回拨组件一并体检:回拨脚本/软链只有 _register
|
|
236
239
|
// (报名)这条刷新通道,漏刷会拿旧脚本拨号
|
|
237
240
|
if (st.registered) {
|
|
241
|
+
const [linkName, entityName] = edgeCallbacksPlan([e.alias]).links[0];
|
|
238
242
|
const bad = [];
|
|
239
|
-
if (
|
|
240
|
-
|
|
241
|
-
packageScriptPath('ssh-net-uplink-edge'),
|
|
242
|
-
) !== 'ok') bad.push('回拨脚本');
|
|
243
|
-
const linkState = auditLink(
|
|
244
|
-
path.join(paths.binDir, edgeLinkName(e.alias)),
|
|
245
|
-
'ssh-net-uplink-edge',
|
|
246
|
-
);
|
|
243
|
+
if (callbackEntityAudit !== 'ok') bad.push('回拨脚本');
|
|
244
|
+
const linkState = auditLink(paths.binScript(linkName), entityName);
|
|
247
245
|
if (linkState !== 'ok') bad.push(`软链(${linkState})`);
|
|
248
246
|
if (bad.length)
|
|
249
247
|
// 已注册 edge 的实体/软链其实会被 install 刷新(26.9.19 起),但
|
|
@@ -257,11 +255,8 @@ async function collectCore(ctx) {
|
|
|
257
255
|
// install)都会让 `ssh <别名>` 失败或解析错。休眠(disabled)时片段
|
|
258
256
|
// 本就该删(六连败收尸),不报;未注册无 v6 也不报
|
|
259
257
|
let fragmentAudit = null;
|
|
260
|
-
if (st
|
|
261
|
-
fragmentAudit = auditConfFragment(
|
|
262
|
-
paths.edgeHostFile(e.alias),
|
|
263
|
-
renderEdgeHost({ alias: e.alias, aliases: e.aliases, user: e.user, v6 }),
|
|
264
|
-
);
|
|
258
|
+
if (edgeFragmentShouldExist(st, v6)) {
|
|
259
|
+
fragmentAudit = auditConfFragment(paths.edgeHostFile(e.alias), edgeFragmentContent(e, v6));
|
|
265
260
|
if (fragmentAudit !== 'ok')
|
|
266
261
|
issues.push(`${e.alias} 的 ssh 入口片段${fragmentAudit === 'missing' ? '缺失' : '过期'}(state 有 v6、job 未休眠)——跑一次 ssh-net install 重建`);
|
|
267
262
|
}
|
|
@@ -298,14 +293,7 @@ async function collectCore(ctx) {
|
|
|
298
293
|
|
|
299
294
|
async function collectNode(ctx) {
|
|
300
295
|
const { topo } = ctx;
|
|
301
|
-
const issues =
|
|
302
|
-
const vIssue = runtimeVersionIssue(ctx.paths);
|
|
303
|
-
if (vIssue) issues.push(vIssue);
|
|
304
|
-
const { issues: rtIssues } = runtimeIssues(ctx, {
|
|
305
|
-
entities: [['ssh-net-uplink-relay', 'ssh-net-uplink-relay']],
|
|
306
|
-
serviceLinks: [[relayLinkName(topo.relay.alias), 'ssh-net-uplink-relay']],
|
|
307
|
-
});
|
|
308
|
-
issues.push(...rtIssues);
|
|
296
|
+
const { issues } = baseAudit(ctx, relayRuntimePlan(topo));
|
|
309
297
|
|
|
310
298
|
const unit = relayUnitName(topo.relay.alias);
|
|
311
299
|
const active = systemd.isActive(ctx.runner, unit);
|
|
@@ -348,6 +336,13 @@ function profileOf(sw, linkInstalled) {
|
|
|
348
336
|
return '仅中继信道(待命:不报名、无本地转发口)';
|
|
349
337
|
}
|
|
350
338
|
|
|
339
|
+
/** core/node 共用的 relay 状态行。 */
|
|
340
|
+
function relayStatusLine(s) {
|
|
341
|
+
return s.relayPort.listening
|
|
342
|
+
? ` relay: ✓ 开着(在外面经 ${s.relayAlias} 进入 ${s.alias} · 入口 ${s.relayPort.port})`
|
|
343
|
+
: ` relay: ✗ ${s.relayPort.reachable === false ? `连不上 ${s.relayAlias},入口状态未知` : `入口 ${s.relayPort.port} 没在监听`}`;
|
|
344
|
+
}
|
|
345
|
+
|
|
351
346
|
export function renderText(s) {
|
|
352
347
|
const lines = [];
|
|
353
348
|
lines.push(`ssh-net ${s.role} ${s.alias}`);
|
|
@@ -391,9 +386,7 @@ export function renderText(s) {
|
|
|
391
386
|
}
|
|
392
387
|
lines.push(` 后台任务: ${!s.job.registered ? `未装 link(默认零常驻)` : s.job.running ? 'link 运行中' : 'link 已停止'}`);
|
|
393
388
|
} else if (s.role === 'core') {
|
|
394
|
-
lines.push(s
|
|
395
|
-
? ` relay: ✓ 开着(在外面经 ${s.relayAlias} 进入 ${s.alias} · 入口 ${s.relayPort.port})`
|
|
396
|
-
: ` relay: ✗ ${s.relayPort.reachable === false ? `连不上 ${s.relayAlias},入口状态未知` : `入口 ${s.relayPort.port} 没在监听`}`);
|
|
389
|
+
lines.push(relayStatusLine(s));
|
|
397
390
|
lines.push(` relay 进程: ${s.relayJob.running ? `运行中${exitNote(s.relayJob.lastExitCode)}` : '未运行'}`);
|
|
398
391
|
lines.push(' edge:');
|
|
399
392
|
for (const e of s.edges) {
|
|
@@ -412,9 +405,7 @@ export function renderText(s) {
|
|
|
412
405
|
lines.push(` ${e.alias} ${state}${frag}`);
|
|
413
406
|
}
|
|
414
407
|
} else {
|
|
415
|
-
lines.push(s
|
|
416
|
-
? ` relay: ✓ 开着(在外面经 ${s.relayAlias} 进入 ${s.alias} · 入口 ${s.relayPort.port})`
|
|
417
|
-
: ` relay: ✗ ${s.relayPort.reachable === false ? `连不上 ${s.relayAlias},入口状态未知` : `入口 ${s.relayPort.port} 没在监听`}`);
|
|
408
|
+
lines.push(relayStatusLine(s));
|
|
418
409
|
lines.push(` relay 进程: ${s.active === 'active' ? '运行中(systemd)' : `${s.active}(systemd)`}`);
|
|
419
410
|
}
|
|
420
411
|
|
|
@@ -6,7 +6,8 @@
|
|
|
6
6
|
// node stop+disable。
|
|
7
7
|
import * as launchd from '../lib/launchd.mjs';
|
|
8
8
|
import * as systemd from '../lib/systemd.mjs';
|
|
9
|
-
import {
|
|
9
|
+
import { reapEdgeLink } from '../lib/jobctl.mjs';
|
|
10
|
+
import { assertUnitSwitchArgs } from '../lib/cliargs.mjs';
|
|
10
11
|
import { relayLabel, relayUnitName } from '../lib/render.mjs';
|
|
11
12
|
|
|
12
13
|
const TARGETS = ['relay', 'peer'];
|
|
@@ -44,9 +45,11 @@ export function run(ctx, args = []) {
|
|
|
44
45
|
// 时 sn-route 不报名,自然只走中继
|
|
45
46
|
return 'peer 已关闭(软生效:停止报名。已建立的连接继续用,断了重连就只走中继)';
|
|
46
47
|
}
|
|
47
|
-
// 全关 / 只关 relay
|
|
48
|
+
// 全关 / 只关 relay:级联删两开关(peer 依赖 relay,见 design §5),
|
|
49
|
+
// 硬停 link job(PathState 见 RELAY_ON 消失不再重拉;未装附加件直接 gone)
|
|
48
50
|
const alreadyAllOff = !before.relay && !before.peer;
|
|
49
|
-
|
|
51
|
+
ctx.state.removeOn(['relay', 'peer']);
|
|
52
|
+
const { gone, label } = reapEdgeLink(ctx);
|
|
50
53
|
if (!gone)
|
|
51
54
|
throw new Error(
|
|
52
55
|
`${label} 仍在运行(TERM 已发两次)。手工排查:launchctl print gui/${ctx.uid}/${label}`,
|
|
@@ -61,16 +64,11 @@ export function run(ctx, args = []) {
|
|
|
61
64
|
: 'relay 已关闭(peer 一并关闭——它依赖 relay)。几分钟后 core 侧会停止回拨并静默';
|
|
62
65
|
}
|
|
63
66
|
// core/node 是单总开关(等同 --all):显式 -a/--all 收下,其余参数拒绝
|
|
64
|
-
|
|
65
|
-
throw new Error('本机是单总开关,stop 不带参数(-a/--all 亦可,等同缺省)');
|
|
67
|
+
assertUnitSwitchArgs(args, 'stop');
|
|
66
68
|
if (ctx.role === 'core') return stopCore(ctx);
|
|
67
69
|
if (ctx.role === 'node') {
|
|
68
70
|
const unit = relayUnitName(ctx.topo.relay.alias);
|
|
69
|
-
|
|
70
|
-
// 与 uninstallNode 同规则:unit 未加载(未装/已卸)的无害失败放行;
|
|
71
|
-
// 真失败必须报——否则「已关闭」是谎报,在跑进程仍在外面监听
|
|
72
|
-
if (s.code !== 0 && !/not loaded/.test(s.stderr))
|
|
73
|
-
throw new Error(`systemctl stop 失败:${s.stderr.trim()}`);
|
|
71
|
+
systemd.stopIfLoaded(ctx.runner, unit); // not-loaded(未装/已卸)无害,真失败抛出
|
|
74
72
|
const d = systemd.disableUnit(ctx.runner, unit);
|
|
75
73
|
if (d.code !== 0) throw new Error(`systemctl disable 失败:${d.stderr.trim()}`);
|
|
76
74
|
return 'relay 已关闭(重启后保持关闭)';
|
|
@@ -13,16 +13,22 @@ import {
|
|
|
13
13
|
|
|
14
14
|
const rm = (p) => fs.rmSync(p, { recursive: true, force: true });
|
|
15
15
|
|
|
16
|
+
/** 三角色共同产物:整棵 bin/state/conf.d + 版本戳 + link 意图 marker
|
|
17
|
+
* (marker 只 edge 可能有,其余角色 force rm 不存在的文件无害)。 */
|
|
18
|
+
function removeArtifacts(ctx) {
|
|
19
|
+
rm(ctx.paths.binDir);
|
|
20
|
+
rm(ctx.paths.stateDir);
|
|
21
|
+
rm(ctx.paths.confDir);
|
|
22
|
+
rm(ctx.paths.runtimeVersionFile);
|
|
23
|
+
rm(ctx.paths.linkEnabledFile);
|
|
24
|
+
}
|
|
25
|
+
|
|
16
26
|
function uninstallEdge(ctx) {
|
|
17
27
|
const label = linkLabel(ctx.topo.core.alias);
|
|
18
28
|
removeJob(ctx, label, path.join(
|
|
19
29
|
ctx.paths.launchAgentsDir, plistFileName(label),
|
|
20
30
|
));
|
|
21
|
-
|
|
22
|
-
rm(ctx.paths.stateDir);
|
|
23
|
-
rm(ctx.paths.confDir); // routes(edge 装的);node 上不存在无害
|
|
24
|
-
rm(ctx.paths.runtimeVersionFile);
|
|
25
|
-
rm(ctx.paths.linkEnabledFile); // link 附加件意图
|
|
31
|
+
removeArtifacts(ctx);
|
|
26
32
|
rm(path.join(ctx.paths.logsDir, `${label}.log`));
|
|
27
33
|
return '已卸载。最后一步:npm uninstall -g ssh-net';
|
|
28
34
|
}
|
|
@@ -45,10 +51,7 @@ function uninstallCore(ctx) {
|
|
|
45
51
|
const relay = relayLabel(ctx.topo.relay.alias);
|
|
46
52
|
removeJob(ctx, relay, path.join(dir, plistFileName(relay)));
|
|
47
53
|
|
|
48
|
-
|
|
49
|
-
rm(ctx.paths.stateDir);
|
|
50
|
-
rm(ctx.paths.confDir); // 全部 edge-<别名> 动态片段
|
|
51
|
-
rm(ctx.paths.runtimeVersionFile);
|
|
54
|
+
removeArtifacts(ctx); // 全部 edge-<别名> 动态片段随 conf.d 整棵删
|
|
52
55
|
for (const label of [...edgeJobs.keys(), relay])
|
|
53
56
|
rm(path.join(ctx.paths.logsDir, `${label}.log`));
|
|
54
57
|
return `已卸载(relay 通道与 ${edgeJobs.size} 个 edge 的回拨作业)。最后一步:npm uninstall -g ssh-net`;
|
|
@@ -58,18 +61,13 @@ function uninstallNode(ctx) {
|
|
|
58
61
|
if (ctx.uid !== 0)
|
|
59
62
|
throw new Error('node 卸载写 /etc/systemd/system 需要 root,请以 root 运行');
|
|
60
63
|
const unit = relayUnitName(ctx.topo.relay.alias);
|
|
61
|
-
|
|
62
|
-
//
|
|
63
|
-
|
|
64
|
-
if (s.code !== 0 && !/not loaded/.test(s.stderr))
|
|
65
|
-
throw new Error(`systemctl stop 失败:${s.stderr.trim()}`);
|
|
64
|
+
// not-loaded(未装/已卸)无害,真失败必须报——否则删了 unit 文件、
|
|
65
|
+
// systemd 里留下 ghost 态的在跑进程
|
|
66
|
+
systemd.stopIfLoaded(ctx.runner, unit);
|
|
66
67
|
systemd.disableUnit(ctx.runner, unit); // 已禁无害
|
|
67
68
|
rm(path.join(ctx.paths.systemdUnitDir, unit));
|
|
68
69
|
systemd.daemonReload(ctx.runner);
|
|
69
|
-
|
|
70
|
-
rm(ctx.paths.stateDir);
|
|
71
|
-
rm(ctx.paths.confDir);
|
|
72
|
-
rm(ctx.paths.runtimeVersionFile);
|
|
70
|
+
removeArtifacts(ctx);
|
|
73
71
|
return '已卸载。最后一步:npm uninstall -g ssh-net';
|
|
74
72
|
}
|
|
75
73
|
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// start/stop 共用的命令行参数小助手(edge 的 relay/peer 解析两命令返回
|
|
2
|
+
// 语义不同,各自保留;只收逐字相同的部分)。
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* core/node 是单总开关:无参或显式 -a/--all 收下(等同缺省),其余参数拒绝。
|
|
6
|
+
* @param {string[]} args
|
|
7
|
+
* @param {'start'|'stop'} verb
|
|
8
|
+
*/
|
|
9
|
+
export function assertUnitSwitchArgs(args, verb) {
|
|
10
|
+
if (args.length && !(args.length === 1 && (args[0] === '-a' || args[0] === '--all')))
|
|
11
|
+
throw new Error(`本机是单总开关,${verb} 不带参数(-a/--all 亦可,等同缺省)`);
|
|
12
|
+
}
|
package/libexec/lib/conf.mjs
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
// - sync 幂等:内容字节相同不动盘(routes 每次 install 调,不产生无意义变更)
|
|
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
|
/** 确保 conf.d 存在且 0700(~/.ssh/ssh-net 已由 state 侧建为 0700)。 */
|
|
11
12
|
function ensureConfDir(confDir) {
|
|
@@ -20,21 +21,12 @@ function ensureConfDir(confDir) {
|
|
|
20
21
|
* @returns {boolean} 是否实际重写
|
|
21
22
|
*/
|
|
22
23
|
export function syncConfFile(file, content) {
|
|
23
|
-
|
|
24
|
-
try {
|
|
25
|
-
existing = fs.readFileSync(file, 'utf8');
|
|
26
|
-
} catch {
|
|
27
|
-
existing = null;
|
|
28
|
-
}
|
|
29
|
-
if (existing === content) return false;
|
|
24
|
+
if (readFileOrNull(file) === content) return false;
|
|
30
25
|
|
|
31
26
|
const dir = path.dirname(file);
|
|
32
27
|
ensureConfDir(dir);
|
|
33
28
|
// tmp 落 conf.d 的**上级**目录:glob conf.d/* 扫不到它
|
|
34
|
-
|
|
35
|
-
fs.writeFileSync(tmp, content, { mode: 0o600 });
|
|
36
|
-
fs.renameSync(tmp, file);
|
|
37
|
-
fs.chmodSync(file, 0o600);
|
|
29
|
+
writeFileAtomic(file, content, { mode: 0o600, tmpDir: path.dirname(dir) });
|
|
38
30
|
return true;
|
|
39
31
|
}
|
|
40
32
|
|
package/libexec/lib/config.mjs
CHANGED
|
@@ -35,7 +35,6 @@ const BIND = '127.0.0.1'; // 所有转发强制只绑环回(design §6.5)
|
|
|
35
35
|
* @property {string} user
|
|
36
36
|
*
|
|
37
37
|
* @typedef {Object} Forward
|
|
38
|
-
* @property {string} bind 恒 127.0.0.1
|
|
39
38
|
* @property {number} listenPort
|
|
40
39
|
* @property {string} targetAlias 目标别名:'loopback'=落地机 v4 环回、
|
|
41
40
|
* 'loopback6'=v6 环回(固定 [::1]),其余=主机别名(渲染时查
|
|
@@ -211,7 +210,7 @@ export function parseTopology(text) {
|
|
|
211
210
|
if (target.role === 'edge')
|
|
212
211
|
throw new ConfigError(line, `forward local 目标 ${targetAlias} 是 edge(edge 无注册地址,不能做转发目标)`);
|
|
213
212
|
}
|
|
214
|
-
forwards.push({
|
|
213
|
+
forwards.push({ listenPort, targetAlias, targetPort });
|
|
215
214
|
}
|
|
216
215
|
|
|
217
216
|
return Object.freeze({
|
|
@@ -282,7 +281,7 @@ export function renderForwardArgs(forwards, hostTable) {
|
|
|
282
281
|
throw new Error(`forward local 目标 ${f.targetAlias} 在 ~/.ssh/config 缺 Host 块或 HostName`);
|
|
283
282
|
target = host.hostname;
|
|
284
283
|
}
|
|
285
|
-
args.push('-L',
|
|
284
|
+
args.push('-L', `${BIND}:${f.listenPort}:${target}:${f.targetPort}`);
|
|
286
285
|
}
|
|
287
286
|
return args;
|
|
288
287
|
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// 文件系统小原语:原子落盘与「缺失即 null」读取。manage/state/runtime/conf
|
|
2
|
+
// 等所有落盘方共用这一份,不各自抄 tmp+rename+chmod。
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 读文件;不存在返回 null(仅吞 ENOENT,其余错误照抛)。
|
|
8
|
+
* @param {string} file
|
|
9
|
+
* @param {BufferEncoding|null} [encoding] 缺省 utf8;传 null 读 Buffer
|
|
10
|
+
* @returns {string|Buffer|null}
|
|
11
|
+
*/
|
|
12
|
+
export function readFileOrNull(file, encoding = 'utf8') {
|
|
13
|
+
try {
|
|
14
|
+
return fs.readFileSync(file, encoding === null ? undefined : encoding);
|
|
15
|
+
} catch (err) {
|
|
16
|
+
if (err && err.code === 'ENOENT') return null;
|
|
17
|
+
throw err;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 原子落盘:tmp 写好再 rename 落定,落定后 chmod 兜底(怪 umask/拷贝工具
|
|
23
|
+
* 吃位都修正到 mode)。
|
|
24
|
+
* @param {string} file
|
|
25
|
+
* @param {string|Buffer} content
|
|
26
|
+
* @param {{mode?: number, tmpDir?: string}} [opts] tmpDir 给 Include 片段用:
|
|
27
|
+
* tmp 必须写在 glob 目录之外(conf.d/* 会被 ssh 扫到)
|
|
28
|
+
*/
|
|
29
|
+
export function writeFileAtomic(file, content, { mode = 0o644, tmpDir = path.dirname(file) } = {}) {
|
|
30
|
+
const tmp = path.join(tmpDir, `.${path.basename(file)}.tmp-${process.pid}`);
|
|
31
|
+
fs.writeFileSync(tmp, content, { mode });
|
|
32
|
+
fs.renameSync(tmp, file);
|
|
33
|
+
fs.chmodSync(file, mode);
|
|
34
|
+
}
|
package/libexec/lib/ipv6.mjs
CHANGED
|
@@ -8,20 +8,21 @@
|
|
|
8
8
|
import net from 'node:net';
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
|
-
*
|
|
11
|
+
* 分类一个候选 v6:合法(无 scope、非内嵌 v4 的 IPv6 字面值)给首 hextet
|
|
12
|
+
* 数值;否则给中文拒收原因。isDialableV6 与 explainV6Error 的唯一共同来源,
|
|
13
|
+
* 拒收规则不抄第二份。
|
|
12
14
|
* @param {unknown} value
|
|
13
|
-
* @returns {number
|
|
15
|
+
* @returns {{g0:number}|{reason:string}}
|
|
14
16
|
*/
|
|
15
|
-
function
|
|
16
|
-
if (typeof value !== 'string') return
|
|
17
|
-
if (value.
|
|
18
|
-
if (net.isIP(value) !== 6) return
|
|
17
|
+
function classifyV6(value) {
|
|
18
|
+
if (typeof value !== 'string' || value.length === 0) return { reason: 'v6 为空' };
|
|
19
|
+
if (value.includes('%')) return { reason: 'v6 带 scope id,拒绝' };
|
|
20
|
+
if (net.isIP(value) !== 6) return { reason: '不是合法 IPv6 字面值' };
|
|
19
21
|
// 任何内嵌 v4 形态(末段含点,如 ::ffff:192.0.2.1)一律拒
|
|
20
|
-
|
|
21
|
-
|
|
22
|
+
if (value.slice(value.lastIndexOf(':') + 1).includes('.'))
|
|
23
|
+
return { reason: '拒绝内嵌 v4(含 ::ffff: v4-mapped)' };
|
|
22
24
|
const firstGroup = value.split(':')[0];
|
|
23
|
-
|
|
24
|
-
return Number.isInteger(g0) ? g0 : null;
|
|
25
|
+
return { g0: firstGroup === '' ? 0 : Number.parseInt(firstGroup, 16) };
|
|
25
26
|
}
|
|
26
27
|
|
|
27
28
|
/**
|
|
@@ -30,23 +31,19 @@ function firstHextetOf(value) {
|
|
|
30
31
|
* @returns {boolean}
|
|
31
32
|
*/
|
|
32
33
|
export function isDialableV6(value) {
|
|
33
|
-
const
|
|
34
|
-
return g0
|
|
35
|
-
&& ((g0 >= 0x2000 && g0 <= 0x3fff) || (g0 >= 0xfc00 && g0 <= 0xfdff));
|
|
34
|
+
const c = classifyV6(value);
|
|
35
|
+
return 'g0' in c
|
|
36
|
+
&& ((c.g0 >= 0x2000 && c.g0 <= 0x3fff) || (c.g0 >= 0xfc00 && c.g0 <= 0xfdff));
|
|
36
37
|
}
|
|
37
38
|
|
|
38
39
|
/**
|
|
39
|
-
* 给报名 RPC
|
|
40
|
+
* 给报名 RPC 报错用的中文原因;合法可拨号返回 null。
|
|
40
41
|
* @param {unknown} value
|
|
41
42
|
* @returns {string | null}
|
|
42
43
|
*/
|
|
43
44
|
export function explainV6Error(value) {
|
|
44
|
-
|
|
45
|
-
if (
|
|
46
|
-
if (net.isIP(value) !== 6) return '不是合法 IPv6 字面值';
|
|
47
|
-
if (value.slice(value.lastIndexOf(':') + 1).includes('.'))
|
|
48
|
-
return '拒绝内嵌 v4(含 ::ffff: v4-mapped)';
|
|
49
|
-
// 可拨号判定单一来源是 isDialableV6,别在这抄第二份
|
|
45
|
+
const c = classifyV6(value);
|
|
46
|
+
if ('reason' in c) return c.reason;
|
|
50
47
|
if (!isDialableV6(value))
|
|
51
48
|
return '不是可拨号 v6(收 2000::/3 公网单播与 fc00::/7 ULA,拒 link-local/组播/保留段)';
|
|
52
49
|
return null;
|