mihomo-cli 4.2.0 → 4.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # Changelog
2
2
 
3
+ ## [4.2.1] - 2026-09-05
4
+
5
+ 修一个让 `stop` 完全失效的高危缺陷。实测复现并回归验证。
6
+
7
+ ### 修复
8
+
9
+ - **`mihomo stop` 停不掉内核,却打印「不在运行」并退出 0**。内核照常在跑、代理照常生效,只是自启被关掉了——下次开机才「看起来正常」。同一个缺陷还让 `status` 把运行中的内核报成未运行、`start` 看不见需要清理的残留、`reset` 漏掉进程清理。
10
+
11
+ 根因在识别内核进程的正则:它用了 JS 的非捕获组 `(?:a|b)`,而 `pgrep -f` / `pkill -f` 走 POSIX ERE(`regcomp(REG_EXTENDED)`),ERE 里 `(` 后紧跟 `?` 是语法错误。实测 `pgrep` 直接报 `Cannot compile regular expression ... (repetition-operator operand invalid)` 并以退出码 2 结束、不输出任何 PID,而 `getMihomoPids()` 把这个失败吞成了「没有进程」。`pkill` 同样编译失败,一个进程都不杀却照常返回。于是整条停止链路在「什么都没做」的情况下报告成功。
12
+
13
+ 该分支是 v4.2.0 引入的(服务经符号链 `mihomo-cli-service` 启动,需同时匹配符号链与真实二进制两种命令行),此前的 pattern 无分组,不受影响。
14
+
15
+ 除改用 ERE 的 `(a|b)` 外,还堵上了让它能潜伏下来的那个洞:`getMihomoPids` 现在只接受 `pgrep` 的退出码 0(有匹配)与 1(无匹配),其余一律报错——**探测失败不能再伪装成「没有进程」**。回归测试直接调用真实 `pgrep` 编译该 pattern,任何 JS-only 的正则语法都会被当场拦下。
16
+
3
17
  ## [4.2.0] - 2026-09-05
4
18
 
5
19
  修两个「报告成功但其实没做到」的高危缺陷,均实测复现并回归验证。
package/dist/index.js CHANGED
@@ -4570,7 +4570,7 @@ function isProcessRoot(pid) {
4570
4570
  return false;
4571
4571
  }
4572
4572
  }
4573
- var BINARY_ALTERNATION = `(?:${escapeRegExp(PATHS.serviceBinary)}|${escapeRegExp(PATHS.mihomoBinary)})`;
4573
+ var BINARY_ALTERNATION = `(${escapeRegExp(PATHS.serviceBinary)}|${escapeRegExp(PATHS.mihomoBinary)})`;
4574
4574
  var MAIN_INSTANCE_PATTERN = `${BINARY_ALTERNATION}.*${escapeRegExp(PATHS.configFile)}`;
4575
4575
  function getPid() {
4576
4576
  if (!fs6.existsSync(PATHS.pidFile)) return null;
@@ -4587,14 +4587,20 @@ function isRunning() {
4587
4587
  return isProcessRunning(pid) && (isProcessCommandMatching(pid, PATHS.mihomoBinary) || isProcessCommandMatching(pid, PATHS.serviceBinary));
4588
4588
  }
4589
4589
  function getMihomoPids() {
4590
- try {
4591
- const result = spawnSync2("pgrep", ["-f", MAIN_INSTANCE_PATTERN], { encoding: "utf8", timeout: 1e4 });
4592
- const output = (result.stdout || "").trim();
4593
- if (!output) return [];
4594
- return output.split("\n").filter(Boolean).map((p) => parseInt(p, 10)).filter((p) => Number.isInteger(p) && p > 0);
4595
- } catch {
4596
- return [];
4590
+ const result = spawnSync2("pgrep", ["-f", MAIN_INSTANCE_PATTERN], { encoding: "utf8", timeout: 1e4 });
4591
+ if (result.error || result.status === null) {
4592
+ throw new CliError("\u65E0\u6CD5\u63A2\u6D4B mihomo \u8FDB\u7A0B\uFF08pgrep \u6267\u884C\u5931\u8D25\uFF09", {
4593
+ hint: ["\u8FD9\u4E0D\u4EE3\u8868\u5185\u6838\u672A\u8FD0\u884C\uFF0C\u53EA\u8868\u793A\u67E5\u4E0D\u5230\u3002", "\u8BF7\u624B\u52A8\u786E\u8BA4: pgrep -fl mihomo"]
4594
+ });
4595
+ }
4596
+ if (result.status !== 0 && result.status !== 1) {
4597
+ throw new CliError(`\u8FDB\u7A0B\u63A2\u6D4B\u5931\u8D25\uFF08pgrep \u9000\u51FA\u7801 ${result.status}\uFF09`, {
4598
+ hint: [(result.stderr || "").trim(), "", "\u8FD9\u662F CLI \u7684\u7F3A\u9677\uFF0C\u8BF7\u53CD\u9988\u3002\u624B\u52A8\u786E\u8BA4\u5185\u6838\u72B6\u6001: pgrep -fl mihomo"].filter(Boolean)
4599
+ });
4597
4600
  }
4601
+ const output = (result.stdout || "").trim();
4602
+ if (!output) return [];
4603
+ return output.split("\n").filter(Boolean).map((p) => parseInt(p, 10)).filter((p) => Number.isInteger(p) && p > 0);
4598
4604
  }
4599
4605
  function isPidFileOwnedByRoot() {
4600
4606
  if (!fs6.existsSync(PATHS.pidFile)) return false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mihomo-cli",
3
- "version": "4.2.0",
3
+ "version": "4.2.1",
4
4
  "type": "module",
5
5
  "description": "A terminal-based mihomo (Clash.Meta) client for macOS",
6
6
  "bin": {