ssh-net 26.10.58 → 26.10.60

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 CHANGED
@@ -1,13 +1,35 @@
1
1
  # Changelog
2
2
 
3
- 本项目版本号与发布节奏仿 mihomo-cli:npm 公共分发,版本即升级动作。
4
- 版本号格式为**年.月.序号**(如 26.9.1):月是发布时间戳,**序号 = 累计
5
- 发包数**、跨月累加永不重置(0.x 过渡期 4 包 + 26.9 起编号;26.9.12 后
6
- 跳号至 26.9.17 对齐累计数,13–16 永久空缺)。发布 = 改版本号 + 发
7
- GitHub Release(Actions 自动跑测试后 npm publish;跳号与 0.x 的完整
8
- 考据见 git 史)。
9
-
10
- ## [Unreleased]
3
+ 本项目使用 npm 公共分发,版本号格式为**年.月.序号**:月内序号递增,
4
+ 跨月从 1 开始;历史跳号不补、不另维护累计发包数。发布 = 改版本号 +
5
+ CHANGELOG + 发 GitHub Release,Actions 跑测试后 npm publish。
6
+
7
+ ## [26.10.60] - 2026-10-25
8
+
9
+ ### 修复
10
+
11
+ - Linux 卸载仅在存在中继 unit 时要求 root,修复零 unit 的非 root 清理被误拒
12
+ - update 在收尾前核对 PATH 选中的 ssh-net 版本,避免旧 bin 执行 install 后假报升级成功
13
+ - status 直接比对 install 管理的 plist/unit,只改服务模板的未完成升级也会提示修复
14
+ - 发布流程要求 push CI 成功后再发 GitHub Release,不再只凭 publish 结果判断全平台测试
15
+
16
+ ## [26.10.59] - 2026-10-25
17
+
18
+ ### 简化
19
+
20
+ - install/status 的对账只汇总有无变化,core 的 edge 任务直接列 launchd
21
+ 事实;删除 runtime-version 版本戳与 update 后置校验,旧文件由 install
22
+ 和 uninstall 清理
23
+ - stop/uninstall 始终按磁盘 job/unit 操作,不再读取 topology 或维护独立
24
+ 恢复模式;stop peer 仍只关闭主动恢复,系统查询与停止失败照常报错
25
+ - sn-route 的 follower 本轮不再接管被杀的 leader,ENSURE 改为空锁文件;
26
+ 并发仍只报一次名,失败印记过期后的下一条连接负责重试
27
+ - update 保留明确版本安装与防降级,删除 ETARGET 专属分支和升级/降级
28
+ 措辞;publish 删除 latest 防反超检查,不补历史 Release,版本号按月编号
29
+
30
+ ### 发布纪律
31
+
32
+ - 明确「发布」包含提交、合并回 main、推送、GitHub Release 与在线机器升级
11
33
 
12
34
  ## [26.10.58] - 2026-10-25
13
35
 
package/README.md CHANGED
@@ -127,8 +127,8 @@ edge 不自动判断所在网络,按下面的场景与需求手动操作:
127
127
  edge 的通知和两次重试机制自动管理,不需要手动干预;node 的中继服务由 systemd
128
128
  自动重启。需要手动重拉中继时,先 `stop` 再 `start`
129
129
 
130
- core 的 status 中,edge 行只表示**自动连接任务**是否运行、休眠或未加载,
131
- 不表示 SSH 线路已经连通;下方「ssh 入口」是最近登记的地址及其新旧
130
+ core 的 status 中,edge 行只列出**自动连接任务**的注册、运行、休眠和上次
131
+ 退出事实,不表示 SSH 线路已经连通;下方「ssh 入口」是最近登记的地址及其新旧
132
132
 
133
133
  ### 退出码
134
134
 
@@ -150,22 +150,22 @@ ssh-net update <版本号> # 切换或回滚;显式当前版也会重跑 i
150
150
  - 无参确认已最新时不会重装或修复组件;需要按当前版重新同步时,显式运行
151
151
  `ssh-net update <当前版本>`,或直接运行 `ssh-net install`
152
152
  - 查询失败或 registry 返回比当前更旧的版本时,在安装前停止,不退回 `@latest`;
153
- 刚发布遇到 ETARGET 时等几分钟,按提示用明确版本号重试
153
+ 安装失败直接保留 npm 原因,刚发布遇到 ETARGET 时等几分钟,用同一明确版本号重试
154
154
  - node 使用 `sudo -i ssh-net update`;开发机的 `npm link` 状态不要用 update
155
155
  - **core/node 升级会恢复此前 stop 的中继服务**;维护停机期间不要顺手升级
156
156
  - 已在运行的进程只在下次重连后使用更新后的运行时脚本;服务配置变化或用
157
157
  `stop` 后 `start` 手动重拉可能断开当前连接
158
158
 
159
- ## 配置损坏时恢复
159
+ ## 配置缺失时停止与卸载
160
160
 
161
161
  `install`、`update`、`status` 等命令需要有效 topology、静态 Host 配置和本机
162
162
  主机名匹配;修复配置运行 `dot apply ssh`
