mihomo-cli 4.1.0 → 4.2.1
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 +38 -0
- package/README.md +20 -2
- package/dist/index.js +243 -169
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,43 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [4.2.1] - 2026-09-05
|
|
4
|
+
|
|
5
|
+
修一个让 `stop` 完全失效的高危缺陷。实测复现并回归验证。
|
|
6
|
+
|
|
7
|
+
### 修复
|
|
8
|
+
|
|
9
|
+
- **`mihomo stop` 停不掉内核,却打印「不在运行」并退出 0**。内核照常在跑、代理照常生效,只是自启被关掉了——下次开机才「看起来正常」。同一个缺陷还让 `status` 把运行中的内核报成未运行、`start` 看不见需要清理的残留、`reset` 漏掉进程清理。
|
|
10
|
+
|
|
11
|
+
根因在识别内核进程的正则:它用了 JS 的非捕获组 `(?:a|b)`,而 `pgrep -f` / `pkill -f` 走 POSIX ERE(`regcomp(REG_EXTENDED)`),ERE 里 `(` 后紧跟 `?` 是语法错误。实测 `pgrep` 直接报 `Cannot compile regular expression ... (repetition-operator operand invalid)` 并以退出码 2 结束、不输出任何 PID,而 `getMihomoPids()` 把这个失败吞成了「没有进程」。`pkill` 同样编译失败,一个进程都不杀却照常返回。于是整条停止链路在「什么都没做」的情况下报告成功。
|
|
12
|
+
|
|
13
|
+
该分支是 v4.2.0 引入的(服务经符号链 `mihomo-cli-service` 启动,需同时匹配符号链与真实二进制两种命令行),此前的 pattern 无分组,不受影响。
|
|
14
|
+
|
|
15
|
+
除改用 ERE 的 `(a|b)` 外,还堵上了让它能潜伏下来的那个洞:`getMihomoPids` 现在只接受 `pgrep` 的退出码 0(有匹配)与 1(无匹配),其余一律报错——**探测失败不能再伪装成「没有进程」**。回归测试直接调用真实 `pgrep` 编译该 pattern,任何 JS-only 的正则语法都会被当场拦下。
|
|
16
|
+
|
|
17
|
+
## [4.2.0] - 2026-09-05
|
|
18
|
+
|
|
19
|
+
修两个「报告成功但其实没做到」的高危缺陷,均实测复现并回归验证。
|
|
20
|
+
|
|
21
|
+
### 修复
|
|
22
|
+
|
|
23
|
+
- **`start` 会把崩溃循环报成「已启动」**。内核因配置问题(端口占用、订阅里有内核不接受的字段)启动后立即退出时,`start` 打印「已启动 (PID xxx)」并退出 0,而 `KeepAlive` 正每隔约 10 秒把它反复拉起——用户以为代理开着,实际完全没有代理,日志被崩溃信息刷爆。更别扭的是几秒后 `status` 会显示「不在运行」,同一个状态两条命令给出相反答案。
|
|
24
|
+
|
|
25
|
+
根因是 `launchctl bootstrap` 成功只代表**任务被装载**,不代表进程活着,而此前的实现固定 `sleep 500ms` 后取一次 pid 就认定成功。现在 `start` 会观察一个窗口确认内核稳定运行,失败则报错、退出码非 0,并直接附上日志尾部(TUN 路径本就 `tail -25`,服务路径此前什么都不给)。
|
|
26
|
+
|
|
27
|
+
判据用 `last exit code` 而非 `runs`:`KeepAlive` 有约 10 秒重启节流,崩溃后 2 秒内 `runs` 仍是 1。实测还发现全新 `bootstrap` 后存在一段**假健康窗口**(`state = running` 且 pid 拿得到,进程其实马上要退出),长度不固定——同一台机器上量到过 180ms 与 540ms,故健康判定不能一看到 running 就收口。
|
|
28
|
+
|
|
29
|
+
- **服务模式下日志永不轮转**。`rotateAndCleanupLogs()` 只在 TUN 启动路径被调用,而 Mixed(默认模式)走 launchd,于是 `mihomo.log` 无限增长、`logs` 的归档列表恒为空——README 承诺的「自动轮转,保留 7 天」对默认模式根本不成立。唯一的兜底是日志超 10MB 时借重启做 copy-truncate。
|
|
30
|
+
|
|
31
|
+
现在 `startService` 在 `bootout` 与 `bootstrap` 之间轮转日志。必须卡在这个窗口:运行中 rename 是无效的,launchd 的 `StandardOutPath` fd 指向旧 inode,改名后内核会继续往归档文件里写。
|
|
32
|
+
|
|
33
|
+
- **`status` 无法区分「用户主动停止」与「崩溃循环」**:两者都显示「不在运行」。现在检出内核上次非 0 退出时会额外提示,并给出排查与止损命令。
|
|
34
|
+
|
|
35
|
+
### 改进
|
|
36
|
+
|
|
37
|
+
- `help` 的说明列改为按最长命令签名自动对齐。此前靠手写空格,实测三段错位(控制组落在第 34 列、其余组第 30 列,`subscription add <url> [name]` 直接溢出)。对齐宽度按显示宽度计算,含中文占位符(`[编号]`、`[镜像]`、`[目标...]`)的签名不再少缩进
|
|
38
|
+
- 删除无消费者的死代码:`registerCleanup`/`runCleanup` 退出清理注册表(v4.1.0 移除 detached spawn 后已恒空转,留着会让人误以为信号安全网仍在生效)、`hasRootResidue`、`sleepSync`、`ProcessStatus` 的 `allProcesses`/`hasStaleProcesses`、`StartResult.alreadyRunning`、`downloadSubscription` 的 `persist` 参数
|
|
39
|
+
- 文档整理:`CLAUDE.md` 新增「报告成功前必须确认事情真的成立」一节收敛这类缺陷的共性,launchd 实测事实补录本轮四条新结论;`CODE_REVIEW.md` 从两轮历史修复清单(含已删除功能的条目)重写为当前基线的未处理项与已验证结论
|
|
40
|
+
|
|
3
41
|
## [4.1.0] - 2026-09-05
|
|
4
42
|
|
|
5
43
|
`daemon` 保活重构为 install/start/stop 服务模型,日常操作全程免密。
|
package/README.md
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
- 🛡️ **服务托管** - 基于 launchd,崩溃/登录自动拉起,代理后台常驻;日常 `start`/`stop` **全程免密**
|
|
16
16
|
- 🔄 **双模式支持** - Mixed 模式和 TUN 透明代理模式
|
|
17
17
|
- 📊 **状态监控** - 查看运行状态、内存占用、订阅流量与到期时间
|
|
18
|
-
- 📝 **日志管理** -
|
|
18
|
+
- 📝 **日志管理** - 每次启动归档上一次日志,保留 7 天,支持列表/跟随/编号查看
|
|
19
19
|
- 🎨 **Web UI** - 一键打开 Web 控制面板 (zash/metacubexd/yacd)
|
|
20
20
|
- 🔄 **内核更新** - 自动检查更新,支持 GitHub 镜像加速
|
|
21
21
|
- 💡 **容错提示** - 命令/子命令拼错时给出 did-you-mean 纠错建议
|
|
@@ -209,6 +209,7 @@ mihomo status # 查看状态
|
|
|
209
209
|
- **`RunAtLoad`** — 登录后自动启动
|
|
210
210
|
- 常驻的是系统 launchd 进程本身,**不额外占用资源、无轮询**
|
|
211
211
|
- 切换订阅、开关覆写后的重启**优先走内核热重载**,免密且不中断连接
|
|
212
|
+
- `start` 会确认内核**真的跑起来了**才报「已启动」:坏配置下内核启动即退出时报错并附日志尾部,而不是报成功后让 `KeepAlive` 静默地反复重试
|
|
212
213
|
- 「系统设置 → 通用 → 登录项与扩展」中显示为 **`mihomo-cli-service`**(plist 指向一个同名符号链,否则那里只会显示一个没有上下文的 `mihomo`)
|
|
213
214
|
|
|
214
215
|
### 关于 macOS 本地网络授权
|
|
@@ -245,7 +246,13 @@ mihomo start # 恢复服务
|
|
|
245
246
|
|
|
246
247
|
### 日志
|
|
247
248
|
|
|
248
|
-
|
|
249
|
+
每次 `mihomo start` 会把上一次的 `mihomo.log` 归档为 `mihomo.<时间戳>.log`,归档保留 7 天。运行期间日志持续追加到 `mihomo.log`;若单次运行就写超 10MB,配置变更触发的重启会顺便轮转,不会无限增长。
|
|
250
|
+
|
|
251
|
+
```bash
|
|
252
|
+
mihomo logs # 列出当前日志与归档
|
|
253
|
+
mihomo logs 0 -f # 实时跟随当前日志
|
|
254
|
+
mihomo logs 1 # 查看最新的归档
|
|
255
|
+
```
|
|
249
256
|
|
|
250
257
|
## 内核更新镜像
|
|
251
258
|
|
|
@@ -418,6 +425,17 @@ match:
|
|
|
418
425
|
|
|
419
426
|
## 故障排除
|
|
420
427
|
|
|
428
|
+
### 启动失败
|
|
429
|
+
|
|
430
|
+
`mihomo start` 会确认内核真的跑起来了才报「已启动」——内核因配置问题启动后立即退出时,命令会报错并直接附上日志尾部,退出码非 0。常见原因是端口被占用或订阅里有内核不接受的字段。
|
|
431
|
+
|
|
432
|
+
```bash
|
|
433
|
+
mihomo logs 0 # 看完整原因
|
|
434
|
+
mihomo stop # 止住 launchd 的反复重试
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
若 `status` 显示「不在运行」但带「内核上次异常退出」的提示,说明内核正在崩溃循环中被反复拉起,同样按上面两步处理。
|
|
438
|
+
|
|
421
439
|
### 进程无法停止
|
|
422
440
|
|
|
423
441
|
```bash
|
package/dist/index.js
CHANGED
|
@@ -3740,7 +3740,6 @@ function listOverwriteFile() {
|
|
|
3740
3740
|
}
|
|
3741
3741
|
|
|
3742
3742
|
// src/utils.ts
|
|
3743
|
-
var sleepBuf = new Int32Array(new SharedArrayBuffer(4));
|
|
3744
3743
|
function sleep(ms) {
|
|
3745
3744
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
3746
3745
|
}
|
|
@@ -3750,6 +3749,25 @@ function escapeRegExp(s) {
|
|
|
3750
3749
|
function shellQuote(s) {
|
|
3751
3750
|
return `'${s.replace(/'/g, "'\\''")}'`;
|
|
3752
3751
|
}
|
|
3752
|
+
function displayWidth(s) {
|
|
3753
|
+
let width = 0;
|
|
3754
|
+
for (const ch of s) {
|
|
3755
|
+
const code = ch.codePointAt(0) ?? 0;
|
|
3756
|
+
const isWide = code >= 4352 && code <= 4447 || // 韩文字母
|
|
3757
|
+
code >= 11904 && code <= 42191 || // CJK 部首 … 注音、统一表意文字
|
|
3758
|
+
code >= 44032 && code <= 55203 || // 韩文音节
|
|
3759
|
+
code >= 63744 && code <= 64255 || // CJK 兼容表意文字
|
|
3760
|
+
code >= 65072 && code <= 65135 || // CJK 兼容形式
|
|
3761
|
+
code >= 65280 && code <= 65376 || // 全角字母数字与标点
|
|
3762
|
+
code >= 65504 && code <= 65510;
|
|
3763
|
+
width += isWide ? 2 : 1;
|
|
3764
|
+
}
|
|
3765
|
+
return width;
|
|
3766
|
+
}
|
|
3767
|
+
function padEndDisplay(s, width) {
|
|
3768
|
+
const pad = width - displayWidth(s);
|
|
3769
|
+
return pad > 0 ? s + " ".repeat(pad) : s;
|
|
3770
|
+
}
|
|
3753
3771
|
function formatBytes(bytes) {
|
|
3754
3772
|
if (bytes === void 0 || bytes === null) return "\u672A\u77E5";
|
|
3755
3773
|
const num = Number(bytes);
|
|
@@ -4316,18 +4334,19 @@ var GROUP_TITLES = [
|
|
|
4316
4334
|
function printHelp(commands) {
|
|
4317
4335
|
const lines = [`
|
|
4318
4336
|
${colors.cyan(colors.bold(`mihomo-cli v${VERSION}`))}`, "", "\u547D\u4EE4\u522B\u540D: mihomo, mhm, mh", "", "\u7528\u6CD5:", " mihomo <\u547D\u4EE4> [\u9009\u9879]"];
|
|
4337
|
+
const allUsage = commands.flatMap((c) => c.usage);
|
|
4338
|
+
const signatureWidth = Math.max(...allUsage.map((u) => displayWidth(u.signature)));
|
|
4339
|
+
const formatUsage = (u) => ` ${colors.bold(padEndDisplay(u.signature, signatureWidth))} ${u.description}`;
|
|
4319
4340
|
for (const [group, title] of GROUP_TITLES) {
|
|
4320
4341
|
const usageLines = commands.filter((c) => c.group === group).flatMap((c) => c.usage);
|
|
4321
4342
|
if (usageLines.length === 0) continue;
|
|
4322
4343
|
lines.push("", colors.cyan(title));
|
|
4323
|
-
for (const u of usageLines)
|
|
4324
|
-
lines.push(u.startsWith(" ") ? ` ${u}` : ` ${boldFirstToken(u)}`);
|
|
4325
|
-
}
|
|
4344
|
+
for (const u of usageLines) lines.push(formatUsage(u));
|
|
4326
4345
|
}
|
|
4327
4346
|
const meta = commands.filter((c) => c.group === "meta").flatMap((c) => c.usage);
|
|
4328
4347
|
if (meta.length > 0) {
|
|
4329
4348
|
lines.push("", colors.cyan("\u5143:"));
|
|
4330
|
-
for (const u of meta) lines.push(
|
|
4349
|
+
for (const u of meta) lines.push(formatUsage(u));
|
|
4331
4350
|
}
|
|
4332
4351
|
lines.push(
|
|
4333
4352
|
"",
|
|
@@ -4353,11 +4372,6 @@ ${colors.cyan(colors.bold(`mihomo-cli v${VERSION}`))}`, "", "\u547D\u4EE4\u522B\
|
|
|
4353
4372
|
);
|
|
4354
4373
|
console.log(lines.join("\n"));
|
|
4355
4374
|
}
|
|
4356
|
-
function boldFirstToken(usage) {
|
|
4357
|
-
const spaceIdx = usage.indexOf(" ");
|
|
4358
|
-
if (spaceIdx < 0) return colors.bold(usage);
|
|
4359
|
-
return `${colors.bold(usage.slice(0, spaceIdx))}${usage.slice(spaceIdx)}`;
|
|
4360
|
-
}
|
|
4361
4375
|
function printVersion() {
|
|
4362
4376
|
const kv = getKernelVersion() || "\u672A\u5B89\u88C5";
|
|
4363
4377
|
console.log(colors.cyan(colors.bold(`mihomo-cli v${VERSION}`)));
|
|
@@ -4369,7 +4383,6 @@ function printVersion() {
|
|
|
4369
4383
|
import { spawn } from "child_process";
|
|
4370
4384
|
|
|
4371
4385
|
// src/lifecycle.ts
|
|
4372
|
-
var cleanupFns = /* @__PURE__ */ new Set();
|
|
4373
4386
|
var silentSigint = false;
|
|
4374
4387
|
function setSilentSigint(value) {
|
|
4375
4388
|
silentSigint = value;
|
|
@@ -4377,15 +4390,6 @@ function setSilentSigint(value) {
|
|
|
4377
4390
|
function isSilentSigint() {
|
|
4378
4391
|
return silentSigint;
|
|
4379
4392
|
}
|
|
4380
|
-
function runCleanup() {
|
|
4381
|
-
for (const fn of cleanupFns) {
|
|
4382
|
-
try {
|
|
4383
|
-
fn();
|
|
4384
|
-
} catch {
|
|
4385
|
-
}
|
|
4386
|
-
}
|
|
4387
|
-
cleanupFns.clear();
|
|
4388
|
-
}
|
|
4389
4393
|
|
|
4390
4394
|
// src/open.ts
|
|
4391
4395
|
function openUrl(url) {
|
|
@@ -4433,9 +4437,111 @@ function viewLogWithTail(logPath, options) {
|
|
|
4433
4437
|
// src/commands/shared.ts
|
|
4434
4438
|
import readline from "readline";
|
|
4435
4439
|
|
|
4440
|
+
// src/log-files.ts
|
|
4441
|
+
import fs5 from "fs";
|
|
4442
|
+
import path4 from "path";
|
|
4443
|
+
var DEFAULT_LOG_RETENTION_DAYS = 7;
|
|
4444
|
+
function rotateAndCleanupLogs() {
|
|
4445
|
+
rotateLog();
|
|
4446
|
+
cleanupOldLogs(DEFAULT_LOG_RETENTION_DAYS);
|
|
4447
|
+
}
|
|
4448
|
+
function getLogPath() {
|
|
4449
|
+
return PATHS.logFile;
|
|
4450
|
+
}
|
|
4451
|
+
function readLogTail(maxLines = 15) {
|
|
4452
|
+
const TAIL_BYTES = 64 * 1024;
|
|
4453
|
+
let fd = null;
|
|
4454
|
+
try {
|
|
4455
|
+
const size = fs5.statSync(PATHS.logFile).size;
|
|
4456
|
+
if (size === 0) return [];
|
|
4457
|
+
const start = Math.max(0, size - TAIL_BYTES);
|
|
4458
|
+
const length = size - start;
|
|
4459
|
+
const buf = Buffer.alloc(length);
|
|
4460
|
+
fd = fs5.openSync(PATHS.logFile, "r");
|
|
4461
|
+
fs5.readSync(fd, buf, 0, length, start);
|
|
4462
|
+
return buf.toString("utf8").split("\n").map((l) => l.trimEnd()).filter((l) => l.length > 0).slice(-maxLines);
|
|
4463
|
+
} catch {
|
|
4464
|
+
return [];
|
|
4465
|
+
} finally {
|
|
4466
|
+
if (fd !== null) {
|
|
4467
|
+
try {
|
|
4468
|
+
fs5.closeSync(fd);
|
|
4469
|
+
} catch {
|
|
4470
|
+
}
|
|
4471
|
+
}
|
|
4472
|
+
}
|
|
4473
|
+
}
|
|
4474
|
+
function rotateLog() {
|
|
4475
|
+
const logFile = PATHS.logFile;
|
|
4476
|
+
if (!fs5.existsSync(logFile)) return null;
|
|
4477
|
+
const stat = fs5.statSync(logFile);
|
|
4478
|
+
if (stat.size === 0) return null;
|
|
4479
|
+
const rotatedName = `mihomo.${formatLocalTimestamp()}.log`;
|
|
4480
|
+
const rotatedPath = path4.join(DIRS.logs, rotatedName);
|
|
4481
|
+
fs5.renameSync(logFile, rotatedPath);
|
|
4482
|
+
return rotatedPath;
|
|
4483
|
+
}
|
|
4484
|
+
function cleanupOldLogs(maxAgeDays = DEFAULT_LOG_RETENTION_DAYS) {
|
|
4485
|
+
const logsDir = DIRS.logs;
|
|
4486
|
+
if (!fs5.existsSync(logsDir)) return { deleted: 0, errors: 0 };
|
|
4487
|
+
const files = fs5.readdirSync(logsDir);
|
|
4488
|
+
const now = Date.now();
|
|
4489
|
+
const maxAgeMs = maxAgeDays * 24 * 60 * 60 * 1e3;
|
|
4490
|
+
let deleted = 0;
|
|
4491
|
+
let errors = 0;
|
|
4492
|
+
for (const file of files) {
|
|
4493
|
+
if (!file.match(/^mihomo\.\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}\.log$/)) continue;
|
|
4494
|
+
try {
|
|
4495
|
+
const filePath = path4.join(logsDir, file);
|
|
4496
|
+
const stat = fs5.statSync(filePath);
|
|
4497
|
+
if (now - stat.mtimeMs > maxAgeMs) {
|
|
4498
|
+
fs5.unlinkSync(filePath);
|
|
4499
|
+
deleted++;
|
|
4500
|
+
}
|
|
4501
|
+
} catch {
|
|
4502
|
+
errors++;
|
|
4503
|
+
}
|
|
4504
|
+
}
|
|
4505
|
+
return { deleted, errors };
|
|
4506
|
+
}
|
|
4507
|
+
function listLogs() {
|
|
4508
|
+
const logsDir = DIRS.logs;
|
|
4509
|
+
const result = { current: null, archives: [] };
|
|
4510
|
+
if (fs5.existsSync(PATHS.logFile)) {
|
|
4511
|
+
const stat = fs5.statSync(PATHS.logFile);
|
|
4512
|
+
result.current = {
|
|
4513
|
+
name: "mihomo.log (\u5F53\u524D)",
|
|
4514
|
+
path: PATHS.logFile,
|
|
4515
|
+
size: stat.size,
|
|
4516
|
+
mtime: stat.mtime,
|
|
4517
|
+
isCurrent: true
|
|
4518
|
+
};
|
|
4519
|
+
}
|
|
4520
|
+
if (!fs5.existsSync(logsDir)) return result;
|
|
4521
|
+
const files = fs5.readdirSync(logsDir);
|
|
4522
|
+
for (const file of files) {
|
|
4523
|
+
const match = file.match(/^mihomo\.(\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2})\.log$/);
|
|
4524
|
+
if (!match) continue;
|
|
4525
|
+
try {
|
|
4526
|
+
const filePath = path4.join(logsDir, file);
|
|
4527
|
+
const stat = fs5.statSync(filePath);
|
|
4528
|
+
result.archives.push({
|
|
4529
|
+
name: file,
|
|
4530
|
+
path: filePath,
|
|
4531
|
+
size: stat.size,
|
|
4532
|
+
mtime: stat.mtime,
|
|
4533
|
+
isCurrent: false
|
|
4534
|
+
});
|
|
4535
|
+
} catch {
|
|
4536
|
+
}
|
|
4537
|
+
}
|
|
4538
|
+
result.archives.sort((a, b) => b.mtime.getTime() - a.mtime.getTime());
|
|
4539
|
+
return result;
|
|
4540
|
+
}
|
|
4541
|
+
|
|
4436
4542
|
// src/process-probe.ts
|
|
4437
4543
|
import { spawnSync as spawnSync2 } from "child_process";
|
|
4438
|
-
import
|
|
4544
|
+
import fs6 from "fs";
|
|
4439
4545
|
var PS_TIMEOUT_MS = 5e3;
|
|
4440
4546
|
function isProcessRunning(pid) {
|
|
4441
4547
|
if (!pid) return false;
|
|
@@ -4464,12 +4570,12 @@ function isProcessRoot(pid) {
|
|
|
4464
4570
|
return false;
|
|
4465
4571
|
}
|
|
4466
4572
|
}
|
|
4467
|
-
var BINARY_ALTERNATION = `(
|
|
4573
|
+
var BINARY_ALTERNATION = `(${escapeRegExp(PATHS.serviceBinary)}|${escapeRegExp(PATHS.mihomoBinary)})`;
|
|
4468
4574
|
var MAIN_INSTANCE_PATTERN = `${BINARY_ALTERNATION}.*${escapeRegExp(PATHS.configFile)}`;
|
|
4469
4575
|
function getPid() {
|
|
4470
|
-
if (!
|
|
4576
|
+
if (!fs6.existsSync(PATHS.pidFile)) return null;
|
|
4471
4577
|
try {
|
|
4472
|
-
const pid = parseInt(
|
|
4578
|
+
const pid = parseInt(fs6.readFileSync(PATHS.pidFile, "utf8").trim(), 10);
|
|
4473
4579
|
return pid > 0 ? pid : null;
|
|
4474
4580
|
} catch {
|
|
4475
4581
|
return null;
|
|
@@ -4481,19 +4587,25 @@ function isRunning() {
|
|
|
4481
4587
|
return isProcessRunning(pid) && (isProcessCommandMatching(pid, PATHS.mihomoBinary) || isProcessCommandMatching(pid, PATHS.serviceBinary));
|
|
4482
4588
|
}
|
|
4483
4589
|
function getMihomoPids() {
|
|
4484
|
-
|
|
4485
|
-
|
|
4486
|
-
|
|
4487
|
-
|
|
4488
|
-
|
|
4489
|
-
} catch {
|
|
4490
|
-
return [];
|
|
4590
|
+
const result = spawnSync2("pgrep", ["-f", MAIN_INSTANCE_PATTERN], { encoding: "utf8", timeout: 1e4 });
|
|
4591
|
+
if (result.error || result.status === null) {
|
|
4592
|
+
throw new CliError("\u65E0\u6CD5\u63A2\u6D4B mihomo \u8FDB\u7A0B\uFF08pgrep \u6267\u884C\u5931\u8D25\uFF09", {
|
|
4593
|
+
hint: ["\u8FD9\u4E0D\u4EE3\u8868\u5185\u6838\u672A\u8FD0\u884C\uFF0C\u53EA\u8868\u793A\u67E5\u4E0D\u5230\u3002", "\u8BF7\u624B\u52A8\u786E\u8BA4: pgrep -fl mihomo"]
|
|
4594
|
+
});
|
|
4491
4595
|
}
|
|
4596
|
+
if (result.status !== 0 && result.status !== 1) {
|
|
4597
|
+
throw new CliError(`\u8FDB\u7A0B\u63A2\u6D4B\u5931\u8D25\uFF08pgrep \u9000\u51FA\u7801 ${result.status}\uFF09`, {
|
|
4598
|
+
hint: [(result.stderr || "").trim(), "", "\u8FD9\u662F CLI \u7684\u7F3A\u9677\uFF0C\u8BF7\u53CD\u9988\u3002\u624B\u52A8\u786E\u8BA4\u5185\u6838\u72B6\u6001: pgrep -fl mihomo"].filter(Boolean)
|
|
4599
|
+
});
|
|
4600
|
+
}
|
|
4601
|
+
const output = (result.stdout || "").trim();
|
|
4602
|
+
if (!output) return [];
|
|
4603
|
+
return output.split("\n").filter(Boolean).map((p) => parseInt(p, 10)).filter((p) => Number.isInteger(p) && p > 0);
|
|
4492
4604
|
}
|
|
4493
4605
|
function isPidFileOwnedByRoot() {
|
|
4494
|
-
if (!
|
|
4606
|
+
if (!fs6.existsSync(PATHS.pidFile)) return false;
|
|
4495
4607
|
try {
|
|
4496
|
-
const stat =
|
|
4608
|
+
const stat = fs6.statSync(PATHS.pidFile);
|
|
4497
4609
|
return stat.uid === 0;
|
|
4498
4610
|
} catch {
|
|
4499
4611
|
return false;
|
|
@@ -4529,101 +4641,19 @@ function getProcessInfo(pid) {
|
|
|
4529
4641
|
function getStatus() {
|
|
4530
4642
|
const running = isRunning();
|
|
4531
4643
|
const pid = getPid();
|
|
4532
|
-
const allPids = getMihomoPids();
|
|
4533
4644
|
return {
|
|
4534
4645
|
running,
|
|
4535
4646
|
pid: running ? pid : null,
|
|
4536
4647
|
processInfo: running && pid ? getProcessInfo(pid) : null,
|
|
4537
4648
|
hasConfig: hasConfig(),
|
|
4538
4649
|
hasKernel: hasKernel(),
|
|
4539
|
-
kernelVersion: getKernelVersion()
|
|
4540
|
-
allProcesses: allPids,
|
|
4541
|
-
hasStaleProcesses: allPids.length > (running ? 1 : 0)
|
|
4650
|
+
kernelVersion: getKernelVersion()
|
|
4542
4651
|
};
|
|
4543
4652
|
}
|
|
4544
4653
|
|
|
4545
4654
|
// src/process-start.ts
|
|
4546
4655
|
import fs8 from "fs";
|
|
4547
4656
|
|
|
4548
|
-
// src/log-files.ts
|
|
4549
|
-
import fs6 from "fs";
|
|
4550
|
-
import path4 from "path";
|
|
4551
|
-
var DEFAULT_LOG_RETENTION_DAYS = 7;
|
|
4552
|
-
function rotateAndCleanupLogs() {
|
|
4553
|
-
rotateLog();
|
|
4554
|
-
cleanupOldLogs(DEFAULT_LOG_RETENTION_DAYS);
|
|
4555
|
-
}
|
|
4556
|
-
function getLogPath() {
|
|
4557
|
-
return PATHS.logFile;
|
|
4558
|
-
}
|
|
4559
|
-
function rotateLog() {
|
|
4560
|
-
const logFile = PATHS.logFile;
|
|
4561
|
-
if (!fs6.existsSync(logFile)) return null;
|
|
4562
|
-
const stat = fs6.statSync(logFile);
|
|
4563
|
-
if (stat.size === 0) return null;
|
|
4564
|
-
const rotatedName = `mihomo.${formatLocalTimestamp()}.log`;
|
|
4565
|
-
const rotatedPath = path4.join(DIRS.logs, rotatedName);
|
|
4566
|
-
fs6.renameSync(logFile, rotatedPath);
|
|
4567
|
-
return rotatedPath;
|
|
4568
|
-
}
|
|
4569
|
-
function cleanupOldLogs(maxAgeDays = DEFAULT_LOG_RETENTION_DAYS) {
|
|
4570
|
-
const logsDir = DIRS.logs;
|
|
4571
|
-
if (!fs6.existsSync(logsDir)) return { deleted: 0, errors: 0 };
|
|
4572
|
-
const files = fs6.readdirSync(logsDir);
|
|
4573
|
-
const now = Date.now();
|
|
4574
|
-
const maxAgeMs = maxAgeDays * 24 * 60 * 60 * 1e3;
|
|
4575
|
-
let deleted = 0;
|
|
4576
|
-
let errors = 0;
|
|
4577
|
-
for (const file of files) {
|
|
4578
|
-
if (!file.match(/^mihomo\.\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}\.log$/)) continue;
|
|
4579
|
-
try {
|
|
4580
|
-
const filePath = path4.join(logsDir, file);
|
|
4581
|
-
const stat = fs6.statSync(filePath);
|
|
4582
|
-
if (now - stat.mtimeMs > maxAgeMs) {
|
|
4583
|
-
fs6.unlinkSync(filePath);
|
|
4584
|
-
deleted++;
|
|
4585
|
-
}
|
|
4586
|
-
} catch {
|
|
4587
|
-
errors++;
|
|
4588
|
-
}
|
|
4589
|
-
}
|
|
4590
|
-
return { deleted, errors };
|
|
4591
|
-
}
|
|
4592
|
-
function listLogs() {
|
|
4593
|
-
const logsDir = DIRS.logs;
|
|
4594
|
-
const result = { current: null, archives: [] };
|
|
4595
|
-
if (fs6.existsSync(PATHS.logFile)) {
|
|
4596
|
-
const stat = fs6.statSync(PATHS.logFile);
|
|
4597
|
-
result.current = {
|
|
4598
|
-
name: "mihomo.log (\u5F53\u524D)",
|
|
4599
|
-
path: PATHS.logFile,
|
|
4600
|
-
size: stat.size,
|
|
4601
|
-
mtime: stat.mtime,
|
|
4602
|
-
isCurrent: true
|
|
4603
|
-
};
|
|
4604
|
-
}
|
|
4605
|
-
if (!fs6.existsSync(logsDir)) return result;
|
|
4606
|
-
const files = fs6.readdirSync(logsDir);
|
|
4607
|
-
for (const file of files) {
|
|
4608
|
-
const match = file.match(/^mihomo\.(\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2})\.log$/);
|
|
4609
|
-
if (!match) continue;
|
|
4610
|
-
try {
|
|
4611
|
-
const filePath = path4.join(logsDir, file);
|
|
4612
|
-
const stat = fs6.statSync(filePath);
|
|
4613
|
-
result.archives.push({
|
|
4614
|
-
name: file,
|
|
4615
|
-
path: filePath,
|
|
4616
|
-
size: stat.size,
|
|
4617
|
-
mtime: stat.mtime,
|
|
4618
|
-
isCurrent: false
|
|
4619
|
-
});
|
|
4620
|
-
} catch {
|
|
4621
|
-
}
|
|
4622
|
-
}
|
|
4623
|
-
result.archives.sort((a, b) => b.mtime.getTime() - a.mtime.getTime());
|
|
4624
|
-
return result;
|
|
4625
|
-
}
|
|
4626
|
-
|
|
4627
4657
|
// src/sudo.ts
|
|
4628
4658
|
import { spawnSync as spawnSync3 } from "child_process";
|
|
4629
4659
|
import fs7 from "fs";
|
|
@@ -4757,7 +4787,9 @@ function assertServiceLabelSafe() {
|
|
|
4757
4787
|
}
|
|
4758
4788
|
}
|
|
4759
4789
|
var HOT_RELOAD_TIMEOUT_MS = 5e3;
|
|
4760
|
-
var
|
|
4790
|
+
var SERVICE_HEALTH_INTERVAL_MS = 100;
|
|
4791
|
+
var SERVICE_OBSERVE_MS = 1200;
|
|
4792
|
+
var SERVICE_HEALTH_GRACE_MS = 1800;
|
|
4761
4793
|
var LOG_ROTATE_MAX_BYTES = 10 * 1024 * 1024;
|
|
4762
4794
|
var LAUNCHCTL_TIMEOUT_MS = 5e3;
|
|
4763
4795
|
function serviceTarget() {
|
|
@@ -4776,9 +4808,12 @@ function parseServicePrint(output) {
|
|
|
4776
4808
|
const stateMatch = output.match(/^\tstate = (.+)$/m);
|
|
4777
4809
|
const pidMatch = output.match(/^\tpid = (\d+)$/m);
|
|
4778
4810
|
const pid = pidMatch ? Number.parseInt(pidMatch[1], 10) : null;
|
|
4811
|
+
const exitMatch = output.match(/^\tlast exit code = (\d+)$/m);
|
|
4812
|
+
const lastExitCode = exitMatch ? Number.parseInt(exitMatch[1], 10) : null;
|
|
4779
4813
|
return {
|
|
4780
4814
|
state: stateMatch ? stateMatch[1].trim() : null,
|
|
4781
|
-
pid: pid !== null && Number.isInteger(pid) && pid > 0 ? pid : null
|
|
4815
|
+
pid: pid !== null && Number.isInteger(pid) && pid > 0 ? pid : null,
|
|
4816
|
+
lastExitCode: lastExitCode !== null && Number.isInteger(lastExitCode) ? lastExitCode : null
|
|
4782
4817
|
};
|
|
4783
4818
|
}
|
|
4784
4819
|
function parseDisabledList(output, label) {
|
|
@@ -4801,12 +4836,41 @@ function getServiceStatus() {
|
|
|
4801
4836
|
const print = runLaunchctl(["print", serviceTarget()]);
|
|
4802
4837
|
const loaded = print.status === 0;
|
|
4803
4838
|
if (!installed && !loaded) {
|
|
4804
|
-
return { installed: false, loaded: false, running: false, pid: null, disabled: false };
|
|
4839
|
+
return { installed: false, loaded: false, running: false, pid: null, disabled: false, lastExitCode: null };
|
|
4805
4840
|
}
|
|
4806
|
-
const { state, pid } = loaded ? parseServicePrint(print.stdout) : { state: null, pid: null };
|
|
4841
|
+
const { state, pid, lastExitCode } = loaded ? parseServicePrint(print.stdout) : { state: null, pid: null, lastExitCode: null };
|
|
4807
4842
|
const disabledOut = runLaunchctl(["print-disabled", bootstrapDomain()]);
|
|
4808
4843
|
const disabled = disabledOut.status === 0 ? parseDisabledList(disabledOut.stdout, SERVICE_LABEL) : false;
|
|
4809
|
-
return { installed, loaded, running: state === "running", pid, disabled };
|
|
4844
|
+
return { installed, loaded, running: state === "running", pid, disabled, lastExitCode };
|
|
4845
|
+
}
|
|
4846
|
+
async function waitServiceHealthy() {
|
|
4847
|
+
const deadline = Date.now() + SERVICE_OBSERVE_MS;
|
|
4848
|
+
const graceDeadline = deadline + SERVICE_HEALTH_GRACE_MS;
|
|
4849
|
+
let last = getServiceStatus();
|
|
4850
|
+
while (Date.now() < deadline) {
|
|
4851
|
+
await sleep(SERVICE_HEALTH_INTERVAL_MS);
|
|
4852
|
+
last = getServiceStatus();
|
|
4853
|
+
if (isCrashed(last)) {
|
|
4854
|
+
return { healthy: false, crashed: true, pid: null, exitCode: last.lastExitCode };
|
|
4855
|
+
}
|
|
4856
|
+
if (!last.loaded) {
|
|
4857
|
+
return { healthy: false, crashed: false, pid: null, exitCode: last.lastExitCode };
|
|
4858
|
+
}
|
|
4859
|
+
}
|
|
4860
|
+
if (last.running) return { healthy: true, crashed: false, pid: last.pid, exitCode: null };
|
|
4861
|
+
while (Date.now() < graceDeadline) {
|
|
4862
|
+
await sleep(SERVICE_HEALTH_INTERVAL_MS);
|
|
4863
|
+
last = getServiceStatus();
|
|
4864
|
+
if (isCrashed(last)) {
|
|
4865
|
+
return { healthy: false, crashed: true, pid: null, exitCode: last.lastExitCode };
|
|
4866
|
+
}
|
|
4867
|
+
if (!last.loaded) break;
|
|
4868
|
+
if (last.running) return { healthy: true, crashed: false, pid: last.pid, exitCode: null };
|
|
4869
|
+
}
|
|
4870
|
+
return { healthy: false, crashed: false, pid: last.pid, exitCode: last.lastExitCode };
|
|
4871
|
+
}
|
|
4872
|
+
function isCrashed(status) {
|
|
4873
|
+
return !status.running && status.lastExitCode !== null && status.lastExitCode !== 0;
|
|
4810
4874
|
}
|
|
4811
4875
|
function escapeXml(s) {
|
|
4812
4876
|
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
@@ -4954,18 +5018,12 @@ function startService() {
|
|
|
4954
5018
|
cleanupRootResidue();
|
|
4955
5019
|
const target = shellQuote(serviceTarget());
|
|
4956
5020
|
const dest = shellQuote(PATHS.userAgentPlist);
|
|
4957
|
-
runLaunchctlSteps(
|
|
4958
|
-
|
|
4959
|
-
|
|
4960
|
-
|
|
4961
|
-
|
|
4962
|
-
|
|
4963
|
-
],
|
|
4964
|
-
{
|
|
4965
|
-
action: "\u542F\u52A8\u670D\u52A1",
|
|
4966
|
-
codeMessages: { 2: "\u542F\u7528\u670D\u52A1\u5931\u8D25\uFF08launchctl enable\uFF09", 3: "\u88C5\u8F7D\u670D\u52A1\u5931\u8D25\uFF08launchctl bootstrap\uFF09" }
|
|
4967
|
-
}
|
|
4968
|
-
);
|
|
5021
|
+
runLaunchctlSteps([`launchctl bootout ${target} 2>/dev/null || true`, ...waitUnloadedSteps()], { action: "\u5378\u8F7D\u65E7\u670D\u52A1\u5B9E\u4F8B" });
|
|
5022
|
+
rotateAndCleanupLogs();
|
|
5023
|
+
runLaunchctlSteps([`launchctl enable ${target} || exit 2`, `launchctl bootstrap ${bootstrapDomain()} ${dest} || exit 3`], {
|
|
5024
|
+
action: "\u542F\u52A8\u670D\u52A1",
|
|
5025
|
+
codeMessages: { 2: "\u542F\u7528\u670D\u52A1\u5931\u8D25\uFF08launchctl enable\uFF09", 3: "\u88C5\u8F7D\u670D\u52A1\u5931\u8D25\uFF08launchctl bootstrap\uFF09" }
|
|
5026
|
+
});
|
|
4969
5027
|
}
|
|
4970
5028
|
function stopService() {
|
|
4971
5029
|
assertServiceLabelSafe();
|
|
@@ -5039,7 +5097,7 @@ async function restartService() {
|
|
|
5039
5097
|
if (!isServiceInstalled()) {
|
|
5040
5098
|
throw new CliError("\u670D\u52A1\u672A\u5B89\u88C5\uFF0C\u65E0\u6CD5\u91CD\u542F", { hint: "\u5B89\u88C5\u670D\u52A1: mihomo install" });
|
|
5041
5099
|
}
|
|
5042
|
-
if (!logOversized() && await tryHotReload()) return;
|
|
5100
|
+
if (!logOversized() && await tryHotReload()) return { hotReloaded: true };
|
|
5043
5101
|
const target = shellQuote(serviceTarget());
|
|
5044
5102
|
const dest = shellQuote(PATHS.userAgentPlist);
|
|
5045
5103
|
const logFile = shellQuote(PATHS.logFile);
|
|
@@ -5056,6 +5114,7 @@ async function restartService() {
|
|
|
5056
5114
|
{ action: "\u91CD\u542F\u670D\u52A1", codeMessages: { 3: "\u91CD\u542F\u670D\u52A1\u5931\u8D25\uFF08launchctl bootstrap\uFF09" } }
|
|
5057
5115
|
);
|
|
5058
5116
|
cleanupOldLogs();
|
|
5117
|
+
return { hotReloaded: false };
|
|
5059
5118
|
}
|
|
5060
5119
|
|
|
5061
5120
|
// src/runtime.ts
|
|
@@ -5084,12 +5143,27 @@ async function launchOrRestart(mode) {
|
|
|
5084
5143
|
}
|
|
5085
5144
|
const status = getServiceStatus();
|
|
5086
5145
|
if (status.running && !status.disabled) {
|
|
5087
|
-
await restartService();
|
|
5146
|
+
const { hotReloaded } = await restartService();
|
|
5147
|
+
if (hotReloaded) return getServiceStatus().pid;
|
|
5088
5148
|
} else {
|
|
5089
5149
|
startService();
|
|
5090
5150
|
}
|
|
5091
|
-
|
|
5092
|
-
|
|
5151
|
+
return assertServiceHealthy();
|
|
5152
|
+
}
|
|
5153
|
+
async function assertServiceHealthy() {
|
|
5154
|
+
const health = await waitServiceHealthy();
|
|
5155
|
+
if (health.healthy) return health.pid;
|
|
5156
|
+
const tail = readLogTail();
|
|
5157
|
+
const reason = health.crashed ? `\u5185\u6838\u542F\u52A8\u540E\u7ACB\u5373\u9000\u51FA\uFF08\u9000\u51FA\u7801 ${health.exitCode}\uFF09` : "\u5185\u6838\u672A\u80FD\u8FDB\u5165\u8FD0\u884C\u72B6\u6001";
|
|
5158
|
+
throw new CliError(reason, {
|
|
5159
|
+
label: "\u542F\u52A8\u5931\u8D25",
|
|
5160
|
+
hint: [
|
|
5161
|
+
...health.crashed ? ["launchd \u4F1A\u6BCF\u9694\u7EA6 10 \u79D2\u53CD\u590D\u62C9\u8D77\u5B83\uFF0C\u8BF7\u5148\u4FEE\u6B63\u914D\u7F6E\u6216\u6267\u884C mihomo stop\u3002"] : [],
|
|
5162
|
+
...tail.length > 0 ? ["", "--- \u65E5\u5FD7\u5C3E\u90E8 ---", ...tail] : ["", `\u65E5\u5FD7: ${PATHS.logFile}`],
|
|
5163
|
+
"",
|
|
5164
|
+
"\u5B8C\u6574\u65E5\u5FD7: mihomo logs 0"
|
|
5165
|
+
]
|
|
5166
|
+
});
|
|
5093
5167
|
}
|
|
5094
5168
|
|
|
5095
5169
|
// src/http.ts
|
|
@@ -5297,7 +5371,7 @@ function assertLooksLikeSubscription(parsed, maskedUrl) {
|
|
|
5297
5371
|
]
|
|
5298
5372
|
});
|
|
5299
5373
|
}
|
|
5300
|
-
async function downloadSubscription(url, subName = "default", signal
|
|
5374
|
+
async function downloadSubscription(url, subName = "default", signal) {
|
|
5301
5375
|
let response;
|
|
5302
5376
|
try {
|
|
5303
5377
|
response = await HTTP_CLIENT.get(url, { responseType: "text", signal });
|
|
@@ -5319,13 +5393,9 @@ async function downloadSubscription(url, subName = "default", signal, persist =
|
|
|
5319
5393
|
const parsed = parseYamlOrJson(content, "\u8BA2\u9605\u5185\u5BB9");
|
|
5320
5394
|
if (!parsed) throw new Error("\u8BA2\u9605\u5185\u5BB9\u4E3A\u7A7A");
|
|
5321
5395
|
assertLooksLikeSubscription(parsed, maskUrl(url));
|
|
5322
|
-
|
|
5323
|
-
saveSubscriptionRawConfig(subName, content);
|
|
5324
|
-
}
|
|
5396
|
+
saveSubscriptionRawConfig(subName, content);
|
|
5325
5397
|
const meta = extractSubscriptionMeta(response.headers);
|
|
5326
|
-
|
|
5327
|
-
saveSubscriptionMeta(subName, meta);
|
|
5328
|
-
}
|
|
5398
|
+
saveSubscriptionMeta(subName, meta);
|
|
5329
5399
|
const proxies = parsed.proxies;
|
|
5330
5400
|
const proxyGroups = parsed["proxy-groups"];
|
|
5331
5401
|
return {
|
|
@@ -5449,6 +5519,10 @@ async function printStatus() {
|
|
|
5449
5519
|
}
|
|
5450
5520
|
const statusText = running ? colors.green("\u25CF \u8FD0\u884C\u4E2D") : colors.yellow("\u4E0D\u5728\u8FD0\u884C");
|
|
5451
5521
|
console.log(`${colors.gray("\u72B6\u6001: ")}${statusText}${modeLabel}`);
|
|
5522
|
+
if (!running && service.installed && !service.disabled && service.lastExitCode !== null && service.lastExitCode !== 0) {
|
|
5523
|
+
console.log(colors.yellow(` \u5F02\u5E38: \u5185\u6838\u4E0A\u6B21\u5F02\u5E38\u9000\u51FA\uFF08\u9000\u51FA\u7801 ${service.lastExitCode}\uFF09\uFF0Claunchd \u6B63\u5728\u53CD\u590D\u62C9\u8D77`));
|
|
5524
|
+
console.log(colors.gray(" \u67E5\u770B\u539F\u56E0: mihomo logs 0 \u505C\u6B62\u91CD\u8BD5: mihomo stop"));
|
|
5525
|
+
}
|
|
5452
5526
|
console.log(`${colors.gray("\u5185\u6838: ")}${getKernelVersion() || "\u672A\u5B89\u88C5"}`);
|
|
5453
5527
|
if (pid) {
|
|
5454
5528
|
console.log(`${colors.gray("PID: ")}${pid}`);
|
|
@@ -6914,14 +6988,14 @@ var COMMANDS = [
|
|
|
6914
6988
|
aliases: [],
|
|
6915
6989
|
handler: cmdInstall,
|
|
6916
6990
|
group: "control",
|
|
6917
|
-
usage: ["install
|
|
6991
|
+
usage: [{ signature: "install", description: "\u5B89\u88C5\u670D\u52A1\uFF08Mixed \u6A21\u5F0F\u7684\u524D\u7F6E\uFF0C\u53EA\u9700\u4E00\u6B21\uFF09" }]
|
|
6918
6992
|
},
|
|
6919
6993
|
{
|
|
6920
6994
|
name: "start",
|
|
6921
6995
|
aliases: [],
|
|
6922
6996
|
handler: cmdStart,
|
|
6923
6997
|
group: "control",
|
|
6924
|
-
usage: ["start [tun|mixed] [-s] [-u ms]
|
|
6998
|
+
usage: [{ signature: "start [tun|mixed] [-s] [-u ms]", description: "\u542F\u52A8\u4EE3\u7406\u5E76\u5F00\u542F\u767B\u5F55\u81EA\u542F\uFF08\u9ED8\u8BA4 mixed\uFF09" }]
|
|
6925
6999
|
},
|
|
6926
7000
|
{
|
|
6927
7001
|
name: "tun",
|
|
@@ -6936,21 +7010,21 @@ var COMMANDS = [
|
|
|
6936
7010
|
aliases: [],
|
|
6937
7011
|
handler: cmdStop,
|
|
6938
7012
|
group: "control",
|
|
6939
|
-
usage: ["stop
|
|
7013
|
+
usage: [{ signature: "stop", description: "\u505C\u6B62\u4EE3\u7406\u5E76\u5173\u95ED\u767B\u5F55\u81EA\u542F" }]
|
|
6940
7014
|
},
|
|
6941
7015
|
{
|
|
6942
7016
|
name: "uninstall",
|
|
6943
7017
|
aliases: [],
|
|
6944
7018
|
handler: cmdUninstall,
|
|
6945
7019
|
group: "control",
|
|
6946
|
-
usage: ["uninstall
|
|
7020
|
+
usage: [{ signature: "uninstall", description: "\u5378\u8F7D\u670D\u52A1" }]
|
|
6947
7021
|
},
|
|
6948
7022
|
{
|
|
6949
7023
|
name: "status",
|
|
6950
7024
|
aliases: [],
|
|
6951
7025
|
handler: printStatus,
|
|
6952
7026
|
group: "control",
|
|
6953
|
-
usage: ["status
|
|
7027
|
+
usage: [{ signature: "status", description: "\u67E5\u770B\u72B6\u6001" }]
|
|
6954
7028
|
},
|
|
6955
7029
|
// === 已移除(墓碑:显式报错指引迁移,不在帮助中列出) ===
|
|
6956
7030
|
{
|
|
@@ -6987,7 +7061,7 @@ var COMMANDS = [
|
|
|
6987
7061
|
aliases: [],
|
|
6988
7062
|
handler: cmdUI,
|
|
6989
7063
|
group: "interface",
|
|
6990
|
-
usage: ["ui [zash|dash|yacd]
|
|
7064
|
+
usage: [{ signature: "ui [zash|dash|yacd]", description: "\u6253\u5F00 Web UI\uFF08\u9ED8\u8BA4 zash\uFF09" }]
|
|
6991
7065
|
},
|
|
6992
7066
|
{
|
|
6993
7067
|
// 已并入 `logs -f`;保留为隐藏别名过渡,不在帮助中列出(usage 留空)
|
|
@@ -7003,7 +7077,7 @@ var COMMANDS = [
|
|
|
7003
7077
|
aliases: [],
|
|
7004
7078
|
handler: cmdLogs,
|
|
7005
7079
|
group: "interface",
|
|
7006
|
-
usage: ["logs [-f] [-n N] [\u7F16\u53F7] [-o]
|
|
7080
|
+
usage: [{ signature: "logs [-f] [-n N] [\u7F16\u53F7] [-o]", description: "\u65E5\u5FD7\u5217\u8868/\u67E5\u770B\uFF080=\u5F53\u524D\uFF0C1+=\u5F52\u6863\uFF0C-f \u8DDF\u968F\uFF1B\u7701\u7565\u7F16\u53F7\u5373\u5F53\u524D\uFF09" }]
|
|
7007
7081
|
},
|
|
7008
7082
|
// === 订阅 ===
|
|
7009
7083
|
{
|
|
@@ -7012,11 +7086,11 @@ var COMMANDS = [
|
|
|
7012
7086
|
handler: cmdSubscription,
|
|
7013
7087
|
group: "subscription",
|
|
7014
7088
|
usage: [
|
|
7015
|
-
"subscription
|
|
7016
|
-
"subscription use <name>
|
|
7017
|
-
"subscription add <url> [name]
|
|
7018
|
-
"subscription update [name]
|
|
7019
|
-
"subscription remove <name>
|
|
7089
|
+
{ signature: "subscription", description: "\u5217\u51FA\u6240\u6709\u8BA2\u9605\uFF08\u522B\u540D sub/subs\uFF09" },
|
|
7090
|
+
{ signature: "subscription use <name>", description: "\u5207\u6362\u5F53\u524D\u8BA2\u9605" },
|
|
7091
|
+
{ signature: "subscription add <url> [name]", description: "\u6DFB\u52A0\u8BA2\u9605" },
|
|
7092
|
+
{ signature: "subscription update [name]", description: "\u66F4\u65B0\u8BA2\u9605\uFF08\u65E0\u53C2\u66F4\u65B0\u6240\u6709\uFF09" },
|
|
7093
|
+
{ signature: "subscription remove <name>", description: "\u5220\u9664\u8BA2\u9605\uFF08\u6A21\u7CCA\u5339\u914D\u9700\u786E\u8BA4\uFF0C-y \u8DF3\u8FC7\uFF09" }
|
|
7020
7094
|
]
|
|
7021
7095
|
},
|
|
7022
7096
|
// === 配置 ===
|
|
@@ -7025,14 +7099,20 @@ var COMMANDS = [
|
|
|
7025
7099
|
aliases: ["ow"],
|
|
7026
7100
|
handler: cmdOverwrite,
|
|
7027
7101
|
group: "config",
|
|
7028
|
-
usage: [
|
|
7102
|
+
usage: [
|
|
7103
|
+
{ signature: "overwrite", description: "\u67E5\u770B\u8986\u5199\u72B6\u6001\uFF08\u522B\u540D ow\uFF09" },
|
|
7104
|
+
{ signature: "overwrite on|off", description: "\u542F\u7528/\u7981\u7528\u8986\u5199\u914D\u7F6E" }
|
|
7105
|
+
]
|
|
7029
7106
|
},
|
|
7030
7107
|
{
|
|
7031
7108
|
name: "directory",
|
|
7032
7109
|
aliases: ["dir", "dirs", "directories"],
|
|
7033
7110
|
handler: cmdDirectory,
|
|
7034
7111
|
group: "config",
|
|
7035
|
-
usage: [
|
|
7112
|
+
usage: [
|
|
7113
|
+
{ signature: "directory", description: "\u663E\u793A\u6570\u636E\u76EE\u5F55\u4F4D\u7F6E\uFF08\u522B\u540D dir\uFF09" },
|
|
7114
|
+
{ signature: "directory open [target]", description: "\u6253\u5F00\u76EE\u5F55: root|subs|logs|data|runtime|kernel" }
|
|
7115
|
+
]
|
|
7036
7116
|
},
|
|
7037
7117
|
// === 系统 ===
|
|
7038
7118
|
{
|
|
@@ -7040,21 +7120,21 @@ var COMMANDS = [
|
|
|
7040
7120
|
aliases: [],
|
|
7041
7121
|
handler: cmdKernel,
|
|
7042
7122
|
group: "system",
|
|
7043
|
-
usage: ["kernel [--mirror [\u955C\u50CF]]
|
|
7123
|
+
usage: [{ signature: "kernel [--mirror [\u955C\u50CF]]", description: "\u66F4\u65B0\u5185\u6838\uFF08\u9ED8\u8BA4\u76F4\u8FDE\uFF0C--mirror \u8D70\u955C\u50CF\u4E0B\u8F7D\uFF09" }]
|
|
7044
7124
|
},
|
|
7045
7125
|
{
|
|
7046
7126
|
name: "update",
|
|
7047
7127
|
aliases: [],
|
|
7048
7128
|
handler: cmdUpdate,
|
|
7049
7129
|
group: "system",
|
|
7050
|
-
usage: ["update
|
|
7130
|
+
usage: [{ signature: "update", description: "\u66F4\u65B0 mihomo-cli\uFF08npm install -g\uFF09" }]
|
|
7051
7131
|
},
|
|
7052
7132
|
{
|
|
7053
7133
|
name: "reset",
|
|
7054
7134
|
aliases: [],
|
|
7055
7135
|
handler: cmdReset,
|
|
7056
7136
|
group: "system",
|
|
7057
|
-
usage: ["reset [\u76EE\u6807...] [--full] [-y]
|
|
7137
|
+
usage: [{ signature: "reset [\u76EE\u6807...] [--full] [-y]", description: "\u91CD\u7F6E: \u7559\u7A7A\u4FDD\u7559\u8BBE\u7F6E/\u5185\u6838/\u8986\u5199\uFF0C\u6307\u5B9A\u76EE\u6807\u5220\u5BF9\u5E94\u9879\uFF0C--full \u5220\u5168\u90E8\uFF0C-y \u8DF3\u8FC7\u786E\u8BA4" }]
|
|
7058
7138
|
},
|
|
7059
7139
|
// === meta(不在分组清单展示,help 末尾单列) ===
|
|
7060
7140
|
{
|
|
@@ -7062,14 +7142,14 @@ var COMMANDS = [
|
|
|
7062
7142
|
aliases: ["-h", "--help"],
|
|
7063
7143
|
handler: () => printHelp(COMMANDS),
|
|
7064
7144
|
group: "meta",
|
|
7065
|
-
usage: ["help, -h
|
|
7145
|
+
usage: [{ signature: "help, -h", description: "\u663E\u793A\u5E2E\u52A9" }]
|
|
7066
7146
|
},
|
|
7067
7147
|
{
|
|
7068
7148
|
name: "version",
|
|
7069
7149
|
aliases: ["-v", "--version"],
|
|
7070
7150
|
handler: printVersion,
|
|
7071
7151
|
group: "meta",
|
|
7072
|
-
usage: ["version, -v
|
|
7152
|
+
usage: [{ signature: "version, -v", description: "\u663E\u793A\u7248\u672C" }]
|
|
7073
7153
|
}
|
|
7074
7154
|
];
|
|
7075
7155
|
var COMMAND_INDEX = (() => {
|
|
@@ -7096,11 +7176,9 @@ process.on("SIGINT", () => {
|
|
|
7096
7176
|
if (!isSilentSigint()) {
|
|
7097
7177
|
console.log("\n\u6B63\u5728\u9000\u51FA...");
|
|
7098
7178
|
}
|
|
7099
|
-
runCleanup();
|
|
7100
7179
|
process.exit(130);
|
|
7101
7180
|
});
|
|
7102
7181
|
process.on("SIGTERM", () => {
|
|
7103
|
-
runCleanup();
|
|
7104
7182
|
process.exit(143);
|
|
7105
7183
|
});
|
|
7106
7184
|
process.on("uncaughtException", (e) => {
|
|
@@ -7109,14 +7187,12 @@ process.on("uncaughtException", (e) => {
|
|
|
7109
7187
|
if (e.stack) {
|
|
7110
7188
|
console.error(e.stack.split("\n").slice(1).join("\n"));
|
|
7111
7189
|
}
|
|
7112
|
-
runCleanup();
|
|
7113
7190
|
process.exit(1);
|
|
7114
7191
|
});
|
|
7115
7192
|
process.on("unhandledRejection", (reason) => {
|
|
7116
7193
|
const msg = reason instanceof Error ? reason.message : String(reason);
|
|
7117
7194
|
console.error(`
|
|
7118
7195
|
\u672A\u5904\u7406\u7684 Promise \u62D2\u7EDD: ${msg}`);
|
|
7119
|
-
runCleanup();
|
|
7120
7196
|
process.exit(1);
|
|
7121
7197
|
});
|
|
7122
7198
|
function clearProxyEnv() {
|
|
@@ -7167,13 +7243,11 @@ main().catch((e) => {
|
|
|
7167
7243
|
if (e instanceof CliError) {
|
|
7168
7244
|
console.error(`${colors.red(`${e.label}:`)} ${e.message}`);
|
|
7169
7245
|
for (const line of e.hint) console.error(line);
|
|
7170
|
-
runCleanup();
|
|
7171
7246
|
process.exit(e.exitCode);
|
|
7172
7247
|
}
|
|
7173
7248
|
const err = e;
|
|
7174
7249
|
console.error(`${colors.red("\u9519\u8BEF:")} ${err.message}`);
|
|
7175
7250
|
if (err.stack) console.error(err.stack.split("\n").slice(1).join("\n"));
|
|
7176
|
-
runCleanup();
|
|
7177
7251
|
process.exit(1);
|
|
7178
7252
|
});
|
|
7179
7253
|
/*! Bundled license information:
|