ssh-net 0.1.0
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 +24 -0
- package/LICENSE +21 -0
- package/README.md +81 -0
- package/libexec/cli.mjs +114 -0
- package/libexec/commands/checkin.mjs +86 -0
- package/libexec/commands/install.mjs +129 -0
- package/libexec/commands/restart.mjs +30 -0
- package/libexec/commands/start.mjs +46 -0
- package/libexec/commands/status.mjs +187 -0
- package/libexec/commands/stop.mjs +37 -0
- package/libexec/commands/uninstall.mjs +85 -0
- package/libexec/lib/config.mjs +227 -0
- package/libexec/lib/constants.mjs +6 -0
- package/libexec/lib/exec.mjs +67 -0
- package/libexec/lib/identity.mjs +34 -0
- package/libexec/lib/ipv6.mjs +39 -0
- package/libexec/lib/jobctl.mjs +40 -0
- package/libexec/lib/launchd.mjs +106 -0
- package/libexec/lib/links.mjs +61 -0
- package/libexec/lib/manage.mjs +108 -0
- package/libexec/lib/paths.mjs +76 -0
- package/libexec/lib/probe.mjs +116 -0
- package/libexec/lib/render.mjs +157 -0
- package/libexec/lib/state.mjs +76 -0
- package/libexec/lib/systemd.mjs +30 -0
- package/libexec/peer-probe +20 -0
- package/libexec/ssh-net-link-core +94 -0
- package/libexec/ssh-net-uplink-edge +97 -0
- package/libexec/ssh-net-uplink-relay +15 -0
- package/libexec/v6-address +59 -0
- package/package.json +38 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
本项目版本号与发布节奏仿 mihomo-cli:npm 公共分发,版本即升级动作。
|
|
4
|
+
|
|
5
|
+
## [0.1.0] - 2026-09-29
|
|
6
|
+
|
|
7
|
+
首个可发布版本(迁移 Phase 1 软件交付,未做各机部署)。
|
|
8
|
+
|
|
9
|
+
### 新增
|
|
10
|
+
|
|
11
|
+
- 控制面 `ssh-net`(Node 24+ ESM、零依赖):`install` / `uninstall` /
|
|
12
|
+
`start` / `stop` / `restart` / `status` 与 core 专用 RPC `checkin`。
|
|
13
|
+
- 五个运行时 shell 脚本:edge `ssh-net-link-core`(报名 + 8s probe 窗口 +
|
|
14
|
+
relay 600s 寿命)、core/node `ssh-net-uplink-relay`(exec ssh)、
|
|
15
|
+
core `ssh-net-uplink-edge`(活满 30s 清零、6 连败 disable 休眠)、
|
|
16
|
+
`peer-probe`(读 SSH banner 判僵尸)、`v6-address`(autoconf secured 选取)。
|
|
17
|
+
- topology 配置严格校验;plist/systemd unit 由控制面单一来源渲染,
|
|
18
|
+
install 幂等(渲染结果字节相同不 bootout,不断连)。
|
|
19
|
+
- checkin 四态(未注册 / 休眠 / running-v6 同 / v6 变或未运行)按 m4
|
|
20
|
+
launchctl 实测矩阵编排;唤醒固定 enable + kickstart -k。报名先对账
|
|
21
|
+
软链与 plist:被删/悬空即重建,参数(user 等)变更即重新部署。
|
|
22
|
+
- 软链目标经 fnm `aliases/default` 规范化:升级 node 重装包后自动跟随,
|
|
23
|
+
不跑 install;漏装/卸包时链悬空、status 显眼报错。
|
|
24
|
+
- status 软链体检 + 端口/banner/注册态真实探测,输出按角色分行。
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Aex
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# ssh-net
|
|
2
|
+
|
|
3
|
+
个人 SSH 组网:把散在不同网络里的五台机器,用原生 ssh 隧道拼成一张「互相可达」的网。
|
|
4
|
+
单用户、只给自己用。
|
|
5
|
+
|
|
6
|
+
## 它解决什么
|
|
7
|
+
|
|
8
|
+
```
|
|
9
|
+
在公司 edge(MacBook)── 局域网原生直连 ──► core(m4) / node(n007,n061)
|
|
10
|
+
在家 edge ── 公网 IPv6 一条 ssh(peer)──► core
|
|
11
|
+
edge ── ProxyJump s100(relay)───► core / node
|
|
12
|
+
兜底/咖啡厅 直连没建立或只有 v4 时,全部自动走 relay
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
- **edge**:mb5、mb1(两台 MacBook,人在的一端,有公网 v6,经常关机)
|
|
16
|
+
- **core**:m4(Mac mini,7×24 常驻办公室,内网锚点与 SOCKS 出口)
|
|
17
|
+
- **node**:n007、n061(Linux devbox,只走 relay 兜底)
|
|
18
|
+
- **relay**:s100(公网 VPS,只跑标准 sshd,不装 ssh-net)
|
|
19
|
+
|
|
20
|
+
关机/合盖后 core 有界重试(最坏约 6 分钟)即 disable 休眠、彻底静默
|
|
21
|
+
(作业保留不删,跨重启不复发);开盖后约 2 分钟经 checkin 自动唤醒恢复,
|
|
22
|
+
入站恢复后最坏 10 分钟从 relay 升回直连,无需手动操作。
|
|
23
|
+
|
|
24
|
+
## 设计原则
|
|
25
|
+
|
|
26
|
+
- **常驻进程只有套着薄脚本的原生 ssh**:npm 包内每类一个 bash 脚本,install
|
|
27
|
+
按机器名建软链(差异只走参数),不做心跳、TTL、janitor、消息队列——在线
|
|
28
|
+
状态就是 TCP 会话本身
|
|
29
|
+
- **控制面与运行时分离**:控制面是 Node 24+ 的 mjs(install/start/status/RPC);
|
|
30
|
+
运行时是纯 bash 脚本 + ssh,脚本内零 node;软链经 fnm `aliases/default`
|
|
31
|
+
稳定路径,fnm 升级 node 后重装包即自动跟随,包升级无需重跑 install
|
|
32
|
+
- **npm 包分发**:消费机 `npm i -g ssh-net`,不 clone;个人配置全部在 dotfiles
|
|
33
|
+
托管(`~/.ssh/ssh-net/config` 与 ssh config),包内零个人数据
|
|
34
|
+
|
|
35
|
+
## 安装
|
|
36
|
+
|
|
37
|
+
需要 Node 24+(fnm 的 default 版本,非交互 ssh 经 `~/.zshenv` 同样可用)。
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
npm install -g ssh-net
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
源码安装(仅开发机):
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
git clone git@github.com:adaex/ssh-net.git ~/space/ssh-net
|
|
47
|
+
cd ~/space/ssh-net && npm link
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
安装后按 [docs/migration.md](docs/migration.md) 的分阶段方案在各角色机器上执行
|
|
51
|
+
`ssh-net install`。
|
|
52
|
+
|
|
53
|
+
**卸载顺序**:先 `ssh-net uninstall`(移除 launchd/systemd 作业与软链),
|
|
54
|
+
再 `npm uninstall -g ssh-net`;反过来会留下悬空软链与反复重启的注册作业。
|
|
55
|
+
|
|
56
|
+
## 开发
|
|
57
|
+
|
|
58
|
+
零依赖、零构建,要求 Node 24+:
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
node --test # 全部测试(纯模块 + shell 喂参 + 命令编排)
|
|
62
|
+
npm pack --dry-run # 发布前核对产物(五脚本可执行位、files 清单)
|
|
63
|
+
npm link # 开发机:全局 ssh-net 指向本仓,改脚本即时生效
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
发布物零真实拓扑数据:别名、主机名、环回注册端口只在私有文档与个人配置里,
|
|
67
|
+
测试 fixture 一律 RFC 5737(192.0.2.x)与 `2001:db8::` 文档地址。
|
|
68
|
+
|
|
69
|
+
## 文档
|
|
70
|
+
|
|
71
|
+
| 文件 | 内容 |
|
|
72
|
+
| --- | --- |
|
|
73
|
+
| [docs/design.md](docs/design.md) | 架构、角色、三态路径、组件详细设计、安全模型 |
|
|
74
|
+
| [docs/migration.md](docs/migration.md) | 分阶段迁移方案(一次性计划,Phase 6 验收后删除) |
|
|
75
|
+
| [docs/notes.md](docs/notes.md) | 运维事实、参数依据、实测数据、被否方案 |
|
|
76
|
+
| [CHANGELOG.md](CHANGELOG.md) | 发布记录(随包分发) |
|
|
77
|
+
|
|
78
|
+
## 状态
|
|
79
|
+
|
|
80
|
+
0.1.0 软件就绪、测试全绿(2026-09-29);尚未 publish,各机部署按
|
|
81
|
+
docs/migration.md 的 Phase 2 起执行(Phase 0 现场项仍待真机闸门)。
|
package/libexec/cli.mjs
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// ssh-net 控制面入口:参数路由 + buildContext。业务都在 lib/ 与 commands/,
|
|
3
|
+
// 本文件只做装配、帮助与错误边界。
|
|
4
|
+
import fs from 'node:fs';
|
|
5
|
+
import os from 'node:os';
|
|
6
|
+
import { parseTopology } from './lib/config.mjs';
|
|
7
|
+
import { localHostname, matchIdentity } from './lib/identity.mjs';
|
|
8
|
+
import { machinePaths } from './lib/paths.mjs';
|
|
9
|
+
import { createStateStore } from './lib/state.mjs';
|
|
10
|
+
import { createRunner } from './lib/exec.mjs';
|
|
11
|
+
import pkg from '../package.json' with { type: 'json' };
|
|
12
|
+
|
|
13
|
+
import { run as installRun } from './commands/install.mjs';
|
|
14
|
+
import { run as uninstallRun } from './commands/uninstall.mjs';
|
|
15
|
+
import { run as startRun } from './commands/start.mjs';
|
|
16
|
+
import { run as stopRun } from './commands/stop.mjs';
|
|
17
|
+
import { run as restartRun } from './commands/restart.mjs';
|
|
18
|
+
import { run as statusRun } from './commands/status.mjs';
|
|
19
|
+
import { run as checkinRun } from './commands/checkin.mjs';
|
|
20
|
+
|
|
21
|
+
const COMMANDS = {
|
|
22
|
+
install: { run: installRun, desc: '按本机角色安装(建软链 + 渲染 plist/unit)' },
|
|
23
|
+
uninstall: { run: uninstallRun, desc: '停用并删除全部作业、软链与状态(再 npm uninstall -g)' },
|
|
24
|
+
start: { run: startRun, desc: 'edge 置 on / core 拉起 relay / node 启动' },
|
|
25
|
+
stop: { run: stopRun, desc: 'edge 删 on 并停 link / core 停 relay / node 停服' },
|
|
26
|
+
restart: { run: restartRun, desc: 'kickstart/restart 让循环重来' },
|
|
27
|
+
status: { run: statusRun, desc: '本机角色状态(真实探测)' },
|
|
28
|
+
checkin: { run: checkinRun, desc: 'RPC(仅 core):edge 报名 <别名> <v6>', internal: true },
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
function help() {
|
|
32
|
+
const lines = [
|
|
33
|
+
`ssh-net ${pkg.version} — 个人 SSH 组网`,
|
|
34
|
+
'',
|
|
35
|
+
'用法: ssh-net <命令> [参数]',
|
|
36
|
+
'',
|
|
37
|
+
...Object.entries(COMMANDS)
|
|
38
|
+
.filter(([, c]) => !c.internal)
|
|
39
|
+
.map(([name, c]) => ` ${name.padEnd(10)} ${c.desc}`),
|
|
40
|
+
'',
|
|
41
|
+
'配置: ~/.ssh/ssh-net/config(dotfiles 托管);状态: ~/.local/state/ssh-net/',
|
|
42
|
+
];
|
|
43
|
+
return lines.join('\n');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** 装配本机上下文;config 缺失或主机名不匹配在此硬失败。 */
|
|
47
|
+
function buildContext() {
|
|
48
|
+
const env = process.env;
|
|
49
|
+
const paths = machinePaths(env);
|
|
50
|
+
let text;
|
|
51
|
+
try {
|
|
52
|
+
text = fs.readFileSync(paths.configFile, 'utf8');
|
|
53
|
+
} catch (err) {
|
|
54
|
+
if (err && err.code === 'ENOENT')
|
|
55
|
+
throw new Error(`缺配置 ${paths.configFile}(dotfiles apply ssh 后再 install)`);
|
|
56
|
+
throw err;
|
|
57
|
+
}
|
|
58
|
+
const topo = parseTopology(text);
|
|
59
|
+
const hostname = localHostname(os.hostname());
|
|
60
|
+
const self = matchIdentity(topo, hostname);
|
|
61
|
+
if (!self)
|
|
62
|
+
throw new Error(
|
|
63
|
+
`本机主机名 ${hostname} 不在 topology 的 host 行中(登记:${[...topo.byHostname.keys()].join('、')})`,
|
|
64
|
+
);
|
|
65
|
+
return {
|
|
66
|
+
env,
|
|
67
|
+
paths,
|
|
68
|
+
topo,
|
|
69
|
+
self,
|
|
70
|
+
role: self.role,
|
|
71
|
+
runner: createRunner(env),
|
|
72
|
+
state: createStateStore(paths.stateDir),
|
|
73
|
+
uid: typeof process.getuid === 'function' ? process.getuid() : null,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async function main() {
|
|
78
|
+
const argv = process.argv.slice(2);
|
|
79
|
+
const cmd = argv[0];
|
|
80
|
+
|
|
81
|
+
if (!cmd || cmd === '-h' || cmd === '--help' || cmd === 'help') {
|
|
82
|
+
process.stdout.write(`${help()}\n`);
|
|
83
|
+
return 0;
|
|
84
|
+
}
|
|
85
|
+
if (cmd === '-v' || cmd === '--version' || cmd === 'version') {
|
|
86
|
+
process.stdout.write(`${pkg.version}\n`);
|
|
87
|
+
return 0;
|
|
88
|
+
}
|
|
89
|
+
const entry = COMMANDS[cmd];
|
|
90
|
+
if (!entry) {
|
|
91
|
+
process.stderr.write(`未知命令:${cmd}\n\n${help()}\n`);
|
|
92
|
+
return 2;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const ctx = buildContext();
|
|
96
|
+
const result = await entry.run(ctx, argv.slice(1));
|
|
97
|
+
if (typeof result === 'string') {
|
|
98
|
+
process.stdout.write(`${result}\n`);
|
|
99
|
+
return 0;
|
|
100
|
+
}
|
|
101
|
+
// status 返回 {text, issues}
|
|
102
|
+
if (result && typeof result.text === 'string') {
|
|
103
|
+
process.stdout.write(`${result.text}\n`);
|
|
104
|
+
return result.issues.length > 0 ? 1 : 0;
|
|
105
|
+
}
|
|
106
|
+
return 0;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
main()
|
|
110
|
+
.then((code) => process.exit(code))
|
|
111
|
+
.catch((err) => {
|
|
112
|
+
process.stderr.write(`ssh-net: ${err.message}\n`);
|
|
113
|
+
process.exit(1);
|
|
114
|
+
});
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// checkin RPC:edge 经 `ssh m4-via-relay 'ssh-net checkin <别名> <v6>'` 触发,
|
|
2
|
+
// 仅 core 接受。四态显式处理(design §6.3,实测矩阵见 notes);四态之前先
|
|
3
|
+
// 对账软链与 plist——install 不预建 uplink-edge,其软链与 job 参数(user 等)
|
|
4
|
+
// 变更只有这条更新通道,缺了会永远用旧参数/旧目标拨号、六连败休眠循环。
|
|
5
|
+
// launchctl 真返回成功才退出 0;失败非零,edge 下一轮自然重试。
|
|
6
|
+
import fs from 'node:fs';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import { explainV6Error } from '../lib/ipv6.mjs';
|
|
9
|
+
import * as launchd from '../lib/launchd.mjs';
|
|
10
|
+
import { ensureBinLinks, syncPlist } from '../lib/manage.mjs';
|
|
11
|
+
import {
|
|
12
|
+
edgeLabel, edgeLinkName, plistFileName, renderUplinkEdgePlist,
|
|
13
|
+
} from '../lib/render.mjs';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @param {object} ctx
|
|
17
|
+
* @param {string[]} args [别名, v6]
|
|
18
|
+
* @returns {string} 给 edge 的简短结果
|
|
19
|
+
*/
|
|
20
|
+
export function run(ctx, args) {
|
|
21
|
+
if (ctx.role !== 'core')
|
|
22
|
+
throw new Error(`checkin 仅 core 接受,本机角色是 ${ctx.role}`);
|
|
23
|
+
const [alias, v6] = args;
|
|
24
|
+
if (!alias || !v6) throw new Error('用法:checkin <edge 别名> <v6>');
|
|
25
|
+
|
|
26
|
+
const edge = ctx.topo.edges.find((e) => e.alias === alias);
|
|
27
|
+
if (!edge) throw new Error(`未知 edge 别名:${alias}`);
|
|
28
|
+
const v6Err = explainV6Error(v6);
|
|
29
|
+
if (v6Err) throw new Error(`v6 校验失败(${alias}):${v6Err}`);
|
|
30
|
+
|
|
31
|
+
const label = edgeLabel(alias);
|
|
32
|
+
const plistPath = path.join(
|
|
33
|
+
ctx.paths.launchAgentsDir, plistFileName(label),
|
|
34
|
+
);
|
|
35
|
+
const content = renderUplinkEdgePlist({
|
|
36
|
+
binDir: ctx.paths.binDir, edgeAlias: alias, user: edge.user, logsDir: ctx.paths.logsDir,
|
|
37
|
+
});
|
|
38
|
+
let existing = null;
|
|
39
|
+
try {
|
|
40
|
+
existing = fs.readFileSync(plistPath, 'utf8');
|
|
41
|
+
} catch {
|
|
42
|
+
existing = null; // 未注册(从未落盘)或被手删
|
|
43
|
+
}
|
|
44
|
+
const prevV6 = ctx.state.readEdgeV6(alias);
|
|
45
|
+
const st = launchd.inspect(ctx.runner, ctx.uid, label);
|
|
46
|
+
|
|
47
|
+
// 软链对账无条件先行(幂等):软链被删/悬空/参数变更时,四个状态分支
|
|
48
|
+
// 里的任何一个都可能成为重建通道,不能只靠首次报名那一次
|
|
49
|
+
ensureBinLinks(ctx, [[edgeLinkName(alias), 'ssh-net-uplink-edge']]);
|
|
50
|
+
|
|
51
|
+
// 未注册:首次报名,plist 后 enable + bootstrap
|
|
52
|
+
if (!st.registered) {
|
|
53
|
+
ctx.state.writeEdgeV6(alias, v6);
|
|
54
|
+
const r = syncPlist(ctx, { label, plistPath, content });
|
|
55
|
+
if (r.lintError) throw new Error(`uplink-edge plist 校验失败:${r.lintError}`);
|
|
56
|
+
return `registered ${alias}`;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// 已注册但渲染结果与磁盘不符(topology 的 user 等变了):重新部署。
|
|
60
|
+
// syncPlist 走 bootout→落盘→enable→bootstrap,bootstrap 即首发,无需 kickstart
|
|
61
|
+
if (existing !== content) {
|
|
62
|
+
ctx.state.writeEdgeV6(alias, v6);
|
|
63
|
+
const r = syncPlist(ctx, { label, plistPath, content });
|
|
64
|
+
if (r.lintError) throw new Error(`uplink-edge plist 校验失败:${r.lintError}`);
|
|
65
|
+
return `redeployed ${alias}`;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// 休眠中:写 v6 → enable 清 override → kickstart 唤醒(enable 本身不首发)
|
|
69
|
+
if (st.disabled) {
|
|
70
|
+
ctx.state.writeEdgeV6(alias, v6);
|
|
71
|
+
let r = launchd.enable(ctx.runner, ctx.uid, label);
|
|
72
|
+
if (r.code !== 0) throw new Error(`enable 失败:${r.stderr.trim()}`);
|
|
73
|
+
r = launchd.kickstart(ctx.runner, ctx.uid, label);
|
|
74
|
+
if (r.code !== 0) throw new Error(`kickstart 失败:${r.stderr.trim()}`);
|
|
75
|
+
return `woken ${alias}`;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// enabled + running 且 v6 相同:刻意 no-op(不动进程即不清零 fails)
|
|
79
|
+
if (st.running && prevV6 === v6) return `noop ${alias}`;
|
|
80
|
+
|
|
81
|
+
// enabled 但 v6 变了,或 enabled 未运行:写 v6 → kickstart
|
|
82
|
+
ctx.state.writeEdgeV6(alias, v6);
|
|
83
|
+
const r = launchd.kickstart(ctx.runner, ctx.uid, label);
|
|
84
|
+
if (r.code !== 0) throw new Error(`kickstart 失败:${r.stderr.trim()}`);
|
|
85
|
+
return `kickstarted ${alias}`;
|
|
86
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// install(design §5)。幂等:软链目标正确不重建、plist 字节相同不 bootout。
|
|
2
|
+
// edge 只注册 PathState job(on 不在不启动);core 装完即启 uplink-relay、
|
|
3
|
+
// 不预建 uplink-edge(首次 checkin 才生成);node 写 unit 并 enable --now。
|
|
4
|
+
import fs from 'node:fs';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { buildForwardArgs } from '../lib/config.mjs';
|
|
7
|
+
import { CALLBACK_PORT } from '../lib/constants.mjs';
|
|
8
|
+
import {
|
|
9
|
+
ensureBinLinks, syncPlist, writeFileAtomic,
|
|
10
|
+
} from '../lib/manage.mjs';
|
|
11
|
+
import * as launchd from '../lib/launchd.mjs';
|
|
12
|
+
import * as systemd from '../lib/systemd.mjs';
|
|
13
|
+
import {
|
|
14
|
+
classifyLocalPort, listListeners, peerProbeOk,
|
|
15
|
+
} from '../lib/probe.mjs';
|
|
16
|
+
import {
|
|
17
|
+
linkLabel, linkLinkName,
|
|
18
|
+
relayLabel, relayLinkName, relayUnitName,
|
|
19
|
+
plistFileName,
|
|
20
|
+
renderEdgeLinkPlist, renderRelayPlist, renderRelayUnit,
|
|
21
|
+
} from '../lib/render.mjs';
|
|
22
|
+
|
|
23
|
+
/** edge 端口预检(纯读 + banner 判定),冲突抛错并报持有者。 */
|
|
24
|
+
function precheckEdgePorts(ctx) {
|
|
25
|
+
const { runner, paths, uid, topo } = ctx;
|
|
26
|
+
const jobPid = launchd.inspect(runner, uid, linkLabel(topo.core.alias)).pid;
|
|
27
|
+
|
|
28
|
+
for (const port of topo.forwards.map((f) => f.listenPort)) {
|
|
29
|
+
const c = classifyLocalPort(runner, port, jobPid);
|
|
30
|
+
if (c.kind === 'foreign') {
|
|
31
|
+
const who = runner.run('lsof', ['-nP', `-iTCP:${port}`, '-sTCP:LISTEN']).stdout.trim();
|
|
32
|
+
throw new Error(`端口 ${port} 被非本服务进程占用(pid ${c.pids.join(',')}):\n${who}`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// 10022 持口者永远是本机 sshd,无法按 pid 溯源:读到 SSH banner 才认
|
|
37
|
+
const holders = listListeners(runner, CALLBACK_PORT);
|
|
38
|
+
if (holders.length > 0 && !peerProbeOk(runner, paths.binDir)) {
|
|
39
|
+
const who = runner.run('lsof', ['-nP', `-iTCP:${CALLBACK_PORT}`, '-sTCP:LISTEN']).stdout.trim();
|
|
40
|
+
throw new Error(
|
|
41
|
+
`端口 ${CALLBACK_PORT} 在听但读不到 SSH banner(僵尸监听或他进程占用):\n${who}`,
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function installEdge(ctx) {
|
|
47
|
+
const { topo, paths } = ctx;
|
|
48
|
+
// 预检纯读且在落任何产物之前:被拒时 bin/ 与软链一个都不建
|
|
49
|
+
precheckEdgePorts(ctx);
|
|
50
|
+
ensureBinLinks(ctx, [
|
|
51
|
+
[linkLinkName(topo.core.alias), 'ssh-net-link-core'],
|
|
52
|
+
['peer-probe', 'peer-probe'],
|
|
53
|
+
['v6-address', 'v6-address'],
|
|
54
|
+
]);
|
|
55
|
+
const content = renderEdgeLinkPlist({
|
|
56
|
+
binDir: paths.binDir,
|
|
57
|
+
coreAlias: topo.core.alias,
|
|
58
|
+
selfAlias: ctx.self.alias,
|
|
59
|
+
forwardArgs: buildForwardArgs(topo.forwards),
|
|
60
|
+
onFile: paths.onFile,
|
|
61
|
+
logsDir: paths.logsDir,
|
|
62
|
+
});
|
|
63
|
+
const r = syncPlist(ctx, {
|
|
64
|
+
label: linkLabel(topo.core.alias),
|
|
65
|
+
plistPath: path.join(paths.launchAgentsDir, plistFileName(linkLabel(topo.core.alias))),
|
|
66
|
+
content,
|
|
67
|
+
});
|
|
68
|
+
return `edge link 已${r.unchanged ? '就位(未改动)' : '注册'},on 不在不启动`;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function installCore(ctx) {
|
|
72
|
+
const { topo, paths } = ctx;
|
|
73
|
+
ensureBinLinks(ctx, [
|
|
74
|
+
[relayLinkName(topo.relay.alias), 'ssh-net-uplink-relay'],
|
|
75
|
+
]);
|
|
76
|
+
const label = relayLabel(topo.relay.alias);
|
|
77
|
+
const content = renderRelayPlist({
|
|
78
|
+
binDir: paths.binDir,
|
|
79
|
+
relayAlias: topo.relay.alias,
|
|
80
|
+
relayPort: topo.core.relayPort,
|
|
81
|
+
logsDir: paths.logsDir,
|
|
82
|
+
});
|
|
83
|
+
const r = syncPlist(ctx, {
|
|
84
|
+
label,
|
|
85
|
+
plistPath: path.join(paths.launchAgentsDir, plistFileName(label)),
|
|
86
|
+
content,
|
|
87
|
+
});
|
|
88
|
+
return `core uplink-relay ${r.unchanged ? '已在跑(未改动)' : '已启动'}`;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function installNode(ctx) {
|
|
92
|
+
const { topo, paths, runner } = ctx;
|
|
93
|
+
if (ctx.uid !== 0)
|
|
94
|
+
throw new Error('node 安装写 /etc/systemd/system 需要 root,请以 root 运行');
|
|
95
|
+
ensureBinLinks(ctx, [
|
|
96
|
+
[relayLinkName(topo.relay.alias), 'ssh-net-uplink-relay'],
|
|
97
|
+
]);
|
|
98
|
+
const unitName = relayUnitName(topo.relay.alias);
|
|
99
|
+
const unitPath = path.join(paths.systemdUnitDir, unitName);
|
|
100
|
+
const content = renderRelayUnit({
|
|
101
|
+
binDir: paths.binDir,
|
|
102
|
+
relayAlias: topo.relay.alias,
|
|
103
|
+
relayPort: ctx.self.relayPort,
|
|
104
|
+
home: ctx.env.HOME ?? '/root',
|
|
105
|
+
});
|
|
106
|
+
fs.mkdirSync(paths.systemdUnitDir, { recursive: true });
|
|
107
|
+
writeFileAtomic(unitPath, content, 0o644);
|
|
108
|
+
const verify = systemd.verifyUnit(runner, unitPath);
|
|
109
|
+
if (!verify.ok && !verify.skipped)
|
|
110
|
+
throw new Error(`systemd-analyze verify 失败:\n${verify.stderr}`);
|
|
111
|
+
for (const r of [
|
|
112
|
+
systemd.daemonReload(runner),
|
|
113
|
+
systemd.enableUnit(runner, unitName),
|
|
114
|
+
systemd.restartUnit(runner, unitName),
|
|
115
|
+
]) {
|
|
116
|
+
if (r.code !== 0) throw new Error(`systemctl 失败:${r.stderr.trim()}`);
|
|
117
|
+
}
|
|
118
|
+
return `node uplink-relay unit 已启用并启动:${unitName}`;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** @param {object} ctx */
|
|
122
|
+
export function run(ctx) {
|
|
123
|
+
switch (ctx.role) {
|
|
124
|
+
case 'edge': return installEdge(ctx);
|
|
125
|
+
case 'core': return installCore(ctx);
|
|
126
|
+
case 'node': return installNode(ctx);
|
|
127
|
+
default: throw new Error(`未知角色:${ctx.role}`);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// restart(design §5):edge on 不在则拒绝,在则 kickstart -k 让循环重来;
|
|
2
|
+
// core kickstart uplink-relay;node systemctl restart。
|
|
3
|
+
import * as launchd from '../lib/launchd.mjs';
|
|
4
|
+
import * as systemd from '../lib/systemd.mjs';
|
|
5
|
+
import { linkLabel, relayLabel, relayUnitName } from '../lib/render.mjs';
|
|
6
|
+
|
|
7
|
+
export function run(ctx) {
|
|
8
|
+
if (ctx.role === 'edge') {
|
|
9
|
+
if (!ctx.state.isOn()) throw new Error('on 不在(已 stop);请先 ssh-net start');
|
|
10
|
+
const label = linkLabel(ctx.topo.core.alias);
|
|
11
|
+
// kickstartEnabled:与 core 同规则,残留 disabled override 时不被拒
|
|
12
|
+
const r = launchd.kickstartEnabled(ctx.runner, ctx.uid, label);
|
|
13
|
+
if (r.code !== 0) throw new Error(`kickstart 失败:${r.stderr.trim()}`);
|
|
14
|
+
return 'edge link 已 kickstart(循环重来)';
|
|
15
|
+
}
|
|
16
|
+
if (ctx.role === 'core') {
|
|
17
|
+
const label = relayLabel(ctx.topo.relay.alias);
|
|
18
|
+
if (!launchd.inspect(ctx.runner, ctx.uid, label).registered)
|
|
19
|
+
throw new Error('uplink-relay 未注册(已 stop?先 ssh-net start)');
|
|
20
|
+
const r = launchd.kickstartEnabled(ctx.runner, ctx.uid, label);
|
|
21
|
+
if (r.code !== 0) throw new Error(`kickstart 失败:${r.stderr.trim()}`);
|
|
22
|
+
return 'core uplink-relay 已 kickstart';
|
|
23
|
+
}
|
|
24
|
+
if (ctx.role === 'node') {
|
|
25
|
+
const r = systemd.restartUnit(ctx.runner, relayUnitName(ctx.topo.relay.alias));
|
|
26
|
+
if (r.code !== 0) throw new Error(`systemctl restart 失败:${r.stderr.trim()}`);
|
|
27
|
+
return 'node uplink-relay 已 restart';
|
|
28
|
+
}
|
|
29
|
+
throw new Error(`未知角色:${ctx.role}`);
|
|
30
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// start(design §5):edge touch on(PathState 同秒拉起);core 重新
|
|
2
|
+
// enable+bootstrap uplink-relay(stop 只 bootout 不删 plist);node systemctl。
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import * as launchd from '../lib/launchd.mjs';
|
|
6
|
+
import * as systemd from '../lib/systemd.mjs';
|
|
7
|
+
import { linkLabel, relayLabel, relayUnitName, plistFileName } from '../lib/render.mjs';
|
|
8
|
+
|
|
9
|
+
function startCore(ctx) {
|
|
10
|
+
const label = relayLabel(ctx.topo.relay.alias);
|
|
11
|
+
const plist = path.join(ctx.paths.launchAgentsDir, plistFileName(label));
|
|
12
|
+
if (!fs.existsSync(plist)) throw new Error(`缺少 ${plist},请先 ssh-net install`);
|
|
13
|
+
const st = launchd.inspect(ctx.runner, ctx.uid, label);
|
|
14
|
+
if (!st.registered) {
|
|
15
|
+
launchd.enable(ctx.runner, ctx.uid, label);
|
|
16
|
+
const r = launchd.bootstrap(ctx.runner, ctx.uid, plist);
|
|
17
|
+
if (r.code !== 0) throw new Error(`bootstrap 失败:${r.stderr.trim()}`);
|
|
18
|
+
return 'core uplink-relay 已拉起';
|
|
19
|
+
}
|
|
20
|
+
if (!st.running) {
|
|
21
|
+
// kickstartEnabled:kickstart 对 disabled job 被拒(stop 中途失败/手工
|
|
22
|
+
// disable 的残留态),先幂等 enable
|
|
23
|
+
const r = launchd.kickstartEnabled(ctx.runner, ctx.uid, label);
|
|
24
|
+
if (r.code !== 0) throw new Error(`kickstart 失败:${r.stderr.trim()}`);
|
|
25
|
+
return 'core uplink-relay 已 kickstart';
|
|
26
|
+
}
|
|
27
|
+
if (st.disabled) launchd.enable(ctx.runner, ctx.uid, label); // 在跑但 override 残留:清掉防退出后不重拉
|
|
28
|
+
return 'core uplink-relay 已在运行';
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function run(ctx) {
|
|
32
|
+
if (ctx.role === 'edge') {
|
|
33
|
+
const label = linkLabel(ctx.topo.core.alias);
|
|
34
|
+
if (!launchd.inspect(ctx.runner, ctx.uid, label).registered)
|
|
35
|
+
throw new Error(`link job 未注册(先 ssh-net install):launchctl print gui/${ctx.uid}/${label}`);
|
|
36
|
+
ctx.state.setOn();
|
|
37
|
+
return 'on 已置,link 由 launchd PathState 拉起';
|
|
38
|
+
}
|
|
39
|
+
if (ctx.role === 'core') return startCore(ctx);
|
|
40
|
+
if (ctx.role === 'node') {
|
|
41
|
+
const r = systemd.startUnit(ctx.runner, relayUnitName(ctx.topo.relay.alias));
|
|
42
|
+
if (r.code !== 0) throw new Error(`systemctl start 失败:${r.stderr.trim()}`);
|
|
43
|
+
return 'node uplink-relay 已启动';
|
|
44
|
+
}
|
|
45
|
+
throw new Error(`未知角色:${ctx.role}`);
|
|
46
|
+
}
|