163
163
 
164
- `stop`、`uninstall` 不受此限制:配置缺失、不可读、损坏或主机名不匹配时,会明确进入
165
- 恢复模式,只按磁盘上的 ssh-net job/unit 操作,不猜 topology。恢复模式的 stop
166
- 只支持全停,不细分 relay/peer;系统查询或服务停止失败仍会报错,不会假装成功。
167
- 恢复模式只能发现仍有 plist/unit 的任务;若这些文件也被手删,需先用
168
- launchctl/systemctl 检查系统注册表里的残留
164
+ `stop`、`uninstall` 始终不读 topology,只按磁盘上的 ssh-net job/unit 操作,
165
+ 配置正常与损坏时走同一条路径。`stop peer` 仍只关闭主动恢复;其余 stop 形式
166
+ 级联清除 relay/peer 并停止本机中继与本地转发任务。系统查询或服务停止失败
167
+ 仍会报错,不会假装成功。若 plist/unit 也被手删,需用 launchctl/systemctl
168
+ 检查系统注册表里的残留
169
169
 
170
170
  ## 卸载
171
171
 
package/libexec/cli.mjs CHANGED
@@ -2,7 +2,7 @@
2
2
  // ssh-net 控制面入口:参数路由 + buildContext。业务都在 lib/ 与 commands/,
3
3
  // 本文件只做装配、帮助与错误边界。
4
4
  import os from 'node:os';
5
- import { ConfigError, parseTopology, validateHostContract } from './lib/config.mjs';
5
+ import { parseTopology, validateHostContract } from './lib/config.mjs';
6
6
  import { parseStaticHosts } from './lib/sshconfig.mjs';
7
7
  import { localHostname, matchIdentity } from './lib/identity.mjs';
8
8
  import { machinePaths } from './lib/paths.mjs';
@@ -22,9 +22,9 @@ import { run as updateRun } from './commands/update.mjs';
22
22
  const COMMANDS = {
23
23
  install: { run: installRun, desc: '安装或修复本机组件;edge 可选 --with-link/--without-link' },
24
24
  update: { run: updateRun, desc: '升级或按版本号切换并同步组件;core/node 会恢复中继服务' },
25
- uninstall: { run: uninstallRun, desc: '停止并删除本机任务与数据;之后再 npm uninstall -g' },
25
+ uninstall: { run: uninstallRun, desc: '按磁盘任务停止并删除本机组件;之后再 npm uninstall -g' },
26
26
  start: { run: startRun, desc: '启动中继;edge 默认同时恢复 IPv6 直连(relay|peer)' },
27
- stop: { run: stopRun, desc: '停止中继;edge 可用 peer 仅停止直连自动恢复(默认全部)' },
27
+ stop: { run: stopRun, desc: '按磁盘任务停止中继与本地转发;peer 仅关闭自动恢复' },
28
28
  status: { run: statusRun, desc: '查看本机状态;--probe 增加线路实测' },
29
29
  _register: { run: registerRun, internal: true, desc: '内部 RPC(core):接收 edge 的连接通知,建/唤自动连接任务' },
30
30
  };
@@ -40,30 +40,41 @@ function help() {
40
40
  .map(([name, c]) => ` ${name.padEnd(10)} ${c.desc}`),
41
41
  '',
42
42
  '配置:~/.ssh/ssh-net/topology 与 ~/.ssh/config(由 dot apply ssh 生成)',
43
- '配置损坏时仍可 stop/uninstall;角色差异与日常示例见 README',
43
+ 'stop/uninstall 只按磁盘任务操作,不读配置;角色差异与日常示例见 README',
44
44
  ];
45
45
  return lines.join('\n');
46
46
  }
47
47
 
48
- /** 配置或本机身份不满足正常命令前提;stop/uninstall 可据此转磁盘恢复模式。 */
49
- const contextError = (message) => new ConfigError(0, message);
50
-
51
- /** 读取配置文件的 IO 失败也属于可恢复的配置前提,不把系统错误原样抛成死路。 */
52
48
  function readConfig(file) {
53
49
  try {
54
50
  return readFileOrNull(file);
55
51
  } catch (err) {
56
- throw contextError(`无法读取配置 ${file}(${err.code ?? '未知错误'}):${err.message}`);
52
+ throw new Error(`无法读取配置 ${file}(${err.code ?? '未知错误'}):${err.message}`);
57
53
  }
58
54
  }
59
55
 
