ssh-net 26.11.67 → 26.11.68
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 +16 -0
- package/README.md +2 -2
- package/docs/changelog/2026-09.md +363 -0
- package/docs/design.md +437 -0
- package/docs/development.md +47 -0
- package/docs/notes.md +594 -0
- package/docs/releasing.md +47 -0
- package/libexec/commands/install.mjs +24 -23
- package/libexec/commands/register.mjs +28 -23
- package/libexec/commands/start.mjs +12 -2
- package/libexec/commands/status.mjs +9 -1
- package/libexec/commands/uninstall.mjs +14 -10
- package/libexec/lib/manage.mjs +26 -0
- package/libexec/lib/render.mjs +2 -3
- package/libexec/lib/systemd.mjs +17 -19
- package/libexec/sn-route +5 -14
- package/package.json +2 -1
|
@@ -5,7 +5,7 @@ import path from 'node:path';
|
|
|
5
5
|
import {
|
|
6
6
|
CALLBACK_PORT, TERMINAL_NOTIFIER_PATHS, OSASCRIPT_PATH,
|
|
7
7
|
} from '../lib/constants.mjs';
|
|
8
|
-
import { removeJobs, syncPlist } from '../lib/manage.mjs';
|
|
8
|
+
import { acquireSshLock, removeJobs, syncPlist } from '../lib/manage.mjs';
|
|
9
9
|
import { syncConfFile } from '../lib/conf.mjs';
|
|
10
10
|
import { ensureRuntime } from '../lib/runtime.mjs';
|
|
11
11
|
import { edgeLinkJob, coreRelayJob, nodeRelayJob } from '../lib/jobs.mjs';
|
|
@@ -56,8 +56,8 @@ function coreNodeInstallMsg(ctx, comp, entryChanged, result, removed) {
|
|
|
56
56
|
return `安装完成${note},中继通道已开启(${relayLine})`;
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
/**
|
|
60
|
-
async function
|
|
59
|
+
/** 转发端口被别的程序占用时,在写任何产物前退出:link 的 ssh 绑不上口,装了也起不来 */
|
|
60
|
+
async function assertForwardPortsFree({ runner, uid, topo }, labels) {
|
|
61
61
|
const jobPids = () => new Set(labels.map((label) => launchd.printState(runner, uid, label).pid));
|
|
62
62
|
let owned = jobPids();
|
|
63
63
|
for (const { listenPort: port } of topo.forwards) {
|
|
@@ -70,18 +70,22 @@ async function precheckEdgePorts({ runner, uid, topo }, labels) {
|
|
|
70
70
|
throw new Error(`端口 ${port} 被别的程序占用(pid ${foreign.join(',')}):\n${lsofListen(runner, port)}`);
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
|
+
}
|
|
73
74
|
|
|
74
|
-
|
|
75
|
+
/**
|
|
76
|
+
* 回拨端口被占时的提示('\n' 起头的独立行),没被占为 ''。它不影响安装产物,
|
|
77
|
+
* 开盖后旧回拨的残留监听又要等 sshd 保活回收,所以只提示,不挡 install 和 update
|
|
78
|
+
*/
|
|
79
|
+
async function callbackPortNote(runner) {
|
|
80
|
+
// 只看覆盖 [::1] 的 IPv6 监听;同号的纯 IPv4 监听可以共存。
|
|
75
81
|
// 用包内探针读 SSH 标识,不能让磁盘上待修复的探针堵住 install
|
|
76
82
|
const listeners = lsofListen(runner, CALLBACK_PORT);
|
|
77
83
|
const scope = v6ListenerScope(listeners, CALLBACK_PORT);
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
);
|
|
84
|
-
}
|
|
84
|
+
const why = scope === 'wildcard' ? 'IPv6 通配监听占用了回拨地址 [::1]'
|
|
85
|
+
: scope === 'loopback' && !(await peerProbeOk(runner, path.dirname(packageScriptPath(SN_PROBE))))
|
|
86
|
+
? '有程序在监听但没有响应(多半是断线残留,sshd 保活会回收;持续出现就是被别的程序占用)'
|
|
87
|
+
: '';
|
|
88
|
+
return why ? `\n提示:端口 ${CALLBACK_PORT} ${why},core 的回拨暂时绑不上这个口:\n${listeners}` : '';
|
|
85
89
|
}
|
|
86
90
|
|
|
87
91
|
/** 通知依赖提示('\n' 前缀的独立行;terminal-notifier 齐备返回 '')。 */
|
|
@@ -110,7 +114,7 @@ async function installEdge(ctx) {
|
|
|
110
114
|
const link = edgeLinkJob(ctx);
|
|
111
115
|
const diskLabels = launchd.labelsOnDisk(paths.launchAgentsDir, linkLabel(''));
|
|
112
116
|
const obsolete = diskLabels.filter((label) => label !== link?.label);
|
|
113
|
-
await
|
|
117
|
+
await assertForwardPortsFree(ctx, link ? [link.label, ...obsolete] : []);
|
|
114
118
|
const comp = syncRuntime(ctx, edgeRuntimePlan(topo));
|
|
115
119
|
// link 可能在 bootstrap 内立即连接;新旧任务各自启动前都要读到配套的 routes
|
|
116
120
|
let routesChanged = false;
|
|
@@ -137,7 +141,7 @@ async function installEdge(ctx) {
|
|
|
137
141
|
routesChanged ? 'ssh 配置片段已更新' : '');
|
|
138
142
|
const base = note ? `安装完成${note}` : '已安装,配置无需改动';
|
|
139
143
|
const startNote = ctx.state.isOn('relay') ? '' : '\n当前为关闭状态;开启自动选路和 IPv6 直连:ssh-net start';
|
|
140
|
-
return `${base}${startNote}${localNotifyDepNote()}`;
|
|
144
|
+
return `${base}${startNote}${await callbackPortNote(ctx.runner)}${localNotifyDepNote()}`;
|
|
141
145
|
}
|
|
142
146
|
|
|
143
147
|
/** core/node 按 topology 写各 edge 的入口,并移除已经不在 topology 里的旧片段 */
|
|
@@ -187,18 +191,15 @@ function installNode(ctx, home) {
|
|
|
187
191
|
/** @param {object} ctx @param {string[]} [args] */
|
|
188
192
|
export async function run(ctx, args = []) {
|
|
189
193
|
if (args.length) throw new Error('用法:ssh-net install 不带参数');
|
|
190
|
-
|
|
191
|
-
if (!['
|
|
194
|
+
// 不认识的角色直接报错,不能落进 edge 分支去动它的任务
|
|
195
|
+
if (!['core', 'node', 'edge'].includes(ctx.role)) throw new Error(`未知角色:${ctx.role}`);
|
|
192
196
|
if (ctx.role === 'edge' && !hasRouteTimeout(ctx.env))
|
|
193
197
|
throw new Error('缺少可执行的 timeout,未安装 edge 组件。请在这台机器上运行 brew install coreutils,再重跑 ssh-net install');
|
|
194
198
|
const home = ctx.role === 'node' ? requireNodeHome(ctx, 'install') : null;
|
|
195
|
-
//
|
|
196
|
-
const fd =
|
|
199
|
+
// 回滚按命令开头记下的旧状态还原;安装、core 报名和 mac 卸载共用同一把锁
|
|
200
|
+
const fd = acquireSshLock(ctx);
|
|
197
201
|
try {
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
if (lock.code !== 0) throw new Error(`无法取得安装锁:${lock.error?.message || lock.stderr.trim() || '有其他安装正在运行,请稍后重试'}`);
|
|
202
|
-
return ctx.role === 'node' ? installNode(ctx, home) : await installEdge(ctx);
|
|
203
|
-
} finally { fs.closeSync(fd); }
|
|
202
|
+
if (ctx.role === 'node') return installNode(ctx, home);
|
|
203
|
+
return ctx.role === 'core' ? installCore(ctx) : await installEdge(ctx);
|
|
204
|
+
} finally { if (fd !== null) fs.closeSync(fd); }
|
|
204
205
|
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
// core 侧报名 RPC:edge 的 sn-route 经 relay 调用,登记最近地址并建/唤回拨。
|
|
2
2
|
// 人工 start peer 也复用 sn-route,edge 侧不另维护一套报名实现。
|
|
3
3
|
// install 与报名都会刷新运行时脚本;job 参数由报名重新渲染。
|
|
4
|
+
import fs from 'node:fs';
|
|
4
5
|
import { explainV6Error } from '../lib/ipv6.mjs';
|
|
5
6
|
import * as launchd from '../lib/launchd.mjs';
|
|
6
|
-
import { syncPlist } from '../lib/manage.mjs';
|
|
7
|
+
import { acquireSshLock, syncPlist } from '../lib/manage.mjs';
|
|
7
8
|
import { ensureRuntime } from '../lib/runtime.mjs';
|
|
8
9
|
import { readFileOrNull } from '../lib/fsutil.mjs';
|
|
9
10
|
import { edgeCallbacksPlan } from '../lib/manifest.mjs';
|
|
@@ -35,28 +36,32 @@ export function runRegister(ctx, args = []) {
|
|
|
35
36
|
const v6Err = explainV6Error(v6);
|
|
36
37
|
if (v6Err) throw new Error(`v6 校验失败(${alias}):${v6Err}`);
|
|
37
38
|
|
|
38
|
-
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
39
|
+
// 两台 edge 可同时报名;短等正常报名收尾,仍受 sn-route 的报名总时限约束
|
|
40
|
+
const fd = acquireSshLock(ctx, 1);
|
|
41
|
+
try {
|
|
42
|
+
const label = edgeLabel(alias);
|
|
43
|
+
const plistPath = ctx.paths.plistFile(label);
|
|
44
|
+
const content = renderUplinkEdgePlist({
|
|
45
|
+
binDir: ctx.paths.binDir, edgeAlias: alias, user: edge.user,
|
|
46
|
+
logsDir: ctx.paths.logsDir,
|
|
47
|
+
});
|
|
48
|
+
const plan = edgeCallbacksPlan([alias]);
|
|
49
|
+
ensureRuntime(ctx, plan.entities, plan.links);
|
|
50
|
+
ctx.state.writeEdgeV6(alias, v6);
|
|
51
|
+
ctx.state.writeEdgeAddr(alias, v6);
|
|
48
52
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
53
|
+
// 未注册(首次报名)或 plist 与 topology 渲染不符(user 等变了):
|
|
54
|
+
// 重新部署,syncPlist 的 bootstrap 即首发
|
|
55
|
+
const st = launchd.inspect(ctx.runner, ctx.uid, label);
|
|
56
|
+
if (!st.registered || readFileOrNull(plistPath) !== content) {
|
|
57
|
+
const r = syncPlist(ctx, { label, plistPath, content });
|
|
58
|
+
if (r.lintError) throw new Error(`uplink-edge plist 校验失败:${r.lintError}`);
|
|
59
|
+
return st.registered ? `已更新登记 ${alias}` : `已登记 ${alias}`;
|
|
60
|
+
}
|
|
57
61
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
+
// 已注册:休眠中要先 enable 清 override,enabled 时幂等;kickstart -k 杀旧起新
|
|
63
|
+
const r = launchd.kickstartEnabled(ctx.runner, ctx.uid, label);
|
|
64
|
+
if (r.code !== 0) throw new Error(`kickstart 失败:${r.stderr.trim()}`);
|
|
65
|
+
return st.disabled ? `已唤醒 ${alias}` : `已开始拨号 ${alias}`;
|
|
66
|
+
} finally { if (fd !== null) fs.closeSync(fd); }
|
|
62
67
|
}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
import fs from 'node:fs';
|
|
5
5
|
import path from 'node:path';
|
|
6
6
|
import * as systemd from '../lib/systemd.mjs';
|
|
7
|
-
import { ensureLoaded } from '../lib/manage.mjs';
|
|
7
|
+
import { acquireSshLock, ensureLoaded } from '../lib/manage.mjs';
|
|
8
8
|
import { SWITCH_NAMES } from '../lib/state.mjs';
|
|
9
9
|
import { SN_ROUTE } from '../lib/manifest.mjs';
|
|
10
10
|
import { edgeLinkJob } from '../lib/jobs.mjs';
|
|
@@ -51,7 +51,7 @@ function startCore(ctx) {
|
|
|
51
51
|
return '中继通道已开启';
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
|
|
54
|
+
function runUnlocked(ctx, args = []) {
|
|
55
55
|
if (ctx.role === 'edge') {
|
|
56
56
|
const target = parseEdgeTarget(args);
|
|
57
57
|
const peerWasOn = target === 'relay' && ctx.state.isOn('peer');
|
|
@@ -84,3 +84,13 @@ export function run(ctx, args = []) {
|
|
|
84
84
|
}
|
|
85
85
|
throw new Error(`未知角色:${ctx.role}`);
|
|
86
86
|
}
|
|
87
|
+
|
|
88
|
+
export function run(ctx, args = []) {
|
|
89
|
+
if (ctx.role === 'node' || (ctx.platform ?? process.platform) === 'linux')
|
|
90
|
+
return runUnlocked(ctx, args);
|
|
91
|
+
// start 可能在卸载的 bootout 后重新 bootstrap;共用锁,避免留下无 plist 的注册任务
|
|
92
|
+
const fd = acquireSshLock(ctx);
|
|
93
|
+
try {
|
|
94
|
+
return runUnlocked(ctx, args);
|
|
95
|
+
} finally { if (fd !== null) fs.closeSync(fd); }
|
|
96
|
+
}
|
|
@@ -296,10 +296,18 @@ async function collectNode(ctx, probe) {
|
|
|
296
296
|
if (configIssue) issues.push(configIssue);
|
|
297
297
|
if (systemd.hasPendingUpdate(deployment.unitPath)) issues.push('中继服务更新尚未完成——跑一次 ssh-net install 完成更新');
|
|
298
298
|
const unit = deployment.unitName;
|
|
299
|
-
const active = systemd.
|
|
299
|
+
const { activeState: active, fileState } = systemd.unitSnapshot(ctx.runner, unit);
|
|
300
300
|
const relayPort = probe ? await relayPortListening(ctx, ctx.self.relayPort) : null;
|
|
301
301
|
if (active !== 'active')
|
|
302
302
|
issues.push(`系统服务 ${unit} 状态 ${active}——systemctl status ${unit} 查看`);
|
|
303
|
+
else if (fileState !== 'enabled') {
|
|
304
|
+
const repair = ['disabled', 'enabled-runtime'].includes(fileState)
|
|
305
|
+
? 'ssh-net start 可重新启用'
|
|
306
|
+
: fileState.includes('masked')
|
|
307
|
+
? '先用 systemctl unmask 取消屏蔽,再用 ssh-net start 恢复'
|
|
308
|
+
: '检查 unit 文件与 systemctl enable 状态';
|
|
309
|
+
issues.push(`系统服务 ${unit} 仍在运行,但开机自启为 ${fileState || 'unknown'}——${repair}`);
|
|
310
|
+
}
|
|
303
311
|
if (relayPort && !relayPort.listening)
|
|
304
312
|
issues.push(relayPortIssue(topo.relay.alias, ctx.self.relayPort, relayPort));
|
|
305
313
|
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// plist/unit、运行时脚本、状态与日志;topology 改名前的残留由 glob 覆盖。
|
|
4
4
|
import fs from 'node:fs';
|
|
5
5
|
import path from 'node:path';
|
|
6
|
-
import { removeJobs } from '../lib/manage.mjs';
|
|
6
|
+
import { acquireSshLock, removeJobs } from '../lib/manage.mjs';
|
|
7
7
|
import { requireNodeHome } from '../lib/identity.mjs';
|
|
8
8
|
import * as launchd from '../lib/launchd.mjs';
|
|
9
9
|
import * as systemd from '../lib/systemd.mjs';
|
|
@@ -46,13 +46,17 @@ export function run(ctx, args = []) {
|
|
|
46
46
|
return '已卸载。最后一步:npm uninstall -g ssh-net';
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
49
|
+
// 快照与删除必须和 core 报名互斥,否则新回拨会在快照后注册、再被删掉运行时
|
|
50
|
+
const fd = acquireSshLock(ctx);
|
|
51
|
+
try {
|
|
52
|
+
const labels = new Set([
|
|
53
|
+
...launchd.labelsOnDisk(ctx.paths.launchAgentsDir, linkLabel('')),
|
|
54
|
+
...launchd.labelsOnDisk(ctx.paths.launchAgentsDir, relayLabel('')),
|
|
55
|
+
...launchd.labelsOnDisk(ctx.paths.launchAgentsDir, edgeLabel('')),
|
|
56
|
+
]);
|
|
57
|
+
removeJobs(ctx, labels);
|
|
58
|
+
removeArtifacts(ctx);
|
|
59
|
+
removeSshNetLogs(ctx);
|
|
60
|
+
return '已卸载。最后一步:npm uninstall -g ssh-net';
|
|
61
|
+
} finally { if (fd !== null) fs.closeSync(fd); }
|
|
58
62
|
}
|
package/libexec/lib/manage.mjs
CHANGED
|
@@ -6,6 +6,32 @@ import * as launchd from './launchd.mjs';
|
|
|
6
6
|
import path from 'node:path';
|
|
7
7
|
import { readFileOrNull, removeObsoleteNames, withTempFile, writeFileAtomic } from './fsutil.mjs';
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* 取得本账户 .ssh 目录的排他锁,调用方持有返回的 fd 直至操作结束。
|
|
11
|
+
* install、core 报名和 mac start/uninstall 共用,避免回滚或删除产物时互相交错。
|
|
12
|
+
* 没有 .ssh 说明没有可互斥的本地产物,供空 HOME 的卸载保持幂等。
|
|
13
|
+
* @param {object} ctx
|
|
14
|
+
* @param {number} [waitSeconds] 正常报名之间短等;安装、启动和卸载默认不等待
|
|
15
|
+
* @returns {number|null} 调用方负责 close;null 表示无需锁
|
|
16
|
+
*/
|
|
17
|
+
export function acquireSshLock(ctx, waitSeconds = 0) {
|
|
18
|
+
if (!fs.existsSync(ctx.paths.sshDir)) return null;
|
|
19
|
+
const fd = fs.openSync(ctx.paths.sshDir, 'r');
|
|
20
|
+
try {
|
|
21
|
+
const mac = (ctx.platform ?? process.platform) === 'darwin';
|
|
22
|
+
const args = mac ? ['-s', '-t', String(waitSeconds), '3']
|
|
23
|
+
: waitSeconds ? ['-w', String(waitSeconds), '3'] : ['-n', '3'];
|
|
24
|
+
const lock = ctx.runner.run(mac ? 'lockf' : 'flock', args,
|
|
25
|
+
{ stdio: ['ignore', 'pipe', 'pipe', fd] });
|
|
26
|
+
if (lock.code !== 0)
|
|
27
|
+
throw new Error(`无法取得安装锁:${lock.error?.message || lock.stderr.trim() || '有其他安装、报名或卸载正在运行,请稍后重试'}`);
|
|
28
|
+
return fd;
|
|
29
|
+
} catch (err) {
|
|
30
|
+
fs.closeSync(fd);
|
|
31
|
+
throw err;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
9
35
|
/**
|
|
10
36
|
* install 与 start 共用的任务恢复逻辑。带 PathState 的任务由 launchd 按文件状态启动
|
|
11
37
|
* @param {object} ctx
|
package/libexec/lib/render.mjs
CHANGED
|
@@ -94,10 +94,9 @@ const ROUTES_HEADER = `# 本文件由 \`ssh-net install\` 从 ~/.ssh/ssh-net/top
|
|
|
94
94
|
// Match exec 只保留 ssh_config 的外层双引号:OpenSSH 9.x 不能解析内嵌
|
|
95
95
|
// 转义双引号。路径用 ~ 让 shell 展开(结果不受 HOME 空白分词影响),topology
|
|
96
96
|
// 已拒绝 argv 的空白与 shell 元字符,不在此层做 shell 引号
|
|
97
|
-
/** @param {string} pattern @param {string} command */
|
|
97
|
+
/** @param {string} pattern @param {string} command(不得含引号) */
|
|
98
98
|
function matchExec(pattern, command) {
|
|
99
|
-
|
|
100
|
-
return `Match originalhost ${pattern} exec "${escaped}"`;
|
|
99
|
+
return `Match originalhost ${pattern} exec "${command}"`;
|
|
101
100
|
}
|
|
102
101
|
|
|
103
102
|
/**
|
package/libexec/lib/systemd.mjs
CHANGED
|
@@ -64,16 +64,6 @@ function restart(runner, unit) {
|
|
|
64
64
|
if (r.code !== 0) throw new Error(`systemctl restart 失败:${r.stderr.trim()}`);
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
/** @returns {string} active|inactive|failed|... */
|
|
68
|
-
export function isActive(runner, unit) {
|
|
69
|
-
const r = sc(runner, ['is-active', unit]);
|
|
70
|
-
const state = r.stdout.trim();
|
|
71
|
-
// inactive/failed 也有非零退出码;此时 stdout 仍有状态,与无法连接 manager 区分
|
|
72
|
-
if (!state || ![0, 3, 4].includes(r.code))
|
|
73
|
-
throw new Error(`systemctl is-active 失败(${unit}):${r.stderr.trim() || r.error?.message || `exit ${r.code}`}`);
|
|
74
|
-
return state;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
67
|
/**
|
|
78
68
|
* unit 内容校验:写进私有临时目录、**文件名与正式 unit 相同**(verify 要求
|
|
79
69
|
* .service 后缀,basename 参与模板/实例语义),过了才允许落 /etc——坏 unit
|
|
@@ -139,20 +129,28 @@ export function removeUnits(runner, unitDir, units) {
|
|
|
139
129
|
}
|
|
140
130
|
}
|
|
141
131
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
132
|
+
/** 一次只读快照;空 UnitFileState 留给调用方解释,属性行缺失仍是查询失败 */
|
|
133
|
+
export function unitSnapshot(runner, unit, withInvocation = false) {
|
|
134
|
+
const props = ['ActiveState', 'UnitFileState'];
|
|
135
|
+
if (withInvocation) props.push('InvocationID');
|
|
136
|
+
const state = sc(runner, ['show', ...props.flatMap((name) => ['-p', name]), unit]);
|
|
146
137
|
if (state.code !== 0) throw new Error(`systemctl show 失败(${unit}):${state.stderr.trim()}`);
|
|
147
138
|
const activeState = state.stdout.match(/^ActiveState=(\S+)$/m)?.[1];
|
|
148
139
|
if (!activeState) throw new Error(`systemctl show 未返回 ${unit} 的运行状态`);
|
|
149
|
-
const fileState = state.stdout.match(/^UnitFileState=(
|
|
150
|
-
if (
|
|
151
|
-
const active = ['active', 'activating', 'reloading', 'refreshing'].includes(activeState);
|
|
140
|
+
const fileState = state.stdout.match(/^UnitFileState=(.*)$/m)?.[1].trim();
|
|
141
|
+
if (fileState === undefined) throw new Error(`systemctl show 未返回 ${unit} 的自启状态`);
|
|
152
142
|
const invocation = state.stdout.match(/^InvocationID=([0-9a-f]{32})$/m)?.[1];
|
|
153
|
-
|
|
143
|
+
return { activeState, fileState, invocation };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function unitState(runner, unit, withInvocation = false) {
|
|
147
|
+
const state = unitSnapshot(runner, unit, withInvocation);
|
|
148
|
+
// 安装和回滚必须知道真实自启状态;空值不能像 status 一样只归一为 unknown
|
|
149
|
+
if (!state.fileState) throw new Error(`systemctl show 未返回 ${unit} 的自启状态`);
|
|
150
|
+
const active = ['active', 'activating', 'reloading', 'refreshing'].includes(state.activeState);
|
|
151
|
+
if (withInvocation && active && (!state.invocation || /^0+$/.test(state.invocation)))
|
|
154
152
|
throw new Error(`systemctl show 未返回 ${unit} 的运行实例 ID`);
|
|
155
|
-
return { active, fileState, invocation: active ? invocation : null };
|
|
153
|
+
return { active, fileState: state.fileState, invocation: active ? state.invocation : null };
|
|
156
154
|
}
|
|
157
155
|
|
|
158
156
|
/** 各 unit 的运行和自启分别恢复,单步错误在尝试完其他服务后一起报告 */
|
package/libexec/sn-route
CHANGED
|
@@ -33,15 +33,6 @@ RM=${RM:-/bin/rm}
|
|
|
33
33
|
SLEEP=${SLEEP:-/bin/sleep}
|
|
34
34
|
LAUNCHCTL=${LAUNCHCTL:-/bin/launchctl}
|
|
35
35
|
LSOF=${LSOF:-/usr/sbin/lsof}
|
|
36
|
-
# edge 生产是 macOS lockf;同脚本在 Linux 跑时用 util-linux flock,接口差异
|
|
37
|
-
# 收在 lock_try,不引第三方锁包。
|
|
38
|
-
if [ -x /usr/bin/lockf ]; then
|
|
39
|
-
LOCK_BACKEND=lockf
|
|
40
|
-
elif [ -x /usr/bin/flock ]; then
|
|
41
|
-
LOCK_BACKEND=flock
|
|
42
|
-
else
|
|
43
|
-
LOCK_BACKEND=none
|
|
44
|
-
fi
|
|
45
36
|
STATE_DIR=${SSH_NET_STATE_DIR:-$HOME/.ssh/ssh-net/state}
|
|
46
37
|
SSH_NET_DIR=${SSH_NET_DIR:-$HOME/.ssh/ssh-net}
|
|
47
38
|
BIN_DIR=${SSH_NET_BIN_DIR:-$SSH_NET_DIR/bin}
|
|
@@ -85,12 +76,12 @@ say() {
|
|
|
85
76
|
[ -t 2 ] && printf '%s\n' "$*" >&2
|
|
86
77
|
return 0
|
|
87
78
|
}
|
|
79
|
+
# 锁后端:edge 生产是 macOS lockf,同脚本在 Linux 跑时用 util-linux flock,不引第三方
|
|
80
|
+
# 锁包。拿不到锁一律非 0(flock 不存在、执行失败时也一样):没有锁就不能报名。
|
|
81
|
+
# 报错进 /dev/null:非终端下 stderr 必须保持零输出
|
|
88
82
|
lock_try() {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
flock) /usr/bin/flock -n 9 ;;
|
|
92
|
-
*) return 1 ;;
|
|
93
|
-
esac
|
|
83
|
+
if [ -x /usr/bin/lockf ]; then /usr/bin/lockf -s -t 0 9
|
|
84
|
+
else /usr/bin/flock -n 9 2>/dev/null; fi
|
|
94
85
|
}
|
|
95
86
|
|
|
96
87
|
# 失败印记新鲜判定:$1=文件 $2=TTL;整份内容只许一个 epoch,空/非数/超龄=不新鲜
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ssh-net",
|
|
3
|
-
"version": "26.11.
|
|
3
|
+
"version": "26.11.68",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "个人 SSH 组网:IPv6 直连优先,中继自动兜底,断开后有界重试、按需恢复",
|
|
6
6
|
"bin": {
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
},
|
|
9
9
|
"files": [
|
|
10
10
|
"libexec",
|
|
11
|
+
"docs",
|
|
11
12
|
"CHANGELOG.md"
|
|
12
13
|
],
|
|
13
14
|
"scripts": {
|