mihomo-cli 3.12.1 → 4.0.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 +26 -0
- package/README.md +6 -63
- package/dist/index.js +84 -739
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,31 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [4.0.0] - 2026-09-05
|
|
4
|
+
|
|
5
|
+
移除 ssh 隧道功能。
|
|
6
|
+
|
|
7
|
+
### 破坏性变更
|
|
8
|
+
|
|
9
|
+
- **移除 `ssh` 命令及全部相关能力**。删除项:`mihomo ssh`(`add`/`up`/`down`/`status`/`rm`)、`start` 与 `stop` 的 `--no-ssh` 选项、`reset` 的 `ssh` 目标、settings 的 `ssh` 字段、数据目录的 `ssh/`。
|
|
10
|
+
|
|
11
|
+
演进轨迹:v3.9.0 `tunnel` 统一为 `ssh` → v3.12.0 剥离配置层弱化为「只管端口」→ 本版整体移除。功能价值撑不起维护面——既要防 `--host` 的 `-oProxyCommand=` 注入、又要真实探测端口识别「假活」、还要维护 `started_by` 的 auto/manual 单向提升语义以免 `stop` 误杀,而它做的事等价于用户自己跑一条 `ssh -D 127.0.0.1:1080 -N host`。
|
|
12
|
+
|
|
13
|
+
**升级须知**:自己起 `ssh -D`(可用 `~/.ssh/config` 的 `LocalForward` 或系统 launchd 托管),节点与分流规则照旧写在 `overwrite.yaml` 里,无需改动:
|
|
14
|
+
|
|
15
|
+
```yaml
|
|
16
|
+
~proxies:
|
|
17
|
+
- {name: SSH-work, type: socks5, server: 127.0.0.1, port: 1080}
|
|
18
|
+
+rules:
|
|
19
|
+
- DOMAIN-SUFFIX,example.internal,SSH-work
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
老的 `settings.json` 里会留着 `ssh` 键、数据目录会留着 `ssh/` 与 `logs/ssh-*.log`。**不自动清理**:未知键本就被忽略,孤儿目录不影响任何行为,自动删用户数据的风险大于收益。想清干净就手动 `rm -rf ~/.mihomo-cli/ssh`。
|
|
23
|
+
|
|
24
|
+
### 内部
|
|
25
|
+
|
|
26
|
+
- 连带移除已无消费者的 `parseStringArg`(`--host`/`--port` 是仅有的两个调用方)与 `ResetTarget.onBefore` 钩子
|
|
27
|
+
- `readSettingsList` 泛型包装内联回 `getSubscriptions`(订阅是唯一剩余的列表字段)
|
|
28
|
+
|
|
3
29
|
## [3.12.1] - 2026-09-05
|
|
4
30
|
|
|
5
31
|
并发数据丢失与信号响应两处稳定性修复。
|
package/README.md
CHANGED
|
@@ -13,7 +13,6 @@
|
|
|
13
13
|
- 🔄 **智能重启** - `sub use` 切换订阅、`ow on/off` 切换覆写后自动重启
|
|
14
14
|
- 🚀 **进程管理** - 启动/停止/切换模式,自动清理残留进程
|
|
15
15
|
- 🛡️ **进程保活** - 基于 launchd(root),崩溃/开机自动拉起,代理后台常驻(`daemon on`)
|
|
16
|
-
- 🔌 **ssh 隧道出口** - 管理 `ssh -D` 进程生命周期,把内网机器变成本地 SOCKS5 端口,随 `start` 一并拉起
|
|
17
16
|
- 🔄 **双模式支持** - Mixed 模式和 TUN 透明代理模式
|
|
18
17
|
- 📊 **状态监控** - 查看运行状态、内存占用、订阅流量与到期时间
|
|
19
18
|
- 📝 **日志管理** - 实时日志 + 历史日志归档(自动轮转,保留7天)
|
|
@@ -87,8 +86,8 @@ mihomo ui yacd # YACD
|
|
|
87
86
|
|
|
88
87
|
| 命令 | 说明 |
|
|
89
88
|
| --------------------------- | ---------------------------------------------------------------------------- |
|
|
90
|
-
| `mihomo start [tun\|mixed]` | 启动/重启/切换代理模式(`-s` 跳过订阅更新,`-u`
|
|
91
|
-
| `mihomo stop` |
|
|
89
|
+
| `mihomo start [tun\|mixed]` | 启动/重启/切换代理模式(`-s` 跳过订阅更新,`-u` 更新超时) |
|
|
90
|
+
| `mihomo stop` | 停止代理 |
|
|
92
91
|
| `mihomo status` | 查看运行状态(含订阅流量、到期时间) |
|
|
93
92
|
| `mihomo logs` | 列出所有日志(当前 + 历史归档) |
|
|
94
93
|
| `mihomo logs <编号>` | 查看指定日志(`0`=当前,`1+`=归档,`-f` 实时跟随,`-n N` 行数,`-o` 打开) |
|
|
@@ -122,16 +121,11 @@ mihomo ui yacd # YACD
|
|
|
122
121
|
| --------------------------------- | ------------------------------------------------------------------- |
|
|
123
122
|
| `mihomo kernel [--mirror [镜像]]` | 更新内核(默认直连,`--mirror` 使用镜像;更新后运行中实例需重启生效) |
|
|
124
123
|
| `mihomo daemon [on\|off]` | 进程保活:开机自启 + 崩溃自动重启(仅 Mixed 模式,on/off 需管理员密码,无参看状态) |
|
|
125
|
-
| `mihomo ssh` | 列出 ssh 隧道及真实状态 |
|
|
126
|
-
| `mihomo ssh add <名字> --host <主机> --port <端口> [--no-auto]` | 添加隧道(默认随 start 拉起) |
|
|
127
|
-
| `mihomo ssh up\|down [名字]` | 启动/停止隧道(无参即全部) |
|
|
128
|
-
| `mihomo ssh status [名字]` | 查看隧道状态(真实探测端口,能识别「假活」) |
|
|
129
|
-
| `mihomo ssh rm <名字> [-y]` | 删除隧道 |
|
|
130
124
|
| `mihomo update` | 更新 mihomo-cli(先查 npm 最新版,已是最新则跳过重装) |
|
|
131
125
|
| `mihomo ui [zash\|dash\|yacd]` | 打开 Web UI |
|
|
132
126
|
| `mihomo dir` | 显示数据目录位置 |
|
|
133
127
|
| `mihomo dir open [target]` | 打开指定目录(`root`, `subs`, `logs`, `data`, `runtime`, `kernel`) |
|
|
134
|
-
| `mihomo reset [目标...] [--full] [-y]` | 重置用户数据(可用目标:`subs`, `logs`, `data`, `runtime`, `settings`, `kernel`, `overwrites`, `daemon
|
|
128
|
+
| `mihomo reset [目标...] [--full] [-y]` | 重置用户数据(可用目标:`subs`, `logs`, `data`, `runtime`, `settings`, `kernel`, `overwrites`, `daemon`;`--full` 删全部,`-y` 跳过确认) |
|
|
135
129
|
| `mihomo version` | 显示版本信息 |
|
|
136
130
|
| `mihomo help` | 显示帮助信息 |
|
|
137
131
|
|
|
@@ -144,7 +138,7 @@ mihomo ui yacd # YACD
|
|
|
144
138
|
- `mhm`
|
|
145
139
|
- `mh`
|
|
146
140
|
|
|
147
|
-
子命令组亦有别名:`subscription` = `sub`/`subs`/`subscriptions`,`directory` = `dir`/`dirs`/`directories`,`overwrite` = `ow
|
|
141
|
+
子命令组亦有别名:`subscription` = `sub`/`subs`/`subscriptions`,`directory` = `dir`/`dirs`/`directories`,`overwrite` = `ow`
|
|
148
142
|
|
|
149
143
|
### 快捷命令
|
|
150
144
|
|
|
@@ -170,54 +164,6 @@ mihomo ui yacd # YACD
|
|
|
170
164
|
- 需要 sudo / 管理员权限
|
|
171
165
|
- 首次使用会自动配置 DNS 和路由
|
|
172
166
|
|
|
173
|
-
## ssh 隧道出口
|
|
174
|
-
|
|
175
|
-
把一台可 ssh 登录的机器(如公司内网的机器)变成本地 SOCKS5 出口,配合分流规则即可只让内网域名走它,其余流量照常走订阅节点。本功能**只负责 ssh 进程的生命周期**——起一个端口、告诉你它是死是活,不碰任何配置文件。
|
|
176
|
-
|
|
177
|
-
```bash
|
|
178
|
-
mihomo ssh add work --host m4 --port 1080 # m4 是 ~/.ssh/config 里的别名
|
|
179
|
-
mihomo ssh up work # 启动
|
|
180
|
-
mihomo ssh status # 查看状态(真实探测端口)
|
|
181
|
-
mihomo ssh down work # 停止
|
|
182
|
-
```
|
|
183
|
-
|
|
184
|
-
隧道起来之后,要不要接进分流、怎么接,由你写进 `overwrite.yaml`(`ssh add` 会把片段打印出来,复制即可):
|
|
185
|
-
|
|
186
|
-
```yaml
|
|
187
|
-
~proxies:
|
|
188
|
-
- {name: SSH-work, type: socks5, server: 127.0.0.1, port: 1080}
|
|
189
|
-
+rules:
|
|
190
|
-
- DOMAIN-SUFFIX,example.internal,SSH-work
|
|
191
|
-
- IP-CIDR,10.0.0.0/8,SSH-work
|
|
192
|
-
```
|
|
193
|
-
|
|
194
|
-
改完执行 `mihomo start` 生效。端口以 `mihomo ssh` 显示的为准——改端口时记得同步改这里,CLI 不会代改你的覆写文件。
|
|
195
|
-
|
|
196
|
-
### 与 start / stop 的联动
|
|
197
|
-
|
|
198
|
-
隧道默认带 `auto` 标记,`mihomo start` 会顺带拉起、`mihomo stop` 会连带停止(`--no-ssh` 可跳过;`add` 时加 `--no-auto` 则不参与)。
|
|
199
|
-
|
|
200
|
-
- **隧道起不来不会让 `start` 失败**——它只影响内网分流那部分规则,其余流量正常,但会打印显眼的黄色警告并附上 ssh 给出的原因
|
|
201
|
-
- **`stop` 只停自己起的**:手动 `ssh up` 起的隧道不会被 `mihomo stop` 带走,避免下次 start 又起一个而累积僵尸进程
|
|
202
|
-
|
|
203
|
-
### 状态的三种形态
|
|
204
|
-
|
|
205
|
-
`ssh status` 会**真实探测端口是否在监听**,而不是只看进程在不在:
|
|
206
|
-
|
|
207
|
-
| 状态 | 含义 |
|
|
208
|
-
| --- | --- |
|
|
209
|
-
| 运行中 | 进程在且端口在监听,可正常使用 |
|
|
210
|
-
| **假活** | 进程还在但端口不通——最需要警惕的形态,此时 mihomo 仍在往死端口送流量 |
|
|
211
|
-
| 未运行 | 进程不在 |
|
|
212
|
-
|
|
213
|
-
### 安全边界
|
|
214
|
-
|
|
215
|
-
- `-D` **恒绑 `127.0.0.1`**,不提供绑定地址开关:绑 `0.0.0.0` 会让同一 WiFi 下任何设备都能经本机进入内网
|
|
216
|
-
- ssh 参数固定带 `ExitOnForwardFailure`/`BatchMode`/`ConnectTimeout`/`ServerAlive*`,分别防「假活」、无 TTY 挂死、久等、断线后端口成僵尸
|
|
217
|
-
- `--host` 拒绝以 `-` 开头的值(`-oProxyCommand=...` 会被 ssh 当选项解析,等同任意命令执行)
|
|
218
|
-
|
|
219
|
-
> 暂不做自动重连保活。断线依靠 `ServerAliveInterval` 让 ssh 进程自行退出,再用 `ssh status` 查出来。
|
|
220
|
-
|
|
221
167
|
## 进程保活
|
|
222
168
|
|
|
223
169
|
默认情况下,mihomo 内核在后台独立运行,但如果内核崩溃、被系统 kill(如内存不足)、或重启/重新登录后,代理就会失效且不会自动恢复。进程保活用 macOS 原生的 **launchd** 解决这个问题。
|
|
@@ -297,7 +243,7 @@ mihomo start --update-timeout 30000 # 长选项 + 空格
|
|
|
297
243
|
mihomo start --update-timeout=30000 # 长选项 + 等号
|
|
298
244
|
```
|
|
299
245
|
|
|
300
|
-
布尔开关:`-s`(跳过订阅更新)、`--no-update
|
|
246
|
+
布尔开关:`-s`(跳过订阅更新)、`--no-update`、`-y`/`--yes`(跳过确认)、`-o`(用系统默认程序打开)。
|
|
301
247
|
|
|
302
248
|
上述数值选项只接受 **>= 1 的整数**,非法值(`0`、负数、`5s`、`abc`)会直接报错而非静默取默认值——避免静默产出看似成功的错误结果。
|
|
303
249
|
|
|
@@ -324,11 +270,8 @@ mihomo start --update-timeout=30000 # 长选项 + 等号
|
|
|
324
270
|
│ └── mihomo # mihomo 内核二进制
|
|
325
271
|
├── logs/
|
|
326
272
|
│ ├── mihomo.log # 当前日志
|
|
327
|
-
│
|
|
328
|
-
│ └── ssh-<name>.log # 隧道 ssh 输出(每次启动覆写)
|
|
273
|
+
│ └── mihomo.YYYY-MM-DD_HH-MM-SS.log # 归档日志
|
|
329
274
|
├── data/ # mihomo 运行数据(GeoIP 等,由内核自行管理)
|
|
330
|
-
├── ssh/ # 隧道运行态(stop 不清除,故不放在 runtime/)
|
|
331
|
-
│ └── <name>.json # PID、谁启动的、启动时间
|
|
332
275
|
└── runtime/ # 运行时临时文件(stop 自动清除)
|
|
333
276
|
├── pid # 进程 PID
|
|
334
277
|
├── config.yaml # 运行时生成的配置
|
package/dist/index.js
CHANGED
|
@@ -3167,11 +3167,7 @@ var DIRS = {
|
|
|
3167
3167
|
subscriptions: path.join(USER_DATA_DIR, "subscriptions"),
|
|
3168
3168
|
logs: path.join(USER_DATA_DIR, "logs"),
|
|
3169
3169
|
data: path.join(USER_DATA_DIR, "data"),
|
|
3170
|
-
runtime: path.join(USER_DATA_DIR, "runtime")
|
|
3171
|
-
// ssh 隧道运行态。刻意独立于 runtime/:clearRuntime() 会在 stop() 成功路径 rmrf 整个
|
|
3172
|
-
// runtime 目录,隧道状态放那里会被 `mihomo stop` 连同 config.yaml 一起抹掉,
|
|
3173
|
-
// 于是「谁起的」标记丢失、手动起的隧道再也无法被识别
|
|
3174
|
-
ssh: path.join(USER_DATA_DIR, "ssh")
|
|
3170
|
+
runtime: path.join(USER_DATA_DIR, "runtime")
|
|
3175
3171
|
};
|
|
3176
3172
|
var PATHS = {
|
|
3177
3173
|
mihomoBinary: path.join(DIRS.kernel, "mihomo"),
|
|
@@ -3393,21 +3389,18 @@ function deleteSubscriptionCache(subName) {
|
|
|
3393
3389
|
writeSubscriptionCache(cache);
|
|
3394
3390
|
});
|
|
3395
3391
|
}
|
|
3396
|
-
function
|
|
3397
|
-
|
|
3392
|
+
function isValidSubscription(s) {
|
|
3393
|
+
return s != null && typeof s === "object" && typeof s.name === "string" && typeof s.url === "string";
|
|
3394
|
+
}
|
|
3395
|
+
function getSubscriptions() {
|
|
3396
|
+
const list = readSettings().subscriptions;
|
|
3398
3397
|
if (!Array.isArray(list)) {
|
|
3399
3398
|
if (list !== void 0) {
|
|
3400
|
-
console.warn(
|
|
3399
|
+
console.warn("\u8B66\u544A: settings.json \u7684 subscriptions \u4E0D\u662F\u5217\u8868\uFF0C\u5DF2\u5FFD\u7565\uFF08\u53EF\u7528 mihomo sub add \u91CD\u65B0\u6DFB\u52A0\uFF09");
|
|
3401
3400
|
}
|
|
3402
3401
|
return [];
|
|
3403
3402
|
}
|
|
3404
|
-
return list.filter(
|
|
3405
|
-
}
|
|
3406
|
-
function isValidSubscription(s) {
|
|
3407
|
-
return s != null && typeof s === "object" && typeof s.name === "string" && typeof s.url === "string";
|
|
3408
|
-
}
|
|
3409
|
-
function getSubscriptions() {
|
|
3410
|
-
return readSettingsList("subscriptions", isValidSubscription, "\u53EF\u7528 mihomo sub add \u91CD\u65B0\u6DFB\u52A0");
|
|
3403
|
+
return list.filter(isValidSubscription);
|
|
3411
3404
|
}
|
|
3412
3405
|
function getSubscriptionsWithCache() {
|
|
3413
3406
|
const subs = getSubscriptions();
|
|
@@ -3418,17 +3411,6 @@ function getSubscriptionsWithCache() {
|
|
|
3418
3411
|
}));
|
|
3419
3412
|
}
|
|
3420
3413
|
var SAFE_NAME_RE = /^[\w\-\p{Unified_Ideograph}]{1,64}$/u;
|
|
3421
|
-
function isValidSshConfig(t) {
|
|
3422
|
-
return t != null && typeof t === "object" && typeof t.name === "string" && typeof t.host === "string" && Number.isInteger(t.port);
|
|
3423
|
-
}
|
|
3424
|
-
function getSshTunnels() {
|
|
3425
|
-
return readSettingsList("ssh", isValidSshConfig, "\u53EF\u7528 mihomo ssh add \u91CD\u65B0\u6DFB\u52A0");
|
|
3426
|
-
}
|
|
3427
|
-
function validateSshName(name) {
|
|
3428
|
-
if (!name || !SAFE_NAME_RE.test(name)) {
|
|
3429
|
-
throw new CliError(`\u96A7\u9053\u540D\u79F0\u65E0\u6548: "${name}"\uFF0C\u53EA\u5141\u8BB8\u5B57\u6BCD\u3001\u6570\u5B57\u3001\u4E0B\u5212\u7EBF\u3001\u77ED\u6A2A\u7EBF\u548C\u4E2D\u6587\uFF08\u6700\u957F 64 \u5B57\u7B26\uFF09`);
|
|
3430
|
-
}
|
|
3431
|
-
}
|
|
3432
3414
|
function validateSubscriptionName(name) {
|
|
3433
3415
|
if (!name || !SAFE_NAME_RE.test(name)) {
|
|
3434
3416
|
throw new CliError(`\u8BA2\u9605\u540D\u79F0\u65E0\u6548: "${name}"\uFF0C\u53EA\u5141\u8BB8\u5B57\u6BCD\u3001\u6570\u5B57\u3001\u4E0B\u5212\u7EBF\u3001\u77ED\u6A2A\u7EBF\u548C\u4E2D\u6587\uFF08\u6700\u957F 64 \u5B57\u7B26\uFF09`);
|
|
@@ -3824,27 +3806,21 @@ function parseIntArg(args, short, long, defaultValue) {
|
|
|
3824
3806
|
}
|
|
3825
3807
|
return defaultValue;
|
|
3826
3808
|
}
|
|
3827
|
-
function
|
|
3828
|
-
if (!args) return
|
|
3829
|
-
|
|
3830
|
-
|
|
3831
|
-
|
|
3832
|
-
|
|
3833
|
-
|
|
3834
|
-
|
|
3835
|
-
|
|
3836
|
-
|
|
3837
|
-
const value = args[i].slice(long.length + 1);
|
|
3838
|
-
if (!value) throw new CliError(`\u9009\u9879 ${long} \u7F3A\u5C11\u503C`, { hint: [`\u4F8B\u5982: ${long}=<\u503C>`] });
|
|
3839
|
-
return value;
|
|
3840
|
-
}
|
|
3841
|
-
}
|
|
3842
|
-
return null;
|
|
3809
|
+
function assertNoRemovedSshFlag(args) {
|
|
3810
|
+
if (!args?.some((a) => a === "--no-ssh" || a.startsWith("--no-ssh="))) return;
|
|
3811
|
+
throw new CliError("--no-ssh \u5DF2\u79FB\u9664\uFF08v4.0.0\uFF09", {
|
|
3812
|
+
label: "\u53C2\u6570\u9519\u8BEF",
|
|
3813
|
+
hint: [
|
|
3814
|
+
"ssh \u96A7\u9053\u529F\u80FD\u5DF2\u6574\u4F53\u79FB\u9664\uFF0C\u8BE5\u9009\u9879\u4E0D\u518D\u6709\u5BF9\u5E94\u884C\u4E3A\u3002",
|
|
3815
|
+
"\u5982\u4ECD\u9700\u5185\u7F51\u51FA\u53E3\uFF1A\u81EA\u884C\u8FD0\u884C ssh -D 127.0.0.1:<\u7AEF\u53E3> -N <\u4E3B\u673A>\uFF0C",
|
|
3816
|
+
"\u8282\u70B9\u4E0E\u5206\u6D41\u89C4\u5219\u5199\u5728 overwrite.yaml \u91CC\uFF08\u5199\u6CD5\u89C1 CHANGELOG \u7684 4.0.0 \u5347\u7EA7\u987B\u77E5\uFF09\u3002"
|
|
3817
|
+
]
|
|
3818
|
+
});
|
|
3843
3819
|
}
|
|
3844
|
-
var VALUE_FLAGS = /* @__PURE__ */ new Set(["-n", "--lines", "-u", "--update-timeout"
|
|
3820
|
+
var VALUE_FLAGS = /* @__PURE__ */ new Set(["-n", "--lines", "-u", "--update-timeout"]);
|
|
3845
3821
|
function extractStartOptions(args) {
|
|
3846
3822
|
if (!args) return [];
|
|
3847
|
-
const BOOL_FLAGS = /* @__PURE__ */ new Set(["-s", "--no-update"
|
|
3823
|
+
const BOOL_FLAGS = /* @__PURE__ */ new Set(["-s", "--no-update"]);
|
|
3848
3824
|
const out = [];
|
|
3849
3825
|
for (let i = 0; i < args.length; i++) {
|
|
3850
3826
|
const a = args[i];
|
|
@@ -4347,7 +4323,6 @@ ${colors.cyan(colors.bold(`mihomo-cli v${VERSION}`))}`, "", "\u547D\u4EE4\u522B\
|
|
|
4347
4323
|
" mihomo start -s # \u8DF3\u8FC7\u81EA\u52A8\u66F4\u65B0\u8BA2\u9605",
|
|
4348
4324
|
" mihomo start -u 30000 # \u81EA\u52A8\u66F4\u65B0\u8D85\u65F6 30 \u79D2 (\u9ED8\u8BA4 10s)",
|
|
4349
4325
|
" mihomo daemon on # \u5F00\u542F\u4FDD\u6D3B\uFF08\u5F00\u673A\u81EA\u542F + \u5D29\u6E83\u91CD\u542F\uFF09",
|
|
4350
|
-
" mihomo ssh add work --host m4 --port 1080 # \u52A0 ssh \u96A7\u9053\u51FA\u53E3",
|
|
4351
4326
|
" mihomo sub add <url> # \u6DFB\u52A0\u8BA2\u9605 (sub \u662F subscription \u522B\u540D)",
|
|
4352
4327
|
" mihomo ui # \u6253\u5F00 Web UI",
|
|
4353
4328
|
"",
|
|
@@ -5432,363 +5407,6 @@ async function launchOrRestart(mode) {
|
|
|
5432
5407
|
return result.pid;
|
|
5433
5408
|
}
|
|
5434
5409
|
|
|
5435
|
-
// src/ssh.ts
|
|
5436
|
-
import { spawn as spawn2 } from "child_process";
|
|
5437
|
-
import fs11 from "fs";
|
|
5438
|
-
import net from "net";
|
|
5439
|
-
import path7 from "path";
|
|
5440
|
-
|
|
5441
|
-
// src/lifecycle.ts
|
|
5442
|
-
var cleanupFns = /* @__PURE__ */ new Set();
|
|
5443
|
-
var silentSigint = false;
|
|
5444
|
-
function setSilentSigint(value) {
|
|
5445
|
-
silentSigint = value;
|
|
5446
|
-
}
|
|
5447
|
-
function isSilentSigint() {
|
|
5448
|
-
return silentSigint;
|
|
5449
|
-
}
|
|
5450
|
-
function registerCleanup(fn) {
|
|
5451
|
-
cleanupFns.add(fn);
|
|
5452
|
-
return () => {
|
|
5453
|
-
cleanupFns.delete(fn);
|
|
5454
|
-
};
|
|
5455
|
-
}
|
|
5456
|
-
function runCleanup() {
|
|
5457
|
-
for (const fn of cleanupFns) {
|
|
5458
|
-
try {
|
|
5459
|
-
fn();
|
|
5460
|
-
} catch {
|
|
5461
|
-
}
|
|
5462
|
-
}
|
|
5463
|
-
cleanupFns.clear();
|
|
5464
|
-
}
|
|
5465
|
-
|
|
5466
|
-
// src/ssh.ts
|
|
5467
|
-
var PORT_PROBE_TIMEOUT_MS = 300;
|
|
5468
|
-
var STOP_WAIT_ATTEMPTS = 20;
|
|
5469
|
-
var STOP_WAIT_INTERVAL = 100;
|
|
5470
|
-
var START_WAIT_ATTEMPTS = 40;
|
|
5471
|
-
var START_WAIT_INTERVAL = 500;
|
|
5472
|
-
function getReservedPorts() {
|
|
5473
|
-
const reserved = /* @__PURE__ */ new Map();
|
|
5474
|
-
const mixedPort = Number(BASE_CONFIG["mixed-port"]);
|
|
5475
|
-
if (Number.isInteger(mixedPort)) reserved.set(mixedPort, "mihomo \u6DF7\u5408\u4EE3\u7406\u7AEF\u53E3");
|
|
5476
|
-
reserved.set(CONTROLLER_PORT, "mihomo \u63A7\u5236\u5668\u7AEF\u53E3");
|
|
5477
|
-
return reserved;
|
|
5478
|
-
}
|
|
5479
|
-
var SAFE_HOST_RE = /^[A-Za-z0-9][A-Za-z0-9._@-]*$/;
|
|
5480
|
-
function validateSshHost(host) {
|
|
5481
|
-
if (!host) {
|
|
5482
|
-
throw new CliError("\u7F3A\u5C11 --host", { hint: ["\u4F8B\u5982: mihomo ssh add work --host m4 --port 1080"] });
|
|
5483
|
-
}
|
|
5484
|
-
if (host.startsWith("-")) {
|
|
5485
|
-
throw new CliError(`\u4E3B\u673A\u540D\u65E0\u6548: "${host}"`, {
|
|
5486
|
-
label: "\u53C2\u6570\u9519\u8BEF",
|
|
5487
|
-
hint: ['\u4E3B\u673A\u540D\u4E0D\u80FD\u4EE5 "-" \u5F00\u5934\u2014\u2014\u5B83\u4F1A\u88AB ssh \u5F53\u4F5C\u9009\u9879\u89E3\u6790\uFF08\u5982 -oProxyCommand=... \u53EF\u6267\u884C\u4EFB\u610F\u547D\u4EE4\uFF09\u3002']
|
|
5488
|
-
});
|
|
5489
|
-
}
|
|
5490
|
-
if (!SAFE_HOST_RE.test(host)) {
|
|
5491
|
-
throw new CliError(`\u4E3B\u673A\u540D\u65E0\u6548: "${host}"\uFF0C\u53EA\u5141\u8BB8\u5B57\u6BCD\u3001\u6570\u5B57\u3001\u70B9\u3001\u4E0B\u5212\u7EBF\u3001\u77ED\u6A2A\u7EBF\u548C @`, {
|
|
5492
|
-
label: "\u53C2\u6570\u9519\u8BEF",
|
|
5493
|
-
hint: ["\u53EF\u7528 ssh \u522B\u540D\uFF08~/.ssh/config \u91CC\u7684 Host\uFF09\u6216 user@hostname\u3002"]
|
|
5494
|
-
});
|
|
5495
|
-
}
|
|
5496
|
-
}
|
|
5497
|
-
function validateSshPort(port, exclude) {
|
|
5498
|
-
if (!Number.isInteger(port) || port < 1 || port > 65535) {
|
|
5499
|
-
throw new CliError(`\u7AEF\u53E3\u65E0\u6548: ${port}\uFF0C\u9700\u4E3A 1-65535 \u7684\u6574\u6570`);
|
|
5500
|
-
}
|
|
5501
|
-
const reservedLabel = getReservedPorts().get(port);
|
|
5502
|
-
if (reservedLabel) {
|
|
5503
|
-
throw new CliError(`\u7AEF\u53E3 ${port} \u5DF2\u88AB ${reservedLabel} \u5360\u7528`, {
|
|
5504
|
-
label: "\u7AEF\u53E3\u51B2\u7A81",
|
|
5505
|
-
hint: ["\u8BF7\u6362\u4E00\u4E2A\u7AEF\u53E3\uFF0C\u4F8B\u5982 1080\u3002"]
|
|
5506
|
-
});
|
|
5507
|
-
}
|
|
5508
|
-
const conflict = getSshTunnels().find((t) => t.port === port && t.name !== exclude);
|
|
5509
|
-
if (conflict) {
|
|
5510
|
-
throw new CliError(`\u7AEF\u53E3 ${port} \u5DF2\u88AB\u96A7\u9053 "${conflict.name}" \u4F7F\u7528`, { label: "\u7AEF\u53E3\u51B2\u7A81" });
|
|
5511
|
-
}
|
|
5512
|
-
}
|
|
5513
|
-
function findSshTunnel(name) {
|
|
5514
|
-
return getSshTunnels().find((t) => t.name === name);
|
|
5515
|
-
}
|
|
5516
|
-
function addSshTunnel(config) {
|
|
5517
|
-
validateSshName(config.name);
|
|
5518
|
-
validateSshHost(config.host);
|
|
5519
|
-
validateSshPort(config.port);
|
|
5520
|
-
let duplicate = false;
|
|
5521
|
-
updateSettings(() => {
|
|
5522
|
-
const tunnels = [...getSshTunnels()];
|
|
5523
|
-
if (tunnels.some((t) => t.name === config.name)) {
|
|
5524
|
-
duplicate = true;
|
|
5525
|
-
return {};
|
|
5526
|
-
}
|
|
5527
|
-
tunnels.push(config);
|
|
5528
|
-
return { ssh: tunnels };
|
|
5529
|
-
});
|
|
5530
|
-
if (duplicate) {
|
|
5531
|
-
throw new CliError(`\u96A7\u9053 "${config.name}" \u5DF2\u5B58\u5728\uFF0C\u8BF7\u6362\u4E2A\u540D\u79F0\uFF0C\u6216\u5148\u5220\u9664\uFF08mihomo ssh rm ${config.name}\uFF09`);
|
|
5532
|
-
}
|
|
5533
|
-
}
|
|
5534
|
-
function removeSshTunnel(name) {
|
|
5535
|
-
let found = false;
|
|
5536
|
-
updateSettings(() => {
|
|
5537
|
-
const tunnels = [...getSshTunnels()];
|
|
5538
|
-
const idx = tunnels.findIndex((t) => t.name === name);
|
|
5539
|
-
if (idx < 0) return {};
|
|
5540
|
-
found = true;
|
|
5541
|
-
tunnels.splice(idx, 1);
|
|
5542
|
-
return { ssh: tunnels };
|
|
5543
|
-
});
|
|
5544
|
-
if (!found) return false;
|
|
5545
|
-
clearSshRuntime(name);
|
|
5546
|
-
return true;
|
|
5547
|
-
}
|
|
5548
|
-
function getSshRuntimePath(name) {
|
|
5549
|
-
validateSshName(name);
|
|
5550
|
-
return path7.join(DIRS.ssh, `${name}.json`);
|
|
5551
|
-
}
|
|
5552
|
-
function readSshRuntime(name) {
|
|
5553
|
-
try {
|
|
5554
|
-
const raw = fs11.readFileSync(getSshRuntimePath(name), "utf8");
|
|
5555
|
-
const parsed = JSON.parse(raw);
|
|
5556
|
-
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) return null;
|
|
5557
|
-
const r = parsed;
|
|
5558
|
-
if (!Number.isInteger(r.pid) || r.pid <= 0) return null;
|
|
5559
|
-
return {
|
|
5560
|
-
pid: r.pid,
|
|
5561
|
-
started_by: r.started_by === "manual" ? "manual" : "auto",
|
|
5562
|
-
started_at: typeof r.started_at === "string" ? r.started_at : "",
|
|
5563
|
-
port: Number.isInteger(r.port) ? r.port : 0
|
|
5564
|
-
};
|
|
5565
|
-
} catch {
|
|
5566
|
-
return null;
|
|
5567
|
-
}
|
|
5568
|
-
}
|
|
5569
|
-
function writeSshRuntime(name, runtime) {
|
|
5570
|
-
ensureDirs();
|
|
5571
|
-
atomicWriteFileSync(getSshRuntimePath(name), JSON.stringify(runtime, null, 2), { mode: 384 });
|
|
5572
|
-
}
|
|
5573
|
-
function clearSshRuntime(name) {
|
|
5574
|
-
try {
|
|
5575
|
-
fs11.rmSync(getSshRuntimePath(name), { force: true });
|
|
5576
|
-
} catch {
|
|
5577
|
-
}
|
|
5578
|
-
}
|
|
5579
|
-
function buildSshArgs(tunnel) {
|
|
5580
|
-
return [
|
|
5581
|
-
"-D",
|
|
5582
|
-
`127.0.0.1:${tunnel.port}`,
|
|
5583
|
-
"-N",
|
|
5584
|
-
"-o",
|
|
5585
|
-
"ExitOnForwardFailure=yes",
|
|
5586
|
-
"-o",
|
|
5587
|
-
"BatchMode=yes",
|
|
5588
|
-
"-o",
|
|
5589
|
-
"ServerAliveInterval=30",
|
|
5590
|
-
"-o",
|
|
5591
|
-
"ServerAliveCountMax=3",
|
|
5592
|
-
"-o",
|
|
5593
|
-
"ConnectTimeout=15",
|
|
5594
|
-
tunnel.host
|
|
5595
|
-
];
|
|
5596
|
-
}
|
|
5597
|
-
function commandNeedle(port) {
|
|
5598
|
-
return `-D 127.0.0.1:${port}`;
|
|
5599
|
-
}
|
|
5600
|
-
function getSshLogPath(name) {
|
|
5601
|
-
validateSshName(name);
|
|
5602
|
-
return path7.join(DIRS.logs, `ssh-${name}.log`);
|
|
5603
|
-
}
|
|
5604
|
-
function isSshProcessAlive(pid, port) {
|
|
5605
|
-
return isProcessRunning(pid) && isProcessCommandMatching(pid, commandNeedle(port));
|
|
5606
|
-
}
|
|
5607
|
-
function isPortListening(port, timeoutMs = PORT_PROBE_TIMEOUT_MS) {
|
|
5608
|
-
return new Promise((resolve) => {
|
|
5609
|
-
const socket = new net.Socket();
|
|
5610
|
-
let settled = false;
|
|
5611
|
-
const finish = (result) => {
|
|
5612
|
-
if (settled) return;
|
|
5613
|
-
settled = true;
|
|
5614
|
-
socket.destroy();
|
|
5615
|
-
resolve(result);
|
|
5616
|
-
};
|
|
5617
|
-
socket.setTimeout(timeoutMs);
|
|
5618
|
-
socket.once("connect", () => finish(true));
|
|
5619
|
-
socket.once("timeout", () => finish(false));
|
|
5620
|
-
socket.once("error", () => finish(false));
|
|
5621
|
-
socket.connect(port, "127.0.0.1");
|
|
5622
|
-
});
|
|
5623
|
-
}
|
|
5624
|
-
async function getSshStatus(config) {
|
|
5625
|
-
const runtime = readSshRuntime(config.name);
|
|
5626
|
-
const alive = runtime !== null && isSshProcessAlive(runtime.pid, runtime.port || config.port);
|
|
5627
|
-
if (!alive) {
|
|
5628
|
-
return { config, state: "stopped", pid: null, started_by: null, started_at: null };
|
|
5629
|
-
}
|
|
5630
|
-
const listening = await isPortListening(config.port);
|
|
5631
|
-
return {
|
|
5632
|
-
config,
|
|
5633
|
-
state: listening ? "running" : "dead-port",
|
|
5634
|
-
pid: runtime.pid,
|
|
5635
|
-
started_by: runtime.started_by,
|
|
5636
|
-
started_at: runtime.started_at || null
|
|
5637
|
-
};
|
|
5638
|
-
}
|
|
5639
|
-
async function getAllSshStatus() {
|
|
5640
|
-
return Promise.all(getSshTunnels().map(getSshStatus));
|
|
5641
|
-
}
|
|
5642
|
-
async function startSshTunnel(name, options) {
|
|
5643
|
-
const config = findSshTunnel(name);
|
|
5644
|
-
if (!config) {
|
|
5645
|
-
throw new CliError(`\u672A\u627E\u5230\u96A7\u9053 "${name}"`, { hint: ["\u67E5\u770B\u5168\u90E8\u96A7\u9053: mihomo ssh"] });
|
|
5646
|
-
}
|
|
5647
|
-
const existing = readSshRuntime(name);
|
|
5648
|
-
if (existing && isSshProcessAlive(existing.pid, existing.port || config.port)) {
|
|
5649
|
-
if (options.startedBy === "manual" && existing.started_by === "auto") {
|
|
5650
|
-
writeSshRuntime(name, { ...existing, started_by: "manual" });
|
|
5651
|
-
}
|
|
5652
|
-
return { alreadyRunning: true, pid: existing.pid };
|
|
5653
|
-
}
|
|
5654
|
-
if (existing) clearSshRuntime(name);
|
|
5655
|
-
if (await isPortListening(config.port)) {
|
|
5656
|
-
throw new CliError(`\u7AEF\u53E3 ${config.port} \u5DF2\u88AB\u5360\u7528`, {
|
|
5657
|
-
label: "\u65E0\u6CD5\u542F\u52A8\u96A7\u9053",
|
|
5658
|
-
hint: [`\u53EF\u80FD\u5DF2\u6709\u96A7\u9053\u6216\u5176\u4ED6\u7A0B\u5E8F\u5728\u76D1\u542C 127.0.0.1:${config.port}\u3002`, `\u67E5\u770B\u5360\u7528: lsof -nP -iTCP:${config.port} -sTCP:LISTEN`]
|
|
5659
|
-
});
|
|
5660
|
-
}
|
|
5661
|
-
ensureDirs();
|
|
5662
|
-
const logPath = getSshLogPath(name);
|
|
5663
|
-
const logFd = fs11.openSync(logPath, "w");
|
|
5664
|
-
const child = spawn2("ssh", buildSshArgs(config), {
|
|
5665
|
-
detached: true,
|
|
5666
|
-
stdio: ["ignore", logFd, logFd]
|
|
5667
|
-
});
|
|
5668
|
-
child.on("error", () => {
|
|
5669
|
-
});
|
|
5670
|
-
fs11.closeSync(logFd);
|
|
5671
|
-
child.unref();
|
|
5672
|
-
const pid = child.pid;
|
|
5673
|
-
if (!pid) {
|
|
5674
|
-
throw new CliError("\u65E0\u6CD5\u521B\u5EFA ssh \u8FDB\u7A0B", { label: "\u542F\u52A8\u96A7\u9053\u5931\u8D25", hint: ["\u8BF7\u786E\u8BA4\u5DF2\u5B89\u88C5 ssh \u5BA2\u6237\u7AEF\u3002"] });
|
|
5675
|
-
}
|
|
5676
|
-
writeSshRuntime(name, {
|
|
5677
|
-
pid,
|
|
5678
|
-
started_by: options.startedBy,
|
|
5679
|
-
started_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
5680
|
-
port: config.port
|
|
5681
|
-
});
|
|
5682
|
-
const unregisterCleanup = registerCleanup(() => {
|
|
5683
|
-
try {
|
|
5684
|
-
process.kill(pid, "SIGKILL");
|
|
5685
|
-
} catch {
|
|
5686
|
-
}
|
|
5687
|
-
clearSshRuntime(name);
|
|
5688
|
-
});
|
|
5689
|
-
try {
|
|
5690
|
-
for (let i = 0; i < START_WAIT_ATTEMPTS; i++) {
|
|
5691
|
-
if (!isProcessRunning(pid)) break;
|
|
5692
|
-
if (await isPortListening(config.port)) {
|
|
5693
|
-
unregisterCleanup();
|
|
5694
|
-
return { alreadyRunning: false, pid };
|
|
5695
|
-
}
|
|
5696
|
-
await new Promise((resolve) => setTimeout(resolve, START_WAIT_INTERVAL));
|
|
5697
|
-
}
|
|
5698
|
-
} finally {
|
|
5699
|
-
unregisterCleanup();
|
|
5700
|
-
}
|
|
5701
|
-
clearSshRuntime(name);
|
|
5702
|
-
if (isProcessRunning(pid)) {
|
|
5703
|
-
try {
|
|
5704
|
-
process.kill(pid, "SIGKILL");
|
|
5705
|
-
} catch {
|
|
5706
|
-
}
|
|
5707
|
-
}
|
|
5708
|
-
throw new CliError("ssh \u96A7\u9053\u672A\u80FD\u5EFA\u7ACB", {
|
|
5709
|
-
label: "\u542F\u52A8\u96A7\u9053\u5931\u8D25",
|
|
5710
|
-
hint: [...readLogTail(logPath), `\u5B8C\u6574\u65E5\u5FD7: ${logPath}`]
|
|
5711
|
-
});
|
|
5712
|
-
}
|
|
5713
|
-
function readLogTail(logPath, maxLines = 5) {
|
|
5714
|
-
try {
|
|
5715
|
-
const content = fs11.readFileSync(logPath, "utf8").trim();
|
|
5716
|
-
if (!content) return [];
|
|
5717
|
-
return content.split("\n").slice(-maxLines).map((line) => ` ${line.trim()}`);
|
|
5718
|
-
} catch {
|
|
5719
|
-
return [];
|
|
5720
|
-
}
|
|
5721
|
-
}
|
|
5722
|
-
async function stopSshTunnel(name) {
|
|
5723
|
-
const config = findSshTunnel(name);
|
|
5724
|
-
const runtime = readSshRuntime(name);
|
|
5725
|
-
if (!runtime) {
|
|
5726
|
-
clearSshRuntime(name);
|
|
5727
|
-
return { notRunning: true, pid: null };
|
|
5728
|
-
}
|
|
5729
|
-
const port = runtime.port || config?.port || 0;
|
|
5730
|
-
const { pid } = runtime;
|
|
5731
|
-
if (!isSshProcessAlive(pid, port)) {
|
|
5732
|
-
clearSshRuntime(name);
|
|
5733
|
-
return { notRunning: true, pid: null };
|
|
5734
|
-
}
|
|
5735
|
-
try {
|
|
5736
|
-
process.kill(pid, "SIGTERM");
|
|
5737
|
-
} catch {
|
|
5738
|
-
}
|
|
5739
|
-
for (let i = 0; i < STOP_WAIT_ATTEMPTS; i++) {
|
|
5740
|
-
if (!isProcessRunning(pid)) break;
|
|
5741
|
-
await sleep(STOP_WAIT_INTERVAL);
|
|
5742
|
-
}
|
|
5743
|
-
if (isProcessRunning(pid) && isProcessCommandMatching(pid, commandNeedle(port))) {
|
|
5744
|
-
try {
|
|
5745
|
-
process.kill(pid, "SIGKILL");
|
|
5746
|
-
} catch {
|
|
5747
|
-
}
|
|
5748
|
-
for (let i = 0; i < STOP_WAIT_ATTEMPTS; i++) {
|
|
5749
|
-
if (!isProcessRunning(pid)) break;
|
|
5750
|
-
await sleep(STOP_WAIT_INTERVAL);
|
|
5751
|
-
}
|
|
5752
|
-
}
|
|
5753
|
-
clearSshRuntime(name);
|
|
5754
|
-
return { notRunning: false, pid };
|
|
5755
|
-
}
|
|
5756
|
-
async function startAutoSshTunnels() {
|
|
5757
|
-
const outcomes = [];
|
|
5758
|
-
for (const tunnel of getSshTunnels()) {
|
|
5759
|
-
if (!tunnel.auto) continue;
|
|
5760
|
-
try {
|
|
5761
|
-
const result = await startSshTunnel(tunnel.name, { startedBy: "auto" });
|
|
5762
|
-
outcomes.push({ name: tunnel.name, ok: true, alreadyRunning: result.alreadyRunning });
|
|
5763
|
-
} catch (e) {
|
|
5764
|
-
outcomes.push({
|
|
5765
|
-
name: tunnel.name,
|
|
5766
|
-
ok: false,
|
|
5767
|
-
error: e instanceof CliError ? e : new CliError(e.message)
|
|
5768
|
-
});
|
|
5769
|
-
}
|
|
5770
|
-
}
|
|
5771
|
-
return outcomes;
|
|
5772
|
-
}
|
|
5773
|
-
async function stopAutoSshTunnels() {
|
|
5774
|
-
const stopped = [];
|
|
5775
|
-
for (const tunnel of getSshTunnels()) {
|
|
5776
|
-
const runtime = readSshRuntime(tunnel.name);
|
|
5777
|
-
if (runtime?.started_by !== "auto") continue;
|
|
5778
|
-
const result = await stopSshTunnel(tunnel.name);
|
|
5779
|
-
if (!result.notRunning) stopped.push(tunnel.name);
|
|
5780
|
-
}
|
|
5781
|
-
return stopped;
|
|
5782
|
-
}
|
|
5783
|
-
async function stopAllSshTunnels() {
|
|
5784
|
-
const stopped = [];
|
|
5785
|
-
for (const tunnel of getSshTunnels()) {
|
|
5786
|
-
const result = await stopSshTunnel(tunnel.name);
|
|
5787
|
-
if (!result.notRunning) stopped.push(tunnel.name);
|
|
5788
|
-
}
|
|
5789
|
-
return stopped;
|
|
5790
|
-
}
|
|
5791
|
-
|
|
5792
5410
|
// src/commands/status.ts
|
|
5793
5411
|
async function printStatus() {
|
|
5794
5412
|
const state = getRunningState();
|
|
@@ -5852,17 +5470,6 @@ async function printStatus() {
|
|
|
5852
5470
|
if (isDaemonEnabled()) {
|
|
5853
5471
|
console.log(`${colors.gray("\u4FDD\u6D3B: ")}${colors.green("\u5DF2\u542F\u7528")} ${colors.gray("(\u5F00\u673A\u81EA\u542F + \u5D29\u6E83\u91CD\u542F)")}`);
|
|
5854
5472
|
}
|
|
5855
|
-
const tunnels = getSshTunnels();
|
|
5856
|
-
if (tunnels.length > 0) {
|
|
5857
|
-
const statuses = await getAllSshStatus();
|
|
5858
|
-
const parts = statuses.map((s) => {
|
|
5859
|
-
const label = `${s.config.name}:${s.config.port}`;
|
|
5860
|
-
if (s.state === "running") return colors.green(label);
|
|
5861
|
-
if (s.state === "dead-port") return colors.yellow(`${label} \u5047\u6D3B`);
|
|
5862
|
-
return colors.gray(`${label} \u672A\u8FD0\u884C`);
|
|
5863
|
-
});
|
|
5864
|
-
console.log(`${colors.gray("\u96A7\u9053: ")}${parts.join(", ")}`);
|
|
5865
|
-
}
|
|
5866
5473
|
console.log("");
|
|
5867
5474
|
}
|
|
5868
5475
|
|
|
@@ -5872,51 +5479,24 @@ function handleStopResult(result) {
|
|
|
5872
5479
|
throw new CliError(result.remaining.join(", "), { label: "\u90E8\u5206\u8FDB\u7A0B\u672A\u7EC8\u6B62", hint: "\u8BF7\u624B\u52A8\u8FD0\u884C: sudo pkill -9 mihomo" });
|
|
5873
5480
|
}
|
|
5874
5481
|
}
|
|
5875
|
-
async function stopAutoSshTunnelsWithLog() {
|
|
5876
|
-
const stopped = await stopAutoSshTunnels();
|
|
5877
|
-
if (stopped.length > 0) {
|
|
5878
|
-
console.log(`${colors.green("\u5DF2\u505C\u6B62\u96A7\u9053")}: ${stopped.join(", ")}`);
|
|
5879
|
-
}
|
|
5880
|
-
}
|
|
5881
5482
|
async function cmdStop(args) {
|
|
5483
|
+
assertNoRemovedSshFlag(args);
|
|
5882
5484
|
if (isDaemonEnabled()) {
|
|
5883
5485
|
console.log(colors.yellow("\u4FDD\u6D3B\u5DF2\u542F\u7528\uFF0C\u4EE3\u7406\u7531 launchd \u6258\u7BA1"));
|
|
5884
5486
|
console.log("\u76F4\u63A5\u505C\u6B62\u4F1A\u88AB\u81EA\u52A8\u91CD\u65B0\u62C9\u8D77\uFF0C\u8BF7\u7528: mihomo daemon off");
|
|
5885
5487
|
return;
|
|
5886
5488
|
}
|
|
5887
|
-
const skipSsh = hasFlag(args, "--no-ssh");
|
|
5888
5489
|
const pids = getMihomoPids();
|
|
5889
5490
|
if (pids.length === 0) {
|
|
5890
5491
|
console.log(colors.yellow("\u4E0D\u5728\u8FD0\u884C"));
|
|
5891
|
-
if (!skipSsh) await stopAutoSshTunnelsWithLog();
|
|
5892
5492
|
return;
|
|
5893
5493
|
}
|
|
5894
5494
|
console.log(`\u505C\u6B62 ${pids.length} \u4E2A\u8FDB\u7A0B...`);
|
|
5895
5495
|
handleStopResult(await stop());
|
|
5896
5496
|
console.log(colors.green("\u5DF2\u505C\u6B62\u8FDB\u7A0B"));
|
|
5897
|
-
if (!skipSsh) await stopAutoSshTunnelsWithLog();
|
|
5898
5497
|
}
|
|
5899
5498
|
|
|
5900
5499
|
// src/commands/start.ts
|
|
5901
|
-
async function startAutoSshTunnelsWithWarning() {
|
|
5902
|
-
const outcomes = await startAutoSshTunnels();
|
|
5903
|
-
if (outcomes.length === 0) return;
|
|
5904
|
-
const started = outcomes.filter((o) => o.ok && !o.alreadyRunning);
|
|
5905
|
-
if (started.length > 0) {
|
|
5906
|
-
console.log(`${colors.green("\u5DF2\u542F\u52A8\u96A7\u9053")}: ${started.map((o) => o.name).join(", ")}`);
|
|
5907
|
-
}
|
|
5908
|
-
for (const failed of outcomes.filter((o) => !o.ok)) {
|
|
5909
|
-
console.log("");
|
|
5910
|
-
console.log(colors.yellow(`\u8B66\u544A: \u96A7\u9053 "${failed.name}" \u542F\u52A8\u5931\u8D25`));
|
|
5911
|
-
console.log(colors.gray(` ${failed.error?.message ?? "\u672A\u77E5\u9519\u8BEF"}`));
|
|
5912
|
-
for (const line of failed.error?.hint ?? []) {
|
|
5913
|
-
if (line.trim()) console.log(colors.gray(line.startsWith(" ") ? line : ` ${line}`));
|
|
5914
|
-
}
|
|
5915
|
-
console.log(colors.gray(" \u5185\u7F51\u5206\u6D41\u89C4\u5219\u5C06\u4E0D\u53EF\u7528\uFF0C\u5176\u4F59\u6D41\u91CF\u6B63\u5E38"));
|
|
5916
|
-
console.log(colors.gray(` \u6392\u67E5: mihomo ssh status ${failed.name}`));
|
|
5917
|
-
console.log("");
|
|
5918
|
-
}
|
|
5919
|
-
}
|
|
5920
5500
|
function resolveStartMode(args) {
|
|
5921
5501
|
const modeArg = getNonFlagArg(args, 1);
|
|
5922
5502
|
const modeToken = modeArg?.toLowerCase();
|
|
@@ -5926,6 +5506,7 @@ function resolveStartMode(args) {
|
|
|
5926
5506
|
return modeToken === "tun" ? "tun" : "mixed";
|
|
5927
5507
|
}
|
|
5928
5508
|
async function cmdStart(args) {
|
|
5509
|
+
assertNoRemovedSshFlag(args);
|
|
5929
5510
|
const targetMode = resolveStartMode(args);
|
|
5930
5511
|
if (!hasKernel()) {
|
|
5931
5512
|
throw new CliError('\u672A\u627E\u5230\u5185\u6838\uFF0C\u8BF7\u8FD0\u884C "mihomo kernel"');
|
|
@@ -5935,7 +5516,6 @@ async function cmdStart(args) {
|
|
|
5935
5516
|
throw new CliError("\u4FDD\u6D3B\u5DF2\u542F\u7528\uFF08\u4EC5\u652F\u6301 Mixed \u6A21\u5F0F\uFF09\uFF0C\u65E0\u6CD5\u542F\u52A8 TUN", { hint: "\u8BF7\u5148\u5173\u95ED\u4FDD\u6D3B: mihomo daemon off" });
|
|
5936
5517
|
}
|
|
5937
5518
|
const skipUpdate = hasFlag(args, "-s", "--no-update");
|
|
5938
|
-
const skipSsh = hasFlag(args, "--no-ssh");
|
|
5939
5519
|
const updateTimeout = parseIntArg(args, "-u", "--update-timeout", DEFAULT_AUTO_UPDATE_TIMEOUT);
|
|
5940
5520
|
const sub = requireActiveSubscription("\u6CA1\u6709\u8BA2\u9605\uFF0C\u8BF7\u5148\u6DFB\u52A0\u8BA2\u9605");
|
|
5941
5521
|
if (!skipUpdate) {
|
|
@@ -5978,9 +5558,6 @@ async function cmdStart(args) {
|
|
|
5978
5558
|
const lines = e.message.split("\n");
|
|
5979
5559
|
throw new CliError(lines[0], { label: "\u542F\u52A8\u5931\u8D25", hint: lines.slice(1) });
|
|
5980
5560
|
}
|
|
5981
|
-
if (!skipSsh) {
|
|
5982
|
-
await startAutoSshTunnelsWithWarning();
|
|
5983
|
-
}
|
|
5984
5561
|
await printStatus();
|
|
5985
5562
|
}
|
|
5986
5563
|
|
|
@@ -6109,10 +5686,31 @@ async function cmdDaemon(args) {
|
|
|
6109
5686
|
}
|
|
6110
5687
|
|
|
6111
5688
|
// src/open.ts
|
|
6112
|
-
import { spawn as
|
|
5689
|
+
import { spawn as spawn2 } from "child_process";
|
|
5690
|
+
|
|
5691
|
+
// src/lifecycle.ts
|
|
5692
|
+
var cleanupFns = /* @__PURE__ */ new Set();
|
|
5693
|
+
var silentSigint = false;
|
|
5694
|
+
function setSilentSigint(value) {
|
|
5695
|
+
silentSigint = value;
|
|
5696
|
+
}
|
|
5697
|
+
function isSilentSigint() {
|
|
5698
|
+
return silentSigint;
|
|
5699
|
+
}
|
|
5700
|
+
function runCleanup() {
|
|
5701
|
+
for (const fn of cleanupFns) {
|
|
5702
|
+
try {
|
|
5703
|
+
fn();
|
|
5704
|
+
} catch {
|
|
5705
|
+
}
|
|
5706
|
+
}
|
|
5707
|
+
cleanupFns.clear();
|
|
5708
|
+
}
|
|
5709
|
+
|
|
5710
|
+
// src/open.ts
|
|
6113
5711
|
function openUrl(url) {
|
|
6114
5712
|
try {
|
|
6115
|
-
const child =
|
|
5713
|
+
const child = spawn2("open", ["--", url], { stdio: "ignore", detached: true });
|
|
6116
5714
|
child.unref();
|
|
6117
5715
|
child.on("error", () => {
|
|
6118
5716
|
});
|
|
@@ -6143,7 +5741,7 @@ function viewLogWithTail(logPath, options) {
|
|
|
6143
5741
|
if (follow) tailArgs.push("-f");
|
|
6144
5742
|
tailArgs.push("-n", lines.toString());
|
|
6145
5743
|
tailArgs.push(logPath);
|
|
6146
|
-
const tail =
|
|
5744
|
+
const tail = spawn2("tail", tailArgs, { stdio: "inherit" });
|
|
6147
5745
|
if (follow) setSilentSigint(true);
|
|
6148
5746
|
tail.on("close", (code, signal) => process.exit(signal ? 0 : code ?? 0));
|
|
6149
5747
|
tail.on("error", (e) => {
|
|
@@ -6213,8 +5811,8 @@ async function cmdDirectory(args) {
|
|
|
6213
5811
|
|
|
6214
5812
|
// src/kernel.ts
|
|
6215
5813
|
import { spawnSync as spawnSync5 } from "child_process";
|
|
6216
|
-
import
|
|
6217
|
-
import
|
|
5814
|
+
import fs11 from "fs";
|
|
5815
|
+
import path7 from "path";
|
|
6218
5816
|
|
|
6219
5817
|
// ../../../node_modules/compare-versions/lib/esm/utils.js
|
|
6220
5818
|
var semver = /^[v^~<>=]*?(\d+)(?:\.([x*]|\d+)(?:\.([x*]|\d+)(?:\.([x*]|\d+))?(?:-([\da-z\-]+(?:\.[\da-z\-]+)*))?(?:\+[\da-z\-]+(?:\.[\da-z\-]+)*)?)?)?$/i;
|
|
@@ -6357,10 +5955,10 @@ async function checkUpdate() {
|
|
|
6357
5955
|
}
|
|
6358
5956
|
function findBinaryInDir(dir, maxDepth = 4) {
|
|
6359
5957
|
if (maxDepth <= 0) return null;
|
|
6360
|
-
const files =
|
|
5958
|
+
const files = fs11.readdirSync(dir);
|
|
6361
5959
|
for (const f of files) {
|
|
6362
|
-
const fullPath =
|
|
6363
|
-
const stat =
|
|
5960
|
+
const fullPath = path7.join(dir, f);
|
|
5961
|
+
const stat = fs11.lstatSync(fullPath);
|
|
6364
5962
|
if (stat.isDirectory()) {
|
|
6365
5963
|
const found = findBinaryInDir(fullPath, maxDepth - 1);
|
|
6366
5964
|
if (found) return found;
|
|
@@ -6388,7 +5986,7 @@ async function downloadKernel(progressCallback, mirror, releaseInfo) {
|
|
|
6388
5986
|
}
|
|
6389
5987
|
assertTrustedAssetUrl(asset.browser_download_url);
|
|
6390
5988
|
const downloadUrl = withMirror(asset.browser_download_url, mirror);
|
|
6391
|
-
const tempPath =
|
|
5989
|
+
const tempPath = path7.join(DIRS.kernel, path7.basename(asset.name));
|
|
6392
5990
|
const sizeMB = (asset.size / 1024 / 1024).toFixed(2);
|
|
6393
5991
|
if (mirror && progressCallback) {
|
|
6394
5992
|
progressCallback("\u63D0\u793A: \u7ECF\u7B2C\u4E09\u65B9\u955C\u50CF\u4E2D\u8F6C\u4E0B\u8F7D\uFF0C\u65E0\u6CD5\u9A8C\u8BC1\u6765\u6E90\u5B8C\u6574\u6027\uFF0C\u5EFA\u8BAE\u76F4\u8FDE\u6216\u81EA\u884C\u6821\u9A8C\u4EA7\u7269");
|
|
@@ -6426,19 +6024,19 @@ async function downloadKernel(progressCallback, mirror, releaseInfo) {
|
|
|
6426
6024
|
}
|
|
6427
6025
|
if (curlResult.status !== 0) {
|
|
6428
6026
|
try {
|
|
6429
|
-
|
|
6027
|
+
fs11.unlinkSync(tempPath);
|
|
6430
6028
|
} catch {
|
|
6431
6029
|
}
|
|
6432
6030
|
throw new Error(`\u4E0B\u8F7D\u5931\u8D25 (curl \u9000\u51FA\u7801 ${curlResult.status})`);
|
|
6433
6031
|
}
|
|
6434
|
-
if (!
|
|
6032
|
+
if (!fs11.existsSync(tempPath)) {
|
|
6435
6033
|
throw new Error("\u4E0B\u8F7D\u5931\u8D25: \u6587\u4EF6\u672A\u751F\u6210");
|
|
6436
6034
|
}
|
|
6437
6035
|
if (Number.isFinite(asset.size) && asset.size > 0) {
|
|
6438
|
-
const actual =
|
|
6036
|
+
const actual = fs11.statSync(tempPath).size;
|
|
6439
6037
|
if (actual !== asset.size) {
|
|
6440
6038
|
try {
|
|
6441
|
-
|
|
6039
|
+
fs11.unlinkSync(tempPath);
|
|
6442
6040
|
} catch {
|
|
6443
6041
|
}
|
|
6444
6042
|
throw new Error(
|
|
@@ -6478,17 +6076,17 @@ async function downloadKernel(progressCallback, mirror, releaseInfo) {
|
|
|
6478
6076
|
if (tarResult.error) throw tarResult.error;
|
|
6479
6077
|
if (tarResult.status !== 0) throw new Error(`tar \u9000\u51FA\u7801 ${tarResult.status}`);
|
|
6480
6078
|
} else if (tempPath.endsWith(".gz")) {
|
|
6481
|
-
const baseName =
|
|
6482
|
-
const outputPath =
|
|
6079
|
+
const baseName = path7.basename(tempPath, ".gz");
|
|
6080
|
+
const outputPath = path7.join(extractPath, baseName);
|
|
6483
6081
|
const gzipResult = spawnSync5("gzip", ["-dc", tempPath], { maxBuffer: 256 * 1024 * 1024, timeout: 6e4 });
|
|
6484
6082
|
if (gzipResult.error) throw gzipResult.error;
|
|
6485
6083
|
if (gzipResult.status !== 0) throw new Error(`gzip \u9000\u51FA\u7801 ${gzipResult.status}`);
|
|
6486
|
-
|
|
6084
|
+
fs11.writeFileSync(outputPath, gzipResult.stdout, { mode: 493 });
|
|
6487
6085
|
extractedBinary = outputPath;
|
|
6488
6086
|
}
|
|
6489
6087
|
} catch (e) {
|
|
6490
6088
|
try {
|
|
6491
|
-
|
|
6089
|
+
fs11.unlinkSync(tempPath);
|
|
6492
6090
|
} catch {
|
|
6493
6091
|
}
|
|
6494
6092
|
throw new Error(`\u89E3\u538B\u5931\u8D25: ${e.message}`);
|
|
@@ -6496,23 +6094,23 @@ async function downloadKernel(progressCallback, mirror, releaseInfo) {
|
|
|
6496
6094
|
const foundBinary = extractedBinary || findBinaryInDir(extractPath);
|
|
6497
6095
|
if (!foundBinary) {
|
|
6498
6096
|
try {
|
|
6499
|
-
|
|
6097
|
+
fs11.unlinkSync(tempPath);
|
|
6500
6098
|
} catch {
|
|
6501
6099
|
}
|
|
6502
6100
|
throw new Error("\u89E3\u538B\u540E\u672A\u627E\u5230\u53EF\u6267\u884C\u6587\u4EF6");
|
|
6503
6101
|
}
|
|
6504
6102
|
const targetPath = PATHS.mihomoBinary;
|
|
6505
6103
|
if (foundBinary !== targetPath) {
|
|
6506
|
-
if (
|
|
6507
|
-
|
|
6104
|
+
if (fs11.existsSync(targetPath)) {
|
|
6105
|
+
fs11.chmodSync(targetPath, 493);
|
|
6508
6106
|
try {
|
|
6509
|
-
|
|
6107
|
+
fs11.unlinkSync(targetPath);
|
|
6510
6108
|
} catch {
|
|
6511
6109
|
}
|
|
6512
6110
|
}
|
|
6513
|
-
|
|
6111
|
+
fs11.renameSync(foundBinary, targetPath);
|
|
6514
6112
|
}
|
|
6515
|
-
|
|
6113
|
+
fs11.chmodSync(targetPath, 493);
|
|
6516
6114
|
if (progressCallback) {
|
|
6517
6115
|
progressCallback("\u6821\u9A8C\u5185\u6838...");
|
|
6518
6116
|
}
|
|
@@ -6520,11 +6118,11 @@ async function downloadKernel(progressCallback, mirror, releaseInfo) {
|
|
|
6520
6118
|
const checkOutput = `${check.stdout || ""}${check.stderr || ""}`.trim();
|
|
6521
6119
|
if (check.error || check.status !== 0 || !/v?\d+\.\d+\.\d+/.test(checkOutput)) {
|
|
6522
6120
|
try {
|
|
6523
|
-
|
|
6121
|
+
fs11.unlinkSync(targetPath);
|
|
6524
6122
|
} catch {
|
|
6525
6123
|
}
|
|
6526
6124
|
try {
|
|
6527
|
-
|
|
6125
|
+
fs11.unlinkSync(tempPath);
|
|
6528
6126
|
} catch {
|
|
6529
6127
|
}
|
|
6530
6128
|
throw new Error(`\u5185\u6838\u81EA\u68C0\u5931\u8D25\uFF08\u53EF\u80FD\u4E0B\u8F7D\u635F\u574F\u6216\u67B6\u6784\u4E0D\u5339\u914D\uFF09\uFF0C\u5DF2\u5220\u9664
|
|
@@ -6532,7 +6130,7 @@ async function downloadKernel(progressCallback, mirror, releaseInfo) {
|
|
|
6532
6130
|
\u8F93\u51FA: ${checkOutput || "(\u7A7A)"}`);
|
|
6533
6131
|
}
|
|
6534
6132
|
try {
|
|
6535
|
-
|
|
6133
|
+
fs11.unlinkSync(tempPath);
|
|
6536
6134
|
} catch {
|
|
6537
6135
|
}
|
|
6538
6136
|
clearKernelVersionCache();
|
|
@@ -6672,7 +6270,7 @@ function cmdLogs(args) {
|
|
|
6672
6270
|
}
|
|
6673
6271
|
|
|
6674
6272
|
// src/commands/overwrite.ts
|
|
6675
|
-
import
|
|
6273
|
+
import path8 from "path";
|
|
6676
6274
|
function printOverwriteList() {
|
|
6677
6275
|
const info = listOverwriteFile();
|
|
6678
6276
|
const statusText = info.enabled ? colors.green("\u5DF2\u542F\u7528") : colors.yellow("\u5DF2\u7981\u7528");
|
|
@@ -6682,8 +6280,8 @@ function printOverwriteList() {
|
|
|
6682
6280
|
if (info.files.length === 0) {
|
|
6683
6281
|
console.log("\u6682\u65E0\u8986\u5199\u6587\u4EF6");
|
|
6684
6282
|
console.log("");
|
|
6685
|
-
console.log(`\u7528\u6CD5\u793A\u4F8B: \u521B\u5EFA\u6587\u4EF6 ${
|
|
6686
|
-
console.log(` \u6216 ${
|
|
6283
|
+
console.log(`\u7528\u6CD5\u793A\u4F8B: \u521B\u5EFA\u6587\u4EF6 ${path8.join(info.dir, "overwrite.yaml")}`);
|
|
6284
|
+
console.log(` \u6216 ${path8.join(info.dir, "overwrite.dns.yaml")}`);
|
|
6687
6285
|
console.log("");
|
|
6688
6286
|
} else {
|
|
6689
6287
|
console.log(`${colors.cyan("\u8986\u5199\u6587\u4EF6")} (${info.files.length} \u4E2A\uFF0C\u6309\u987A\u5E8F\u52A0\u8F7D):`);
|
|
@@ -6739,7 +6337,7 @@ async function cmdOverwrite(args) {
|
|
|
6739
6337
|
}
|
|
6740
6338
|
|
|
6741
6339
|
// src/commands/reset.ts
|
|
6742
|
-
import
|
|
6340
|
+
import fs12 from "fs";
|
|
6743
6341
|
var RESET_TARGETS = [
|
|
6744
6342
|
{
|
|
6745
6343
|
id: "subs",
|
|
@@ -6772,24 +6370,6 @@ var RESET_TARGETS = [
|
|
|
6772
6370
|
paths: () => [DIRS.runtime],
|
|
6773
6371
|
needsStop: true
|
|
6774
6372
|
},
|
|
6775
|
-
{
|
|
6776
|
-
// 隧道要在删 pid 文件之前先停进程(onBefore):文件一删就再也找不到那些 ssh 进程,
|
|
6777
|
-
// 它们会继续占着端口跑下去,且 CLI 无任何路径能停掉。
|
|
6778
|
-
// **必须排在 settings 之前**:onAfter 会 writeSettings 重建 settings.json,
|
|
6779
|
-
// 排在 settings 之后会让 `reset --full` 留下一个 {}(同 subs 的处理,见执行顺序注释)
|
|
6780
|
-
id: "ssh",
|
|
6781
|
-
aliases: ["ssh"],
|
|
6782
|
-
label: "\u96A7\u9053",
|
|
6783
|
-
// 只删运行态:v3.12.0 起 CLI 不再生成任何 ssh 配置文件,节点/规则都在用户自己
|
|
6784
|
-
// 维护的 overwrite.yaml 里(归 overwrites 目标管),这里没有额外文件要清
|
|
6785
|
-
paths: () => [DIRS.ssh],
|
|
6786
|
-
needsStop: false,
|
|
6787
|
-
onBefore: async () => {
|
|
6788
|
-
await stopAllSshTunnels();
|
|
6789
|
-
},
|
|
6790
|
-
// 同步清空 settings 里的隧道列表:只删运行态会留下「列表在但状态没了」的半重置状态
|
|
6791
|
-
onAfter: () => writeSettings({ ssh: void 0 })
|
|
6792
|
-
},
|
|
6793
6373
|
{
|
|
6794
6374
|
id: "settings",
|
|
6795
6375
|
aliases: ["setting", "settings", "config"],
|
|
@@ -6817,8 +6397,8 @@ var RESET_TARGETS = [
|
|
|
6817
6397
|
label: "\u8986\u5199",
|
|
6818
6398
|
paths: () => {
|
|
6819
6399
|
const dir = USER_DATA_DIR;
|
|
6820
|
-
if (!
|
|
6821
|
-
return
|
|
6400
|
+
if (!fs12.existsSync(dir)) return [];
|
|
6401
|
+
return fs12.readdirSync(dir).filter(isOverwriteFilename).map((f) => `${dir}/${f}`);
|
|
6822
6402
|
},
|
|
6823
6403
|
needsStop: false
|
|
6824
6404
|
},
|
|
@@ -6881,7 +6461,7 @@ async function cmdReset(args) {
|
|
|
6881
6461
|
}
|
|
6882
6462
|
targets = matched;
|
|
6883
6463
|
} else {
|
|
6884
|
-
targets = RESET_TARGETS.filter((t) => !["settings", "kernel", "overwrites", "daemon"
|
|
6464
|
+
targets = RESET_TARGETS.filter((t) => !["settings", "kernel", "overwrites", "daemon"].includes(t.id));
|
|
6885
6465
|
}
|
|
6886
6466
|
for (const t of targets) {
|
|
6887
6467
|
if (t.checkEmpty?.()) {
|
|
@@ -6941,9 +6521,8 @@ async function cmdReset(args) {
|
|
|
6941
6521
|
}
|
|
6942
6522
|
}
|
|
6943
6523
|
for (const t of targets) {
|
|
6944
|
-
await t.onBefore?.();
|
|
6945
6524
|
for (const p of t.paths()) {
|
|
6946
|
-
if (
|
|
6525
|
+
if (fs12.existsSync(p)) {
|
|
6947
6526
|
try {
|
|
6948
6527
|
rmrf(p);
|
|
6949
6528
|
} catch (e) {
|
|
@@ -6960,225 +6539,6 @@ async function cmdReset(args) {
|
|
|
6960
6539
|
console.log(colors.green(`\u5DF2\u91CD\u7F6E: ${targets.map((t) => t.label).join("\u3001")}`));
|
|
6961
6540
|
}
|
|
6962
6541
|
|
|
6963
|
-
// src/commands/ssh.ts
|
|
6964
|
-
import fs14 from "fs";
|
|
6965
|
-
function warnLegacySshConfigFiles() {
|
|
6966
|
-
if (!fs14.existsSync(USER_DATA_DIR)) return;
|
|
6967
|
-
const legacy = fs14.readdirSync(USER_DATA_DIR).filter((f) => /^ssh\..+\.ya?ml$/.test(f));
|
|
6968
|
-
if (legacy.length === 0) return;
|
|
6969
|
-
console.log(colors.yellow(`\u8B66\u544A: \u53D1\u73B0 ${legacy.length} \u4E2A\u5DF2\u5931\u6548\u7684 ssh \u914D\u7F6E\u6587\u4EF6`));
|
|
6970
|
-
for (const f of legacy) {
|
|
6971
|
-
console.log(colors.gray(` ${f}`));
|
|
6972
|
-
}
|
|
6973
|
-
console.log(colors.gray(" v3.12.0 \u8D77 CLI \u4E0D\u518D\u52A0\u8F7D\u5B83\u4EEC\uFF0C\u5176\u4E2D\u7684\u5206\u7EC4\u4E0E\u89C4\u5219\u5DF2\u4E0D\u751F\u6548"));
|
|
6974
|
-
console.log(colors.gray(" \u9700\u8981\u7684\u8BDD\u628A\u5185\u5BB9\u5E76\u5165 overwrite.yaml\uFF08\u542B socks5 \u8282\u70B9\u5B9A\u4E49\uFF09\uFF0C\u7136\u540E\u81EA\u884C\u5220\u9664"));
|
|
6975
|
-
console.log("");
|
|
6976
|
-
}
|
|
6977
|
-
function printOverwriteSnippet(config) {
|
|
6978
|
-
console.log(colors.gray("\u628A\u96A7\u9053\u63A5\u5165\u5206\u6D41\uFF1A\u5728 overwrite.yaml \u91CC\u52A0\u4E0A\uFF08\u6539\u6210\u4F60\u7684\u5185\u7F51\u57DF\u540D\uFF09"));
|
|
6979
|
-
console.log(colors.gray(` ~proxies:`));
|
|
6980
|
-
console.log(colors.gray(` - {name: SSH-${config.name}, type: socks5, server: 127.0.0.1, port: ${config.port}}`));
|
|
6981
|
-
console.log(colors.gray(` +rules:`));
|
|
6982
|
-
console.log(colors.gray(` - DOMAIN-SUFFIX,example.internal,SSH-${config.name}`));
|
|
6983
|
-
}
|
|
6984
|
-
function formatState(status) {
|
|
6985
|
-
switch (status.state) {
|
|
6986
|
-
case "running":
|
|
6987
|
-
return colors.green(`\u8FD0\u884C\u4E2D (PID ${status.pid})`);
|
|
6988
|
-
case "dead-port":
|
|
6989
|
-
return colors.yellow(`\u5047\u6D3B (PID ${status.pid}\uFF0C\u7AEF\u53E3 ${status.config.port} \u4E0D\u901A)`);
|
|
6990
|
-
default:
|
|
6991
|
-
return colors.yellow("\u672A\u8FD0\u884C");
|
|
6992
|
-
}
|
|
6993
|
-
}
|
|
6994
|
-
function getSshNameArg(args) {
|
|
6995
|
-
return getNonFlagArg(args, 2);
|
|
6996
|
-
}
|
|
6997
|
-
function requireSshNameArg(args, usage) {
|
|
6998
|
-
const name = getSshNameArg(args);
|
|
6999
|
-
if (!name) {
|
|
7000
|
-
const tunnels = getSshTunnels();
|
|
7001
|
-
throw new CliError("\u8BF7\u6307\u5B9A\u96A7\u9053\u540D\u79F0", {
|
|
7002
|
-
hint: [usage, ...tunnels.length > 0 ? ["", "\u53EF\u7528\u96A7\u9053:", ...tunnels.map((t) => ` ${t.name}`)] : []]
|
|
7003
|
-
});
|
|
7004
|
-
}
|
|
7005
|
-
return name;
|
|
7006
|
-
}
|
|
7007
|
-
async function printSshList() {
|
|
7008
|
-
const tunnels = getSshTunnels();
|
|
7009
|
-
console.log("");
|
|
7010
|
-
warnLegacySshConfigFiles();
|
|
7011
|
-
if (tunnels.length === 0) {
|
|
7012
|
-
console.log("\u6CA1\u6709\u914D\u7F6E\u96A7\u9053");
|
|
7013
|
-
console.log("");
|
|
7014
|
-
console.log("\u6DFB\u52A0\u96A7\u9053: mihomo ssh add <\u540D\u5B57> --host <ssh\u4E3B\u673A> --port <\u7AEF\u53E3>");
|
|
7015
|
-
console.log(colors.gray(" \u4F8B\u5982: mihomo ssh add work --host m4 --port 1080"));
|
|
7016
|
-
console.log(colors.gray(" \u96A7\u9053\u628A\u5185\u7F51\u51FA\u53E3\u66B4\u9732\u4E3A\u672C\u5730 SOCKS5\uFF0C\u8282\u70B9\u4E0E\u5206\u6D41\u89C4\u5219\u5199\u8FDB overwrite.yaml"));
|
|
7017
|
-
console.log("");
|
|
7018
|
-
return;
|
|
7019
|
-
}
|
|
7020
|
-
const statuses = await getAllSshStatus();
|
|
7021
|
-
console.log(colors.cyan("\u96A7\u9053\u5217\u8868:"));
|
|
7022
|
-
console.log("");
|
|
7023
|
-
for (const status of statuses) {
|
|
7024
|
-
const { config } = status;
|
|
7025
|
-
const autoLabel = config.auto ? colors.gray(" [auto]") : "";
|
|
7026
|
-
console.log(` ${colors.bold(config.name)}${autoLabel}`);
|
|
7027
|
-
console.log(` ${colors.gray("\u51FA\u53E3: ")}${config.host} \u2192 127.0.0.1:${config.port}`);
|
|
7028
|
-
console.log(` ${colors.gray("\u72B6\u6001: ")}${formatState(status)}`);
|
|
7029
|
-
if (status.started_by) {
|
|
7030
|
-
console.log(` ${colors.gray("\u6765\u6E90: ")}${status.started_by === "auto" ? "\u968F start \u62C9\u8D77" : "\u624B\u52A8\u542F\u52A8"}`);
|
|
7031
|
-
}
|
|
7032
|
-
}
|
|
7033
|
-
console.log("");
|
|
7034
|
-
console.log("\u542F\u52A8: mihomo ssh up [\u540D\u5B57] \u505C\u6B62: mihomo ssh down [\u540D\u5B57]");
|
|
7035
|
-
console.log("\u72B6\u6001: mihomo ssh status \u5220\u9664: mihomo ssh rm <\u540D\u5B57>");
|
|
7036
|
-
console.log("");
|
|
7037
|
-
}
|
|
7038
|
-
function sshAdd(args) {
|
|
7039
|
-
const name = requireSshNameArg(args, "\u7528\u6CD5: mihomo ssh add <\u540D\u5B57> --host <ssh\u4E3B\u673A> --port <\u7AEF\u53E3> [--no-auto]");
|
|
7040
|
-
const host = parseStringArg(args, "--host");
|
|
7041
|
-
if (!host) {
|
|
7042
|
-
throw new CliError("\u7F3A\u5C11 --host", {
|
|
7043
|
-
hint: ["\u7528\u6CD5: mihomo ssh add <\u540D\u5B57> --host <ssh\u4E3B\u673A> --port <\u7AEF\u53E3>", "", "\u4E3B\u673A\u53EF\u7528 ~/.ssh/config \u91CC\u7684\u522B\u540D\uFF0C\u4F8B\u5982 --host m4"]
|
|
7044
|
-
});
|
|
7045
|
-
}
|
|
7046
|
-
const portRaw = parseStringArg(args, "--port");
|
|
7047
|
-
if (!portRaw) {
|
|
7048
|
-
throw new CliError("\u7F3A\u5C11 --port", { hint: ["\u7528\u6CD5: mihomo ssh add <\u540D\u5B57> --host <ssh\u4E3B\u673A> --port <\u7AEF\u53E3>"] });
|
|
7049
|
-
}
|
|
7050
|
-
if (!/^\d+$/.test(portRaw.trim())) {
|
|
7051
|
-
throw new CliError(`\u7AEF\u53E3\u65E0\u6548: "${portRaw}"\uFF0C\u9700\u4E3A 1-65535 \u7684\u6574\u6570`);
|
|
7052
|
-
}
|
|
7053
|
-
const port = Number(portRaw.trim());
|
|
7054
|
-
const auto = !hasFlag(args, "--no-auto");
|
|
7055
|
-
const config = { name, host, port, auto };
|
|
7056
|
-
addSshTunnel(config);
|
|
7057
|
-
console.log(`${colors.green("\u5DF2\u6DFB\u52A0\u96A7\u9053")} ${name} \xB7 ${host} \u2192 127.0.0.1:${port}${auto ? " \xB7 auto" : ""}`);
|
|
7058
|
-
console.log("");
|
|
7059
|
-
printOverwriteSnippet(config);
|
|
7060
|
-
console.log("");
|
|
7061
|
-
console.log(`\u542F\u52A8\u96A7\u9053: mihomo ssh up ${name}`);
|
|
7062
|
-
}
|
|
7063
|
-
async function sshUp(args) {
|
|
7064
|
-
const name = getSshNameArg(args);
|
|
7065
|
-
const targets = name ? [resolveSshTunnel(name)] : getSshTunnels();
|
|
7066
|
-
if (targets.length === 0) {
|
|
7067
|
-
throw new CliError("\u6CA1\u6709\u914D\u7F6E\u96A7\u9053", { hint: ["\u6DFB\u52A0\u96A7\u9053: mihomo ssh add <\u540D\u5B57> --host <ssh\u4E3B\u673A> --port <\u7AEF\u53E3>"] });
|
|
7068
|
-
}
|
|
7069
|
-
for (const config of targets) {
|
|
7070
|
-
const result = await startSshTunnel(config.name, { startedBy: "manual" });
|
|
7071
|
-
if (result.alreadyRunning) {
|
|
7072
|
-
console.log(`${colors.gray("\u5DF2\u5728\u8FD0\u884C")} ${config.name} (PID ${result.pid})`);
|
|
7073
|
-
} else {
|
|
7074
|
-
console.log(`${colors.green("\u5DF2\u542F\u52A8\u96A7\u9053")} ${config.name} \xB7 ${config.host} \u2192 127.0.0.1:${config.port} (PID ${result.pid})`);
|
|
7075
|
-
}
|
|
7076
|
-
}
|
|
7077
|
-
}
|
|
7078
|
-
async function sshDown(args) {
|
|
7079
|
-
const name = getSshNameArg(args);
|
|
7080
|
-
const targets = name ? [resolveSshTunnel(name)] : getSshTunnels();
|
|
7081
|
-
if (targets.length === 0) {
|
|
7082
|
-
throw new CliError("\u6CA1\u6709\u914D\u7F6E\u96A7\u9053");
|
|
7083
|
-
}
|
|
7084
|
-
let stopped = 0;
|
|
7085
|
-
for (const config of targets) {
|
|
7086
|
-
const result = await stopSshTunnel(config.name);
|
|
7087
|
-
if (result.notRunning) {
|
|
7088
|
-
console.log(`${colors.yellow("\u4E0D\u5728\u8FD0\u884C")} ${config.name}`);
|
|
7089
|
-
} else {
|
|
7090
|
-
console.log(`${colors.green("\u5DF2\u505C\u6B62\u96A7\u9053")} ${config.name} (PID ${result.pid})`);
|
|
7091
|
-
stopped++;
|
|
7092
|
-
}
|
|
7093
|
-
}
|
|
7094
|
-
if (stopped > 0) {
|
|
7095
|
-
console.log("");
|
|
7096
|
-
console.log(colors.gray("\u6CE8\u610F: overwrite.yaml \u91CC\u6307\u5411\u8BE5\u7AEF\u53E3\u7684\u8282\u70B9\u73B0\u5728\u8FDE\u4E0D\u901A"));
|
|
7097
|
-
}
|
|
7098
|
-
}
|
|
7099
|
-
async function sshStatus(args) {
|
|
7100
|
-
const name = getSshNameArg(args);
|
|
7101
|
-
if (!name) {
|
|
7102
|
-
await printSshList();
|
|
7103
|
-
return;
|
|
7104
|
-
}
|
|
7105
|
-
const config = resolveSshTunnel(name);
|
|
7106
|
-
const status = await getSshStatus(config);
|
|
7107
|
-
console.log("");
|
|
7108
|
-
console.log(`${colors.gray("\u96A7\u9053: ")}${config.name}${config.auto ? colors.gray(" [auto]") : ""}`);
|
|
7109
|
-
console.log(`${colors.gray("\u51FA\u53E3: ")}${config.host} \u2192 127.0.0.1:${config.port}`);
|
|
7110
|
-
console.log(`${colors.gray("\u72B6\u6001: ")}${formatState(status)}`);
|
|
7111
|
-
if (status.started_by) {
|
|
7112
|
-
console.log(`${colors.gray("\u6765\u6E90: ")}${status.started_by === "auto" ? "\u968F start \u62C9\u8D77" : "\u624B\u52A8\u542F\u52A8"}`);
|
|
7113
|
-
}
|
|
7114
|
-
console.log("");
|
|
7115
|
-
if (status.state === "dead-port") {
|
|
7116
|
-
console.log(colors.yellow("ssh \u8FDB\u7A0B\u8FD8\u5728\uFF0C\u4F46\u7AEF\u53E3\u6CA1\u6709\u76D1\u542C\u2014\u2014\u5185\u7F51\u5206\u6D41\u5F53\u524D\u4E0D\u53EF\u7528"));
|
|
7117
|
-
console.log(`\u91CD\u542F\u96A7\u9053: mihomo ssh down ${config.name} && mihomo ssh up ${config.name}`);
|
|
7118
|
-
console.log(colors.gray(` \u65E5\u5FD7: ${getSshLogPath(config.name)}`));
|
|
7119
|
-
console.log("");
|
|
7120
|
-
} else if (status.state === "stopped") {
|
|
7121
|
-
console.log(`\u542F\u52A8\u96A7\u9053: mihomo ssh up ${config.name}`);
|
|
7122
|
-
console.log("");
|
|
7123
|
-
}
|
|
7124
|
-
}
|
|
7125
|
-
async function sshRemove(args) {
|
|
7126
|
-
const name = requireSshNameArg(args, "\u7528\u6CD5: mihomo ssh rm <\u540D\u5B57> [-y]");
|
|
7127
|
-
const config = resolveSshTunnel(name);
|
|
7128
|
-
const skipConfirm = hasFlag(args, "-y", "--yes");
|
|
7129
|
-
if (!skipConfirm) {
|
|
7130
|
-
if (!await confirmOrThrow(`\u786E\u8BA4\u5220\u9664\u96A7\u9053 "${config.name}"?`, {
|
|
7131
|
-
nonTtyMessage: "\u5220\u9664\u96A7\u9053\u9700\u8981\u786E\u8BA4",
|
|
7132
|
-
hint: [`\u8DF3\u8FC7\u786E\u8BA4: mihomo ssh rm ${config.name} -y`]
|
|
7133
|
-
})) {
|
|
7134
|
-
console.log("\u5DF2\u53D6\u6D88");
|
|
7135
|
-
return;
|
|
7136
|
-
}
|
|
7137
|
-
}
|
|
7138
|
-
await stopSshTunnel(config.name);
|
|
7139
|
-
removeSshTunnel(config.name);
|
|
7140
|
-
console.log(`${colors.green("\u5DF2\u5220\u9664\u96A7\u9053")} ${config.name}`);
|
|
7141
|
-
console.log("");
|
|
7142
|
-
console.log(colors.gray(`\u5982 overwrite.yaml \u91CC\u5199\u8FC7\u6307\u5411 127.0.0.1:${config.port} \u7684\u8282\u70B9\uFF0C\u8BF7\u81EA\u884C\u79FB\u9664`));
|
|
7143
|
-
}
|
|
7144
|
-
function resolveSshTunnel(name) {
|
|
7145
|
-
const config = findSshTunnel(name);
|
|
7146
|
-
if (!config) {
|
|
7147
|
-
const tunnels = getSshTunnels();
|
|
7148
|
-
const suggestion = suggestSimilar(
|
|
7149
|
-
name,
|
|
7150
|
-
tunnels.map((t) => t.name)
|
|
7151
|
-
);
|
|
7152
|
-
throw new CliError(`\u672A\u627E\u5230\u96A7\u9053 "${name}"`, {
|
|
7153
|
-
hint: [
|
|
7154
|
-
...suggestion.length > 0 ? [`\u662F\u5426\u60F3\u8F93\u5165: ${suggestion.join(" / ")}?`] : [],
|
|
7155
|
-
...tunnels.length > 0 ? ["", "\u53EF\u7528\u96A7\u9053:", ...tunnels.map((t) => ` ${t.name}`)] : ["", "\u6DFB\u52A0\u96A7\u9053: mihomo ssh add <\u540D\u5B57> --host <ssh\u4E3B\u673A> --port <\u7AEF\u53E3>"]
|
|
7156
|
-
]
|
|
7157
|
-
});
|
|
7158
|
-
}
|
|
7159
|
-
return config;
|
|
7160
|
-
}
|
|
7161
|
-
var SUBCOMMANDS4 = [
|
|
7162
|
-
{ name: "add", handler: sshAdd },
|
|
7163
|
-
{ name: "up", aliases: ["start"], handler: sshUp },
|
|
7164
|
-
{ name: "down", aliases: ["stop"], handler: sshDown },
|
|
7165
|
-
{ name: "status", handler: sshStatus },
|
|
7166
|
-
{ name: "remove", aliases: ["rm", "delete"], handler: sshRemove }
|
|
7167
|
-
];
|
|
7168
|
-
async function cmdSsh(args) {
|
|
7169
|
-
await dispatchSubcommand(args, SUBCOMMANDS4, {
|
|
7170
|
-
// 无 action → 列表;未知 action → 报错(不静默回落,否则 `ssh upp` 会看似成功)
|
|
7171
|
-
fallback: printSshList,
|
|
7172
|
-
onUnknown: (action) => {
|
|
7173
|
-
const names = SUBCOMMANDS4.flatMap((c) => [c.name, ...c.aliases ?? []]);
|
|
7174
|
-
const suggestion = suggestSimilar(action, names);
|
|
7175
|
-
throw new CliError(`\u672A\u77E5\u7684 ssh \u5B50\u547D\u4EE4: ${action}`, {
|
|
7176
|
-
hint: [...suggestion.length > 0 ? [`\u662F\u5426\u60F3\u8F93\u5165: ${suggestion.join(" / ")}?`] : [], "", "\u53EF\u7528\u5B50\u547D\u4EE4: add, up, down, status, rm\uFF08\u88F8 ssh \u5373\u5217\u8868\uFF09"]
|
|
7177
|
-
});
|
|
7178
|
-
}
|
|
7179
|
-
});
|
|
7180
|
-
}
|
|
7181
|
-
|
|
7182
6542
|
// src/commands/subscription.ts
|
|
7183
6543
|
function printRestartHintIfRunning() {
|
|
7184
6544
|
if (getRunningState().running) {
|
|
@@ -7342,18 +6702,18 @@ async function subRemove(args) {
|
|
|
7342
6702
|
console.log("");
|
|
7343
6703
|
printSubscriptionList();
|
|
7344
6704
|
}
|
|
7345
|
-
var
|
|
6705
|
+
var SUBCOMMANDS4 = [
|
|
7346
6706
|
{ name: "add", handler: subAdd },
|
|
7347
6707
|
{ name: "update", handler: subUpdate },
|
|
7348
6708
|
{ name: "use", handler: subUse },
|
|
7349
6709
|
{ name: "remove", aliases: ["rm", "delete"], handler: subRemove }
|
|
7350
6710
|
];
|
|
7351
6711
|
async function cmdSubscription(args) {
|
|
7352
|
-
await dispatchSubcommand(args,
|
|
6712
|
+
await dispatchSubcommand(args, SUBCOMMANDS4, {
|
|
7353
6713
|
// 无子命令 → 列表;未知子命令 → 报错
|
|
7354
6714
|
fallback: printSubscriptionList,
|
|
7355
6715
|
onUnknown: (action) => {
|
|
7356
|
-
const names =
|
|
6716
|
+
const names = SUBCOMMANDS4.flatMap((c) => [c.name, ...c.aliases ?? []]);
|
|
7357
6717
|
const suggestion = suggestSimilar(action, names);
|
|
7358
6718
|
throw new CliError(`\u672A\u77E5\u7684\u8BA2\u9605\u547D\u4EE4: ${action}`, {
|
|
7359
6719
|
hint: [...suggestion.length > 0 ? [`\u662F\u5426\u60F3\u8F93\u5165: ${suggestion.join(" / ")}?`] : [], "\u7528\u6CD5: mihomo sub [use|add|update|remove]\uFF08\u88F8 sub \u5373\u5217\u8868\uFF09"]
|
|
@@ -7386,7 +6746,7 @@ function cmdUI(args) {
|
|
|
7386
6746
|
}
|
|
7387
6747
|
|
|
7388
6748
|
// src/commands/update.ts
|
|
7389
|
-
import { execFile, spawn as
|
|
6749
|
+
import { execFile, spawn as spawn3 } from "child_process";
|
|
7390
6750
|
import { promisify } from "util";
|
|
7391
6751
|
var execFileAsync = promisify(execFile);
|
|
7392
6752
|
var NPM_VIEW_TIMEOUT_MS = 15e3;
|
|
@@ -7417,7 +6777,7 @@ async function cmdUpdate() {
|
|
|
7417
6777
|
console.log("\u6B63\u5728\u66F4\u65B0 mihomo-cli...");
|
|
7418
6778
|
console.log("");
|
|
7419
6779
|
await new Promise((resolve, reject) => {
|
|
7420
|
-
const npm =
|
|
6780
|
+
const npm = spawn3("npm", ["install", "-g", PKG_NAME], { stdio: "inherit" });
|
|
7421
6781
|
npm.on("close", (code) => {
|
|
7422
6782
|
if (code === 0) {
|
|
7423
6783
|
resolve();
|
|
@@ -7454,7 +6814,7 @@ var COMMANDS = [
|
|
|
7454
6814
|
aliases: ["up"],
|
|
7455
6815
|
handler: cmdStart,
|
|
7456
6816
|
group: "control",
|
|
7457
|
-
usage: ["start [tun|mixed] [-s] [-u ms]
|
|
6817
|
+
usage: ["start [tun|mixed] [-s] [-u ms] \u542F\u52A8/\u5207\u6362\u4EE3\u7406 (\u9ED8\u8BA4 mixed)"]
|
|
7458
6818
|
},
|
|
7459
6819
|
{
|
|
7460
6820
|
name: "tun",
|
|
@@ -7469,7 +6829,7 @@ var COMMANDS = [
|
|
|
7469
6829
|
aliases: ["down"],
|
|
7470
6830
|
handler: cmdStop,
|
|
7471
6831
|
group: "control",
|
|
7472
|
-
usage: ["stop
|
|
6832
|
+
usage: ["stop \u505C\u6B62\u4EE3\u7406"]
|
|
7473
6833
|
},
|
|
7474
6834
|
{
|
|
7475
6835
|
name: "status",
|
|
@@ -7546,21 +6906,6 @@ var COMMANDS = [
|
|
|
7546
6906
|
group: "system",
|
|
7547
6907
|
usage: ["daemon [on|off] \u5F00\u673A\u81EA\u542F + \u5D29\u6E83\u91CD\u542F\uFF08\u4EC5 Mixed\uFF0C\u9700\u7BA1\u7406\u5458\u5BC6\u7801\uFF09\uFF1B\u65E0\u53C2\u770B\u72B6\u6001"]
|
|
7548
6908
|
},
|
|
7549
|
-
{
|
|
7550
|
-
// 无别名:`tun` 被 TUN 模式快捷命令占用,`ssh` 本身已够短。
|
|
7551
|
-
// (注册表重复 token 会在模块加载期直接抛错)
|
|
7552
|
-
name: "ssh",
|
|
7553
|
-
aliases: [],
|
|
7554
|
-
handler: cmdSsh,
|
|
7555
|
-
group: "system",
|
|
7556
|
-
usage: [
|
|
7557
|
-
"ssh \u5217\u51FA ssh \u96A7\u9053\uFF08\u672C\u5730 SOCKS5 \u7AEF\u53E3\uFF09",
|
|
7558
|
-
"ssh add <\u540D\u5B57> --host <\u4E3B\u673A> --port <\u7AEF\u53E3> [--no-auto]",
|
|
7559
|
-
"ssh up|down [\u540D\u5B57] \u542F\u52A8/\u505C\u6B62\u96A7\u9053\uFF08\u65E0\u53C2\u5373\u5168\u90E8\uFF09",
|
|
7560
|
-
"ssh status [\u540D\u5B57] \u67E5\u770B\u96A7\u9053\u72B6\u6001\uFF08\u771F\u5B9E\u63A2\u6D4B\u7AEF\u53E3\uFF09",
|
|
7561
|
-
"ssh rm <\u540D\u5B57> [-y] \u5220\u9664\u96A7\u9053"
|
|
7562
|
-
]
|
|
7563
|
-
},
|
|
7564
6909
|
{
|
|
7565
6910
|
name: "update",
|
|
7566
6911
|
aliases: [],
|