mihomo-cli 26.10.100 → 26.10.101
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 +19 -1
- package/README.md +11 -6
- package/dist/index.js +287 -305
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [26.10.101]
|
|
4
|
+
|
|
5
|
+
### 功能(JS 覆写脚本命中约定)
|
|
6
|
+
|
|
7
|
+
- **脚本 `return true` = 命中当前订阅**:脚本没有 match 声明,status 的「生效/不适用」此前对脚本永远报生效——作用域不中的脚本混在生效清单里,与「match 不命中的 YAML」同一种误读。约定刻意最简:判据过了在函数末尾 `return true`,其余(提前退出、无返回值)一律未命中;严格 `=== true`,返回 config 等对象不算(防「顺手 return」被误读)。判定只供展示、不参与合并闸门(selectActiveOverwriteFiles 不看它):status 文本行与 `--json` 的 `applied` 据此把未命中脚本列进「不适用(脚本未返回 true)」,config 提示段给一行事实性提示(不断言「没改配置」);status 用活跃订阅的缓存正文跑一次真实构建取判定,构建失败按未判定降级(诊断面不崩,D7 同款姿态)。README 脚本示例与契约、CLAUDE.md 不变量、decisions.md D13 尾段同步更新
|
|
8
|
+
|
|
9
|
+
### 修复与评审(命中判定的诊断面收口)
|
|
10
|
+
|
|
11
|
+
- **status 判定改走独立旁路(judgeScriptMatches),不再经 buildConfig**:后者吃 loadOverwriteFile 硬失败门,一个坏 YAML 会把全部脚本的判定打回「未判定=生效」——恰是判定要消灭的误读、且坏文件在场时正是最需要判定的时刻(评审实验复现)。判定复用 listOverwriteFile 同一次读目录的 ok 条目(新 entries 字段),近失文件名的 stderr 警告不再打两遍、YAML 不再解析两次;无脚本的目录直接跳过判定(不为空判定白跑一次订阅解析)。两个降级语义均有回归用例(坏文件在场判定不降级、警告恰好一次),均反向验证
|
|
12
|
+
- **mode 推导判据收敛为 deriveRuntimeMode**(config/status/doctor 三处共用;runtime.ts 的 getRuntimeMode 带服务安装前置条件、不属同一判据):防止三份 `info?.tun ? 'tun' : 'mixed'` 拷贝将来漂移,脚本按 ctx.mode 得到矛盾的生效结论
|
|
13
|
+
- **文档承诺同步**:README「变换函数执行中抛错」条目改为如实描述(status 的判定构建会执行函数体、把执行失败按未判定降级);`ow` 列表的脚本行补 return true 约定提示(用户从 status 的「脚本未返回 true」跳到 ow 排查时能看到改法);types.ts 的 OverwriteTransform 契约注释与类型签名(`void | true`)对齐
|
|
14
|
+
|
|
15
|
+
### 变更(全量质量清理)
|
|
16
|
+
|
|
17
|
+
- **doctor 与日常命令的重复探测收口**:一次体检原先对服务状态跑三遍 launchctl 查询、对同一内核二进制连 spawn 三次 `-v`,现在各查一次(新增 probeKernelVersion,可执行性判据与版本提取共用一份正则,doctor 检查项、内核下载自检、版本对账三处同源);短帮助、热重载、stop 等路径的冗余子进程一并收口,体检更快、口径单点维护
|
|
18
|
+
- **散在各命令的提示与守卫收口**:遗留服务清理的前后提示、杀不掉进程的手动清理命令、「订阅有条目但没有本地配置文件」「未找到内核」等此前各写一份且已漂移出多种说法的提示/报错,统一为单点维护的共享出口;个别场景的提示更完整(uninstall 的遗留清理提示补齐密码说明、订阅配置缺失的指引统一带「更新订阅:」前缀)
|
|
19
|
+
- **类型与死代码清理**:覆写 match 类型收窄为恒数组(加载时已归一,消费侧三处永假的防御分支删除);maskUrl 迁入 text.ts(底层 HTTP 模块不再反向依赖设置模块),其「token 黑名单键不重复」断言原先埋在 try 内会被当畸形输入吞掉、现落成独立测试;内核下载通道的三元判别、服务停止的锁内序列复制、损坏文件备份的双份同构实现等十余处简化,行为不变(全量测试锁定)
|
|
20
|
+
|
|
3
21
|
## [26.10.100] - 2026-10-02
|
|
4
22
|
|
|
5
23
|
### 文档(第十七轮:文档冗余与归档专项)
|
|
@@ -11,7 +29,7 @@
|
|
|
11
29
|
### 重构(第十五轮:过度设计专项审查)
|
|
12
30
|
|
|
13
31
|
- **删四项无论证的复杂/冗余**(其余复杂度逐一核对均有事故背景,未动):①`getLogPath()` 纯转手 `PATHS.logFile`(唯一调用方直写,同文件其余三处本就直用 PATHS);②`maskUrl`→`maskSingleUrl` 的 1:1 转发层内联(多源合并功能 e090618 删除后遗留的空壳,空串守卫经逐值推演冗余——catch 分支原样返回);③service.ts 两个类型 re-export 无任何消费方(拆分「保持导出清单不变」原则下的多余项,typecheck 与全仓 grep 双重确认零消费);④logs 列表逐条「查看:」行删除(尾部六行用法尾注已完整覆盖同一信息,归档 5–10 条时同一提示打 N+1 遍)
|
|
14
|
-
- 自查六维度零过度信号:模块规模分布健康(最大 878 行为核心复杂度所在)、全仓唯一 `opts?` 参数化有测试理由、依赖 2+7 全在用、工具链/CI 与本地管线逐字一致、基础设施模块消费方匹配、types.ts 44
|
|
32
|
+
- 自查六维度零过度信号:模块规模分布健康(最大 878 行为核心复杂度所在)、全仓唯一 `opts?` 参数化有测试理由、依赖 2+7 全在用、工具链/CI 与本地管线逐字一致、基础设施模块消费方匹配、types.ts 44 个类型全是领域实体;别名面克制(19 个命令仅 6 个带别名)
|
|
15
33
|
|
|
16
34
|
### 修复与测试(第十四轮:高负载偶发失败根因 + 脱敏短串例外 + 文案一致性)
|
|
17
35
|
|
package/README.md
CHANGED
|
@@ -445,11 +445,13 @@ hosts:
|
|
|
445
445
|
|
|
446
446
|
### JS 覆写脚本
|
|
447
447
|
|
|
448
|
-
YAML 操作符只保留最简单的三种,其余一律写脚本自由处理。脚本是一个默认导出的函数,**就地修改**传入的 `config
|
|
448
|
+
YAML 操作符只保留最简单的三种,其余一律写脚本自由处理。脚本是一个默认导出的函数,**就地修改**传入的 `config`(订阅解析后的配置);脚本没有 `match` 声明,靠**返回值报告命中**——走到最后 `return true` 表示「适用于当前订阅、已生效」,其余情况(提前 `return`、不写返回值)都视为不适用。YAML 覆写在全部脚本之后才声明式合并:
|
|
449
449
|
|
|
450
450
|
```js
|
|
451
451
|
// ~/.mihomo-cli/overwrite.custom.js
|
|
452
452
|
export default function (config, ctx) {
|
|
453
|
+
// 作用域判据不中:提前退出(= 未命中,status 会把它列进「不适用」)
|
|
454
|
+
if (!ctx.subscription.name.startsWith('edu')) return;
|
|
453
455
|
// 把订阅下发的 Developer 分组默认选中改为 TW Fixed IP;
|
|
454
456
|
// 该分组不存在时跳过并提示(不用为它改 match 作用域)
|
|
455
457
|
const groups = config['proxy-groups'] || [];
|
|
@@ -459,6 +461,7 @@ export default function (config, ctx) {
|
|
|
459
461
|
} else {
|
|
460
462
|
ctx.warn('当前订阅无 Developer 分组,跳过 default-selected 注入');
|
|
461
463
|
}
|
|
464
|
+
return true; // 命中当前订阅
|
|
462
465
|
}
|
|
463
466
|
```
|
|
464
467
|
|
|
@@ -470,16 +473,17 @@ export default function (config, ctx) {
|
|
|
470
473
|
| `ctx.subscription.url` | 订阅原始 URL |
|
|
471
474
|
| `ctx.subscription.host` | 预解析的 URL hostname(解析失败为空串),按域名限定作用域时用它 |
|
|
472
475
|
| `ctx.mode` | 本次构建的运行模式:`'mixed'` 或 `'tun'` |
|
|
473
|
-
| `ctx.warn(message)` | 发一条提示进 warnings 通道,`config` / `doctor` / `start` 的输出可见(`status`
|
|
476
|
+
| `ctx.warn(message)` | 发一条提示进 warnings 通道,`config` / `doctor` / `start` 的输出可见(`status` 不显示 `ctx.warn` 的内容) |
|
|
474
477
|
|
|
475
478
|
约定与边界:
|
|
476
479
|
|
|
477
480
|
- **必须同步**:返回 Promise 会报错。脚本是纯数据变换,没有要等网络的场景
|
|
481
|
+
- **`return true` = 命中当前订阅**:脚本没有 `match` 声明,`status` / `config` 靠返回值区分「生效中」与「不适用于当前订阅」——判据过了就在最后 `return true`,不中就提前退出。判定严格认 `true`(返回别的 truthy 值不算),也不影响合并本身
|
|
478
482
|
- **全信任**:脚本以你的用户身份运行(和 `.zshrc` 一个待遇),不做沙箱与超时——别装来路不明的覆写脚本
|
|
479
483
|
- **改不动系统锁定项**:`mixed-port`、`external-controller`、`allow-lan` 等入站与控制面键由 CLI 管理,脚本设置了会被剥除并提示(与 YAML 覆写同一条边界)
|
|
480
484
|
- **脚本先于 YAML 执行**:脚本看到的是订阅原始配置,读不到 YAML 覆写注入的内容;需要脚本处理 YAML 注入项时,把那段逻辑也写进脚本
|
|
481
|
-
- **只读命令也会加载脚本**:`ow` / `status` 扫描文件时会加载脚本,模块顶层代码随之执行(顶层只定义函数,变换都在导出函数里做);导出的变换函数在 `config` / `doctor` / `start`
|
|
482
|
-
- 加载失败(语法错误、缺少导出、顶层抛错)与坏 YAML 同款姿态:`ow` / `status` 里「加载失败」可见,`config` / `start` / `doctor` 硬失败并带文件名;变换函数执行中抛错只在后三者报出(`
|
|
485
|
+
- **只读命令也会加载脚本**:`ow` / `status` 扫描文件时会加载脚本,模块顶层代码随之执行(顶层只定义函数,变换都在导出函数里做);导出的变换函数在 `config` / `doctor` / `start` 构建时调用;`status` 有活跃订阅时也会执行一次判定构建(独立副本,坏覆写文件不影响判定),但不显示 `ctx.warn` 的内容
|
|
486
|
+
- 加载失败(语法错误、缺少导出、顶层抛错)与坏 YAML 同款姿态:`ow` / `status` 里「加载失败」可见,`config` / `start` / `doctor` 硬失败并带文件名;变换函数执行中抛错只在后三者报出(`status` 的判定构建会执行函数体,但把执行失败按「未判定」降级、不显示错误)
|
|
483
487
|
- 脚本受 `mihomo-cli ow off` 全局开关管理;想临时停用单个脚本,改个扩展名(如 `.bak`)即可
|
|
484
488
|
|
|
485
489
|
### 作用域限定(match)
|
|
@@ -503,7 +507,7 @@ export default function (config, ctx) {
|
|
|
503
507
|
|
|
504
508
|
`match` 块**写错会直接报错**(键名拼错、值为空、空块、写已移除的 `subscription` 键),而不是静默忽略后对所有订阅生效——写了 `match` 显然是想限定作用域,悄悄放宽比报错危险得多。历史写法 `subscription` 与 `name` 同义、已收掉,写它直接报错指明改写 `name`。
|
|
505
509
|
|
|
506
|
-
> `url-domain` 命中该域名下的**所有**订阅。同一机场的多条订阅(如 `edu1`、`mini1`)URL 往往同域名,用 `url-domain` 会一并生效;要在同机场内按套餐区分,用 `name: edu*`。若只是担心某条订阅没有要改的分组,在 JS 脚本里判(找不到就 `ctx.warn` 跳过),不必为此改作用域;`match` 应当按「这份覆写在语义上属于哪些订阅」来写。JS 脚本没有 match 机制——作用域判断写在脚本开头(`if (!ctx.subscription.name.startsWith('edu')) return
|
|
510
|
+
> `url-domain` 命中该域名下的**所有**订阅。同一机场的多条订阅(如 `edu1`、`mini1`)URL 往往同域名,用 `url-domain` 会一并生效;要在同机场内按套餐区分,用 `name: edu*`。若只是担心某条订阅没有要改的分组,在 JS 脚本里判(找不到就 `ctx.warn` 跳过),不必为此改作用域;`match` 应当按「这份覆写在语义上属于哪些订阅」来写。JS 脚本没有 match 机制——作用域判断写在脚本开头(`if (!ctx.subscription.name.startsWith('edu')) return;`),判据过了在函数末尾 `return true`,`status` 便能把命中的脚本列进「生效」、未命中的列进「不适用」。
|
|
507
511
|
|
|
508
512
|
`mihomo-cli status` 会按当前活跃订阅区分「生效」与「不适用」,括号里只列本次真正参与合并的文件:
|
|
509
513
|
|
|
@@ -512,7 +516,7 @@ export default function (config, ctx) {
|
|
|
512
516
|
glados 不适用于当前订阅 mini1(作用域 name=edu*)
|
|
513
517
|
```
|
|
514
518
|
|
|
515
|
-
「不适用」指文件本身是启用的,只是 `match`
|
|
519
|
+
「不适用」指文件本身是启用的,只是 `match` 没命中当前订阅(JS 脚本则是没走到 `return true`)——切到命中的订阅(`sub use`)、改 `match` 或脚本判据才会生效,与 `enabled: false` 的「已禁用」是两回事。`mihomo-cli ow` 列表不做这个判断(它不绑定某条订阅),那里的作用域一栏只说明该文件管哪些订阅。`--json` 形态下 `overwrite.applied` 是生效清单(`ow off` 全局关闭时为空数组),`overwrite.files` 仍是「未被 `enabled: false` 停用」的全部文件;语法或元数据键写错的文件进 `overwrite.errors`(不混进 files/applied)。
|
|
516
520
|
|
|
517
521
|
**坏文件不阻断诊断、但阻断启动**:YAML 语法错误(含 `enabled: no` 这类元数据键错误)的文件在 `mihomo-cli ow` 与 `status` 中以「加载失败」红字标出,诊断命令永远可用;但该文件不参与合并,`mihomo-cli start`/`doctor` 会硬失败并给出原因——曾经语法错只警告一行就跳过、退出码 0,启动成功但覆写根本没生效。
|
|
518
522
|
|
|
@@ -567,6 +571,7 @@ export default function (config, ctx) {
|
|
|
567
571
|
// 精简套餐没有该分组:提示并跳过,不追加残缺分组
|
|
568
572
|
ctx.warn('当前订阅无 Developer 分组,跳过 default-selected 注入');
|
|
569
573
|
}
|
|
574
|
+
return true; // 命中当前订阅(status 据此区分生效与不适用)
|
|
570
575
|
}
|
|
571
576
|
```
|
|
572
577
|
|
package/dist/index.js
CHANGED
|
@@ -3669,18 +3669,21 @@ function sleepSyncMs(ms) {
|
|
|
3669
3669
|
// src/settings.ts
|
|
3670
3670
|
import fs2 from "fs";
|
|
3671
3671
|
import path2 from "path";
|
|
3672
|
-
function
|
|
3673
|
-
const backup = `${
|
|
3672
|
+
function backupCorruptFile(file, noun, reason, fallbackNote) {
|
|
3673
|
+
const backup = `${file}.bak`;
|
|
3674
3674
|
try {
|
|
3675
3675
|
if (fs2.existsSync(backup)) {
|
|
3676
|
-
console.warn(`\u8B66\u544A:
|
|
3676
|
+
console.warn(`\u8B66\u544A: ${noun} ${reason}\uFF0C${fallbackNote}\uFF08\u539F\u4EF6\u5DF2\u5728\u65E9\u524D\u5907\u4EFD: ${backup}\uFF0C\u672A\u8986\u76D6\uFF09`);
|
|
3677
3677
|
} else {
|
|
3678
|
-
fs2.copyFileSync(
|
|
3679
|
-
console.warn(`\u8B66\u544A:
|
|
3678
|
+
fs2.copyFileSync(file, backup);
|
|
3679
|
+
console.warn(`\u8B66\u544A: ${noun} ${reason}\uFF0C\u5DF2\u5907\u4EFD\u5230 ${backup}\uFF0C${fallbackNote}`);
|
|
3680
3680
|
}
|
|
3681
3681
|
} catch {
|
|
3682
|
-
console.warn(`\u8B66\u544A:
|
|
3682
|
+
console.warn(`\u8B66\u544A: ${noun} ${reason}\uFF0C${fallbackNote}`);
|
|
3683
3683
|
}
|
|
3684
|
+
}
|
|
3685
|
+
function backupCorruptSettings(reason) {
|
|
3686
|
+
backupCorruptFile(PATHS.settingsFile, "settings.json", reason, "\u4F7F\u7528\u9ED8\u8BA4\u8BBE\u7F6E");
|
|
3684
3687
|
return {};
|
|
3685
3688
|
}
|
|
3686
3689
|
function readSettings() {
|
|
@@ -3750,51 +3753,16 @@ function getPorts(settings = readSettings()) {
|
|
|
3750
3753
|
}
|
|
3751
3754
|
return { mixed, controller };
|
|
3752
3755
|
}
|
|
3753
|
-
function
|
|
3756
|
+
function getControllerPortOrNull() {
|
|
3754
3757
|
try {
|
|
3755
|
-
|
|
3756
|
-
const TOKEN_KEY_NAMES = [
|
|
3757
|
-
"token",
|
|
3758
|
-
"key",
|
|
3759
|
-
"secret",
|
|
3760
|
-
"pass",
|
|
3761
|
-
"password",
|
|
3762
|
-
"auth",
|
|
3763
|
-
"access_token",
|
|
3764
|
-
"api_key",
|
|
3765
|
-
"uuid",
|
|
3766
|
-
"sid",
|
|
3767
|
-
"id",
|
|
3768
|
-
"sub",
|
|
3769
|
-
"user",
|
|
3770
|
-
"email",
|
|
3771
|
-
"passwd",
|
|
3772
|
-
"apikey",
|
|
3773
|
-
"access"
|
|
3774
|
-
];
|
|
3775
|
-
if (new Set(TOKEN_KEY_NAMES).size !== TOKEN_KEY_NAMES.length) {
|
|
3776
|
-
throw new Error("tokenKeys \u5B58\u5728\u91CD\u590D\u767B\u8BB0");
|
|
3777
|
-
}
|
|
3778
|
-
const tokenKeys = new Set(TOKEN_KEY_NAMES);
|
|
3779
|
-
for (const [key, value] of parsed.searchParams) {
|
|
3780
|
-
if (tokenKeys.has(key.toLowerCase()) || value.length >= 16) {
|
|
3781
|
-
parsed.searchParams.set(key, "***");
|
|
3782
|
-
}
|
|
3783
|
-
}
|
|
3784
|
-
if (parsed.username) parsed.username = "***";
|
|
3785
|
-
if (parsed.password) parsed.password = "***";
|
|
3786
|
-
parsed.pathname = parsed.pathname.split("/").map((seg) => seg.length >= 16 ? `${seg.slice(0, 4)}***${seg.slice(-4)}` : seg).join("/");
|
|
3787
|
-
return parsed.toString();
|
|
3758
|
+
return getPorts().controller;
|
|
3788
3759
|
} catch {
|
|
3789
|
-
|
|
3790
|
-
return `${url.slice(0, 15)}...`;
|
|
3791
|
-
}
|
|
3792
|
-
return url;
|
|
3760
|
+
return null;
|
|
3793
3761
|
}
|
|
3794
3762
|
}
|
|
3795
|
-
function
|
|
3763
|
+
function getMixedPortOrNull() {
|
|
3796
3764
|
try {
|
|
3797
|
-
return getPorts().
|
|
3765
|
+
return getPorts().mixed;
|
|
3798
3766
|
} catch {
|
|
3799
3767
|
return null;
|
|
3800
3768
|
}
|
|
@@ -3818,17 +3786,7 @@ function readSubscriptionCache() {
|
|
|
3818
3786
|
return empty();
|
|
3819
3787
|
}
|
|
3820
3788
|
function backupCorruptSubscriptionCache(reason) {
|
|
3821
|
-
|
|
3822
|
-
try {
|
|
3823
|
-
if (fs2.existsSync(cacheBackup)) {
|
|
3824
|
-
console.warn(`\u8B66\u544A: \u8BA2\u9605\u7F13\u5B58${reason}\uFF0C\u5DF2\u5FFD\u7565\uFF08\u65E9\u524D\u5907\u4EFD\u4FDD\u7559\u5728 ${cacheBackup}\uFF0C\u672A\u8986\u76D6\uFF09`);
|
|
3825
|
-
} else {
|
|
3826
|
-
fs2.copyFileSync(PATHS.subscriptionsCacheFile, cacheBackup);
|
|
3827
|
-
console.warn(`\u8B66\u544A: \u8BA2\u9605\u7F13\u5B58${reason}\uFF0C\u5DF2\u5907\u4EFD\u5230 ${cacheBackup}`);
|
|
3828
|
-
}
|
|
3829
|
-
} catch {
|
|
3830
|
-
console.warn(`\u8B66\u544A: \u8BA2\u9605\u7F13\u5B58${reason}\uFF0C\u5DF2\u5FFD\u7565`);
|
|
3831
|
-
}
|
|
3789
|
+
backupCorruptFile(PATHS.subscriptionsCacheFile, "\u8BA2\u9605\u7F13\u5B58", reason, "\u5DF2\u5FFD\u7565");
|
|
3832
3790
|
return /* @__PURE__ */ Object.create(null);
|
|
3833
3791
|
}
|
|
3834
3792
|
function writeSubscriptionCache(cache) {
|
|
@@ -3956,6 +3914,13 @@ function readSubscriptionRawConfig(subName) {
|
|
|
3956
3914
|
if (!fs2.existsSync(filePath)) return null;
|
|
3957
3915
|
return fs2.readFileSync(filePath, "utf8");
|
|
3958
3916
|
}
|
|
3917
|
+
function requireSubscriptionRawConfig(subName) {
|
|
3918
|
+
const content = readSubscriptionRawConfig(subName);
|
|
3919
|
+
if (content === null) {
|
|
3920
|
+
throw new CliError(`\u8BA2\u9605 "${subName}" \u6709\u6761\u76EE\u4F46\u6CA1\u6709\u672C\u5730\u914D\u7F6E\u6587\u4EF6`, { hint: `\u66F4\u65B0\u8BA2\u9605: mihomo-cli sub update ${subName}` });
|
|
3921
|
+
}
|
|
3922
|
+
return content;
|
|
3923
|
+
}
|
|
3959
3924
|
|
|
3960
3925
|
// src/overwrite.ts
|
|
3961
3926
|
function parseOverrideKey(key) {
|
|
@@ -4215,8 +4180,7 @@ function summarizeMatch(match) {
|
|
|
4215
4180
|
if (!match) return void 0;
|
|
4216
4181
|
const parts = [];
|
|
4217
4182
|
for (const [key, value] of Object.entries(match)) {
|
|
4218
|
-
|
|
4219
|
-
parts.push(`${key}=${vals.join("/")}`);
|
|
4183
|
+
parts.push(`${key}=${value.join("/")}`);
|
|
4220
4184
|
}
|
|
4221
4185
|
return parts.length > 0 ? parts.join(", ") : void 0;
|
|
4222
4186
|
}
|
|
@@ -4238,13 +4202,12 @@ function nameMatchesPattern(name, pattern) {
|
|
|
4238
4202
|
function matchesScope(match, scope) {
|
|
4239
4203
|
if (!match) return true;
|
|
4240
4204
|
if (match.name) {
|
|
4241
|
-
const names = Array.isArray(match.name) ? match.name : [match.name];
|
|
4242
4205
|
if (!scope?.subName) return false;
|
|
4243
4206
|
const subName = scope.subName;
|
|
4244
|
-
if (!
|
|
4207
|
+
if (!match.name.some((n) => nameMatchesPattern(subName, n))) return false;
|
|
4245
4208
|
}
|
|
4246
4209
|
if (match["url-domain"]) {
|
|
4247
|
-
const domains =
|
|
4210
|
+
const domains = match["url-domain"];
|
|
4248
4211
|
if (!scope?.subUrl) return false;
|
|
4249
4212
|
let host;
|
|
4250
4213
|
try {
|
|
@@ -4341,7 +4304,7 @@ function loadOverwriteScript(filePath, fileName) {
|
|
|
4341
4304
|
throw new CliError(`\u8986\u5199\u811A\u672C "${fileName}" \u7F3A\u5C11\u53D8\u6362\u51FD\u6570`, {
|
|
4342
4305
|
label: "\u8986\u5199\u914D\u7F6E\u9519\u8BEF",
|
|
4343
4306
|
hint: [
|
|
4344
|
-
"\u5199\u6CD5\uFF08\u5C31\u5730\u4FEE\u6539\u4F20\u5165\u7684 config\uFF0C\u8FD4\u56DE\u503C\u5FFD\u7565\uFF09:",
|
|
4307
|
+
"\u5199\u6CD5\uFF08\u5C31\u5730\u4FEE\u6539\u4F20\u5165\u7684 config\uFF1Breturn true \u8868\u793A\u547D\u4E2D\u5F53\u524D\u8BA2\u9605\uFF0C\u5176\u4F59\u8FD4\u56DE\u503C\u5FFD\u7565\uFF09:",
|
|
4345
4308
|
" export default function (config, ctx) { ... }",
|
|
4346
4309
|
"CommonJS \u5199\u6CD5 module.exports = function (config, ctx) { ... } \u540C\u6837\u8BA4\u3002"
|
|
4347
4310
|
]
|
|
@@ -4426,6 +4389,7 @@ function applyOverwrite(baseConfig, files, opts) {
|
|
|
4426
4389
|
let result = { ...baseConfig };
|
|
4427
4390
|
const scriptWarnings = [];
|
|
4428
4391
|
const scriptLockedHits = [];
|
|
4392
|
+
const scriptMatches = [];
|
|
4429
4393
|
let host = "";
|
|
4430
4394
|
try {
|
|
4431
4395
|
if (opts.scope?.subUrl) host = new URL(opts.scope.subUrl.trim()).hostname;
|
|
@@ -4454,13 +4418,14 @@ function applyOverwrite(baseConfig, files, opts) {
|
|
|
4454
4418
|
});
|
|
4455
4419
|
}
|
|
4456
4420
|
scriptWarnings.push(...notes.map((message) => `${message}\uFF08\u811A\u672C ${file.name}\uFF09`));
|
|
4421
|
+
scriptMatches.push({ file: file.name, matched: returned === true });
|
|
4457
4422
|
const keys = diffLockedKeys(before, result);
|
|
4458
4423
|
if (keys.length > 0) scriptLockedHits.push({ file: file.name, keys });
|
|
4459
4424
|
continue;
|
|
4460
4425
|
}
|
|
4461
4426
|
result = mergeConfigLevel(result, file.config ?? {}, true);
|
|
4462
4427
|
}
|
|
4463
|
-
return { config: result, scriptWarnings, scriptLockedHits };
|
|
4428
|
+
return { config: result, scriptWarnings, scriptLockedHits, scriptMatches };
|
|
4464
4429
|
}
|
|
4465
4430
|
function listOverwriteFile(scope) {
|
|
4466
4431
|
const { ok, broken } = readOverwriteFiles();
|
|
@@ -4468,6 +4433,7 @@ function listOverwriteFile(scope) {
|
|
|
4468
4433
|
return {
|
|
4469
4434
|
enabled,
|
|
4470
4435
|
dir: USER_DATA_DIR,
|
|
4436
|
+
entries: ok,
|
|
4471
4437
|
files: ok.map((f) => ({
|
|
4472
4438
|
name: f.name,
|
|
4473
4439
|
path: f.path,
|
|
@@ -4493,6 +4459,45 @@ function sanitizeTerminal(s) {
|
|
|
4493
4459
|
const controlChars = /[\x00-\x08\x0b\x0c\x0d\x0e-\x1f]/g;
|
|
4494
4460
|
return s.replace(ansiEscape, "").replace(controlChars, "");
|
|
4495
4461
|
}
|
|
4462
|
+
var TOKEN_KEY_NAMES = [
|
|
4463
|
+
"token",
|
|
4464
|
+
"key",
|
|
4465
|
+
"secret",
|
|
4466
|
+
"pass",
|
|
4467
|
+
"password",
|
|
4468
|
+
"auth",
|
|
4469
|
+
"access_token",
|
|
4470
|
+
"api_key",
|
|
4471
|
+
"uuid",
|
|
4472
|
+
"sid",
|
|
4473
|
+
"id",
|
|
4474
|
+
"sub",
|
|
4475
|
+
"user",
|
|
4476
|
+
"email",
|
|
4477
|
+
"passwd",
|
|
4478
|
+
"apikey",
|
|
4479
|
+
"access"
|
|
4480
|
+
];
|
|
4481
|
+
var TOKEN_KEYS = new Set(TOKEN_KEY_NAMES);
|
|
4482
|
+
function maskUrl(url) {
|
|
4483
|
+
try {
|
|
4484
|
+
const parsed = new URL(url);
|
|
4485
|
+
for (const [key, value] of parsed.searchParams) {
|
|
4486
|
+
if (TOKEN_KEYS.has(key.toLowerCase()) || value.length >= 16) {
|
|
4487
|
+
parsed.searchParams.set(key, "***");
|
|
4488
|
+
}
|
|
4489
|
+
}
|
|
4490
|
+
if (parsed.username) parsed.username = "***";
|
|
4491
|
+
if (parsed.password) parsed.password = "***";
|
|
4492
|
+
parsed.pathname = parsed.pathname.split("/").map((seg) => seg.length >= 16 ? `${seg.slice(0, 4)}***${seg.slice(-4)}` : seg).join("/");
|
|
4493
|
+
return parsed.toString();
|
|
4494
|
+
} catch {
|
|
4495
|
+
if (url.length > 15) {
|
|
4496
|
+
return `${url.slice(0, 15)}...`;
|
|
4497
|
+
}
|
|
4498
|
+
return url;
|
|
4499
|
+
}
|
|
4500
|
+
}
|
|
4496
4501
|
|
|
4497
4502
|
// src/config.ts
|
|
4498
4503
|
var SAFE_YAML_LOAD_OPTIONS = { maxAliases: YAML_MAX_ALIASES };
|
|
@@ -4586,7 +4591,8 @@ function buildConfig(subRawContent, mode, scope) {
|
|
|
4586
4591
|
const {
|
|
4587
4592
|
config: withOverwrites,
|
|
4588
4593
|
scriptWarnings,
|
|
4589
|
-
scriptLockedHits
|
|
4594
|
+
scriptLockedHits,
|
|
4595
|
+
scriptMatches
|
|
4590
4596
|
} = applyOverwrite(subscriptionConfig, overwriteFiles, {
|
|
4591
4597
|
mode: mode === "tun" ? "tun" : "mixed",
|
|
4592
4598
|
scope
|
|
@@ -4594,6 +4600,9 @@ function buildConfig(subRawContent, mode, scope) {
|
|
|
4594
4600
|
const overwriteSummaries = overwriteFiles.map(describeOverwriteScope);
|
|
4595
4601
|
const systemConfig = {};
|
|
4596
4602
|
const lockedWarnings = [...scriptWarnings];
|
|
4603
|
+
for (const m of scriptMatches.filter((s) => !s.matched)) {
|
|
4604
|
+
lockedWarnings.push(`\u8986\u5199\u811A\u672C ${m.file} \u672A\u8FD4\u56DE true\uFF0C\u4E0D\u89C6\u4E3A\u547D\u4E2D\u5F53\u524D\u8BA2\u9605${scope?.subName ? ` ${scope.subName}` : ""}`);
|
|
4605
|
+
}
|
|
4597
4606
|
for (const hit of scriptLockedHits) {
|
|
4598
4607
|
lockedWarnings.push(renderLockedWarning(`\u8986\u5199\u811A\u672C ${hit.file} \u4E2D\u7684`, hit.keys));
|
|
4599
4608
|
}
|
|
@@ -4669,11 +4678,28 @@ function buildConfig(subRawContent, mode, scope) {
|
|
|
4669
4678
|
};
|
|
4670
4679
|
}
|
|
4671
4680
|
assertConfigShape(merged);
|
|
4672
|
-
return { config: merged, warnings: lockedWarnings, overwriteSummaries };
|
|
4681
|
+
return { config: merged, warnings: lockedWarnings, overwriteSummaries, scriptMatches };
|
|
4673
4682
|
}
|
|
4674
4683
|
function renderLockedWarning(source, keys) {
|
|
4675
4684
|
return `${source}\u7CFB\u7EDF\u9501\u5B9A\u9879\u5DF2\u5FFD\u7565: ${keys.join("\u3001")}\uFF08\u5165\u7AD9\u7AEF\u53E3\u3001\u63A7\u5236\u9762\u3001\u63A7\u5236\u5668\u8BC1\u4E66\u4E0E\u5C40\u57DF\u7F51/\u5165\u7AD9\u9274\u6743\u7531 mihomo-cli \u7BA1\u7406\uFF1B\u7AEF\u53E3\u4E0E controller secret \u5728 settings.json \u914D\u7F6E\uFF0C\u5165\u7AD9\u56FA\u5B9A\u53EA\u76D1\u542C\u56DE\u73AF\uFF0C\u9700\u8981\u5C40\u57DF\u7F51\u5165\u7AD9\u8BF7\u5728\u672C\u673A\u53E6\u8D77\u4E00\u4E2A mihomo-cli \u5B9E\u4F8B\uFF09`;
|
|
4676
4685
|
}
|
|
4686
|
+
function judgeScriptMatches(subRawContent, mode, scope, entries) {
|
|
4687
|
+
try {
|
|
4688
|
+
const subscriptionConfig = parseConfigContent(subRawContent, "\u8BA2\u9605\u5185\u5BB9");
|
|
4689
|
+
const settings = readSettings();
|
|
4690
|
+
const active = settings.overwrite_enabled !== false ? selectActiveOverwriteFiles(entries, scope) : [];
|
|
4691
|
+
const { scriptMatches } = applyOverwrite(subscriptionConfig, active, {
|
|
4692
|
+
mode: mode === "tun" ? "tun" : "mixed",
|
|
4693
|
+
scope
|
|
4694
|
+
});
|
|
4695
|
+
return { matches: scriptMatches };
|
|
4696
|
+
} catch (e) {
|
|
4697
|
+
return { matches: [], error: e.message?.split("\n")[0] ?? String(e) };
|
|
4698
|
+
}
|
|
4699
|
+
}
|
|
4700
|
+
function deriveRuntimeMode(info) {
|
|
4701
|
+
return info?.tun ? "tun" : "mixed";
|
|
4702
|
+
}
|
|
4677
4703
|
function writeMihomoConfig(configObj) {
|
|
4678
4704
|
ensureDirs();
|
|
4679
4705
|
const content = dumpYaml(configObj);
|
|
@@ -4700,7 +4726,7 @@ function buildKernelRejectHint(detail, overwriteSummaries, opts = {}) {
|
|
|
4700
4726
|
return hint;
|
|
4701
4727
|
}
|
|
4702
4728
|
async function validateConfigWithKernel(config, overwriteSummaries) {
|
|
4703
|
-
|
|
4729
|
+
assertKernelInstalled();
|
|
4704
4730
|
ensureDirs();
|
|
4705
4731
|
const stageDir = fs4.mkdtempSync(path4.join(DIRS.runtime, "check-"));
|
|
4706
4732
|
const stageFile = path4.join(stageDir, "config.yaml");
|
|
@@ -4756,14 +4782,24 @@ function getConfigInfo() {
|
|
|
4756
4782
|
function hasKernel() {
|
|
4757
4783
|
return fs4.existsSync(PATHS.mihomoBinary);
|
|
4758
4784
|
}
|
|
4759
|
-
function
|
|
4785
|
+
function assertKernelInstalled() {
|
|
4786
|
+
if (!hasKernel()) throw new CliError("\u672A\u627E\u5230\u5185\u6838", { hint: "\u4E0B\u8F7D\u5185\u6838: mihomo-cli kernel" });
|
|
4787
|
+
}
|
|
4788
|
+
function assertRuntimeConfigPresent() {
|
|
4789
|
+
if (!fs4.existsSync(PATHS.configFile)) throw new CliError("\u672A\u627E\u5230\u8FD0\u884C\u65F6\u914D\u7F6E", { hint: "\u8BF7\u5148\u6DFB\u52A0\u8BA2\u9605: mihomo-cli sub add <url>" });
|
|
4790
|
+
}
|
|
4791
|
+
function probeKernelVersion(binary = PATHS.mihomoBinary) {
|
|
4792
|
+
const r = spawnSync(binary, ["-v"], { encoding: "utf8", timeout: 5e3 });
|
|
4793
|
+
const output = `${r.stdout || ""}${r.stderr || ""}`.trim();
|
|
4794
|
+
const match = output.match(/v?\d+\.\d+\.\d+/);
|
|
4795
|
+
return { spawnError: r.error ?? null, status: r.status ?? null, output, version: match ? match[0] : null };
|
|
4796
|
+
}
|
|
4797
|
+
function getKernelVersion(probe) {
|
|
4760
4798
|
if (!hasKernel()) return null;
|
|
4761
4799
|
try {
|
|
4762
|
-
const
|
|
4763
|
-
|
|
4764
|
-
|
|
4765
|
-
const match = output.match(/v?[\d]+\.[\d]+\.[\d]+/);
|
|
4766
|
-
return match ? match[0] : output.split("\n")[0];
|
|
4800
|
+
const p = probe ?? probeKernelVersion();
|
|
4801
|
+
if (!p.output) return "unknown";
|
|
4802
|
+
return p.version ?? p.output.split("\n")[0];
|
|
4767
4803
|
} catch {
|
|
4768
4804
|
return "unknown";
|
|
4769
4805
|
}
|
|
@@ -4865,7 +4901,6 @@ import fs13 from "fs";
|
|
|
4865
4901
|
import path7 from "path";
|
|
4866
4902
|
|
|
4867
4903
|
// src/hot-reload.ts
|
|
4868
|
-
import { spawnSync as spawnSync3 } from "child_process";
|
|
4869
4904
|
import fs7 from "fs";
|
|
4870
4905
|
|
|
4871
4906
|
// src/launchctl.ts
|
|
@@ -4904,7 +4939,7 @@ function parseServicePrint(output) {
|
|
|
4904
4939
|
};
|
|
4905
4940
|
}
|
|
4906
4941
|
function parseDisabledList(output, label) {
|
|
4907
|
-
const escaped = label
|
|
4942
|
+
const escaped = escapeRegExp(label);
|
|
4908
4943
|
const match = output.match(new RegExp(`^\\s*"${escaped}" => (\\S+)$`, "m"));
|
|
4909
4944
|
if (!match) return false;
|
|
4910
4945
|
const value = match[1].toLowerCase();
|
|
@@ -5001,6 +5036,60 @@ async function waitUntilUnloaded(target = serviceTarget()) {
|
|
|
5001
5036
|
});
|
|
5002
5037
|
}
|
|
5003
5038
|
|
|
5039
|
+
// src/proxy-probe.ts
|
|
5040
|
+
import { execFile as execFile2, spawnSync as spawnSync3 } from "child_process";
|
|
5041
|
+
import { promisify as promisify2 } from "util";
|
|
5042
|
+
var execFileAsync = promisify2(execFile2);
|
|
5043
|
+
function lsofListenPids(port) {
|
|
5044
|
+
try {
|
|
5045
|
+
const r = spawnSync3("lsof", ["-nP", `-iTCP:${port}`, "-sTCP:LISTEN", "-t"], { encoding: "utf8", timeout: 5e3 });
|
|
5046
|
+
if (r.status !== 0) return null;
|
|
5047
|
+
return r.stdout.split("\n").map((line) => Number.parseInt(line.trim(), 10)).filter((pid) => Number.isInteger(pid) && pid > 0);
|
|
5048
|
+
} catch {
|
|
5049
|
+
return null;
|
|
5050
|
+
}
|
|
5051
|
+
}
|
|
5052
|
+
var PROBE_URL = "https://www.gstatic.com/generate_204";
|
|
5053
|
+
function isProbeSuccessStatus(code) {
|
|
5054
|
+
return code !== null && code >= 200 && code < 300;
|
|
5055
|
+
}
|
|
5056
|
+
var PROBE_TIMEOUT_MS = 2e3;
|
|
5057
|
+
var PROBE_CACHE_TTL_MS = 3e3;
|
|
5058
|
+
var probeCache = null;
|
|
5059
|
+
async function probeProxyConnectivity(port) {
|
|
5060
|
+
if (probeCache && probeCache.port === port && Date.now() - probeCache.at < PROBE_CACHE_TTL_MS) {
|
|
5061
|
+
return probeCache.result;
|
|
5062
|
+
}
|
|
5063
|
+
const result = await probeProxyConnectivityUncached(port);
|
|
5064
|
+
probeCache = { port, at: Date.now(), result };
|
|
5065
|
+
return result;
|
|
5066
|
+
}
|
|
5067
|
+
async function probeProxyConnectivityUncached(port) {
|
|
5068
|
+
const start = Date.now();
|
|
5069
|
+
try {
|
|
5070
|
+
const { stdout } = await execFileAsync(
|
|
5071
|
+
"curl",
|
|
5072
|
+
["-x", `http://127.0.0.1:${port}`, "-s", "-o", "/dev/null", "-w", "%{http_code}", "--max-time", String(Math.ceil(PROBE_TIMEOUT_MS / 1e3)), PROBE_URL],
|
|
5073
|
+
{ timeout: PROBE_TIMEOUT_MS + 2e3 }
|
|
5074
|
+
);
|
|
5075
|
+
const code = Number.parseInt(stdout.trim(), 10);
|
|
5076
|
+
const statusCode = Number.isFinite(code) ? code : null;
|
|
5077
|
+
const ok = isProbeSuccessStatus(statusCode);
|
|
5078
|
+
return {
|
|
5079
|
+
ok,
|
|
5080
|
+
statusCode,
|
|
5081
|
+
error: ok ? null : `HTTP ${statusCode ?? "\u65E0\u54CD\u5E94"}`,
|
|
5082
|
+
durationMs: Date.now() - start
|
|
5083
|
+
};
|
|
5084
|
+
} catch (e) {
|
|
5085
|
+
const err = e;
|
|
5086
|
+
const stderr = err.stderr?.toString().trim();
|
|
5087
|
+
const lastLine = stderr ? stderr.split("\n").pop() : void 0;
|
|
5088
|
+
const detail = lastLine ? lastLine.replace(/^curl: \(\d+\)\s*/, "") : err.message ?? "\u8BF7\u6C42\u5931\u8D25";
|
|
5089
|
+
return { ok: false, statusCode: null, error: detail, durationMs: Date.now() - start };
|
|
5090
|
+
}
|
|
5091
|
+
}
|
|
5092
|
+
|
|
5004
5093
|
// src/stop-epoch.ts
|
|
5005
5094
|
import fs6 from "fs";
|
|
5006
5095
|
function readStopEpoch() {
|
|
@@ -5048,7 +5137,8 @@ async function tryHotReload() {
|
|
|
5048
5137
|
try {
|
|
5049
5138
|
const status = getServiceStatus({ withDisabled: false });
|
|
5050
5139
|
if (!status.running || status.pid === null) return false;
|
|
5051
|
-
const
|
|
5140
|
+
const { controller: controllerPort } = getPorts();
|
|
5141
|
+
const baseUrl = `http://127.0.0.1:${controllerPort}`;
|
|
5052
5142
|
const secret = readSettings().controller_secret;
|
|
5053
5143
|
const headers = { "Content-Type": "application/json" };
|
|
5054
5144
|
if (typeof secret === "string" && secret) headers.Authorization = `Bearer ${secret}`;
|
|
@@ -5058,11 +5148,8 @@ async function tryHotReload() {
|
|
|
5058
5148
|
if (!probe.ok) return false;
|
|
5059
5149
|
const info = await probe.json();
|
|
5060
5150
|
if (typeof info?.version !== "string") return false;
|
|
5061
|
-
const
|
|
5062
|
-
|
|
5063
|
-
if (lsofResult.status !== 0) return false;
|
|
5064
|
-
const listenerPid = Number.parseInt(lsofResult.stdout.trim(), 10);
|
|
5065
|
-
if (!Number.isFinite(listenerPid) || listenerPid !== status.pid) return false;
|
|
5151
|
+
const listenerPids = lsofListenPids(controllerPort);
|
|
5152
|
+
if (listenerPids === null || listenerPids[0] !== status.pid) return false;
|
|
5066
5153
|
const res = await fetch(`${baseUrl}/configs?force=true`, {
|
|
5067
5154
|
method: "PUT",
|
|
5068
5155
|
headers,
|
|
@@ -5256,6 +5343,7 @@ function clearRuntime() {
|
|
|
5256
5343
|
function describePidCleanupFailure(err) {
|
|
5257
5344
|
return `pid \u6587\u4EF6\u672A\u80FD\u6E05\u7406\uFF08${err.message}\uFF09`;
|
|
5258
5345
|
}
|
|
5346
|
+
var MANUAL_PKILL_HINT = "\u624B\u52A8\u6E05\u7406: sudo pkill -9 mihomo";
|
|
5259
5347
|
function warnPidCleanupFailed(err) {
|
|
5260
5348
|
if (!err) return;
|
|
5261
5349
|
console.warn(colors.yellow(`\u8B66\u544A: ${describePidCleanupFailure(err)}\uFF0C\u4E0B\u6B21 stop \u4F1A\u518D\u6B21\u5C1D\u8BD5`));
|
|
@@ -5367,12 +5455,12 @@ async function stop() {
|
|
|
5367
5455
|
console.warn(colors.yellow(`\u8B66\u544A: root \u6B8B\u7559\u6E05\u7406\u672A\u5B8C\u6210\uFF08${describeSudoFailure(result.scriptError)}\uFF09\uFF0C\u8FDB\u7A0B\u76EE\u524D\u5DF2\u4E0D\u5728\uFF1B\u518D\u53D1\u73B0\u6B8B\u7559\u53EF\u91CD\u8BD5 mihomo-cli stop`));
|
|
5368
5456
|
}
|
|
5369
5457
|
}
|
|
5370
|
-
const remaining =
|
|
5458
|
+
const remaining = result.remaining;
|
|
5371
5459
|
if (remaining.length > 0) {
|
|
5372
5460
|
console.log("");
|
|
5373
5461
|
console.log("\u4ECD\u6709\u8FDB\u7A0B\u6B8B\u7559\uFF0C\u9700\u8981\u624B\u52A8\u6E05\u7406:");
|
|
5374
5462
|
console.log(`\u8FDB\u7A0B PID: ${remaining.join(", ")}`);
|
|
5375
|
-
console.log(
|
|
5463
|
+
console.log(MANUAL_PKILL_HINT);
|
|
5376
5464
|
console.log("");
|
|
5377
5465
|
return { success: true, remaining };
|
|
5378
5466
|
}
|
|
@@ -5391,7 +5479,7 @@ function buildRootResidueCleanupError(result, ctx) {
|
|
|
5391
5479
|
const pidList = `PID ${result.remaining.join(", ")}`;
|
|
5392
5480
|
const residueHint = hasKernelResidue ? scriptError ? `root \u6B8B\u7559\u5185\u6838\u4ECD\u5728\u8FD0\u884C\uFF08${pidList}\uFF09\uFF0C\u53EF\u80FD\u7EE7\u7EED\u5360\u7528\u4EE3\u7406\u7AEF\u53E3` : `\u6B8B\u7559\u5185\u6838\u4ECD\u5728\u8FD0\u884C\uFF08${pidList}\uFF09\u2014\u2014\u7528\u6237\u6001\u672A\u80FD\u7EC8\u6B62\uFF0C\u4E0E\u63D0\u6743\u65E0\u5173` : scriptError ? `root \u6B8B\u7559\u6E05\u7406\u672A\u5B8C\u6210\uFF0C\u8FDB\u7A0B\u76EE\u524D\u5DF2\u4E0D\u5728\uFF08\u6B7B\u4EA1\u7B49\u5F85\u5185\u81EA\u884C\u9000\u51FA\uFF0C\u975E sudo \u6E05\u7406\uFF09${pidError ? `\uFF1B${describePidCleanupFailure(pidError)}` : ""}` : pidError ? `${describePidCleanupFailure(pidError)}: ${PATHS.pidFile}` : "root \u6B8B\u7559\u672A\u6E05\u7406\u5E72\u51C0";
|
|
5393
5481
|
const hint = [ctx.mainOutcome, residueHint, `\u91CD\u65B0\u8FD0\u884C\u53EF\u518D\u6B21\u5C1D\u8BD5\u6E05\u7406: ${ctx.retryCommand}`];
|
|
5394
|
-
hint.push(hasKernelResidue || scriptError ?
|
|
5482
|
+
hint.push(hasKernelResidue || scriptError ? MANUAL_PKILL_HINT : `\u624B\u52A8\u6E05\u7406: sudo rm -f ${PATHS.pidFile}`);
|
|
5395
5483
|
if (cancelled) {
|
|
5396
5484
|
return new CliError("\u7BA1\u7406\u5458\u5BC6\u7801\u672A\u8F93\u5165\u6216\u6709\u8BEF\uFF0Croot \u6B8B\u7559\u672A\u88AB\u6E05\u7406", { label: "\u5DF2\u53D6\u6D88", hint });
|
|
5397
5485
|
}
|
|
@@ -5460,6 +5548,14 @@ async function cleanupLegacyInstallOrThrow() {
|
|
|
5460
5548
|
console.warn(colors.yellow(`\u8B66\u544A: ${describePidCleanupFailure(pidError)}\uFF0C\u4E0B\u6B21 stop \u4F1A\u518D\u6B21\u5C1D\u8BD5`));
|
|
5461
5549
|
}
|
|
5462
5550
|
}
|
|
5551
|
+
async function announceLegacyCleanupOrThrow(reason, extraNotes = []) {
|
|
5552
|
+
console.log(colors.yellow(`\u68C0\u6D4B\u5230\u65E7\u7248\u672C\u5B89\u88C5\u7684\u7CFB\u7EDF\u7EA7\u670D\u52A1\uFF08root LaunchDaemon\uFF09\uFF0C${reason}`));
|
|
5553
|
+
for (const note of extraNotes) console.log(colors.gray(note));
|
|
5554
|
+
console.log(colors.gray(" \u6E05\u7406\u9700\u8981\u4E00\u6B21\u7BA1\u7406\u5458\u5BC6\u7801\uFF08\u5220\u9664 root \u62E5\u6709\u7684\u6587\u4EF6\uFF09"));
|
|
5555
|
+
await cleanupLegacyInstallOrThrow();
|
|
5556
|
+
console.log(colors.green("\u5DF2\u6E05\u7406\u9057\u7559\u7684\u7CFB\u7EDF\u7EA7\u670D\u52A1"));
|
|
5557
|
+
console.log("");
|
|
5558
|
+
}
|
|
5463
5559
|
|
|
5464
5560
|
// src/log-files.ts
|
|
5465
5561
|
import fs12 from "fs";
|
|
@@ -5664,9 +5760,7 @@ function describeAbnormalExit(status) {
|
|
|
5664
5760
|
return describeExitCause(status.lastExitCode, status.lastTerminatingSignal);
|
|
5665
5761
|
}
|
|
5666
5762
|
function ensureServiceSymlink() {
|
|
5667
|
-
|
|
5668
|
-
throw new CliError("\u672A\u627E\u5230 mihomo \u5185\u6838\uFF0C\u8BF7\u5148\u4E0B\u8F7D\u5185\u6838", { hint: "\u4E0B\u8F7D\u5185\u6838: mihomo-cli kernel" });
|
|
5669
|
-
}
|
|
5763
|
+
assertKernelInstalled();
|
|
5670
5764
|
try {
|
|
5671
5765
|
const current = fs13.readlinkSync(PATHS.serviceBinary);
|
|
5672
5766
|
if (current === "mihomo") return;
|
|
@@ -5757,9 +5851,7 @@ async function startService() {
|
|
|
5757
5851
|
if (!isServiceInstalled()) {
|
|
5758
5852
|
throw new CliError("\u670D\u52A1\u672A\u5B89\u88C5", { hint: "\u5B89\u88C5\u670D\u52A1: mihomo-cli install" });
|
|
5759
5853
|
}
|
|
5760
|
-
|
|
5761
|
-
throw new CliError("\u672A\u627E\u5230\u8FD0\u884C\u65F6\u914D\u7F6E", { hint: "\u8BF7\u5148\u6DFB\u52A0\u8BA2\u9605: mihomo-cli sub add <url>" });
|
|
5762
|
-
}
|
|
5854
|
+
assertRuntimeConfigPresent();
|
|
5763
5855
|
if (getConfigInfo()?.tun) {
|
|
5764
5856
|
throw new CliError("\u8FD0\u884C\u65F6\u914D\u7F6E\u4E3A TUN \u6A21\u5F0F\uFF0C\u670D\u52A1\u65E0\u6CD5\u4F7F\u7528", {
|
|
5765
5857
|
hint: [
|
|
@@ -5796,22 +5888,21 @@ function disableServiceAutoStart(timeoutMs = LAUNCHCTL_TIMEOUT_MS) {
|
|
|
5796
5888
|
}
|
|
5797
5889
|
bumpStopEpoch();
|
|
5798
5890
|
}
|
|
5799
|
-
async function
|
|
5800
|
-
assertServiceLabelSafe();
|
|
5891
|
+
async function bootoutAndDisableInLock() {
|
|
5801
5892
|
withFileLock(PATHS.serviceLock, () => {
|
|
5802
5893
|
bootoutService(SERVICE_LOCK_LAUNCHCTL_TIMEOUT_MS);
|
|
5803
5894
|
disableServiceAutoStart(SERVICE_LOCK_LAUNCHCTL_TIMEOUT_MS);
|
|
5804
5895
|
});
|
|
5805
5896
|
await waitUntilUnloaded();
|
|
5897
|
+
}
|
|
5898
|
+
async function stopService() {
|
|
5899
|
+
assertServiceLabelSafe();
|
|
5900
|
+
await bootoutAndDisableInLock();
|
|
5806
5901
|
await cleanupKernelsOrThrow({ mainOutcome: "\u670D\u52A1\u5DF2\u505C\u6B62\uFF0C\u767B\u5F55\u81EA\u542F\u5DF2\u5173\u95ED", retryCommand: "mihomo-cli stop" });
|
|
5807
5902
|
}
|
|
5808
5903
|
async function uninstallService() {
|
|
5809
5904
|
assertServiceLabelSafe();
|
|
5810
|
-
|
|
5811
|
-
bootoutService(SERVICE_LOCK_LAUNCHCTL_TIMEOUT_MS);
|
|
5812
|
-
disableServiceAutoStart(SERVICE_LOCK_LAUNCHCTL_TIMEOUT_MS);
|
|
5813
|
-
});
|
|
5814
|
-
await waitUntilUnloaded();
|
|
5905
|
+
await bootoutAndDisableInLock();
|
|
5815
5906
|
try {
|
|
5816
5907
|
fs13.rmSync(PATHS.userAgentPlist, { force: true });
|
|
5817
5908
|
} catch (e) {
|
|
@@ -5828,10 +5919,11 @@ async function restartService() {
|
|
|
5828
5919
|
if (!isServiceInstalled()) {
|
|
5829
5920
|
throw new CliError("\u670D\u52A1\u672A\u5B89\u88C5\uFF0C\u65E0\u6CD5\u91CD\u542F", { hint: "\u5B89\u88C5\u670D\u52A1: mihomo-cli install" });
|
|
5830
5921
|
}
|
|
5831
|
-
|
|
5922
|
+
const oversized = logOversized();
|
|
5923
|
+
if (!oversized && await tryHotReload()) {
|
|
5832
5924
|
return concludeHotReload(stopEpochBaseline(), readStopEpoch());
|
|
5833
5925
|
}
|
|
5834
|
-
if (
|
|
5926
|
+
if (oversized) {
|
|
5835
5927
|
const archiveFile = allocateArchivePath();
|
|
5836
5928
|
try {
|
|
5837
5929
|
fs13.copyFileSync(PATHS.logFile, archiveFile);
|
|
@@ -5897,7 +5989,7 @@ ${colors.cyan(colors.bold(`mihomo-cli v${VERSION}`))} (mihomo-cli help \u67E5\u
|
|
|
5897
5989
|
const steps = [];
|
|
5898
5990
|
if (!hasKernel()) steps.push(["mihomo-cli kernel", "\u4E0B\u8F7D\u5185\u6838"]);
|
|
5899
5991
|
if (getSubscriptions().length === 0) steps.push(["mihomo-cli sub add <url>", "\u6DFB\u52A0\u8BA2\u9605"]);
|
|
5900
|
-
if (!
|
|
5992
|
+
if (!isServiceInstalled()) steps.push(["mihomo-cli install", "\u5B89\u88C5\u670D\u52A1\uFF08\u53EA\u9700\u4E00\u6B21\uFF09"]);
|
|
5901
5993
|
if (steps.length > 0) {
|
|
5902
5994
|
steps.push(["mihomo-cli start", "\u542F\u52A8\u4EE3\u7406"]);
|
|
5903
5995
|
const width = Math.max(...steps.map(([cmd]) => displayWidth(cmd)));
|
|
@@ -6350,12 +6442,7 @@ async function downloadSubscription(url, subName = "default", signal) {
|
|
|
6350
6442
|
};
|
|
6351
6443
|
}
|
|
6352
6444
|
async function prepareConfigForStart(mode, subName = "default") {
|
|
6353
|
-
const rawContent =
|
|
6354
|
-
if (!rawContent) {
|
|
6355
|
-
throw new CliError(`\u8BA2\u9605 "${subName}" \u6709\u6761\u76EE\u4F46\u6CA1\u6709\u672C\u5730\u914D\u7F6E\u6587\u4EF6`, {
|
|
6356
|
-
hint: `\u66F4\u65B0\u8BA2\u9605: mihomo-cli sub update ${subName}`
|
|
6357
|
-
});
|
|
6358
|
-
}
|
|
6445
|
+
const rawContent = requireSubscriptionRawConfig(subName);
|
|
6359
6446
|
const subUrl = getSubscriptions().find((s) => s.name === subName)?.url;
|
|
6360
6447
|
const buildResult = buildConfig(rawContent, mode, { subName, subUrl });
|
|
6361
6448
|
await validateConfigWithKernel(buildResult.config, buildResult.overwriteSummaries);
|
|
@@ -6459,11 +6546,8 @@ function cmdConfig(args = []) {
|
|
|
6459
6546
|
if (!active) {
|
|
6460
6547
|
throw new CliError("\u5C1A\u65E0\u8BA2\u9605\uFF0C\u65E0\u6CD5\u63A8\u5BFC\u914D\u7F6E", { hint: "\u6DFB\u52A0\u8BA2\u9605: mihomo-cli sub add <url>" });
|
|
6461
6548
|
}
|
|
6462
|
-
const rawContent =
|
|
6463
|
-
|
|
6464
|
-
throw new CliError(`\u8BA2\u9605 "${active.name}" \u6709\u6761\u76EE\u4F46\u6CA1\u6709\u672C\u5730\u914D\u7F6E\u6587\u4EF6`, { hint: `\u66F4\u65B0\u8BA2\u9605: mihomo-cli sub update ${active.name}` });
|
|
6465
|
-
}
|
|
6466
|
-
const mode = getConfigInfo()?.tun ? "tun" : "mixed";
|
|
6549
|
+
const rawContent = requireSubscriptionRawConfig(active.name);
|
|
6550
|
+
const mode = deriveRuntimeMode(getConfigInfo());
|
|
6467
6551
|
const { config, warnings } = buildConfig(rawContent, mode, { subName: active.name, subUrl: active.url });
|
|
6468
6552
|
const redacted = reveal ? { config, changed: false } : redactConfigSecrets(config);
|
|
6469
6553
|
const shown = redacted.config;
|
|
@@ -6604,12 +6688,8 @@ exit 2
|
|
|
6604
6688
|
}
|
|
6605
6689
|
async function startTun() {
|
|
6606
6690
|
ensureDirs();
|
|
6607
|
-
|
|
6608
|
-
|
|
6609
|
-
}
|
|
6610
|
-
if (!fs14.existsSync(PATHS.configFile)) {
|
|
6611
|
-
throw new CliError("\u672A\u627E\u5230\u8FD0\u884C\u65F6\u914D\u7F6E", { hint: "\u8BF7\u5148\u6DFB\u52A0\u8BA2\u9605: mihomo-cli sub add <url>" });
|
|
6612
|
-
}
|
|
6691
|
+
assertKernelInstalled();
|
|
6692
|
+
assertRuntimeConfigPresent();
|
|
6613
6693
|
if (getServiceStatus().loaded) {
|
|
6614
6694
|
throw new CliError("\u53E6\u4E00\u7EC8\u7AEF\u5DF2\u542F\u52A8 Mixed \u670D\u52A1\uFF0CTUN \u672A\u542F\u52A8", {
|
|
6615
6695
|
hint: ["\u4E24\u8005\u4F1A\u62A2\u5360\u540C\u4E00\u7EC4\u7AEF\u53E3\u4E0E\u914D\u7F6E\u3002\u8BF7\u5148\u505C\u6B62\u670D\u52A1:", " mihomo-cli stop", "", "\u4E4B\u540E\u53EF\u91CD\u8BD5 TUN: mihomo-cli start tun"]
|
|
@@ -6641,7 +6721,7 @@ async function startTun() {
|
|
|
6641
6721
|
file: "launch-tun.sh",
|
|
6642
6722
|
codeMessages: { 2: "TUN \u542F\u52A8\u5931\u8D25\uFF08\u8BE6\u89C1\u4E0A\u65B9\u65E5\u5FD7\uFF09" }
|
|
6643
6723
|
});
|
|
6644
|
-
await
|
|
6724
|
+
await sleep(TUN_MODE_POST_WAIT_MS);
|
|
6645
6725
|
const finalPid = getPid();
|
|
6646
6726
|
if (!finalPid || !isRunning()) {
|
|
6647
6727
|
const tail = readLogTail();
|
|
@@ -6658,7 +6738,7 @@ function getRuntimeMode() {
|
|
|
6658
6738
|
return getConfigInfo()?.tun ? "tun" : "mixed";
|
|
6659
6739
|
}
|
|
6660
6740
|
function getRunningState(serviceStatus) {
|
|
6661
|
-
const service = serviceStatus ?? getServiceStatus();
|
|
6741
|
+
const service = serviceStatus ?? getServiceStatus({ withDisabled: false });
|
|
6662
6742
|
if (service.running) {
|
|
6663
6743
|
return { running: true, pid: service.pid, kind: "service", processInfo: null };
|
|
6664
6744
|
}
|
|
@@ -6853,51 +6933,6 @@ function printSystemProxyHint(mixedPort) {
|
|
|
6853
6933
|
console.log(colors.gray(` networksetup -setsocksfirewallproxy "Wi-Fi" 127.0.0.1 ${mixedPort}`));
|
|
6854
6934
|
}
|
|
6855
6935
|
|
|
6856
|
-
// src/proxy-probe.ts
|
|
6857
|
-
import { execFile as execFile2 } from "child_process";
|
|
6858
|
-
import { promisify as promisify2 } from "util";
|
|
6859
|
-
var execFileAsync = promisify2(execFile2);
|
|
6860
|
-
var PROBE_URL = "https://www.gstatic.com/generate_204";
|
|
6861
|
-
function isProbeSuccessStatus(code) {
|
|
6862
|
-
return code !== null && code >= 200 && code < 300;
|
|
6863
|
-
}
|
|
6864
|
-
var PROBE_TIMEOUT_MS = 2e3;
|
|
6865
|
-
var PROBE_CACHE_TTL_MS = 3e3;
|
|
6866
|
-
var probeCache = null;
|
|
6867
|
-
async function probeProxyConnectivity(port) {
|
|
6868
|
-
if (probeCache && probeCache.port === port && Date.now() - probeCache.at < PROBE_CACHE_TTL_MS) {
|
|
6869
|
-
return probeCache.result;
|
|
6870
|
-
}
|
|
6871
|
-
const result = await probeProxyConnectivityUncached(port);
|
|
6872
|
-
probeCache = { port, at: Date.now(), result };
|
|
6873
|
-
return result;
|
|
6874
|
-
}
|
|
6875
|
-
async function probeProxyConnectivityUncached(port) {
|
|
6876
|
-
const start = Date.now();
|
|
6877
|
-
try {
|
|
6878
|
-
const { stdout } = await execFileAsync(
|
|
6879
|
-
"curl",
|
|
6880
|
-
["-x", `http://127.0.0.1:${port}`, "-s", "-o", "/dev/null", "-w", "%{http_code}", "--max-time", String(Math.ceil(PROBE_TIMEOUT_MS / 1e3)), PROBE_URL],
|
|
6881
|
-
{ timeout: PROBE_TIMEOUT_MS + 2e3 }
|
|
6882
|
-
);
|
|
6883
|
-
const code = Number.parseInt(stdout.trim(), 10);
|
|
6884
|
-
const statusCode = Number.isFinite(code) ? code : null;
|
|
6885
|
-
const ok = isProbeSuccessStatus(statusCode);
|
|
6886
|
-
return {
|
|
6887
|
-
ok,
|
|
6888
|
-
statusCode,
|
|
6889
|
-
error: ok ? null : `HTTP ${statusCode ?? "\u65E0\u54CD\u5E94"}`,
|
|
6890
|
-
durationMs: Date.now() - start
|
|
6891
|
-
};
|
|
6892
|
-
} catch (e) {
|
|
6893
|
-
const err = e;
|
|
6894
|
-
const stderr = err.stderr?.toString().trim();
|
|
6895
|
-
const lastLine = stderr ? stderr.split("\n").pop() : void 0;
|
|
6896
|
-
const detail = lastLine ? lastLine.replace(/^curl: \(\d+\)\s*/, "") : err.message ?? "\u8BF7\u6C42\u5931\u8D25";
|
|
6897
|
-
return { ok: false, statusCode: null, error: detail, durationMs: Date.now() - start };
|
|
6898
|
-
}
|
|
6899
|
-
}
|
|
6900
|
-
|
|
6901
6936
|
// src/commands/status.ts
|
|
6902
6937
|
function connectivityHint(urgency) {
|
|
6903
6938
|
if (urgency === "expired") return "\u8BA2\u9605\u5DF2\u8FC7\u671F\uFF0C\u8BF7\u7EED\u8D39\u6216\u66F4\u6362\u8BA2\u9605";
|
|
@@ -6923,7 +6958,7 @@ function buildStatusJson(args) {
|
|
|
6923
6958
|
version: VERSION,
|
|
6924
6959
|
running: args.running,
|
|
6925
6960
|
connectivity: args.probe ? { ok: args.probe.ok, statusCode: args.probe.statusCode, error: args.probe.error, durationMs: args.probe.durationMs } : null,
|
|
6926
|
-
mode: args.info ? args.info
|
|
6961
|
+
mode: args.info ? deriveRuntimeMode(args.info) : null,
|
|
6927
6962
|
carrier: args.kind,
|
|
6928
6963
|
pid: args.pid,
|
|
6929
6964
|
kernel: getKernelVersion(),
|
|
@@ -6979,8 +7014,19 @@ async function printStatus(args = []) {
|
|
|
6979
7014
|
const {
|
|
6980
7015
|
enabled: overwriteEnabled,
|
|
6981
7016
|
files: overwriteFiles,
|
|
7017
|
+
entries: overwriteEntries,
|
|
6982
7018
|
broken: overwriteBroken
|
|
6983
7019
|
} = listOverwriteFile(activeSub ? { subName: activeSub.name, subUrl: activeSub.url } : void 0);
|
|
7020
|
+
if (activeSub && overwriteEnabled && overwriteFiles.some((f) => f.kind === "script")) {
|
|
7021
|
+
const rawContent = readSubscriptionRawConfig(activeSub.name);
|
|
7022
|
+
if (rawContent) {
|
|
7023
|
+
const { matches } = judgeScriptMatches(rawContent, deriveRuntimeMode(info), { subName: activeSub.name, subUrl: activeSub.url }, overwriteEntries);
|
|
7024
|
+
const matchedByName = new Map(matches.map((m) => [m.file, m.matched]));
|
|
7025
|
+
for (const f of overwriteFiles) {
|
|
7026
|
+
if (f.kind === "script" && matchedByName.has(f.name)) f.matched = matchedByName.get(f.name);
|
|
7027
|
+
}
|
|
7028
|
+
}
|
|
7029
|
+
}
|
|
6984
7030
|
const cached = activeSub ? getSubscriptionsWithCache().find((s) => s.name === activeSub.name) : void 0;
|
|
6985
7031
|
const legacy = detectLegacySystemInstall();
|
|
6986
7032
|
const controllerPort = getControllerPortOrNull();
|
|
@@ -7113,8 +7159,8 @@ function printOverwriteLines(enabled, files, broken, activeSub) {
|
|
|
7113
7159
|
console.log(`${colors.gray("\u8986\u5199: ")}${colors.green("\u5DF2\u542F\u7528")} (${files.length > 0 ? `\u65E0\u751F\u6548\u6587\u4EF6${suffix}` : "\u65E0\u6587\u4EF6"})`);
|
|
7114
7160
|
}
|
|
7115
7161
|
for (const f of unmatched) {
|
|
7116
|
-
const
|
|
7117
|
-
console.log(colors.gray(` ${shortOverwriteName(f.name)} \u4E0D\u9002\u7528\u4E8E\u5F53\u524D\u8BA2\u9605${activeSub ? ` ${activeSub.name}` : ""}\uFF08${
|
|
7162
|
+
const reason = f.kind === "script" ? "\u811A\u672C\u672A\u8FD4\u56DE true" : f.scope ? `\u4F5C\u7528\u57DF ${f.scope}` : "\u4F5C\u7528\u57DF\u53D7\u9650";
|
|
7163
|
+
console.log(colors.gray(` ${shortOverwriteName(f.name)} \u4E0D\u9002\u7528\u4E8E\u5F53\u524D\u8BA2\u9605${activeSub ? ` ${activeSub.name}` : ""}\uFF08${reason}\uFF09`));
|
|
7118
7164
|
}
|
|
7119
7165
|
for (const b of broken) {
|
|
7120
7166
|
console.log(colors.red(` ${b.message}`));
|
|
@@ -7158,17 +7204,11 @@ async function cmdStart(args) {
|
|
|
7158
7204
|
const skipUpdate = hasFlag(args, "-s", "--no-update");
|
|
7159
7205
|
const updateTimeout = parseIntArg(args, "-u", "--update-timeout", DEFAULT_AUTO_UPDATE_TIMEOUT);
|
|
7160
7206
|
let disabledAutoStartForTun = false;
|
|
7161
|
-
|
|
7162
|
-
throw new CliError("\u672A\u627E\u5230\u5185\u6838", { hint: "\u4E0B\u8F7D\u5185\u6838: mihomo-cli kernel" });
|
|
7163
|
-
}
|
|
7207
|
+
assertKernelInstalled();
|
|
7164
7208
|
const serviceBefore = getServiceStatus();
|
|
7165
7209
|
if (targetMode === "tun") {
|
|
7166
7210
|
if (detectLegacySystemInstall()) {
|
|
7167
|
-
|
|
7168
|
-
console.log(colors.gray(" \u6E05\u7406\u9700\u8981\u4E00\u6B21\u7BA1\u7406\u5458\u5BC6\u7801\uFF08\u5220\u9664 root \u62E5\u6709\u7684\u6587\u4EF6\uFF09"));
|
|
7169
|
-
await cleanupLegacyInstallOrThrow();
|
|
7170
|
-
console.log(colors.green("\u5DF2\u6E05\u7406\u9057\u7559\u7684\u7CFB\u7EDF\u7EA7\u670D\u52A1"));
|
|
7171
|
-
console.log("");
|
|
7211
|
+
await announceLegacyCleanupOrThrow("\u542F\u52A8 TUN \u524D\u9700\u6E05\u7406");
|
|
7172
7212
|
}
|
|
7173
7213
|
if (serviceBefore.loaded) {
|
|
7174
7214
|
throw tunBlockedByRunningService();
|
|
@@ -7260,6 +7300,16 @@ async function dispatchSubcommand(args, table, options) {
|
|
|
7260
7300
|
}
|
|
7261
7301
|
return options.fallback(args);
|
|
7262
7302
|
}
|
|
7303
|
+
function unknownSubcommandError(action, table, opts) {
|
|
7304
|
+
if (action.startsWith("-")) {
|
|
7305
|
+
throw new CliError(`\u672A\u77E5\u7684\u9009\u9879: ${action}`, { label: "\u53C2\u6570\u9519\u8BEF", hint: opts.optionHint });
|
|
7306
|
+
}
|
|
7307
|
+
const names = table.flatMap((c) => [c.name, ...c.aliases ?? []]);
|
|
7308
|
+
const suggestion = suggestSimilar(action, names);
|
|
7309
|
+
throw new CliError(`\u672A\u77E5\u7684${opts.what}: ${action}`, {
|
|
7310
|
+
hint: [...suggestion.length > 0 ? [`\u662F\u5426\u60F3\u8F93\u5165: ${suggestion.join(" / ")}?`] : [], ...opts.unknownHint]
|
|
7311
|
+
});
|
|
7312
|
+
}
|
|
7263
7313
|
async function confirmPrompt(question) {
|
|
7264
7314
|
if (!process.stdin.isTTY) return false;
|
|
7265
7315
|
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
@@ -7330,29 +7380,16 @@ async function cmdDirectory(args) {
|
|
|
7330
7380
|
assertKnownFlags(args.slice(1), [], "dir");
|
|
7331
7381
|
await dispatchSubcommand(args, SUBCOMMANDS, {
|
|
7332
7382
|
fallback: printDirectoryInfo,
|
|
7333
|
-
|
|
7334
|
-
|
|
7335
|
-
|
|
7336
|
-
|
|
7337
|
-
|
|
7338
|
-
|
|
7339
|
-
}
|
|
7340
|
-
const names = SUBCOMMANDS.flatMap((c) => [c.name, ...c.aliases ?? []]);
|
|
7341
|
-
const suggestion = suggestSimilar(action, names);
|
|
7342
|
-
throw new CliError(`\u672A\u77E5\u7684\u76EE\u5F55\u5B50\u547D\u4EE4: ${action}`, {
|
|
7343
|
-
hint: [
|
|
7344
|
-
...suggestion.length > 0 ? [`\u662F\u5426\u60F3\u8F93\u5165: ${suggestion.join(" / ")}?`] : [],
|
|
7345
|
-
"",
|
|
7346
|
-
"\u53EF\u7528\u5B50\u547D\u4EE4: open",
|
|
7347
|
-
"\u6253\u5F00\u6307\u5B9A\u76EE\u5F55: mihomo-cli dir open <target>"
|
|
7348
|
-
]
|
|
7349
|
-
});
|
|
7350
|
-
}
|
|
7383
|
+
// 裸 dir 只展示目录信息(选项由分发前的白名单统一拦);守卫收口在 shared
|
|
7384
|
+
onUnknown: (action) => unknownSubcommandError(action, SUBCOMMANDS, {
|
|
7385
|
+
what: "\u76EE\u5F55\u5B50\u547D\u4EE4",
|
|
7386
|
+
optionHint: ["\u88F8 dir \u53EA\u5C55\u793A\u76EE\u5F55\u4FE1\u606F\uFF0C\u4E0D\u63A5\u53D7\u9009\u9879", "", "\u7528\u6CD5: mihomo-cli dir open [root|subs|logs|data|runtime|kernel]"],
|
|
7387
|
+
unknownHint: ["", "\u53EF\u7528\u5B50\u547D\u4EE4: open", "\u6253\u5F00\u6307\u5B9A\u76EE\u5F55: mihomo-cli dir open <target>"]
|
|
7388
|
+
})
|
|
7351
7389
|
});
|
|
7352
7390
|
}
|
|
7353
7391
|
|
|
7354
7392
|
// src/commands/doctor.ts
|
|
7355
|
-
import { spawnSync as spawnSync10 } from "child_process";
|
|
7356
7393
|
import fs16 from "fs";
|
|
7357
7394
|
|
|
7358
7395
|
// src/kernel.ts
|
|
@@ -7537,7 +7574,7 @@ async function getLatestRelease(repo, opts = { proxy: null, useGh: false }) {
|
|
|
7537
7574
|
return pickLatestRelease(response.data);
|
|
7538
7575
|
}
|
|
7539
7576
|
async function checkUpdate(opts) {
|
|
7540
|
-
const currentVersion = getKernelVersion();
|
|
7577
|
+
const currentVersion = opts.currentVersion !== void 0 ? opts.currentVersion : getKernelVersion();
|
|
7541
7578
|
let ghFallbackToDirect = false;
|
|
7542
7579
|
const optsWithFallbackNote = {
|
|
7543
7580
|
...opts,
|
|
@@ -7627,7 +7664,7 @@ function resolveFallbackQueryOptions(channel) {
|
|
|
7627
7664
|
}
|
|
7628
7665
|
async function downloadKernel(progressCallback, channel, releaseInfo) {
|
|
7629
7666
|
ensureDirs();
|
|
7630
|
-
const apiProxy = channel.kind === "
|
|
7667
|
+
const apiProxy = channel.kind === "direct" ? null : channel.proxy ?? null;
|
|
7631
7668
|
const latest = releaseInfo || await getLatestRelease(GITHUB_REPO, resolveFallbackQueryOptions(channel));
|
|
7632
7669
|
const arch = getArch();
|
|
7633
7670
|
const platform = process.platform;
|
|
@@ -7642,7 +7679,7 @@ async function downloadKernel(progressCallback, channel, releaseInfo) {
|
|
|
7642
7679
|
}
|
|
7643
7680
|
assertTrustedAssetUrl(asset.browser_download_url);
|
|
7644
7681
|
const downloadUrl = channel.kind === "mirror" ? withMirror(asset.browser_download_url, channel.mirror) : asset.browser_download_url;
|
|
7645
|
-
const mirrorIneffective = channel.kind === "mirror" &&
|
|
7682
|
+
const mirrorIneffective = channel.kind === "mirror" && downloadUrl === asset.browser_download_url;
|
|
7646
7683
|
if (mirrorIneffective) {
|
|
7647
7684
|
console.warn("\u8B66\u544A: \u955C\u50CF\u524D\u7F00\u672A\u80FD\u4F5C\u7528\u4E8E\u8BE5\u8D44\u4EA7\u5730\u5740\uFF08host \u975E github.com\uFF09\uFF0C\u672C\u6B21\u4E3A\u76F4\u8FDE\u4E0B\u8F7D\uFF1B\u82E5\u76F4\u8FDE\u8D85\u65F6\u53EF\u6539\u7528 gh \u901A\u9053\uFF08mihomo-cli kernel\uFF09");
|
|
7648
7685
|
}
|
|
@@ -7727,16 +7764,14 @@ async function downloadKernel(progressCallback, channel, releaseInfo) {
|
|
|
7727
7764
|
progressCallback("\u6821\u9A8C\u5185\u6838...");
|
|
7728
7765
|
}
|
|
7729
7766
|
fs15.chmodSync(foundBinary, 493);
|
|
7730
|
-
const
|
|
7731
|
-
|
|
7732
|
-
if (check.error || check.status !== 0 || !/v?\d+\.\d+\.\d+/.test(checkOutput)) {
|
|
7767
|
+
const probe = probeKernelVersion(foundBinary);
|
|
7768
|
+
if (probe.spawnError || probe.status !== 0 || probe.version === null) {
|
|
7733
7769
|
throw new Error(`\u5185\u6838\u81EA\u68C0\u5931\u8D25\uFF08\u53EF\u80FD\u4E0B\u8F7D\u635F\u574F\u6216\u67B6\u6784\u4E0D\u5339\u914D\uFF09\uFF0C\u65E7\u5185\u6838\u672A\u53D7\u5F71\u54CD
|
|
7734
|
-
\u9000\u51FA\u7801: ${
|
|
7735
|
-
\u8F93\u51FA: ${
|
|
7770
|
+
\u9000\u51FA\u7801: ${probe.status}
|
|
7771
|
+
\u8F93\u51FA: ${probe.output || "(\u7A7A)"}`);
|
|
7736
7772
|
}
|
|
7737
|
-
|
|
7738
|
-
|
|
7739
|
-
const binaryVersion = versionMatch[1];
|
|
7773
|
+
if (probe.version && latest.tag_name) {
|
|
7774
|
+
const binaryVersion = probe.version.replace(/^v/, "");
|
|
7740
7775
|
const expectedVersion = latest.tag_name.replace(/^v/, "");
|
|
7741
7776
|
if (binaryVersion !== expectedVersion) {
|
|
7742
7777
|
throw new Error(`\u5185\u6838\u7248\u672C\u4E0D\u5339\u914D\uFF08\u671F\u671B ${expectedVersion}\uFF0C\u5B9E\u9645 ${binaryVersion}\uFF09\uFF0C\u65E7\u5185\u6838\u672A\u53D7\u5F71\u54CD`);
|
|
@@ -7872,12 +7907,7 @@ function withAbortableTimeout(promise, ms) {
|
|
|
7872
7907
|
return promise(controller.signal).finally(() => clearTimeout(timer));
|
|
7873
7908
|
}
|
|
7874
7909
|
function isPortListening(port) {
|
|
7875
|
-
|
|
7876
|
-
const r = spawnSync10("lsof", ["-nP", `-iTCP:${port}`, "-sTCP:LISTEN"], { encoding: "utf8", timeout: 5e3 });
|
|
7877
|
-
return r.status === 0 && (r.stdout || "").trim().length > 0;
|
|
7878
|
-
} catch {
|
|
7879
|
-
return false;
|
|
7880
|
-
}
|
|
7910
|
+
return (lsofListenPids(port)?.length ?? 0) > 0;
|
|
7881
7911
|
}
|
|
7882
7912
|
async function collectChecks() {
|
|
7883
7913
|
const checks = [];
|
|
@@ -7885,33 +7915,27 @@ async function collectChecks() {
|
|
|
7885
7915
|
checks.push({ name, status, detail, fix, notes });
|
|
7886
7916
|
};
|
|
7887
7917
|
const latestVersionPromise = getLatestNpmVersion(4e3).catch(() => null);
|
|
7888
|
-
const
|
|
7889
|
-
|
|
7890
|
-
|
|
7891
|
-
|
|
7892
|
-
|
|
7893
|
-
kernelProxyPort = null;
|
|
7894
|
-
}
|
|
7895
|
-
const kernelVersionPromise = hasKernel() ? withAbortableTimeout(
|
|
7918
|
+
const earlyService = getServiceStatus();
|
|
7919
|
+
const earlyState = getRunningState(earlyService);
|
|
7920
|
+
const kernelProxyPort = earlyState.running ? getMixedPortOrNull() : null;
|
|
7921
|
+
const kernelProbe = hasKernel() ? probeKernelVersion() : null;
|
|
7922
|
+
const kernelVersionPromise = kernelProbe ? withAbortableTimeout(
|
|
7896
7923
|
(signal) => checkUpdate({
|
|
7897
7924
|
proxy: kernelProxyPort !== null ? `http://127.0.0.1:${kernelProxyPort}` : null,
|
|
7898
7925
|
useGh: kernelProxyPort === null && hasGh(),
|
|
7899
|
-
signal
|
|
7926
|
+
signal,
|
|
7927
|
+
currentVersion: getKernelVersion(kernelProbe)
|
|
7900
7928
|
}),
|
|
7901
7929
|
4e3
|
|
7902
7930
|
).catch(() => null) : Promise.resolve(null);
|
|
7903
|
-
if (!
|
|
7931
|
+
if (!kernelProbe) {
|
|
7904
7932
|
push("\u5185\u6838", "fail", "\u672A\u5B89\u88C5", "mihomo-cli kernel");
|
|
7933
|
+
} else if (kernelProbe.status === 0 && kernelProbe.version !== null) {
|
|
7934
|
+
push("\u5185\u6838", "ok", getKernelVersion(kernelProbe) || "\u53EF\u6267\u884C");
|
|
7935
|
+
} else if (kernelProbe.spawnError) {
|
|
7936
|
+
push("\u5185\u6838", "fail", `\u4E8C\u8FDB\u5236\u65E0\u6CD5\u6267\u884C\uFF08${kernelProbe.spawnError.message}\uFF09`, "\u91CD\u65B0\u4E0B\u8F7D: mihomo-cli kernel");
|
|
7905
7937
|
} else {
|
|
7906
|
-
|
|
7907
|
-
const r = spawnSync10(PATHS.mihomoBinary, ["-v"], { encoding: "utf8", timeout: 5e3 });
|
|
7908
|
-
if (r.status === 0 && /v?\d+\.\d+\.\d+/.test(`${r.stdout}${r.stderr}`)) {
|
|
7909
|
-
push("\u5185\u6838", "ok", v || "\u53EF\u6267\u884C");
|
|
7910
|
-
} else if (r.error) {
|
|
7911
|
-
push("\u5185\u6838", "fail", `\u4E8C\u8FDB\u5236\u65E0\u6CD5\u6267\u884C\uFF08${r.error.message}\uFF09`, "\u91CD\u65B0\u4E0B\u8F7D: mihomo-cli kernel");
|
|
7912
|
-
} else {
|
|
7913
|
-
push("\u5185\u6838", "fail", `\u4E8C\u8FDB\u5236\u65E0\u6CD5\u6267\u884C\uFF08\u9000\u51FA\u7801 ${r.status}\uFF09`, "\u91CD\u65B0\u4E0B\u8F7D: mihomo-cli kernel");
|
|
7914
|
-
}
|
|
7938
|
+
push("\u5185\u6838", "fail", `\u4E8C\u8FDB\u5236\u65E0\u6CD5\u6267\u884C\uFF08\u9000\u51FA\u7801 ${kernelProbe.status}\uFF09`, "\u91CD\u65B0\u4E0B\u8F7D: mihomo-cli kernel");
|
|
7915
7939
|
}
|
|
7916
7940
|
if (fs16.existsSync(KERNEL_SELF_UPDATE_DIR)) {
|
|
7917
7941
|
push("\u5185\u6838\u81EA\u5347\u7EA7\u6B8B\u7559", "warn", `\u4E2D\u65AD\u7684\u4E0B\u8F7D\u6682\u5B58: ${KERNEL_SELF_UPDATE_DIR}`, `rm -rf ${KERNEL_SELF_UPDATE_DIR}`);
|
|
@@ -7941,18 +7965,12 @@ async function collectChecks() {
|
|
|
7941
7965
|
} else {
|
|
7942
7966
|
push("\u8BA2\u9605", "ok", `${subs.length} \u4E2A${active ? `\uFF0C\u5F53\u524D: ${active.name}` : ""}`);
|
|
7943
7967
|
if (active) {
|
|
7944
|
-
let rawConfig;
|
|
7945
7968
|
try {
|
|
7946
|
-
|
|
7947
|
-
} catch (e) {
|
|
7948
|
-
rawConfig = e;
|
|
7949
|
-
}
|
|
7950
|
-
if (rawConfig instanceof Error) {
|
|
7951
|
-
push("\u8BA2\u9605\u914D\u7F6E", "fail", `${rawConfig.message}`, "\u624B\u5DE5\u4FEE\u6B63 settings.json \u4E2D\u7684\u8BA2\u9605\u540D\u540E\u91CD\u8BD5");
|
|
7952
|
-
} else if (!rawConfig) {
|
|
7953
|
-
push("\u8BA2\u9605\u914D\u7F6E", "fail", `\u5F53\u524D\u8BA2\u9605 "${active.name}" \u6709\u6761\u76EE\u4F46\u65E0\u914D\u7F6E\u6587\u4EF6`, `mihomo-cli sub update ${active.name}`);
|
|
7954
|
-
} else {
|
|
7969
|
+
requireSubscriptionRawConfig(active.name);
|
|
7955
7970
|
push("\u8BA2\u9605\u914D\u7F6E", "ok", `"${active.name}" \u914D\u7F6E\u6587\u4EF6\u5B58\u5728`);
|
|
7971
|
+
} catch (e) {
|
|
7972
|
+
const err = e;
|
|
7973
|
+
push("\u8BA2\u9605\u914D\u7F6E", "fail", err.message, err.hint[0] ?? "\u624B\u5DE5\u4FEE\u6B63 settings.json \u4E2D\u7684\u8BA2\u9605\u540D\u540E\u91CD\u8BD5");
|
|
7956
7974
|
}
|
|
7957
7975
|
const cached = subs.find((s) => s.name === active.name);
|
|
7958
7976
|
if (cached?.updated_at) {
|
|
@@ -7965,7 +7983,7 @@ async function collectChecks() {
|
|
|
7965
7983
|
}
|
|
7966
7984
|
}
|
|
7967
7985
|
}
|
|
7968
|
-
const service =
|
|
7986
|
+
const service = earlyService;
|
|
7969
7987
|
const legacy = detectLegacySystemInstall();
|
|
7970
7988
|
if (legacy) {
|
|
7971
7989
|
push("\u670D\u52A1", "fail", "\u68C0\u6D4B\u5230\u65E7\u7248\u672C\u7684\u7CFB\u7EDF\u7EA7\u670D\u52A1\uFF08root LaunchDaemon\uFF09\uFF0C\u4F1A\u62A2\u5360\u7AEF\u53E3", "mihomo-cli uninstall\uFF08\u9700\u4E00\u6B21\u7BA1\u7406\u5458\u5BC6\u7801\uFF09");
|
|
@@ -7987,7 +8005,7 @@ async function collectChecks() {
|
|
|
7987
8005
|
push("\u670D\u52A1", "ok", `\u5DF2\u5B89\u88C5\uFF0C\u672A\u8FD0\u884C${service.disabled ? "\uFF08\u81EA\u542F\u5DF2\u5173\u95ED\uFF09" : ""}`);
|
|
7988
8006
|
}
|
|
7989
8007
|
}
|
|
7990
|
-
const state =
|
|
8008
|
+
const state = earlyState;
|
|
7991
8009
|
const info = getConfigInfo();
|
|
7992
8010
|
let mixedPortDefault = DEFAULT_MIXED_PORT;
|
|
7993
8011
|
try {
|
|
@@ -8010,7 +8028,7 @@ async function collectChecks() {
|
|
|
8010
8028
|
}
|
|
8011
8029
|
if (active && hasKernel()) {
|
|
8012
8030
|
try {
|
|
8013
|
-
const mode = info
|
|
8031
|
+
const mode = deriveRuntimeMode(info);
|
|
8014
8032
|
const prepared = await prepareConfigForStart(mode, active.name);
|
|
8015
8033
|
const warnings = prepared.buildResult.warnings;
|
|
8016
8034
|
if (warnings.length > 0) {
|
|
@@ -8298,14 +8316,7 @@ async function cmdKernel(args) {
|
|
|
8298
8316
|
});
|
|
8299
8317
|
}
|
|
8300
8318
|
const proxyRunning = getRunningState().running;
|
|
8301
|
-
|
|
8302
|
-
if (proxyRunning) {
|
|
8303
|
-
try {
|
|
8304
|
-
proxyPort = getPorts().mixed;
|
|
8305
|
-
} catch {
|
|
8306
|
-
proxyPort = null;
|
|
8307
|
-
}
|
|
8308
|
-
}
|
|
8319
|
+
const proxyPort = proxyRunning ? getMixedPortOrNull() : null;
|
|
8309
8320
|
const forceDirect = mirrorInfo.isOverride && !mirrorInfo.mirror;
|
|
8310
8321
|
const ghAvailable = ghProbeNeeded({ forceDirect, proxyOverride: proxyInfo.proxy }) && hasGh();
|
|
8311
8322
|
const channels = resolveDownloadChannels({
|
|
@@ -8512,7 +8523,7 @@ function printOverwriteList() {
|
|
|
8512
8523
|
console.log(` ${colors.gray("\u4F5C\u7528\u57DF: ")}${f.scope}`);
|
|
8513
8524
|
}
|
|
8514
8525
|
if (f.kind === "script") {
|
|
8515
|
-
console.log(` ${colors.gray("\u7C7B\u578B: ")}JS \u811A\u672C\uFF08\u5728\u5168\u90E8 YAML \u8986\u5199\u4E4B\u524D\u6267\u884C\uFF09`);
|
|
8526
|
+
console.log(` ${colors.gray("\u7C7B\u578B: ")}JS \u811A\u672C\uFF08\u5728\u5168\u90E8 YAML \u8986\u5199\u4E4B\u524D\u6267\u884C\uFF1B\u9002\u7528\u4E8E\u5F53\u524D\u8BA2\u9605\u65F6\u672B\u5C3E return true\uFF0Cstatus \u636E\u6B64\u533A\u5206\u751F\u6548\u4E0E\u4E0D\u9002\u7528\uFF09`);
|
|
8516
8527
|
} else if (f.keys.length > 0) {
|
|
8517
8528
|
console.log(` ${colors.gray("\u5B57\u6BB5: ")}${f.keys.join(", ")}`);
|
|
8518
8529
|
}
|
|
@@ -8556,24 +8567,17 @@ var SUBCOMMANDS2 = [
|
|
|
8556
8567
|
async function cmdOverwrite(args) {
|
|
8557
8568
|
assertKnownFlags(args, ["-s", "--no-update", "-u", "--update-timeout"], "ow [on|off]");
|
|
8558
8569
|
await dispatchSubcommand(args, SUBCOMMANDS2, {
|
|
8559
|
-
// 无子命令 → 列表;未知子命令 →
|
|
8570
|
+
// 无子命令 → 列表;未知子命令 → 报错(守卫与 did-you-mean 拼装收口在 shared)
|
|
8560
8571
|
fallback: () => {
|
|
8561
8572
|
console.log("");
|
|
8562
8573
|
printOverwriteList();
|
|
8563
8574
|
},
|
|
8564
|
-
onUnknown: (action) => {
|
|
8565
|
-
|
|
8566
|
-
|
|
8567
|
-
|
|
8568
|
-
|
|
8569
|
-
|
|
8570
|
-
}
|
|
8571
|
-
const names = SUBCOMMANDS2.flatMap((c) => [c.name, ...c.aliases ?? []]);
|
|
8572
|
-
const suggestion = suggestSimilar(action, names);
|
|
8573
|
-
throw new CliError(`\u672A\u77E5\u7684\u8986\u5199\u5B50\u547D\u4EE4: ${action}`, {
|
|
8574
|
-
hint: [...suggestion.length > 0 ? [`\u662F\u5426\u60F3\u8F93\u5165: ${suggestion.join(" / ")}?`] : [], "", "\u53EF\u7528\u5B50\u547D\u4EE4: on, off"]
|
|
8575
|
-
});
|
|
8576
|
-
}
|
|
8575
|
+
onUnknown: (action) => unknownSubcommandError(action, SUBCOMMANDS2, {
|
|
8576
|
+
what: "\u8986\u5199\u5B50\u547D\u4EE4",
|
|
8577
|
+
// 重启透传选项必须跟在 on/off 后,裸 ow 不消费任何选项
|
|
8578
|
+
optionHint: ["\u88F8 ow \u53EA\u67E5\u770B\u8986\u5199\u72B6\u6001\uFF0C\u4E0D\u63A5\u53D7\u9009\u9879", "", "\u7528\u6CD5: mihomo-cli ow on|off [-s] [-u ms]"],
|
|
8579
|
+
unknownHint: ["", "\u53EF\u7528\u5B50\u547D\u4EE4: on, off"]
|
|
8580
|
+
})
|
|
8577
8581
|
});
|
|
8578
8582
|
}
|
|
8579
8583
|
|
|
@@ -8674,7 +8678,7 @@ async function cmdReset(args) {
|
|
|
8674
8678
|
if (cleanup.remaining.length > 0) {
|
|
8675
8679
|
throw new CliError(cleanup.remaining.join(", "), {
|
|
8676
8680
|
label: "\u8FDB\u7A0B\u672A\u80FD\u505C\u6B62\uFF0C\u91CD\u7F6E\u4E2D\u6B62",
|
|
8677
|
-
hint: [
|
|
8681
|
+
hint: [MANUAL_PKILL_HINT]
|
|
8678
8682
|
});
|
|
8679
8683
|
}
|
|
8680
8684
|
if (classifyResidueCleanup(cleanup) === "warn") {
|
|
@@ -8731,13 +8735,7 @@ async function cmdReset(args) {
|
|
|
8731
8735
|
import fs18 from "fs";
|
|
8732
8736
|
async function handleLegacyInstall() {
|
|
8733
8737
|
if (!detectLegacySystemInstall()) return;
|
|
8734
|
-
|
|
8735
|
-
console.log(colors.gray(" \u5B83\u4F1A\u4E0E\u65B0\u7684\u7528\u6237\u7EA7\u670D\u52A1\u62A2\u5360\u540C\u4E00\u7EC4\u7AEF\u53E3\uFF0C\u9700\u5148\u6E05\u7406"));
|
|
8736
|
-
console.log(colors.gray(" \u6E05\u7406\u9700\u8981\u4E00\u6B21\u7BA1\u7406\u5458\u5BC6\u7801\uFF08\u5220\u9664 root \u62E5\u6709\u7684\u6587\u4EF6\uFF09"));
|
|
8737
|
-
console.log("");
|
|
8738
|
-
await cleanupLegacyInstallOrThrow();
|
|
8739
|
-
console.log(`${colors.green("\u5DF2\u6E05\u7406\u9057\u7559\u7684\u7CFB\u7EDF\u7EA7\u670D\u52A1")}`);
|
|
8740
|
-
console.log("");
|
|
8738
|
+
await announceLegacyCleanupOrThrow("\u9700\u5148\u6E05\u7406", [" \u5B83\u4F1A\u4E0E\u65B0\u7684\u7528\u6237\u7EA7\u670D\u52A1\u62A2\u5360\u540C\u4E00\u7EC4\u7AEF\u53E3"]);
|
|
8741
8739
|
}
|
|
8742
8740
|
function printRestoreSkipped() {
|
|
8743
8741
|
console.log(colors.yellow("\u672A\u6062\u590D\u8FD0\u884C\uFF1A\u5B89\u88C5\u671F\u95F4\u68C0\u6D4B\u5230\u505C\u6B62\u64CD\u4F5C"));
|
|
@@ -8748,9 +8746,7 @@ function printRestoreSkipped() {
|
|
|
8748
8746
|
async function cmdInstall(args) {
|
|
8749
8747
|
assertKnownFlags(args.slice(1), [], "install");
|
|
8750
8748
|
assertPositionalCount(args, 0, 1, "mihomo-cli install");
|
|
8751
|
-
|
|
8752
|
-
throw new CliError("\u672A\u627E\u5230\u5185\u6838", { hint: "\u4E0B\u8F7D\u5185\u6838: mihomo-cli kernel" });
|
|
8753
|
-
}
|
|
8749
|
+
assertKernelInstalled();
|
|
8754
8750
|
await handleLegacyInstall();
|
|
8755
8751
|
const wasRunning = getServiceStatus().running;
|
|
8756
8752
|
const { restoreSkipped } = await installService(wasRunning);
|
|
@@ -8804,15 +8800,13 @@ async function cmdUninstall(args) {
|
|
|
8804
8800
|
console.log(colors.green("\u5DF2\u5378\u8F7D\u670D\u52A1"));
|
|
8805
8801
|
}
|
|
8806
8802
|
if (legacy) {
|
|
8807
|
-
|
|
8808
|
-
await cleanupLegacyInstallOrThrow();
|
|
8809
|
-
console.log(colors.green("\u5DF2\u6E05\u7406\u9057\u7559\u7684\u7CFB\u7EDF\u7EA7\u670D\u52A1"));
|
|
8803
|
+
await announceLegacyCleanupOrThrow("\u5378\u8F7D\u65F6\u4E00\u5E76\u6E05\u7406");
|
|
8810
8804
|
}
|
|
8811
8805
|
const remaining = getMihomoPids();
|
|
8812
8806
|
if (remaining.length > 0) {
|
|
8813
8807
|
console.log("");
|
|
8814
8808
|
console.log(colors.yellow(`\u4ECD\u6709\u5185\u6838\u8FDB\u7A0B\u6B8B\u7559 (PID ${remaining.join(", ")})`));
|
|
8815
|
-
console.log(
|
|
8809
|
+
console.log(MANUAL_PKILL_HINT);
|
|
8816
8810
|
}
|
|
8817
8811
|
console.log(colors.gray("\u91CD\u65B0\u5B89\u88C5: mihomo-cli install"));
|
|
8818
8812
|
console.log(colors.gray("\u5F7B\u5E95\u79FB\u9664 mihomo-cli:"));
|
|
@@ -8824,18 +8818,14 @@ async function cmdUninstall(args) {
|
|
|
8824
8818
|
// src/commands/stop.ts
|
|
8825
8819
|
function handleStopResult(result) {
|
|
8826
8820
|
if (result.remaining && result.remaining.length > 0) {
|
|
8827
|
-
throw new CliError(result.remaining.join(", "), { label: "\u90E8\u5206\u8FDB\u7A0B\u672A\u7EC8\u6B62", hint:
|
|
8821
|
+
throw new CliError(result.remaining.join(", "), { label: "\u90E8\u5206\u8FDB\u7A0B\u672A\u7EC8\u6B62", hint: MANUAL_PKILL_HINT });
|
|
8828
8822
|
}
|
|
8829
8823
|
}
|
|
8830
8824
|
async function cmdStop(args) {
|
|
8831
8825
|
assertKnownFlags(args.slice(1), [], "stop");
|
|
8832
8826
|
assertPositionalCount(args, 0, 1, "mihomo-cli stop");
|
|
8833
8827
|
if (detectLegacySystemInstall()) {
|
|
8834
|
-
|
|
8835
|
-
console.log(colors.gray(" \u6E05\u7406\u9700\u8981\u4E00\u6B21\u7BA1\u7406\u5458\u5BC6\u7801\uFF08\u5220\u9664 root \u62E5\u6709\u7684\u6587\u4EF6\uFF09"));
|
|
8836
|
-
await cleanupLegacyInstallOrThrow();
|
|
8837
|
-
console.log(colors.green("\u5DF2\u6E05\u7406\u9057\u7559\u7684\u7CFB\u7EDF\u7EA7\u670D\u52A1"));
|
|
8838
|
-
console.log("");
|
|
8828
|
+
await announceLegacyCleanupOrThrow("\u505C\u6B62\u524D\u9700\u6E05\u7406");
|
|
8839
8829
|
}
|
|
8840
8830
|
const status = getServiceStatus();
|
|
8841
8831
|
const pids = getMihomoPids();
|
|
@@ -8858,7 +8848,7 @@ async function cmdStop(args) {
|
|
|
8858
8848
|
}
|
|
8859
8849
|
|
|
8860
8850
|
// src/commands/subscription.ts
|
|
8861
|
-
import { spawnSync as
|
|
8851
|
+
import { spawnSync as spawnSync10 } from "child_process";
|
|
8862
8852
|
function printRestartHintIfRunning(variant = "update") {
|
|
8863
8853
|
const state = getRunningState();
|
|
8864
8854
|
if (state.running) {
|
|
@@ -8913,7 +8903,7 @@ function printSubscriptionList() {
|
|
|
8913
8903
|
function readUrlFromClipboard() {
|
|
8914
8904
|
if (!process.stdin.isTTY) return null;
|
|
8915
8905
|
try {
|
|
8916
|
-
const r =
|
|
8906
|
+
const r = spawnSync10("pbpaste", [], { encoding: "utf8", timeout: 3e3 });
|
|
8917
8907
|
if (r.status !== 0) return null;
|
|
8918
8908
|
const text = (r.stdout || "").trim();
|
|
8919
8909
|
return isValidHttpUrl(text) ? text : null;
|
|
@@ -9092,26 +9082,18 @@ var SUBCOMMANDS3 = [
|
|
|
9092
9082
|
];
|
|
9093
9083
|
async function cmdSubscription(args) {
|
|
9094
9084
|
await dispatchSubcommand(args, SUBCOMMANDS3, {
|
|
9095
|
-
// 无子命令 → 列表;未知子命令 →
|
|
9085
|
+
// 无子命令 → 列表;未知子命令 → 报错(守卫与 did-you-mean 拼装收口在 shared)
|
|
9096
9086
|
fallback: printSubscriptionList,
|
|
9097
|
-
onUnknown: (action) => {
|
|
9098
|
-
|
|
9099
|
-
|
|
9100
|
-
|
|
9101
|
-
|
|
9102
|
-
});
|
|
9103
|
-
}
|
|
9104
|
-
const names = SUBCOMMANDS3.flatMap((c) => [c.name, ...c.aliases ?? []]);
|
|
9105
|
-
const suggestion = suggestSimilar(action, names);
|
|
9106
|
-
throw new CliError(`\u672A\u77E5\u7684\u8BA2\u9605\u547D\u4EE4: ${action}`, {
|
|
9107
|
-
hint: [...suggestion.length > 0 ? [`\u662F\u5426\u60F3\u8F93\u5165: ${suggestion.join(" / ")}?`] : [], "\u7528\u6CD5: mihomo-cli sub [use|add|update|remove]\uFF08\u88F8 sub \u5373\u5217\u8868\uFF09"]
|
|
9108
|
-
});
|
|
9109
|
-
}
|
|
9087
|
+
onUnknown: (action) => unknownSubcommandError(action, SUBCOMMANDS3, {
|
|
9088
|
+
what: "\u8BA2\u9605\u547D\u4EE4",
|
|
9089
|
+
optionHint: ["\u88F8 sub \u53EA\u5217\u51FA\u8BA2\u9605\uFF0C\u4E0D\u63A5\u53D7\u9009\u9879", "", "\u7528\u6CD5: mihomo-cli sub [use|add|update|remove]\uFF08\u88F8 sub \u5373\u5217\u8868\uFF09"],
|
|
9090
|
+
unknownHint: ["\u7528\u6CD5: mihomo-cli sub [use|add|update|remove]\uFF08\u88F8 sub \u5373\u5217\u8868\uFF09"]
|
|
9091
|
+
})
|
|
9110
9092
|
});
|
|
9111
9093
|
}
|
|
9112
9094
|
|
|
9113
9095
|
// src/commands/ui.ts
|
|
9114
|
-
import { spawnSync as
|
|
9096
|
+
import { spawnSync as spawnSync11 } from "child_process";
|
|
9115
9097
|
function resolveUiName(args) {
|
|
9116
9098
|
const raw = getNonFlagArg(args, 1);
|
|
9117
9099
|
const name = (raw ?? "zash").toLowerCase();
|
|
@@ -9122,7 +9104,7 @@ function resolveUiName(args) {
|
|
|
9122
9104
|
}
|
|
9123
9105
|
function copyToClipboard(text) {
|
|
9124
9106
|
try {
|
|
9125
|
-
return
|
|
9107
|
+
return spawnSync11("pbcopy", [], { input: text }).status === 0;
|
|
9126
9108
|
} catch {
|
|
9127
9109
|
return false;
|
|
9128
9110
|
}
|