ssh-net 26.10.45 → 26.10.46
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 +49 -3
- package/libexec/commands/doctor.mjs +3 -3
- package/libexec/commands/install.mjs +69 -80
- package/libexec/commands/register.mjs +34 -112
- package/libexec/commands/restart.mjs +1 -1
- package/libexec/commands/start.mjs +5 -7
- package/libexec/commands/status.mjs +155 -215
- package/libexec/commands/stop.mjs +8 -9
- package/libexec/commands/uninstall.mjs +1 -1
- package/libexec/commands/update.mjs +7 -10
- package/libexec/lib/conf.mjs +1 -6
- package/libexec/lib/constants.mjs +26 -0
- package/libexec/lib/identity.mjs +1 -1
- package/libexec/lib/ipv6.mjs +1 -1
- package/libexec/lib/jobctl.mjs +11 -16
- package/libexec/lib/launchd.mjs +5 -6
- package/libexec/lib/manage.mjs +1 -1
- package/libexec/lib/paths.mjs +20 -20
- package/libexec/lib/probe.mjs +7 -50
- package/libexec/lib/render.mjs +57 -29
- package/libexec/lib/runtime.mjs +11 -16
- package/libexec/lib/sshconfig.mjs +2 -2
- package/libexec/lib/state.mjs +50 -16
- package/libexec/lib/systemd.mjs +26 -16
- package/libexec/sn-probe +11 -17
- package/libexec/sn-route +15 -20
- package/libexec/ssh-net-link-core +6 -11
- package/libexec/ssh-net-uplink-edge +27 -50
- package/libexec/ssh-net-uplink-relay +5 -6
- package/package.json +1 -1
- package/libexec/lib/registration.mjs +0 -47
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
// status
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
// bin/ 体检(2026-09-30 软链方案):实体逐字节比对 + 服务软链指向/悬空检查。
|
|
8
|
-
// 退出码:健康(含 edge 关闭态)0;有可行动问题 1。
|
|
1
|
+
// status:只报本机角色,三类信息——开关(意图)、线路就绪(实测)、安装产物
|
|
2
|
+
// 一致性(bin/ 实体逐字节、服务软链、版本戳、edge 的 routes、core/node 的 edge
|
|
3
|
+
// 入口片段)。core/node 另列每台 edge 当前的 ssh 入口地址。开关关的线路
|
|
4
|
+
// 报「关」不探测。relay 就绪 = 命令行 -J 经 relay 真跑一条 ssh,不从「probe
|
|
5
|
+
// 不通」推断。collect 返回结构化对象(测试断言对象),renderText 给人看。
|
|
6
|
+
// 退出码:健康(含 edge 全关态)0;有可行动问题 1。
|
|
9
7
|
import fs from 'node:fs';
|
|
10
8
|
import {
|
|
11
9
|
auditScript, auditLink, runtimeVersionIssue,
|
|
@@ -15,35 +13,22 @@ import * as launchd from '../lib/launchd.mjs';
|
|
|
15
13
|
import * as systemd from '../lib/systemd.mjs';
|
|
16
14
|
import { peerProbeOk, tcpOpen, classifyV6Probe } from '../lib/probe.mjs';
|
|
17
15
|
import { readFileOrNull } from '../lib/fsutil.mjs';
|
|
16
|
+
import { LAN_ADDR_RE } from '../lib/constants.mjs';
|
|
18
17
|
import {
|
|
19
18
|
SN_ROUTE, SN_IPV6, UPLINK_EDGE,
|
|
20
19
|
edgeRuntimePlan, relayRuntimePlan, edgeCallbacksPlan,
|
|
21
20
|
} from '../lib/manifest.mjs';
|
|
22
|
-
import { edgeFragmentContent, edgeFragmentShouldExist } from '../lib/registration.mjs';
|
|
23
21
|
import {
|
|
24
|
-
linkLabel, relayLabel, relayUnitName,
|
|
25
|
-
|
|
26
|
-
renderRoutes, coreViaRelayArgs, probeSshOpts,
|
|
22
|
+
linkLabel, relayLabel, relayUnitName, edgeLabel,
|
|
23
|
+
renderEdgeHost, renderRoutes, coreViaRelayArgs, probeSshOpts,
|
|
27
24
|
} from '../lib/render.mjs';
|
|
28
25
|
|
|
29
|
-
/**
|
|
30
|
-
* 读 conf.d 片段并与期望内容比对。
|
|
31
|
-
* @returns {'ok'|'stale'|'missing'}
|
|
32
|
-
*/
|
|
33
|
-
function auditConfFragment(file, expected) {
|
|
34
|
-
const text = readFileOrNull(file);
|
|
35
|
-
if (text === null) return 'missing';
|
|
36
|
-
return text === expected ? 'ok' : 'stale';
|
|
37
|
-
}
|
|
38
|
-
|
|
39
26
|
/**
|
|
40
27
|
* 各角色共用的实体+软链体检。
|
|
41
28
|
* @returns {{issues:string[], entityAudits:{name:string,audit:string}[]}}
|
|
42
|
-
* issues 是人话问题列表(空=健康)
|
|
43
29
|
*/
|
|
44
30
|
function runtimeIssues(ctx, { entities, links }) {
|
|
45
31
|
const issues = [];
|
|
46
|
-
// 实体逐字节比对(name 是 bin 实体名,不是软链)
|
|
47
32
|
const entityAudits = entities.map(([name, script]) => ({
|
|
48
33
|
name,
|
|
49
34
|
audit: auditScript(ctx.paths.binScript(name), packageScriptPath(script)),
|
|
@@ -73,17 +58,58 @@ function baseAudit(ctx, plan) {
|
|
|
73
58
|
return { issues, entityAudits };
|
|
74
59
|
}
|
|
75
60
|
|
|
61
|
+
/** conf.d 片段(install 生成物)与期望内容比对,返回问题文案(''=健康):
|
|
62
|
+
* routes 与 edge 入口两处体检同一口径。 */
|
|
63
|
+
function confFragmentIssue(file, expected, label) {
|
|
64
|
+
const text = readFileOrNull(file);
|
|
65
|
+
return text === expected
|
|
66
|
+
? ''
|
|
67
|
+
: `${label}${text === null ? '缺失' : '与 topology 不一致'}——跑一次 ssh-net install 刷新`;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** core/node:各 edge 入口片段与渲染期望比对(install 的产物),并取当前入口
|
|
71
|
+
* 地址。缺失/过期会让 `ssh <别名>` 解析失败或用错 user,引导跑 install。 */
|
|
72
|
+
function edgeEntries(ctx, issues) {
|
|
73
|
+
return ctx.topo.edges.map((e) => {
|
|
74
|
+
const issue = confFragmentIssue(
|
|
75
|
+
ctx.paths.edgeHostFile(e.alias), renderEdgeHost(e), `${e.alias} 的 ssh 入口配置`,
|
|
76
|
+
);
|
|
77
|
+
if (issue) issues.push(issue);
|
|
78
|
+
return { alias: e.alias, ...ctx.state.readEdgeEntry(e.alias) };
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** 分钟数的人话:刚刚 / N 分钟 / N 小时 / N 天。 */
|
|
83
|
+
function ageText(min) {
|
|
84
|
+
if (min < 1) return '刚刚';
|
|
85
|
+
if (min < 60) return `${min} 分钟前`;
|
|
86
|
+
if (min < 48 * 60) return `${Math.floor(min / 60)} 小时前`;
|
|
87
|
+
return `${Math.floor(min / 1440)} 天前`;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** 入口地址的人话:10.x 只会是 rc 登记的内网直连地址(网段判据单源在
|
|
91
|
+
* constants.LAN_ADDR_RE),v6 是中继报名的公网地址,其余 v4 按字面标 IPv4
|
|
92
|
+
* 不替 rc 的网段策略背书;过期地址入口已不采用(ssh 会报找不到主机),
|
|
93
|
+
* 如实标出。 */
|
|
94
|
+
function addrNote({ addr, ageMin, fresh }) {
|
|
95
|
+
if (!addr) return 'ssh 入口:还没有地址(等它连过来)';
|
|
96
|
+
const kind = addr.includes(':') ? '公网 v6' : LAN_ADDR_RE.test(addr) ? '内网' : 'IPv4';
|
|
97
|
+
const age = ageMin === null ? '' : `,${ageText(ageMin)}`;
|
|
98
|
+
return fresh
|
|
99
|
+
? `ssh 入口:${addr}(${kind}${age})`
|
|
100
|
+
: `ssh 入口:已过期(上次 ${addr},${kind}${age};等它下次连过来)`;
|
|
101
|
+
}
|
|
102
|
+
|
|
76
103
|
/** forward 的人话标签:写出目标(loopback 落在出口机本机)。 */
|
|
77
104
|
function forwardLabel(f) {
|
|
78
|
-
const target = f.targetAlias === 'loopback'
|
|
105
|
+
const target = f.targetAlias === 'loopback' || f.targetAlias === 'loopback6'
|
|
79
106
|
? `出口机本机:${f.targetPort}`
|
|
80
107
|
: `${f.targetAlias}:${f.targetPort}`;
|
|
81
108
|
return `端口转发 127.0.0.1:${f.listenPort} → ${target}`;
|
|
82
109
|
}
|
|
83
110
|
|
|
84
|
-
/** relay 口没在监听时的 issue(core/node
|
|
85
|
-
* 把 relay
|
|
86
|
-
* 完全不同。 */
|
|
111
|
+
/** relay 口没在监听时的 issue(core/node 同款):不可达与没监听分开报——
|
|
112
|
+
* 把 relay 不可达说成「没在监听」会把故障点错指向本机。 */
|
|
87
113
|
function relayPortIssue(relayAlias, port, r) {
|
|
88
114
|
return r.reachable === false
|
|
89
115
|
? `连不上 ${relayAlias}(${r.detail || 'ssh 探测失败'}),入口 ${port} 状态未知——查 ${relayAlias} 可达性或本机网络`
|
|
@@ -91,9 +117,7 @@ function relayPortIssue(relayAlias, port, r) {
|
|
|
91
117
|
}
|
|
92
118
|
|
|
93
119
|
async function relayPortListening(ctx, port) {
|
|
94
|
-
// relay 别名的 User/HostName 由 ssh config Host
|
|
95
|
-
//(BatchMode/IdentitiesOnly/ControlMaster)单源在 render.probeSshOpts,
|
|
96
|
-
// 与 coreViaRelayArgs 同一份(运行时五个 bash ssh 则由 constants.test 锚)
|
|
120
|
+
// relay 别名的 User/HostName 由 ssh config Host 块提供
|
|
97
121
|
const r = await ctx.runner.runAsync('ssh', [
|
|
98
122
|
...probeSshOpts(5),
|
|
99
123
|
ctx.topo.relay.alias, 'ss', '-tlnH',
|
|
@@ -102,10 +126,9 @@ async function relayPortListening(ctx, port) {
|
|
|
102
126
|
return { port, reachable: true, listening: new RegExp(`127\\.0\\.0\\.1[:.]${port}\\b`).test(r.stdout) };
|
|
103
127
|
}
|
|
104
128
|
|
|
105
|
-
/** edge 的 relay 线路实测:命令行显式 -J 经 relay 跑一条真 ssh
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
* accept-new 是刻意分级(design §4.3)。 */
|
|
129
|
+
/** edge 的 relay 线路实测:命令行显式 -J 经 relay 跑一条真 ssh(字面
|
|
130
|
+
* 127.0.0.1 不命中 Match,绕开自动选路)。探测不接受新 host key——与无人
|
|
131
|
+
* 值守报名的 accept-new 是刻意分级(design §4.3)。 */
|
|
109
132
|
async function relayPathOk(ctx) {
|
|
110
133
|
const r = await ctx.runner.runAsync(
|
|
111
134
|
'ssh',
|
|
@@ -117,98 +140,58 @@ async function relayPathOk(ctx) {
|
|
|
117
140
|
|
|
118
141
|
async function collectEdge(ctx) {
|
|
119
142
|
const { topo, paths, state } = ctx;
|
|
120
|
-
// link 附加件以 marker 为准:默认只有 3 实体、无服务软链;装了才加
|
|
121
|
-
// link-core(转发口只取决于装没装,不是运行时开关)
|
|
122
143
|
const linkInstalled = fs.existsSync(paths.linkEnabledFile);
|
|
123
144
|
const plan = edgeRuntimePlan(topo, linkInstalled);
|
|
124
145
|
const { issues, entityAudits } = baseAudit(ctx, plan);
|
|
125
146
|
|
|
126
|
-
// routes
|
|
127
|
-
//
|
|
128
|
-
// 的 VS Code 打标块同属这份内容,逐字节对账一并覆盖,不用单独体检
|
|
147
|
+
// routes 被手删/过期会让 ssh 静默退回 lan 直连,是故障。sn-route 没装时
|
|
148
|
+
// 上面已报「尚未安装」,不重复
|
|
129
149
|
if (entityAudits.some((a) => a.name === SN_ROUTE && a.audit === 'ok')) {
|
|
130
|
-
const
|
|
131
|
-
|
|
132
|
-
|
|
150
|
+
const issue = confFragmentIssue(
|
|
151
|
+
paths.routesFile, renderRoutes(topo, ctx.self.alias), 'ssh 配置 routes ',
|
|
152
|
+
);
|
|
153
|
+
if (issue) issues.push(issue);
|
|
133
154
|
}
|
|
134
155
|
|
|
135
|
-
//
|
|
136
|
-
|
|
137
|
-
const job =
|
|
156
|
+
// link 未装时 job 无消费者(渲染走「未安装」分支)——默认零常驻形态不为它
|
|
157
|
+
// 白跑一次 launchctl print
|
|
158
|
+
const job = linkInstalled
|
|
159
|
+
? launchd.printState(ctx.runner, ctx.uid, linkLabel(topo.core.alias))
|
|
160
|
+
: null;
|
|
138
161
|
const switches = state.readSwitches();
|
|
139
162
|
|
|
140
|
-
//
|
|
141
|
-
//
|
|
142
|
-
|
|
143
|
-
if (switches.peer && !switches.relay)
|
|
144
|
-
issues.push('开关组合异常:peer 开但 relay 关(peer 依赖 relay,多半是开关文件被手动改动)——跑一次 ssh-net stop 后再按需要 start');
|
|
145
|
-
|
|
146
|
-
// v6 探测与下面的就绪实测互不依赖:同一批并发(5s sn-ipv6 不再串行白等在
|
|
147
|
-
// 8s relay 探测之后)。全关(在办公室)时不跑。v6 只是诊断线索不是判决:
|
|
148
|
-
// 探测没跑成(超时/非零退出/实体缺失)时不能谎称「没有 IPv6」——那个
|
|
149
|
-
// 结论只在 sn-ipv6 正常跑完且无输出时才成立
|
|
150
|
-
const needV6 = (switches.relay || switches.peer)
|
|
163
|
+
// v6 只是诊断线索不是判决,与就绪实测并发。探测没跑成时不能谎称「没有
|
|
164
|
+
// IPv6」——那个结论只在 sn-ipv6 正常跑完且无输出时才成立
|
|
165
|
+
const needV6 = switches.relay
|
|
151
166
|
&& entityAudits.some((a) => a.name === SN_IPV6 && a.audit === 'ok');
|
|
152
167
|
const v6P = needV6
|
|
153
168
|
? ctx.runner.runRawAsync(paths.binScript(SN_IPV6), [], { timeout: 5000 })
|
|
154
169
|
: Promise.resolve(null);
|
|
155
170
|
|
|
156
|
-
//
|
|
157
|
-
// 转发口只有装了 link
|
|
171
|
+
// 就绪实测并发(开关是意图、ready 是事实;开关关的线路不探测=null)。
|
|
172
|
+
// 转发口只有装了 link 且 relay 开才可能在听
|
|
158
173
|
const relayOk = ctx.relayOk ?? relayPathOk; // 测试可注入
|
|
159
174
|
const openTcp = ctx.openTcp ?? tcpOpen;
|
|
160
|
-
const
|
|
175
|
+
const forwards = linkInstalled && switches.relay ? topo.forwards : [];
|
|
176
|
+
const [relayReady, peerReady, portsOk, v6Probe] = await Promise.all([
|
|
161
177
|
switches.relay ? relayOk(ctx) : null,
|
|
162
178
|
switches.relay && switches.peer ? peerProbeOk(ctx.runner, paths.binDir) : null,
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
// relay 开但 core 的 LAN 22 可达 = 人在办公室忘了关(只提示不纠偏)。
|
|
167
|
-
// core 的 LAN IP 不进 topology,从 ~/.ssh/config 的静态 Host 块取(契约校验
|
|
168
|
-
// 已保证存在),不读 routes——Match 会把地址改写成环回口
|
|
169
|
-
const coreLanIp = ctx.hostTable.get(topo.core.alias)?.hostname;
|
|
170
|
-
const lanIdx = switches.relay && coreLanIp ? probes.length : -1;
|
|
171
|
-
if (lanIdx >= 0) probes.push(openTcp(coreLanIp, 22, 1200));
|
|
172
|
-
const [got, v6Probe] = await Promise.all([Promise.all(probes), v6P]);
|
|
173
|
-
const relayReady = got[0];
|
|
174
|
-
const peerReady = got[1];
|
|
175
|
-
const ports = {};
|
|
176
|
-
if (linkInstalled)
|
|
177
|
-
topo.forwards.forEach((f, i) => { ports[f.listenPort] = got[2 + i]; });
|
|
178
|
-
const lanReachable = lanIdx >= 0 ? got[lanIdx] : false;
|
|
179
|
+
Promise.all(forwards.map((f) => openTcp('127.0.0.1', f.listenPort))),
|
|
180
|
+
v6P,
|
|
181
|
+
]);
|
|
179
182
|
|
|
180
183
|
let v6 = '';
|
|
181
184
|
let v6ProbeOk = false;
|
|
182
185
|
if (v6Probe) {
|
|
183
|
-
// 三态口径单源在 classifyV6Probe(与 register 报名共用):v6ProbeOk
|
|
184
|
-
// 的语义是「探测正常跑完」(含「确认无 v6」),不是「有 v6」
|
|
185
186
|
const c = classifyV6Probe(v6Probe);
|
|
186
187
|
v6 = c.v6;
|
|
187
188
|
v6ProbeOk = c.state !== 'failed';
|
|
188
189
|
}
|
|
189
190
|
|
|
190
|
-
if (lanReachable)
|
|
191
|
-
issues.push(`检测到 ${topo.core.alias}(${coreLanIp})局域网直连可达——隧道可能在空转绕中继;人在办公室可 ssh-net stop`);
|
|
192
|
-
|
|
193
|
-
// relay 关但转发口仍在听 = 孤儿进程(仅装了 link 才可能)
|
|
194
|
-
if (!switches.relay && linkInstalled)
|
|
195
|
-
for (const f of topo.forwards)
|
|
196
|
-
if (ports[f.listenPort])
|
|
197
|
-
issues.push(`隧道已关但 ${forwardLabel(f)} 仍被占用——疑似残留进程:lsof -nP -iTCP:${f.listenPort} -sTCP:LISTEN 查占用者`);
|
|
198
|
-
|
|
199
|
-
// loopback 转发的出口在 core 本机环回(microsocks),ssh-net 管辖外:
|
|
200
|
-
// 未就绪给根因提示,别让排障只在 edge 侧打转
|
|
201
|
-
if (linkInstalled)
|
|
202
|
-
for (const f of topo.forwards)
|
|
203
|
-
if (f.targetAlias === 'loopback' && !ports[f.listenPort])
|
|
204
|
-
issues.push(`${forwardLabel(f)} 未就绪:edge 侧正常时多半是 core 上的出口代理(microsocks)没跑——到 core 上查 local.microsocks`);
|
|
205
|
-
|
|
206
|
-
const forwardViews = linkInstalled
|
|
207
|
-
? topo.forwards.map((f) => [forwardLabel(f), ports[f.listenPort]])
|
|
208
|
-
: [];
|
|
209
191
|
return {
|
|
210
192
|
role: 'edge', alias: ctx.self.alias, switches, relayReady, peerReady,
|
|
211
|
-
linkInstalled, job,
|
|
193
|
+
linkInstalled, job,
|
|
194
|
+
forwardViews: forwards.map((f, i) => [forwardLabel(f), portsOk[i]]),
|
|
212
195
|
coreAlias: topo.core.alias, relayAlias: topo.relay.alias,
|
|
213
196
|
v6, v6ProbeOk, issues,
|
|
214
197
|
};
|
|
@@ -220,23 +203,22 @@ async function collectCore(ctx) {
|
|
|
220
203
|
|
|
221
204
|
// disabled override 是整域同一份字典:relay job 与各 edge job 共用一次拉取
|
|
222
205
|
const dmap = launchd.disabledMap(ctx.runner, ctx.uid);
|
|
223
|
-
const
|
|
224
|
-
|
|
225
|
-
// relay 口实测是 8s 超时的真 ssh:先发起来,边等边做下面的注册态体检
|
|
206
|
+
const relayJob = launchd.inspectWith(ctx.runner, ctx.uid, relayLabel(topo.relay.alias), dmap);
|
|
207
|
+
// relay 口实测是 8s 超时的真 ssh:先发起来,边等边做注册态体检
|
|
226
208
|
const relayPortP = relayPortListening(ctx, topo.core.relayPort);
|
|
227
209
|
|
|
228
|
-
//
|
|
229
|
-
// 一次,各已注册 edge 只检各自的带别名软链。topology 无 edge 时不审
|
|
210
|
+
// 回拨实体全 core 一份:循环外体检一次,各已注册 edge 只检各自的软链
|
|
230
211
|
const callbackEntityAudit = topo.edges.length > 0
|
|
231
212
|
? auditScript(paths.binScript(UPLINK_EDGE), packageScriptPath(UPLINK_EDGE))
|
|
232
213
|
: null;
|
|
233
214
|
|
|
215
|
+
// 按别名关联入口(entry 自带 alias):位置对位会把对齐不变式藏在另一个
|
|
216
|
+
// 函数的迭代顺序里
|
|
217
|
+
const entryByAlias = new Map(edgeEntries(ctx, issues).map((en) => [en.alias, en]));
|
|
234
218
|
const edges = [];
|
|
235
219
|
for (const e of topo.edges) {
|
|
236
|
-
const
|
|
237
|
-
|
|
238
|
-
// 已连接过的随身机,回拨组件一并体检:回拨脚本/软链只有 _register
|
|
239
|
-
// (报名)这条刷新通道,漏刷会拿旧脚本拨号
|
|
220
|
+
const st = launchd.inspectWith(ctx.runner, ctx.uid, edgeLabel(e.alias), dmap);
|
|
221
|
+
// 已连接过的 edge 体检回拨脚本/软链:漏刷会拿旧脚本拨号
|
|
240
222
|
if (st.registered) {
|
|
241
223
|
const [linkName, entityName] = edgeCallbacksPlan([e.alias]).links[0];
|
|
242
224
|
const bad = [];
|
|
@@ -245,30 +227,16 @@ async function collectCore(ctx) {
|
|
|
245
227
|
const linkState = auditLink(paths.binScript(linkName), entityName);
|
|
246
228
|
if (linkState !== 'ok') bad.push(`软链(${linkState})`);
|
|
247
229
|
if (bad.length)
|
|
248
|
-
// 已注册 edge 的实体/软链其实会被 install 刷新(26.9.19 起),但
|
|
249
|
-
// 报名是自愈通道且不用人手动跑——按「等下次来连」引导,想立即修
|
|
250
|
-
// 也可跑 ssh-net install
|
|
251
230
|
issues.push(`${e.alias} 的自动连接脚本异常(${bad.join('、')})——它下次连过来时会自动刷新(或跑一次 ssh-net install 手动修复)`);
|
|
252
231
|
}
|
|
253
|
-
const v6 = ctx.state.readEdgeV6(e.alias);
|
|
254
|
-
// edge 入口片段体检:job 在册且未休眠(enabled)+ 有 v6 时,片段必须
|
|
255
|
-
// 与 state 一致——缺失(手删/磁盘丢失)或过期(短名/user 变更后没
|
|
256
|
-
// install)都会让 `ssh <别名>` 失败或解析错。休眠(disabled)时片段
|
|
257
|
-
// 本就该删(拨号失败收尸),不报;未注册无 v6 也不报
|
|
258
|
-
let fragmentAudit = null;
|
|
259
|
-
if (edgeFragmentShouldExist(st, v6)) {
|
|
260
|
-
fragmentAudit = auditConfFragment(paths.edgeHostFile(e.alias), edgeFragmentContent(e, v6));
|
|
261
|
-
if (fragmentAudit !== 'ok')
|
|
262
|
-
issues.push(`${e.alias} 的 ssh 入口配置${fragmentAudit === 'missing' ? '缺失' : '过期'}(它有地址记录、连接任务未休眠)——跑一次 ssh-net install 重建`);
|
|
263
|
-
}
|
|
264
232
|
edges.push({
|
|
265
233
|
alias: e.alias,
|
|
266
234
|
registered: st.registered,
|
|
267
235
|
disabled: st.disabled,
|
|
268
236
|
running: st.running,
|
|
269
237
|
lastExitCode: st.lastExitCode,
|
|
270
|
-
v6,
|
|
271
|
-
|
|
238
|
+
v6: ctx.state.readEdgeV6(e.alias),
|
|
239
|
+
entry: entryByAlias.get(e.alias),
|
|
272
240
|
});
|
|
273
241
|
}
|
|
274
242
|
|
|
@@ -277,15 +245,6 @@ async function collectCore(ctx) {
|
|
|
277
245
|
if (!relayPort.listening)
|
|
278
246
|
issues.push(relayPortIssue(topo.relay.alias, topo.core.relayPort, relayPort));
|
|
279
247
|
|
|
280
|
-
// topology 已删(edge 改名/退役)但磁盘上还有回拨 job:不渲染成 edges 行
|
|
281
|
-
//(state/conf.d 都按新别名走),单独点破。残留无害——拨号失败后自休眠、
|
|
282
|
-
// 端口争抢由胜者持有且监听功能等价——但不可见会让改名现场少一条线索
|
|
283
|
-
const knownAliases = new Set(topo.edges.map((e) => e.alias));
|
|
284
|
-
for (const alias of launchd.edgeAliasesOnDisk(paths.launchAgentsDir)) {
|
|
285
|
-
if (!knownAliases.has(alias))
|
|
286
|
-
issues.push(`${alias} 的自动连接任务还在但 topology 已没有这个 edge(改名/退役残留,无害)——清理:launchctl bootout gui/${ctx.uid}/${edgeLabel(alias)} 后删该 plist;或无视(它拨号失败后会自休眠)`);
|
|
287
|
-
}
|
|
288
|
-
|
|
289
248
|
return {
|
|
290
249
|
role: 'core', alias: ctx.self.alias,
|
|
291
250
|
relayAlias: topo.relay.alias, relayJob, relayPort, edges, issues,
|
|
@@ -295,6 +254,7 @@ async function collectCore(ctx) {
|
|
|
295
254
|
async function collectNode(ctx) {
|
|
296
255
|
const { topo } = ctx;
|
|
297
256
|
const { issues } = baseAudit(ctx, relayRuntimePlan(topo));
|
|
257
|
+
const entries = edgeEntries(ctx, issues);
|
|
298
258
|
|
|
299
259
|
const unit = relayUnitName(topo.relay.alias);
|
|
300
260
|
const active = systemd.isActive(ctx.runner, unit);
|
|
@@ -306,7 +266,7 @@ async function collectNode(ctx) {
|
|
|
306
266
|
|
|
307
267
|
return {
|
|
308
268
|
role: 'node', alias: ctx.self.alias,
|
|
309
|
-
relayAlias: topo.relay.alias, active, relayPort, issues,
|
|
269
|
+
relayAlias: topo.relay.alias, active, relayPort, entries, issues,
|
|
310
270
|
};
|
|
311
271
|
}
|
|
312
272
|
|
|
@@ -320,23 +280,13 @@ export async function collect(ctx) {
|
|
|
320
280
|
/** 上次退出码非 0 才值得看;0 与从未退出都不显示。 */
|
|
321
281
|
const exitNote = (code) => (code && code !== '0' ? `(上次退出 ${code})` : '');
|
|
322
282
|
|
|
323
|
-
/** 线路行的统一形状:✓
|
|
324
|
-
function lineOf(name,
|
|
325
|
-
if (!on) return ` ${name}: 关(${offText})`;
|
|
283
|
+
/** 线路行的统一形状:✓ 就绪 / ✗ 未就绪。「关」只有 peer 有,由调用方自写。 */
|
|
284
|
+
function lineOf(name, ready, okText, failText) {
|
|
326
285
|
return ready
|
|
327
286
|
? ` ${name}: ✓ ${okText}`
|
|
328
287
|
: ` ${name}: ✗ ${failText}`;
|
|
329
288
|
}
|
|
330
289
|
|
|
331
|
-
/** 开关+附加件 → 一行人话总结(回答「我现在是什么形态」)。 */
|
|
332
|
-
function profileOf(sw, linkInstalled) {
|
|
333
|
-
if (!sw.relay) return null; // 全关有独立文案,不加形态行
|
|
334
|
-
if (sw.relay && sw.peer && linkInstalled) return '全功能:core 能主动连过来,本地转发口也开';
|
|
335
|
-
if (sw.relay && sw.peer) return '可被连接:core 会主动连过来,无本地转发口';
|
|
336
|
-
if (sw.relay && linkInstalled) return '仅本地转发:经中继出去,core 不主动连';
|
|
337
|
-
return '仅中继待命(不主动联系 core、无本地转发口)';
|
|
338
|
-
}
|
|
339
|
-
|
|
340
290
|
/** core/node 共用的 relay 状态行。 */
|
|
341
291
|
function relayStatusLine(s) {
|
|
342
292
|
return s.relayPort.listening
|
|
@@ -344,81 +294,71 @@ function relayStatusLine(s) {
|
|
|
344
294
|
: ` relay: ✗ ${s.relayPort.reachable === false ? `连不上 ${s.relayAlias},入口状态未知` : `入口 ${s.relayPort.port} 没在监听`}`;
|
|
345
295
|
}
|
|
346
296
|
|
|
347
|
-
|
|
348
|
-
const
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
'按开关'));
|
|
365
|
-
// peer/link 依赖 relay:relay 关时一并显示关(手动 touch 出的非法定态)
|
|
366
|
-
const peerUnready = !s.v6ProbeOk
|
|
367
|
-
? '未就绪(本机 v6 探测没跑成:sn-ipv6 超时或异常,直连状态未知;中继不受影响)'
|
|
368
|
-
: s.v6 === ''
|
|
369
|
-
? '未就绪(本网络没有 IPv6,直连建不起来;中继不受影响)'
|
|
370
|
-
: '未就绪(等 core 连过来;ssh-net doctor 立即重试,中继不受影响)';
|
|
371
|
-
lines.push(lineOf('peer', sw.relay && sw.peer, s.peerReady,
|
|
297
|
+
function renderEdgeLines(s, lines) {
|
|
298
|
+
const sw = s.switches;
|
|
299
|
+
if (!sw.relay) {
|
|
300
|
+
lines.push(' 开关: 全关(在办公室,局域网直连即可;需要时 ssh-net start)');
|
|
301
|
+
} else {
|
|
302
|
+
lines.push(` 开关: relay 开 · peer ${sw.peer ? '开' : '关'}`);
|
|
303
|
+
lines.push(lineOf('relay', s.relayReady,
|
|
304
|
+
`可用(经 ${s.relayAlias} 连到 ${s.coreAlias})`,
|
|
305
|
+
`不可用(经 ${s.relayAlias} 连不上 ${s.coreAlias};刚开启请等几秒再看,持续出现查 ${s.relayAlias} 或 core 侧 ssh-net status)`));
|
|
306
|
+
const peerUnready = !s.v6ProbeOk
|
|
307
|
+
? '未就绪(本机 v6 探测没跑成,直连状态未知;中继不受影响)'
|
|
308
|
+
: s.v6 === ''
|
|
309
|
+
? '未就绪(本网络没有 IPv6,直连建不起来;中继不受影响)'
|
|
310
|
+
: '未就绪(等 core 连过来;ssh-net doctor 立即重试,中继不受影响)';
|
|
311
|
+
lines.push(!sw.peer
|
|
312
|
+
? ' peer: 关(不通知 core,它不会连过来)'
|
|
313
|
+
: lineOf('peer', s.peerReady,
|
|
372
314
|
`已就绪(v6 直达 ${s.coreAlias},一条 ssh)`,
|
|
373
|
-
peerUnready
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
const views = s.forwardViews;
|
|
380
|
-
// 装了 link 但 topology 没配 forward 行:没有该监听的口,别把空列表
|
|
381
|
-
// 渲染成「✗ 未就绪( 没在听…)」——那是配置空,不是线路故障
|
|
382
|
-
if (views.length === 0) {
|
|
383
|
-
lines.push(' link: 已安装(topology 没配转发口:要转发口加 forward local 行后重跑 ssh-net install)');
|
|
384
|
-
} else {
|
|
385
|
-
const allOk = views.every(([, ok]) => ok);
|
|
386
|
-
lines.push(allOk
|
|
387
|
-
? ` link: ✓ 就绪(${views.map(([l]) => l).join(' · ')})`
|
|
388
|
-
: ` link: ✗ 未就绪(${views.filter(([, ok]) => !ok).map(([l]) => l).join('、')} 没在听;隧道建立中)`);
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
lines.push(s.v6 !== ''
|
|
392
|
-
? ` 对外地址: ${s.v6}`
|
|
393
|
-
: !s.v6ProbeOk
|
|
394
|
-
? ' 对外地址: 未知(v6 探测没跑成:sn-ipv6 超时或异常——vpn 重环境下 route 查询可能拖过探测时限,稍后再试)'
|
|
395
|
-
: ` 对外地址: 无 v6${sw.peer ? '(peer 建不起来,只能走 relay)' : ''}`);
|
|
315
|
+
peerUnready));
|
|
316
|
+
if (s.linkInstalled && s.forwardViews.length > 0) {
|
|
317
|
+
const bad = s.forwardViews.filter(([, ok]) => !ok).map(([l]) => l);
|
|
318
|
+
lines.push(lineOf('link', bad.length === 0,
|
|
319
|
+
`就绪(${s.forwardViews.map(([l]) => l).join(' · ')})`,
|
|
320
|
+
`未就绪(${bad.join('、')} 没在听;刚开启请等几秒,持续出现查 ${s.coreAlias} 上的转发目标服务)`));
|
|
396
321
|
}
|
|
397
|
-
lines.push(
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
322
|
+
lines.push(s.v6 !== ''
|
|
323
|
+
? ` 对外地址: ${s.v6}`
|
|
324
|
+
: !s.v6ProbeOk
|
|
325
|
+
? ' 对外地址: 未知(v6 探测没跑成,稍后再试)'
|
|
326
|
+
: ` 对外地址: 无 v6${sw.peer ? '(peer 建不起来,只能走 relay)' : ''}`);
|
|
327
|
+
}
|
|
328
|
+
lines.push(s.linkInstalled
|
|
329
|
+
? ` link 附加件: 已安装(${s.job.running ? '运行中' : '未运行'})`
|
|
330
|
+
: ' link 附加件: 未安装(要 SOCKS/VNC 转发口:ssh-net install --with-link)');
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function renderCoreLines(s, lines) {
|
|
334
|
+
lines.push(relayStatusLine(s));
|
|
335
|
+
lines.push(` relay 进程: ${s.relayJob.running ? `运行中${exitNote(s.relayJob.lastExitCode)}` : '未运行'}`);
|
|
336
|
+
lines.push(' edge:');
|
|
337
|
+
for (const e of s.edges) {
|
|
338
|
+
// 未注册分两态:state 无 v6 = 真从未连接;有 v6 = 曾报名过(休眠后 gui 域
|
|
339
|
+
// 会话重建会这样),等它下次报名自愈——别说成「到场装机」
|
|
340
|
+
const state = !e.registered
|
|
341
|
+
? (e.v6
|
|
342
|
+
? `曾连接过(地址 ${e.v6}),现在没在拨——休眠/重启后的正常态,它下次来连时自动恢复`
|
|
343
|
+
: '从未连接(到场装机后自动出现)')
|
|
344
|
+
: e.disabled ? '休眠中(这台笔记本下次 ssh 或 start peer 时自动唤醒)'
|
|
345
|
+
: e.running ? `已连接(地址 ${e.v6 ?? '?'})${exitNote(e.lastExitCode)}`
|
|
346
|
+
: '已断开(自动重试中)';
|
|
347
|
+
lines.push(` ${e.alias} ${state}`);
|
|
348
|
+
lines.push(` ${addrNote(e.entry)}`);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
export function renderText(s) {
|
|
353
|
+
const lines = [`ssh-net ${s.role} ${s.alias}`];
|
|
354
|
+
if (s.role === 'edge') renderEdgeLines(s, lines);
|
|
355
|
+
else if (s.role === 'core') renderCoreLines(s, lines);
|
|
356
|
+
else {
|
|
418
357
|
lines.push(relayStatusLine(s));
|
|
419
358
|
lines.push(` relay 进程: ${s.active === 'active' ? '运行中(systemd)' : `${s.active}(systemd)`}`);
|
|
359
|
+
lines.push(' edge:');
|
|
360
|
+
for (const e of s.entries) lines.push(` ${e.alias} ${addrNote(e)}`);
|
|
420
361
|
}
|
|
421
|
-
|
|
422
362
|
if (s.issues.length) {
|
|
423
363
|
lines.push('需要处理:');
|
|
424
364
|
for (const i of s.issues) lines.push(` - ${i}`);
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
// stop
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
// node stop+disable。
|
|
1
|
+
// stop:edge 关两开关——缺省或 -a/--all(或带 relay)=级联全关(删两开关 +
|
|
2
|
+
// TERM link job 并校验真退出);只关 peer 是软关(只删标记、零闪断:已建立的
|
|
3
|
+
// 回拨照用,只是不再报名)。link 不是运行时目标(移除附加件用 install
|
|
4
|
+
// --without-link)。重复 stop 是幂等确认,措辞区分「已关闭」与「已是关闭状态」。
|
|
5
|
+
// core disable+bootout uplink-relay(不动 uplink-edge);node stop+disable。
|
|
7
6
|
import * as launchd from '../lib/launchd.mjs';
|
|
8
7
|
import * as systemd from '../lib/systemd.mjs';
|
|
9
8
|
import { reapEdgeLink } from '../lib/jobctl.mjs';
|
|
@@ -44,14 +43,14 @@ export function run(ctx, args = []) {
|
|
|
44
43
|
// 重连时也不会通知,自然只走中继
|
|
45
44
|
return 'peer 已关闭(不再通知 core 来连。已建立的连接继续用,新连接走中继)';
|
|
46
45
|
}
|
|
47
|
-
// 全关 / 只关 relay:级联删两开关(peer 依赖 relay
|
|
48
|
-
//
|
|
46
|
+
// 全关 / 只关 relay:级联删两开关(peer 依赖 relay),硬停 link job
|
|
47
|
+
// (PathState 见 RELAY_ON 消失不再重拉;未装附加件直接 gone)
|
|
49
48
|
const alreadyAllOff = !before.relay && !before.peer;
|
|
50
49
|
ctx.state.removeOn(['relay', 'peer']);
|
|
51
50
|
const { gone, label } = reapEdgeLink(ctx);
|
|
52
51
|
if (!gone)
|
|
53
52
|
throw new Error(
|
|
54
|
-
`${label}
|
|
53
|
+
`${label} 发 TERM 后仍在运行。手工排查:launchctl print gui/${ctx.uid}/${label}`,
|
|
55
54
|
);
|
|
56
55
|
if (target === null) {
|
|
57
56
|
return alreadyAllOff
|
|
@@ -11,8 +11,7 @@
|
|
|
11
11
|
//
|
|
12
12
|
// 边界:node 角色需要 root(install 写 /etc/systemd/system),先检查再动
|
|
13
13
|
// npm,避免「包装上了、install 被拒」的半截态;core 侧**已注册** edge 的
|
|
14
|
-
// uplink-edge 实体随 install
|
|
15
|
-
// 报名对账(design §5)。
|
|
14
|
+
// uplink-edge 实体随 install 刷新,job 参数仍等各 edge 下次报名对账。
|
|
16
15
|
import { readRuntimeVersion, RUNTIME_VERSION } from '../lib/runtime.mjs';
|
|
17
16
|
|
|
18
17
|
const NPM_TIMEOUT = 180_000; // 慢网络下全局装包的余量
|
|
@@ -28,18 +27,16 @@ export async function run(ctx, args = []) {
|
|
|
28
27
|
if (ctx.role === 'node' && ctx.uid !== 0)
|
|
29
28
|
throw new Error('node 上升级要写 /etc/systemd/system(install 步骤),请以 root 运行(sudo ssh-net update)');
|
|
30
29
|
|
|
31
|
-
// 首跑就带 --prefer-online(notes
|
|
32
|
-
//
|
|
33
|
-
//
|
|
34
|
-
// 完全绕过它。跳过缓存读 dist-tags 一劳永逸,慢个几秒可接受
|
|
30
|
+
// 首跑就带 --prefer-online(notes 第 10 条):npm 默认读本地缓存的
|
|
31
|
+
// dist-tags,刚发布后 latest 仍是旧值时不报 ETARGET 而是「成功」装回旧版。
|
|
32
|
+
// 跳过缓存读 dist-tags,慢几秒可接受
|
|
35
33
|
const npmArgs = ['install', '-g', 'ssh-net@latest', '--prefer-online'];
|
|
36
34
|
let r = ctx.runner.run('npm', npmArgs, { timeout: NPM_TIMEOUT });
|
|
37
35
|
if (r.error && r.error.code === 'ENOENT')
|
|
38
36
|
throw new Error('PATH 上找不到 npm——检查 ~/.zshenv(mac)或 .bashrc(Linux)里 node 的 PATH 配置后重试');
|
|
39
37
|
if (r.code !== 0 && /ETARGET/.test(r.stderr)) {
|
|
40
|
-
//
|
|
41
|
-
//
|
|
42
|
-
// 再试一次(覆盖另一台边缘节点/瞬时网络),仍失败才让人等
|
|
38
|
+
// 剩余传播窗口:--prefer-online 也绕不开 registry 各边缘节点间的元数据
|
|
39
|
+
// 不一致。再试一次(覆盖另一台边缘节点/瞬时网络),仍失败才让人等
|
|
43
40
|
r = ctx.runner.run('npm', npmArgs, { timeout: NPM_TIMEOUT });
|
|
44
41
|
if (r.code !== 0)
|
|
45
42
|
throw new Error(
|
|
@@ -51,7 +48,7 @@ export async function run(ctx, args = []) {
|
|
|
51
48
|
|
|
52
49
|
// 换装后用新 bin 跑 install:对账运行时实体/软链/plist/unit 并落
|
|
53
50
|
// runtime-version 戳(幂等,健康的 job 不断连)。PATH 上找 ssh-net:
|
|
54
|
-
// 非交互环境由 dotfiles 保证(同报名 RPC 的前提,design §7
|
|
51
|
+
// 非交互环境由 dotfiles 保证(同报名 RPC 的前提,design §7 第 5 条)
|
|
55
52
|
const upToDate = /up to date/.test(r.stdout); // npm 零变更:如实说,不硬报「升级完成」
|
|
56
53
|
const inst = ctx.runner.run('ssh-net', ['install'], { timeout: INSTALL_TIMEOUT });
|
|
57
54
|
if (inst.error && inst.error.code === 'ENOENT')
|
package/libexec/lib/conf.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// conf.d 片段的落盘原语(design §4.2
|
|
1
|
+
// conf.d 片段的落盘原语(design §4.2)。~/.ssh/config 顶部
|
|
2
2
|
// `Include ~/.ssh/ssh-net/conf.d/*` 会 glob 进本目录每个文件,所以:
|
|
3
3
|
// - tmp 文件绝不写在 conf.d/ 内(写入中的半截文件会被某次 ssh 扫到)——
|
|
4
4
|
// 写在上级目录 ~/.ssh/ssh-net/.<名>.tmp-<pid> 再原子 rename 进来
|
|
@@ -29,8 +29,3 @@ export function syncConfFile(file, content) {
|
|
|
29
29
|
writeFileAtomic(file, content, { mode: 0o600, tmpDir: path.dirname(dir) });
|
|
30
30
|
return true;
|
|
31
31
|
}
|
|
32
|
-
|
|
33
|
-
/** 删一个片段(不存在静默)。 */
|
|
34
|
-
export function removeConfFile(file) {
|
|
35
|
-
fs.rmSync(file, { force: true });
|
|
36
|
-
}
|