60
- /** 装配本机上下文;topology/ssh config 缺失或主机名不匹配在此硬失败。 */
61
- function buildContext() {
56
+ /** 停止/卸载与正常命令共用的本机上下文;前者只使用这些磁盘与系统接口。 */
57
+ function buildBaseContext() {
62
58
  const env = process.env;
63
59
  const paths = machinePaths(env);
60
+ return {
61
+ env,
62
+ paths,
63
+ runner: createRunner(env),
64
+ progress: (message) => process.stderr.write(`${message}\n`),
65
+ state: createStateStore(paths),
66
+ uid: process.getuid(),
67
+ platform: process.platform,
68
+ };
69
+ }
70
+
71
+ /** 装配需要 topology、本机身份与静态 Host 契约的命令上下文。 */
72
+ function buildContext() {
73
+ const base = buildBaseContext();
74
+ const { paths } = base;
64
75
  const text = readConfig(paths.topologyFile);
65
76
  if (text === null)
66
- throw contextError(`缺配置 ${paths.topologyFile}——先运行 dot apply ssh;只需停止或卸载时,ssh-net stop/uninstall 可在配置缺失下执行`);
77
+ throw new Error(`缺配置 ${paths.topologyFile}——先运行 dot apply ssh;stop/uninstall 不读配置,可直接执行`);
67
78
  const topo = parseTopology(text);
68
79
 
69
80
  // 静态主机表(IP 唯一来源);~/.ssh/config 缺失时给空表,契约校验报具体缺口
@@ -74,37 +85,15 @@ function buildContext() {
74
85
  const hostname = localHostname(os.hostname());
75
86
  const self = matchIdentity(topo, hostname);
76
87
  if (!self)
77
- throw contextError(
88
+ throw new Error(
78
89
  `本机主机名 ${hostname} 不在 topology 的 host 行中(登记:${[...topo.byHostname.keys()].join('、')})`,
79
90
  );
80
91
  return {
81
- env,
82
- paths,
92
+ ...base,
83
93
  topo,
84
94
  hostTable,
85
95
  self,
86
96
  role: self.role,
87
- runner: createRunner(env),
88
- progress: (message) => process.stderr.write(`${message}\n`),
89
- state: createStateStore(paths),
90
- uid: process.getuid(),
91
- platform: process.platform,
92
- };
93
- }
94
-
95
- /** 配置不可用时仍保留停止/卸载入口;恢复命令只能依赖磁盘产物,不读 topo。 */
96
- function buildRecoveryContext(cause) {
97
- const env = process.env;
98
- const paths = machinePaths(env);
99
- return {
100
- env,
101
- paths,
102
- runner: createRunner(env),
103
- progress: (message) => process.stderr.write(`${message}\n`),
104
- state: createStateStore(paths),
105
- uid: process.getuid(),
106
- platform: process.platform,
107
- configError: cause,
108
97
  };
109
98
  }
110
99
 
@@ -126,13 +115,7 @@ async function main() {
126
115
  return 2;
127
116
  }
128
117
 
129
- let ctx;
130
- try {
131
- ctx = buildContext();
132
- } catch (err) {
133
- if (!(err instanceof ConfigError) || !['stop', 'uninstall'].includes(cmd)) throw err;
134
- ctx = buildRecoveryContext(err);
135
- }
118
+ const ctx = ['stop', 'uninstall'].includes(cmd) ? buildBaseContext() : buildContext();
136
119
  const result = await entry.run(ctx, argv.slice(1));
137
120
  if (typeof result === 'string') {
138
121
  process.stdout.write(`${result}\n`);
@@ -3,15 +3,14 @@
3
3
  // 不在不启动);core/node 会恢复 stop 的中继服务;core 不预建 uplink-edge
4
4
  //(首次报名才生成),node 写 unit 并 enable --now。
5
5
  import fs from 'node:fs';
6
- import os from 'node:os';
7
6
  import path from 'node:path';
8
- import { renderForwardArgs } from '../lib/config.mjs';
9
7
  import {
10
8
  CALLBACK_PORT, TERMINAL_NOTIFIER_PATHS, OSASCRIPT_PATH,
11
9
  } from '../lib/constants.mjs';
12
10
  import { syncPlist, removeJob } from '../lib/manage.mjs';
13
11
  import { syncConfFile } from '../lib/conf.mjs';
14
12
  import { ensureRuntime } from '../lib/runtime.mjs';
13
+ import { edgeLinkJob, coreRelayJob, nodeRelayJob } from '../lib/jobs.mjs';
15
14
  import * as launchd from '../lib/launchd.mjs';
16
15
  import * as systemd from '../lib/systemd.mjs';
17
16
  import {
@@ -23,29 +22,16 @@ import {
23
22
  SN_PROBE, RESIDENT_SCRIPTS, edgeRuntimePlan, coreNodeRuntimePlan,
24
23
  edgeCallbacksPlan, linkAddonPlan, mergePlans,
25
24
  } from '../lib/manifest.mjs';
26
- import {
27
- linkLabel, relayLabel, relayUnitName,
28
- renderEdgeHost, renderEdgeLinkPlist, renderRelayPlist, renderRelayUnit, renderRoutes,
29
- } from '../lib/render.mjs';
25
+ import { linkLabel, renderEdgeHost, renderRoutes } from '../lib/render.mjs';
30
26
 
31
- /** ensureRuntime 结果的人话摘要;常驻实体换新时顺便说明运行中进程的生效时机。 */
27
+ /** 对账只汇总有无变化;具体差异看文件与 launchd/systemd,不维护逐态文案。 */
32
28
  function compNote(results, entities) {
33
- const LABELS = {
34
- created: '新拷脚本', updated: '更新脚本', repaired: '补执行权限',
35
- linked: '新软链', relinked: '修正软链', replaced: '换为软链',
36
- };
37
- const counts = new Map();
38
- for (const s of results) {
39
- if (s === 'unchanged') continue;
40
- counts.set(s, (counts.get(s) ?? 0) + 1);
41
- }
42
- if (counts.size === 0) return '';
43
- const summary = `本次更新:${[...counts].map(([k, n]) => `${LABELS[k] ?? k} ${n} 个`).join('、')}`;
29
+ if (!results.some(Boolean)) return '';
44
30
  const residentUpdated = entities.some(([name], i) =>
45
- results[i] === 'updated' && RESIDENT_SCRIPTS.includes(name));
31
+ results[i] && RESIDENT_SCRIPTS.includes(name));
46
32
  return residentUpdated
47
- ? `${summary};常驻脚本已更新,运行中的进程将在下次重连后使用新版`
48
- : summary;
33
+ ? '后台脚本已同步,常驻进程将在下次重连后使用新版'
34
+ : '后台脚本已同步';
49
35
  }
50
36
 
51
37
  /** 安装结果文案:组件刷新/配置更新若干条 note 合并进一对括号(''=零附加信息)。 */
@@ -56,22 +42,18 @@ function notesPart(...notes) {
56
42
 
57
43
  /** plist/unit 的同构部署结果统一变成用户可读的任务变化。 */
58
44
  function deploymentNote(r, task, config) {
59
- if (r.deployed)
60
- return r.restarted ? `${config}已更新,可能断开当前连接` : `${task}已部署`;
61
- if (r.revived) return `${task}已恢复`;
62
- if (r.registered) return `${task}已注册`;
63
- return '';
45
+ if (!r.changed) return '';
46
+ return r.restarted ? `${config}已更新,可能断开当前连接` : `${task}已同步`;
64
47
  }
65
48
 
66
- /** core/node 安装结果同构:服务配置未变只报刷新项,变了报中继通道已开启。 */
67
49
  function coreNodeInstallMsg(comp, entryChanged, result, relayLine) {
68
50
  const note = notesPart(
69
51
  comp,
70
52
  entryChanged ? 'edge 入口已更新' : '',
71
53
  deploymentNote(result, '中继任务', '中继服务配置'),
72
54
  );
73
- if (result.unchanged)
74
- return note ? `已刷新${note};服务配置无需改动` : '已安装,无需改动';
55
+ if (!note)
56
+ return `已安装,服务配置无需改动;中继通道已开启(${relayLine})`;
75
57
  return `安装完成${note},中继通道已开启(${relayLine})`;
76
58
  }
77
59
 
@@ -159,9 +141,6 @@ async function installEdge(ctx, args) {
159
141
  // 预检纯读且在落任何产物之前:被拒时 bin/ 一个都不建
160
142
  await precheckEdgePorts(ctx, linkOn);
161
143
 
162
- const label = linkLabel(topo.core.alias);
163
- const plistPath = paths.plistFile(label);
164
-
165
144
  // 显式意图先落/删 marker;无参 install(含 update 内置)不触碰它
166
145
  if (flag === true) {
167
146
  fs.mkdirSync(paths.sshNetDir, { recursive: true });
@@ -179,20 +158,15 @@ async function installEdge(ctx, args) {
179
158
  if (linkOn) {
180
159
  // 附加件:渲染纯 ssh plist 并对账(RELAY_ON 在 syncPlist 按 stopped 语义
181
160
  // 首发,不在注册待命);转发参数是最终 -L argv
182
- const content = renderEdgeLinkPlist({
183
- binDir: paths.binDir,
184
- coreAlias: topo.core.alias,
185
- forwardArgs: renderForwardArgs(topo.forwards, ctx.hostTable),
186
- relayOnFile: paths.switchFiles.relay,
187
- logsDir: paths.logsDir,
188
- });
189
- const r = syncPlist(ctx, { label, plistPath, content });
161
+ const r = syncPlist(ctx, edgeLinkJob(ctx));
190
162
  if (r.lintError) throw new Error(`link plist 校验失败:${r.lintError}`);
191
- linkChanged = !r.unchanged;
163
+ linkChanged = r.changed;
192
164
  linkNote = deploymentNote(r, '本地转发任务', '本地转发配置');
193
165
  } else {
194
166
  // 默认零常驻:清掉加装过的 link 全部产物。plist 必须先 bootout,否则
195
167
  // 删了实体后 launchd 每 ThrottleInterval 刷 ENOENT
168
+ const label = linkLabel(topo.core.alias);
169
+ const plistPath = paths.plistFile(label);
196
170
  const jobRemoved = removeJob(ctx, label, plistPath);
197
171
  const addon = linkAddonPlan(topo);
198
172
  const addonPaths = [...addon.entities, ...addon.links].map(([name]) => paths.binScript(name));
@@ -207,13 +181,9 @@ async function installEdge(ctx, args) {
207
181
  // 选路 Match 与尾部打标块(VS Code 标识 + 本机别名)
208
182
  const routesChanged = syncConfFile(paths.routesFile, renderRoutes(topo, ctx.self.alias));
209
183
  const depNote = localNotifyDepNote();
210
- const head = linkOn
211
- ? '安装完成(含本地端口转发)'
212
- : '安装完成(默认不安装本地端口转发;需要时运行 ssh-net install --with-link)';
184
+ const head = linkOn ? '安装完成(含本地端口转发)' : '安装完成';
213
185
  const base = (!comp && !linkChanged && !routesChanged && flag === undefined)
214
- ? (linkOn
215
- ? '已安装(本地端口转发已启用),无需改动'
216
- : '已安装,无需改动(未安装本地端口转发)')
186
+ ? '已安装,配置无需改动'
217
187
  : `${head}${notesPart(comp, linkNote, routesChanged ? 'ssh 配置片段已更新' : '')}`;
218
188
  const startNote = ctx.state.isOn('relay')
219
189
  ? ''
@@ -258,14 +228,7 @@ function installCore(ctx) {
258
228
  // relay 基础形态 + 磁盘在册 edge 的回拨组件(清单与报名/status 体检同源)
259
229
  const plan = mergePlans(coreNodeRuntimePlan(topo), edgeCallbacksPlan(edges));
260
230
  const comp = compNote(ensureRuntime(ctx, plan.entities, plan.links), plan.entities);
261
- const label = relayLabel(topo.relay.alias);
262
- const content = renderRelayPlist({
263
- binDir: paths.binDir,
264
- relayAlias: topo.relay.alias,
265
- relayPort: topo.core.relayPort,
266
- logsDir: paths.logsDir,
267
- });
268
- const r = syncPlist(ctx, { label, plistPath: paths.plistFile(label), content });
231
+ const r = syncPlist(ctx, coreRelayJob(ctx));
269
232
  // lint 失败不能静默报「安装完成」:syncPlist 以返回值交错误(register 同判)
270
233
  if (r.lintError) throw new Error(`relay plist 校验失败:${r.lintError}`);
271
234
  return coreNodeInstallMsg(comp, entryChanged, r,
@@ -279,26 +242,12 @@ function installNode(ctx) {
279
242
  const entryChanged = syncEdgeEntries(ctx);
280
243
  const plan = coreNodeRuntimePlan(topo);
281
244
  const comp = compNote(ensureRuntime(ctx, plan.entities, plan.links), plan.entities);
282
- const unitName = relayUnitName(topo.relay.alias);
283
- // HOME 用 root 的真实家目录(getpwuid),不取 $HOME:sudo 保留 HOME 的环境
284
- // (sudo -E,或部分发行版的默认值)会把调用者的家目录写进 unit——root
285
- // 运行的 ssh 将去读别人的 ~/.ssh/config,relay 别名解析错配且无声
286
- const home = ctx.nodeHome ?? os.userInfo().homedir;
245
+ const { home, ...job } = nodeRelayJob(ctx);
287
246
  // ExecStart 按空白分词、无 shell 引号转义通路,HOME 含空白会拆断 bin 路径
288
247
  // (plist 侧是 XML 数组无此问题)——当场拒绝比装出一个起不来的 unit 诚实
289
248
  if (/\s/.test(home))
290
249
  throw new Error(`node 的 HOME(${home})含空白字符:systemd ExecStart 无法转义,拒绝安装`);
291
- const unitPath = path.join(paths.systemdUnitDir, unitName);
292
- const r = systemd.syncUnit(runner, {
293
- unitPath,
294
- unitName,
295
- content: renderRelayUnit({
296
- binDir: paths.binDir,
297
- relayAlias: topo.relay.alias,
298
- relayPort: ctx.self.relayPort,
299
- home,
300
- }),
301
- });
250
+ const r = systemd.syncUnit(runner, job);
302
251
  return coreNodeInstallMsg(comp, entryChanged, r,
303
252
  `${ctx.topo.relay.alias} 入口 ${ctx.self.relayPort}`);
304
253
  }
@@ -1,12 +1,10 @@
1
1
  // start:edge 两开关(缺省或 -a/--all=relay+peer,peer 自动补 relay);
2
2
  // core 重新 enable+bootstrap uplink-relay;node systemctl。link 不是
3
3
  // 运行时开关(转发口只取决于装没装附加件)。peer 路径复用 sn-route 做
4
- // 人工强制恢复,不在控制面另维护一套报名实现。重复 start 是幂等确认:
5
- // 检查与恢复照做,措辞区分「已开启」与「已是开启状态」。
4
+ // 人工强制恢复,不在控制面另维护一套报名实现。
6
5
  import fs from 'node:fs';
7
6
  import * as systemd from '../lib/systemd.mjs';
8
7
  import { ensureLoaded } from '../lib/manage.mjs';
9
- import { assertUnitSwitchArgs } from '../lib/cliargs.mjs';
10
8
  import { SWITCH_NAMES } from '../lib/state.mjs';
11
9
  import { SN_ROUTE } from '../lib/manifest.mjs';
12
10
  import { relayLabel, relayUnitName, snRouteArgv } from '../lib/render.mjs';
@@ -22,12 +20,12 @@ function parseTargets(args) {
22
20
  }
23
21
 
24
22
  /** 先保存开关,再沿日常 ssh 的选路路径强制确保一次;未就绪不撤销用户意图。 */
25
- function startEdgePeer(ctx, alreadyOn) {
23
+ function startEdgePeer(ctx) {
26
24
  const route = ctx.paths.binScript(SN_ROUTE);
27
25
  if (!fs.existsSync(route))
28
26
  throw new Error('未安装(缺 sn-route)——先跑 ssh-net install');
29
27
 
30
- const prefix = alreadyOn ? '隧道已是开启状态' : 'peer 和 relay 开关已开启';
28
+ const prefix = 'relay 和 peer 开关已开启';
31
29
  ctx.state.setOn(['relay', 'peer']);
32
30
  // FORCE 只跳过新鲜失败印记;probe、ENSURE 与等待预算都和普通 ssh 相同。
33
31
  // stdio:'inherit':sn-route 进度和报名错误直达终端。
@@ -53,12 +51,8 @@ function startCore(ctx) {
53
51
  const plist = ctx.paths.plistFile(label);
54
52
  if (!fs.existsSync(plist)) throw new Error(`尚未安装(缺 ${plist}),先跑 ssh-net install`);
55
53
  // start 语义:在册但停着要踢活;在跑但 disabled 只清 override、不闪断
56
- const action = ensureLoaded(ctx, { label, plistPath: plist }, {
57
- kickWhenStopped: true,
58
- });
59
- if (action === 'bootstrapped') return '中继通道已开启';
60
- if (action === 'kickstarted') return '中继通道已重新拉起';
61
- return '中继通道已在运行';
54
+ ensureLoaded(ctx, { label, plistPath: plist }, { kickWhenStopped: true });
55
+ return '中继通道已开启';
62
56
  }
63
57
 
64
58
  export function run(ctx, args = []) {
@@ -66,16 +60,16 @@ export function run(ctx, args = []) {
66
60
  const before = ctx.state.readSwitches();
67
61
  const targets = parseTargets(args); // ['relay','peer'] 或 ['relay']
68
62
 
69
- if (targets.includes('peer'))
70
- return startEdgePeer(ctx, before.relay && before.peer);
63
+ if (targets.includes('peer')) return startEdgePeer(ctx);
71
64
  ctx.state.setOn(['relay']);
72
65
  const peerNote = before.peer
73
66
  ? 'peer 保持开启,仍会主动恢复 IPv6 直连'
74
67
  : 'peer 仍关闭;需要自动恢复时运行 ssh-net start peer';
75
- return `relay ${before.relay ? '已是开启状态' : '已开启'}(${peerNote})`;
68
+ return `relay 已开启(${peerNote})`;
76
69
  }
77
70
  // core/node 是单总开关(等同 --all):显式 -a/--all 收下,其余参数拒绝
78
- assertUnitSwitchArgs(args, 'start');
71
+ if (args.length && !(args.length === 1 && (args[0] === '-a' || args[0] === '--all')))
72
+ throw new Error('本机是单总开关,start 不带参数(-a/--all 亦可,等同缺省)');
79
73
  if (ctx.role === 'core') return startCore(ctx);
80
74
  if (ctx.role === 'node') {
81
75
  systemd.enableStart(ctx.runner, relayUnitName(ctx.topo.relay.alias));
@@ -4,9 +4,7 @@
4
4
  // 退出码:无「需要处理」事项 0;有可行动问题 1。线路关闭或 peer 暂不可用不等于
5
5
  // 安装故障;实测发现中继不可用会进入处理项。
6
6
  import fs from 'node:fs';
7
- import {
8
- auditScript, auditLink, runtimeVersionIssue,
9
- } from '../lib/runtime.mjs';
7
+ import { auditScript, auditLink } from '../lib/runtime.mjs';
10
8
  import { packageScriptPath } from '../lib/paths.mjs';
11
9
  import * as launchd from '../lib/launchd.mjs';
12
10
  import * as systemd from '../lib/systemd.mjs';
@@ -17,14 +15,14 @@ import {
17
15
  SN_ROUTE, SN_IPV6, UPLINK_EDGE,
18
16
  edgeRuntimePlan, coreNodeRuntimePlan, edgeCallbacksPlan,
19
17
  } from '../lib/manifest.mjs';
18
+ import { edgeLinkJob, coreRelayJob, nodeRelayJob } from '../lib/jobs.mjs';
20
19
  import {
21
- linkLabel, relayLabel, relayUnitName, edgeLabel,
22
- renderEdgeHost, renderRoutes, coreViaRelayArgs, probeSshOpts,
20
+ edgeLabel, renderEdgeHost, renderRoutes, coreViaRelayArgs, probeSshOpts,
23
21
  } from '../lib/render.mjs';
24
22
 
25
23
  /**
26
- * 各角色共用的实体+软链体检;完全未安装只报根因,不级联报版本戳和悬链。
27
- * @returns {{issues:string[], entityAudits:{name:string,audit:string}[], allMissing:boolean}}
24
+ * 各角色共用的实体+软链体检;完全未安装只报根因,不级联报其他产物。
25
+ * @returns {{issues:string[], entityAudits:{name:string,audit:string}[]}}
28
26
  */
29
27
  function runtimeIssues(ctx, { entities, links }) {
30
28
  const issues = [];
@@ -43,38 +41,26 @@ function runtimeIssues(ctx, { entities, links }) {
43
41
  // 部分缺失:探针丢了会让开着的隧道恒判中继,不能只显示不报
44
42
  issues.push(`后台脚本不完整(缺 ${entityAudits.filter((a) => a.audit === 'missing').map((a) => a.name).join('、')})——跑一次 ssh-net install 修复`);
45
43
  for (const [linkName, entityName] of links) {
46
- const r = auditLink(ctx.paths.binScript(linkName), entityName);
47
- if (r !== 'ok')
48
- issues.push(`后台脚本异常(${linkName}:${r})——跑一次 ssh-net install 修复`);
44
+ if (!auditLink(ctx.paths.binScript(linkName), entityName))
45
+ issues.push(`后台脚本或服务软链异常(${linkName})——跑一次 ssh-net install 修复`);
49
46
  }
50
47
  }
51
- return { issues, entityAudits, allMissing };
52
- }
53
-
54
- /** 三个 collector 的共同前奏:版本戳问题 + 实体/软链体检。 */
55
- function baseAudit(ctx, plan) {
56
- const issues = [];
57
- const { issues: rtIssues, entityAudits, allMissing } = runtimeIssues(ctx, plan);
58
- const vIssue = runtimeVersionIssue(ctx.paths);
59
- if (vIssue && !allMissing) issues.push(vIssue);
60
- issues.push(...rtIssues);
61
48
  return { issues, entityAudits };
62
49
  }
63
50
 
64
- /** conf.d 片段(install 生成物)与期望内容比对,返回问题文案(''=健康):
65
- * routes 与 edge 入口两处体检同一口径。 */
66
- function confFragmentIssue(file, expected, label) {
51
+ /** install 生成物与期望内容比对,返回问题文案(''=健康)。 */
52
+ function configFileIssue(file, expected, label, staleText = '与 topology 不一致') {
67
53
  const text = readFileOrNull(file);
68
54
  return text === expected
69
55
  ? ''
70
- : `${label}${text === null ? '缺失' : '与 topology 不一致'}——跑一次 ssh-net install 刷新`;
56
+ : `${label}${text === null ? '缺失' : staleText}——跑一次 ssh-net install 刷新`;
71
57
  }
72
58
 
73
59
  /** core/node:各 edge 入口片段与渲染期望比对(install 的产物),并取当前入口
74
60
  * 地址。缺失/过期会让 `ssh <别名>` 解析失败或用错 user,引导跑 install。 */
75
61
  function edgeEntries(ctx, issues) {
76
62
  return ctx.topo.edges.map((e) => {
77
- const issue = confFragmentIssue(
63
+ const issue = configFileIssue(
78
64
  ctx.paths.edgeHostFile(e.alias), renderEdgeHost(e), `${e.alias} 的 ssh 入口配置`,
79
65
  );
80
66
  if (issue) issues.push(issue);
@@ -146,12 +132,19 @@ async function collectEdge(ctx, probe) {
146
132
  const { topo, paths, state } = ctx;
147
133
  const linkInstalled = fs.existsSync(paths.linkEnabledFile);
148
134
  const plan = edgeRuntimePlan(topo, linkInstalled);
149
- const { issues, entityAudits } = baseAudit(ctx, plan);
135
+ const { issues, entityAudits } = runtimeIssues(ctx, plan);
136
+ const linkDeployment = linkInstalled ? edgeLinkJob(ctx) : null;
137
+ if (linkDeployment) {
138
+ const issue = configFileIssue(
139
+ linkDeployment.plistPath, linkDeployment.content, '本地转发配置', '需要更新',
140
+ );
141
+ if (issue) issues.push(issue);
142
+ }
150
143
 
151
144
  // routes 被手删/过期会让 ssh 静默退回 lan 直连,是故障。sn-route 没装时
152
145
  // 上面已报「尚未安装」,不重复
153
146
  if (entityAudits.some((a) => a.name === SN_ROUTE && a.audit === 'ok')) {
154
- const issue = confFragmentIssue(
147
+ const issue = configFileIssue(
155
148
  paths.routesFile, renderRoutes(topo, ctx.self.alias), 'ssh 自动选路配置',
156
149
  );
157
150
  if (issue) issues.push(issue);
@@ -160,7 +153,7 @@ async function collectEdge(ctx, probe) {
160
153
  // link 未装时 job 无消费者(渲染走「未安装」分支)——默认零常驻形态不为它
161
154
  // 白跑一次 launchctl print
162
155
  const job = linkInstalled
163
- ? launchd.printState(ctx.runner, ctx.uid, linkLabel(topo.core.alias))
156
+ ? launchd.printState(ctx.runner, ctx.uid, linkDeployment.label)
164
157
  : null;
165
158
  const switches = state.readSwitches();
166
159
 
@@ -210,11 +203,16 @@ async function collectEdge(ctx, probe) {
210
203
 
211
204
  async function collectCore(ctx, probe) {
212
205
  const { topo, paths } = ctx;
213
- const { issues } = baseAudit(ctx, coreNodeRuntimePlan(topo));
206
+ const { issues } = runtimeIssues(ctx, coreNodeRuntimePlan(topo));
214
207
 
215
208
  // disabled override 是整域同一份字典:relay job 与各 edge job 共用一次拉取
216
209
  const dmap = launchd.disabledMap(ctx.runner, ctx.uid);
217
- const relayJob = launchd.inspectWith(ctx.runner, ctx.uid, relayLabel(topo.relay.alias), dmap);
210
+ const relayDeployment = coreRelayJob(ctx);
211
+ const configIssue = configFileIssue(
212
+ relayDeployment.plistPath, relayDeployment.content, '中继服务配置', '需要更新',
213
+ );
214
+ if (configIssue) issues.push(configIssue);
215
+ const relayJob = launchd.inspectWith(ctx.runner, ctx.uid, relayDeployment.label, dmap);
218
216
  // 实测时先发起来,边等边做注册态体检;默认快查不发起 ssh
219
217
  const relayPortP = probe
220
218
  ? relayPortListening(ctx, topo.core.relayPort)
@@ -237,8 +235,7 @@ async function collectCore(ctx, probe) {
237
235
  const bad = [];
238
236
  if (callbackEntityAudit !== 'ok')
239
237
  bad.push(`连接脚本${callbackEntityAudit === 'missing' ? '缺失' : '过期'}`);
240
- const linkState = auditLink(paths.binScript(linkName), entityName);
241
- if (linkState !== 'ok') bad.push(`软链(${linkState})`);
238
+ if (!auditLink(paths.binScript(linkName), entityName)) bad.push('服务软链异常');
242
239
  if (bad.length)
243
240
  issues.push(`${e.alias} 的自动连接脚本异常(${bad.join('、')})——它下次连过来时会自动刷新(或跑一次 ssh-net install 手动修复)`);
244
241
  }
@@ -248,7 +245,6 @@ async function collectCore(ctx, probe) {
248
245
  disabled: st.disabled,
249
246
  running: st.running,
250
247
  lastExitCode: st.lastExitCode,
251
- v6: ctx.state.readEdgeV6(e.alias),
252
248
  entry: entryByAlias.get(e.alias),
253
249
  });
254
250
  }
@@ -266,10 +262,15 @@ async function collectCore(ctx, probe) {
266
262
 
267
263
  async function collectNode(ctx, probe) {
268
264
  const { topo } = ctx;
269
- const { issues } = baseAudit(ctx, coreNodeRuntimePlan(topo));
265
+ const { issues } = runtimeIssues(ctx, coreNodeRuntimePlan(topo));
270
266
  const entries = edgeEntries(ctx, issues);
271
267
 
272
- const unit = relayUnitName(topo.relay.alias);
268
+ const deployment = nodeRelayJob(ctx);
269
+ const configIssue = configFileIssue(
270
+ deployment.unitPath, deployment.content, '中继服务配置', '需要更新',
271
+ );
272
+ if (configIssue) issues.push(configIssue);
273
+ const unit = deployment.unitName;
273
274
  const active = systemd.isActive(ctx.runner, unit);
274
275
  const relayPort = probe ? await relayPortListening(ctx, ctx.self.relayPort) : null;
275
276
  if (active !== 'active')
@@ -354,20 +355,13 @@ function renderEdgeLines(s, lines) {
354
355
  }
355
356
 
356
357
  function edgeTaskState(e) {
357
- const failed = Boolean(e.lastExitCode && e.lastExitCode !== '0');
358
- if (!e.registered) {
359
- return e.v6
360
- ? `当前未加载(曾收到地址 ${e.v6};它下次连接时自动恢复)`
361
- : '尚未建立(等这台笔记本第一次发起连接)';
362
- }
363
- if (e.disabled) {
364
- if (e.running) return '已标记休眠,进程尚未退出';
365
- if (failed) return `已标记休眠,但上次退出 ${e.lastExitCode};launchd 仍可能自动重拉`;
366
- return '休眠中(这台笔记本下次 ssh 或 start peer 时自动唤醒)';
367
- }
368
- if (e.running) return `运行中(仅表示任务进程,不代表线路已通)${exitNote(e.lastExitCode)}`;
369
- if (failed) return `未运行(上次退出 ${e.lastExitCode},launchd 会自动重拉)`;
370
- return '未运行(对端下次通知时会重新拉起)';
358
+ // 只列 launchd 事实,不为每种组合另写一句推断;排障解释集中在 notes
359
+ const facts = [
360
+ e.registered ? '已注册' : '未注册',
361
+ e.running ? '运行中' : '未运行',
362
+ ];
363
+ if (e.registered && e.disabled) facts.push('休眠');
364
+ return `${facts.join(',')}${exitNote(e.lastExitCode)}`;
371
365
  }
372
366
 
373
367
  function renderCoreLines(s, lines) {