mihomo-cli 26.9.90 → 26.9.92
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 +57 -0
- package/dist/index.js +263 -103
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,62 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [26.9.92] - 2026-09-30
|
|
4
|
+
|
|
5
|
+
### 修复
|
|
6
|
+
|
|
7
|
+
- 两个终端同时 `mihomo start` 时,后到者不再报「启动服务失败(退出码 5)」——bootstrap 撞上先到者刚完成的任务时 launchctl 报 exit 5,与「disabled 标签」的报错完全同形(用户被指向错误的排查方向,服务实际健康在跑);现在收到 exit 5 会复读服务装载状态,已装载按幂等成功处理,真失败(未装载)维持报错
|
|
8
|
+
- `mihomo sub remove` 删除正在运行实例所使用的订阅时给出重启提示:此前 active 静默切到下一条订阅,运行中的内核仍服务已删除订阅的旧配置,用户看到「已自动切换到 X」会误以为代理已在用 X(add/update 早有同款提示,唯独 remove 漏了)
|
|
9
|
+
- 覆写扩展文件的加载顺序不再随系统语言(locale)漂移:同一组文件在中文/英文机器上可能合并出不同的运行配置,全程静默;改为固定的码点序,并补了锁定用例
|
|
10
|
+
- `https_proxy=http://0.0.0.0:7890`(及 `0:`、`[::]:` 等未指定地址族写法)现在能被识别为「指向本机自己的 Mixed 端口」并在启动/下载前清除——macOS 上这类地址实际路由到回环监听器,漏识别时重启内核后下载仍经已死的代理出网(与此前修的裸 localhost 同族漏网)
|
|
11
|
+
- 订阅内容携带顶层 `__proto__` 键时 `mihomo config` 不再崩溃:此前脱敏副本的原型被静默换掉、键丢失,展示时抛裸异常按程序 bug 渲染
|
|
12
|
+
- `mihomo ui` 在 ports 配置写坏(如两端口相同)时照常打开并打印降级文案,不再整个命令硬失败(status 对同一场景早有降级,UI 此前漏了)
|
|
13
|
+
- `mihomo start tun` 检测到另一终端已启动 Mixed 服务时,不再先把在跑服务的日志搬去归档才拒绝——旧顺序下拒绝后 `logs 0` 会看不到服务的新日志(日志继续写进归档文件)
|
|
14
|
+
- `mihomo reset <目标> --full` 同时给出时改为报错,不再静默忽略目标、扩成全量重置(本意多半是「彻底删这个目标」,被放大到删设置/内核/服务远超预期)
|
|
15
|
+
- `mihomo reset config` 报「未知目标」——`config` 不再是 settings 的别名;它与 `mihomo config` 命令的运行配置直觉对撞,照原语义执行会删超预期的订阅列表/端口/密钥
|
|
16
|
+
- `mihomo kernel --mirror=`(等号空值)与 `--mirror ""` 报错,不再静默按默认裸域处理(与 `--proxy=` 姿态对齐;裸 `--mirror` 语义不变)
|
|
17
|
+
- 显式指定镜像的用户在版本查询失败时拿到可执行指引(检查代理 / gh 认证),不再只剩裸「更新失败」——提示压制条件本意是「别再建议镜像」,此前把真正的出路也一起吞了
|
|
18
|
+
- 订阅更新时缓存写入失败(如 cache.json 被手改成目录)现在回滚刚写入的订阅文件并给出带标签的报错——此前报「更新失败」但新配置已落盘,回执与终态矛盾,且错误是无指引的裸系统报错
|
|
19
|
+
- `install` 的 plist 暂存文件挪出运行时目录:安装期间另一终端 stop/reset 删除该目录会让安装以裸文件错误失败
|
|
20
|
+
- 订阅缓存读取补齐 ENOENT 分支:并发删除间隙不再误报「格式损坏」(与 settings 的同族修复对齐)
|
|
21
|
+
- 原子写遗留 `*.tmp` 的清扫挪到守卫与豁免判定之后、目录覆盖扩到根目录/subscriptions/runtime 三处(清扫是删除动作,不在被拒或豁免的命令上执行)
|
|
22
|
+
|
|
23
|
+
### 调整
|
|
24
|
+
|
|
25
|
+
- service.lock 内的 launchctl 调用统一单次超时 3s:并发 start 撞车的幂等处理给锁内新增了一次调用,最坏持锁时长按立项纪律压回强夺阈值(10s)以内——launchctl 响应慢于 3s 时快速失败并报错,不再持锁等待
|
|
26
|
+
|
|
27
|
+
## [26.9.91] - 2026-09-30
|
|
28
|
+
|
|
29
|
+
### 修复
|
|
30
|
+
|
|
31
|
+
- `start tun` 补上对并发 `start` 的防线(此前只有 mixed 侧防「stop 被 start 覆盖」,反方向裸奔):TUN 分支确认服务停着后立即递增停止计数——慢速阶段(订阅更新 + sudo 密码窗口最长 60s)里另一终端 `start` 会在锁内检出变化而放弃启动;`startTun` 执行含 pkill 的 sudo 脚本前再复核一次服务装载状态,检出即中止,否则脚本会杀掉刚起的服务内核、KeepAlive 拉回后与 root TUN 内核互抢端口。
|
|
32
|
+
- 覆写文件写了 `match:` 但条件块缩进笔误(解析成空值)时不再静默全局生效,改为报错并给出正确写法——此前该文件会应用到**所有**订阅,笔误的垃圾键还进最终配置。
|
|
33
|
+
- `https_proxy=localhost:7890`(无协议、localhost 形态)现在能被识别为「指向本机自己的 Mixed 端口」并在启动/下载前清除:curl 与 gh 都认这个形态的代理 env,漏识别时重启内核后下载仍经已死的代理出网,正是这段清除逻辑唯一要防的死锁。
|
|
34
|
+
- 订阅缓存 cache.json 变成「合法 JSON 但不是对象」(如数组/数字)时先备份 `.bak` 再回退空缓存——与 settings.json 的既有处理对齐,此前下一次写缓存会把原件无声覆盖。
|
|
35
|
+
- sudo 脚本改写在数据根目录而非 runtime/:runtime 会被 stop/reset 整体删除,密码窗口内脚本被连带删掉的话,用户输完密码后 sudo 执行不存在的文件,错误被误诊成「密码错误」。
|
|
36
|
+
- 清理 root 属主 pid 文件的 sudo 分支超时对齐统一的 60s 并检查结果:此前自抄 10s,密码输得慢的用户提示被杀、删除从未执行且无人知晓。
|
|
37
|
+
|
|
38
|
+
### 内部
|
|
39
|
+
|
|
40
|
+
- 删除无消费者的 `StopResult.warning` 字段与探测 curl 恒不可达的 `--connect-timeout` 参数;CLAUDE.md 修正版本查询优先级描述(代理可用直接经代理)并补登 `system-proxy.ts` 模块行。
|
|
41
|
+
|
|
42
|
+
### 修复(第二轮复审)
|
|
43
|
+
|
|
44
|
+
- 覆写里出现 `__proto__` 键(无论嵌套层还是 `~key` 元素补丁)改为可读报错:此前它会静默把合并结果的原型换掉,写配置时抛带堆栈的裸异常、按程序 bug 渲染,用户无从定位源头。
|
|
45
|
+
- `sub remove` 删原始配置文件挪到设置写盘成功之后(仍在同一锁内):此前先删文件后写设置,磁盘满/权限失败时留下「条目还在、文件已没」的不一致。
|
|
46
|
+
- 原子写补 fsync(临时文件 + rename 后父目录):OS 崩溃/掉电时 settings.json 截断为空的窗口收窄到 fsync 返回之后(macOS 上普通 fsync 只刷页缓存,严格落盘需 F_FULLFSYNC,为已知边界);崩溃遗留的 `*.tmp` 临时文件由每次命令执行时按龄清扫(数据根目录、订阅与运行时目录三处)。
|
|
47
|
+
- `doctor` 的内核版本查询超时改为真正中止子进程(AbortSignal 透传 gh/curl/直连三路):此前只弃掉等待,报告打完后进程还要等满子进程自身超时(最长约两分钟)才退出,CI 里体检结论已打印却拿不到退出码。
|
|
48
|
+
- 覆写 `match` 的 `url-domain` 值含 `*`/`?` 时报错:它只做字面后缀比对,通配恒不命中,此前文件静默对任何订阅都不生效且零提示。
|
|
49
|
+
- `mihomo kernel` 在 settings.ports 损坏时降级走 gh/直连(与 doctor/status 同姿态),不再在做任何下载前就中止。
|
|
50
|
+
- `start tun` 的日志轮转挪到内核/配置存在性校验之后:秒失败错误不再先把日志 rename 成归档、留下运行中内核往归档文件写的错位。
|
|
51
|
+
- 逐 pid 清理内核进程在发信号前复核命令行(`isMihomoProcess`):pid 被复用时不再可能误杀无关进程,与批量 pkill 分支的安全性对齐。
|
|
52
|
+
- `logs 0 -f -o` 报互斥错误:`-o` 打开系统查看器后 `-f` 无处生效,静默忽略会让用户以为在跟随刷新。
|
|
53
|
+
- `sub add <url> ""` 报「订阅名不能为空」:不再静默把空串命名成 default(与其他命令对空串位置参数的处理对齐)。
|
|
54
|
+
- settings.json 在并发 `reset` 间隙被删除时不再误报「格式损坏」。
|
|
55
|
+
|
|
56
|
+
### 内部(第二轮)
|
|
57
|
+
|
|
58
|
+
- `YAML_MAX_ALIASES` 收成共享常量(config/overwrite 两处解析共用);`needsAutoUpdate` 与 `isSubscriptionStale` 对异常时间戳**刻意相反**的口径加用例锁死,防止重构合并去重时悄悄统一。
|
|
59
|
+
|
|
3
60
|
## [26.9.90] - 2026-09-29
|
|
4
61
|
|
|
5
62
|
版本号序号修正,无代码变化:26.9.37 的序号误取了当月发布计数,自本版起序号为**全局历史发布计数**(本版是第 90 个发布)。年.月 仍取发布当日日历,序号永不重置。26.9.37 已标记 deprecated,装到该版本执行 `mihomo update` 会直接升到本版。
|
package/dist/index.js
CHANGED
|
@@ -3121,6 +3121,7 @@ var CHOMPING_KEEP = CHOMPING_MODE.KEEP;
|
|
|
3121
3121
|
import { createRequire } from "module";
|
|
3122
3122
|
var require2 = createRequire(import.meta.url);
|
|
3123
3123
|
var pkg = require2("../package.json");
|
|
3124
|
+
var YAML_MAX_ALIASES = 200;
|
|
3124
3125
|
var VERSION = pkg.version;
|
|
3125
3126
|
var PKG_NAME = pkg.name;
|
|
3126
3127
|
var MIN_NODE_VERSION = (() => {
|
|
@@ -3265,6 +3266,14 @@ var PATHS = {
|
|
|
3265
3266
|
configFile: path.join(DIRS.runtime, "config.yaml"),
|
|
3266
3267
|
logFile: path.join(DIRS.logs, "mihomo.log"),
|
|
3267
3268
|
pidFile: path.join(DIRS.runtime, "pid"),
|
|
3269
|
+
/**
|
|
3270
|
+
* installService 的 plist 暂存文件(plutil -lint 校验通过后才 copyFileSync 到
|
|
3271
|
+
* LaunchAgents)。**不放 runtime/**:stage 要活到 copy 那一刻,中间隔着 plutil、
|
|
3272
|
+
* bootout、waitUntilUnloaded(最多 5s)——此窗口并发 stop(游离内核路径 rmrf runtime/)
|
|
3273
|
+
* 或含 runtime 目标的 reset 删掉目录,copyFileSync 就裸 ENOENT。与锁文件同族
|
|
3274
|
+
* (「runtime 会被整体删除,不能放有生命周期的文件」),用后即删(service.ts finally)
|
|
3275
|
+
*/
|
|
3276
|
+
servicePlistStage: path.join(USER_DATA_DIR, "service.plist.stage"),
|
|
3268
3277
|
/**
|
|
3269
3278
|
* 跨进程锁文件。**全部以 `Lock` 结尾命名并放在 USER_DATA_DIR 根下**——这两点都是不变量,
|
|
3270
3279
|
* `paths.spec.ts` 按命名约定枚举它们并断言位置,故新增锁只要照此命名就自动进回归测试。
|
|
@@ -3322,8 +3331,23 @@ var atomicWriteSeq = 0;
|
|
|
3322
3331
|
function atomicWriteFileSync(filePath, content, options) {
|
|
3323
3332
|
const tmp = `${filePath}.${process.pid}.${atomicWriteSeq++}.tmp`;
|
|
3324
3333
|
try {
|
|
3325
|
-
fs.
|
|
3334
|
+
const fd = fs.openSync(tmp, "w", options?.mode ?? 384);
|
|
3335
|
+
try {
|
|
3336
|
+
fs.writeFileSync(fd, content);
|
|
3337
|
+
fs.fsyncSync(fd);
|
|
3338
|
+
} finally {
|
|
3339
|
+
fs.closeSync(fd);
|
|
3340
|
+
}
|
|
3326
3341
|
fs.renameSync(tmp, filePath);
|
|
3342
|
+
try {
|
|
3343
|
+
const dirFd = fs.openSync(path.dirname(filePath), "r");
|
|
3344
|
+
try {
|
|
3345
|
+
fs.fsyncSync(dirFd);
|
|
3346
|
+
} finally {
|
|
3347
|
+
fs.closeSync(dirFd);
|
|
3348
|
+
}
|
|
3349
|
+
} catch {
|
|
3350
|
+
}
|
|
3327
3351
|
} catch (e) {
|
|
3328
3352
|
try {
|
|
3329
3353
|
fs.unlinkSync(tmp);
|
|
@@ -3335,6 +3359,22 @@ function atomicWriteFileSync(filePath, content, options) {
|
|
|
3335
3359
|
function rmrf(dir) {
|
|
3336
3360
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
3337
3361
|
}
|
|
3362
|
+
function cleanupStaleTmpFiles() {
|
|
3363
|
+
for (const dir of [USER_DATA_DIR, DIRS.subscriptions, DIRS.runtime]) {
|
|
3364
|
+
try {
|
|
3365
|
+
const cutoff = Date.now() - 60 * 60 * 1e3;
|
|
3366
|
+
for (const entry of fs.readdirSync(dir)) {
|
|
3367
|
+
if (!entry.endsWith(".tmp")) continue;
|
|
3368
|
+
const full = path.join(dir, entry);
|
|
3369
|
+
try {
|
|
3370
|
+
if (fs.statSync(full).mtimeMs < cutoff) fs.rmSync(full, { force: true });
|
|
3371
|
+
} catch {
|
|
3372
|
+
}
|
|
3373
|
+
}
|
|
3374
|
+
} catch {
|
|
3375
|
+
}
|
|
3376
|
+
}
|
|
3377
|
+
}
|
|
3338
3378
|
var LOCK_STALE_MS = 1e4;
|
|
3339
3379
|
var LOCK_RETRY_MS = 20;
|
|
3340
3380
|
function withFileLock(lockPath, fn, opts) {
|
|
@@ -3431,7 +3471,8 @@ function readSettings() {
|
|
|
3431
3471
|
const parsed = JSON.parse(fs2.readFileSync(PATHS.settingsFile, "utf8"));
|
|
3432
3472
|
if (parsed !== null && typeof parsed === "object" && !Array.isArray(parsed)) return parsed;
|
|
3433
3473
|
return backupCorruptSettings(`\u5185\u5BB9\u4E0D\u662F\u5BF9\u8C61\uFF08\u5F53\u524D\u662F${Array.isArray(parsed) ? "\u6570\u7EC4" : parsed === null ? "null" : typeof parsed}\uFF09`);
|
|
3434
|
-
} catch {
|
|
3474
|
+
} catch (e) {
|
|
3475
|
+
if (e.code === "ENOENT") return {};
|
|
3435
3476
|
return backupCorruptSettings("\u683C\u5F0F\u635F\u574F");
|
|
3436
3477
|
}
|
|
3437
3478
|
}
|
|
@@ -3446,7 +3487,7 @@ function isValidSettingsContent(content) {
|
|
|
3446
3487
|
function writeSettings(settings) {
|
|
3447
3488
|
return updateSettings(() => settings);
|
|
3448
3489
|
}
|
|
3449
|
-
function updateSettings(mutate) {
|
|
3490
|
+
function updateSettings(mutate, postCommit) {
|
|
3450
3491
|
ensureDirs();
|
|
3451
3492
|
return withFileLock(PATHS.settingsLock, () => {
|
|
3452
3493
|
const current = readSettings();
|
|
@@ -3457,6 +3498,7 @@ function updateSettings(mutate) {
|
|
|
3457
3498
|
if (value === void 0) delete merged[key];
|
|
3458
3499
|
}
|
|
3459
3500
|
atomicWriteFileSync(PATHS.settingsFile, JSON.stringify(merged, null, 2), { mode: 384 });
|
|
3501
|
+
postCommit?.();
|
|
3460
3502
|
return merged;
|
|
3461
3503
|
});
|
|
3462
3504
|
}
|
|
@@ -3540,25 +3582,31 @@ function readSubscriptionCache() {
|
|
|
3540
3582
|
try {
|
|
3541
3583
|
const content = fs2.readFileSync(PATHS.subscriptionsCacheFile, "utf8");
|
|
3542
3584
|
const parsed = JSON.parse(content);
|
|
3543
|
-
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed))
|
|
3544
|
-
|
|
3545
|
-
} catch {
|
|
3546
|
-
const cacheBackup = `${PATHS.subscriptionsCacheFile}.bak`;
|
|
3547
|
-
try {
|
|
3548
|
-
if (fs2.existsSync(cacheBackup)) {
|
|
3549
|
-
console.warn(`\u8B66\u544A: \u8BA2\u9605\u7F13\u5B58\u683C\u5F0F\u635F\u574F\uFF0C\u5DF2\u5FFD\u7565\uFF08\u65E9\u524D\u5907\u4EFD\u4FDD\u7559\u5728 ${cacheBackup}\uFF0C\u672A\u8986\u76D6\uFF09`);
|
|
3550
|
-
} else {
|
|
3551
|
-
fs2.copyFileSync(PATHS.subscriptionsCacheFile, cacheBackup);
|
|
3552
|
-
console.warn(`\u8B66\u544A: \u8BA2\u9605\u7F13\u5B58\u683C\u5F0F\u635F\u574F\uFF0C\u5DF2\u5907\u4EFD\u5230 ${cacheBackup}`);
|
|
3553
|
-
}
|
|
3554
|
-
} catch {
|
|
3555
|
-
console.warn("\u8B66\u544A: \u8BA2\u9605\u7F13\u5B58\u683C\u5F0F\u635F\u574F\uFF0C\u5DF2\u5FFD\u7565");
|
|
3585
|
+
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
3586
|
+
return backupCorruptSubscriptionCache(`\u5185\u5BB9\u4E0D\u662F\u5BF9\u8C61\uFF08\u5F53\u524D\u662F${Array.isArray(parsed) ? "\u6570\u7EC4" : parsed === null ? "null" : typeof parsed}\uFF09`);
|
|
3556
3587
|
}
|
|
3557
|
-
return empty();
|
|
3588
|
+
return Object.assign(empty(), parsed);
|
|
3589
|
+
} catch (e) {
|
|
3590
|
+
if (e.code === "ENOENT") return empty();
|
|
3591
|
+
return backupCorruptSubscriptionCache("\u683C\u5F0F\u635F\u574F");
|
|
3558
3592
|
}
|
|
3559
3593
|
}
|
|
3560
3594
|
return empty();
|
|
3561
3595
|
}
|
|
3596
|
+
function backupCorruptSubscriptionCache(reason) {
|
|
3597
|
+
const cacheBackup = `${PATHS.subscriptionsCacheFile}.bak`;
|
|
3598
|
+
try {
|
|
3599
|
+
if (fs2.existsSync(cacheBackup)) {
|
|
3600
|
+
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`);
|
|
3601
|
+
} else {
|
|
3602
|
+
fs2.copyFileSync(PATHS.subscriptionsCacheFile, cacheBackup);
|
|
3603
|
+
console.warn(`\u8B66\u544A: \u8BA2\u9605\u7F13\u5B58${reason}\uFF0C\u5DF2\u5907\u4EFD\u5230 ${cacheBackup}`);
|
|
3604
|
+
}
|
|
3605
|
+
} catch {
|
|
3606
|
+
console.warn(`\u8B66\u544A: \u8BA2\u9605\u7F13\u5B58${reason}\uFF0C\u5DF2\u5FFD\u7565`);
|
|
3607
|
+
}
|
|
3608
|
+
return /* @__PURE__ */ Object.create(null);
|
|
3609
|
+
}
|
|
3562
3610
|
function writeSubscriptionCache(cache) {
|
|
3563
3611
|
ensureDirs();
|
|
3564
3612
|
atomicWriteFileSync(PATHS.subscriptionsCacheFile, JSON.stringify(cache, null, 2), { mode: 384 });
|
|
@@ -3624,23 +3672,27 @@ function addSubscription(url, name = "default") {
|
|
|
3624
3672
|
function removeSubscription(name) {
|
|
3625
3673
|
let switchedTo = null;
|
|
3626
3674
|
let found = false;
|
|
3627
|
-
updateSettings(
|
|
3628
|
-
|
|
3629
|
-
|
|
3630
|
-
|
|
3631
|
-
|
|
3632
|
-
|
|
3633
|
-
|
|
3634
|
-
|
|
3635
|
-
|
|
3636
|
-
|
|
3637
|
-
|
|
3638
|
-
|
|
3639
|
-
|
|
3640
|
-
}
|
|
3675
|
+
updateSettings(
|
|
3676
|
+
(settings) => {
|
|
3677
|
+
const subs = getSubscriptions(settings);
|
|
3678
|
+
const idx = subs.findIndex((s) => s.name === name);
|
|
3679
|
+
if (idx < 0) return {};
|
|
3680
|
+
found = true;
|
|
3681
|
+
subs.splice(idx, 1);
|
|
3682
|
+
const updates = { subscriptions: subs };
|
|
3683
|
+
if (settings.active_subscription === name) {
|
|
3684
|
+
switchedTo = subs.length > 0 ? subs[0].name : null;
|
|
3685
|
+
updates.active_subscription = switchedTo ?? void 0;
|
|
3686
|
+
}
|
|
3687
|
+
return updates;
|
|
3688
|
+
},
|
|
3689
|
+
() => {
|
|
3690
|
+
try {
|
|
3691
|
+
fs2.rmSync(getSubscriptionRawConfigPath(name), { force: true });
|
|
3692
|
+
} catch {
|
|
3693
|
+
}
|
|
3641
3694
|
}
|
|
3642
|
-
|
|
3643
|
-
});
|
|
3695
|
+
);
|
|
3644
3696
|
if (!found) return null;
|
|
3645
3697
|
deleteSubscriptionCache(name);
|
|
3646
3698
|
return switchedTo;
|
|
@@ -3664,6 +3716,10 @@ function saveSubscriptionRawConfig(subName, content) {
|
|
|
3664
3716
|
const filePath = getSubscriptionRawConfigPath(subName);
|
|
3665
3717
|
atomicWriteFileSync(filePath, content, { mode: 384 });
|
|
3666
3718
|
}
|
|
3719
|
+
function removeSubscriptionRawConfig(subName) {
|
|
3720
|
+
const filePath = getSubscriptionRawConfigPath(subName);
|
|
3721
|
+
fs2.rmSync(filePath, { force: true });
|
|
3722
|
+
}
|
|
3667
3723
|
function readSubscriptionRawConfig(subName) {
|
|
3668
3724
|
const filePath = getSubscriptionRawConfigPath(subName);
|
|
3669
3725
|
if (!fs2.existsSync(filePath)) return null;
|
|
@@ -3767,6 +3823,12 @@ function mergeConfigLevel(target, override, collectors, parseOperators) {
|
|
|
3767
3823
|
} else if (isOperatorShapedNestedKey(rawKey)) {
|
|
3768
3824
|
noteOperatorShapedKey(collectors, rawKey);
|
|
3769
3825
|
}
|
|
3826
|
+
if (key === "__proto__") {
|
|
3827
|
+
throw new CliError('\u8986\u5199\u91CC\u51FA\u73B0\u4E86 "__proto__" \u952E', {
|
|
3828
|
+
label: "\u8986\u5199\u914D\u7F6E\u9519\u8BEF",
|
|
3829
|
+
hint: ["\u6B63\u5E38 mihomo \u914D\u7F6E\u6CA1\u6709\u8FD9\u4E2A\u952E\uFF0C\u8BF7\u68C0\u67E5\u8986\u5199\u6587\u4EF6\u7684\u5185\u5BB9\u4E0E\u6765\u6E90\u3002"]
|
|
3830
|
+
});
|
|
3831
|
+
}
|
|
3770
3832
|
const existingValue = result[key];
|
|
3771
3833
|
if (arrayMergeByName) {
|
|
3772
3834
|
if (existingValue !== void 0 && !Array.isArray(existingValue)) {
|
|
@@ -3855,7 +3917,13 @@ function isOverwriteFilenameTypo(filename) {
|
|
|
3855
3917
|
var MATCH_KEYS = /* @__PURE__ */ new Set(["name", "subscription", "url-domain"]);
|
|
3856
3918
|
var SUBSCRIPTION_KEYS = ["name", "subscription"];
|
|
3857
3919
|
function normalizeMatch(raw, fileName) {
|
|
3858
|
-
if (raw
|
|
3920
|
+
if (raw === void 0) return void 0;
|
|
3921
|
+
if (raw === null) {
|
|
3922
|
+
throw new CliError(`\u8986\u5199\u6587\u4EF6 "${fileName}" \u7684 match \u4E3A\u7A7A`, {
|
|
3923
|
+
label: "\u8986\u5199\u914D\u7F6E\u9519\u8BEF",
|
|
3924
|
+
hint: ["match: \u540E\u9762\u8981\u8DDF\u6761\u4EF6\u5757\uFF08\u68C0\u67E5\u7F29\u8FDB\uFF09\uFF0C\u5982:", " match:", " name: edu*"]
|
|
3925
|
+
});
|
|
3926
|
+
}
|
|
3859
3927
|
if (typeof raw !== "object" || Array.isArray(raw)) {
|
|
3860
3928
|
throw new CliError(`\u8986\u5199\u6587\u4EF6 "${fileName}" \u7684 match \u5FC5\u987B\u662F\u5BF9\u8C61\uFF08name / url-domain\uFF09`, { label: "\u8986\u5199\u914D\u7F6E\u9519\u8BEF" });
|
|
3861
3929
|
}
|
|
@@ -3884,6 +3952,13 @@ function normalizeMatch(raw, fileName) {
|
|
|
3884
3952
|
result.subscriptionKey = key;
|
|
3885
3953
|
continue;
|
|
3886
3954
|
}
|
|
3955
|
+
if (key === "url-domain") {
|
|
3956
|
+
const wildcard = arr.find((v) => v.includes("*") || v.includes("?"));
|
|
3957
|
+
if (wildcard) {
|
|
3958
|
+
problems.push(`url-domain \u4E0D\u652F\u6301\u901A\u914D\u7B26\uFF08\u5F53\u524D\u503C "${wildcard}"\uFF09\uFF0C\u53EA\u505A\u5B57\u9762\u540E\u7F00\u6BD4\u5BF9`);
|
|
3959
|
+
continue;
|
|
3960
|
+
}
|
|
3961
|
+
}
|
|
3887
3962
|
result[key] = arr;
|
|
3888
3963
|
}
|
|
3889
3964
|
if (problems.length > 0) {
|
|
@@ -4030,7 +4105,7 @@ function readOverwriteFiles() {
|
|
|
4030
4105
|
const files = entries.filter(isOverwriteFilename).sort((a, b) => {
|
|
4031
4106
|
if (a === "overwrite.yaml") return -1;
|
|
4032
4107
|
if (b === "overwrite.yaml") return 1;
|
|
4033
|
-
return a
|
|
4108
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
4034
4109
|
});
|
|
4035
4110
|
for (const typo of entries.filter((e) => isOverwriteFilenameTypo(e))) {
|
|
4036
4111
|
console.warn(
|
|
@@ -4041,7 +4116,7 @@ function readOverwriteFiles() {
|
|
|
4041
4116
|
const filePath = path3.join(USER_DATA_DIR, file);
|
|
4042
4117
|
try {
|
|
4043
4118
|
const content = fs3.readFileSync(filePath, "utf8");
|
|
4044
|
-
const parsed = load(content, { maxAliases:
|
|
4119
|
+
const parsed = load(content, { maxAliases: YAML_MAX_ALIASES });
|
|
4045
4120
|
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
4046
4121
|
const { match, enabled, ...config } = parsed;
|
|
4047
4122
|
assertNoMetadataKeyLookalikes(config, file);
|
|
@@ -4151,12 +4226,13 @@ function shellQuote(s) {
|
|
|
4151
4226
|
}
|
|
4152
4227
|
function isLoopbackHost(host) {
|
|
4153
4228
|
const h = host.toLowerCase();
|
|
4154
|
-
return h === "127.0.0.1" || h === "localhost" || h === "::1";
|
|
4229
|
+
return h === "127.0.0.1" || h === "localhost" || h === "::1" || h === "0.0.0.0" || h === "::";
|
|
4155
4230
|
}
|
|
4156
4231
|
function proxyEnvPointsAtSelf(value, selfPort) {
|
|
4157
4232
|
let parsed;
|
|
4158
4233
|
try {
|
|
4159
4234
|
parsed = new URL(value);
|
|
4235
|
+
if (parsed.hostname === "") parsed = new URL(`http://${value}`);
|
|
4160
4236
|
} catch {
|
|
4161
4237
|
try {
|
|
4162
4238
|
parsed = new URL(`http://${value}`);
|
|
@@ -4441,6 +4517,12 @@ function parseMirrorArg(args) {
|
|
|
4441
4517
|
const mirrorIdx = args.indexOf("--mirror");
|
|
4442
4518
|
if (mirrorIdx >= 0 || mirrorEq) {
|
|
4443
4519
|
const inline = mirrorEq?.slice("--mirror=".length);
|
|
4520
|
+
if (inline === "" || mirrorIdx >= 0 && args[mirrorIdx + 1] === "") {
|
|
4521
|
+
throw new CliError("--mirror \u7684\u503C\u4E0D\u80FD\u4E3A\u7A7A\uFF08\u5355\u72EC\u7684 --mirror \u8868\u793A\u4F7F\u7528\u9ED8\u8BA4\u955C\u50CF\u57DF\uFF09", {
|
|
4522
|
+
label: "\u53C2\u6570\u9519\u8BEF",
|
|
4523
|
+
hint: ["\u7528\u6CD5: mihomo kernel [--mirror [\u955C\u50CF]]", `\u53EF\u7528\u955C\u50CF: ${AVAILABLE_MIRRORS.join(", ")}`, "\u4E0D\u4F7F\u7528\u955C\u50CF: mihomo kernel --mirror direct"]
|
|
4524
|
+
});
|
|
4525
|
+
}
|
|
4444
4526
|
const nextArg = inline ?? args[mirrorIdx + 1];
|
|
4445
4527
|
if (!nextArg || nextArg.startsWith("-")) {
|
|
4446
4528
|
return { mirror: MIRROR_BARE, isOverride: true };
|
|
@@ -4518,7 +4600,7 @@ function parseProxyArg(args) {
|
|
|
4518
4600
|
}
|
|
4519
4601
|
|
|
4520
4602
|
// src/config.ts
|
|
4521
|
-
var SAFE_YAML_LOAD_OPTIONS = { maxAliases:
|
|
4603
|
+
var SAFE_YAML_LOAD_OPTIONS = { maxAliases: YAML_MAX_ALIASES };
|
|
4522
4604
|
var LOCKED_CONFIG_KEYS = [
|
|
4523
4605
|
"mixed-port",
|
|
4524
4606
|
"port",
|
|
@@ -5060,6 +5142,10 @@ function getPid() {
|
|
|
5060
5142
|
}
|
|
5061
5143
|
function isRunning() {
|
|
5062
5144
|
const pid = getPid();
|
|
5145
|
+
if (!pid) return false;
|
|
5146
|
+
return isMihomoProcess(pid);
|
|
5147
|
+
}
|
|
5148
|
+
function isMihomoProcess(pid) {
|
|
5063
5149
|
if (!pid) return false;
|
|
5064
5150
|
return isProcessRunning(pid) && (isProcessCommandMatching(pid, PATHS.mihomoBinary) || isProcessCommandMatching(pid, PATHS.serviceBinary));
|
|
5065
5151
|
}
|
|
@@ -5148,7 +5234,7 @@ function runSudoScript(scriptBody, opts) {
|
|
|
5148
5234
|
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");
|
|
5149
5235
|
}
|
|
5150
5236
|
ensureDirs();
|
|
5151
|
-
const scriptPath = path6.join(
|
|
5237
|
+
const scriptPath = path6.join(USER_DATA_DIR, opts.file);
|
|
5152
5238
|
fs7.writeFileSync(scriptPath, scriptBody, { mode: 448 });
|
|
5153
5239
|
fs7.chmodSync(scriptPath, 448);
|
|
5154
5240
|
try {
|
|
@@ -5333,7 +5419,18 @@ function ensureServiceSymlink() {
|
|
|
5333
5419
|
throw e;
|
|
5334
5420
|
}
|
|
5335
5421
|
}
|
|
5336
|
-
|
|
5422
|
+
try {
|
|
5423
|
+
fs8.symlinkSync("mihomo", PATHS.serviceBinary);
|
|
5424
|
+
} catch (e) {
|
|
5425
|
+
if (e.code !== "EEXIST") throw e;
|
|
5426
|
+
let existing;
|
|
5427
|
+
try {
|
|
5428
|
+
existing = fs8.readlinkSync(PATHS.serviceBinary);
|
|
5429
|
+
} catch {
|
|
5430
|
+
throw e;
|
|
5431
|
+
}
|
|
5432
|
+
if (existing !== "mihomo") throw e;
|
|
5433
|
+
}
|
|
5337
5434
|
}
|
|
5338
5435
|
function runLaunchctlOrThrow(args, what, timeoutMs = LAUNCHCTL_TIMEOUT_MS) {
|
|
5339
5436
|
const result = runLaunchctl(args, timeoutMs);
|
|
@@ -5343,6 +5440,15 @@ function runLaunchctlOrThrow(args, what, timeoutMs = LAUNCHCTL_TIMEOUT_MS) {
|
|
|
5343
5440
|
hint: [detail, `\u624B\u52A8\u786E\u8BA4: launchctl ${args.join(" ")}`].filter(Boolean)
|
|
5344
5441
|
});
|
|
5345
5442
|
}
|
|
5443
|
+
function bootstrapServiceIdempotentOrThrow(what) {
|
|
5444
|
+
const result = runLaunchctl(["bootstrap", bootstrapDomain(), PATHS.userAgentPlist], SERVICE_LOCK_LAUNCHCTL_TIMEOUT_MS);
|
|
5445
|
+
if (result.status === 0) return;
|
|
5446
|
+
if (result.status === 5 && runLaunchctl(["print", serviceTarget()], SERVICE_LOCK_LAUNCHCTL_TIMEOUT_MS).status === 0) return;
|
|
5447
|
+
const detail = result.stderr.trim();
|
|
5448
|
+
throw new CliError(`${what}\u5931\u8D25\uFF08launchctl \u9000\u51FA\u7801 ${result.status ?? "\u6267\u884C\u5931\u8D25"}\uFF09`, {
|
|
5449
|
+
hint: [detail, `\u624B\u52A8\u786E\u8BA4: launchctl print ${serviceTarget()}`].filter(Boolean)
|
|
5450
|
+
});
|
|
5451
|
+
}
|
|
5346
5452
|
function bootoutService(timeoutMs = LAUNCHCTL_TIMEOUT_MS) {
|
|
5347
5453
|
const result = runLaunchctl(["bootout", serviceTarget()], timeoutMs);
|
|
5348
5454
|
if (result.status === 0 || result.status === 3 || result.status === LAUNCHCTL_NOT_LOADED) return;
|
|
@@ -5419,6 +5525,7 @@ function killResidualKernels(ctx) {
|
|
|
5419
5525
|
const rootPids = pids.filter(isProcessRoot);
|
|
5420
5526
|
for (const pid of pids) {
|
|
5421
5527
|
if (rootPids.includes(pid)) continue;
|
|
5528
|
+
if (!isMihomoProcess(pid)) continue;
|
|
5422
5529
|
try {
|
|
5423
5530
|
process.kill(pid, "SIGKILL");
|
|
5424
5531
|
} catch {
|
|
@@ -5431,7 +5538,7 @@ async function installService(wasRunning, stopEpochBefore) {
|
|
|
5431
5538
|
ensureDirs();
|
|
5432
5539
|
ensureServiceSymlink();
|
|
5433
5540
|
let restoreSkipped = false;
|
|
5434
|
-
const stagePath =
|
|
5541
|
+
const stagePath = PATHS.servicePlistStage;
|
|
5435
5542
|
atomicWriteFileSync(stagePath, buildPlist(), { mode: 384 });
|
|
5436
5543
|
try {
|
|
5437
5544
|
const lint = spawnSync4("plutil", ["-lint", stagePath], { encoding: "utf8", timeout: 1e4 });
|
|
@@ -5451,8 +5558,8 @@ async function installService(wasRunning, stopEpochBefore) {
|
|
|
5451
5558
|
restoreSkipped = true;
|
|
5452
5559
|
return;
|
|
5453
5560
|
}
|
|
5454
|
-
runLaunchctlOrThrow(["enable", serviceTarget()], "\u542F\u7528\u670D\u52A1");
|
|
5455
|
-
|
|
5561
|
+
runLaunchctlOrThrow(["enable", serviceTarget()], "\u542F\u7528\u670D\u52A1", SERVICE_LOCK_LAUNCHCTL_TIMEOUT_MS);
|
|
5562
|
+
bootstrapServiceIdempotentOrThrow("\u88C5\u8F7D\u670D\u52A1");
|
|
5456
5563
|
});
|
|
5457
5564
|
}
|
|
5458
5565
|
} finally {
|
|
@@ -5517,8 +5624,8 @@ async function startService(stopEpochBefore) {
|
|
|
5517
5624
|
started = false;
|
|
5518
5625
|
return;
|
|
5519
5626
|
}
|
|
5520
|
-
runLaunchctlOrThrow(["enable", serviceTarget()], "\u542F\u7528\u670D\u52A1");
|
|
5521
|
-
|
|
5627
|
+
runLaunchctlOrThrow(["enable", serviceTarget()], "\u542F\u7528\u670D\u52A1", SERVICE_LOCK_LAUNCHCTL_TIMEOUT_MS);
|
|
5628
|
+
bootstrapServiceIdempotentOrThrow("\u542F\u52A8\u670D\u52A1");
|
|
5522
5629
|
});
|
|
5523
5630
|
return { started };
|
|
5524
5631
|
}
|
|
@@ -5632,7 +5739,7 @@ async function tryHotReload() {
|
|
|
5632
5739
|
body: "{}",
|
|
5633
5740
|
signal: controller.signal
|
|
5634
5741
|
});
|
|
5635
|
-
return res.
|
|
5742
|
+
return res.ok;
|
|
5636
5743
|
} catch {
|
|
5637
5744
|
return false;
|
|
5638
5745
|
} finally {
|
|
@@ -5665,8 +5772,8 @@ async function restartService(stopEpochBefore) {
|
|
|
5665
5772
|
started = false;
|
|
5666
5773
|
return;
|
|
5667
5774
|
}
|
|
5668
|
-
runLaunchctl(["enable", serviceTarget()]);
|
|
5669
|
-
|
|
5775
|
+
runLaunchctl(["enable", serviceTarget()], SERVICE_LOCK_LAUNCHCTL_TIMEOUT_MS);
|
|
5776
|
+
bootstrapServiceIdempotentOrThrow("\u91CD\u542F\u670D\u52A1");
|
|
5670
5777
|
});
|
|
5671
5778
|
if (!started) return { hotReloaded: false, started: false };
|
|
5672
5779
|
}
|
|
@@ -5802,15 +5909,22 @@ function walk(node, withinProvider, state) {
|
|
|
5802
5909
|
if (node !== null && typeof node === "object") {
|
|
5803
5910
|
const out = {};
|
|
5804
5911
|
for (const [key, value] of Object.entries(node)) {
|
|
5912
|
+
const assign = (v) => {
|
|
5913
|
+
if (key === "__proto__") {
|
|
5914
|
+
Object.defineProperty(out, key, { value: v, enumerable: true, writable: true, configurable: true });
|
|
5915
|
+
} else {
|
|
5916
|
+
out[key] = v;
|
|
5917
|
+
}
|
|
5918
|
+
};
|
|
5805
5919
|
if (typeof value === "string" && SECRET_KEYS.has(key.toLowerCase())) {
|
|
5806
|
-
|
|
5920
|
+
assign("***");
|
|
5807
5921
|
state.changed = true;
|
|
5808
5922
|
} else if (typeof value === "string" && withinProvider && key === "url") {
|
|
5809
5923
|
const masked = maskUrl(value);
|
|
5810
|
-
|
|
5924
|
+
assign(masked);
|
|
5811
5925
|
if (masked !== value) state.changed = true;
|
|
5812
5926
|
} else {
|
|
5813
|
-
|
|
5927
|
+
assign(walk(value, withinProvider || URL_PROVIDER_KEYS.has(key), state));
|
|
5814
5928
|
}
|
|
5815
5929
|
}
|
|
5816
5930
|
return out;
|
|
@@ -6095,7 +6209,20 @@ async function downloadSubscription(url, subName = "default", signal) {
|
|
|
6095
6209
|
assertLooksLikeSubscription(parsed, maskUrl(url));
|
|
6096
6210
|
saveSubscriptionRawConfig(subName, content);
|
|
6097
6211
|
const meta = extractSubscriptionMeta(response.headers);
|
|
6098
|
-
|
|
6212
|
+
try {
|
|
6213
|
+
saveSubscriptionMeta(subName, meta);
|
|
6214
|
+
} catch (e) {
|
|
6215
|
+
try {
|
|
6216
|
+
removeSubscriptionRawConfig(subName);
|
|
6217
|
+
} catch {
|
|
6218
|
+
}
|
|
6219
|
+
throw new CliError(`\u8BA2\u9605\u7F13\u5B58\u5199\u5165\u5931\u8D25: ${e.message}`, {
|
|
6220
|
+
label: "\u66F4\u65B0\u5931\u8D25",
|
|
6221
|
+
// 不给命令级指引:add 与 update 两个消费方都会走到这里,而 add 失败后条目已
|
|
6222
|
+
// 回滚删除,照「sub update」执行只会得到「没有订阅」——hint 只陈述已做的事
|
|
6223
|
+
hint: ["\u8BA2\u9605\u6587\u4EF6\u5DF2\u968F\u5931\u8D25\u56DE\u6EDA\u5220\u9664", "\u82E5 cache.json \u88AB\u624B\u5DE5\u6539\u52A8\uFF0C\u4FEE\u590D\u540E\u91CD\u8BD5"]
|
|
6224
|
+
});
|
|
6225
|
+
}
|
|
6099
6226
|
const proxies = parsed.proxies;
|
|
6100
6227
|
const proxyGroups = parsed["proxy-groups"];
|
|
6101
6228
|
return {
|
|
@@ -6358,13 +6485,18 @@ exit 2
|
|
|
6358
6485
|
}
|
|
6359
6486
|
async function startTun() {
|
|
6360
6487
|
ensureDirs();
|
|
6361
|
-
rotateAndCleanupLogs();
|
|
6362
6488
|
if (!fs9.existsSync(PATHS.mihomoBinary)) {
|
|
6363
6489
|
throw new CliError("\u672A\u627E\u5230 mihomo \u5185\u6838\uFF0C\u8BF7\u5148\u4E0B\u8F7D\u5185\u6838", { hint: "\u4E0B\u8F7D\u5185\u6838: mihomo kernel" });
|
|
6364
6490
|
}
|
|
6365
6491
|
if (!fs9.existsSync(PATHS.configFile)) {
|
|
6366
6492
|
throw new CliError("\u672A\u627E\u5230\u914D\u7F6E\u6587\u4EF6\uFF0C\u8BF7\u5148\u6DFB\u52A0\u8BA2\u9605\u5E76\u542F\u52A8");
|
|
6367
6493
|
}
|
|
6494
|
+
if (getServiceStatus().loaded) {
|
|
6495
|
+
throw new CliError("\u53E6\u4E00\u7EC8\u7AEF\u5DF2\u542F\u52A8 Mixed \u670D\u52A1\uFF0CTUN \u672A\u542F\u52A8", {
|
|
6496
|
+
hint: ["\u4E24\u8005\u4F1A\u62A2\u5360\u540C\u4E00\u7EC4\u7AEF\u53E3\u4E0E\u914D\u7F6E\u3002\u8BF7\u5148\u505C\u6B62\u670D\u52A1:", " mihomo stop", "", "\u4E4B\u540E\u53EF\u91CD\u8BD5 TUN: mihomo start tun"]
|
|
6497
|
+
});
|
|
6498
|
+
}
|
|
6499
|
+
rotateAndCleanupLogs();
|
|
6368
6500
|
const logFile = PATHS.logFile;
|
|
6369
6501
|
if (fs9.existsSync(logFile)) {
|
|
6370
6502
|
try {
|
|
@@ -6564,20 +6696,7 @@ async function probeProxyConnectivity(port) {
|
|
|
6564
6696
|
try {
|
|
6565
6697
|
const { stdout } = await execFileAsync(
|
|
6566
6698
|
"curl",
|
|
6567
|
-
[
|
|
6568
|
-
"-x",
|
|
6569
|
-
`http://127.0.0.1:${port}`,
|
|
6570
|
-
"-s",
|
|
6571
|
-
"-o",
|
|
6572
|
-
"/dev/null",
|
|
6573
|
-
"-w",
|
|
6574
|
-
"%{http_code}",
|
|
6575
|
-
"--connect-timeout",
|
|
6576
|
-
"3",
|
|
6577
|
-
"--max-time",
|
|
6578
|
-
String(Math.ceil(PROBE_TIMEOUT_MS / 1e3)),
|
|
6579
|
-
PROBE_URL
|
|
6580
|
-
],
|
|
6699
|
+
["-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],
|
|
6581
6700
|
{ timeout: PROBE_TIMEOUT_MS + 2e3 }
|
|
6582
6701
|
);
|
|
6583
6702
|
const code = Number.parseInt(stdout.trim(), 10);
|
|
@@ -6884,6 +7003,8 @@ async function cmdStart(args) {
|
|
|
6884
7003
|
console.log(colors.gray("\u5DF2\u4E34\u65F6\u5173\u95ED\u670D\u52A1\u81EA\u542F\uFF08\u907F\u514D\u91CD\u542F\u540E\u670D\u52A1\u62FF TUN \u914D\u7F6E\u542F\u52A8\uFF09"));
|
|
6885
7004
|
console.log(colors.gray("TUN \u7528\u5B8C\u540E mihomo start \u53EF\u6062\u590D"));
|
|
6886
7005
|
console.log("");
|
|
7006
|
+
} else {
|
|
7007
|
+
recordServiceStopped();
|
|
6887
7008
|
}
|
|
6888
7009
|
} else if (!serviceBefore.installed) {
|
|
6889
7010
|
if (serviceBefore.loaded) {
|
|
@@ -7168,11 +7289,12 @@ function buildGhApiReleaseArgs(repo) {
|
|
|
7168
7289
|
return ["api", `repos/${repo}/releases`, "--method", "GET"];
|
|
7169
7290
|
}
|
|
7170
7291
|
var GH_API_TIMEOUT = 1e4;
|
|
7171
|
-
async function getLatestReleaseViaGh(repo) {
|
|
7292
|
+
async function getLatestReleaseViaGh(repo, signal) {
|
|
7172
7293
|
const result = await execFileAsync2("gh", buildGhApiReleaseArgs(repo), {
|
|
7173
7294
|
encoding: "utf8",
|
|
7174
7295
|
maxBuffer: 50 * 1024 * 1024,
|
|
7175
|
-
timeout: GH_API_TIMEOUT
|
|
7296
|
+
timeout: GH_API_TIMEOUT,
|
|
7297
|
+
signal
|
|
7176
7298
|
});
|
|
7177
7299
|
return pickLatestRelease(JSON.parse(result.stdout));
|
|
7178
7300
|
}
|
|
@@ -7180,8 +7302,9 @@ async function getLatestRelease(repo, opts = { proxy: null, useGh: false }) {
|
|
|
7180
7302
|
const url = `https://api.github.com/repos/${repo}/releases`;
|
|
7181
7303
|
if (opts.useGh) {
|
|
7182
7304
|
try {
|
|
7183
|
-
return await getLatestReleaseViaGh(repo);
|
|
7184
|
-
} catch {
|
|
7305
|
+
return await getLatestReleaseViaGh(repo, opts.signal);
|
|
7306
|
+
} catch (e) {
|
|
7307
|
+
if (opts.signal?.aborted) throw e;
|
|
7185
7308
|
}
|
|
7186
7309
|
}
|
|
7187
7310
|
if (opts.proxy) {
|
|
@@ -7190,7 +7313,8 @@ async function getLatestRelease(repo, opts = { proxy: null, useGh: false }) {
|
|
|
7190
7313
|
const result = await execFileAsync2("curl", buildReleaseApiCurlArgs(opts.proxy, url), {
|
|
7191
7314
|
encoding: "utf8",
|
|
7192
7315
|
maxBuffer: 50 * 1024 * 1024,
|
|
7193
|
-
timeout: KERNEL_HTTP_TIMEOUT + 1e4
|
|
7316
|
+
timeout: KERNEL_HTTP_TIMEOUT + 1e4,
|
|
7317
|
+
signal: opts.signal
|
|
7194
7318
|
});
|
|
7195
7319
|
stdout = result.stdout;
|
|
7196
7320
|
} catch (e) {
|
|
@@ -7208,7 +7332,7 @@ async function getLatestRelease(repo, opts = { proxy: null, useGh: false }) {
|
|
|
7208
7332
|
}
|
|
7209
7333
|
return pickLatestRelease(releases);
|
|
7210
7334
|
}
|
|
7211
|
-
const response = await HTTP_CLIENT2.get(url, { responseType: "json" });
|
|
7335
|
+
const response = await HTTP_CLIENT2.get(url, { responseType: "json", signal: opts.signal });
|
|
7212
7336
|
return pickLatestRelease(response.data);
|
|
7213
7337
|
}
|
|
7214
7338
|
async function checkUpdate(opts) {
|
|
@@ -7462,19 +7586,9 @@ async function cmdUpdate(args = []) {
|
|
|
7462
7586
|
|
|
7463
7587
|
// src/commands/doctor.ts
|
|
7464
7588
|
function withTimeout2(promise, ms) {
|
|
7465
|
-
|
|
7466
|
-
|
|
7467
|
-
|
|
7468
|
-
(v) => {
|
|
7469
|
-
clearTimeout(timer);
|
|
7470
|
-
resolve(v);
|
|
7471
|
-
},
|
|
7472
|
-
(e) => {
|
|
7473
|
-
clearTimeout(timer);
|
|
7474
|
-
reject(e);
|
|
7475
|
-
}
|
|
7476
|
-
);
|
|
7477
|
-
});
|
|
7589
|
+
const controller = new AbortController();
|
|
7590
|
+
const timer = setTimeout(() => controller.abort(), ms);
|
|
7591
|
+
return promise(controller.signal).finally(() => clearTimeout(timer));
|
|
7478
7592
|
}
|
|
7479
7593
|
function isPortListening(port) {
|
|
7480
7594
|
try {
|
|
@@ -7498,15 +7612,13 @@ async function collectChecks() {
|
|
|
7498
7612
|
kernelProxyPort = null;
|
|
7499
7613
|
}
|
|
7500
7614
|
const kernelVersionPromise = hasKernel() ? withTimeout2(
|
|
7501
|
-
checkUpdate({
|
|
7615
|
+
(signal) => checkUpdate({
|
|
7502
7616
|
proxy: kernelProxyPort !== null ? `http://127.0.0.1:${kernelProxyPort}` : null,
|
|
7503
|
-
useGh: kernelProxyPort === null && hasGh()
|
|
7617
|
+
useGh: kernelProxyPort === null && hasGh(),
|
|
7618
|
+
signal
|
|
7504
7619
|
}),
|
|
7505
7620
|
4e3
|
|
7506
|
-
).
|
|
7507
|
-
(v) => v,
|
|
7508
|
-
() => null
|
|
7509
|
-
) : Promise.resolve(null);
|
|
7621
|
+
).catch(() => null) : Promise.resolve(null);
|
|
7510
7622
|
if (!hasKernel()) {
|
|
7511
7623
|
push("\u5185\u6838", "fail", "\u672A\u5B89\u88C5", "mihomo kernel");
|
|
7512
7624
|
} else {
|
|
@@ -7693,7 +7805,14 @@ async function cmdKernel(args) {
|
|
|
7693
7805
|
});
|
|
7694
7806
|
}
|
|
7695
7807
|
const proxyRunning = getRunningState().running;
|
|
7696
|
-
|
|
7808
|
+
let proxyPort = null;
|
|
7809
|
+
if (proxyRunning) {
|
|
7810
|
+
try {
|
|
7811
|
+
proxyPort = getPorts().mixed;
|
|
7812
|
+
} catch {
|
|
7813
|
+
proxyPort = null;
|
|
7814
|
+
}
|
|
7815
|
+
}
|
|
7697
7816
|
const forceDirect = mirrorInfo.isOverride && !mirrorInfo.mirror;
|
|
7698
7817
|
const ghAvailable = hasGh();
|
|
7699
7818
|
const channel = resolveDownloadChannel({
|
|
@@ -7754,6 +7873,18 @@ async function cmdKernel(args) {
|
|
|
7754
7873
|
" mihomo kernel --proxy <\u7AEF\u53E3> # \u7ECF\u672C\u673A\u5176\u4ED6\u4EE3\u7406\u5DE5\u5177\u51FA\u7F51"
|
|
7755
7874
|
);
|
|
7756
7875
|
}
|
|
7876
|
+
} else if (apiProxy) {
|
|
7877
|
+
hint.push("", "\u63D0\u793A: \u7248\u672C\u67E5\u8BE2\uFF08GitHub API\uFF09\u7ECF\u4EE3\u7406\u5931\u8D25\uFF0C\u53EF\u68C0\u67E5\u4EE3\u7406\u662F\u5426\u53EF\u7528\uFF1B\u955C\u50CF\u53EA\u4F5C\u7528\u4E8E\u5185\u6838\u4E0B\u8F7D\uFF0C\u4E0E\u67E5\u8BE2\u65E0\u5173");
|
|
7878
|
+
} else if (useGh) {
|
|
7879
|
+
hint.push("", "\u63D0\u793A: gh \u8BA4\u8BC1\u901A\u9053\u4E0E\u76F4\u8FDE\u90FD\u5931\u8D25\u4E86\uFF0C\u53EF\u68C0\u67E5 gh \u767B\u5F55\u72B6\u6001: gh auth status");
|
|
7880
|
+
} else {
|
|
7881
|
+
hint.push(
|
|
7882
|
+
"",
|
|
7883
|
+
"\u63D0\u793A: \u955C\u50CF\u53EA\u4F5C\u7528\u4E8E\u5185\u6838\u4E0B\u8F7D\uFF0C\u7248\u672C\u67E5\u8BE2\u4ECD\u9700\u76F4\u8FDE GitHub API\uFF08\u5F53\u524D\u4E0D\u901A\uFF09\u3002\u51FA\u8DEF:",
|
|
7884
|
+
" \u5B89\u88C5\u5E76\u767B\u5F55 GitHub CLI\uFF08\u8BA4\u8BC1\u914D\u989D 5000 \u6B21/\u65F6\uFF09:",
|
|
7885
|
+
" brew install gh && gh auth login",
|
|
7886
|
+
" \u6216\u7ECF\u672C\u673A\u4EE3\u7406\u5DE5\u5177\u67E5\u8BE2: mihomo kernel --proxy <\u7AEF\u53E3>"
|
|
7887
|
+
);
|
|
7757
7888
|
}
|
|
7758
7889
|
throw new CliError(err.message, { label: "\u66F4\u65B0\u5931\u8D25", hint });
|
|
7759
7890
|
}
|
|
@@ -7814,6 +7945,11 @@ function cmdLogs(args) {
|
|
|
7814
7945
|
logPath = archive.path;
|
|
7815
7946
|
}
|
|
7816
7947
|
if (openInViewer) {
|
|
7948
|
+
if (follow) {
|
|
7949
|
+
throw new CliError("-o\uFF08\u7CFB\u7EDF\u67E5\u770B\u5668\u6253\u5F00\uFF09\u4E0E -f\uFF08\u7EC8\u7AEF\u8DDF\u968F\uFF09\u4E92\u65A5", {
|
|
7950
|
+
hint: "\u8DDF\u968F\u8F93\u51FA\u8BF7\u53BB\u6389 -o: mihomo logs 0 -f"
|
|
7951
|
+
});
|
|
7952
|
+
}
|
|
7817
7953
|
openLogFile(logPath);
|
|
7818
7954
|
return;
|
|
7819
7955
|
}
|
|
@@ -7967,9 +8103,9 @@ function clearRuntime() {
|
|
|
7967
8103
|
function clearPid() {
|
|
7968
8104
|
if (!fs12.existsSync(PATHS.pidFile)) return;
|
|
7969
8105
|
if (isPidFileOwnedByRoot()) {
|
|
7970
|
-
|
|
7971
|
-
|
|
7972
|
-
|
|
8106
|
+
const result = spawnSync8("sudo", ["rm", "-f", PATHS.pidFile], { stdio: "inherit", timeout: SUDO_TIMEOUT_MS });
|
|
8107
|
+
if (result.error || result.status !== 0) {
|
|
8108
|
+
console.warn("\u8B66\u544A: root \u5C5E\u4E3B\u7684 pid \u6587\u4EF6\u672A\u80FD\u6E05\u7406\uFF08sudo \u5931\u8D25\u6216\u5DF2\u53D6\u6D88\uFF09\uFF0C\u4E0B\u6B21 stop \u4F1A\u518D\u6B21\u5C1D\u8BD5");
|
|
7973
8109
|
}
|
|
7974
8110
|
} else {
|
|
7975
8111
|
try {
|
|
@@ -8024,6 +8160,10 @@ async function cleanupAll(forceSudo = false) {
|
|
|
8024
8160
|
}
|
|
8025
8161
|
} else {
|
|
8026
8162
|
for (const pid of pids) {
|
|
8163
|
+
if (!isMihomoProcess(pid)) {
|
|
8164
|
+
killedCount++;
|
|
8165
|
+
continue;
|
|
8166
|
+
}
|
|
8027
8167
|
if (killProcess(pid)) {
|
|
8028
8168
|
killedCount++;
|
|
8029
8169
|
} else {
|
|
@@ -8054,7 +8194,7 @@ async function stop(forceSudo = false) {
|
|
|
8054
8194
|
console.log(`\u8FDB\u7A0B PID: ${remaining.join(", ")}`);
|
|
8055
8195
|
console.log("\u624B\u52A8\u547D\u4EE4: sudo pkill -9 mihomo");
|
|
8056
8196
|
console.log("");
|
|
8057
|
-
return { success: true,
|
|
8197
|
+
return { success: true, remaining };
|
|
8058
8198
|
}
|
|
8059
8199
|
clearRuntime();
|
|
8060
8200
|
return { success: true, killed: result.killed };
|
|
@@ -8075,8 +8215,11 @@ var RESET_TARGETS = [
|
|
|
8075
8215
|
paths: () => fs13.existsSync(USER_DATA_DIR) ? fs13.readdirSync(USER_DATA_DIR).filter(isOverwriteFilename).map((f) => `${USER_DATA_DIR}/${f}`) : []
|
|
8076
8216
|
},
|
|
8077
8217
|
{
|
|
8218
|
+
// 别名刻意不含 'config':用户从 `mihomo config` 命令得到的直觉是「运行配置」
|
|
8219
|
+
// (那属于 runtime 目标),而这里删的是 settings(订阅列表/端口/密钥)——
|
|
8220
|
+
// 静默对撞会让 reset config -y 删超预期的数据。未知目标报错 + 目标列表兜底
|
|
8078
8221
|
id: "settings",
|
|
8079
|
-
aliases: ["setting", "settings"
|
|
8222
|
+
aliases: ["setting", "settings"],
|
|
8080
8223
|
label: "\u8BBE\u7F6E",
|
|
8081
8224
|
needsStop: false,
|
|
8082
8225
|
preserveOnBare: true,
|
|
@@ -8100,6 +8243,11 @@ function resolveResetTargets(names) {
|
|
|
8100
8243
|
async function cmdReset(args) {
|
|
8101
8244
|
assertKnownFlags(args, ["--full", "--yes", "-y"], "reset [\u76EE\u6807...] [--full] [-y]");
|
|
8102
8245
|
const names = args.slice(1).filter((a) => !a.startsWith("-"));
|
|
8246
|
+
if (names.length > 0 && args.includes("--full")) {
|
|
8247
|
+
throw new CliError("\u4E0D\u80FD\u540C\u65F6\u6307\u5B9A\u91CD\u7F6E\u76EE\u6807\u4E0E --full", {
|
|
8248
|
+
hint: [`\u53EA\u5220\u6307\u5B9A\u76EE\u6807: mihomo reset ${names.join(" ")}`, "\u91CD\u7F6E\u5168\u90E8: mihomo reset --full"]
|
|
8249
|
+
});
|
|
8250
|
+
}
|
|
8103
8251
|
const namedTargets = resolveResetTargets(names);
|
|
8104
8252
|
const targets = args.includes("--full") ? RESET_TARGETS : names.length > 0 ? namedTargets : RESET_TARGETS.filter((t) => !t.preserveOnBare);
|
|
8105
8253
|
const ids = new Set(targets.map((t) => t.id));
|
|
@@ -8325,11 +8473,12 @@ async function cmdStop(args) {
|
|
|
8325
8473
|
|
|
8326
8474
|
// src/commands/subscription.ts
|
|
8327
8475
|
import { spawnSync as spawnSync9 } from "child_process";
|
|
8328
|
-
function printRestartHintIfRunning() {
|
|
8476
|
+
function printRestartHintIfRunning(variant = "update") {
|
|
8329
8477
|
const state = getRunningState();
|
|
8330
8478
|
if (state.running) {
|
|
8331
8479
|
const hintCommand = state.kind === "tun" ? "mihomo start tun" : "mihomo start";
|
|
8332
|
-
|
|
8480
|
+
const message = variant === "removed-active" ? `\u63D0\u793A: \u8FD0\u884C\u4E2D\u7684\u5B9E\u4F8B\u4ECD\u5728\u4F7F\u7528\u5DF2\u5220\u9664\u8BA2\u9605\u7684\u914D\u7F6E\uFF0C\u6267\u884C ${hintCommand} \u5207\u6362\u5230\u65B0\u8BA2\u9605` : `\u63D0\u793A: \u8FD0\u884C\u4E2D\u7684\u5B9E\u4F8B\u4ECD\u4F7F\u7528\u65E7\u914D\u7F6E\uFF0C\u6267\u884C ${hintCommand} \u4F7F\u66F4\u65B0\u751F\u6548`;
|
|
8481
|
+
console.log(colors.yellow(message));
|
|
8333
8482
|
console.log("");
|
|
8334
8483
|
}
|
|
8335
8484
|
}
|
|
@@ -8397,7 +8546,10 @@ function readUrlFromClipboard() {
|
|
|
8397
8546
|
async function subAdd(args) {
|
|
8398
8547
|
assertPositionalCount(args, 2, 2, "mihomo sub add <url> [name]");
|
|
8399
8548
|
let url = args[2]?.trim();
|
|
8400
|
-
const name = args[3]
|
|
8549
|
+
const name = args[3] === void 0 ? "default" : args[3];
|
|
8550
|
+
if (!name.trim()) {
|
|
8551
|
+
throw new CliError("\u8BA2\u9605\u540D\u4E0D\u80FD\u4E3A\u7A7A", { hint: "\u4E0D\u6307\u5B9A\u540D\u79F0\u65F6\u7701\u7565\u8BE5\u53C2\u6570\u5373\u53EF\uFF08\u9ED8\u8BA4\u540D default\uFF09" });
|
|
8552
|
+
}
|
|
8401
8553
|
if (!url) {
|
|
8402
8554
|
const clipped = readUrlFromClipboard();
|
|
8403
8555
|
if (clipped) {
|
|
@@ -8536,6 +8688,7 @@ async function subRemove(args) {
|
|
|
8536
8688
|
console.log(`\u5DF2\u5220\u9664\u8BA2\u9605 "${target.name}"`);
|
|
8537
8689
|
if (switchedTo) {
|
|
8538
8690
|
console.log(`\u5DF2\u81EA\u52A8\u5207\u6362\u5230 "${switchedTo}"`);
|
|
8691
|
+
printRestartHintIfRunning("removed-active");
|
|
8539
8692
|
}
|
|
8540
8693
|
console.log("");
|
|
8541
8694
|
printSubscriptionList();
|
|
@@ -8601,10 +8754,15 @@ function cmdUI(args) {
|
|
|
8601
8754
|
console.log("");
|
|
8602
8755
|
}
|
|
8603
8756
|
const url = UI_URLS[uiName];
|
|
8604
|
-
|
|
8757
|
+
let controllerPort = null;
|
|
8758
|
+
try {
|
|
8759
|
+
controllerPort = getPorts().controller;
|
|
8760
|
+
} catch {
|
|
8761
|
+
controllerPort = null;
|
|
8762
|
+
}
|
|
8605
8763
|
console.log(`\u6253\u5F00 Web UI: ${uiName}`);
|
|
8606
8764
|
console.log(`\u9875\u9762: ${url}`);
|
|
8607
|
-
console.log(`\u63A7\u5236\u5668: http://127.0.0.1:${controllerPort}`);
|
|
8765
|
+
console.log(`\u63A7\u5236\u5668: http://127.0.0.1:${controllerPort ?? "\uFF08settings.json \u7684 ports \u914D\u7F6E\u975E\u6CD5\uFF0C\u65E0\u6CD5\u786E\u5B9A\uFF09"}`);
|
|
8608
8766
|
const secret = readSettings().controller_secret;
|
|
8609
8767
|
if (typeof secret === "string" && secret) {
|
|
8610
8768
|
if (copySecret) {
|
|
@@ -8886,6 +9044,7 @@ async function main() {
|
|
|
8886
9044
|
assertSupportedPlatform("status");
|
|
8887
9045
|
assertNotRoot("status");
|
|
8888
9046
|
ensureDirs();
|
|
9047
|
+
cleanupStaleTmpFiles();
|
|
8889
9048
|
await printStatus();
|
|
8890
9049
|
printShortHelp();
|
|
8891
9050
|
return;
|
|
@@ -8903,6 +9062,7 @@ async function main() {
|
|
|
8903
9062
|
assertNotRoot(command.name);
|
|
8904
9063
|
if (!GUARD_EXEMPT_COMMANDS.has(command.name)) {
|
|
8905
9064
|
ensureDirs();
|
|
9065
|
+
cleanupStaleTmpFiles();
|
|
8906
9066
|
}
|
|
8907
9067
|
if (command.group === "meta") {
|
|
8908
9068
|
assertKnownFlags(args.slice(1), [], command.name);
|