ssh-net 26.10.58 → 26.10.59
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +21 -6
- package/README.md +9 -9
- package/libexec/cli.mjs +26 -43
- package/libexec/commands/install.mjs +12 -30
- package/libexec/commands/start.mjs +9 -15
- package/libexec/commands/status.mjs +16 -38
- package/libexec/commands/stop.mjs +17 -63
- package/libexec/commands/uninstall.mjs +13 -73
- package/libexec/commands/update.mjs +10 -36
- package/libexec/lib/jobctl.mjs +2 -3
- package/libexec/lib/manage.mjs +13 -15
- package/libexec/lib/paths.mjs +5 -4
- package/libexec/lib/runtime.mjs +25 -64
- package/libexec/lib/systemd.mjs +4 -3
- package/libexec/sn-route +14 -23
- package/package.json +1 -1
- package/libexec/lib/cliargs.mjs +0 -18
package/CHANGELOG.md
CHANGED
|
@@ -1,14 +1,29 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
跳号至 26.9.17 对齐累计数,13–16 永久空缺)。发布 = 改版本号 + 发
|
|
7
|
-
GitHub Release(Actions 自动跑测试后 npm publish;跳号与 0.x 的完整
|
|
8
|
-
考据见 git 史)。
|
|
3
|
+
本项目使用 npm 公共分发,版本号格式为**年.月.序号**:月内序号递增,
|
|
4
|
+
跨月从 1 开始;历史跳号不补、不另维护累计发包数。发布 = 改版本号 +
|
|
5
|
+
CHANGELOG + 发 GitHub Release,Actions 跑测试后 npm publish。
|
|
9
6
|
|
|
10
7
|
## [Unreleased]
|
|
11
8
|
|
|
9
|
+
## [26.10.59] - 2026-10-25
|
|
10
|
+
|
|
11
|
+
### 简化
|
|
12
|
+
|
|
13
|
+
- install/status 的对账只汇总有无变化,core 的 edge 任务直接列 launchd
|
|
14
|
+
事实;删除 runtime-version 版本戳与 update 后置校验,旧文件由 install
|
|
15
|
+
和 uninstall 清理
|
|
16
|
+
- stop/uninstall 始终按磁盘 job/unit 操作,不再读取 topology 或维护独立
|
|
17
|
+
恢复模式;stop peer 仍只关闭主动恢复,系统查询与停止失败照常报错
|
|
18
|
+
- sn-route 的 follower 本轮不再接管被杀的 leader,ENSURE 改为空锁文件;
|
|
19
|
+
并发仍只报一次名,失败印记过期后的下一条连接负责重试
|
|
20
|
+
- update 保留明确版本安装与防降级,删除 ETARGET 专属分支和升级/降级
|
|
21
|
+
措辞;publish 删除 latest 防反超检查,不补历史 Release,版本号按月编号
|
|
22
|
+
|
|
23
|
+
### 发布纪律
|
|
24
|
+
|
|
25
|
+
- 明确「发布」包含提交、合并回 main、推送、GitHub Release 与在线机器升级
|
|
26
|
+
|
|
12
27
|
## [26.10.58] - 2026-10-25
|
|
13
28
|
|
|
14
29
|
### 文档与文案
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
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 {
|
|
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: '
|
|
25
|
+
uninstall: { run: uninstallRun, desc: '按磁盘任务停止并删除本机组件;之后再 npm uninstall -g' },
|
|
26
26
|
start: { run: startRun, desc: '启动中继;edge 默认同时恢复 IPv6 直连(relay|peer)' },
|
|
27
|
-
stop: { run: stopRun, desc: '
|
|
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
|
-
'
|
|
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
|
|
52
|
+
throw new Error(`无法读取配置 ${file}(${err.code ?? '未知错误'}):${err.message}`);
|
|
57
53
|
}
|
|
58
54
|
}
|
|
59
55
|
|
|
60
|
-
/**
|
|
61
|
-
function
|
|
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
|
|
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
|
|
88
|
+
throw new Error(
|
|
78
89
|
`本机主机名 ${hostname} 不在 topology 的 host 行中(登记:${[...topo.byHostname.keys()].join('、')})`,
|
|
79
90
|
);
|
|
80
91
|
return {
|
|
81
|
-
|
|
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
|
-
|
|
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`);
|
|
@@ -28,24 +28,14 @@ import {
|
|
|
28
28
|
renderEdgeHost, renderEdgeLinkPlist, renderRelayPlist, renderRelayUnit, renderRoutes,
|
|
29
29
|
} from '../lib/render.mjs';
|
|
30
30
|
|
|
31
|
-
/**
|
|
31
|
+
/** 对账只汇总有无变化;具体差异看文件与 launchd/systemd,不维护逐态文案。 */
|
|
32
32
|
function compNote(results, entities) {
|
|
33
|
-
|
|
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('、')}`;
|
|
33
|
+
if (!results.some(Boolean)) return '';
|
|
44
34
|
const residentUpdated = entities.some(([name], i) =>
|
|
45
|
-
results[i]
|
|
35
|
+
results[i] && RESIDENT_SCRIPTS.includes(name));
|
|
46
36
|
return residentUpdated
|
|
47
|
-
?
|
|
48
|
-
:
|
|
37
|
+
? '后台脚本已同步,常驻进程将在下次重连后使用新版'
|
|
38
|
+
: '后台脚本已同步';
|
|
49
39
|
}
|
|
50
40
|
|
|
51
41
|
/** 安装结果文案:组件刷新/配置更新若干条 note 合并进一对括号(''=零附加信息)。 */
|
|
@@ -56,22 +46,18 @@ function notesPart(...notes) {
|
|
|
56
46
|
|
|
57
47
|
/** plist/unit 的同构部署结果统一变成用户可读的任务变化。 */
|
|
58
48
|
function deploymentNote(r, task, config) {
|
|
59
|
-
if (r.
|
|
60
|
-
|
|
61
|
-
if (r.revived) return `${task}已恢复`;
|
|
62
|
-
if (r.registered) return `${task}已注册`;
|
|
63
|
-
return '';
|
|
49
|
+
if (!r.changed) return '';
|
|
50
|
+
return r.restarted ? `${config}已更新,可能断开当前连接` : `${task}已同步`;
|
|
64
51
|
}
|
|
65
52
|
|
|
66
|
-
/** core/node 安装结果同构:服务配置未变只报刷新项,变了报中继通道已开启。 */
|
|
67
53
|
function coreNodeInstallMsg(comp, entryChanged, result, relayLine) {
|
|
68
54
|
const note = notesPart(
|
|
69
55
|
comp,
|
|
70
56
|
entryChanged ? 'edge 入口已更新' : '',
|
|
71
57
|
deploymentNote(result, '中继任务', '中继服务配置'),
|
|
72
58
|
);
|
|
73
|
-
if (
|
|
74
|
-
return
|
|
59
|
+
if (!note)
|
|
60
|
+
return `已安装,服务配置无需改动;中继通道已开启(${relayLine})`;
|
|
75
61
|
return `安装完成${note},中继通道已开启(${relayLine})`;
|
|
76
62
|
}
|
|
77
63
|
|
|
@@ -188,7 +174,7 @@ async function installEdge(ctx, args) {
|
|
|
188
174
|
});
|
|
189
175
|
const r = syncPlist(ctx, { label, plistPath, content });
|
|
190
176
|
if (r.lintError) throw new Error(`link plist 校验失败:${r.lintError}`);
|
|
191
|
-
linkChanged =
|
|
177
|
+
linkChanged = r.changed;
|
|
192
178
|
linkNote = deploymentNote(r, '本地转发任务', '本地转发配置');
|
|
193
179
|
} else {
|
|
194
180
|
// 默认零常驻:清掉加装过的 link 全部产物。plist 必须先 bootout,否则
|
|
@@ -207,13 +193,9 @@ async function installEdge(ctx, args) {
|
|
|
207
193
|
// 选路 Match 与尾部打标块(VS Code 标识 + 本机别名)
|
|
208
194
|
const routesChanged = syncConfFile(paths.routesFile, renderRoutes(topo, ctx.self.alias));
|
|
209
195
|
const depNote = localNotifyDepNote();
|
|
210
|
-
const head = linkOn
|
|
211
|
-
? '安装完成(含本地端口转发)'
|
|
212
|
-
: '安装完成(默认不安装本地端口转发;需要时运行 ssh-net install --with-link)';
|
|
196
|
+
const head = linkOn ? '安装完成(含本地端口转发)' : '安装完成';
|
|
213
197
|
const base = (!comp && !linkChanged && !routesChanged && flag === undefined)
|
|
214
|
-
?
|
|
215
|
-
? '已安装(本地端口转发已启用),无需改动'
|
|
216
|
-
: '已安装,无需改动(未安装本地端口转发)')
|
|
198
|
+
? '已安装,配置无需改动'
|
|
217
199
|
: `${head}${notesPart(comp, linkNote, routesChanged ? 'ssh 配置片段已更新' : '')}`;
|
|
218
200
|
const startNote = ctx.state.isOn('relay')
|
|
219
201
|
? ''
|
|
@@ -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
|
-
//
|
|
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
|
|
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 =
|
|
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
|
-
|
|
57
|
-
|
|
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
|
|
68
|
+
return `relay 已开启(${peerNote})`;
|
|
76
69
|
}
|
|
77
70
|
// core/node 是单总开关(等同 --all):显式 -a/--all 收下,其余参数拒绝
|
|
78
|
-
|
|
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';
|
|
@@ -23,8 +21,8 @@ import {
|
|
|
23
21
|
} from '../lib/render.mjs';
|
|
24
22
|
|
|
25
23
|
/**
|
|
26
|
-
*
|
|
27
|
-
* @returns {{issues:string[], entityAudits:{name:string,audit:string}[]
|
|
24
|
+
* 各角色共用的实体+软链体检;完全未安装只报根因,不级联报其他产物。
|
|
25
|
+
* @returns {{issues:string[], entityAudits:{name:string,audit:string}[]}}
|
|
28
26
|
*/
|
|
29
27
|
function runtimeIssues(ctx, { entities, links }) {
|
|
30
28
|
const issues = [];
|
|
@@ -43,21 +41,10 @@ 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
|
-
|
|
47
|
-
|
|
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
|
|
|
@@ -146,7 +133,7 @@ async function collectEdge(ctx, probe) {
|
|
|
146
133
|
const { topo, paths, state } = ctx;
|
|
147
134
|
const linkInstalled = fs.existsSync(paths.linkEnabledFile);
|
|
148
135
|
const plan = edgeRuntimePlan(topo, linkInstalled);
|
|
149
|
-
const { issues, entityAudits } =
|
|
136
|
+
const { issues, entityAudits } = runtimeIssues(ctx, plan);
|
|
150
137
|
|
|
151
138
|
// routes 被手删/过期会让 ssh 静默退回 lan 直连,是故障。sn-route 没装时
|
|
152
139
|
// 上面已报「尚未安装」,不重复
|
|
@@ -210,7 +197,7 @@ async function collectEdge(ctx, probe) {
|
|
|
210
197
|
|
|
211
198
|
async function collectCore(ctx, probe) {
|
|
212
199
|
const { topo, paths } = ctx;
|
|
213
|
-
const { issues } =
|
|
200
|
+
const { issues } = runtimeIssues(ctx, coreNodeRuntimePlan(topo));
|
|
214
201
|
|
|
215
202
|
// disabled override 是整域同一份字典:relay job 与各 edge job 共用一次拉取
|
|
216
203
|
const dmap = launchd.disabledMap(ctx.runner, ctx.uid);
|
|
@@ -237,8 +224,7 @@ async function collectCore(ctx, probe) {
|
|
|
237
224
|
const bad = [];
|
|
238
225
|
if (callbackEntityAudit !== 'ok')
|
|
239
226
|
bad.push(`连接脚本${callbackEntityAudit === 'missing' ? '缺失' : '过期'}`);
|
|
240
|
-
|
|
241
|
-
if (linkState !== 'ok') bad.push(`软链(${linkState})`);
|
|
227
|
+
if (!auditLink(paths.binScript(linkName), entityName)) bad.push('服务软链异常');
|
|
242
228
|
if (bad.length)
|
|
243
229
|
issues.push(`${e.alias} 的自动连接脚本异常(${bad.join('、')})——它下次连过来时会自动刷新(或跑一次 ssh-net install 手动修复)`);
|
|
244
230
|
}
|
|
@@ -248,7 +234,6 @@ async function collectCore(ctx, probe) {
|
|
|
248
234
|
disabled: st.disabled,
|
|
249
235
|
running: st.running,
|
|
250
236
|
lastExitCode: st.lastExitCode,
|
|
251
|
-
v6: ctx.state.readEdgeV6(e.alias),
|
|
252
237
|
entry: entryByAlias.get(e.alias),
|
|
253
238
|
});
|
|
254
239
|
}
|
|
@@ -266,7 +251,7 @@ async function collectCore(ctx, probe) {
|
|
|
266
251
|
|
|
267
252
|
async function collectNode(ctx, probe) {
|
|
268
253
|
const { topo } = ctx;
|
|
269
|
-
const { issues } =
|
|
254
|
+
const { issues } = runtimeIssues(ctx, coreNodeRuntimePlan(topo));
|
|
270
255
|
const entries = edgeEntries(ctx, issues);
|
|
271
256
|
|
|
272
257
|
const unit = relayUnitName(topo.relay.alias);
|
|
@@ -354,20 +339,13 @@ function renderEdgeLines(s, lines) {
|
|
|
354
339
|
}
|
|
355
340
|
|
|
356
341
|
function edgeTaskState(e) {
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
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 '未运行(对端下次通知时会重新拉起)';
|
|
342
|
+
// 只列 launchd 事实,不为每种组合另写一句推断;排障解释集中在 notes
|
|
343
|
+
const facts = [
|
|
344
|
+
e.registered ? '已注册' : '未注册',
|
|
345
|
+
e.running ? '运行中' : '未运行',
|
|
346
|
+
];
|
|
347
|
+
if (e.registered && e.disabled) facts.push('休眠');
|
|
348
|
+
return `${facts.join(',')}${exitNote(e.lastExitCode)}`;
|
|
371
349
|
}
|
|
372
350
|
|
|
373
351
|
function renderCoreLines(s, lines) {
|
|
@@ -1,26 +1,21 @@
|
|
|
1
|
-
// stop
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
// --without-link)。重复 stop 是幂等确认,措辞区分「已关闭」与「已是关闭状态」。
|
|
5
|
-
// core disable+bootout uplink-relay(不动 uplink-edge);node stop+disable。
|
|
1
|
+
// stop:只按磁盘上的 ssh-net 任务操作,不读 topology。peer 只删开关,
|
|
2
|
+
// 不闪断已有回拨;relay/缺省/-a/--all 级联清两开关,并停止本机全部
|
|
3
|
+
// link/relay 任务。core 的 uplink-edge 不在停止范围内(人工全清用 uninstall)。
|
|
6
4
|
import * as launchd from '../lib/launchd.mjs';
|
|
7
5
|
import * as systemd from '../lib/systemd.mjs';
|
|
8
6
|
import { reapEdgeLink } from '../lib/jobctl.mjs';
|
|
9
|
-
import { assertUnitSwitchArgs, recoveryResult } from '../lib/cliargs.mjs';
|
|
10
7
|
import { SWITCH_NAMES } from '../lib/state.mjs';
|
|
11
|
-
import { linkLabel, relayLabel, relayLinkName
|
|
8
|
+
import { linkLabel, relayLabel, relayLinkName } from '../lib/render.mjs';
|
|
12
9
|
|
|
13
10
|
function parseTarget(args) {
|
|
14
|
-
if (args.length === 0 || args.length === 1 &&
|
|
15
|
-
return
|
|
16
|
-
if (args.length === 1 && SWITCH_NAMES.includes(args[0]))
|
|
17
|
-
return args[0];
|
|
11
|
+
if (args.length === 0 || args.length === 1 && ['-a', '--all'].includes(args[0]))
|
|
12
|
+
return 'relay';
|
|
13
|
+
if (args.length === 1 && SWITCH_NAMES.includes(args[0])) return args[0];
|
|
18
14
|
if (args.length === 1 && args[0] === 'link')
|
|
19
15
|
throw new Error('link 不是运行时开关:移除本地转发组件跑 ssh-net install --without-link(卸载会一并清掉)');
|
|
20
16
|
throw new Error('用法:stop [-a|--all|relay|peer](不带参数或 -a=全部关闭)');
|
|
21
17
|
}
|
|
22
18
|
|
|
23
|
-
/** 三类停止原语:正常与恢复路径只负责发现 label/unit,顺序和失败处理共用。 */
|
|
24
19
|
function stopRelayJob(ctx, label) {
|
|
25
20
|
const d = launchd.disable(ctx.runner, ctx.uid, label);
|
|
26
21
|
if (d.code !== 0) throw new Error(`disable 失败(${label}):${d.stderr.trim()}`);
|
|
@@ -41,70 +36,29 @@ function reapLinkOrThrow(ctx, label) {
|
|
|
41
36
|
throw new Error(`${label} 发 TERM 后仍在运行。手工排查:launchctl print gui/${ctx.uid}/${label}`);
|
|
42
37
|
}
|
|
43
38
|
|
|
44
|
-
/**
|
|
45
|
-
function
|
|
46
|
-
return `${recoveryResult(ctx, action)}\n修复配置:dot apply ssh;之后运行 ssh-net status 检查`;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function stopRecovery(ctx, args) {
|
|
50
|
-
assertUnitSwitchArgs(args, 'stop',
|
|
51
|
-
'配置不可用时 stop 只支持不带参数或 -a/--all;修复配置后再按 relay/peer 分别操作');
|
|
39
|
+
/** 停止磁盘发现的全部 link/relay 任务;改名前的残留也一并覆盖。 */
|
|
40
|
+
function stopJobs(ctx) {
|
|
52
41
|
if ((ctx.platform ?? process.platform) === 'linux') {
|
|
53
42
|
const units = systemd.unitNamesOnDisk(ctx.paths.systemdUnitDir, relayLinkName(''));
|
|
54
43
|
if (units.length > 0 && ctx.uid !== 0)
|
|
55
44
|
throw new Error('停止 node 的中继服务需要 root,请运行 sudo -i ssh-net stop');
|
|
56
|
-
ctx.state.removeOn(['relay', 'peer']);
|
|
57
45
|
for (const unit of units) stopNodeUnit(ctx, unit);
|
|
58
|
-
return
|
|
59
|
-
? `已停止并禁用 ${units.length} 个中继服务`
|
|
60
|
-
: '未发现中继服务;已清除本机开关');
|
|
46
|
+
return `已停止并禁用 ${units.length} 个中继服务`;
|
|
61
47
|
}
|
|
62
48
|
|
|
63
49
|
const links = launchd.labelsOnDisk(ctx.paths.launchAgentsDir, linkLabel(''));
|
|
64
50
|
const relays = launchd.labelsOnDisk(ctx.paths.launchAgentsDir, relayLabel(''));
|
|
65
|
-
ctx.state.removeOn(['relay', 'peer']);
|
|
66
51
|
for (const label of links) reapLinkOrThrow(ctx, label);
|
|
67
52
|
for (const label of relays) stopRelayJob(ctx, label);
|
|
68
|
-
return
|
|
69
|
-
`已清除 relay/peer 开关,停止 ${links.length} 个本地转发任务和 ${relays.length} 个中继任务`);
|
|
53
|
+
return `已停止 ${links.length} 个本地转发任务和 ${relays.length} 个中继任务`;
|
|
70
54
|
}
|
|
71
55
|
|
|
72
56
|
export function run(ctx, args = []) {
|
|
73
|
-
|
|
74
|
-
if (
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
if (target === 'peer') {
|
|
79
|
-
ctx.state.removeOn(['peer']);
|
|
80
|
-
if (!before.peer) return 'peer 已是关闭状态(无需改动)';
|
|
81
|
-
// 软关:不再通知 core 来连。已建立的连接继续用;装了 link,其转发 ssh
|
|
82
|
-
// 重连时也不会通知,自然只走中继
|
|
83
|
-
return 'peer 已关闭:不再主动通知 core 恢复 IPv6 直连;现有直连断开前仍可供新 ssh 使用,断开后走中继';
|
|
84
|
-
}
|
|
85
|
-
// 全关 / 只关 relay:级联删两开关(peer 依赖 relay),硬停 link job
|
|
86
|
-
// (PathState 见 RELAY_ON 消失不再重拉;未装附加件直接 gone)
|
|
87
|
-
const alreadyAllOff = !before.relay && !before.peer;
|
|
88
|
-
ctx.state.removeOn(['relay', 'peer']);
|
|
89
|
-
reapLinkOrThrow(ctx, linkLabel(ctx.topo.core.alias));
|
|
90
|
-
if (target === null) {
|
|
91
|
-
return alreadyAllOff
|
|
92
|
-
? '隧道已是关闭状态(无需改动)'
|
|
93
|
-
: '隧道已关闭;core 会在现有连接退出后停止重试,下次开启自动恢复';
|
|
94
|
-
}
|
|
95
|
-
return alreadyAllOff
|
|
96
|
-
? 'relay 已是关闭状态(peer 本就没开)'
|
|
97
|
-
: 'relay 和 peer 已一并关闭;core 会在现有连接退出后停止重试,下次开启自动恢复';
|
|
57
|
+
const target = parseTarget(args);
|
|
58
|
+
if (target === 'peer') {
|
|
59
|
+
ctx.state.removeOn(['peer']);
|
|
60
|
+
return 'peer 已关闭:不再主动恢复 IPv6 直连;现有直连断开前仍可供新 ssh 使用,断开后走中继';
|
|
98
61
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
if (ctx.role === 'core') {
|
|
102
|
-
stopRelayJob(ctx, relayLabel(ctx.topo.relay.alias));
|
|
103
|
-
return '中继通道已关闭(现有 IPv6 直连不受影响)';
|
|
104
|
-
}
|
|
105
|
-
if (ctx.role === 'node') {
|
|
106
|
-
stopNodeUnit(ctx, relayUnitName(ctx.topo.relay.alias));
|
|
107
|
-
return '中继通道已关闭(重启后保持关闭)';
|
|
108
|
-
}
|
|
109
|
-
throw new Error(`未知角色:${ctx.role}`);
|
|
62
|
+
ctx.state.removeOn(['relay', 'peer']);
|
|
63
|
+
return `relay 和 peer 已一并关闭;${stopJobs(ctx)}`;
|
|
110
64
|
}
|
|
@@ -1,30 +1,25 @@
|
|
|
1
|
-
// uninstall
|
|
2
|
-
//
|
|
3
|
-
//
|
|
1
|
+
// uninstall:只按磁盘上的 ssh-net 任务与产物清理,不读 topology。
|
|
2
|
+
// 先 bootout/stop 全部 job(TERM trap 转发,不留孤儿 ssh),再删
|
|
3
|
+
// plist/unit、运行时脚本、状态与日志;topology 改名前的残留由 glob 覆盖。
|
|
4
4
|
import fs from 'node:fs';
|
|
5
5
|
import path from 'node:path';
|
|
6
6
|
import { removeJob } from '../lib/manage.mjs';
|
|
7
7
|
import * as launchd from '../lib/launchd.mjs';
|
|
8
8
|
import * as systemd from '../lib/systemd.mjs';
|
|
9
9
|
import {
|
|
10
|
-
linkLabel,
|
|
11
|
-
relayLabel, relayLinkName, relayUnitName, edgeLabel,
|
|
10
|
+
linkLabel, relayLabel, relayLinkName, edgeLabel,
|
|
12
11
|
} from '../lib/render.mjs';
|
|
13
|
-
import { recoveryResult } from '../lib/cliargs.mjs';
|
|
14
12
|
|
|
15
13
|
const rm = (p) => fs.rmSync(p, { recursive: true, force: true });
|
|
16
14
|
|
|
17
|
-
/** 三角色共同产物:整棵 bin/state/conf.d + 版本戳 + link 意图 marker
|
|
18
|
-
* (marker 只 edge 可能有,其余角色 force rm 不存在的文件无害)。 */
|
|
19
15
|
function removeArtifacts(ctx) {
|
|
20
16
|
rm(ctx.paths.binDir);
|
|
21
17
|
rm(ctx.paths.stateDir);
|
|
22
18
|
rm(ctx.paths.confDir);
|
|
23
|
-
rm(ctx.paths.
|
|
19
|
+
rm(ctx.paths.legacyRuntimeVersionFile);
|
|
24
20
|
rm(ctx.paths.linkEnabledFile);
|
|
25
21
|
}
|
|
26
22
|
|
|
27
|
-
/** 日志也按工具独占前缀清理,覆盖 plist 已丢但日志仍在的恢复场景。 */
|
|
28
23
|
function removeSshNetLogs(ctx) {
|
|
29
24
|
let files = [];
|
|
30
25
|
try { files = fs.readdirSync(ctx.paths.logsDir); } catch (err) {
|
|
@@ -36,7 +31,6 @@ function removeSshNetLogs(ctx) {
|
|
|
36
31
|
}
|
|
37
32
|
}
|
|
38
33
|
|
|
39
|
-
/** 删除原语由正常与恢复路径共用;目标清单的发现方式留在各入口。 */
|
|
40
34
|
function removeJobs(ctx, labels) {
|
|
41
35
|
for (const label of labels) removeJob(ctx, label, ctx.paths.plistFile(label));
|
|
42
36
|
}
|
|
@@ -50,64 +44,22 @@ function removeNodeUnits(ctx, units) {
|
|
|
50
44
|
rm(path.join(ctx.paths.systemdUnitDir, unit));
|
|
51
45
|
}
|
|
52
46
|
// daemon-reload 失败不能吞:systemd 仍持有已删 unit,Restart=always 会
|
|
53
|
-
// 留着/重拉 ghost
|
|
47
|
+
// 留着/重拉 ghost 进程
|
|
54
48
|
const dr = systemd.daemonReload(ctx.runner);
|
|
55
49
|
if (dr.code !== 0) throw new Error(`systemctl daemon-reload 失败:${dr.stderr.trim()}`);
|
|
56
50
|
}
|
|
57
51
|
|
|
58
|
-
function
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
linkLabel(ctx.topo.core.alias),
|
|
62
|
-
...launchd.labelsOnDisk(ctx.paths.launchAgentsDir, linkLabel('')),
|
|
63
|
-
]);
|
|
64
|
-
removeJobs(ctx, labels);
|
|
65
|
-
removeArtifacts(ctx);
|
|
66
|
-
removeSshNetLogs(ctx);
|
|
67
|
-
return '已卸载。最后一步:npm uninstall -g ssh-net';
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function uninstallCore(ctx) {
|
|
71
|
-
// label → plist 路径:磁盘 glob(topology 已删的残留)∪ topology 在册 edge
|
|
72
|
-
const edgeJobs = new Map();
|
|
73
|
-
for (const alias of launchd.edgeAliasesOnDisk(ctx.paths.launchAgentsDir))
|
|
74
|
-
edgeJobs.set(edgeLabel(alias), alias);
|
|
75
|
-
for (const e of ctx.topo.edges)
|
|
76
|
-
edgeJobs.set(edgeLabel(e.alias), e.alias);
|
|
77
|
-
removeJobs(ctx, edgeJobs.keys());
|
|
78
|
-
|
|
79
|
-
const relays = new Set([
|
|
80
|
-
relayLabel(ctx.topo.relay.alias),
|
|
81
|
-
...launchd.labelsOnDisk(ctx.paths.launchAgentsDir, relayLabel('')),
|
|
82
|
-
]);
|
|
83
|
-
removeJobs(ctx, relays);
|
|
84
|
-
|
|
85
|
-
removeArtifacts(ctx); // 全部 edge-<别名> 动态片段随 conf.d 整棵删
|
|
86
|
-
removeSshNetLogs(ctx);
|
|
87
|
-
return `已卸载(中继通道与 ${edgeJobs.size} 台笔记本的自动连接任务)。最后一步:npm uninstall -g ssh-net`;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
function uninstallNode(ctx) {
|
|
91
|
-
if (ctx.uid !== 0)
|
|
92
|
-
throw new Error('node 卸载写 /etc/systemd/system 需要 root,请运行 sudo -i ssh-net uninstall');
|
|
93
|
-
const units = new Set([
|
|
94
|
-
relayUnitName(ctx.topo.relay.alias),
|
|
95
|
-
...systemd.unitNamesOnDisk(ctx.paths.systemdUnitDir, relayLinkName('')),
|
|
96
|
-
]);
|
|
97
|
-
removeNodeUnits(ctx, units);
|
|
98
|
-
removeArtifacts(ctx);
|
|
99
|
-
return '已卸载。最后一步:npm uninstall -g ssh-net';
|
|
100
|
-
}
|
|
52
|
+
export function run(ctx, args = []) {
|
|
53
|
+
if (args.length)
|
|
54
|
+
throw new Error('用法:uninstall 不带参数');
|
|
101
55
|
|
|
102
|
-
/** 配置不可用:按磁盘上的全部 ssh-net 任务卸载,不需要猜本机角色或别名。 */
|
|
103
|
-
function uninstallRecovery(ctx) {
|
|
104
56
|
if ((ctx.platform ?? process.platform) === 'linux') {
|
|
57
|
+
if (ctx.uid !== 0)
|
|
58
|
+
throw new Error('node 卸载写 /etc/systemd/system 需要 root,请运行 sudo -i ssh-net uninstall');
|
|
105
59
|
const units = systemd.unitNamesOnDisk(ctx.paths.systemdUnitDir, relayLinkName(''));
|
|
106
|
-
if (units.length > 0 && ctx.uid !== 0)
|
|
107
|
-
throw new Error('卸载 node 的中继服务需要 root,请运行 sudo -i ssh-net uninstall');
|
|
108
60
|
if (units.length > 0) removeNodeUnits(ctx, units);
|
|
109
61
|
removeArtifacts(ctx);
|
|
110
|
-
return
|
|
62
|
+
return '已卸载。最后一步:npm uninstall -g ssh-net';
|
|
111
63
|
}
|
|
112
64
|
|
|
113
65
|
const labels = new Set([
|
|
@@ -118,17 +70,5 @@ function uninstallRecovery(ctx) {
|
|
|
118
70
|
removeJobs(ctx, labels);
|
|
119
71
|
removeArtifacts(ctx);
|
|
120
72
|
removeSshNetLogs(ctx);
|
|
121
|
-
return
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
export function run(ctx, args = []) {
|
|
125
|
-
if (args.length)
|
|
126
|
-
throw new Error('用法:uninstall 不带参数');
|
|
127
|
-
if (ctx.configError) return uninstallRecovery(ctx);
|
|
128
|
-
switch (ctx.role) {
|
|
129
|
-
case 'edge': return uninstallEdge(ctx);
|
|
130
|
-
case 'core': return uninstallCore(ctx);
|
|
131
|
-
case 'node': return uninstallNode(ctx);
|
|
132
|
-
default: throw new Error(`未知角色:${ctx.role}`);
|
|
133
|
-
}
|
|
73
|
+
return '已卸载。最后一步:npm uninstall -g ssh-net';
|
|
134
74
|
}
|
|
@@ -6,16 +6,14 @@
|
|
|
6
6
|
// 只做一轮、不轮询等待;知道目标版号时直接 update <版号>。
|
|
7
7
|
//
|
|
8
8
|
// 刻意「换装后重新 exec 新 bin」而不是在当前进程里直接调 installRun:
|
|
9
|
-
//
|
|
10
|
-
//
|
|
11
|
-
// runtime-version 戳与 status 的对账都依赖新进程里的新常量,用旧进程
|
|
12
|
-
// 跑 install 会盖一个旧版本戳、越对账越乱。
|
|
9
|
+
// 当前进程加载的是旧包代码与包内路径,只有新进程才能保证 install 与刚装
|
|
10
|
+
// 上的版本一致。
|
|
13
11
|
//
|
|
14
12
|
// 边界:node 角色需要 root(install 写 /etc/systemd/system),先检查再动
|
|
15
|
-
// npm,避免「包装上了、install 被拒」的半截态;core
|
|
13
|
+
// npm,避免「包装上了、install 被拒」的半截态;core 侧已注册 edge 的
|
|
16
14
|
// uplink-edge 实体随 install 刷新,job 参数仍等各 edge 下次报名对账。
|
|
17
|
-
import { readRuntimeVersion, RUNTIME_VERSION } from '../lib/runtime.mjs';
|
|
18
15
|
import { cmpVersion, VERSION_RE } from '../lib/version.mjs';
|
|
16
|
+
import pkg from '../../package.json' with { type: 'json' };
|
|
19
17
|
|
|
20
18
|
const NPM_TIMEOUT = 180_000; // 慢网络下全局装包的余量
|
|
21
19
|
const QUERY_TIMEOUT = 60_000; // view 只读查询
|
|
@@ -70,9 +68,9 @@ export async function run(ctx, args = []) {
|
|
|
70
68
|
const latest = npmLatest(runner);
|
|
71
69
|
if (latest === null)
|
|
72
70
|
throw new Error('没能从 npm view 确认最新版,未执行安装——稍后重试;知道目标版号可直接 ssh-net update <版本号>');
|
|
73
|
-
const relation = cmpVersion(latest,
|
|
71
|
+
const relation = cmpVersion(latest, pkg.version);
|
|
74
72
|
if (relation < 0)
|
|
75
|
-
throw new Error(`npm 返回的最新版 ${latest} 比当前版本 ${
|
|
73
|
+
throw new Error(`npm 返回的最新版 ${latest} 比当前版本 ${pkg.version} 更旧,已停止且未执行安装——稍后重试;知道目标版号可直接 ssh-net update <版本号>`);
|
|
76
74
|
if (relation === 0) return '已是最新版本';
|
|
77
75
|
resolved = latest;
|
|
78
76
|
}
|
|
@@ -81,18 +79,10 @@ export async function run(ctx, args = []) {
|
|
|
81
79
|
const r = npmInstall(runner, resolved);
|
|
82
80
|
if (r.error && r.error.code === 'ENOENT')
|
|
83
81
|
throw new Error(NPM_NOT_FOUND);
|
|
84
|
-
if (r.code !== 0)
|
|
85
|
-
|
|
86
|
-
const why = '还没传播到本机打到的 registry 节点(常见于刚发布几分钟内)';
|
|
87
|
-
const kind = target !== null ? '指定版本' : '最新版';
|
|
88
|
-
const tail = `${kind} ${resolved} ${why}——等几分钟重跑 ssh-net update ${resolved},不用换命令。`;
|
|
89
|
-
throw new Error(`${tail} npm 输出:${r.stderr.trim()}`);
|
|
90
|
-
}
|
|
91
|
-
throw new Error(`npm 安装 ${resolved} 失败:${failWhy(r)}(网络或 registry 问题;已装的版本不受影响,稍后重试)`);
|
|
92
|
-
}
|
|
82
|
+
if (r.code !== 0)
|
|
83
|
+
throw new Error(`npm 安装 ${resolved} 失败:${failWhy(r)}(已装的版本不受影响;网络或 registry 问题稍后重试,知道目标版号可直接 ssh-net update <版本号>)`);
|
|
93
84
|
|
|
94
|
-
// 版号地址 code 0
|
|
95
|
-
// 换装后用新 bin 跑 install;变更摘要由 install 统一产出,update 只转述
|
|
85
|
+
// 版号地址 code 0 即装上该版;换装后用新 bin 跑 install,摘要由 install 统一产出
|
|
96
86
|
progress('正在同步本机组件…');
|
|
97
87
|
const inst = runner.run('ssh-net', ['install'], { timeout: INSTALL_TIMEOUT });
|
|
98
88
|
if (inst.error && inst.error.code === 'ENOENT')
|
|
@@ -100,21 +90,5 @@ export async function run(ctx, args = []) {
|
|
|
100
90
|
if (inst.code !== 0)
|
|
101
91
|
throw new Error(`npm 包已切换,但 install 刷新没成功:${failWhy(inst)}——手动跑一次 ssh-net install 修复`);
|
|
102
92
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
// 后置校验:真换装后新 bin 的 install 必把版本戳刷到刚装上的版本
|
|
106
|
-
// (ensureRuntime 无条件 refreshStamp);戳没跟上说明 PATH 解析到了旧
|
|
107
|
-
// ssh-net,install 跑在旧代码上、升级静默没生效(status 的 runtime-version
|
|
108
|
-
// 对账能查到,但当场报比事后对账快)。同版修复新旧版本恒等,校验无意义,跳过
|
|
109
|
-
const stamp = readRuntimeVersion(ctx.paths);
|
|
110
|
-
const stampWarning = rel !== 0 && stamp !== null && stamp !== resolved
|
|
111
|
-
? `⚠️ 后台脚本版本仍为 ${stamp}——刚才的 install 可能跑了旧 ssh-net;用 which -a ssh-net 检查 PATH,再手动运行 ssh-net install`
|
|
112
|
-
: '';
|
|
113
|
-
|
|
114
|
-
const head = target !== null && rel < 0
|
|
115
|
-
? `已切换到指定版本 ${resolved}(原版本 ${RUNTIME_VERSION},这是降级)`
|
|
116
|
-
: rel === 0
|
|
117
|
-
? `当前已是指定版本 ${resolved}`
|
|
118
|
-
: `已升级到 ${resolved}`;
|
|
119
|
-
return [head, inst.stdout.trim(), stampWarning].filter(Boolean).join('\n');
|
|
93
|
+
return [`已安装 ssh-net@${resolved}`, inst.stdout.trim()].filter(Boolean).join('\n');
|
|
120
94
|
}
|
package/libexec/lib/jobctl.mjs
CHANGED
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
// 开关是 stop 的命令语义。未装 link(label 不在册)直接 gone。超时报错给手工
|
|
4
4
|
// 排障命令,绝不自动 KILL。同步 CLI 用 Atomics.wait 节流。
|
|
5
5
|
import * as launchd from './launchd.mjs';
|
|
6
|
-
import { linkLabel } from './render.mjs';
|
|
7
6
|
|
|
8
7
|
const WAIT_MS = 5_000;
|
|
9
8
|
const POLL_MS = 200;
|
|
@@ -21,10 +20,10 @@ function isGone(ctx, label) {
|
|
|
21
20
|
|
|
22
21
|
/**
|
|
23
22
|
* @param {object} ctx
|
|
24
|
-
* @param {string}
|
|
23
|
+
* @param {string} label 磁盘发现的 link job label
|
|
25
24
|
* @returns {{gone:boolean, label:string}}
|
|
26
25
|
*/
|
|
27
|
-
export function reapEdgeLink(ctx, label
|
|
26
|
+
export function reapEdgeLink(ctx, label) {
|
|
28
27
|
if (!launchd.printState(ctx.runner, ctx.uid, label, true).registered)
|
|
29
28
|
return { gone: true, label };
|
|
30
29
|
|
package/libexec/lib/manage.mjs
CHANGED
|
@@ -15,7 +15,7 @@ import { readFileOrNull } from './fsutil.mjs';
|
|
|
15
15
|
* 是否踢活:install 对账不踢(不断连),start 要踢
|
|
16
16
|
* @param {boolean} [opts.reviveRunningDisabled=false] registered+disabled 但
|
|
17
17
|
* 还在跑时是否踢重启:install 要 revive,start 只清 override、不闪断
|
|
18
|
-
*
|
|
18
|
+
* 只执行必要的加载/恢复动作;是否有配置变化由 syncPlist 另行判断
|
|
19
19
|
*/
|
|
20
20
|
export function ensureLoaded(ctx, { label, plistPath }, {
|
|
21
21
|
kickWhenStopped = false,
|
|
@@ -25,33 +25,33 @@ export function ensureLoaded(ctx, { label, plistPath }, {
|
|
|
25
25
|
const st = launchd.inspect(runner, uid, label);
|
|
26
26
|
if (!st.registered) {
|
|
27
27
|
launchd.enableBootstrap(runner, uid, label, plistPath);
|
|
28
|
-
return
|
|
28
|
+
return;
|
|
29
29
|
}
|
|
30
30
|
if (st.disabled && (!st.running || reviveRunningDisabled)) {
|
|
31
31
|
const k = launchd.kickstartEnabled(runner, uid, label);
|
|
32
32
|
if (k.code !== 0) throw new Error(`kickstart 失败(${label}):${k.stderr.trim()}`);
|
|
33
|
-
return
|
|
33
|
+
return;
|
|
34
34
|
}
|
|
35
35
|
if (st.disabled) {
|
|
36
36
|
// 在跑但 override 残留:只 enable 清掉,防退出后不重拉,不打断现连接
|
|
37
37
|
const en = launchd.enable(runner, uid, label);
|
|
38
38
|
if (en.code !== 0) throw new Error(`enable 失败(${label}):${en.stderr.trim()}`);
|
|
39
|
-
return
|
|
39
|
+
return;
|
|
40
40
|
}
|
|
41
41
|
if (kickWhenStopped && !st.running) {
|
|
42
42
|
const k = launchd.kickstart(runner, uid, label);
|
|
43
43
|
if (k.code !== 0) throw new Error(`kickstart 失败(${label}):${k.stderr.trim()}`);
|
|
44
|
-
return
|
|
44
|
+
return;
|
|
45
45
|
}
|
|
46
|
-
return 'unchanged';
|
|
47
46
|
}
|
|
48
47
|
|
|
49
48
|
/**
|
|
50
49
|
* 同步一个 plist 到磁盘并按需注册(幂等、不断连)。
|
|
51
50
|
* @param {object} ctx
|
|
52
51
|
* @param {{label:string, plistPath:string, content:string}} job
|
|
53
|
-
* @returns {{
|
|
54
|
-
*
|
|
52
|
+
* @returns {{changed:boolean, restarted?:boolean, lintError?:string}}
|
|
53
|
+
* changed 只表示 plist 内容变化,服务恢复不计入;restarted 只在替换已注册
|
|
54
|
+
* job 的配置时为真,用于提示可能断连
|
|
55
55
|
*/
|
|
56
56
|
export function syncPlist(ctx, { label, plistPath, content }) {
|
|
57
57
|
const { runner, uid, paths } = ctx;
|
|
@@ -62,13 +62,11 @@ export function syncPlist(ctx, { label, plistPath, content }) {
|
|
|
62
62
|
|
|
63
63
|
if (existing === content) {
|
|
64
64
|
// registered+disabled(stop 中途失败/手工 disable)不是「已部署」:
|
|
65
|
-
// enable 后拉起,否则 install
|
|
66
|
-
|
|
65
|
+
// enable 后拉起,否则 install 报配置未变却没在跑、start 的 kickstart 被拒
|
|
66
|
+
ensureLoaded(ctx, { label, plistPath }, {
|
|
67
67
|
reviveRunningDisabled: true,
|
|
68
68
|
});
|
|
69
|
-
|
|
70
|
-
if (action === 'bootstrapped') return { registered: true };
|
|
71
|
-
return { revived: true };
|
|
69
|
+
return { changed: false };
|
|
72
70
|
}
|
|
73
71
|
|
|
74
72
|
// 内容变化(或首次):先 lint tmp,过了再动现文件
|
|
@@ -77,7 +75,7 @@ export function syncPlist(ctx, { label, plistPath, content }) {
|
|
|
77
75
|
const lint = launchd.lintPlist(runner, tmp);
|
|
78
76
|
if (lint.code !== 0) {
|
|
79
77
|
fs.rmSync(tmp, { force: true });
|
|
80
|
-
return { lintError: lint.stderr.trim() || `plutil exit ${lint.code}` };
|
|
78
|
+
return { changed: false, lintError: lint.stderr.trim() || `plutil exit ${lint.code}` };
|
|
81
79
|
}
|
|
82
80
|
|
|
83
81
|
// 只要 registered 一个事实:printState 即可,不跑全域 print-disabled
|
|
@@ -94,7 +92,7 @@ export function syncPlist(ctx, { label, plistPath, content }) {
|
|
|
94
92
|
//(停机态)——真失败(override 数据库损坏)下 bootstrap 也过不去(error 5),
|
|
95
93
|
// 可见性优先于「也许 bootstrap 能侥幸成功」
|
|
96
94
|
launchd.enableBootstrap(runner, uid, label, plistPath);
|
|
97
|
-
return {
|
|
95
|
+
return { changed: true, restarted: st.registered };
|
|
98
96
|
}
|
|
99
97
|
|
|
100
98
|
/**
|
package/libexec/lib/paths.mjs
CHANGED
|
@@ -30,8 +30,8 @@ export function packageScriptPath(scriptName) {
|
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
/**
|
|
33
|
-
* 本机全部产物与配置路径。config 在 dotfiles;bin/state
|
|
34
|
-
*
|
|
33
|
+
* 本机全部产物与配置路径。config 在 dotfiles;bin/state 是本工具产物,
|
|
34
|
+
* 全部收在 ~/.ssh/ssh-net/ 一棵树。
|
|
35
35
|
* @param {NodeJS.ProcessEnv} [env]
|
|
36
36
|
*/
|
|
37
37
|
export function machinePaths(env = process.env) {
|
|
@@ -50,7 +50,8 @@ export function machinePaths(env = process.env) {
|
|
|
50
50
|
binDir: path.join(sshNetDir, 'bin'),
|
|
51
51
|
/** @param {string} name bin/ 下产物名(实体或服务软链) */
|
|
52
52
|
binScript: (name) => path.join(sshNetDir, 'bin', name),
|
|
53
|
-
|
|
53
|
+
// 旧安装版本戳(26.10.59 退役):只供对账/卸载做迁移清理
|
|
54
|
+
legacyRuntimeVersionFile: path.join(sshNetDir, 'runtime-version'),
|
|
54
55
|
stateDir,
|
|
55
56
|
// ssh Include 片段目录(design §4.2):routes 仅 edge install 写;
|
|
56
57
|
// edge-<别名> 由 core/node install 按 topology 渲染。Include glob 零匹配
|
|
@@ -77,7 +78,7 @@ export function machinePaths(env = process.env) {
|
|
|
77
78
|
// PROBE_FAIL:sn-route 的失败印记(内容 epoch,15s TTL)。文件名是
|
|
78
79
|
// paths ↔ sn-route 两处契约(constants.test 锚)
|
|
79
80
|
probeFailFile: path.join(stateDir, 'PROBE_FAIL'),
|
|
80
|
-
// ENSURE:sn-route 按需确保的 single-flight
|
|
81
|
+
// ENSURE:sn-route 按需确保的 single-flight 锁(内容无意义,互斥在 fd 上)
|
|
81
82
|
ensureLockFile: path.join(stateDir, 'ENSURE'),
|
|
82
83
|
edgesDir: path.join(stateDir, 'edges'),
|
|
83
84
|
launchAgentsDir,
|
package/libexec/lib/runtime.mjs
CHANGED
|
@@ -4,21 +4,17 @@
|
|
|
4
4
|
// 一列仍带机器名可辨识(ssh-net-uplink-edge-<别名> 等)
|
|
5
5
|
// - 实体/软链都幂等:内容相同不重写(只补可执行位)、链指向正确不动
|
|
6
6
|
// - 运行时零 npm/fnm 依赖;包升级后重跑 install 刷新(core 侧 uplink-edge
|
|
7
|
-
// 由每次报名自动对账),
|
|
8
|
-
// 版本差即报
|
|
7
|
+
// 由每次报名自动对账),status 直接比对包内脚本与磁盘产物
|
|
9
8
|
import fs from 'node:fs';
|
|
10
9
|
import path from 'node:path';
|
|
11
10
|
import { packageScriptPath } from './paths.mjs';
|
|
12
|
-
import { readFileOrNull,
|
|
13
|
-
import pkg from '../../package.json' with { type: 'json' };
|
|
14
|
-
|
|
15
|
-
export const RUNTIME_VERSION = pkg.version;
|
|
11
|
+
import { readFileOrNull, writeFileAtomic } from './fsutil.mjs';
|
|
16
12
|
|
|
17
13
|
/**
|
|
18
14
|
* 拷贝单脚本到 bin/(实体)。
|
|
19
15
|
* @param {string} dstPath
|
|
20
16
|
* @param {string} srcPath 包内脚本实体
|
|
21
|
-
* @returns {
|
|
17
|
+
* @returns {boolean} 是否发生变更(含补可执行位)
|
|
22
18
|
*/
|
|
23
19
|
export function ensureScript(dstPath, srcPath) {
|
|
24
20
|
const src = fs.readFileSync(srcPath);
|
|
@@ -26,13 +22,13 @@ export function ensureScript(dstPath, srcPath) {
|
|
|
26
22
|
const st = cur === null ? null : fs.lstatSync(dstPath);
|
|
27
23
|
if (st !== null && st.isFile() && cur.equals(src)) {
|
|
28
24
|
// 内容对但可执行位丢了(cp/同步工具吃掉 mode):补上
|
|
29
|
-
if (st.mode & 0o111) return
|
|
25
|
+
if (st.mode & 0o111) return false;
|
|
30
26
|
fs.chmodSync(dstPath, 0o755);
|
|
31
|
-
return
|
|
27
|
+
return true;
|
|
32
28
|
}
|
|
33
29
|
// 缺失、旧内容或软链:tmp+rename 原子换(rename 替换目录项本身,不跟随软链)
|
|
34
30
|
writeFileAtomic(dstPath, src, { mode: 0o755 });
|
|
35
|
-
return
|
|
31
|
+
return true;
|
|
36
32
|
}
|
|
37
33
|
|
|
38
34
|
/**
|
|
@@ -40,7 +36,7 @@ export function ensureScript(dstPath, srcPath) {
|
|
|
40
36
|
* 相对链目标(同目录实体名):bin/ 内部自洽,不受外部目录迁移影响。
|
|
41
37
|
* @param {string} linkPath
|
|
42
38
|
* @param {string} entityName 同目录实体文件名
|
|
43
|
-
* @returns {
|
|
39
|
+
* @returns {boolean} 是否发生变更
|
|
44
40
|
*/
|
|
45
41
|
export function ensureLink(linkPath, entityName) {
|
|
46
42
|
let st = null;
|
|
@@ -49,27 +45,22 @@ export function ensureLink(linkPath, entityName) {
|
|
|
49
45
|
} catch (err) {
|
|
50
46
|
if (err.code !== 'ENOENT') throw err;
|
|
51
47
|
}
|
|
52
|
-
if (st !== null && st.isSymbolicLink())
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
fs.symlinkSync(entityName, linkPath);
|
|
56
|
-
return 'relinked';
|
|
57
|
-
}
|
|
58
|
-
// 缺失,或软链位被实体文件占了(手拷/同步工具复制):删了换链——
|
|
59
|
-
// status 报 notlink 引导跑 install,修复靠这里
|
|
48
|
+
if (st !== null && st.isSymbolicLink() && fs.readlinkSync(linkPath) === entityName)
|
|
49
|
+
return false;
|
|
50
|
+
// 缺失无需处理;错链或实体占位都删掉后统一建链
|
|
60
51
|
if (st !== null) fs.rmSync(linkPath);
|
|
61
52
|
fs.symlinkSync(entityName, linkPath);
|
|
62
|
-
return
|
|
53
|
+
return true;
|
|
63
54
|
}
|
|
64
55
|
|
|
65
56
|
/**
|
|
66
|
-
* 幂等对账 bin/:实体拷贝 +
|
|
67
|
-
*
|
|
68
|
-
*
|
|
57
|
+
* 幂等对账 bin/:实体拷贝 + 服务软链。所有 install 路径都先经这里。
|
|
58
|
+
* 顺序是 entries 先刷实体、links 后建链:软链位有实体残留时敢直接删掉换链,
|
|
59
|
+
* 靠的就是实体已先行刷对。
|
|
69
60
|
* @param {object} ctx
|
|
70
61
|
* @param {readonly (readonly [string, string])[]} entries [bin 名, 包脚本名] 实体
|
|
71
62
|
* @param {readonly (readonly [string, string])[]} links [链名, 实体 bin 名]
|
|
72
|
-
* @returns {
|
|
63
|
+
* @returns {boolean[]} 每项是否发生变更
|
|
73
64
|
*/
|
|
74
65
|
export function ensureRuntime(ctx, entries, links = []) {
|
|
75
66
|
fs.mkdirSync(ctx.paths.binDir, { recursive: true });
|
|
@@ -78,17 +69,11 @@ export function ensureRuntime(ctx, entries, links = []) {
|
|
|
78
69
|
for (const [name, entityName] of links) {
|
|
79
70
|
results.push(ensureLink(path.join(ctx.paths.binDir, name), entityName));
|
|
80
71
|
}
|
|
81
|
-
|
|
72
|
+
// 旧版本戳(26.10.59 退役)只作迁移清理,保留约一个月后随版本删除
|
|
73
|
+
fs.rmSync(ctx.paths.legacyRuntimeVersionFile, { force: true });
|
|
82
74
|
return results;
|
|
83
75
|
}
|
|
84
76
|
|
|
85
|
-
/** 刷新版本戳(ensureRuntime 尾部) */
|
|
86
|
-
function refreshStamp(ctx) {
|
|
87
|
-
const stampPath = ctx.paths.runtimeVersionFile;
|
|
88
|
-
if (readFileOrNull(stampPath) !== `${RUNTIME_VERSION}\n`)
|
|
89
|
-
writeFileAtomic(stampPath, `${RUNTIME_VERSION}\n`);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
77
|
/**
|
|
93
78
|
* 运行时脚本体检(status 用):实体必须是普通文件,与包内逐字节比对,并查
|
|
94
79
|
* 可执行位。软链即使同内容也判 stale,ensureScript 会替成实体,避免运行时
|
|
@@ -113,49 +98,25 @@ export function auditScript(dstPath, srcPath) {
|
|
|
113
98
|
}
|
|
114
99
|
|
|
115
100
|
/**
|
|
116
|
-
* 服务软链体检(status
|
|
101
|
+
* 服务软链体检(status 用):链类型、目标与实体可执行位全部正确才健康。
|
|
102
|
+
* 具体损坏形态由 install 修复,status 不再维护一套损坏原因状态机。
|
|
117
103
|
* @param {string} linkPath
|
|
118
104
|
* @param {string} entityName
|
|
119
|
-
* @returns {
|
|
105
|
+
* @returns {boolean}
|
|
120
106
|
*/
|
|
121
107
|
export function auditLink(linkPath, entityName) {
|
|
122
108
|
let st;
|
|
123
109
|
try {
|
|
124
110
|
st = fs.lstatSync(linkPath);
|
|
125
111
|
} catch (err) {
|
|
126
|
-
if (err && err.code === 'ENOENT') return
|
|
112
|
+
if (err && err.code === 'ENOENT') return false;
|
|
127
113
|
throw err;
|
|
128
114
|
}
|
|
129
|
-
if (!st.isSymbolicLink()) return
|
|
130
|
-
if (fs.readlinkSync(linkPath) !== entityName) return
|
|
131
|
-
// 指向的实体缺失/不可执行 = 悬链(auditScript 会另报实体问题)
|
|
115
|
+
if (!st.isSymbolicLink()) return false;
|
|
116
|
+
if (fs.readlinkSync(linkPath) !== entityName) return false;
|
|
132
117
|
try {
|
|
133
|
-
|
|
134
|
-
if (!(target.mode & 0o111)) return 'dangling';
|
|
118
|
+
return Boolean(fs.statSync(linkPath).mode & 0o111);
|
|
135
119
|
} catch {
|
|
136
|
-
return
|
|
120
|
+
return false;
|
|
137
121
|
}
|
|
138
|
-
return 'ok';
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* 读运行时版本戳;缺失/为空返回 null。
|
|
143
|
-
* @param {{runtimeVersionFile:string}} paths
|
|
144
|
-
* @returns {string | null}
|
|
145
|
-
*/
|
|
146
|
-
export function readRuntimeVersion(paths) {
|
|
147
|
-
return readTrimmedOrNull(paths.runtimeVersionFile);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* 包版本与运行时戳的对账结果;一致返回 null,否则给 status 的问题文案。
|
|
152
|
-
* @param {{runtimeVersionFile:string}} paths
|
|
153
|
-
* @returns {string | null}
|
|
154
|
-
*/
|
|
155
|
-
export function runtimeVersionIssue(paths) {
|
|
156
|
-
const v = readRuntimeVersion(paths);
|
|
157
|
-
if (v === null) return '后台脚本缺版本信息——跑一次 ssh-net install 修复';
|
|
158
|
-
if (v !== RUNTIME_VERSION)
|
|
159
|
-
return `程序已升级到 ${RUNTIME_VERSION},后台脚本还是 ${v}——跑一次 ssh-net install 更新`;
|
|
160
|
-
return null;
|
|
161
122
|
}
|
package/libexec/lib/systemd.mjs
CHANGED
|
@@ -111,7 +111,8 @@ export function stopIfLoaded(runner, unit) {
|
|
|
111
111
|
* 旧文件与 manager,首装撤掉 enable/进程,更新用旧 unit 恢复启动。
|
|
112
112
|
* @param {import('./exec.mjs').Runner} runner
|
|
113
113
|
* @param {{unitPath:string, unitName:string, content:string}} job
|
|
114
|
-
* @returns {{
|
|
114
|
+
* @returns {{changed:boolean, restarted?:boolean}} 配置是否变化;restarted
|
|
115
|
+
* 只在更新已存在的 unit 时为真,用于提示可能断连
|
|
115
116
|
*/
|
|
116
117
|
export function syncUnit(runner, { unitPath, unitName, content }) {
|
|
117
118
|
fs.mkdirSync(path.dirname(unitPath), { recursive: true });
|
|
@@ -119,7 +120,7 @@ export function syncUnit(runner, { unitPath, unitName, content }) {
|
|
|
119
120
|
const previous = readFileOrNull(unitPath);
|
|
120
121
|
if (previous === content) {
|
|
121
122
|
enableStart(runner, unitName);
|
|
122
|
-
return {
|
|
123
|
+
return { changed: false };
|
|
123
124
|
}
|
|
124
125
|
|
|
125
126
|
const verify = verifyUnitContent(runner, unitName, content);
|
|
@@ -153,5 +154,5 @@ export function syncUnit(runner, { unitPath, unitName, content }) {
|
|
|
153
154
|
}
|
|
154
155
|
throw err;
|
|
155
156
|
}
|
|
156
|
-
return {
|
|
157
|
+
return { changed: true, restarted: previous !== null };
|
|
157
158
|
}
|
package/libexec/sn-route
CHANGED
|
@@ -70,7 +70,7 @@ peer_user=$5
|
|
|
70
70
|
|
|
71
71
|
# 预算计时用 bash 内建 $SECONDS(整秒且受秒边界相位影响,轮询每 tick 不生
|
|
72
72
|
# 进程);内部终点多加一个刻度,配合 probe_in_budget 的保守下界,实际等待
|
|
73
|
-
# 落在 DEADLINE-1~DEADLINE
|
|
73
|
+
# 落在 DEADLINE-1~DEADLINE 之间而不越上限。写印记的时间仍取 date epoch
|
|
74
74
|
# ——那是跨进程/跨语言的契约
|
|
75
75
|
deadline=$((SECONDS + DEADLINE + 1))
|
|
76
76
|
|
|
@@ -266,13 +266,13 @@ ann_err=
|
|
|
266
266
|
follower_tick=0
|
|
267
267
|
|
|
268
268
|
# macOS 系统 lockf 用 BSD flock 锁已打开的 fd;锁属于内核中的打开文件描述,
|
|
269
|
-
#
|
|
270
|
-
# inode),进程正常退出/kill -9/重启都自动释放,不需要陈旧判断和 rm
|
|
271
|
-
#
|
|
272
|
-
#
|
|
269
|
+
# 不由文件是否存在决定。所有进程保留同一个空锁文件(删路径会让后来者锁到
|
|
270
|
+
# 不同 inode),进程正常退出/kill -9/重启都自动释放,不需要陈旧判断和 rm
|
|
271
|
+
# 接管。报名 ssh 继承 fd9:父脚本 kill -9 时在途 RPC 继续持锁到退出;桌面
|
|
272
|
+
# 通知则 9>&-,不为收尾多持锁。
|
|
273
273
|
exec 9>>"$lock"
|
|
274
274
|
|
|
275
|
-
# 后台发起报名 ssh(leader
|
|
275
|
+
# 后台发起报名 ssh(leader 专用):经 relay、ConnectTimeout 5;
|
|
276
276
|
# stdin/stdout 全吞,stderr 收进本进程临时文件,失败时再由 say 按 tty 纪律显示
|
|
277
277
|
announce_now() {
|
|
278
278
|
ann_err="$STATE_DIR/ANNOUNCE.stderr.tmp.$$"
|
|
@@ -338,26 +338,21 @@ cleanup() {
|
|
|
338
338
|
trap cleanup EXIT
|
|
339
339
|
trap 'cleanup; exit 1' TERM INT
|
|
340
340
|
|
|
341
|
-
#
|
|
342
|
-
# FORCE 已让本次人工重试越过层 4
|
|
343
|
-
|
|
344
|
-
become_leader() {
|
|
341
|
+
# 抢锁后、报名前复查印记,闭合「前任写印记后、释放锁前」的竞态。
|
|
342
|
+
# FORCE 已让本次人工重试越过层 4,不能让同一块旧印记在这里把它重新挡回。
|
|
343
|
+
if lock_try; then
|
|
345
344
|
role=leader
|
|
346
|
-
printf '%s\n' "$("$DATE" +%s)" >"$lock"
|
|
347
345
|
if stamp_fresh "$STATE_DIR/PROBE_FAIL" "$FAIL_TTL" && [ "$ROUTE_FORCE" != 1 ]; then
|
|
348
346
|
say "直连未就绪,本次选择中继($peer)"
|
|
349
|
-
|
|
347
|
+
exit 1
|
|
350
348
|
fi
|
|
351
349
|
announce_now
|
|
352
350
|
say "等待 $peer 连过来…"
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
if lock_try; then
|
|
356
|
-
become_leader || exit 1
|
|
357
351
|
fi
|
|
358
352
|
|
|
359
|
-
# 共同轮询(follower
|
|
360
|
-
# leader 报名成功与否都继续轮询——回拨由
|
|
353
|
+
# 共同轮询(follower 额外看失败印记;不在本轮接管被杀的 leader——最坏本次
|
|
354
|
+
# 落 relay,下一条连接自会报名。leader 报名成功与否都继续轮询——回拨由
|
|
355
|
+
# core 侧 job 发起)
|
|
361
356
|
while :; do
|
|
362
357
|
probe_in_budget
|
|
363
358
|
probe_rc=$?
|
|
@@ -387,7 +382,7 @@ while :; do
|
|
|
387
382
|
fi
|
|
388
383
|
fi
|
|
389
384
|
else
|
|
390
|
-
# probe
|
|
385
|
+
# probe 仍每轮做;印记检查第一轮立即做,此后每三轮一次。
|
|
391
386
|
# 生产 0.3s tick 下检查间隔约 0.9s,省掉另外两轮的外部命令。
|
|
392
387
|
follower_tick=$((follower_tick + 1))
|
|
393
388
|
if [ $((follower_tick % 3)) -eq 1 ]; then
|
|
@@ -395,10 +390,6 @@ while :; do
|
|
|
395
390
|
say "直连未就绪,本次选择中继($peer)"
|
|
396
391
|
exit 1
|
|
397
392
|
fi
|
|
398
|
-
# 前任退出(含 kill -9)后内核锁立即可拿;拿到才成为新 leader
|
|
399
|
-
if lock_try; then
|
|
400
|
-
become_leader || exit 1
|
|
401
|
-
fi
|
|
402
393
|
fi
|
|
403
394
|
fi
|
|
404
395
|
# probe 后重算剩余整秒;不足一秒就不再睡,避免收尾越过预算
|
package/package.json
CHANGED
package/libexec/lib/cliargs.mjs
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
// 命令行参数与配置恢复输出的共用助手。
|
|
2
|
-
|
|
3
|
-
/** 恢复结果同时交代配置前提与实际处理范围,避免看起来像正常全量体检。 */
|
|
4
|
-
export function recoveryResult(ctx, action) {
|
|
5
|
-
const reason = ctx.configError?.message ?? '未知配置错误';
|
|
6
|
-
return `配置不可用,已进入恢复模式:${action}\n原因:${reason}`;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* core/node 是单总开关:无参或显式 -a/--all 收下(等同缺省),其余参数拒绝。
|
|
11
|
-
* @param {string[]} args
|
|
12
|
-
* @param {'start'|'stop'} verb
|
|
13
|
-
* @param {string} [message] 恢复模式等场景可覆盖错误说明,参数判断仍只有这一份
|
|
14
|
-
*/
|
|
15
|
-
export function assertUnitSwitchArgs(args, verb, message) {
|
|
16
|
-
if (args.length && !(args.length === 1 && (args[0] === '-a' || args[0] === '--all')))
|
|
17
|
-
throw new Error(message ?? `本机是单总开关,${verb} 不带参数(-a/--all 亦可,等同缺省)`);
|
|
18
|
-
}
|