ssh-net 26.10.54 → 26.10.55
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 +37 -1
- package/README.md +157 -104
- package/libexec/cli.mjs +51 -16
- package/libexec/commands/doctor.mjs +16 -13
- package/libexec/commands/install.mjs +12 -9
- package/libexec/commands/register.mjs +23 -18
- package/libexec/commands/restart.mjs +5 -5
- package/libexec/commands/start.mjs +8 -5
- package/libexec/commands/status.mjs +61 -45
- package/libexec/commands/stop.mjs +56 -20
- package/libexec/commands/uninstall.mjs +62 -22
- package/libexec/commands/update.mjs +20 -15
- package/libexec/lib/cliargs.mjs +10 -4
- package/libexec/lib/jobctl.mjs +5 -4
- package/libexec/lib/launchd.mjs +4 -2
- package/libexec/lib/manifest.mjs +3 -0
- package/libexec/lib/paths.mjs +2 -1
- package/libexec/lib/runtime.mjs +35 -0
- package/libexec/lib/systemd.mjs +4 -2
- package/libexec/sn-route +6 -6
- package/libexec/ssh-net-uplink-edge +3 -3
- package/package.json +3 -3
|
@@ -16,14 +16,14 @@
|
|
|
16
16
|
// 边界:node 角色需要 root(install 写 /etc/systemd/system),先检查再动
|
|
17
17
|
// npm,避免「包装上了、install 被拒」的半截态;core 侧**已注册** edge 的
|
|
18
18
|
// uplink-edge 实体随 install 刷新,job 参数仍等各 edge 下次报名对账。
|
|
19
|
-
import { readRuntimeVersion, RUNTIME_VERSION } from '../lib/runtime.mjs';
|
|
19
|
+
import { readRuntimeVersion, RUNTIME_VERSION, residentRuntimeFingerprint } from '../lib/runtime.mjs';
|
|
20
20
|
import { cmpVersion, VERSION_RE } from '../lib/version.mjs';
|
|
21
21
|
|
|
22
22
|
const NPM_TIMEOUT = 180_000; // 慢网络下全局装包的余量
|
|
23
23
|
const QUERY_TIMEOUT = 60_000; // view 只读查询
|
|
24
24
|
const INSTALL_TIMEOUT = 120_000;
|
|
25
25
|
|
|
26
|
-
const ARG_HINT = 'update
|
|
26
|
+
const ARG_HINT = 'update 只接受一个可选的三段数字版本号';
|
|
27
27
|
const NPM_NOT_FOUND = 'PATH 上找不到 npm——检查 ~/.zshenv(mac)或 .bashrc(Linux)里 node 的 PATH 配置后重试';
|
|
28
28
|
|
|
29
29
|
/** 失败详情:stderr 优先;被信号杀(code null,典型是超时)别说成 exit null */
|
|
@@ -61,9 +61,10 @@ const npmLatest = (runner) => {
|
|
|
61
61
|
export async function run(ctx, args = []) {
|
|
62
62
|
const target = parseArgs(args);
|
|
63
63
|
if (ctx.role === 'node' && ctx.uid !== 0)
|
|
64
|
-
throw new Error('node
|
|
64
|
+
throw new Error('node 上升级需要 root(install 要写 /etc/systemd/system),请运行 sudo -i ssh-net update');
|
|
65
65
|
|
|
66
66
|
const { runner } = ctx;
|
|
67
|
+
const residentBefore = residentRuntimeFingerprint(ctx.paths);
|
|
67
68
|
|
|
68
69
|
// 无参只装 view 确认的 exact 版号;查询失败/旧版都不能拿 @latest 再赌一次
|
|
69
70
|
let resolved = target;
|
|
@@ -86,36 +87,40 @@ export async function run(ctx, args = []) {
|
|
|
86
87
|
const tail = `${kind} ${resolved} ${why}——等几分钟重跑 ssh-net update ${resolved},不用换命令。`;
|
|
87
88
|
throw new Error(`${tail} npm 输出:${r.stderr.trim()}`);
|
|
88
89
|
}
|
|
89
|
-
throw new Error(`npm
|
|
90
|
+
throw new Error(`npm 安装 ${resolved} 失败:${failWhy(r)}(网络或 registry 问题;已装的版本不受影响,稍后重试)`);
|
|
90
91
|
}
|
|
91
92
|
|
|
92
93
|
// 版号地址 code 0 即装上该版,无需再解析 npm 人类文案或 ls 猜版本
|
|
93
94
|
// 换装后用新 bin 跑 install:对账运行时实体/软链/plist/unit 并落
|
|
94
|
-
// runtime-version
|
|
95
|
+
// runtime-version 戳(内容没变不重写或重启;服务配置变化仍可能断连)。PATH 上找 ssh-net:
|
|
95
96
|
// 非交互环境由 dotfiles 保证(同报名 RPC 的前提,design §7 第 5 条)
|
|
96
97
|
const inst = runner.run('ssh-net', ['install'], { timeout: INSTALL_TIMEOUT });
|
|
97
98
|
if (inst.error && inst.error.code === 'ENOENT')
|
|
98
99
|
throw new Error('npm 换装后 PATH 上找不到 ssh-net(全局 bin 未生效)——新开一个 shell 或手动跑一次 ssh-net install;若反复出现,检查 fnm 的 global 包路径');
|
|
99
100
|
if (inst.code !== 0)
|
|
100
|
-
throw new Error(
|
|
101
|
+
throw new Error(`npm 包已切换,但 install 刷新没成功:${failWhy(inst)}——手动跑一次 ssh-net install 修复`);
|
|
101
102
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
//
|
|
105
|
-
|
|
103
|
+
const rel = cmpVersion(resolved, RUNTIME_VERSION);
|
|
104
|
+
const residentChanged = residentRuntimeFingerprint(ctx.paths) !== residentBefore;
|
|
105
|
+
// 提示依据 install 前后的常驻脚本,而不是包版本号或整个 bin/:同版修复也会
|
|
106
|
+
// 换脚本,按需工具与文档-only 更新不需要后台重连提示
|
|
107
|
+
const rollover = residentChanged
|
|
108
|
+
? '提示:常驻脚本已更新;已在运行的后台进程到下次重连才使用新版,服务配置变化或手动 restart 可能断开当前连接'
|
|
109
|
+
: '';
|
|
106
110
|
|
|
107
111
|
// 后置校验:真换装后新 bin 的 install 必把版本戳刷到刚装上的版本
|
|
108
112
|
// (ensureRuntime 无条件 refreshStamp);戳没跟上说明 PATH 解析到了旧
|
|
109
113
|
// ssh-net,install 跑在旧代码上、升级静默没生效(status 的 runtime-version
|
|
110
114
|
// 对账能查到,但当场报比事后对账快)。no-op 时新旧版本恒等,校验无意义,跳过
|
|
111
115
|
const stamp = readRuntimeVersion(ctx.paths);
|
|
112
|
-
const rel = cmpVersion(resolved, RUNTIME_VERSION);
|
|
113
116
|
const stampWarning = rel !== 0 && stamp !== null && stamp !== resolved
|
|
114
|
-
?
|
|
117
|
+
? `⚠️ 后台脚本版本仍为 ${stamp}——刚才的 install 可能跑了旧 ssh-net;用 which -a ssh-net 检查 PATH,再手动运行 ssh-net install`
|
|
115
118
|
: '';
|
|
116
119
|
|
|
117
120
|
const head = target !== null && rel < 0
|
|
118
|
-
? `已切换到指定版本 ${resolved}
|
|
119
|
-
:
|
|
120
|
-
|
|
121
|
+
? `已切换到指定版本 ${resolved}(原版本 ${RUNTIME_VERSION},这是降级)`
|
|
122
|
+
: rel === 0
|
|
123
|
+
? (target !== null ? `当前已是指定版本 ${resolved}` : '已是最新版本')
|
|
124
|
+
: `已升级到 ${resolved}`;
|
|
125
|
+
return [head, inst.stdout.trim(), stampWarning, rollover].filter(Boolean).join('\n');
|
|
121
126
|
}
|
package/libexec/lib/cliargs.mjs
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
|
-
//
|
|
2
|
-
|
|
1
|
+
// 命令行参数与配置恢复输出的共用助手。
|
|
2
|
+
|
|
3
|
+
/** 恢复结果同时交代配置前提与实际处理范围,避免看起来像正常全量体检。 */
|
|
4
|
+
export function recoveryResult(ctx, action) {
|
|
5
|
+
const reason = ctx.configError?.message ?? '未知配置错误';
|
|
6
|
+
return `配置不可用,已进入恢复模式:${action}\n原因:${reason}`;
|
|
7
|
+
}
|
|
3
8
|
|
|
4
9
|
/**
|
|
5
10
|
* core/node 是单总开关:无参或显式 -a/--all 收下(等同缺省),其余参数拒绝。
|
|
6
11
|
* @param {string[]} args
|
|
7
12
|
* @param {'start'|'stop'} verb
|
|
13
|
+
* @param {string} [message] 恢复模式等场景可覆盖错误说明,参数判断仍只有这一份
|
|
8
14
|
*/
|
|
9
|
-
export function assertUnitSwitchArgs(args, verb) {
|
|
15
|
+
export function assertUnitSwitchArgs(args, verb, message) {
|
|
10
16
|
if (args.length && !(args.length === 1 && (args[0] === '-a' || args[0] === '--all')))
|
|
11
|
-
throw new Error(`本机是单总开关,${verb} 不带参数(-a/--all 亦可,等同缺省)`);
|
|
17
|
+
throw new Error(message ?? `本机是单总开关,${verb} 不带参数(-a/--all 亦可,等同缺省)`);
|
|
12
18
|
}
|
package/libexec/lib/jobctl.mjs
CHANGED
|
@@ -14,17 +14,18 @@ function sleepMs(ms) {
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
function isGone(ctx, label) {
|
|
17
|
-
|
|
17
|
+
// 停止路径不能把 launchd 查询失败当成已退出,否则会继续谎报关闭成功
|
|
18
|
+
const st = launchd.printState(ctx.runner, ctx.uid, label, true);
|
|
18
19
|
return !st.registered || (!st.running && st.pid === null);
|
|
19
20
|
}
|
|
20
21
|
|
|
21
22
|
/**
|
|
22
23
|
* @param {object} ctx
|
|
24
|
+
* @param {string} [label] 配置恢复模式按磁盘 label 停止;正常路径取当前 core
|
|
23
25
|
* @returns {{gone:boolean, label:string}}
|
|
24
26
|
*/
|
|
25
|
-
export function reapEdgeLink(ctx) {
|
|
26
|
-
|
|
27
|
-
if (!launchd.printState(ctx.runner, ctx.uid, label).registered)
|
|
27
|
+
export function reapEdgeLink(ctx, label = linkLabel(ctx.topo.core.alias)) {
|
|
28
|
+
if (!launchd.printState(ctx.runner, ctx.uid, label, true).registered)
|
|
28
29
|
return { gone: true, label };
|
|
29
30
|
|
|
30
31
|
launchd.killJob(ctx.runner, ctx.uid, label, 'TERM');
|
package/libexec/lib/launchd.mjs
CHANGED
|
@@ -30,8 +30,10 @@ export function labelsOnDisk(launchAgentsDir, prefix) {
|
|
|
30
30
|
.readdirSync(launchAgentsDir)
|
|
31
31
|
.filter((f) => f.startsWith(prefix) && f.endsWith('.plist'))
|
|
32
32
|
.map((f) => f.slice(0, -'.plist'.length));
|
|
33
|
-
} catch {
|
|
34
|
-
|
|
33
|
+
} catch (err) {
|
|
34
|
+
// 只有目录不存在才是空清单;权限/IO 错误若吞掉,恢复卸载会漏掉在册任务
|
|
35
|
+
if (err.code === 'ENOENT') return [];
|
|
36
|
+
throw err;
|
|
35
37
|
}
|
|
36
38
|
}
|
|
37
39
|
|
package/libexec/lib/manifest.mjs
CHANGED
|
@@ -14,6 +14,9 @@ export const SN_REGISTER_LAN = 'sn-register-lan';
|
|
|
14
14
|
const LINK_CORE = 'ssh-net-link-core';
|
|
15
15
|
const UPLINK_RELAY = 'ssh-net-uplink-relay';
|
|
16
16
|
export const UPLINK_EDGE = 'ssh-net-uplink-edge';
|
|
17
|
+
// 可能被后台 job 长期持有的实体;软链按目标名归入同一组。sn-* 按需工具
|
|
18
|
+
// 不影响 update 的重连提示
|
|
19
|
+
export const RESIDENT_SCRIPTS = Object.freeze([LINK_CORE, UPLINK_RELAY, UPLINK_EDGE]);
|
|
17
20
|
|
|
18
21
|
/**
|
|
19
22
|
* link 附加件专属产物:第 4 实体 + 带机器名软链。edgeRuntimePlan 装了 link
|
package/libexec/lib/paths.mjs
CHANGED
|
@@ -85,6 +85,7 @@ export function machinePaths(env = process.env) {
|
|
|
85
85
|
* @param {string} label local.<...> job label */
|
|
86
86
|
plistFile: (label) => path.join(launchAgentsDir, plistFileName(label)),
|
|
87
87
|
logsDir: path.join(home, 'Library', 'Logs'),
|
|
88
|
-
|
|
88
|
+
// 测试 CLI 的 Linux 恢复路径时重定向到临时目录;生产缺省系统目录
|
|
89
|
+
systemdUnitDir: env.SSH_NET_SYSTEMD_UNIT_DIR ?? '/etc/systemd/system',
|
|
89
90
|
});
|
|
90
91
|
}
|
package/libexec/lib/runtime.mjs
CHANGED
|
@@ -8,12 +8,47 @@
|
|
|
8
8
|
// 版本差即报
|
|
9
9
|
import fs from 'node:fs';
|
|
10
10
|
import path from 'node:path';
|
|
11
|
+
import { createHash } from 'node:crypto';
|
|
11
12
|
import { packageScriptPath } from './paths.mjs';
|
|
13
|
+
import { RESIDENT_SCRIPTS } from './manifest.mjs';
|
|
12
14
|
import { readFileOrNull, readTrimmedOrNull, writeFileAtomic } from './fsutil.mjs';
|
|
13
15
|
import pkg from '../../package.json' with { type: 'json' };
|
|
14
16
|
|
|
15
17
|
export const RUNTIME_VERSION = pkg.version;
|
|
16
18
|
|
|
19
|
+
/**
|
|
20
|
+
* 常驻服务脚本及其服务软链的实际内容指纹。update 用它比较 install 前后是否
|
|
21
|
+
* 换了后台进程需要重连的脚本;sn-* 按需工具更新不参与 rollover 判断。
|
|
22
|
+
* @param {{binDir:string}} paths
|
|
23
|
+
* @returns {string}
|
|
24
|
+
*/
|
|
25
|
+
export function residentRuntimeFingerprint(paths) {
|
|
26
|
+
const hash = createHash('sha256');
|
|
27
|
+
let entries = [];
|
|
28
|
+
try { entries = fs.readdirSync(paths.binDir, { withFileTypes: true }); } catch (err) {
|
|
29
|
+
if (err.code === 'ENOENT') return hash.digest('hex');
|
|
30
|
+
throw err;
|
|
31
|
+
}
|
|
32
|
+
for (const entry of entries.sort((a, b) => a.name.localeCompare(b.name))) {
|
|
33
|
+
const file = path.join(paths.binDir, entry.name);
|
|
34
|
+
const st = fs.lstatSync(file);
|
|
35
|
+
const target = st.isSymbolicLink() ? fs.readlinkSync(file) : entry.name;
|
|
36
|
+
if (!RESIDENT_SCRIPTS.includes(target)) continue;
|
|
37
|
+
hash.update(`${entry.name}\0${st.mode & 0o777}\0`);
|
|
38
|
+
if (st.isSymbolicLink()) {
|
|
39
|
+
hash.update(`link\0${target}\0`);
|
|
40
|
+
} else if (st.isFile()) {
|
|
41
|
+
hash.update('file\0');
|
|
42
|
+
hash.update(fs.readFileSync(file));
|
|
43
|
+
hash.update('\0');
|
|
44
|
+
} else {
|
|
45
|
+
// bin/ 契约只有实体和软链;异常类型也进入指纹,不能被当成无变化
|
|
46
|
+
hash.update(`other\0${st.mode}\0`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return hash.digest('hex');
|
|
50
|
+
}
|
|
51
|
+
|
|
17
52
|
/**
|
|
18
53
|
* 拷贝单脚本到 bin/(实体)。
|
|
19
54
|
* @param {string} dstPath
|
package/libexec/lib/systemd.mjs
CHANGED
|
@@ -26,8 +26,10 @@ export function unitNamesOnDisk(unitDir, prefix) {
|
|
|
26
26
|
try {
|
|
27
27
|
return fs.readdirSync(unitDir)
|
|
28
28
|
.filter((f) => f.startsWith(prefix) && f.endsWith('.service'));
|
|
29
|
-
} catch {
|
|
30
|
-
|
|
29
|
+
} catch (err) {
|
|
30
|
+
// 恢复卸载依赖完整清单;目录不存在才是空,其余错误不能静默吞掉
|
|
31
|
+
if (err.code === 'ENOENT') return [];
|
|
32
|
+
throw err;
|
|
31
33
|
}
|
|
32
34
|
}
|
|
33
35
|
|
package/libexec/sn-route
CHANGED
|
@@ -143,7 +143,7 @@ mark_fail() {
|
|
|
143
143
|
# 需要留下失败印记的中继收尾;软关与已有印记的短路保持只读
|
|
144
144
|
fail_relay() {
|
|
145
145
|
mark_fail
|
|
146
|
-
say "
|
|
146
|
+
say "直连未就绪,本次选择中继($peer)"
|
|
147
147
|
exit 1
|
|
148
148
|
}
|
|
149
149
|
|
|
@@ -228,13 +228,13 @@ fi
|
|
|
228
228
|
|
|
229
229
|
# ── 层 3:PEER_ON 不在 → 不报名
|
|
230
230
|
if [ "$peer_on" = 0 ]; then
|
|
231
|
-
say "
|
|
231
|
+
say "直连未就绪,本次选择中继($peer)"
|
|
232
232
|
exit 1
|
|
233
233
|
fi
|
|
234
234
|
|
|
235
235
|
# ── 层 4:失败印记新鲜 → 不报名
|
|
236
236
|
if stamp_fresh "$STATE_DIR/PROBE_FAIL" "$FAIL_TTL"; then
|
|
237
|
-
say "
|
|
237
|
+
say "直连未就绪,本次选择中继($peer)"
|
|
238
238
|
exit 1
|
|
239
239
|
fi
|
|
240
240
|
|
|
@@ -250,7 +250,7 @@ fi
|
|
|
250
250
|
v6=$("$BIN_DIR/sn-ipv6" 2>/dev/null)
|
|
251
251
|
if [ -z "$v6" ]; then
|
|
252
252
|
mark_fail
|
|
253
|
-
say "当前网络无可用 IPv6
|
|
253
|
+
say "当前网络无可用 IPv6,本次选择中继($peer)"
|
|
254
254
|
exit 1
|
|
255
255
|
fi
|
|
256
256
|
|
|
@@ -355,7 +355,7 @@ while :; do
|
|
|
355
355
|
fi
|
|
356
356
|
else
|
|
357
357
|
if stamp_fresh "$STATE_DIR/PROBE_FAIL" "$FAIL_TTL"; then
|
|
358
|
-
say "
|
|
358
|
+
say "直连未就绪,本次选择中继($peer)"
|
|
359
359
|
exit 1
|
|
360
360
|
fi
|
|
361
361
|
# 前任退出(含 kill -9)后内核锁立即可拿;拿到才成为新 leader
|
|
@@ -364,7 +364,7 @@ while :; do
|
|
|
364
364
|
printf '%s\n' "$("$DATE" +%s)" >"$lock"
|
|
365
365
|
# 抢锁后复查失败印记:前任可能刚写完印记、尚未退出释放锁
|
|
366
366
|
if stamp_fresh "$STATE_DIR/PROBE_FAIL" "$FAIL_TTL"; then
|
|
367
|
-
say "
|
|
367
|
+
say "直连未就绪,本次选择中继($peer)"
|
|
368
368
|
exit 1
|
|
369
369
|
fi
|
|
370
370
|
announce_now
|
|
@@ -48,8 +48,8 @@ on_term() {
|
|
|
48
48
|
}
|
|
49
49
|
trap on_term TERM INT
|
|
50
50
|
|
|
51
|
-
# 休眠 = disable + 自愿 exit 0(launchd 见自愿退出不重拉;disable
|
|
52
|
-
#
|
|
51
|
+
# 休眠 = disable + 自愿 exit 0(launchd 见自愿退出不重拉;disable 用于加载期
|
|
52
|
+
# 拦截,跨重登/重启的持久性仍需真机验证,不在日志里承诺)
|
|
53
53
|
hibernate() {
|
|
54
54
|
log "$1"
|
|
55
55
|
"$LAUNCHCTL" disable "$gui_target"
|
|
@@ -68,7 +68,7 @@ dial() {
|
|
|
68
68
|
fi
|
|
69
69
|
# 文件缺失只可能是人工误删:休眠等下次报名,不空转拨号
|
|
70
70
|
if [ -z "$v6" ]; then
|
|
71
|
-
hibernate "
|
|
71
|
+
hibernate "没有对端地址记录 → 休眠,等对端下次连接时唤醒"
|
|
72
72
|
fi
|
|
73
73
|
|
|
74
74
|
# host key 走默认 known_hosts + accept-new:v6 会轮换,安全兜底是 user key
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ssh-net",
|
|
3
|
-
"version": "26.10.
|
|
3
|
+
"version": "26.10.55",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"description": "个人 SSH 组网:
|
|
5
|
+
"description": "个人 SSH 组网:IPv6 直连优先,中继自动兜底,断开后有界重试、按需恢复",
|
|
6
6
|
"bin": {
|
|
7
7
|
"ssh-net": "libexec/cli.mjs"
|
|
8
8
|
},
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"launchd",
|
|
28
28
|
"systemd",
|
|
29
29
|
"ipv6",
|
|
30
|
-
"reverse-
|
|
30
|
+
"reverse-tunnel"
|
|
31
31
|
],
|
|
32
32
|
"author": "Aex",
|
|
33
33
|
"license": "MIT",
|