ssh-net 26.10.34 → 26.10.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +42 -0
- package/libexec/cli.mjs +5 -14
- package/libexec/commands/doctor.mjs +7 -5
- package/libexec/commands/install.mjs +58 -95
- package/libexec/commands/register.mjs +28 -55
- package/libexec/commands/start.mjs +12 -24
- package/libexec/commands/status.mjs +97 -106
- package/libexec/commands/stop.mjs +8 -10
- package/libexec/commands/uninstall.mjs +16 -18
- package/libexec/lib/cliargs.mjs +12 -0
- package/libexec/lib/conf.mjs +3 -11
- package/libexec/lib/config.mjs +2 -3
- package/libexec/lib/fsutil.mjs +34 -0
- package/libexec/lib/ipv6.mjs +17 -20
- package/libexec/lib/jobctl.mjs +13 -14
- package/libexec/lib/launchd.mjs +66 -12
- package/libexec/lib/manage.mjs +51 -34
- package/libexec/lib/manifest.mjs +76 -0
- package/libexec/lib/paths.mjs +2 -0
- package/libexec/lib/probe.mjs +27 -10
- package/libexec/lib/registration.mjs +47 -0
- package/libexec/lib/render.mjs +79 -24
- package/libexec/lib/runtime.mjs +24 -35
- package/libexec/lib/state.mjs +12 -20
- package/libexec/lib/systemd.mjs +52 -5
- package/libexec/sn-route +36 -19
- package/libexec/ssh-net-uplink-edge +12 -8
- package/package.json +1 -1
package/libexec/lib/runtime.mjs
CHANGED
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
import fs from 'node:fs';
|
|
11
11
|
import path from 'node:path';
|
|
12
12
|
import { packageScriptPath } from './paths.mjs';
|
|
13
|
+
import { readFileOrNull, writeFileAtomic } from './fsutil.mjs';
|
|
13
14
|
import pkg from '../../package.json' with { type: 'json' };
|
|
14
15
|
|
|
15
16
|
export const RUNTIME_VERSION = pkg.version;
|
|
@@ -22,12 +23,7 @@ export const RUNTIME_VERSION = pkg.version;
|
|
|
22
23
|
*/
|
|
23
24
|
export function ensureScript(dstPath, srcPath) {
|
|
24
25
|
const src = fs.readFileSync(srcPath);
|
|
25
|
-
|
|
26
|
-
try {
|
|
27
|
-
cur = fs.readFileSync(dstPath);
|
|
28
|
-
} catch (err) {
|
|
29
|
-
if (err.code !== 'ENOENT') throw err;
|
|
30
|
-
}
|
|
26
|
+
const cur = readFileOrNull(dstPath, null);
|
|
31
27
|
if (cur !== null && cur.equals(src)) {
|
|
32
28
|
// 内容对但可执行位丢了(cp/同步工具吃掉 mode):补上
|
|
33
29
|
if (fs.statSync(dstPath).mode & 0o111) return 'unchanged';
|
|
@@ -35,10 +31,7 @@ export function ensureScript(dstPath, srcPath) {
|
|
|
35
31
|
return 'repaired';
|
|
36
32
|
}
|
|
37
33
|
// 缺失或旧内容:tmp+rename 原子换(rename 直接替换目录项)
|
|
38
|
-
|
|
39
|
-
fs.writeFileSync(tmp, src, { mode: 0o755 });
|
|
40
|
-
fs.renameSync(tmp, dstPath);
|
|
41
|
-
fs.chmodSync(dstPath, 0o755);
|
|
34
|
+
writeFileAtomic(dstPath, src, { mode: 0o755 });
|
|
42
35
|
return cur !== null ? 'updated' : 'created';
|
|
43
36
|
}
|
|
44
37
|
|
|
@@ -69,17 +62,23 @@ export function ensureLink(linkPath, entityName) {
|
|
|
69
62
|
}
|
|
70
63
|
|
|
71
64
|
/**
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
65
|
+
* 已退役产物清单:bin/state/conf.d 都是本工具独占位置,install 对账时静默
|
|
66
|
+
* 删除,三种产物的退役清理只有这一个入口。条目带退役版本,保留约一个月后
|
|
67
|
+
* 随版本删除(见 CLAUDE.md「约定」),别让一次性兜底变成永久代码。
|
|
68
|
+
* dir 是 ctx.paths 的目录键。
|
|
69
|
+
* @type {{dir:'binDir'|'stateDir'|'confDir', name:string, retired:string}[]}
|
|
75
70
|
*/
|
|
76
|
-
const
|
|
77
|
-
'sn-notify',
|
|
78
|
-
'sn-signal',
|
|
71
|
+
const STALE_FILES = [
|
|
72
|
+
{ dir: 'binDir', name: 'sn-notify', retired: '26.10.31' }, // 通知内联 sn-route
|
|
73
|
+
{ dir: 'binDir', name: 'sn-signal', retired: '26.10.31' }, // 跨机反拨通道取消
|
|
74
|
+
{ dir: 'stateDir', name: 'ROUTE_PEER', retired: '26.10.31' }, // 开关收敛为 relay/peer
|
|
75
|
+
{ dir: 'stateDir', name: 'LINK_ON', retired: '26.10.31' }, // link 退化为纯 ssh
|
|
76
|
+
{ dir: 'confDir', name: 'vscode-mark', retired: '26.10.32' }, // 打标块并入 routes
|
|
79
77
|
];
|
|
80
78
|
|
|
81
79
|
/**
|
|
82
|
-
* 幂等对账 bin
|
|
80
|
+
* 幂等对账 bin/:退役产物清理 + 实体拷贝 + 服务软链 + 版本戳。所有 install
|
|
81
|
+
* 路径(edge/core/node,含 link 形态切换分支)都先经这里,退役清理无死角。
|
|
83
82
|
* @param {object} ctx
|
|
84
83
|
* @param {readonly (readonly [string, string])[]} entries [bin 名, 包脚本名] 实体
|
|
85
84
|
* @param {readonly (readonly [string, string])[]} links [链名, 实体 bin 名]
|
|
@@ -87,8 +86,8 @@ const STALE_SCRIPTS = [
|
|
|
87
86
|
*/
|
|
88
87
|
export function ensureRuntime(ctx, entries, links = []) {
|
|
89
88
|
fs.mkdirSync(ctx.paths.binDir, { recursive: true });
|
|
90
|
-
for (const name of
|
|
91
|
-
fs.rmSync(path.join(ctx.paths
|
|
89
|
+
for (const { dir, name } of STALE_FILES)
|
|
90
|
+
fs.rmSync(path.join(ctx.paths[dir], name), { force: true });
|
|
92
91
|
const results = entries.map(([name, scriptName]) =>
|
|
93
92
|
ensureScript(path.join(ctx.paths.binDir, name), packageScriptPath(scriptName)));
|
|
94
93
|
for (const [name, entityName] of links) {
|
|
@@ -100,17 +99,9 @@ export function ensureRuntime(ctx, entries, links = []) {
|
|
|
100
99
|
|
|
101
100
|
/** 刷新版本戳(ensureRuntime 尾部) */
|
|
102
101
|
function refreshStamp(ctx) {
|
|
103
|
-
const stamp = `${RUNTIME_VERSION}\n`;
|
|
104
102
|
const stampPath = ctx.paths.runtimeVersionFile;
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
cur = fs.readFileSync(stampPath, 'utf8');
|
|
108
|
-
} catch { cur = null; }
|
|
109
|
-
if (cur !== stamp) {
|
|
110
|
-
const tmp = `${stampPath}.tmp-${process.pid}`;
|
|
111
|
-
fs.writeFileSync(tmp, stamp, { mode: 0o644 });
|
|
112
|
-
fs.renameSync(tmp, stampPath);
|
|
113
|
-
}
|
|
103
|
+
if (readFileOrNull(stampPath) !== `${RUNTIME_VERSION}\n`)
|
|
104
|
+
writeFileAtomic(stampPath, `${RUNTIME_VERSION}\n`);
|
|
114
105
|
}
|
|
115
106
|
|
|
116
107
|
/**
|
|
@@ -162,12 +153,10 @@ export function auditLink(linkPath, entityName) {
|
|
|
162
153
|
* @returns {string | null}
|
|
163
154
|
*/
|
|
164
155
|
export function readRuntimeVersion(paths) {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
return null;
|
|
170
|
-
}
|
|
156
|
+
const v = readFileOrNull(paths.runtimeVersionFile);
|
|
157
|
+
if (v === null) return null;
|
|
158
|
+
const trimmed = v.trim();
|
|
159
|
+
return trimmed.length > 0 ? trimmed : null;
|
|
171
160
|
}
|
|
172
161
|
|
|
173
162
|
/**
|
package/libexec/lib/state.mjs
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
// 目录 0700、文件 0600,写 v6 走 tmp+rename 防报名写入与 uplink-edge 读到半截。
|
|
7
7
|
import fs from 'node:fs';
|
|
8
8
|
import path from 'node:path';
|
|
9
|
+
import { readFileOrNull, writeFileAtomic } from './fsutil.mjs';
|
|
9
10
|
|
|
10
11
|
const SWITCH_NAMES = ['relay', 'peer'];
|
|
11
12
|
|
|
@@ -28,8 +29,6 @@ const SWITCH_NAMES = ['relay', 'peer'];
|
|
|
28
29
|
* @returns {StateStore}
|
|
29
30
|
*/
|
|
30
31
|
export function createStateStore({ stateDir, switchFiles, edgesDir }) {
|
|
31
|
-
const files = switchFiles;
|
|
32
|
-
|
|
33
32
|
const ensureState = () => {
|
|
34
33
|
fs.mkdirSync(stateDir, { recursive: true, mode: 0o700 });
|
|
35
34
|
};
|
|
@@ -42,7 +41,7 @@ export function createStateStore({ stateDir, switchFiles, edgesDir }) {
|
|
|
42
41
|
return {
|
|
43
42
|
isOn(name) {
|
|
44
43
|
assertNames([name]);
|
|
45
|
-
return fs.existsSync(
|
|
44
|
+
return fs.existsSync(switchFiles[name]);
|
|
46
45
|
},
|
|
47
46
|
|
|
48
47
|
setOn(names) {
|
|
@@ -50,9 +49,9 @@ export function createStateStore({ stateDir, switchFiles, edgesDir }) {
|
|
|
50
49
|
ensureState();
|
|
51
50
|
for (const n of names) {
|
|
52
51
|
// 'a':不截断、已存在不报错(touch 语义)
|
|
53
|
-
const fd = fs.openSync(
|
|
52
|
+
const fd = fs.openSync(switchFiles[n], 'a');
|
|
54
53
|
fs.closeSync(fd);
|
|
55
|
-
fs.chmodSync(
|
|
54
|
+
fs.chmodSync(switchFiles[n], 0o600);
|
|
56
55
|
}
|
|
57
56
|
},
|
|
58
57
|
|
|
@@ -60,7 +59,7 @@ export function createStateStore({ stateDir, switchFiles, edgesDir }) {
|
|
|
60
59
|
assertNames(names);
|
|
61
60
|
for (const n of names) {
|
|
62
61
|
try {
|
|
63
|
-
fs.unlinkSync(
|
|
62
|
+
fs.unlinkSync(switchFiles[n]);
|
|
64
63
|
} catch (err) {
|
|
65
64
|
if (err && err.code === 'ENOENT') continue;
|
|
66
65
|
throw err;
|
|
@@ -70,29 +69,22 @@ export function createStateStore({ stateDir, switchFiles, edgesDir }) {
|
|
|
70
69
|
|
|
71
70
|
readSwitches() {
|
|
72
71
|
return {
|
|
73
|
-
relay: fs.existsSync(
|
|
74
|
-
peer: fs.existsSync(
|
|
72
|
+
relay: fs.existsSync(switchFiles.relay),
|
|
73
|
+
peer: fs.existsSync(switchFiles.peer),
|
|
75
74
|
};
|
|
76
75
|
},
|
|
77
76
|
|
|
78
77
|
readEdgeV6(alias) {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
if (err && err.code === 'ENOENT') return null;
|
|
84
|
-
throw err;
|
|
85
|
-
}
|
|
78
|
+
const v6 = readFileOrNull(path.join(edgesDir, alias));
|
|
79
|
+
if (v6 === null) return null;
|
|
80
|
+
const trimmed = v6.trim();
|
|
81
|
+
return trimmed.length > 0 ? trimmed : null;
|
|
86
82
|
},
|
|
87
83
|
|
|
88
84
|
writeEdgeV6(alias, v6) {
|
|
89
85
|
ensureState();
|
|
90
86
|
fs.mkdirSync(edgesDir, { recursive: true, mode: 0o700 });
|
|
91
|
-
|
|
92
|
-
const tmp = path.join(edgesDir, `.${alias}.tmp-${process.pid}`);
|
|
93
|
-
fs.writeFileSync(tmp, `${v6}\n`, { mode: 0o600 });
|
|
94
|
-
fs.renameSync(tmp, final);
|
|
95
|
-
fs.chmodSync(final, 0o600);
|
|
87
|
+
writeFileAtomic(path.join(edgesDir, alias), `${v6}\n`, { mode: 0o600 });
|
|
96
88
|
},
|
|
97
89
|
|
|
98
90
|
removeEdgeV6(alias) {
|
package/libexec/lib/systemd.mjs
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
// node 机 systemd 编排。node 登录为 root(migration 实测前提),直接
|
|
2
2
|
// systemctl、直接写 /etc/systemd/system,不包 sudo;非 root 调用由命令层拒绝。
|
|
3
3
|
// Restart=always/RestartSec/StartLimitIntervalSec 在渲染的 unit 里定死。
|
|
4
|
+
import fs from 'node:fs';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { readFileOrNull, writeFileAtomic } from './fsutil.mjs';
|
|
4
7
|
|
|
5
8
|
/** @param {import('./exec.mjs').Runner} runner @param {string} unit 含 .service */
|
|
6
9
|
const sc = (runner, args) => runner.run('systemctl', args);
|
|
@@ -17,14 +20,58 @@ export const isActive = (runner, unit) =>
|
|
|
17
20
|
sc(runner, ['is-active', unit]).stdout.trim();
|
|
18
21
|
|
|
19
22
|
/**
|
|
20
|
-
* unit 文件校验;systemd-analyze
|
|
21
|
-
*
|
|
23
|
+
* unit 文件校验;systemd-analyze 不存在(ENOENT)视为通过——lint 是尽力而为,
|
|
24
|
+
* 不该让装不了 systemd-analyze 的机器无法安装。
|
|
25
|
+
* @returns {{ok:boolean, stdout:string, stderr:string}}
|
|
22
26
|
*/
|
|
23
27
|
export function verifyUnit(runner, unitPath) {
|
|
24
28
|
const r = runner.run('systemdAnalyze', ['verify', unitPath]);
|
|
25
|
-
if (r.error && r.error.code === 'ENOENT') {
|
|
26
|
-
return { ok: true, skipped: true, stdout: r.stdout, stderr: r.stderr };
|
|
27
|
-
}
|
|
29
|
+
if (r.error && r.error.code === 'ENOENT') return { ok: true, stdout: r.stdout, stderr: r.stderr };
|
|
28
30
|
// verify 对只告警也可能非零;这里把非零视为失败,命令层打印 stderr
|
|
29
31
|
return { ok: r.code === 0, stdout: r.stdout, stderr: r.stderr };
|
|
30
32
|
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 停一个 unit:未加载(未装/已卸)的 not-loaded 失败放行,其余抛出。
|
|
36
|
+
* stop 与 uninstall 共用这条规则——真失败必须报,否则「已关闭」是谎报、
|
|
37
|
+
* 删了 unit 文件还会留下在跑的 ghost 进程。
|
|
38
|
+
* @param {import('./exec.mjs').Runner} runner
|
|
39
|
+
* @param {string} unit
|
|
40
|
+
*/
|
|
41
|
+
export function stopIfLoaded(runner, unit) {
|
|
42
|
+
const r = stopUnit(runner, unit);
|
|
43
|
+
if (r.code !== 0 && !/not loaded/.test(r.stderr))
|
|
44
|
+
throw new Error(`systemctl stop 失败:${r.stderr.trim()}`);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 对账一个 unit 到磁盘并确保服务在跑(与 mac 侧 syncPlist 同构、对齐 design §5
|
|
49
|
+
* 「install 幂等不断连」):字节相同只 enable+start,不重写不 restart;内容
|
|
50
|
+
* 变了才 verify → 原子落盘 → daemon-reload → enable → restart。
|
|
51
|
+
* @param {import('./exec.mjs').Runner} runner
|
|
52
|
+
* @param {{unitPath:string, unitName:string, content:string}} job
|
|
53
|
+
* @returns {'unchanged'|'deployed'}
|
|
54
|
+
*/
|
|
55
|
+
export function syncUnit(runner, { unitPath, unitName, content }) {
|
|
56
|
+
fs.mkdirSync(path.dirname(unitPath), { recursive: true });
|
|
57
|
+
|
|
58
|
+
if (readFileOrNull(unitPath) === content) {
|
|
59
|
+
for (const r of [enableUnit(runner, unitName), startUnit(runner, unitName)]) {
|
|
60
|
+
if (r.code !== 0) throw new Error(`systemctl 失败:${r.stderr.trim()}`);
|
|
61
|
+
}
|
|
62
|
+
return 'unchanged';
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
writeFileAtomic(unitPath, content, { mode: 0o644 });
|
|
66
|
+
const verify = verifyUnit(runner, unitPath);
|
|
67
|
+
if (!verify.ok)
|
|
68
|
+
throw new Error(`systemd-analyze verify 失败:\n${verify.stderr}`);
|
|
69
|
+
for (const r of [
|
|
70
|
+
daemonReload(runner),
|
|
71
|
+
enableUnit(runner, unitName),
|
|
72
|
+
restartUnit(runner, unitName),
|
|
73
|
+
]) {
|
|
74
|
+
if (r.code !== 0) throw new Error(`systemctl 失败:${r.stderr.trim()}`);
|
|
75
|
+
}
|
|
76
|
+
return 'deployed';
|
|
77
|
+
}
|
package/libexec/sn-route
CHANGED
|
@@ -55,8 +55,9 @@ peer_user=$5
|
|
|
55
55
|
ui=$6
|
|
56
56
|
case "$ui" in show|quiet) ;; *) exit 1 ;; esac
|
|
57
57
|
|
|
58
|
-
|
|
59
|
-
|
|
58
|
+
# 预算计时用 bash 内建 $SECONDS(整秒精度与 date +%s 相同,但轮询每 tick
|
|
59
|
+
# 不生进程);写进印记/锁的时间仍取 date epoch——那是跨进程/跨语言的契约
|
|
60
|
+
deadline=$((SECONDS + DEADLINE))
|
|
60
61
|
|
|
61
62
|
# 进度只在 show 且 stderr 是终端时输出:VS Code/Remote-SSH 的 stderr 是管道,
|
|
62
63
|
# scp、脚本调用同样自动静默。恒返回 0,输出不影响判决
|
|
@@ -64,16 +65,18 @@ say() {
|
|
|
64
65
|
[ "$ui" = show ] && [ -t 2 ] && printf '%s\n' "$*" >&2
|
|
65
66
|
return 0
|
|
66
67
|
}
|
|
67
|
-
time_left() { [ "$
|
|
68
|
+
time_left() { [ "$SECONDS" -lt "$deadline" ]; }
|
|
68
69
|
|
|
69
|
-
# 印记/锁新鲜判定:$1=文件 $2=TTL
|
|
70
|
+
# 印记/锁新鲜判定:$1=文件 $2=TTL,$3=可选当前 epoch(轮询里两处判定
|
|
71
|
+
# 共用一次 date);内容为 epoch,空/非数/超龄=不新鲜
|
|
70
72
|
stamp_fresh() {
|
|
71
|
-
local v
|
|
73
|
+
local v now
|
|
72
74
|
# 不用 $(<file) 2>/dev/null:bash 3.2 下赋值后尾随重定向会让读取优化
|
|
73
75
|
# 失效、把 file 当命令执行(实测)
|
|
74
76
|
v=$("$CAT" "$1" 2>/dev/null) || return 1
|
|
75
77
|
case "$v" in ''|*[!0-9]*) return 1 ;; esac
|
|
76
|
-
|
|
78
|
+
now=${3:-$("$DATE" +%s)}
|
|
79
|
+
[ $(( now - v )) -le "$2" ]
|
|
77
80
|
}
|
|
78
81
|
|
|
79
82
|
# banner probe 在子 shell 里做:stderr 重定向只在子内生效(主 shell 的 stderr
|
|
@@ -83,8 +86,7 @@ probe() (
|
|
|
83
86
|
exec 2>/dev/null
|
|
84
87
|
exec 3<>"/dev/tcp/$HOST/$PORT" || exit 1
|
|
85
88
|
# read 必须显式 <&3:fd0 在 Match exec 下是 /dev/null,漏了会立即 EOF
|
|
86
|
-
IFS= read -t "$PROBE_W" -r line <&3
|
|
87
|
-
[ "$?" -eq 0 ] || exit 1
|
|
89
|
+
IFS= read -t "$PROBE_W" -r line <&3 || exit 1
|
|
88
90
|
case "$line" in SSH-*) exit 0 ;; *) exit 1 ;; esac
|
|
89
91
|
)
|
|
90
92
|
|
|
@@ -95,8 +97,9 @@ mark_fail() {
|
|
|
95
97
|
local t
|
|
96
98
|
t=$("$DATE" +%s) || return 0
|
|
97
99
|
( umask 077; printf '%s\n' "$t" >"$STATE_DIR/PROBE_FAIL.tmp.$$" ) 2>/dev/null || return 0
|
|
98
|
-
|
|
99
|
-
"$
|
|
100
|
+
# rename 成功 tmp 即不存在,不再白 rm;rename 失败才清 tmp
|
|
101
|
+
"$MV" -f "$STATE_DIR/PROBE_FAIL.tmp.$$" "$STATE_DIR/PROBE_FAIL" 2>/dev/null \
|
|
102
|
+
|| "$RM" -f "$STATE_DIR/PROBE_FAIL.tmp.$$" 2>/dev/null
|
|
100
103
|
}
|
|
101
104
|
|
|
102
105
|
# ── 层 1
|
|
@@ -148,10 +151,16 @@ announce_now() {
|
|
|
148
151
|
ann_pid=$!
|
|
149
152
|
}
|
|
150
153
|
|
|
154
|
+
# 抢锁写当前 epoch:umask 077 + set -C 保证不覆盖在干活的锁;成功返回 0。
|
|
155
|
+
# leader 首抢与 follower 陈旧接管同一咒语
|
|
156
|
+
trylock() {
|
|
157
|
+
( umask 077; set -C; printf '%s\n' "$("$DATE" +%s)" >"$lock" ) 2>/dev/null
|
|
158
|
+
}
|
|
159
|
+
|
|
151
160
|
# 获取锁:0=leader(创建成功),1=follower(新鲜锁);陈旧/损坏删锁重抢
|
|
152
161
|
acquire() {
|
|
153
162
|
while time_left; do
|
|
154
|
-
if
|
|
163
|
+
if trylock; then
|
|
155
164
|
return 0
|
|
156
165
|
fi
|
|
157
166
|
if stamp_fresh "$lock" "$LOCK_STALE"; then
|
|
@@ -162,9 +171,6 @@ acquire() {
|
|
|
162
171
|
return 1
|
|
163
172
|
}
|
|
164
173
|
|
|
165
|
-
# 收尾:只有 leader 杀报名 ssh 并删锁(follower 删锁会毁掉在干活的互斥)。
|
|
166
|
-
# kill -9 的残留由下一任的 LOCK_STALE 判定接管
|
|
167
|
-
|
|
168
174
|
# 回拨建立后的本地副作用(仅 leader 在「本次报名换回新回拨」时调一次,
|
|
169
175
|
# 见下方轮询;层 2 旧回拨直接命中不调,避免每次 ssh 都弹/都踢):
|
|
170
176
|
# ① 桌面通知「直连已就绪」——terminal-notifier 优先,缺它 osascript 兜底;
|
|
@@ -193,6 +199,8 @@ peer_ready() {
|
|
|
193
199
|
fi
|
|
194
200
|
}
|
|
195
201
|
|
|
202
|
+
# 收尾:只有 leader 杀报名 ssh 并删锁(follower 删锁会毁掉在干活的互斥)。
|
|
203
|
+
# kill -9 的残留由下一任的 LOCK_STALE 判定接管
|
|
196
204
|
cleanup() {
|
|
197
205
|
trap - EXIT TERM INT
|
|
198
206
|
if [ "$role" = leader ]; then
|
|
@@ -233,16 +241,25 @@ while time_left; do
|
|
|
233
241
|
fi
|
|
234
242
|
fi
|
|
235
243
|
else
|
|
236
|
-
|
|
244
|
+
# follower 每 tick 只取一次 epoch:失败印记与锁陈旧两处新鲜判定共用
|
|
245
|
+
now=$("$DATE" +%s)
|
|
246
|
+
if stamp_fresh "$STATE_DIR/PROBE_FAIL" "$FAIL_TTL" "$now"; then
|
|
237
247
|
say "经中继连接 $peer"
|
|
238
248
|
exit 1
|
|
239
249
|
fi
|
|
240
250
|
# leader 被 kill/卡死留下陈旧锁 → 用剩余预算接管
|
|
241
|
-
if ! stamp_fresh "$lock" "$LOCK_STALE"; then
|
|
251
|
+
if ! stamp_fresh "$lock" "$LOCK_STALE" "$now"; then
|
|
242
252
|
"$RM" -f "$lock" 2>/dev/null
|
|
243
|
-
if
|
|
244
|
-
|
|
245
|
-
|
|
253
|
+
if trylock; then
|
|
254
|
+
# 抢空锁后复查失败印记:锁也可能是 leader 秒败刚删的——双
|
|
255
|
+
# Match 并发的晚到者可能正夹在「印记已写、锁已删」之间跳过
|
|
256
|
+
# 上面那条检查。新鲜印记下认 follower、释放空锁,不重复报名
|
|
257
|
+
if stamp_fresh "$STATE_DIR/PROBE_FAIL" "$FAIL_TTL"; then
|
|
258
|
+
"$RM" -f "$lock" 2>/dev/null
|
|
259
|
+
else
|
|
260
|
+
role=leader
|
|
261
|
+
announce_now
|
|
262
|
+
fi
|
|
246
263
|
fi
|
|
247
264
|
fi
|
|
248
265
|
fi
|
|
@@ -23,6 +23,7 @@ RETRY_SLEEP=${SSH_NET_RETRY_SLEEP:-30}
|
|
|
23
23
|
alias_name=$1
|
|
24
24
|
user=$2
|
|
25
25
|
label="local.ssh-net-uplink-edge-$alias_name"
|
|
26
|
+
gui_target="gui/$("$ID" -u)/$label"
|
|
26
27
|
state_file="$STATE_DIR/edges/$alias_name"
|
|
27
28
|
|
|
28
29
|
# 事件日志:一行一事件(design §6.2)。ssh 自身 stderr 照旧落同一文件但无
|
|
@@ -63,6 +64,15 @@ rounds=0
|
|
|
63
64
|
# 与 _register 的写有竞态(本删晚于报名的写):报名 RPC 无条件两处同写,
|
|
64
65
|
# 下一次报名即补回片段(窗口只影响人主动 ssh,不影响隧道)
|
|
65
66
|
retire_host_fragment() { "$RM" -f "$CONF_DIR/edge-$alias_name" 2>/dev/null || true; }
|
|
67
|
+
|
|
68
|
+
# 休眠 = edge 当前不在线:删入口片段 + disable + exit 0(launchd 见自愿退出
|
|
69
|
+
# 不重拉)。state 无 v6 与六连败两条路径同构,收在这一处
|
|
70
|
+
hibernate() {
|
|
71
|
+
log "$1"
|
|
72
|
+
retire_host_fragment
|
|
73
|
+
"$LAUNCHCTL" disable "$gui_target"
|
|
74
|
+
exit 0
|
|
75
|
+
}
|
|
66
76
|
while :; do
|
|
67
77
|
if [ -n "${SSH_NET_MAX_ROUNDS:-}" ]; then
|
|
68
78
|
rounds=$((rounds + 1))
|
|
@@ -78,10 +88,7 @@ while :; do
|
|
|
78
88
|
# 文件缺失只可能是人工误删(休眠不删 state):disable 休眠等下次报名,
|
|
79
89
|
# 不空转拨号;conf.d 入口片段照删(与休眠同款收尸,v6 都没了它必是假入口)
|
|
80
90
|
if [ -z "$v6" ]; then
|
|
81
|
-
|
|
82
|
-
retire_host_fragment
|
|
83
|
-
"$LAUNCHCTL" disable "gui/$("$ID" -u)/$label"
|
|
84
|
-
exit 0
|
|
91
|
+
hibernate "state 无 v6(人工误删?)→ disable 休眠,等下次报名唤醒"
|
|
85
92
|
fi
|
|
86
93
|
|
|
87
94
|
# host key 走默认 known_hosts + accept-new(2026-09-29 弃专用信任本;
|
|
@@ -116,10 +123,7 @@ while :; do
|
|
|
116
123
|
if [ "$fails" -ge "$FAIL_LIMIT" ]; then
|
|
117
124
|
# 当下零重拉靠本 exit 0(SuccessfulExit=false 只在非自愿退出时重拉);
|
|
118
125
|
# disable 管加载期拦截与跨登录持久(实测矩阵见 notes)
|
|
119
|
-
|
|
120
|
-
retire_host_fragment
|
|
121
|
-
"$LAUNCHCTL" disable "gui/$("$ID" -u)/$label"
|
|
122
|
-
exit 0
|
|
126
|
+
hibernate "$FAIL_LIMIT 次快速失败 → disable 休眠(正常路径:edge 关机/合盖/不可达;等下次报名唤醒)"
|
|
123
127
|
fi
|
|
124
128
|
# 后台 + wait(不是前台 sleep):wait 可被 trap 打断,前台命令要跑完
|
|
125
129
|
# 才处理 TERM——30s 会把 launchd 的停 job 扣到超时被 SIGKILL(收尾逻辑
|