mihomo-cli 26.10.106 → 26.10.108
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 +27 -20
- package/dist/index.js +237 -150
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,32 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [26.10.108] - 2026-10-04
|
|
4
|
+
|
|
5
|
+
### 修复
|
|
6
|
+
|
|
7
|
+
- **`ui -c` 写剪贴板加 3 秒超时**:剪贴板服务(pboard)短暂无响应时 pbcopy 不再让命令无限挂死、Ctrl+C 前无任何输出,与读剪贴板侧既有超时对称
|
|
8
|
+
|
|
9
|
+
### 变更(内部加固,无用户可见变化)
|
|
10
|
+
|
|
11
|
+
- **手改 cache.json 的坏条目按无缓存处理**:单条被写成字符串/数字时不再展开成数字键垃圾对象,与 saveSubscriptionCache 同判据(新增不变量用例锁住);`lsofListenPids` 注释如实写明 null/[] 三态不可达(lsof 对「无匹配」与 fatal 同退 1,消费方不得假设二者之别);覆写文件排序键改为每文件只构建一次。961 用例全绿
|
|
12
|
+
|
|
13
|
+
## [26.10.107] - 2026-10-04
|
|
14
|
+
|
|
15
|
+
### 修复
|
|
16
|
+
|
|
17
|
+
- **curl 代理通道不再被 no_proxy 例外表静默改写**:shell 里 export 过 `no_proxy='*'` 或含目标域条目时,curl 会绕过显式 `-x` 直连——版本查询、内核下载、连通性探测三处 spawn 统一置空 per-spawn env 的 no_proxy/NO_PROXY(`curl-spawn.ts` 唯一出口,新增加 curl 必须经此);direct 通道显式 `--noproxy '*'` 堵住另一类分叉——curl 默认读 `https_proxy` 等 env 代理,此前 `--mirror direct` 的「强制直连」实际会经 shell 代理出网,两条出网路径分叉、故障时还把原因误诊成直连被墙
|
|
18
|
+
- **订阅下载重定向逐跳校验 https**:改手动逐跳跟随、每一跳校验协议并设 20 跳上限,堵「先降 http 再跳回 https」的中间明文 hop——订阅 URL 的 token 常被服务器保留在重定向查询串里,旧守卫只看最终地址挡不住;错误消息对降级地址脱敏
|
|
19
|
+
- **启动 TUN 失败的恢复指引扩面**:关掉服务自启后任何一步失败(订阅校验/自动更新/配置构建/启动本身)都在错误里带「恢复 Mixed: `mihomo-cli start`」,此前只有启动失败分支带——只报「配置错误」会让用户以为一切照旧,下次开机才发现代理没回来
|
|
20
|
+
- **status/doctor 对坏状态容错**:手改 settings.json 写入非法订阅名不再击穿 status 整屏报错;doctor 内核版本检查失败带真实原因(「全部是预发行版」「未找到 curl」不再一律渲染成「GitHub 不可达」);start 成功后的系统代理提示撞上坏 ports 配置时降级默认端口提示,不再把成功命令翻成 exit 1
|
|
21
|
+
- **`sub remove` 删最后一个订阅保留运行中提示**:此前按「切换目标」判定会把提示整个吞掉;没有可切换的新订阅时提示改为「(订阅已全部删除),重新添加订阅后执行 mihomo-cli start tun」
|
|
22
|
+
- **`reset service` 补残留进程提示**:纯重置服务此前把卸载透传的残留进程丢弃、「已重置: 服务」成了谎报,现在与 uninstall 同款式黄字列出 PID
|
|
23
|
+
- **覆写键 `++key` 加载即报错**:`++rules`/`rules++` 这类笔误此前静默落成字面顶层键不生效(内核对未知键宽容),`++secret` 还绕过锁定键告警;与 `key!+` 同口径报「操作符位置矛盾」
|
|
24
|
+
- **其余并发与容错修正**:内核配置校验的临时 stage 迁出会被整目录删除的 runtime/(并发 stop/reset 曾把校验配置一起删掉,把误诊指向毫无问题的订阅/覆写);sudo 脚本名加 pid 后缀,双终端并发跑同一动作不再互踩「密码正确却报已取消」;启动过程中订阅被并发移除时给「已被移除」分档报错,不再指引必败的 `sub update <已删除名>`;热重载的控制器端口与密钥改同一份快照读取(settings 被原子替换时不再发新端口带旧密钥);`-h` 提示文案与实际命令级帮助行为对齐
|
|
25
|
+
|
|
26
|
+
### 变更(内部清理,无用户可见变化)
|
|
27
|
+
|
|
28
|
+
- **/simplify 三轮收尾**:`firstLine` 收编全仓 12 处 `split('\n')[0]` 副本;卸载/重置残留提示与 bootout 未装载容忍码(3/113)收单点(TS 判定与 legacy bash 脚本生成共用一份码表);控制器端口+密钥快照读收 `getControllerEndpoint`;CliError 再包装收 `relabelCliError`(exitCode 透传在结构上不可能丢);「桩 curl 记录 no_proxy」端到端夹具收 test-support 新模块(三份逐字骨架合一);http 手动跟随重定收单循环。960 用例全绿
|
|
29
|
+
|
|
3
30
|
## [26.10.106] - 2026-10-03
|
|
4
31
|
|
|
5
32
|
### 修复(覆写提示降噪)
|
|
@@ -188,23 +215,3 @@
|
|
|
188
215
|
- help.spec 补整页帮助的别名行(列出全部入口)与快捷命令节(tun/use/restart)断言;log.ts 归档文件名注释修正;`mihomo-cli ui` 的运行态查询无容错记入 CODE_REVIEW(纯打开操作、失败重跑成本为零)
|
|
189
216
|
- `OverwriteScriptContext.warn` 注释修正为现行口径:仅 `start`/`config`/`doctor` 可见,`status` 走诊断旁路不执行脚本
|
|
190
217
|
- sudo 密码窗口(spawnSync 最长 60s)期间 Ctrl+C 无响应的已知权衡记入 CODE_REVIEW
|
|
191
|
-
|
|
192
|
-
## [26.10.98] - 2026-10-02
|
|
193
|
-
|
|
194
|
-
### 新增
|
|
195
|
-
|
|
196
|
-
- **doctor 检查面板自升级残留**:在面板里升级内核(`POST /upgrade`)后,内核会在 kernel 目录留下 `meta-backup`(旧内核备份),异常中断时还会遗留 `meta-update`(下载暂存);doctor 会提示目录位置、区分两者性质(备份可留作回滚、暂存可安全删)并给出删除命令
|
|
197
|
-
|
|
198
|
-
### 改进
|
|
199
|
-
|
|
200
|
-
- **内核下载本机代理优先、失败自动换通道**:`mihomo kernel` 在本机代理运行时默认经代理下载,失败自动回退 gh;全部失败时汇总各通道原因,并提示可在面板手动换节点。显式 `--mirror`/`--proxy` 仍严格按指定通道、不自动切换
|
|
201
|
-
- **劣质节点下载不再长时间干等**:节点握手延迟低但实际带宽极低时(实测约 20 KB/s),此前 gh 通道会卡满 210 秒;现代理通道在 20 秒内平均速度低于 50 KB/s 即快速失败,gh 通道超时也缩短到 100 秒。超时或低速失败给出可读原因,不再只显示「curl 退出码 28」
|
|
202
|
-
|
|
203
|
-
## [26.10.97] - 2026-10-01
|
|
204
|
-
|
|
205
|
-
### 修复
|
|
206
|
-
|
|
207
|
-
- **sudo 执行超时不再漏出内部错误串**:密码提示停留或脚本执行超过 60 秒时,此前用户看到的是 `spawnSync sudo ETIMEDOUT`,现改为超时提示并点明操作可能只完成了一部分
|
|
208
|
-
- **覆写 match 值加载期校验补齐**:`url-domain` 带协议、尾斜杠、端口、空格,或 `name` 精确值含非法字符时,文件会静默永不生效,现一律加载时报错;`rules!+`、`+~rules` 等操作符漏网形态同样拦下
|
|
209
|
-
- **帮助文案与实际行为对齐**:logs 用法行更正裸命令语义、version 用法不再内嵌别名、subscription use 补上自动重启说明;子命令名(如 `sub USE`)改为大小写不敏感
|
|
210
|
-
- **到期时间戳对非法值返回「未知」**:手工改坏缓存(非数字、负数、超出日期上限)时此前漏出字面 `Invalid Date` 或 1970 日期
|
package/dist/index.js
CHANGED
|
@@ -80,6 +80,9 @@ var CliError = class extends Error {
|
|
|
80
80
|
this.exitCode = options.exitCode ?? 1;
|
|
81
81
|
}
|
|
82
82
|
};
|
|
83
|
+
function relabelCliError(e, options) {
|
|
84
|
+
return new CliError(e.message, { label: options.label ?? e.label, hint: options.hint ?? e.hint, exitCode: e.exitCode });
|
|
85
|
+
}
|
|
83
86
|
function errorMessage(e) {
|
|
84
87
|
return e instanceof Error ? e.message : String(e);
|
|
85
88
|
}
|
|
@@ -199,7 +202,7 @@ var LOCKED_CONFIG_KEYS = [
|
|
|
199
202
|
// allow-lan 恒为 false 由 config.ts 的 systemConfig 写入(**不在 BASE_CONFIG**,
|
|
200
203
|
// 理由同 mixed-port:锁定项是「恒定此值」,不是「用户没写时的默认」)。剥除来源盲,
|
|
201
204
|
// 故覆写也不能再给 Mixed 端口设 authentication——缓解是 allow-lan 已强制 false、
|
|
202
|
-
// Mixed 只在回环,残余威胁面是同机其他进程(见
|
|
205
|
+
// Mixed 只在回环,残余威胁面是同机其他进程(见 CONCLUSIONS)
|
|
203
206
|
"allow-lan",
|
|
204
207
|
"bind-address",
|
|
205
208
|
"authentication",
|
|
@@ -291,7 +294,7 @@ function assertKnownFlags(args, known, command) {
|
|
|
291
294
|
const match = matchValueFlagToken(a);
|
|
292
295
|
if (match && match.form !== "exact" && knownSet.has(match.baseForm)) continue;
|
|
293
296
|
const isHelpFlag = a === "-h" || a === "--help";
|
|
294
|
-
const helpNote = isHelpFlag ? ["", `${a} \
|
|
297
|
+
const helpNote = isHelpFlag ? ["", `${a} \u8981\u7D27\u8DDF\u547D\u4EE4\u540D\u624D\u663E\u793A\u8BE5\u547D\u4EE4\u7684\u5E2E\u52A9\uFF08\u4F8B: mihomo-cli start ${a}\uFF09\uFF1B\u7EFC\u5408\u5E2E\u52A9: mihomo-cli help`] : [];
|
|
295
298
|
const hasValueFlag = known.some((f) => VALUE_FLAGS.has(f));
|
|
296
299
|
const looksLikeName = a.length > 2 && !a.startsWith("--") && !hasValueFlag;
|
|
297
300
|
const nameNote = looksLikeName ? ["", `\u82E5\u8FD9\u662F\u5728\u586B\u5199\u540D\u79F0\uFF1A\u540D\u79F0\u4E0D\u80FD\u4EE5 \`-\` \u5F00\u5934\uFF08argv \u4F1A\u628A\u5B83\u5F53\u9009\u9879\u62E6\u622A\uFF09\uFF0C\u8BF7\u53BB\u6389\u524D\u5BFC \`-\``] : [];
|
|
@@ -3526,7 +3529,7 @@ var PATHS = {
|
|
|
3526
3529
|
subscriptionCacheLock: path.join(USER_DATA_DIR, "subscription-cache.lock"),
|
|
3527
3530
|
serviceLock: path.join(USER_DATA_DIR, "service.lock"),
|
|
3528
3531
|
/**
|
|
3529
|
-
* 「服务被要求停止」的单调计数(`
|
|
3532
|
+
* 「服务被要求停止」的单调计数(`stop-epoch.ts` 的 `bumpStopEpoch`/`readStopEpoch`,
|
|
3530
3533
|
* 为什么用计数而非 disable 位见 docs/decisions.md D2)。
|
|
3531
3534
|
*
|
|
3532
3535
|
* 与锁同放 USER_DATA_DIR 根下:`runtime/` 等目录会被 `rmrf`,文件消失即读作 0,
|
|
@@ -3600,7 +3603,7 @@ function cleanupStaleTmpFiles() {
|
|
|
3600
3603
|
for (const entry of entries) {
|
|
3601
3604
|
const isAtomicTmpFile = entry.endsWith(".tmp");
|
|
3602
3605
|
const isKernelTmpDir = dir === DIRS.kernel && entry.startsWith(".tmp-");
|
|
3603
|
-
const isConfigCheckTmpDir = dir ===
|
|
3606
|
+
const isConfigCheckTmpDir = dir === USER_DATA_DIR && entry.startsWith("check-");
|
|
3604
3607
|
const isPlistStage = dir === USER_DATA_DIR && entry === "service.plist.stage";
|
|
3605
3608
|
if (!isAtomicTmpFile && !isKernelTmpDir && !isConfigCheckTmpDir && !isPlistStage) continue;
|
|
3606
3609
|
const full = path.join(dir, entry);
|
|
@@ -3760,13 +3763,18 @@ function getPorts(settings = readSettings()) {
|
|
|
3760
3763
|
}
|
|
3761
3764
|
return { mixed, controller };
|
|
3762
3765
|
}
|
|
3763
|
-
function getPortsOrNull() {
|
|
3766
|
+
function getPortsOrNull(settings = readSettings()) {
|
|
3764
3767
|
try {
|
|
3765
|
-
return getPorts();
|
|
3768
|
+
return getPorts(settings);
|
|
3766
3769
|
} catch {
|
|
3767
3770
|
return null;
|
|
3768
3771
|
}
|
|
3769
3772
|
}
|
|
3773
|
+
function getControllerEndpoint(settings = readSettings()) {
|
|
3774
|
+
const port = getPortsOrNull(settings)?.controller ?? null;
|
|
3775
|
+
const secret = settings.controller_secret;
|
|
3776
|
+
return { port, secret: typeof secret === "string" && secret ? secret : null };
|
|
3777
|
+
}
|
|
3770
3778
|
function readSubscriptionCache() {
|
|
3771
3779
|
ensureDirs();
|
|
3772
3780
|
const empty = () => /* @__PURE__ */ Object.create(null);
|
|
@@ -3835,10 +3843,11 @@ function getSubscriptions(settings = readSettings()) {
|
|
|
3835
3843
|
function getSubscriptionsWithCache() {
|
|
3836
3844
|
const subs = getSubscriptions();
|
|
3837
3845
|
const cache = readSubscriptionCache();
|
|
3838
|
-
return subs.map((s) =>
|
|
3839
|
-
|
|
3840
|
-
|
|
3841
|
-
|
|
3846
|
+
return subs.map((s) => {
|
|
3847
|
+
const entry = cache[s.name];
|
|
3848
|
+
const safe = entry !== null && typeof entry === "object" && !Array.isArray(entry) ? entry : {};
|
|
3849
|
+
return { ...s, ...safe };
|
|
3850
|
+
});
|
|
3842
3851
|
}
|
|
3843
3852
|
var SAFE_NAME_RE = /^[\w\-\p{Unified_Ideograph}]{1,64}$/u;
|
|
3844
3853
|
function validateSubscriptionName(name) {
|
|
@@ -3862,6 +3871,7 @@ function addSubscription(url, name = "default") {
|
|
|
3862
3871
|
function removeSubscription(name) {
|
|
3863
3872
|
let switchedTo = null;
|
|
3864
3873
|
let found = false;
|
|
3874
|
+
let wasActive = false;
|
|
3865
3875
|
updateSettings(
|
|
3866
3876
|
(settings) => {
|
|
3867
3877
|
const subs = getSubscriptions(settings);
|
|
@@ -3871,6 +3881,7 @@ function removeSubscription(name) {
|
|
|
3871
3881
|
subs.splice(idx, 1);
|
|
3872
3882
|
const updates = { subscriptions: subs };
|
|
3873
3883
|
if (settings.active_subscription === name) {
|
|
3884
|
+
wasActive = true;
|
|
3874
3885
|
switchedTo = subs.length > 0 ? subs[0].name : null;
|
|
3875
3886
|
updates.active_subscription = switchedTo ?? void 0;
|
|
3876
3887
|
}
|
|
@@ -3883,9 +3894,9 @@ function removeSubscription(name) {
|
|
|
3883
3894
|
}
|
|
3884
3895
|
}
|
|
3885
3896
|
);
|
|
3886
|
-
if (!found) return { found: false, switchedTo: null };
|
|
3897
|
+
if (!found) return { found: false, switchedTo: null, wasActive: false };
|
|
3887
3898
|
deleteSubscriptionCache(name);
|
|
3888
|
-
return { found: true, switchedTo };
|
|
3899
|
+
return { found: true, switchedTo, wasActive };
|
|
3889
3900
|
}
|
|
3890
3901
|
function setDefaultSubscription(name) {
|
|
3891
3902
|
let found = false;
|
|
@@ -3912,8 +3923,13 @@ function removeSubscriptionRawConfig(subName) {
|
|
|
3912
3923
|
}
|
|
3913
3924
|
function readSubscriptionRawConfig(subName) {
|
|
3914
3925
|
const filePath = getSubscriptionRawConfigPath(subName);
|
|
3915
|
-
|
|
3916
|
-
|
|
3926
|
+
try {
|
|
3927
|
+
return fs2.readFileSync(filePath, "utf8");
|
|
3928
|
+
} catch (e) {
|
|
3929
|
+
const code = e.code;
|
|
3930
|
+
if (code === "ENOENT" || code === "EISDIR") return null;
|
|
3931
|
+
throw e;
|
|
3932
|
+
}
|
|
3917
3933
|
}
|
|
3918
3934
|
function requireSubscriptionRawConfig(subName) {
|
|
3919
3935
|
const content = readSubscriptionRawConfig(subName);
|
|
@@ -3923,6 +3939,66 @@ function requireSubscriptionRawConfig(subName) {
|
|
|
3923
3939
|
return content;
|
|
3924
3940
|
}
|
|
3925
3941
|
|
|
3942
|
+
// src/text.ts
|
|
3943
|
+
function escapeRegExp(s) {
|
|
3944
|
+
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
3945
|
+
}
|
|
3946
|
+
function shellQuote(s) {
|
|
3947
|
+
return `'${s.replace(/'/g, "'\\''")}'`;
|
|
3948
|
+
}
|
|
3949
|
+
function firstLine(text) {
|
|
3950
|
+
return text.split("\n")[0];
|
|
3951
|
+
}
|
|
3952
|
+
function sanitizeTerminal(s) {
|
|
3953
|
+
const ansiEscape = /\x1b\[[0-9;]*[a-zA-Z]/g;
|
|
3954
|
+
const controlChars = /[\x00-\x08\x0b\x0c\x0d\x0e-\x1f]/g;
|
|
3955
|
+
return s.replace(ansiEscape, "").replace(controlChars, "");
|
|
3956
|
+
}
|
|
3957
|
+
function lastCurlErrorLine(stderr) {
|
|
3958
|
+
const text = typeof stderr === "string" || Buffer.isBuffer(stderr) ? stderr.toString().trim() : "";
|
|
3959
|
+
const lastLine = text ? text.split("\n").pop() : void 0;
|
|
3960
|
+
return lastLine ? lastLine.replace(/^curl: \(\d+\)\s*/, "") : void 0;
|
|
3961
|
+
}
|
|
3962
|
+
var TOKEN_KEY_NAMES = [
|
|
3963
|
+
"token",
|
|
3964
|
+
"key",
|
|
3965
|
+
"secret",
|
|
3966
|
+
"pass",
|
|
3967
|
+
"password",
|
|
3968
|
+
"auth",
|
|
3969
|
+
"access_token",
|
|
3970
|
+
"api_key",
|
|
3971
|
+
"uuid",
|
|
3972
|
+
"sid",
|
|
3973
|
+
"id",
|
|
3974
|
+
"sub",
|
|
3975
|
+
"user",
|
|
3976
|
+
"email",
|
|
3977
|
+
"passwd",
|
|
3978
|
+
"apikey",
|
|
3979
|
+
"access"
|
|
3980
|
+
];
|
|
3981
|
+
var TOKEN_KEYS = new Set(TOKEN_KEY_NAMES);
|
|
3982
|
+
function maskUrl(url) {
|
|
3983
|
+
try {
|
|
3984
|
+
const parsed = new URL(url);
|
|
3985
|
+
for (const [key, value] of parsed.searchParams) {
|
|
3986
|
+
if (TOKEN_KEYS.has(key.toLowerCase()) || value.length >= 16) {
|
|
3987
|
+
parsed.searchParams.set(key, "***");
|
|
3988
|
+
}
|
|
3989
|
+
}
|
|
3990
|
+
if (parsed.username) parsed.username = "***";
|
|
3991
|
+
if (parsed.password) parsed.password = "***";
|
|
3992
|
+
parsed.pathname = parsed.pathname.split("/").map((seg) => seg.length >= 16 ? `${seg.slice(0, 4)}***${seg.slice(-4)}` : seg).join("/");
|
|
3993
|
+
return parsed.toString();
|
|
3994
|
+
} catch {
|
|
3995
|
+
if (url.length > 15) {
|
|
3996
|
+
return `${url.slice(0, 15)}...`;
|
|
3997
|
+
}
|
|
3998
|
+
return url;
|
|
3999
|
+
}
|
|
4000
|
+
}
|
|
4001
|
+
|
|
3926
4002
|
// src/overwrite.ts
|
|
3927
4003
|
function parseOverrideKey(key) {
|
|
3928
4004
|
let actualKey = key;
|
|
@@ -3980,6 +4056,12 @@ function assertValidParsedKey(rawKey, parsed) {
|
|
|
3980
4056
|
hint: ["\u6574\u4F53\u8986\u76D6\u7684 ! \u53EA\u80FD\u5199\u5728\u952E\u540D\u672B\u5C3E\u3001\u4E14\u4E0D\u80FD\u4E0E\u5176\u4ED6\u64CD\u4F5C\u7B26\u540C\u7528\uFF08key!\uFF09\u3002", "\u60F3\u5411\u6570\u7EC4\u8FFD\u52A0\u8BF7\u53EA\u7528 key+\u3002"]
|
|
3981
4057
|
});
|
|
3982
4058
|
}
|
|
4059
|
+
if (parsed.key.includes("+")) {
|
|
4060
|
+
throw new CliError(`\u8986\u5199\u952E "${rawKey}" \u7684\u64CD\u4F5C\u7B26\u4F4D\u7F6E\u77DB\u76FE`, {
|
|
4061
|
+
label: "\u8986\u5199\u914D\u7F6E\u9519\u8BEF",
|
|
4062
|
+
hint: ["\u6570\u7EC4\u524D\u63D2/\u8FFD\u52A0\u5404\u53EA\u6709\u4E00\u4E2A +\uFF08+key \u6216 key+\uFF09\uFF0C\u952E\u540D\u91CC\u4E0D\u80FD\u542B +\u3002"]
|
|
4063
|
+
});
|
|
4064
|
+
}
|
|
3983
4065
|
const ops = [];
|
|
3984
4066
|
if (parsed.forceOverwrite) ops.push("!\uFF08\u6574\u4F53\u8986\u76D6\uFF09");
|
|
3985
4067
|
if (parsed.arrayPrepend) ops.push("+\u524D\u7F00\uFF08\u6570\u7EC4\u524D\u63D2\uFF09");
|
|
@@ -4319,7 +4401,7 @@ function loadOverwriteScript(filePath, fileName) {
|
|
|
4319
4401
|
try {
|
|
4320
4402
|
mod = createRequire2(filePath)(filePath);
|
|
4321
4403
|
} catch (e) {
|
|
4322
|
-
throw new CliError(`\u8986\u5199\u811A\u672C "${fileName}" \u52A0\u8F7D\u5931\u8D25: ${e.message
|
|
4404
|
+
throw new CliError(`\u8986\u5199\u811A\u672C "${fileName}" \u52A0\u8F7D\u5931\u8D25: ${firstLine(e.message ?? String(e))}`, {
|
|
4323
4405
|
label: "\u8986\u5199\u914D\u7F6E\u9519\u8BEF",
|
|
4324
4406
|
hint: ["\u8BE5\u811A\u672C\u5F53\u524D\u672A\u53C2\u4E0E\u5408\u5E76\u3002\u5E38\u89C1\u539F\u56E0\uFF1A\u8BED\u6CD5\u9519\u8BEF\u3001import/require \u4E86\u4E0D\u5B58\u5728\u7684\u6A21\u5757\u3001\u6216\u4F7F\u7528\u4E86 top-level await\uFF08\u811A\u672C\u5FC5\u987B\u53EF\u540C\u6B65\u52A0\u8F7D\uFF09\u3002"]
|
|
4325
4407
|
});
|
|
@@ -4342,7 +4424,7 @@ function readOverwriteFiles() {
|
|
|
4342
4424
|
const broken = [];
|
|
4343
4425
|
if (!fs3.existsSync(USER_DATA_DIR)) return { ok, broken };
|
|
4344
4426
|
const entries = fs3.readdirSync(USER_DATA_DIR);
|
|
4345
|
-
const files = entries.filter(isOverwriteFilename).sort((a, b) =>
|
|
4427
|
+
const files = entries.filter(isOverwriteFilename).map((name) => ({ name, key: overwriteSortKey(name) })).sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0).map((e) => e.name);
|
|
4346
4428
|
for (const typo of entries.filter(isOverwriteFilenameTypo)) {
|
|
4347
4429
|
console.warn(
|
|
4348
4430
|
`\u8B66\u544A: "${typo}" \u4E0D\u4F1A\u88AB\u5F53\u4F5C\u8986\u5199\u6587\u4EF6\u52A0\u8F7D\uFF08\u5408\u6CD5\u6587\u4EF6\u540D: overwrite.yaml \u4E3B\u6587\u4EF6\u3001overwrite.*.yaml / overwrite.*.yml \u6269\u5C55\u6587\u4EF6\u3001overwrite.js \u4E3B\u811A\u672C\u3001overwrite.*.js / .mjs / .cjs \u6269\u5C55\u811A\u672C\uFF09\uFF1B\u82E5\u662F\u7B14\u8BEF\u8BF7\u6539\u540D`
|
|
@@ -4425,7 +4507,7 @@ function applyOverwrite(baseConfig, files, opts) {
|
|
|
4425
4507
|
try {
|
|
4426
4508
|
returned = file.transform(result, ctx);
|
|
4427
4509
|
} catch (e) {
|
|
4428
|
-
throw new CliError(`\u8986\u5199\u811A\u672C "${file.name}" \u6267\u884C\u5931\u8D25: ${e.message
|
|
4510
|
+
throw new CliError(`\u8986\u5199\u811A\u672C "${file.name}" \u6267\u884C\u5931\u8D25: ${firstLine(e.message ?? String(e))}`, {
|
|
4429
4511
|
label: "\u8986\u5199\u914D\u7F6E\u9519\u8BEF",
|
|
4430
4512
|
hint: ["\u672C\u6B21\u6784\u5EFA\u5DF2\u4E2D\u6B62\uFF1B\u4FEE\u590D\u811A\u672C\u540E\u91CD\u8BD5\uFF08mihomo-cli ow \u53EF\u67E5\u770B\u5168\u90E8\u8986\u5199\u6587\u4EF6\u4E0E\u811A\u672C\uFF09\u3002"]
|
|
4431
4513
|
});
|
|
@@ -4465,63 +4547,6 @@ function listOverwriteFile(scope) {
|
|
|
4465
4547
|
};
|
|
4466
4548
|
}
|
|
4467
4549
|
|
|
4468
|
-
// src/text.ts
|
|
4469
|
-
function escapeRegExp(s) {
|
|
4470
|
-
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
4471
|
-
}
|
|
4472
|
-
function shellQuote(s) {
|
|
4473
|
-
return `'${s.replace(/'/g, "'\\''")}'`;
|
|
4474
|
-
}
|
|
4475
|
-
function sanitizeTerminal(s) {
|
|
4476
|
-
const ansiEscape = /\x1b\[[0-9;]*[a-zA-Z]/g;
|
|
4477
|
-
const controlChars = /[\x00-\x08\x0b\x0c\x0d\x0e-\x1f]/g;
|
|
4478
|
-
return s.replace(ansiEscape, "").replace(controlChars, "");
|
|
4479
|
-
}
|
|
4480
|
-
function lastCurlErrorLine(stderr) {
|
|
4481
|
-
const text = typeof stderr === "string" || Buffer.isBuffer(stderr) ? stderr.toString().trim() : "";
|
|
4482
|
-
const lastLine = text ? text.split("\n").pop() : void 0;
|
|
4483
|
-
return lastLine ? lastLine.replace(/^curl: \(\d+\)\s*/, "") : void 0;
|
|
4484
|
-
}
|
|
4485
|
-
var TOKEN_KEY_NAMES = [
|
|
4486
|
-
"token",
|
|
4487
|
-
"key",
|
|
4488
|
-
"secret",
|
|
4489
|
-
"pass",
|
|
4490
|
-
"password",
|
|
4491
|
-
"auth",
|
|
4492
|
-
"access_token",
|
|
4493
|
-
"api_key",
|
|
4494
|
-
"uuid",
|
|
4495
|
-
"sid",
|
|
4496
|
-
"id",
|
|
4497
|
-
"sub",
|
|
4498
|
-
"user",
|
|
4499
|
-
"email",
|
|
4500
|
-
"passwd",
|
|
4501
|
-
"apikey",
|
|
4502
|
-
"access"
|
|
4503
|
-
];
|
|
4504
|
-
var TOKEN_KEYS = new Set(TOKEN_KEY_NAMES);
|
|
4505
|
-
function maskUrl(url) {
|
|
4506
|
-
try {
|
|
4507
|
-
const parsed = new URL(url);
|
|
4508
|
-
for (const [key, value] of parsed.searchParams) {
|
|
4509
|
-
if (TOKEN_KEYS.has(key.toLowerCase()) || value.length >= 16) {
|
|
4510
|
-
parsed.searchParams.set(key, "***");
|
|
4511
|
-
}
|
|
4512
|
-
}
|
|
4513
|
-
if (parsed.username) parsed.username = "***";
|
|
4514
|
-
if (parsed.password) parsed.password = "***";
|
|
4515
|
-
parsed.pathname = parsed.pathname.split("/").map((seg) => seg.length >= 16 ? `${seg.slice(0, 4)}***${seg.slice(-4)}` : seg).join("/");
|
|
4516
|
-
return parsed.toString();
|
|
4517
|
-
} catch {
|
|
4518
|
-
if (url.length > 15) {
|
|
4519
|
-
return `${url.slice(0, 15)}...`;
|
|
4520
|
-
}
|
|
4521
|
-
return url;
|
|
4522
|
-
}
|
|
4523
|
-
}
|
|
4524
|
-
|
|
4525
4550
|
// src/config.ts
|
|
4526
4551
|
var SAFE_YAML_LOAD_OPTIONS = { maxAliases: YAML_MAX_ALIASES };
|
|
4527
4552
|
function loadYamlSafe(content) {
|
|
@@ -4536,7 +4561,7 @@ function parseConfigContent(content, errorMsg) {
|
|
|
4536
4561
|
try {
|
|
4537
4562
|
result = loadYamlSafe(content);
|
|
4538
4563
|
} catch (e) {
|
|
4539
|
-
throw new CliError(`${label}\u683C\u5F0F\u9519\u8BEF\uFF0C\u65E0\u6CD5\u89E3\u6790: ${e.message
|
|
4564
|
+
throw new CliError(`${label}\u683C\u5F0F\u9519\u8BEF\uFF0C\u65E0\u6CD5\u89E3\u6790: ${firstLine(e.message)}`, { label: "\u914D\u7F6E\u9519\u8BEF" });
|
|
4540
4565
|
}
|
|
4541
4566
|
if (result == null || typeof result !== "object" || Array.isArray(result)) {
|
|
4542
4567
|
throw new CliError(`${label}\u4E0D\u662F\u6709\u6548\u7684\u914D\u7F6E\u5BF9\u8C61\uFF08\u9876\u5C42\u9700\u4E3A\u6620\u5C04\uFF0C\u5F53\u524D\u662F${Array.isArray(result) ? "\u5217\u8868" : typeof result}\uFF09`, { label: "\u914D\u7F6E\u9519\u8BEF" });
|
|
@@ -4704,7 +4729,7 @@ function judgeScriptMatches(subRawContent, mode, scope, entries) {
|
|
|
4704
4729
|
});
|
|
4705
4730
|
return { matches: scriptMatches };
|
|
4706
4731
|
} catch (e) {
|
|
4707
|
-
return { matches: [], error: e.message
|
|
4732
|
+
return { matches: [], error: firstLine(e.message ?? String(e)) };
|
|
4708
4733
|
}
|
|
4709
4734
|
}
|
|
4710
4735
|
function deriveRuntimeMode(info) {
|
|
@@ -4749,7 +4774,7 @@ function buildKernelRejectHint(detail, overwriteSummaries, opts = {}) {
|
|
|
4749
4774
|
async function validateConfigWithKernel(config, overwriteSummaries) {
|
|
4750
4775
|
assertKernelInstalled();
|
|
4751
4776
|
ensureDirs();
|
|
4752
|
-
const stageDir = fs4.mkdtempSync(path4.join(
|
|
4777
|
+
const stageDir = fs4.mkdtempSync(path4.join(USER_DATA_DIR, "check-"));
|
|
4753
4778
|
const stageFile = path4.join(stageDir, "config.yaml");
|
|
4754
4779
|
try {
|
|
4755
4780
|
fs4.writeFileSync(stageFile, dumpYaml(config), { mode: 384 });
|
|
@@ -4817,7 +4842,7 @@ function getKernelVersion(probe) {
|
|
|
4817
4842
|
try {
|
|
4818
4843
|
const p = probe ?? probeKernelVersion();
|
|
4819
4844
|
if (!p.output) return "unknown";
|
|
4820
|
-
return p.version ?? p.output
|
|
4845
|
+
return p.version ?? firstLine(p.output);
|
|
4821
4846
|
} catch {
|
|
4822
4847
|
return "unknown";
|
|
4823
4848
|
}
|
|
@@ -4964,6 +4989,10 @@ function parseDisabledList(output, label) {
|
|
|
4964
4989
|
return value === "true" || value === "disabled";
|
|
4965
4990
|
}
|
|
4966
4991
|
var LAUNCHCTL_NOT_LOADED = 113;
|
|
4992
|
+
var BOOTOUT_NOT_LOADED_CODES = [3, LAUNCHCTL_NOT_LOADED];
|
|
4993
|
+
function isBootoutNotLoaded(status) {
|
|
4994
|
+
return status !== null && BOOTOUT_NOT_LOADED_CODES.includes(status);
|
|
4995
|
+
}
|
|
4967
4996
|
function runLaunchctl(args, timeoutMs = LAUNCHCTL_TIMEOUT_MS) {
|
|
4968
4997
|
try {
|
|
4969
4998
|
const result = spawnSync2("launchctl", args, { encoding: "utf8", timeout: timeoutMs });
|
|
@@ -5058,6 +5087,14 @@ async function waitUntilUnloaded(target = serviceTarget()) {
|
|
|
5058
5087
|
import { execFile as execFile2, spawnSync as spawnSync4 } from "child_process";
|
|
5059
5088
|
import { promisify as promisify2 } from "util";
|
|
5060
5089
|
|
|
5090
|
+
// src/curl-spawn.ts
|
|
5091
|
+
function buildCurlSpawnEnv() {
|
|
5092
|
+
const env = { ...process.env };
|
|
5093
|
+
env.no_proxy = "";
|
|
5094
|
+
env.NO_PROXY = "";
|
|
5095
|
+
return env;
|
|
5096
|
+
}
|
|
5097
|
+
|
|
5061
5098
|
// src/process-probe.ts
|
|
5062
5099
|
import { spawnSync as spawnSync3 } from "child_process";
|
|
5063
5100
|
import fs6 from "fs";
|
|
@@ -5172,7 +5209,11 @@ async function probeProxyConnectivity(port) {
|
|
|
5172
5209
|
const { stdout } = await execFileAsync(
|
|
5173
5210
|
"curl",
|
|
5174
5211
|
["-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],
|
|
5175
|
-
{
|
|
5212
|
+
{
|
|
5213
|
+
timeout: PROBE_TIMEOUT_MS + 2e3,
|
|
5214
|
+
// 例外表会绕过显式 -x、让探测考成直连(绿灯作废),置空兜住统一走 curl-spawn.ts 出口
|
|
5215
|
+
env: buildCurlSpawnEnv()
|
|
5216
|
+
}
|
|
5176
5217
|
);
|
|
5177
5218
|
const code = Number.parseInt(stdout.trim(), 10);
|
|
5178
5219
|
const statusCode = Number.isFinite(code) ? code : null;
|
|
@@ -5240,18 +5281,18 @@ async function tryHotReload(prefetched) {
|
|
|
5240
5281
|
try {
|
|
5241
5282
|
const status = prefetched ?? getServiceStatus({ withDisabled: false });
|
|
5242
5283
|
if (!status.running || status.pid === null) return false;
|
|
5243
|
-
const
|
|
5244
|
-
|
|
5245
|
-
const
|
|
5284
|
+
const endpoint = getControllerEndpoint();
|
|
5285
|
+
if (endpoint.port === null) return false;
|
|
5286
|
+
const baseUrl = `http://127.0.0.1:${endpoint.port}`;
|
|
5246
5287
|
const headers = { "Content-Type": "application/json" };
|
|
5247
|
-
if (
|
|
5288
|
+
if (endpoint.secret) headers.Authorization = `Bearer ${endpoint.secret}`;
|
|
5248
5289
|
const timer = setTimeout(() => controller.abort(), HOT_RELOAD_TIMEOUT_MS);
|
|
5249
5290
|
try {
|
|
5250
5291
|
const probe = await fetch(`${baseUrl}/version`, { headers, signal: controller.signal });
|
|
5251
5292
|
if (!probe.ok) return false;
|
|
5252
5293
|
const info = await probe.json();
|
|
5253
5294
|
if (typeof info?.version !== "string") return false;
|
|
5254
|
-
const listenerPids = lsofListenPids(
|
|
5295
|
+
const listenerPids = lsofListenPids(endpoint.port);
|
|
5255
5296
|
if (listenerPids === null || listenerPids[0] !== status.pid) return false;
|
|
5256
5297
|
const res = await fetch(`${baseUrl}/configs?force=true`, {
|
|
5257
5298
|
method: "PUT",
|
|
@@ -5306,7 +5347,7 @@ function runSudoScript(scriptBody, opts) {
|
|
|
5306
5347
|
throw new Error("\u5F53\u524D\u73AF\u5883\u65E0\u6CD5\u8F93\u5165\u7BA1\u7406\u5458\u5BC6\u7801\uFF08\u9700\u8981\u5728\u4EA4\u4E92\u5F0F\u7EC8\u7AEF\u8FD0\u884C sudo\uFF09");
|
|
5307
5348
|
}
|
|
5308
5349
|
ensureDirs();
|
|
5309
|
-
const scriptPath = path5.join(USER_DATA_DIR, opts.file);
|
|
5350
|
+
const scriptPath = path5.join(USER_DATA_DIR, `${opts.file}.${process.pid}.tmp`);
|
|
5310
5351
|
fs9.writeFileSync(scriptPath, scriptBody, { mode: 448 });
|
|
5311
5352
|
fs9.chmodSync(scriptPath, 448);
|
|
5312
5353
|
try {
|
|
@@ -5338,6 +5379,10 @@ function describePidCleanupFailure(err) {
|
|
|
5338
5379
|
return `pid \u6587\u4EF6\u672A\u80FD\u6E05\u7406\uFF08${err.message}\uFF09`;
|
|
5339
5380
|
}
|
|
5340
5381
|
var MANUAL_PKILL_HINT = "\u624B\u52A8\u6E05\u7406: sudo pkill -9 mihomo";
|
|
5382
|
+
function printResidueWarning(remaining) {
|
|
5383
|
+
console.log(colors.yellow(`\u4ECD\u6709\u5185\u6838\u8FDB\u7A0B\u6B8B\u7559 (PID ${remaining.join(", ")})`));
|
|
5384
|
+
console.log(MANUAL_PKILL_HINT);
|
|
5385
|
+
}
|
|
5341
5386
|
function warnPidCleanupFailed(err) {
|
|
5342
5387
|
if (!err) return;
|
|
5343
5388
|
console.warn(colors.yellow(`\u8B66\u544A: ${describePidCleanupFailure(err)}\uFF0C\u4E0B\u6B21 stop \u4F1A\u518D\u6B21\u5C1D\u8BD5`));
|
|
@@ -5507,13 +5552,14 @@ async function cleanupKernelsOrThrow(ctx) {
|
|
|
5507
5552
|
return result;
|
|
5508
5553
|
}
|
|
5509
5554
|
function buildLegacyCleanupScript() {
|
|
5555
|
+
const toleratedBootoutCodes = [0, ...BOOTOUT_NOT_LOADED_CODES].map((code) => `[ $bootout_code -ne ${code} ]`).join(" && ");
|
|
5510
5556
|
return [
|
|
5511
5557
|
"#!/bin/bash",
|
|
5512
|
-
// bootout 退出码分级:113=未装载(daemon
|
|
5513
|
-
//
|
|
5558
|
+
// bootout 退出码分级:0=成功,3/113=未装载(daemon 已不在,正常);其余是真实
|
|
5559
|
+
// 失败,不能 || true 吞掉后照样 rm plist 报「已清理」
|
|
5514
5560
|
`bootout_code=0`,
|
|
5515
5561
|
`launchctl bootout ${shellQuote(`system/${SERVICE_LABEL}`)} 2>/dev/null || bootout_code=$?`,
|
|
5516
|
-
`if
|
|
5562
|
+
`if ${toleratedBootoutCodes}; then`,
|
|
5517
5563
|
` echo "launchctl bootout \u5931\u8D25\uFF08\u9000\u51FA\u7801 $bootout_code\uFF09" >&2`,
|
|
5518
5564
|
` exit 3`,
|
|
5519
5565
|
`fi`,
|
|
@@ -5822,7 +5868,7 @@ function bootstrapServiceIdempotentOrThrow(what) {
|
|
|
5822
5868
|
}
|
|
5823
5869
|
function bootoutService(timeoutMs = LAUNCHCTL_TIMEOUT_MS) {
|
|
5824
5870
|
const result = runLaunchctl(["bootout", serviceTarget()], timeoutMs);
|
|
5825
|
-
if (result.status === 0 || result.status
|
|
5871
|
+
if (result.status === 0 || isBootoutNotLoaded(result.status)) return;
|
|
5826
5872
|
const detail = result.stderr.trim();
|
|
5827
5873
|
throw new CliError(`\u5378\u8F7D\u65E7\u670D\u52A1\u5B9E\u4F8B\u5931\u8D25\uFF08launchctl bootout \u9000\u51FA\u7801 ${result.status ?? "\u6267\u884C\u5931\u8D25"}\uFF09`, {
|
|
5828
5874
|
hint: [detail, `\u624B\u52A8\u786E\u8BA4: launchctl print ${serviceTarget()}`].filter(Boolean)
|
|
@@ -6163,6 +6209,8 @@ function redactConfigSecrets(config) {
|
|
|
6163
6209
|
|
|
6164
6210
|
// src/http.ts
|
|
6165
6211
|
var MAX_RESPONSE_BYTES = 50 * 1024 * 1024;
|
|
6212
|
+
var MAX_REDIRECTS = 20;
|
|
6213
|
+
var REDIRECT_STATUSES = /* @__PURE__ */ new Set([301, 302, 303, 307, 308]);
|
|
6166
6214
|
var MAX_ERROR_BODY_BYTES = 64 * 1024;
|
|
6167
6215
|
function isHttpsUrl(url) {
|
|
6168
6216
|
try {
|
|
@@ -6188,12 +6236,26 @@ function createHttpClient(options = {}) {
|
|
|
6188
6236
|
const timer = setTimeout(() => controller.abort(), timeout);
|
|
6189
6237
|
const signal = config?.signal ? AbortSignal.any([controller.signal, config.signal]) : controller.signal;
|
|
6190
6238
|
try {
|
|
6191
|
-
|
|
6192
|
-
|
|
6193
|
-
|
|
6194
|
-
|
|
6195
|
-
|
|
6196
|
-
|
|
6239
|
+
let currentUrl = url;
|
|
6240
|
+
let response;
|
|
6241
|
+
for (let hop = 0; ; hop++) {
|
|
6242
|
+
response = await fetch(currentUrl, {
|
|
6243
|
+
signal,
|
|
6244
|
+
redirect: "manual",
|
|
6245
|
+
headers: { "User-Agent": `mihomo-cli/${VERSION}` }
|
|
6246
|
+
});
|
|
6247
|
+
if (!REDIRECT_STATUSES.has(response.status)) break;
|
|
6248
|
+
if (hop >= MAX_REDIRECTS) {
|
|
6249
|
+
throw new Error(`\u91CD\u5B9A\u5411\u6B21\u6570\u8D85\u8FC7 ${MAX_REDIRECTS}\uFF0C\u5DF2\u4E2D\u6B62: ${maskUrl(url)}`);
|
|
6250
|
+
}
|
|
6251
|
+
const location = response.headers.get("location");
|
|
6252
|
+
if (!location) break;
|
|
6253
|
+
const nextUrl = new URL(location, currentUrl).toString();
|
|
6254
|
+
if (isHttpsUrl(url) && !isHttpsUrl(nextUrl)) {
|
|
6255
|
+
throw new Error(`\u8BF7\u6C42\u88AB\u91CD\u5B9A\u5411\u5230\u975E https \u5730\u5740\uFF08${maskUrl(nextUrl)}\uFF09\uFF0C\u5DF2\u62D2\u7EDD`);
|
|
6256
|
+
}
|
|
6257
|
+
await response.body?.cancel().catch(() => void 0);
|
|
6258
|
+
currentUrl = nextUrl;
|
|
6197
6259
|
}
|
|
6198
6260
|
if (!response.ok) {
|
|
6199
6261
|
let text2 = "";
|
|
@@ -6457,7 +6519,17 @@ async function downloadSubscription(url, subName = "default", signal) {
|
|
|
6457
6519
|
return { proxies, proxyGroups };
|
|
6458
6520
|
}
|
|
6459
6521
|
async function prepareConfigForStart(mode, subName = "default") {
|
|
6460
|
-
|
|
6522
|
+
let rawContent;
|
|
6523
|
+
try {
|
|
6524
|
+
rawContent = requireSubscriptionRawConfig(subName);
|
|
6525
|
+
} catch (e) {
|
|
6526
|
+
if (e instanceof CliError && !getSubscriptions().some((s) => s.name === subName)) {
|
|
6527
|
+
throw new CliError(`\u8BA2\u9605 "${subName}" \u5DF2\u5728\u542F\u52A8\u8FC7\u7A0B\u4E2D\u88AB\u79FB\u9664\uFF08\u53EF\u80FD\u53E6\u4E00\u7EC8\u7AEF\u6267\u884C\u4E86 sub remove/reset\uFF09`, {
|
|
6528
|
+
hint: ["\u67E5\u770B\u5F53\u524D\u5217\u8868: mihomo-cli sub", "\u786E\u8BA4\u540E\u91CD\u8BD5: mihomo-cli start"]
|
|
6529
|
+
});
|
|
6530
|
+
}
|
|
6531
|
+
throw e;
|
|
6532
|
+
}
|
|
6461
6533
|
const subUrl = getSubscriptions().find((s) => s.name === subName)?.url;
|
|
6462
6534
|
const buildResult = buildConfig(rawContent, mode, { subName, subUrl });
|
|
6463
6535
|
await validateConfigWithKernel(buildResult.config, buildResult.overwriteSummaries);
|
|
@@ -6500,7 +6572,7 @@ function printUpdateResult(r) {
|
|
|
6500
6572
|
} else if (r.aborted) {
|
|
6501
6573
|
console.log(`${colors.gray("\xB7")} ${r.name}: ${colors.gray("\u8DF3\u8FC7\uFF08\u66F4\u65B0\u8D85\u65F6\uFF0C\u4F7F\u7528\u672C\u5730\u7F13\u5B58\uFF09")}`);
|
|
6502
6574
|
} else {
|
|
6503
|
-
console.log(`${colors.red("\u2717")} ${r.name}: ${colors.red("\u5931\u8D25")} (${(r.error || "")
|
|
6575
|
+
console.log(`${colors.red("\u2717")} ${r.name}: ${colors.red("\u5931\u8D25")} (${firstLine(r.error || "")})`);
|
|
6504
6576
|
}
|
|
6505
6577
|
}
|
|
6506
6578
|
async function autoUpdateStaleSubscription(options = {}) {
|
|
@@ -7016,13 +7088,17 @@ async function printStatus(args = []) {
|
|
|
7016
7088
|
broken: overwriteBroken
|
|
7017
7089
|
} = listOverwriteFile(activeSub ? { subName: activeSub.name, subUrl: activeSub.url } : void 0);
|
|
7018
7090
|
if (activeSub && overwriteEnabled && overwriteFiles.some((f) => f.kind === "script")) {
|
|
7019
|
-
|
|
7020
|
-
|
|
7021
|
-
|
|
7022
|
-
|
|
7023
|
-
|
|
7024
|
-
|
|
7091
|
+
try {
|
|
7092
|
+
const rawContent = readSubscriptionRawConfig(activeSub.name);
|
|
7093
|
+
if (rawContent) {
|
|
7094
|
+
const { matches } = judgeScriptMatches(rawContent, deriveRuntimeMode(info), { subName: activeSub.name, subUrl: activeSub.url }, overwriteEntries);
|
|
7095
|
+
const matchedByName = new Map(matches.map((m) => [m.file, m.matched]));
|
|
7096
|
+
for (const f of overwriteFiles) {
|
|
7097
|
+
if (f.kind === "script" && matchedByName.has(f.name)) f.matched = matchedByName.get(f.name);
|
|
7098
|
+
}
|
|
7025
7099
|
}
|
|
7100
|
+
} catch (e) {
|
|
7101
|
+
if (!(e instanceof CliError)) throw e;
|
|
7026
7102
|
}
|
|
7027
7103
|
}
|
|
7028
7104
|
const cached = activeSub ? getSubscriptionsWithCache().find((s) => s.name === activeSub.name) : void 0;
|
|
@@ -7224,40 +7300,41 @@ async function cmdStart(args) {
|
|
|
7224
7300
|
hint: ["Mixed \u6A21\u5F0F\u7531 launchd \u670D\u52A1\u6258\u7BA1\uFF0C\u9700\u5148\u5B89\u88C5:", " mihomo-cli install", "", "\u4E34\u65F6\u4F7F\u7528\u53EF\u8D70 TUN: mihomo-cli tun"]
|
|
7225
7301
|
});
|
|
7226
7302
|
}
|
|
7227
|
-
const
|
|
7228
|
-
|
|
7229
|
-
await autoUpdateStaleSubscription({ timeout: updateTimeout });
|
|
7230
|
-
}
|
|
7231
|
-
let prepared;
|
|
7303
|
+
const tunHint = disabledAutoStartForTun ? ["", "\u670D\u52A1\u81EA\u542F\u5DF2\u88AB\u5173\u95ED\uFF08\u542F\u52A8 TUN \u524D\u5173\u95ED\u4EE5\u907F\u514D\u81EA\u542F\u5931\u8D25\u5FAA\u73AF\uFF09\u3002", "\u6062\u590D Mixed \u6A21\u5F0F: mihomo-cli start"] : [];
|
|
7304
|
+
const appendTunHint = (e) => tunHint.length > 0 ? relabelCliError(e, { hint: [...e.hint, ...tunHint] }) : e;
|
|
7232
7305
|
try {
|
|
7233
|
-
|
|
7306
|
+
const sub = requireActiveSubscription();
|
|
7307
|
+
if (!skipUpdate) {
|
|
7308
|
+
await autoUpdateStaleSubscription({ timeout: updateTimeout });
|
|
7309
|
+
}
|
|
7310
|
+
let prepared;
|
|
7311
|
+
try {
|
|
7312
|
+
prepared = await prepareConfigForStart(targetMode, sub.name);
|
|
7313
|
+
} catch (e) {
|
|
7314
|
+
if (e instanceof CliError) throw e;
|
|
7315
|
+
throw new CliError(e.message, { label: "\u914D\u7F6E\u9519\u8BEF" });
|
|
7316
|
+
}
|
|
7317
|
+
const configInfo = commitPreparedConfig(prepared);
|
|
7318
|
+
console.log([colors.cyan(runtimeModeLabel(targetMode)), sub.name, formatProxySummary(configInfo)].join(" \xB7 "));
|
|
7234
7319
|
} catch (e) {
|
|
7235
|
-
if (e instanceof CliError) throw e;
|
|
7236
|
-
throw
|
|
7320
|
+
if (e instanceof CliError) throw appendTunHint(e);
|
|
7321
|
+
throw e;
|
|
7237
7322
|
}
|
|
7238
|
-
const configInfo = commitPreparedConfig(prepared);
|
|
7239
|
-
console.log([colors.cyan(runtimeModeLabel(targetMode)), sub.name, formatProxySummary(configInfo)].join(" \xB7 "));
|
|
7240
7323
|
try {
|
|
7241
7324
|
const pid = await launchOrRestart(targetMode);
|
|
7242
7325
|
console.log(`${colors.green("\u5DF2\u542F\u52A8")}${pid ? ` (PID ${pid})` : ""}`);
|
|
7243
7326
|
} catch (e) {
|
|
7327
|
+
if (e instanceof CliError) throw appendTunHint(e);
|
|
7244
7328
|
const lines = e.message.split("\n");
|
|
7245
|
-
|
|
7246
|
-
if (targetMode === "tun" && disabledAutoStartForTun) {
|
|
7247
|
-
extraHint.push("", "\u670D\u52A1\u81EA\u542F\u5DF2\u88AB\u5173\u95ED\uFF08\u542F\u52A8 TUN \u524D\u5173\u95ED\u4EE5\u907F\u514D\u81EA\u542F\u5931\u8D25\u5FAA\u73AF\uFF09\u3002", "\u6062\u590D Mixed \u6A21\u5F0F: mihomo-cli start");
|
|
7248
|
-
}
|
|
7249
|
-
if (e instanceof CliError) {
|
|
7250
|
-
throw new CliError(e.message, { label: e.label, hint: [...e.hint, ...extraHint] });
|
|
7251
|
-
}
|
|
7252
|
-
throw new CliError(lines[0], { label: "\u542F\u52A8\u5931\u8D25", hint: [...lines.slice(1), ...extraHint] });
|
|
7329
|
+
throw new CliError(lines[0], { label: "\u542F\u52A8\u5931\u8D25", hint: [...lines.slice(1), ...tunHint] });
|
|
7253
7330
|
}
|
|
7254
7331
|
try {
|
|
7255
7332
|
await printStatus();
|
|
7256
7333
|
} catch (e) {
|
|
7257
|
-
console.log(colors.yellow(`\u72B6\u6001\u5C55\u793A\u5931\u8D25\uFF08\u4E0D\u5F71\u54CD\u5DF2\u5B8C\u6210\u7684\u542F\u52A8\uFF09: ${e.message
|
|
7334
|
+
console.log(colors.yellow(`\u72B6\u6001\u5C55\u793A\u5931\u8D25\uFF08\u4E0D\u5F71\u54CD\u5DF2\u5B8C\u6210\u7684\u542F\u52A8\uFF09: ${firstLine(e.message)}`));
|
|
7258
7335
|
}
|
|
7259
7336
|
if (targetMode === "mixed") {
|
|
7260
|
-
printSystemProxyHint(
|
|
7337
|
+
printSystemProxyHint(getPortsOrNull()?.mixed ?? DEFAULT_MIXED_PORT);
|
|
7261
7338
|
} else {
|
|
7262
7339
|
console.log(colors.gray("TUN \u4E3A\u4E34\u65F6\u8FDB\u7A0B\uFF0C\u5173\u95ED\u7EC8\u7AEF\u4E0D\u4F1A\u505C\u6B62\uFF1B\u505C\u6B62: mihomo-cli stop\uFF08\u4E4B\u540E mihomo-cli start \u6062\u590D Mixed\uFF09"));
|
|
7263
7340
|
}
|
|
@@ -7544,7 +7621,9 @@ async function getLatestRelease(repo, opts) {
|
|
|
7544
7621
|
encoding: "utf8",
|
|
7545
7622
|
maxBuffer: 50 * 1024 * 1024,
|
|
7546
7623
|
timeout: KERNEL_HTTP_TIMEOUT + 1e4,
|
|
7547
|
-
signal: opts.signal
|
|
7624
|
+
signal: opts.signal,
|
|
7625
|
+
// no_proxy 例外表会绕过显式 -x——统一出口置空,保 -x 权威(curl-spawn.ts)
|
|
7626
|
+
env: buildCurlSpawnEnv()
|
|
7548
7627
|
});
|
|
7549
7628
|
stdout = result.stdout;
|
|
7550
7629
|
} catch (e) {
|
|
@@ -7622,6 +7701,8 @@ function buildKernelCurlArgs(args) {
|
|
|
7622
7701
|
];
|
|
7623
7702
|
if (args.proxy) {
|
|
7624
7703
|
argv.push("-x", args.proxy);
|
|
7704
|
+
} else {
|
|
7705
|
+
argv.push("--noproxy", "*");
|
|
7625
7706
|
}
|
|
7626
7707
|
argv.push("-o", args.outputPath, args.url);
|
|
7627
7708
|
return argv;
|
|
@@ -7717,7 +7798,8 @@ async function downloadKernel(progressCallback, channel, releaseInfo) {
|
|
|
7717
7798
|
maxBytes,
|
|
7718
7799
|
outputPath: tempPath
|
|
7719
7800
|
}),
|
|
7720
|
-
|
|
7801
|
+
// no_proxy 例外表会绕过显式 -x——统一出口置空,保 -x 权威(curl-spawn.ts)
|
|
7802
|
+
{ stdio: "inherit", env: buildCurlSpawnEnv() }
|
|
7721
7803
|
);
|
|
7722
7804
|
if (curlResult.error) {
|
|
7723
7805
|
if (curlResult.error.code === "ENOENT") {
|
|
@@ -7925,7 +8007,7 @@ async function collectChecks() {
|
|
|
7925
8007
|
currentVersion: getKernelVersion(kernelProbe)
|
|
7926
8008
|
}),
|
|
7927
8009
|
4e3
|
|
7928
|
-
).catch(() => null) : Promise.resolve(null);
|
|
8010
|
+
).catch((e) => e instanceof Error ? e : null) : Promise.resolve(null);
|
|
7929
8011
|
if (!kernelProbe) {
|
|
7930
8012
|
push("\u5185\u6838", "fail", "\u672A\u5B89\u88C5", "mihomo-cli kernel");
|
|
7931
8013
|
} else if (kernelProbe.status === 0 && kernelProbe.version !== null) {
|
|
@@ -8041,7 +8123,7 @@ async function collectChecks() {
|
|
|
8041
8123
|
}
|
|
8042
8124
|
} catch (e) {
|
|
8043
8125
|
const notes = e instanceof CliError ? e.hint.filter((l) => l.trim().length > 0) : void 0;
|
|
8044
|
-
push("\u914D\u7F6E\u6784\u5EFA", "fail", e.message
|
|
8126
|
+
push("\u914D\u7F6E\u6784\u5EFA", "fail", firstLine(e.message), "\u4FEE\u6B63\u8BA2\u9605\u6216\u8986\u5199\u540E mihomo-cli start", notes);
|
|
8045
8127
|
}
|
|
8046
8128
|
} else {
|
|
8047
8129
|
push("\u914D\u7F6E\u6784\u5EFA", "skip", active ? "\u672A\u5B89\u88C5\u5185\u6838\uFF0C\u8DF3\u8FC7\u6821\u9A8C" : "\u65E0\u8BA2\u9605\uFF0C\u8DF3\u8FC7");
|
|
@@ -8057,8 +8139,10 @@ async function collectChecks() {
|
|
|
8057
8139
|
push("\u4EE3\u7406\u8FDE\u901A", "skip", "\u672A\u8FD0\u884C");
|
|
8058
8140
|
}
|
|
8059
8141
|
const kernelInfo = await kernelVersionPromise;
|
|
8060
|
-
|
|
8061
|
-
|
|
8142
|
+
const kernelPresent = hasKernel();
|
|
8143
|
+
if (kernelInfo instanceof Error || kernelInfo === null && kernelPresent) {
|
|
8144
|
+
const detail = kernelInfo instanceof Error && (kernelInfo.name !== "AbortError" || !kernelPresent) ? `\uFF08${firstLine(kernelInfo.message)}\uFF09` : "";
|
|
8145
|
+
push("\u5185\u6838\u7248\u672C", "skip", `${kernelPresent ? "GitHub \u4E0D\u53EF\u8FBE\uFF0C\u8DF3\u8FC7\u68C0\u67E5" : "\u8DF3\u8FC7\u68C0\u67E5"}${detail}`);
|
|
8062
8146
|
} else if (kernelInfo?.needsUpdate) {
|
|
8063
8147
|
push("\u5185\u6838\u7248\u672C", "warn", `\u5F53\u524D ${kernelInfo.current}\uFF0C\u6700\u65B0 ${kernelInfo.latest}`, "mihomo-cli kernel");
|
|
8064
8148
|
} else if (kernelInfo) {
|
|
@@ -8271,7 +8355,7 @@ function printChannelLine(channel, isExplicitProxy) {
|
|
|
8271
8355
|
console.log("");
|
|
8272
8356
|
}
|
|
8273
8357
|
function formatChannelSwitchLine(previousError) {
|
|
8274
|
-
const reason = previousError
|
|
8358
|
+
const reason = previousError ? firstLine(previousError.message) : "";
|
|
8275
8359
|
return reason ? `\u4E0A\u4E00\u901A\u9053\u5931\u8D25\uFF08${reason}\uFF09\uFF0C\u5207\u6362\u4E3A:` : "\u4E0A\u4E00\u901A\u9053\u5931\u8D25\uFF0C\u5207\u6362\u4E3A:";
|
|
8276
8360
|
}
|
|
8277
8361
|
function buildDownloadFailureError(attempts, isExplicitProxy) {
|
|
@@ -8292,7 +8376,7 @@ function buildDownloadFailureError(attempts, isExplicitProxy) {
|
|
|
8292
8376
|
return new CliError("\u5168\u90E8\u4E0B\u8F7D\u901A\u9053\u5747\u5931\u8D25", {
|
|
8293
8377
|
label: "\u4E0B\u8F7D\u5931\u8D25",
|
|
8294
8378
|
hint: [
|
|
8295
|
-
...attempts.map((a) => ` ${channelLabel(a.channel, isExplicitProxy)}: ${a.error.message
|
|
8379
|
+
...attempts.map((a) => ` ${channelLabel(a.channel, isExplicitProxy)}: ${firstLine(a.error.message)}`),
|
|
8296
8380
|
"",
|
|
8297
8381
|
"\u82E5\u4E24\u6761\u901A\u9053\u90FD\u662F\u4F4E\u901F\u5931\u8D25\uFF1A\u95EE\u9898\u5728\u5F53\u524D\u9009\u4E2D\u7684\u673A\u573A\u8282\u70B9\uFF08url-test \u53EA\u6309\u63E1\u624B\u5EF6\u8FDF\u9009\u3001\u4E0D\u6D4B\u5E26\u5BBD\uFF09\uFF0C",
|
|
8298
8382
|
"\u5728\u9762\u677F\u91CC\u624B\u52A8\u7ED9 Default Proxy \u6362\u4E2A\u7EBF\u8DEF\u6216\u8282\u70B9\u540E\u91CD\u8BD5\uFF1B\u4E5F\u53EF\u6362\u4E2A\u65F6\u95F4\u7B49 url-test \u91CD\u9009",
|
|
@@ -8687,6 +8771,9 @@ async function cmdReset(args) {
|
|
|
8687
8771
|
}
|
|
8688
8772
|
recordServiceStopped();
|
|
8689
8773
|
}
|
|
8774
|
+
if (!needsStop && serviceTargeted && cleanup !== null && cleanup.remaining.length > 0) {
|
|
8775
|
+
printResidueWarning(cleanup.remaining);
|
|
8776
|
+
}
|
|
8690
8777
|
const deleted = /* @__PURE__ */ new Set();
|
|
8691
8778
|
for (const target of targets) {
|
|
8692
8779
|
let hadContent = target.id === "service" && (serviceActive || legacy);
|
|
@@ -8764,8 +8851,7 @@ async function cmdInstall(args) {
|
|
|
8764
8851
|
printRestoreSkipped();
|
|
8765
8852
|
return;
|
|
8766
8853
|
}
|
|
8767
|
-
throw
|
|
8768
|
-
label: e.label,
|
|
8854
|
+
throw relabelCliError(e, {
|
|
8769
8855
|
hint: [...e.hint, "", "\u670D\u52A1\u5DF2\u5B89\u88C5\u6210\u529F\uFF0C\u4EC5\u6062\u590D\u8FD0\u884C\u5931\u8D25\uFF1B\u4FEE\u6B63\u914D\u7F6E\u540E\u53EF\u6267\u884C mihomo-cli start \u91CD\u8BD5\u3002"]
|
|
8770
8856
|
});
|
|
8771
8857
|
}
|
|
@@ -8805,8 +8891,7 @@ async function cmdUninstall(args) {
|
|
|
8805
8891
|
if (remaining === null) remaining = getMihomoPids();
|
|
8806
8892
|
if (remaining.length > 0) {
|
|
8807
8893
|
console.log("");
|
|
8808
|
-
|
|
8809
|
-
console.log(MANUAL_PKILL_HINT);
|
|
8894
|
+
printResidueWarning(remaining);
|
|
8810
8895
|
}
|
|
8811
8896
|
console.log(colors.gray("\u91CD\u65B0\u5B89\u88C5: mihomo-cli install"));
|
|
8812
8897
|
console.log(colors.gray("\u5F7B\u5E95\u79FB\u9664 mihomo-cli:"));
|
|
@@ -8851,12 +8936,12 @@ async function cmdStop(args) {
|
|
|
8851
8936
|
import { spawnSync as spawnSync10 } from "child_process";
|
|
8852
8937
|
function printRestartHintIfRunning(variant = "update") {
|
|
8853
8938
|
const state = getRunningState();
|
|
8854
|
-
if (state.running)
|
|
8855
|
-
|
|
8856
|
-
|
|
8857
|
-
|
|
8858
|
-
|
|
8859
|
-
|
|
8939
|
+
if (!state.running) return;
|
|
8940
|
+
const hintCommand = startCommandForCurrentMode(state);
|
|
8941
|
+
const removedSuffix = variant === "removed-last" ? `\uFF08\u8BA2\u9605\u5DF2\u5168\u90E8\u5220\u9664\uFF09\uFF0C\u91CD\u65B0\u6DFB\u52A0\u8BA2\u9605\u540E\u6267\u884C ${hintCommand}` : `\uFF0C\u6267\u884C ${hintCommand} \u5207\u6362\u5230\u65B0\u8BA2\u9605`;
|
|
8942
|
+
const message = variant === "update" ? `\u63D0\u793A: \u8FD0\u884C\u4E2D\u7684\u5B9E\u4F8B\u4ECD\u4F7F\u7528\u65E7\u914D\u7F6E\uFF0C\u6267\u884C ${hintCommand} \u4F7F\u66F4\u65B0\u751F\u6548` : `\u63D0\u793A: \u8FD0\u884C\u4E2D\u7684\u5B9E\u4F8B\u4ECD\u5728\u4F7F\u7528\u5DF2\u5220\u9664\u8BA2\u9605\u7684\u914D\u7F6E${removedSuffix}`;
|
|
8943
|
+
console.log(colors.yellow(message));
|
|
8944
|
+
console.log("");
|
|
8860
8945
|
}
|
|
8861
8946
|
function printSubscriptionList() {
|
|
8862
8947
|
const subs = getSubscriptionsWithCache();
|
|
@@ -8947,7 +9032,7 @@ async function subAdd(args) {
|
|
|
8947
9032
|
console.log(`\u5DF2\u6DFB\u52A0\u5E76\u5207\u6362\u5230 "${name}" (${formatProxySummary(info)})`);
|
|
8948
9033
|
} catch (e) {
|
|
8949
9034
|
removeSubscription(name);
|
|
8950
|
-
if (e instanceof CliError) throw
|
|
9035
|
+
if (e instanceof CliError) throw relabelCliError(e, { label: "\u6DFB\u52A0\u5931\u8D25" });
|
|
8951
9036
|
throw new CliError(e.message, { label: "\u6DFB\u52A0\u5931\u8D25" });
|
|
8952
9037
|
}
|
|
8953
9038
|
console.log("");
|
|
@@ -8991,7 +9076,7 @@ async function subUpdate(args) {
|
|
|
8991
9076
|
console.log(`\u66F4\u65B0\u8BA2\u9605: ${target.name}`);
|
|
8992
9077
|
const result = await withSpinner("\u4E0B\u8F7D\u8BA2\u9605", () => tryUpdateOne(target));
|
|
8993
9078
|
if (!result.success) {
|
|
8994
|
-
throw new CliError((result.error || "")
|
|
9079
|
+
throw new CliError(firstLine(result.error || ""), { label: "\u66F4\u65B0\u5931\u8D25" });
|
|
8995
9080
|
}
|
|
8996
9081
|
console.log(`\u5DF2\u66F4\u65B0 (${formatProxySummary(result)})`);
|
|
8997
9082
|
console.log("");
|
|
@@ -9054,14 +9139,16 @@ async function subRemove(args) {
|
|
|
9054
9139
|
return;
|
|
9055
9140
|
}
|
|
9056
9141
|
}
|
|
9057
|
-
const { found, switchedTo } = removeSubscription(target.name);
|
|
9142
|
+
const { found, switchedTo, wasActive } = removeSubscription(target.name);
|
|
9058
9143
|
if (!found) {
|
|
9059
9144
|
throw new CliError(`\u8BA2\u9605 "${target.name}" \u4E0D\u5B58\u5728\uFF08\u53EF\u80FD\u5DF2\u88AB\u5E76\u53D1\u5220\u9664\uFF09`, { hint: ["\u67E5\u770B\u5F53\u524D\u5217\u8868: mihomo-cli sub"] });
|
|
9060
9145
|
}
|
|
9061
9146
|
console.log(`\u5DF2\u5220\u9664\u8BA2\u9605 "${target.name}"`);
|
|
9062
9147
|
if (switchedTo) {
|
|
9063
9148
|
console.log(`\u5DF2\u81EA\u52A8\u5207\u6362\u5230 "${switchedTo}"`);
|
|
9064
|
-
|
|
9149
|
+
}
|
|
9150
|
+
if (wasActive) {
|
|
9151
|
+
printRestartHintIfRunning(switchedTo ? "removed-active" : "removed-last");
|
|
9065
9152
|
}
|
|
9066
9153
|
console.log("");
|
|
9067
9154
|
printSubscriptionList();
|
|
@@ -9103,7 +9190,7 @@ function resolveUiName(args) {
|
|
|
9103
9190
|
}
|
|
9104
9191
|
function copyToClipboard(text) {
|
|
9105
9192
|
try {
|
|
9106
|
-
return spawnSync11("pbcopy", [], { input: text }).status === 0;
|
|
9193
|
+
return spawnSync11("pbcopy", [], { input: text, timeout: 3e3 }).status === 0;
|
|
9107
9194
|
} catch {
|
|
9108
9195
|
return false;
|
|
9109
9196
|
}
|
|
@@ -9118,12 +9205,12 @@ function cmdUI(args) {
|
|
|
9118
9205
|
console.log("");
|
|
9119
9206
|
}
|
|
9120
9207
|
const url = UI_URLS[uiName];
|
|
9121
|
-
const
|
|
9208
|
+
const endpoint = getControllerEndpoint();
|
|
9122
9209
|
console.log(`\u6253\u5F00 Web UI: ${uiName}`);
|
|
9123
9210
|
console.log(`\u9875\u9762: ${url}`);
|
|
9124
|
-
console.log(`\u63A7\u5236\u5668: http://127.0.0.1:${
|
|
9125
|
-
const secret =
|
|
9126
|
-
if (
|
|
9211
|
+
console.log(`\u63A7\u5236\u5668: http://127.0.0.1:${endpoint.port ?? "\uFF08settings.json \u7684 ports \u914D\u7F6E\u975E\u6CD5\uFF0C\u65E0\u6CD5\u786E\u5B9A\uFF09"}`);
|
|
9212
|
+
const secret = endpoint.secret;
|
|
9213
|
+
if (secret) {
|
|
9127
9214
|
if (copySecret) {
|
|
9128
9215
|
console.log(
|
|
9129
9216
|
copyToClipboard(secret) ? "\u8BBF\u95EE\u5BC6\u94A5\u5DF2\u590D\u5236\u5230\u526A\u8D34\u677F\uFF0CUI \u8FDE\u63A5\u65F6\u7C98\u8D34" : colors.yellow("\u8BBF\u95EE\u5BC6\u94A5\u590D\u5236\u5931\u8D25\uFF0C\u8BF7\u5230 settings.json \u67E5\u770B controller_secret")
|