ssh-net 26.10.28 → 26.10.30
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 +188 -9
- package/README.md +50 -34
- package/libexec/cli.mjs +10 -21
- package/libexec/commands/checkin.mjs +101 -39
- package/libexec/commands/doctor.mjs +19 -34
- package/libexec/commands/install.mjs +102 -52
- package/libexec/commands/restart.mjs +7 -3
- package/libexec/commands/start.mjs +23 -53
- package/libexec/commands/status.mjs +79 -63
- package/libexec/commands/stop.mjs +19 -33
- package/libexec/commands/uninstall.mjs +1 -0
- package/libexec/commands/upgrade.mjs +13 -7
- package/libexec/lib/config.mjs +28 -48
- package/libexec/lib/ipv6.mjs +1 -1
- package/libexec/lib/jobctl.mjs +7 -6
- package/libexec/lib/launchd.mjs +5 -2
- package/libexec/lib/manage.mjs +11 -3
- package/libexec/lib/paths.mjs +17 -12
- package/libexec/lib/render.mjs +36 -23
- package/libexec/lib/runtime.mjs +3 -11
- package/libexec/lib/state.mjs +10 -12
- package/libexec/sn-route +187 -65
- package/libexec/sn-signal +35 -28
- package/libexec/ssh-net-link-core +13 -474
- package/libexec/ssh-net-uplink-edge +16 -9
- package/package.json +1 -1
|
@@ -1,46 +1,31 @@
|
|
|
1
|
-
// doctor(design §
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
// doctor 只覆盖「人就在机器前、不想等」的场景(典型:公共热点入站被拦、
|
|
6
|
-
// 唤醒后网络慢半拍落了中继、core 侧回拨休眠中)。仅 edge 有动作:
|
|
7
|
-
// core 的 uplink-edge 醒来要靠 edge 报名(core 无 v6 原料),node 的
|
|
8
|
-
// relay 由 systemd 自动重启,二者无 doctor 可为。
|
|
1
|
+
// doctor(design §5):人在机器前的「立即按需确保」——直接跑一次 sn-route
|
|
2
|
+
// show:它会 probe、经 relay 报名、等回拨(6s 预算),进度直接显示在终端。
|
|
3
|
+
// doctor 只负责关态分诊与结果转述。不新增状态、不踢常驻服务(默认形态没有
|
|
4
|
+
// 常驻)。仅 edge 有动作:core 的恢复靠 edge 报名,node 由 systemd 自动重启。
|
|
9
5
|
import path from 'node:path';
|
|
10
|
-
import * as launchd from '../lib/launchd.mjs';
|
|
11
|
-
import { peerProbeOk } from '../lib/probe.mjs';
|
|
12
|
-
import { linkLabel } from '../lib/render.mjs';
|
|
13
6
|
|
|
14
7
|
export async function run(ctx) {
|
|
15
8
|
if (ctx.role !== 'edge')
|
|
16
9
|
return '本机不是 edge,无需处理:core 等对应笔记本报名自动恢复,node 由 systemd 自动重启';
|
|
17
10
|
|
|
18
|
-
//
|
|
11
|
+
// 关态分诊不依赖文件存在,先答最关心的
|
|
19
12
|
if (!ctx.state.isOn('relay'))
|
|
20
13
|
return '隧道关着(在办公室,局域网直连即可)。需要时:ssh-net start';
|
|
21
14
|
if (!ctx.state.isOn('peer'))
|
|
22
|
-
return 'peer
|
|
15
|
+
return 'peer 开关关着:不会报名。要被 core 回拨:ssh-net start peer';
|
|
23
16
|
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
17
|
+
const { topo, paths, self } = ctx;
|
|
18
|
+
// stdio:'inherit':sn-route 进度直达终端,同时其 [ -t 2 ] 才成立;
|
|
19
|
+
// timeout 略大于 6s 预算
|
|
20
|
+
const r = ctx.runner.runRaw(path.join(paths.binDir, 'sn-route'), [
|
|
21
|
+
self.alias, topo.core.alias, topo.relay.alias,
|
|
22
|
+
String(topo.core.relayPort), topo.core.user, 'show',
|
|
23
|
+
], { stdio: 'inherit', timeout: 8000 });
|
|
28
24
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
// 「没有 IPv6」——那个结论只在 sn-ipv6 正常跑完且无输出时才成立
|
|
36
|
-
const probe = ctx.runner.runRaw(path.join(ctx.paths.binDir, 'sn-ipv6'), [], { timeout: 5000 });
|
|
37
|
-
const v6 = probe.stdout.trim();
|
|
38
|
-
const probeOk = !probe.error && probe.code === 0;
|
|
39
|
-
const r = launchd.kickstartEnabled(ctx.runner, ctx.uid, label);
|
|
40
|
-
if (r.code !== 0) throw new Error(`立即重估失败:${r.stderr.trim()}`);
|
|
41
|
-
if (!probeOk)
|
|
42
|
-
return '已触发立即重估(几秒内重新报名 + 等回拨)。本机 v6 探测没跑成(sn-ipv6 异常)——以 ssh-net status 的「对外地址」实测为准;若几分钟后仍走中继,多半是这段网络入站被拦(公共热点常见),中继不受影响,ssh-net status 查看结果';
|
|
43
|
-
return v6
|
|
44
|
-
? `已触发立即重估(几秒内重新报名 + 等回拨)。本机 v6:${v6};若几分钟后仍走中继,多半是这段网络入站被拦(公共热点常见),中继不受影响,ssh-net status 查看结果`
|
|
45
|
-
: '已触发立即重估。当前网络没有可用 IPv6,只能走中继;回到有 v6 的网络约 15 秒内自动切回直连';
|
|
25
|
+
if (r.error && r.error.code === 'ENOENT')
|
|
26
|
+
throw new Error('未安装(缺 sn-route)——先跑 ssh-net install');
|
|
27
|
+
if (r.error) throw new Error(`doctor 执行失败:${r.error.message}`);
|
|
28
|
+
if (r.code === 0)
|
|
29
|
+
return `线路正常(peer 直达 ${topo.core.alias}),无需处理`;
|
|
30
|
+
return `本次经中继连接 ${topo.core.alias}(进度见上)。中继不受影响;持续异常查网络入站或 ssh-net status`;
|
|
46
31
|
}
|
|
@@ -5,9 +5,9 @@
|
|
|
5
5
|
import fs from 'node:fs';
|
|
6
6
|
import os from 'node:os';
|
|
7
7
|
import path from 'node:path';
|
|
8
|
-
import {
|
|
8
|
+
import { renderForwardArgs } from '../lib/config.mjs';
|
|
9
9
|
import { CALLBACK_PORT } from '../lib/constants.mjs';
|
|
10
|
-
import { syncPlist, writeFileAtomic } from '../lib/manage.mjs';
|
|
10
|
+
import { syncPlist, writeFileAtomic, removeJob } from '../lib/manage.mjs';
|
|
11
11
|
import { syncConfFile } from '../lib/conf.mjs';
|
|
12
12
|
import { ensureRuntime } from '../lib/runtime.mjs';
|
|
13
13
|
import * as launchd from '../lib/launchd.mjs';
|
|
@@ -73,65 +73,111 @@ async function precheckEdgePorts(ctx) {
|
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
-
|
|
76
|
+
/** 解析 edge install 的 link 意图:true=--with-link、false=--without-link、
|
|
77
|
+
* undefined=无 flag;两 flag 同现或夹带未知参数即用法错误 */
|
|
78
|
+
function parseLinkFlag(args) {
|
|
79
|
+
/** @type {boolean|undefined} */
|
|
80
|
+
let flag;
|
|
81
|
+
for (const a of args) {
|
|
82
|
+
if (a === '--with-link' || a === '--without-link') {
|
|
83
|
+
if (flag !== undefined)
|
|
84
|
+
throw new Error('--with-link 与 --without-link 不能同时使用');
|
|
85
|
+
flag = a === '--with-link';
|
|
86
|
+
} else {
|
|
87
|
+
throw new Error(`未知参数:${a}——edge install 只接受 --with-link / --without-link`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return flag;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async function installEdge(ctx, args) {
|
|
77
94
|
const { topo, paths } = ctx;
|
|
78
95
|
// 预检纯读且在落任何产物之前:被拒时 bin/ 一个都不建
|
|
79
96
|
await precheckEdgePorts(ctx);
|
|
80
|
-
// bin/ 形态(design §2.3):实体原名 + 带机器名软链(plist argv[0] 经软链,
|
|
81
|
-
// 进程第一列可辨识);sn-* 工具全角色同名,服务链名带对端别名
|
|
82
|
-
const comp = compNote(ensureRuntime(ctx,
|
|
83
|
-
[
|
|
84
|
-
['ssh-net-link-core', 'ssh-net-link-core'],
|
|
85
|
-
['sn-probe', 'sn-probe'],
|
|
86
|
-
['sn-route', 'sn-route'], // Match exec 读 ROUTE_PEER 判决(design §2.2)
|
|
87
|
-
['sn-ipv6', 'sn-ipv6'],
|
|
88
|
-
['sn-signal', 'sn-signal'], // 机器间信令(recv:core 信令接收端在本机的入口)
|
|
89
|
-
['sn-notify', 'sn-notify'], // link 线路状态通知(mode 变化时调)
|
|
90
|
-
],
|
|
91
|
-
[[linkLinkName(topo.core.alias), 'ssh-net-link-core']]));
|
|
92
97
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
98
|
+
const flag = parseLinkFlag(args);
|
|
99
|
+
const label = linkLabel(topo.core.alias);
|
|
100
|
+
const plistPath = path.join(paths.launchAgentsDir, plistFileName(label));
|
|
101
|
+
|
|
102
|
+
// 显式意图先落/删 marker;无参 install(含 upgrade 内置)不触碰它
|
|
103
|
+
if (flag === true) {
|
|
104
|
+
fs.mkdirSync(paths.sshNetDir, { recursive: true });
|
|
105
|
+
writeFileAtomic(paths.linkEnabledFile, '1\n', 0o600);
|
|
106
|
+
}
|
|
107
|
+
if (flag === false) fs.rmSync(paths.linkEnabledFile, { force: true });
|
|
108
|
+
const linkOn = fs.existsSync(paths.linkEnabledFile);
|
|
109
|
+
|
|
110
|
+
// 基础 5 实体(sn-probe/route/ipv6/signal/notify);linkOn 才加第 6 实体
|
|
111
|
+
// 与带机器名软链
|
|
112
|
+
/** @type {[string,string][]} */
|
|
113
|
+
const entities = [
|
|
114
|
+
['sn-probe', 'sn-probe'],
|
|
115
|
+
['sn-route', 'sn-route'], // Match exec 按需选路(design §2.2)
|
|
116
|
+
['sn-ipv6', 'sn-ipv6'],
|
|
117
|
+
['sn-signal', 'sn-signal'], // recv:core 回拨信令在本机的接收入口
|
|
118
|
+
['sn-notify', 'sn-notify'], // 回拨成功的系统通知
|
|
119
|
+
];
|
|
120
|
+
/** @type {[string,string][]} */
|
|
121
|
+
const links = [];
|
|
122
|
+
if (linkOn) {
|
|
123
|
+
entities.push(['ssh-net-link-core', 'ssh-net-link-core']);
|
|
124
|
+
links.push([linkLinkName(topo.core.alias), 'ssh-net-link-core']);
|
|
125
|
+
}
|
|
126
|
+
const comp = compNote(ensureRuntime(ctx, entities, links));
|
|
127
|
+
|
|
128
|
+
if (linkOn) {
|
|
129
|
+
// 附加件:渲染纯 ssh plist 并对账(RELAY_ON 在 syncPlist 按 stopped 语义
|
|
130
|
+
// 首发,不在注册待命);转发参数是最终 -D/-L argv
|
|
131
|
+
const content = renderEdgeLinkPlist({
|
|
132
|
+
binDir: paths.binDir,
|
|
133
|
+
coreAlias: topo.core.alias,
|
|
134
|
+
forwardArgs: renderForwardArgs(topo.forwards, ctx.hostTable),
|
|
135
|
+
relayOnFile: paths.switchFiles.relay,
|
|
136
|
+
logsDir: paths.logsDir,
|
|
137
|
+
});
|
|
138
|
+
syncPlist(ctx, { label, plistPath, content });
|
|
139
|
+
} else {
|
|
140
|
+
// 默认零常驻:清掉旧版/加装过的 link 全部产物。升级时旧 plist 必须
|
|
141
|
+
// bootout,否则 launchd 每 ThrottleInterval 刷 ENOENT
|
|
142
|
+
removeJob(ctx, label, plistPath);
|
|
143
|
+
fs.rmSync(path.join(paths.binDir, 'ssh-net-link-core'), { force: true });
|
|
144
|
+
fs.rmSync(path.join(paths.binDir, linkLinkName(topo.core.alias)), { force: true });
|
|
145
|
+
// 退役状态残留(paths 已无这些键名,按 state 布局直接删)
|
|
146
|
+
fs.rmSync(path.join(paths.stateDir, 'ROUTE_PEER'), { force: true });
|
|
147
|
+
fs.rmSync(path.join(paths.stateDir, 'LINK_ON'), { force: true });
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// 线路通知组件(2026-09-30):已装则顺手 open 一次它的 .app 触发通知授权
|
|
151
|
+
// 注册(terminal-notifier 不主动请求权限,首次发通知静默收不到;open 过再
|
|
152
|
+
// 发就通——edge 实测)。未装不替用户 brew install(改变系统的动作留给
|
|
153
|
+
// 用户),给出确切命令
|
|
97
154
|
const tn = ctx.tnPath === undefined ? detectTerminalNotifier() : ctx.tnPath;
|
|
98
155
|
let notifyNote = '';
|
|
99
156
|
if (tn) {
|
|
100
|
-
|
|
101
|
-
|
|
157
|
+
// realpath 可能因 brew 升级中换目录而抛错——感知增强组件不该打断
|
|
158
|
+
// 安装主流程(对齐下面 open 的「失败静默」),拿不到就跳过授权触发
|
|
159
|
+
let app = null;
|
|
160
|
+
try {
|
|
161
|
+
app = path.join(path.dirname(fs.realpathSync(tn)), '..', 'terminal-notifier.app');
|
|
162
|
+
} catch { app = null; }
|
|
163
|
+
if (app && fs.existsSync(app)) ctx.runner.run('/usr/bin/open', [app]); // 失败静默,授权仍可能已生效
|
|
102
164
|
notifyNote = '。线路通知组件已就位(如系统设置→通知里问授权,请允许)';
|
|
103
165
|
} else {
|
|
104
166
|
notifyNote = '。线路通知:未检测到 terminal-notifier——brew install terminal-notifier 后通知更美观(装完 open "$(brew --prefix terminal-notifier)/terminal-notifier.app" 一次触发授权);不装则回退 osascript';
|
|
105
167
|
}
|
|
106
168
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
selfAlias: ctx.self.alias,
|
|
111
|
-
relayAlias: topo.relay.alias,
|
|
112
|
-
relayPort: topo.core.relayPort,
|
|
113
|
-
coreUser: topo.core.user,
|
|
114
|
-
forwardArgs: buildForwardSpecs(topo.forwards, ctx.hostTable),
|
|
115
|
-
relayOnFile: paths.switchFiles.relay,
|
|
116
|
-
logsDir: paths.logsDir,
|
|
117
|
-
});
|
|
118
|
-
const r = syncPlist(ctx, {
|
|
119
|
-
label: linkLabel(topo.core.alias),
|
|
120
|
-
plistPath: path.join(paths.launchAgentsDir, plistFileName(linkLabel(topo.core.alias))),
|
|
121
|
-
content,
|
|
122
|
-
});
|
|
123
|
-
// conf.d 片段(design §4.2):edge 是 routes 与 vscode-mark 的唯一安装角色——
|
|
124
|
-
// RELAY_ON、VSCODE_IPC_HOOK 都只在 edge 存在,其他机器写了恒不命中;
|
|
125
|
-
// core/node 的 conf.d 没有这两个文件
|
|
126
|
-
const routesChanged = syncConfFile(paths.routesFile, renderRoutes(topo));
|
|
169
|
+
// conf.d 片段(design §4.2):edge 是 routes 与 vscode-mark 的唯一安装角色
|
|
170
|
+
// ——RELAY_ON、VSCODE_IPC_HOOK 都只在 edge 存在;routes 新签名带本机别名
|
|
171
|
+
const routesChanged = syncConfFile(paths.routesFile, renderRoutes(topo, ctx.self.alias));
|
|
127
172
|
const markChanged = syncConfFile(paths.vscodeMarkFile, renderVscodeMark());
|
|
128
173
|
const fragMsg = `${routesChanged ? '(选路片段已更新)' : ''}${markChanged ? '(VS Code 打标片段已更新)' : ''}`;
|
|
129
|
-
if (
|
|
130
|
-
return
|
|
131
|
-
?
|
|
132
|
-
:
|
|
174
|
+
if (!comp && !routesChanged && !markChanged && flag === undefined) {
|
|
175
|
+
return linkOn
|
|
176
|
+
? `已安装(link 附加件在),无需改动${notifyNote}`
|
|
177
|
+
: `已安装,无需改动(默认零常驻)${notifyNote}`;
|
|
133
178
|
}
|
|
134
|
-
|
|
179
|
+
const head = linkOn ? '安装完成(含 link 附加件)' : '安装完成(默认零常驻;要 SOCKS/VNC 转发口跑 ssh-net install --with-link)';
|
|
180
|
+
return `${head}${comp}${fragMsg}${notifyNote}`;
|
|
135
181
|
}
|
|
136
182
|
|
|
137
183
|
/** 磁盘上已注册的 uplink-edge 别名(plist glob;topology 已删的机器靠它兜底)。 */
|
|
@@ -194,6 +240,10 @@ function installNode(ctx) {
|
|
|
194
240
|
// (sudo -E,或部分发行版的默认值)会把调用者的家目录写进 unit——root
|
|
195
241
|
// 运行的 ssh 将去读别人的 ~/.ssh/config,relay 别名解析错配且无声
|
|
196
242
|
const home = ctx.nodeHome ?? os.userInfo().homedir;
|
|
243
|
+
// ExecStart 按空白分词、无 shell 引号转义通路,HOME 含空白会拆断 bin 路径
|
|
244
|
+
// (plist 侧是 XML 数组无此问题)——当场拒绝比装出一个起不来的 unit 诚实
|
|
245
|
+
if (/\s/.test(home))
|
|
246
|
+
throw new Error(`node 的 HOME(${home})含空白字符:systemd ExecStart 无法转义,拒绝安装`);
|
|
197
247
|
const content = renderRelayUnit({
|
|
198
248
|
binDir: paths.binDir,
|
|
199
249
|
relayAlias: topo.relay.alias,
|
|
@@ -272,17 +322,17 @@ function reconcileEdgeFragments(ctx) {
|
|
|
272
322
|
fs.rmSync(path.join(paths.confDir, f), { force: true });
|
|
273
323
|
}
|
|
274
324
|
|
|
275
|
-
/**
|
|
276
|
-
|
|
325
|
+
/**
|
|
326
|
+
* @param {object} ctx
|
|
327
|
+
* @param {string[]} [args] edge 可带 --with-link/--without-link
|
|
328
|
+
*/
|
|
329
|
+
export async function run(ctx, args = []) {
|
|
277
330
|
let result;
|
|
278
331
|
switch (ctx.role) {
|
|
279
|
-
case 'edge': result = await installEdge(ctx); break;
|
|
332
|
+
case 'edge': result = await installEdge(ctx, args); break;
|
|
280
333
|
case 'core': result = installCore(ctx); break;
|
|
281
334
|
case 'node': result = installNode(ctx); break;
|
|
282
335
|
default: throw new Error(`未知角色:${ctx.role}`);
|
|
283
336
|
}
|
|
284
|
-
// 迁移窗口提示:还在吃旧名 config,dot apply 出新 topology 后旧文件可手删
|
|
285
|
-
if (ctx.legacyTopology)
|
|
286
|
-
result += '(当前仍读旧文件 ~/.ssh/ssh-net/config 兜底——dotfiles apply ssh 后可删它)';
|
|
287
337
|
return result;
|
|
288
338
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
// restart(design §5):edge
|
|
2
|
-
// core kickstart uplink-relay;
|
|
1
|
+
// restart(design §5):edge 上只对 link 附加件有对象(kickstart 纯 ssh job
|
|
2
|
+
// 重新选路);默认零常驻形态引导用 doctor。core kickstart uplink-relay;
|
|
3
|
+
// node systemctl restart。
|
|
3
4
|
import * as launchd from '../lib/launchd.mjs';
|
|
4
5
|
import * as systemd from '../lib/systemd.mjs';
|
|
5
6
|
import { linkLabel, relayLabel, relayUnitName } from '../lib/render.mjs';
|
|
@@ -8,10 +9,13 @@ export function run(ctx) {
|
|
|
8
9
|
if (ctx.role === 'edge') {
|
|
9
10
|
if (!ctx.state.isOn('relay')) throw new Error('隧道未开启——先 ssh-net start');
|
|
10
11
|
const label = linkLabel(ctx.topo.core.alias);
|
|
12
|
+
if (!launchd.inspect(ctx.runner, ctx.uid, label).registered) {
|
|
13
|
+
return '默认形态没有常驻服务,没有可重启的对象——人在机器前要立即选路跑 ssh-net doctor;要 SOCKS/VNC 转发口跑 ssh-net install --with-link';
|
|
14
|
+
}
|
|
11
15
|
// kickstartEnabled:与 core 同规则,残留 disabled override 时不被拒
|
|
12
16
|
const r = launchd.kickstartEnabled(ctx.runner, ctx.uid, label);
|
|
13
17
|
if (r.code !== 0) throw new Error(`重启失败:${r.stderr.trim()}`);
|
|
14
|
-
return '
|
|
18
|
+
return 'link 已重启(重新选路中)';
|
|
15
19
|
}
|
|
16
20
|
if (ctx.role === 'core') {
|
|
17
21
|
const label = relayLabel(ctx.topo.relay.alias);
|
|
@@ -1,29 +1,25 @@
|
|
|
1
|
-
// start(design §5):edge
|
|
2
|
-
// relay
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
1
|
+
// start(design §5):edge 两开关(缺省或 -a/--all=relay+peer,peer 自动补
|
|
2
|
+
// relay + 立即报名);core 重新 enable+bootstrap uplink-relay;node systemctl。
|
|
3
|
+
// link 不再是运行时开关(转发口只取决于装没装附加件)。重复 start 是幂等
|
|
4
|
+
// 确认:动作照做(重新报名让恢复立即发生),措辞区分「已开启」与「已是开启
|
|
5
|
+
// 状态」。
|
|
6
6
|
import fs from 'node:fs';
|
|
7
7
|
import path from 'node:path';
|
|
8
8
|
import * as launchd from '../lib/launchd.mjs';
|
|
9
9
|
import * as systemd from '../lib/systemd.mjs';
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
10
|
+
import { relayLabel, relayUnitName, plistFileName } from '../lib/render.mjs';
|
|
11
|
+
import { enablePeer } from './checkin.mjs';
|
|
12
12
|
|
|
13
|
-
const TARGETS = ['relay', 'peer'
|
|
13
|
+
const TARGETS = ['relay', 'peer'];
|
|
14
14
|
|
|
15
15
|
function parseTargets(args) {
|
|
16
|
-
if (args.length === 0 || args.length === 1 && (args[0] === '-a' || args[0] === '--all'))
|
|
17
|
-
return ['relay', 'peer'
|
|
18
|
-
if (args.length
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
/** 细粒度开关立即生效:kickstart 让循环重来(自检新开关)。失败不阻断——
|
|
24
|
-
* 开关已置,下轮循环兜底;结果交调用方诚实措辞。 */
|
|
25
|
-
function kickLinkJob(ctx) {
|
|
26
|
-
return launchd.kickstartEnabled(ctx.runner, ctx.uid, linkLabel(ctx.topo.core.alias));
|
|
16
|
+
if (args.length === 0 || (args.length === 1 && (args[0] === '-a' || args[0] === '--all')))
|
|
17
|
+
return ['relay', 'peer'];
|
|
18
|
+
if (args.length === 1 && TARGETS.includes(args[0]))
|
|
19
|
+
return [args[0]];
|
|
20
|
+
if (args.length === 1 && args[0] === 'link')
|
|
21
|
+
throw new Error('link 不再是运行时开关:转发口只取决于装没装附加件——加装 ssh-net install --with-link,移除 ssh-net install --without-link');
|
|
22
|
+
throw new Error('用法:start [-a|--all|relay|peer](不带参数或 -a=relay+peer)');
|
|
27
23
|
}
|
|
28
24
|
|
|
29
25
|
function startCore(ctx) {
|
|
@@ -32,7 +28,8 @@ function startCore(ctx) {
|
|
|
32
28
|
if (!fs.existsSync(plist)) throw new Error(`尚未安装(缺 ${plist}),先跑 ssh-net install`);
|
|
33
29
|
const st = launchd.inspect(ctx.runner, ctx.uid, label);
|
|
34
30
|
if (!st.registered) {
|
|
35
|
-
launchd.enable(ctx.runner, ctx.uid, label);
|
|
31
|
+
const en = launchd.enable(ctx.runner, ctx.uid, label);
|
|
32
|
+
if (en.code !== 0) throw new Error(`enable 失败:${en.stderr.trim()}`);
|
|
36
33
|
const r = launchd.bootstrap(ctx.runner, ctx.uid, plist);
|
|
37
34
|
if (r.code !== 0) throw new Error(`启动失败:${r.stderr.trim()}`);
|
|
38
35
|
return 'relay 已开启';
|
|
@@ -50,45 +47,18 @@ function startCore(ctx) {
|
|
|
50
47
|
|
|
51
48
|
export function run(ctx, args = []) {
|
|
52
49
|
if (ctx.role === 'edge') {
|
|
53
|
-
const label = linkLabel(ctx.topo.core.alias);
|
|
54
|
-
const st = launchd.inspect(ctx.runner, ctx.uid, label);
|
|
55
|
-
if (!st.registered)
|
|
56
|
-
throw new Error(`link job 未注册(先 ssh-net install):launchctl print gui/${ctx.uid}/${label}`);
|
|
57
|
-
if (!fs.existsSync(path.join(ctx.paths.launchAgentsDir, plistFileName(label))))
|
|
58
|
-
throw new Error('job 配置文件缺失(注册态仍在)——先跑一次 ssh-net install');
|
|
59
|
-
|
|
60
50
|
const before = ctx.state.readSwitches();
|
|
61
|
-
const targets =
|
|
62
|
-
if (targets.has('peer') || targets.has('link')) targets.add('relay'); // 依赖级联
|
|
63
|
-
ctx.state.setOn([...targets]);
|
|
51
|
+
const targets = parseTargets(args); // ['relay','peer'] 或 ['relay'];link 抛错
|
|
64
52
|
|
|
65
|
-
if (targets.
|
|
66
|
-
//
|
|
67
|
-
// 顺手报一次名让 peer 立即跟上;全关态 PathState 拉起循环本来就会报,
|
|
68
|
-
// 多报一次对 core 是 no-op,失败静默(循环自己会报)
|
|
69
|
-
let announced = false;
|
|
70
|
-
try { runEdgeAnnounce(ctx); announced = true; } catch { /* 循环兜底 */ }
|
|
71
|
-
// 幂等确认:开关本来全开——动作照做(让恢复立即发生),措辞说清
|
|
72
|
-
const prefix = before.relay && before.peer && before.link ? '已是开启状态' : '已开启';
|
|
73
|
-
return announced
|
|
74
|
-
? `隧道${prefix}(relay · peer · link;已报名,core 几秒内回拨,ssh-net status 查看线路)`
|
|
75
|
-
: `隧道${prefix}(relay · peer · link;数秒内建立,ssh-net status 查看线路)`;
|
|
76
|
-
}
|
|
77
|
-
if (targets.has('peer')) {
|
|
78
|
-
// 与 checkin 同一条实现;幂等(relay+peer 本就在)时换措辞
|
|
53
|
+
if (targets.includes('peer')) {
|
|
54
|
+
// 默认/-a/peer:enablePeer 置两开关(幂等)+ 立即报名带 now
|
|
79
55
|
const msg = enablePeer(ctx);
|
|
80
56
|
return before.relay && before.peer
|
|
81
|
-
? msg.replace('peer 已开启', '
|
|
57
|
+
? msg.replace('peer 已开启', '隧道已是开启状态')
|
|
82
58
|
: msg;
|
|
83
59
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
const prefix = before.relay && before.link ? '已是开启状态' : '已开启';
|
|
87
|
-
return r.code === 0
|
|
88
|
-
? `link ${prefix}(relay 一并开启;已触发重估,本地转发口几秒内建立)`
|
|
89
|
-
: `link ${prefix}(relay 一并开启;重估没触发成功,转发口随下轮循环自动建立)`;
|
|
90
|
-
}
|
|
91
|
-
return `relay ${before.relay ? '已是开启状态' : '已开启'}(隧道基础。peer/link 未开时不报名、无本地转发——要被 core 连到再 start peer,要本机代理再 start link)`;
|
|
60
|
+
ctx.state.setOn(['relay']);
|
|
61
|
+
return `relay ${before.relay ? '已是开启状态' : '已开启'}(隧道基础,不报名——要被 core 连到再 ssh-net start peer)`;
|
|
92
62
|
}
|
|
93
63
|
// core/node 是单总开关(等同 --all):显式 -a/--all 收下,其余参数拒绝
|
|
94
64
|
if (args.length && !(args.length === 1 && (args[0] === '-a' || args[0] === '--all')))
|