mihomo-cli 26.10.97 → 26.10.99

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.
Files changed (4) hide show
  1. package/CHANGELOG.md +47 -1
  2. package/README.md +121 -116
  3. package/dist/index.js +464 -251
  4. package/package.json +1 -3
package/dist/index.js CHANGED
@@ -277,10 +277,13 @@ function assertKnownFlags(args, known, command) {
277
277
  if (match && match.form !== "exact" && knownSet.has(match.baseForm)) continue;
278
278
  if (a.startsWith("--mirror=") && knownSet.has("--mirror")) continue;
279
279
  const isHelpFlag = a === "-h" || a === "--help";
280
- const helpNote = isHelpFlag ? ["", `${a} \u53EA\u5728\u9876\u5C42\u53EF\u7528\uFF0C\u547D\u4EE4\u7528\u6CD5\u89C1: mihomo help`] : [];
280
+ const helpNote = isHelpFlag ? ["", `${a} \u53EA\u5728\u9876\u5C42\u53EF\u7528\uFF0C\u547D\u4EE4\u7528\u6CD5\u89C1: mihomo-cli help`] : [];
281
+ const hasValueFlag = known.some((f) => VALUE_FLAGS.has(f));
282
+ const looksLikeName = a.length > 2 && !a.startsWith("--") && !hasValueFlag;
283
+ 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 \`-\``] : [];
281
284
  throw new CliError(`\u672A\u77E5\u7684\u9009\u9879: ${a}`, {
282
285
  label: "\u53C2\u6570\u9519\u8BEF",
283
- hint: [known.length > 0 ? `\u53EF\u7528\u9009\u9879: ${known.join(", ")}` : "\u8BE5\u547D\u4EE4\u4E0D\u63A5\u53D7\u4EFB\u4F55\u9009\u9879", ...helpNote, "", `\u7528\u6CD5: mihomo ${command}`]
286
+ hint: [known.length > 0 ? `\u53EF\u7528\u9009\u9879: ${known.join(", ")}` : "\u8BE5\u547D\u4EE4\u4E0D\u63A5\u53D7\u4EFB\u4F55\u9009\u9879", ...helpNote, ...nameNote, "", `\u7528\u6CD5: mihomo-cli ${command}`]
284
287
  });
285
288
  }
286
289
  }
@@ -3445,6 +3448,8 @@ var DIRS = {
3445
3448
  data: path.join(USER_DATA_DIR, "data"),
3446
3449
  runtime: path.join(USER_DATA_DIR, "runtime")
3447
3450
  };
3451
+ var KERNEL_SELF_BACKUP_DIR = path.join(DIRS.kernel, "meta-backup");
3452
+ var KERNEL_SELF_UPDATE_DIR = path.join(DIRS.kernel, "meta-update");
3448
3453
  var PATHS = {
3449
3454
  mihomoBinary: path.join(DIRS.kernel, "mihomo"),
3450
3455
  /**
@@ -3506,7 +3511,7 @@ var DIRECTORY_TARGETS = {
3506
3511
  root: { path: null, label: "\u6839\u76EE\u5F55" },
3507
3512
  subs: { path: DIRS.subscriptions, label: "\u8BA2\u9605\u76EE\u5F55" },
3508
3513
  logs: { path: DIRS.logs, label: "\u65E5\u5FD7\u76EE\u5F55" },
3509
- data: { path: DIRS.data, label: "mihomo \u6570\u636E\u76EE\u5F55" },
3514
+ data: { path: DIRS.data, label: "mihomo-cli \u6570\u636E\u76EE\u5F55" },
3510
3515
  runtime: { path: DIRS.runtime, label: "\u8FD0\u884C\u65F6\u76EE\u5F55" },
3511
3516
  kernel: { path: DIRS.kernel, label: "\u5185\u6838\u76EE\u5F55" }
3512
3517
  };
@@ -3716,7 +3721,7 @@ function getPorts(settings = readSettings()) {
3716
3721
  function maskSingleUrl(url) {
3717
3722
  try {
3718
3723
  const parsed = new URL(url);
3719
- const tokenKeys = /* @__PURE__ */ new Set([
3724
+ const TOKEN_KEY_NAMES = [
3720
3725
  "token",
3721
3726
  "key",
3722
3727
  "secret",
@@ -3733,9 +3738,12 @@ function maskSingleUrl(url) {
3733
3738
  "email",
3734
3739
  "passwd",
3735
3740
  "apikey",
3736
- "api_key",
3737
3741
  "access"
3738
- ]);
3742
+ ];
3743
+ if (new Set(TOKEN_KEY_NAMES).size !== TOKEN_KEY_NAMES.length) {
3744
+ throw new Error("tokenKeys \u5B58\u5728\u91CD\u590D\u767B\u8BB0");
3745
+ }
3746
+ const tokenKeys = new Set(TOKEN_KEY_NAMES);
3739
3747
  for (const [key, value] of parsed.searchParams) {
3740
3748
  if (tokenKeys.has(key.toLowerCase()) || value.length >= 16) {
3741
3749
  parsed.searchParams.set(key, "***");
@@ -3824,7 +3832,7 @@ function getSubscriptions(settings = readSettings()) {
3824
3832
  const list = settings.subscriptions;
3825
3833
  if (!Array.isArray(list)) {
3826
3834
  if (list !== void 0) {
3827
- console.warn("\u8B66\u544A: settings.json \u7684 subscriptions \u4E0D\u662F\u5217\u8868\uFF0C\u5DF2\u5FFD\u7565\uFF08\u53EF\u7528 mihomo sub add \u91CD\u65B0\u6DFB\u52A0\uFF09");
3835
+ console.warn("\u8B66\u544A: settings.json \u7684 subscriptions \u4E0D\u662F\u5217\u8868\uFF0C\u5DF2\u5FFD\u7565\uFF08\u53EF\u7528 mihomo-cli sub add \u91CD\u65B0\u6DFB\u52A0\uFF09");
3828
3836
  }
3829
3837
  return [];
3830
3838
  }
@@ -3849,7 +3857,7 @@ function addSubscription(url, name = "default") {
3849
3857
  updateSettings((settings) => {
3850
3858
  const subs = getSubscriptions(settings);
3851
3859
  if (subs.some((s) => s.name === name)) {
3852
- throw new CliError(`\u8BA2\u9605 "${name}" \u5DF2\u5B58\u5728\uFF0C\u8BF7\u6362\u4E2A\u540D\u79F0\uFF08mihomo sub add <url> <\u540D\u79F0>\uFF09\uFF0C\u6216\u5148\u5220\u9664\uFF08mihomo sub remove ${name}\uFF09`);
3860
+ throw new CliError(`\u8BA2\u9605 "${name}" \u5DF2\u5B58\u5728\uFF0C\u8BF7\u6362\u4E2A\u540D\u79F0\uFF08mihomo-cli sub add <url> <\u540D\u79F0>\uFF09\uFF0C\u6216\u5148\u5220\u9664\uFF08mihomo-cli sub remove ${name}\uFF09`);
3853
3861
  }
3854
3862
  subs.push({ name, url });
3855
3863
  const updates = { subscriptions: subs };
@@ -3944,7 +3952,7 @@ function assertValidParsedKey(rawKey, parsed) {
3944
3952
  if (rawKey.includes("<") || rawKey.includes(">")) {
3945
3953
  throw new CliError(`\u8986\u5199\u952E "${rawKey}" \u7528\u4E86\u5DF2\u79FB\u9664\u7684\u5C16\u62EC\u53F7\u8F6C\u4E49`, {
3946
3954
  label: "\u8986\u5199\u914D\u7F6E\u9519\u8BEF",
3947
- hint: ["<key> \u8F6C\u4E49\u5DF2\u968F ~ \u64CD\u4F5C\u7B26\u79FB\u9664\u2014\u2014mihomo \u9876\u5C42\u952E\u4E0D\u542B\u5C16\u62EC\u53F7\uFF0C\u76F4\u63A5\u5199\u952E\u540D\u5373\u53EF\u3002"]
3955
+ hint: ["<key> \u8F6C\u4E49\u5DF2\u968F ~ \u64CD\u4F5C\u7B26\u79FB\u9664\u2014\u2014mihomo-cli \u9876\u5C42\u952E\u4E0D\u542B\u5C16\u62EC\u53F7\uFF0C\u76F4\u63A5\u5199\u952E\u540D\u5373\u53EF\u3002"]
3948
3956
  });
3949
3957
  }
3950
3958
  if (parsed.key === "") {
@@ -3978,7 +3986,7 @@ function describeValueKind(value) {
3978
3986
  function protoKeyInOverwriteError() {
3979
3987
  return new CliError('\u8986\u5199\u91CC\u51FA\u73B0\u4E86 "__proto__" \u952E', {
3980
3988
  label: "\u8986\u5199\u914D\u7F6E\u9519\u8BEF",
3981
- 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"]
3989
+ hint: ["\u6B63\u5E38 mihomo-cli \u914D\u7F6E\u6CA1\u6709\u8FD9\u4E2A\u952E\uFF0C\u8BF7\u68C0\u67E5\u8986\u5199\u6587\u4EF6\u7684\u5185\u5BB9\u4E0E\u6765\u6E90\u3002"]
3982
3990
  });
3983
3991
  }
3984
3992
  function mergeConfigLevel(target, override, parseOperators) {
@@ -4277,7 +4285,7 @@ function toBrokenFile(file, filePath, e) {
4277
4285
  return { name: file, path: filePath, label: e.label, message: e.message, hint: e.hint };
4278
4286
  }
4279
4287
  const message = e.message || String(e);
4280
- const hint = ["\u8BE5\u6587\u4EF6\u5F53\u524D\u672A\u53C2\u4E0E\u5408\u5E76\uFF0C\u8BF7\u4FEE\u6B63\u540E\u91CD\u8BD5\uFF08mihomo ow \u53EF\u67E5\u770B\u5168\u90E8\u8986\u5199\u6587\u4EF6\uFF09\u3002"];
4288
+ const hint = ["\u8BE5\u6587\u4EF6\u5F53\u524D\u672A\u53C2\u4E0E\u5408\u5E76\uFF0C\u8BF7\u4FEE\u6B63\u540E\u91CD\u8BD5\uFF08mihomo-cli ow \u53EF\u67E5\u770B\u5168\u90E8\u8986\u5199\u6587\u4EF6\uFF09\u3002"];
4281
4289
  if (/alias/i.test(message)) {
4282
4290
  hint.push('\u82E5\u5199\u4E86\u4EE5 * \u5F00\u5934\u7684\u901A\u914D\u503C\uFF08\u5982 name: *edu\uFF09\uFF0CYAML \u4F1A\u628A\u5B83\u5F53\u522B\u540D\u8BED\u6CD5\uFF0C\u8BF7\u52A0\u5F15\u53F7\u5199\u6210 name: "*edu"');
4283
4291
  }
@@ -4394,7 +4402,7 @@ function applyOverwrite(baseConfig, files, opts) {
4394
4402
  } catch (e) {
4395
4403
  throw new CliError(`\u8986\u5199\u811A\u672C "${file.name}" \u6267\u884C\u5931\u8D25: ${e.message?.split("\n")[0] ?? String(e)}`, {
4396
4404
  label: "\u8986\u5199\u914D\u7F6E\u9519\u8BEF",
4397
- hint: ["\u672C\u6B21\u6784\u5EFA\u5DF2\u4E2D\u6B62\uFF1B\u4FEE\u590D\u811A\u672C\u540E\u91CD\u8BD5\uFF08mihomo ow \u53EF\u67E5\u770B\u5168\u90E8\u8986\u5199\u6587\u4EF6\u4E0E\u811A\u672C\uFF09\u3002"]
4405
+ 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"]
4398
4406
  });
4399
4407
  }
4400
4408
  if (returned != null && typeof returned.then === "function") {
@@ -4440,7 +4448,7 @@ function shellQuote(s) {
4440
4448
  }
4441
4449
  function sanitizeTerminal(s) {
4442
4450
  const ansiEscape = /\x1b\[[0-9;]*[a-zA-Z]/g;
4443
- const controlChars = /[\x00-\x08\x0b\x0c\x0e-\x1f]/g;
4451
+ const controlChars = /[\x00-\x08\x0b\x0c\x0d\x0e-\x1f]/g;
4444
4452
  return s.replace(ansiEscape, "").replace(controlChars, "");
4445
4453
  }
4446
4454
 
@@ -4452,16 +4460,16 @@ function loadYamlSafe(content) {
4452
4460
  function parseConfigContent(content, errorMsg) {
4453
4461
  const label = errorMsg || "\u5185\u5BB9";
4454
4462
  if (!content?.trim()) {
4455
- throw new Error(`${label}\u4E3A\u7A7A`);
4463
+ throw new CliError(`${label}\u4E3A\u7A7A`, { label: "\u914D\u7F6E\u9519\u8BEF" });
4456
4464
  }
4457
4465
  let result;
4458
4466
  try {
4459
4467
  result = loadYamlSafe(content);
4460
4468
  } catch (e) {
4461
- throw new Error(`${label}\u683C\u5F0F\u9519\u8BEF\uFF0C\u65E0\u6CD5\u89E3\u6790: ${e.message.split("\n")[0]}`);
4469
+ throw new CliError(`${label}\u683C\u5F0F\u9519\u8BEF\uFF0C\u65E0\u6CD5\u89E3\u6790: ${e.message.split("\n")[0]}`, { label: "\u914D\u7F6E\u9519\u8BEF" });
4462
4470
  }
4463
4471
  if (result == null || typeof result !== "object" || Array.isArray(result)) {
4464
- throw new Error(`${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`);
4472
+ 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" });
4465
4473
  }
4466
4474
  return result;
4467
4475
  }
@@ -4516,7 +4524,7 @@ function assertConfigShape(config) {
4516
4524
  if (typeof name !== "string" || name === "") {
4517
4525
  throw new CliError(`${key}[${i}] \u7F3A\u5C11\u6709\u6548\u7684 name`, {
4518
4526
  label: "\u914D\u7F6E\u9519\u8BEF",
4519
- hint: [`${label}\u6BB5\uFF08${key}\uFF09\u7B2C ${i + 1} \u9879\u6CA1\u6709 name \u5B57\u6BB5\uFF08\u6216\u4E3A\u7A7A\uFF09\uFF0Cmihomo \u4F1A\u62D2\u7EDD\u542F\u52A8\u3002`]
4527
+ hint: [`${label}\u6BB5\uFF08${key}\uFF09\u7B2C ${i + 1} \u9879\u6CA1\u6709 name \u5B57\u6BB5\uFF08\u6216\u4E3A\u7A7A\uFF09\uFF0Cmihomo-cli \u4F1A\u62D2\u7EDD\u542F\u52A8\u3002`]
4520
4528
  });
4521
4529
  }
4522
4530
  } else if (typeof item !== "string") {
@@ -4622,7 +4630,7 @@ function buildConfig(subRawContent, mode, scope) {
4622
4630
  return { config: merged, warnings: lockedWarnings, overwriteSummaries };
4623
4631
  }
4624
4632
  function renderLockedWarning(source, keys) {
4625
- return `${source}\u7CFB\u7EDF\u9501\u5B9A\u9879\u5DF2\u5FFD\u7565: ${keys.join("\u3001")}\uFF08\u5165\u7AD9\u7AEF\u53E3\u3001\u63A7\u5236\u9762\u3001\u63A7\u5236\u5668\u8BC1\u4E66\u4E0E\u5C40\u57DF\u7F51/\u5165\u7AD9\u9274\u6743\u7531 mihomo-cli \u7BA1\u7406\uFF1B\u7AEF\u53E3\u4E0E controller secret \u5728 settings.json \u914D\u7F6E\uFF0C\u5165\u7AD9\u56FA\u5B9A\u53EA\u76D1\u542C\u56DE\u73AF\uFF0C\u9700\u8981\u5C40\u57DF\u7F51\u5165\u7AD9\u8BF7\u5728\u672C\u673A\u53E6\u8D77\u4E00\u4E2A mihomo \u5B9E\u4F8B\uFF09`;
4633
+ return `${source}\u7CFB\u7EDF\u9501\u5B9A\u9879\u5DF2\u5FFD\u7565: ${keys.join("\u3001")}\uFF08\u5165\u7AD9\u7AEF\u53E3\u3001\u63A7\u5236\u9762\u3001\u63A7\u5236\u5668\u8BC1\u4E66\u4E0E\u5C40\u57DF\u7F51/\u5165\u7AD9\u9274\u6743\u7531 mihomo-cli \u7BA1\u7406\uFF1B\u7AEF\u53E3\u4E0E controller secret \u5728 settings.json \u914D\u7F6E\uFF0C\u5165\u7AD9\u56FA\u5B9A\u53EA\u76D1\u542C\u56DE\u73AF\uFF0C\u9700\u8981\u5C40\u57DF\u7F51\u5165\u7AD9\u8BF7\u5728\u672C\u673A\u53E6\u8D77\u4E00\u4E2A mihomo-cli \u5B9E\u4F8B\uFF09`;
4626
4634
  }
4627
4635
  function writeMihomoConfig(configObj) {
4628
4636
  ensureDirs();
@@ -4645,12 +4653,12 @@ function buildKernelRejectHint(detail, overwriteSummaries, opts = {}) {
4645
4653
  `${HINT_INDENT}\u8BF7\u4FEE\u6B63\u8BA2\u9605\u6216\u8986\u5199\uFF1B\u5F53\u524D\u8FD0\u884C\u65F6\u914D\u7F6E\u672A\u6539\u52A8\u3002`,
4646
4654
  // 「未知键/字段」类报错的另一个真实根因是内核落后于订阅:机场开始用新协议字段,
4647
4655
  // 旧内核 -t 一律按不认识拒绝。只提示「修正订阅或覆写」会把方向带反
4648
- `${HINT_INDENT}\u82E5\u8BA2\u9605\u6216\u8986\u5199\u672C\u8EAB\u6CA1\u6709\u660E\u663E\u9519\u8BEF\uFF0C\u4E5F\u53EF\u80FD\u662F\u5185\u6838\u7248\u672C\u8FC7\u65E7\u3001\u4E0D\u8BA4\u8BC6\u65B0\u914D\u7F6E\u952E\uFF0C\u53EF\u5C1D\u8BD5: mihomo kernel`
4656
+ `${HINT_INDENT}\u82E5\u8BA2\u9605\u6216\u8986\u5199\u672C\u8EAB\u6CA1\u6709\u660E\u663E\u9519\u8BEF\uFF0C\u4E5F\u53EF\u80FD\u662F\u5185\u6838\u7248\u672C\u8FC7\u65E7\u3001\u4E0D\u8BA4\u8BC6\u65B0\u914D\u7F6E\u952E\uFF0C\u53EF\u5C1D\u8BD5: mihomo-cli kernel`
4649
4657
  );
4650
4658
  return hint;
4651
4659
  }
4652
4660
  async function validateConfigWithKernel(config, overwriteSummaries) {
4653
- if (!hasKernel()) throw new CliError("\u672A\u627E\u5230\u5185\u6838", { hint: "\u4E0B\u8F7D\u5185\u6838: mihomo kernel" });
4661
+ if (!hasKernel()) throw new CliError("\u672A\u627E\u5230\u5185\u6838", { hint: "\u4E0B\u8F7D\u5185\u6838: mihomo-cli kernel" });
4654
4662
  ensureDirs();
4655
4663
  const stageDir = fs4.mkdtempSync(path4.join(DIRS.runtime, "check-"));
4656
4664
  const stageFile = path4.join(stageDir, "config.yaml");
@@ -4751,10 +4759,11 @@ function formatBytes(bytes) {
4751
4759
  }
4752
4760
  function formatTraffic(upload, download, total) {
4753
4761
  if (download === void 0 && total === void 0) return null;
4754
- const used = (upload || 0) + (download || 0);
4762
+ const used = Number(upload || 0) + Number(download || 0);
4763
+ const totalNum = total === void 0 ? NaN : Number(total);
4755
4764
  let line = `${formatBytes(used)} / ${formatBytes(total)}`;
4756
- if (total && total > 0) {
4757
- line += ` (${Math.min(used / total * 100, 100).toFixed(1)}%)`;
4765
+ if (Number.isFinite(used) && Number.isFinite(totalNum) && totalNum > 0) {
4766
+ line += ` (${Math.min(used / totalNum * 100, 100).toFixed(1)}%)`;
4758
4767
  }
4759
4768
  return line;
4760
4769
  }
@@ -4956,10 +4965,6 @@ function listLogs() {
4956
4965
  return result;
4957
4966
  }
4958
4967
 
4959
- // src/process-stop.ts
4960
- import { spawnSync as spawnSync4 } from "child_process";
4961
- import fs8 from "fs";
4962
-
4963
4968
  // src/process-probe.ts
4964
4969
  import { spawnSync as spawnSync2 } from "child_process";
4965
4970
  import fs6 from "fs";
@@ -5014,13 +5019,13 @@ function isMihomoProcess(pid) {
5014
5019
  function getMihomoPids() {
5015
5020
  const result = spawnSync2("pgrep", ["-f", MAIN_INSTANCE_PATTERN], { encoding: "utf8", timeout: 1e4 });
5016
5021
  if (result.error || result.status === null) {
5017
- throw new CliError("\u65E0\u6CD5\u63A2\u6D4B mihomo \u8FDB\u7A0B\uFF08pgrep \u6267\u884C\u5931\u8D25\uFF09", {
5018
- 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"]
5022
+ throw new CliError("\u65E0\u6CD5\u63A2\u6D4B mihomo-cli \u8FDB\u7A0B\uFF08pgrep \u6267\u884C\u5931\u8D25\uFF09", {
5023
+ 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-cli"]
5019
5024
  });
5020
5025
  }
5021
5026
  if (result.status !== 0 && result.status !== 1) {
5022
5027
  throw new CliError(`\u8FDB\u7A0B\u63A2\u6D4B\u5931\u8D25\uFF08pgrep \u9000\u51FA\u7801 ${result.status}\uFF09`, {
5023
- 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)
5028
+ 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-cli"].filter(Boolean)
5024
5029
  });
5025
5030
  }
5026
5031
  const output = (result.stdout || "").trim();
@@ -5069,6 +5074,10 @@ function getStatus() {
5069
5074
  };
5070
5075
  }
5071
5076
 
5077
+ // src/process-stop.ts
5078
+ import { spawnSync as spawnSync4 } from "child_process";
5079
+ import fs8 from "fs";
5080
+
5072
5081
  // src/sudo.ts
5073
5082
  import { spawnSync as spawnSync3 } from "child_process";
5074
5083
  import fs7 from "fs";
@@ -5241,7 +5250,7 @@ async function stop() {
5241
5250
  if (result.remaining.length === 0) {
5242
5251
  warnPidCleanupFailed(result.pidError);
5243
5252
  if (result.scriptError) {
5244
- console.warn(colors.yellow(`\u8B66\u544A: root \u6B8B\u7559\u6E05\u7406\u672A\u5B8C\u6210\uFF08${describeSudoFailure(result.scriptError)}\uFF09\uFF0C\u8FDB\u7A0B\u76EE\u524D\u5DF2\u4E0D\u5728\uFF1B\u518D\u53D1\u73B0\u6B8B\u7559\u53EF\u91CD\u8BD5 mihomo stop`));
5253
+ console.warn(colors.yellow(`\u8B66\u544A: root \u6B8B\u7559\u6E05\u7406\u672A\u5B8C\u6210\uFF08${describeSudoFailure(result.scriptError)}\uFF09\uFF0C\u8FDB\u7A0B\u76EE\u524D\u5DF2\u4E0D\u5728\uFF1B\u518D\u53D1\u73B0\u6B8B\u7559\u53EF\u91CD\u8BD5 mihomo-cli stop`));
5245
5254
  }
5246
5255
  }
5247
5256
  const remaining = getMihomoPids();
@@ -5342,10 +5351,14 @@ function getServiceStatus(options = {}) {
5342
5351
  async function waitServiceHealthy() {
5343
5352
  const deadline = Date.now() + SERVICE_OBSERVE_MS;
5344
5353
  const graceDeadline = deadline + SERVICE_HEALTH_GRACE_MS;
5345
- let last;
5354
+ let last = null;
5346
5355
  do {
5347
5356
  await sleep(SERVICE_HEALTH_INTERVAL_MS);
5348
- last = getServiceStatus({ withDisabled: false });
5357
+ try {
5358
+ last = getServiceStatus({ withDisabled: false });
5359
+ } catch {
5360
+ continue;
5361
+ }
5349
5362
  if (isCrashed(last)) {
5350
5363
  return { healthy: false, crashed: true, pid: null, exitCode: last.lastExitCode, terminatingSignal: last.lastTerminatingSignal };
5351
5364
  }
@@ -5353,18 +5366,34 @@ async function waitServiceHealthy() {
5353
5366
  return { healthy: false, crashed: false, pid: null, exitCode: last.lastExitCode, terminatingSignal: last.lastTerminatingSignal };
5354
5367
  }
5355
5368
  } while (Date.now() < deadline);
5369
+ if (last === null) return healthViaProcessProbe();
5356
5370
  if (last.running) return { healthy: true, crashed: false, pid: last.pid, exitCode: null, terminatingSignal: null };
5371
+ let graceQueried = false;
5357
5372
  while (Date.now() < graceDeadline) {
5358
5373
  await sleep(SERVICE_HEALTH_INTERVAL_MS);
5359
- last = getServiceStatus({ withDisabled: false });
5374
+ try {
5375
+ last = getServiceStatus({ withDisabled: false });
5376
+ } catch {
5377
+ continue;
5378
+ }
5379
+ graceQueried = true;
5360
5380
  if (isCrashed(last)) {
5361
5381
  return { healthy: false, crashed: true, pid: null, exitCode: last.lastExitCode, terminatingSignal: last.lastTerminatingSignal };
5362
5382
  }
5363
5383
  if (!last.loaded) break;
5364
5384
  if (last.running) return { healthy: true, crashed: false, pid: last.pid, exitCode: null, terminatingSignal: null };
5365
5385
  }
5386
+ if (!graceQueried) return healthViaProcessProbe();
5366
5387
  return { healthy: false, crashed: false, pid: last.pid, exitCode: last.lastExitCode, terminatingSignal: last.lastTerminatingSignal };
5367
5388
  }
5389
+ function healthViaProcessProbe() {
5390
+ try {
5391
+ const pids = getMihomoPids();
5392
+ if (pids.length > 0) return { healthy: true, crashed: false, pid: pids[0], exitCode: null, terminatingSignal: null };
5393
+ } catch {
5394
+ }
5395
+ return { healthy: false, crashed: false, pid: null, exitCode: null, terminatingSignal: null };
5396
+ }
5368
5397
  function describeExitCause(exitCode, terminatingSignal) {
5369
5398
  if (terminatingSignal !== null) return `\u88AB\u4FE1\u53F7\u7EC8\u6B62\uFF08${terminatingSignal}\uFF09`;
5370
5399
  if (exitCode !== null && exitCode !== 0) return `\u9000\u51FA\u7801 ${exitCode}`;
@@ -5411,7 +5440,7 @@ ${argsXml}
5411
5440
  }
5412
5441
  function ensureServiceSymlink() {
5413
5442
  if (!fs9.existsSync(PATHS.mihomoBinary)) {
5414
- throw new CliError("\u672A\u627E\u5230 mihomo \u5185\u6838\uFF0C\u8BF7\u5148\u4E0B\u8F7D\u5185\u6838", { hint: "\u4E0B\u8F7D\u5185\u6838: mihomo kernel" });
5443
+ throw new CliError("\u672A\u627E\u5230 mihomo \u5185\u6838\uFF0C\u8BF7\u5148\u4E0B\u8F7D\u5185\u6838", { hint: "\u4E0B\u8F7D\u5185\u6838: mihomo-cli kernel" });
5415
5444
  }
5416
5445
  try {
5417
5446
  const current = fs9.readlinkSync(PATHS.serviceBinary);
@@ -5582,10 +5611,10 @@ async function startService() {
5582
5611
  assertServiceLabelSafe();
5583
5612
  ensureServiceSymlink();
5584
5613
  if (!isServiceInstalled()) {
5585
- throw new CliError("\u670D\u52A1\u672A\u5B89\u88C5", { hint: "\u5B89\u88C5\u670D\u52A1: mihomo install" });
5614
+ throw new CliError("\u670D\u52A1\u672A\u5B89\u88C5", { hint: "\u5B89\u88C5\u670D\u52A1: mihomo-cli install" });
5586
5615
  }
5587
5616
  if (!fs9.existsSync(PATHS.configFile)) {
5588
- throw new CliError("\u672A\u627E\u5230\u8FD0\u884C\u65F6\u914D\u7F6E", { hint: "\u8BF7\u5148\u6DFB\u52A0\u8BA2\u9605: mihomo sub add <url>" });
5617
+ throw new CliError("\u672A\u627E\u5230\u8FD0\u884C\u65F6\u914D\u7F6E", { hint: "\u8BF7\u5148\u6DFB\u52A0\u8BA2\u9605: mihomo-cli sub add <url>" });
5589
5618
  }
5590
5619
  if (getConfigInfo()?.tun) {
5591
5620
  throw new CliError("\u8FD0\u884C\u65F6\u914D\u7F6E\u4E3A TUN \u6A21\u5F0F\uFF0C\u670D\u52A1\u65E0\u6CD5\u4F7F\u7528", {
@@ -5593,12 +5622,12 @@ async function startService() {
5593
5622
  "\u670D\u52A1\u4EE5\u666E\u901A\u7528\u6237\u8EAB\u4EFD\u8FD0\u884C\uFF08\u7528\u6237\u7EA7 LaunchAgent\uFF09\uFF0C\u65E0\u6743\u521B\u5EFA TUN \u8BBE\u5907\uFF0C",
5594
5623
  "\u5F3A\u884C\u542F\u52A8\u53EA\u4F1A\u8BA9\u5185\u6838\u53CD\u590D\u5D29\u6E83\u91CD\u542F\u3002",
5595
5624
  "",
5596
- "\u6309 Mixed \u91CD\u5EFA\u914D\u7F6E\u5E76\u542F\u52A8: mihomo start mixed",
5597
- "\u786E\u5B9E\u8981\u7528 TUN: mihomo tun"
5625
+ "\u6309 Mixed \u91CD\u5EFA\u914D\u7F6E\u5E76\u542F\u52A8: mihomo-cli start mixed",
5626
+ "\u786E\u5B9E\u8981\u7528 TUN: mihomo-cli tun"
5598
5627
  ]
5599
5628
  });
5600
5629
  }
5601
- await cleanupKernelsOrThrow({ mainOutcome: "\u670D\u52A1\u5C1A\u672A\u542F\u52A8", retryCommand: "mihomo start" });
5630
+ await cleanupKernelsOrThrow({ mainOutcome: "\u670D\u52A1\u5C1A\u672A\u542F\u52A8", retryCommand: "mihomo-cli start" });
5602
5631
  bootoutService();
5603
5632
  await waitUntilUnloaded();
5604
5633
  rotateAndCleanupLogs();
@@ -5630,7 +5659,7 @@ async function stopService() {
5630
5659
  disableServiceAutoStart(SERVICE_LOCK_LAUNCHCTL_TIMEOUT_MS);
5631
5660
  });
5632
5661
  await waitUntilUnloaded();
5633
- await cleanupKernelsOrThrow({ mainOutcome: "\u670D\u52A1\u5DF2\u505C\u6B62\uFF0C\u767B\u5F55\u81EA\u542F\u5DF2\u5173\u95ED", retryCommand: "mihomo stop" });
5662
+ await cleanupKernelsOrThrow({ mainOutcome: "\u670D\u52A1\u5DF2\u505C\u6B62\uFF0C\u767B\u5F55\u81EA\u542F\u5DF2\u5173\u95ED", retryCommand: "mihomo-cli stop" });
5634
5663
  }
5635
5664
  async function uninstallService() {
5636
5665
  assertServiceLabelSafe();
@@ -5644,7 +5673,7 @@ async function uninstallService() {
5644
5673
  } catch (e) {
5645
5674
  throw new CliError(`\u5220\u9664 plist \u5931\u8D25\uFF08${PATHS.userAgentPlist}\uFF09: ${e.message}`);
5646
5675
  }
5647
- await cleanupKernelsOrThrow({ mainOutcome: "\u670D\u52A1\u5DF2\u5378\u8F7D", retryCommand: "mihomo stop" });
5676
+ await cleanupKernelsOrThrow({ mainOutcome: "\u670D\u52A1\u5DF2\u5378\u8F7D", retryCommand: "mihomo-cli stop" });
5648
5677
  try {
5649
5678
  fs9.rmSync(PATHS.serviceBinary, { force: true });
5650
5679
  } catch {
@@ -5662,6 +5691,12 @@ function buildLegacyCleanupScript() {
5662
5691
  ` exit 3`,
5663
5692
  `fi`,
5664
5693
  `rm -f ${shellQuote(PATHS.systemDaemonPlist)}`,
5694
+ // rm -f 静默吞错:删失败(文件系统只读等极端态)不声不响,而残留 plist 会在
5695
+ // 下次开机被 launchd 重新加载(KeepAlive 幽灵复活)——删除结果必须复核可见
5696
+ `if [ -e ${shellQuote(PATHS.systemDaemonPlist)} ]; then`,
5697
+ ` echo "\u9057\u7559\u670D\u52A1 plist \u5220\u9664\u5931\u8D25\uFF08rm \u540E\u4ECD\u5B58\u5728\uFF09" >&2`,
5698
+ ` exit 4`,
5699
+ `fi`,
5665
5700
  `chown "$SUDO_UID:$SUDO_GID" ${shellQuote(PATHS.logFile)} 2>/dev/null || true`,
5666
5701
  `chown -R "$SUDO_UID:$SUDO_GID" ${shellQuote(DIRS.data)} 2>/dev/null || true`,
5667
5702
  "exit 0",
@@ -5674,8 +5709,9 @@ function cleanupLegacySystemInstall() {
5674
5709
  action: "\u6E05\u7406\u9057\u7559\u7684\u7CFB\u7EDF\u7EA7\u670D\u52A1",
5675
5710
  file: "legacy-cleanup.sh",
5676
5711
  // 3 = 脚本内 bootout 真实失败(见 buildLegacyCleanupScript 的分级);
5712
+ // 4 = plist rm 后复核仍存在(rm -f 静默失败的可见化);
5677
5713
  // 具体退出码已由脚本 echo 到终端,故只指向「上方输出」
5678
- codeMessages: { 3: "launchctl bootout \u672A\u80FD\u5378\u8F7D\u65E7 daemon\uFF08\u8BE6\u89C1\u4E0A\u65B9\u8F93\u51FA\uFF09" }
5714
+ codeMessages: { 3: "launchctl bootout \u672A\u80FD\u5378\u8F7D\u65E7 daemon\uFF08\u8BE6\u89C1\u4E0A\u65B9\u8F93\u51FA\uFF09", 4: "\u672A\u80FD\u5220\u9664\u9057\u7559\u670D\u52A1\u7684 plist\uFF08\u8BE6\u89C1\u4E0A\u65B9\u8F93\u51FA\uFF09" }
5679
5715
  });
5680
5716
  }
5681
5717
  async function cleanupLegacyInstallOrThrow() {
@@ -5737,7 +5773,7 @@ function concludeHotReload(stopEpochBefore, stopEpochNow) {
5737
5773
  }
5738
5774
  async function restartService() {
5739
5775
  if (!isServiceInstalled()) {
5740
- throw new CliError("\u670D\u52A1\u672A\u5B89\u88C5\uFF0C\u65E0\u6CD5\u91CD\u542F", { hint: "\u5B89\u88C5\u670D\u52A1: mihomo install" });
5776
+ throw new CliError("\u670D\u52A1\u672A\u5B89\u88C5\uFF0C\u65E0\u6CD5\u91CD\u542F", { hint: "\u5B89\u88C5\u670D\u52A1: mihomo-cli install" });
5741
5777
  }
5742
5778
  if (!logOversized() && await tryHotReload()) {
5743
5779
  return concludeHotReload(stopEpochBaseline(), readStopEpoch());
@@ -5802,14 +5838,14 @@ function suggestSimilar(input, candidates) {
5802
5838
  // src/commands/help.ts
5803
5839
  function printShortHelp() {
5804
5840
  console.log(`
5805
- ${colors.cyan(colors.bold(`mihomo-cli v${VERSION}`))} (mihomo help \u67E5\u770B\u5B8C\u6574\u5E2E\u52A9)
5841
+ ${colors.cyan(colors.bold(`mihomo-cli v${VERSION}`))} (mihomo-cli help \u67E5\u770B\u5B8C\u6574\u5E2E\u52A9)
5806
5842
  `);
5807
5843
  const steps = [];
5808
- if (!hasKernel()) steps.push(["mihomo kernel", "\u4E0B\u8F7D\u5185\u6838"]);
5809
- if (getSubscriptions().length === 0) steps.push(["mihomo sub add <url>", "\u6DFB\u52A0\u8BA2\u9605"]);
5810
- if (!getServiceStatus().installed) steps.push(["mihomo install", "\u5B89\u88C5\u670D\u52A1\uFF08\u53EA\u9700\u4E00\u6B21\uFF09"]);
5844
+ if (!hasKernel()) steps.push(["mihomo-cli kernel", "\u4E0B\u8F7D\u5185\u6838"]);
5845
+ if (getSubscriptions().length === 0) steps.push(["mihomo-cli sub add <url>", "\u6DFB\u52A0\u8BA2\u9605"]);
5846
+ if (!getServiceStatus().installed) steps.push(["mihomo-cli install", "\u5B89\u88C5\u670D\u52A1\uFF08\u53EA\u9700\u4E00\u6B21\uFF09"]);
5811
5847
  if (steps.length > 0) {
5812
- steps.push(["mihomo start", "\u542F\u52A8\u4EE3\u7406"]);
5848
+ steps.push(["mihomo-cli start", "\u542F\u52A8\u4EE3\u7406"]);
5813
5849
  const width = Math.max(...steps.map(([cmd]) => displayWidth(cmd)));
5814
5850
  console.log("\u5F00\u59CB\u4F7F\u7528:");
5815
5851
  steps.forEach(([cmd, desc], i) => {
@@ -5836,7 +5872,7 @@ var GROUP_TITLES = [
5836
5872
  ];
5837
5873
  function printHelp(commands) {
5838
5874
  const lines = [`
5839
- ${colors.cyan(colors.bold(`mihomo-cli v${VERSION}`))}`, "", "\u547D\u4EE4\u522B\u540D: mihomo, mhm, mh", "", "\u7528\u6CD5:", " mihomo <\u547D\u4EE4> [\u9009\u9879]"];
5875
+ ${colors.cyan(colors.bold(`mihomo-cli v${VERSION}`))}`, "", "\u547D\u4EE4\u522B\u540D: mh", "", "\u7528\u6CD5:", " mihomo-cli <\u547D\u4EE4> [\u9009\u9879]"];
5840
5876
  const allUsage = commands.flatMap((c) => c.usage);
5841
5877
  const signatureWidth = Math.max(...allUsage.map((u) => displayWidth(u.signature)));
5842
5878
  const formatUsage = (u) => ` ${colors.bold(padEndDisplay(u.signature, signatureWidth))} ${u.description}`;
@@ -5856,17 +5892,19 @@ ${colors.cyan(colors.bold(`mihomo-cli v${VERSION}`))}`, "", "\u547D\u4EE4\u522B\
5856
5892
  `${colors.cyan("\u793A\u4F8B:")}`,
5857
5893
  // 顺序即首次使用的依赖顺序(kernel → 订阅 → 服务 → 启动):install 在无内核时会被
5858
5894
  // 拦下提示先跑 kernel,示例若把 install 放第一条,照着敲的新用户第一步就撞墙
5859
- " mihomo kernel # \u4E0B\u8F7D\u5185\u6838\uFF08\u9996\u6B21\u4F7F\u7528\u7684\u7B2C\u4E00\u6B65\uFF09",
5860
- " mihomo sub add <url> # \u6DFB\u52A0\u8BA2\u9605 (sub \u662F subscription \u522B\u540D)",
5861
- " mihomo install # \u5B89\u88C5\u670D\u52A1\uFF08Mixed \u6A21\u5F0F\u7684\u524D\u7F6E\uFF0C\u53EA\u9700\u4E00\u6B21\uFF09",
5862
- " mihomo start # \u542F\u52A8\u4EE3\u7406\u5E76\u5F00\u542F\u767B\u5F55\u81EA\u542F",
5863
- " mihomo stop # \u505C\u6B62\u5E76\u5173\u95ED\u767B\u5F55\u81EA\u542F",
5864
- " mihomo start tun # \u4E34\u65F6 TUN \u900F\u660E\u4EE3\u7406\uFF08\u4E0D\u8D70\u670D\u52A1\uFF0C\u9700 sudo\uFF09",
5865
- " mihomo start -s # \u8DF3\u8FC7\u81EA\u52A8\u66F4\u65B0\u8BA2\u9605",
5866
- " mihomo ui # \u6253\u5F00 Web UI",
5895
+ " mihomo-cli kernel # \u4E0B\u8F7D\u5185\u6838\uFF08\u9996\u6B21\u4F7F\u7528\u7684\u7B2C\u4E00\u6B65\uFF09",
5896
+ " mihomo-cli sub add <url> # \u6DFB\u52A0\u8BA2\u9605 (sub \u662F subscription \u522B\u540D)",
5897
+ " mihomo-cli install # \u5B89\u88C5\u670D\u52A1\uFF08Mixed \u6A21\u5F0F\u7684\u524D\u7F6E\uFF0C\u53EA\u9700\u4E00\u6B21\uFF09",
5898
+ " mihomo-cli start # \u542F\u52A8\u4EE3\u7406\u5E76\u5F00\u542F\u767B\u5F55\u81EA\u542F",
5899
+ " mihomo-cli stop # \u505C\u6B62\u5E76\u5173\u95ED\u767B\u5F55\u81EA\u542F",
5900
+ " mihomo-cli start tun # \u4E34\u65F6 TUN \u900F\u660E\u4EE3\u7406\uFF08\u4E0D\u8D70\u670D\u52A1\uFF0C\u9700 sudo\uFF09",
5901
+ " mihomo-cli start -s # \u8DF3\u8FC7\u81EA\u52A8\u66F4\u65B0\u8BA2\u9605",
5902
+ " mihomo-cli ui # \u6253\u5F00 Web UI",
5867
5903
  "",
5868
5904
  `${colors.cyan("\u5FEB\u6377\u547D\u4EE4:")}`,
5869
- " tun = start tun",
5905
+ " tun = start tun",
5906
+ " use <name> = subscription use <name>",
5907
+ " restart = start\uFF08start \u672C\u8EAB\u5373\u91CD\u542F\uFF09",
5870
5908
  "",
5871
5909
  `${colors.cyan("\u6A21\u5F0F\u8BF4\u660E:")}`,
5872
5910
  " mixed HTTP + SOCKS5 \u6DF7\u5408\u7AEF\u53E3 (\u9ED8\u8BA4)\uFF0C\u7531 launchd \u670D\u52A1\u6258\u7BA1\uFF0C\u5D29\u6E83/\u767B\u5F55\u81EA\u52A8\u62C9\u8D77",
@@ -5880,7 +5918,7 @@ ${colors.cyan(colors.bold(`mihomo-cli v${VERSION}`))}`, "", "\u547D\u4EE4\u522B\
5880
5918
  }
5881
5919
  function printCommandHelp(command) {
5882
5920
  const aliasText = command.aliases.length > 0 ? colors.gray(`\uFF08\u522B\u540D: ${command.aliases.join(", ")}\uFF09`) : "";
5883
- const lines = ["", `${colors.cyan(colors.bold(`mihomo ${command.name}`))}${aliasText}`];
5921
+ const lines = ["", `${colors.cyan(colors.bold(`mihomo-cli ${command.name}`))}${aliasText}`];
5884
5922
  if (command.usage.length > 0) {
5885
5923
  lines.push("", "\u7528\u6CD5:");
5886
5924
  const width = Math.max(...command.usage.map((u) => displayWidth(u.signature)));
@@ -5888,7 +5926,7 @@ function printCommandHelp(command) {
5888
5926
  lines.push(` ${colors.bold(padEndDisplay(u.signature, width))} ${u.description}`);
5889
5927
  }
5890
5928
  } else {
5891
- lines.push("", colors.gray("\u5FEB\u6377\u547D\u4EE4\uFF0C\u5B8C\u6574\u7528\u6CD5\u89C1 mihomo help"));
5929
+ lines.push("", colors.gray("\u5FEB\u6377\u547D\u4EE4\uFF0C\u5B8C\u6574\u7528\u6CD5\u89C1 mihomo-cli help"));
5892
5930
  }
5893
5931
  console.log(lines.join("\n"));
5894
5932
  }
@@ -5905,7 +5943,7 @@ function printHelpEntry(args, commands, finder) {
5905
5943
  commands.flatMap((c) => [c.name, ...c.aliases])
5906
5944
  );
5907
5945
  throw new CliError(`\u672A\u77E5\u547D\u4EE4: ${token}`, {
5908
- hint: [...suggestion.length > 0 ? [`\u662F\u5426\u60F3\u8F93\u5165: ${suggestion.join(" / ")}?`] : [], '\u4F7F\u7528 "mihomo help" \u67E5\u770B\u5168\u90E8\u547D\u4EE4']
5946
+ hint: [...suggestion.length > 0 ? [`\u662F\u5426\u60F3\u8F93\u5165: ${suggestion.join(" / ")}?`] : [], '\u4F7F\u7528 "mihomo-cli help" \u67E5\u770B\u5168\u90E8\u547D\u4EE4']
5909
5947
  });
5910
5948
  }
5911
5949
  printCommandHelp(command);
@@ -5918,7 +5956,7 @@ function printVersion() {
5918
5956
  }
5919
5957
 
5920
5958
  // src/redact.ts
5921
- var SECRET_KEYS = /* @__PURE__ */ new Set(["password", "uuid", "private-key", "pre-shared-key", "auth-str", "secret"]);
5959
+ var SECRET_KEYS = /* @__PURE__ */ new Set(["password", "uuid", "private-key", "pre-shared-key", "auth-str", "auth", "obfs-password", "token", "secret"]);
5922
5960
  var URL_PROVIDER_KEYS = /* @__PURE__ */ new Set(["proxy-providers", "rule-providers"]);
5923
5961
  function walk(node, withinProvider, state) {
5924
5962
  if (Array.isArray(node)) {
@@ -6150,7 +6188,7 @@ function getActiveSubscription() {
6150
6188
  function requireActiveSubscription() {
6151
6189
  const sub = getActiveSubscription();
6152
6190
  if (!sub) {
6153
- throw new CliError("\u6CA1\u6709\u8BA2\u9605\uFF0C\u8BF7\u5148\u6DFB\u52A0\u8BA2\u9605", { hint: "\u6DFB\u52A0\u8BA2\u9605: mihomo sub add <url>" });
6191
+ throw new CliError("\u6CA1\u6709\u8BA2\u9605\uFF0C\u8BF7\u5148\u6DFB\u52A0\u8BA2\u9605", { hint: "\u6DFB\u52A0\u8BA2\u9605: mihomo-cli sub add <url>" });
6154
6192
  }
6155
6193
  return sub;
6156
6194
  }
@@ -6256,7 +6294,7 @@ async function prepareConfigForStart(mode, subName = "default") {
6256
6294
  const rawContent = readSubscriptionRawConfig(subName);
6257
6295
  if (!rawContent) {
6258
6296
  throw new CliError(`\u8BA2\u9605 "${subName}" \u6709\u6761\u76EE\u4F46\u6CA1\u6709\u672C\u5730\u914D\u7F6E\u6587\u4EF6`, {
6259
- hint: `\u66F4\u65B0\u8BA2\u9605: mihomo sub update ${subName}`
6297
+ hint: `\u66F4\u65B0\u8BA2\u9605: mihomo-cli sub update ${subName}`
6260
6298
  });
6261
6299
  }
6262
6300
  const subUrl = getSubscriptions().find((s) => s.name === subName)?.url;
@@ -6355,16 +6393,16 @@ async function autoUpdateStaleSubscription(options = {}) {
6355
6393
  // src/commands/config.ts
6356
6394
  function cmdConfig(args = []) {
6357
6395
  assertKnownFlags(args.slice(1), ["-j", "--json", "--reveal"], "config [--json] [--reveal]");
6358
- assertPositionalCount(args, 0, 1, "mihomo config [--json] [--reveal]");
6396
+ assertPositionalCount(args, 0, 1, "mihomo-cli config [--json] [--reveal]");
6359
6397
  const asJson = hasFlag(args, "-j", "--json");
6360
6398
  const reveal = hasFlag(args, "--reveal");
6361
6399
  const active = getActiveSubscription();
6362
6400
  if (!active) {
6363
- throw new CliError("\u5C1A\u65E0\u8BA2\u9605\uFF0C\u65E0\u6CD5\u63A8\u5BFC\u914D\u7F6E", { hint: "\u6DFB\u52A0\u8BA2\u9605: mihomo sub add <url>" });
6401
+ throw new CliError("\u5C1A\u65E0\u8BA2\u9605\uFF0C\u65E0\u6CD5\u63A8\u5BFC\u914D\u7F6E", { hint: "\u6DFB\u52A0\u8BA2\u9605: mihomo-cli sub add <url>" });
6364
6402
  }
6365
6403
  const rawContent = readSubscriptionRawConfig(active.name);
6366
6404
  if (!rawContent) {
6367
- throw new CliError(`\u8BA2\u9605 "${active.name}" \u6709\u6761\u76EE\u4F46\u6CA1\u6709\u672C\u5730\u914D\u7F6E\u6587\u4EF6`, { hint: `\u66F4\u65B0\u8BA2\u9605: mihomo sub update ${active.name}` });
6405
+ throw new CliError(`\u8BA2\u9605 "${active.name}" \u6709\u6761\u76EE\u4F46\u6CA1\u6709\u672C\u5730\u914D\u7F6E\u6587\u4EF6`, { hint: `\u66F4\u65B0\u8BA2\u9605: mihomo-cli sub update ${active.name}` });
6368
6406
  }
6369
6407
  const mode = getConfigInfo()?.tun ? "tun" : "mixed";
6370
6408
  const { config, warnings } = buildConfig(rawContent, mode, { subName: active.name, subUrl: active.url });
@@ -6508,14 +6546,14 @@ exit 2
6508
6546
  async function startTun() {
6509
6547
  ensureDirs();
6510
6548
  if (!fs10.existsSync(PATHS.mihomoBinary)) {
6511
- throw new CliError("\u672A\u627E\u5230 mihomo \u5185\u6838\uFF0C\u8BF7\u5148\u4E0B\u8F7D\u5185\u6838", { hint: "\u4E0B\u8F7D\u5185\u6838: mihomo kernel" });
6549
+ throw new CliError("\u672A\u627E\u5230 mihomo-cli \u5185\u6838\uFF0C\u8BF7\u5148\u4E0B\u8F7D\u5185\u6838", { hint: "\u4E0B\u8F7D\u5185\u6838: mihomo-cli kernel" });
6512
6550
  }
6513
6551
  if (!fs10.existsSync(PATHS.configFile)) {
6514
6552
  throw new CliError("\u672A\u627E\u5230\u914D\u7F6E\u6587\u4EF6\uFF0C\u8BF7\u5148\u6DFB\u52A0\u8BA2\u9605\u5E76\u542F\u52A8");
6515
6553
  }
6516
6554
  if (getServiceStatus().loaded) {
6517
6555
  throw new CliError("\u53E6\u4E00\u7EC8\u7AEF\u5DF2\u542F\u52A8 Mixed \u670D\u52A1\uFF0CTUN \u672A\u542F\u52A8", {
6518
- 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"]
6556
+ hint: ["\u4E24\u8005\u4F1A\u62A2\u5360\u540C\u4E00\u7EC4\u7AEF\u53E3\u4E0E\u914D\u7F6E\u3002\u8BF7\u5148\u505C\u6B62\u670D\u52A1:", " mihomo-cli stop", "", "\u4E4B\u540E\u53EF\u91CD\u8BD5 TUN: mihomo-cli start tun"]
6519
6557
  });
6520
6558
  }
6521
6559
  rotateAndCleanupLogs();
@@ -6549,7 +6587,7 @@ async function startTun() {
6549
6587
  if (!finalPid || !isRunning()) {
6550
6588
  const tail = readLogTail();
6551
6589
  throw new CliError("TUN \u542F\u52A8\u5931\u8D25\uFF08\u5185\u6838\u672A\u80FD\u4FDD\u6301\u8FD0\u884C\uFF09", {
6552
- hint: [...tail.length > 0 ? ["--- \u65E5\u5FD7\u5C3E\u90E8 ---", ...tail, ""] : [`\u65E5\u5FD7: ${PATHS.logFile}`], "", "\u5B8C\u6574\u65E5\u5FD7: mihomo logs 0"]
6590
+ hint: [...tail.length > 0 ? ["--- \u65E5\u5FD7\u5C3E\u90E8 ---", ...tail, ""] : [`\u65E5\u5FD7: ${PATHS.logFile}`], "", "\u5B8C\u6574\u65E5\u5FD7: mihomo-cli logs 0"]
6553
6591
  });
6554
6592
  }
6555
6593
  return { success: true, pid: finalPid, mode: "tun" };
@@ -6580,6 +6618,7 @@ function restartModeOnChange() {
6580
6618
  }
6581
6619
  async function launchOrRestart(mode) {
6582
6620
  if (mode === "tun") {
6621
+ assertTunStartNotRaced();
6583
6622
  const result = await startTun();
6584
6623
  return result.pid;
6585
6624
  }
@@ -6592,7 +6631,13 @@ async function launchOrRestart(mode) {
6592
6631
  ({ started } = await startService());
6593
6632
  }
6594
6633
  if (!started) throw cancelledByConcurrentStop();
6595
- if (hotReloaded) return getServiceStatus().pid;
6634
+ if (hotReloaded) {
6635
+ try {
6636
+ return getServiceStatus().pid;
6637
+ } catch {
6638
+ return null;
6639
+ }
6640
+ }
6596
6641
  try {
6597
6642
  return await assertServiceHealthy();
6598
6643
  } catch (e) {
@@ -6604,12 +6649,21 @@ function cancelledByConcurrentStop() {
6604
6649
  return new CliError("\u542F\u52A8\u5DF2\u53D6\u6D88\uFF1A\u671F\u95F4\u68C0\u6D4B\u5230\u505C\u6B62\u64CD\u4F5C", {
6605
6650
  label: "\u542F\u52A8\u5931\u8D25",
6606
6651
  hint: [
6607
- "\u53E6\u4E00\u4E2A\u7EC8\u7AEF\u5728\u672C\u6B21\u542F\u52A8\u8FC7\u7A0B\u4E2D\u5173\u95ED\u4E86\u670D\u52A1\u81EA\u542F\uFF08mihomo stop\uFF0C\u6216 tun / install \u7B49\u540C\u6837\u4F1A\u5173\u95ED\u81EA\u542F\u7684\u547D\u4EE4\uFF09\uFF0C\u5DF2\u6309\u6700\u540E\u4E00\u6761\u547D\u4EE4\u4FDD\u6301\u505C\u6B62\u3002",
6652
+ "\u53E6\u4E00\u4E2A\u7EC8\u7AEF\u5728\u672C\u6B21\u542F\u52A8\u8FC7\u7A0B\u4E2D\u5173\u95ED\u4E86\u670D\u52A1\u81EA\u542F\uFF08mihomo-cli stop\uFF0C\u6216 tun / install \u7B49\u540C\u6837\u4F1A\u5173\u95ED\u81EA\u542F\u7684\u547D\u4EE4\uFF09\uFF0C\u5DF2\u6309\u6700\u540E\u4E00\u6761\u547D\u4EE4\u4FDD\u6301\u505C\u6B62\u3002",
6608
6653
  "",
6609
- "\u786E\u5B9E\u8981\u542F\u52A8: mihomo start"
6654
+ "\u786E\u5B9E\u8981\u542F\u52A8: mihomo-cli start"
6610
6655
  ]
6611
6656
  });
6612
6657
  }
6658
+ function tunBlockedByRunningService() {
6659
+ return new CliError("\u670D\u52A1\u6B63\u5728\u8FD0\u884C\uFF0C\u65E0\u6CD5\u542F\u52A8 TUN", {
6660
+ hint: ["\u4E24\u8005\u4F1A\u62A2\u5360\u540C\u4E00\u7EC4\u7AEF\u53E3\u4E0E\u914D\u7F6E\u3002\u8BF7\u5148\u505C\u6B62\u670D\u52A1:", " mihomo-cli stop", "", "TUN \u7528\u5B8C\u540E mihomo-cli start \u53EF\u6062\u590D\u670D\u52A1"]
6661
+ });
6662
+ }
6663
+ function assertTunStartNotRaced() {
6664
+ if (shouldAbortStartOnDisable(stopEpochBaseline(), readStopEpoch())) throw cancelledByConcurrentStop();
6665
+ if (getServiceStatus().loaded) throw tunBlockedByRunningService();
6666
+ }
6613
6667
  async function assertServiceHealthy(label = "\u542F\u52A8\u5931\u8D25") {
6614
6668
  const health = await waitServiceHealthy();
6615
6669
  if (health.healthy) return health.pid;
@@ -6619,10 +6673,10 @@ async function assertServiceHealthy(label = "\u542F\u52A8\u5931\u8D25") {
6619
6673
  throw new CliError(reason, {
6620
6674
  label,
6621
6675
  hint: [
6622
- ...health.crashed ? ["launchd \u4F1A\u6BCF\u9694\u7EA6 10 \u79D2\u53CD\u590D\u62C9\u8D77\u5B83\uFF0C\u8BF7\u5148\u4FEE\u6B63\u914D\u7F6E\u6216\u6267\u884C mihomo stop\u3002"] : [],
6676
+ ...health.crashed ? ["launchd \u4F1A\u6BCF\u9694\u7EA6 10 \u79D2\u53CD\u590D\u62C9\u8D77\u5B83\uFF0C\u8BF7\u5148\u4FEE\u6B63\u914D\u7F6E\u6216\u6267\u884C mihomo-cli stop\u3002"] : [],
6623
6677
  ...tail.length > 0 ? ["", "--- \u65E5\u5FD7\u5C3E\u90E8 ---", ...tail] : ["", `\u65E5\u5FD7: ${PATHS.logFile}`],
6624
6678
  "",
6625
- "\u5B8C\u6574\u65E5\u5FD7: mihomo logs 0"
6679
+ "\u5B8C\u6574\u65E5\u5FD7: mihomo-cli logs 0"
6626
6680
  ]
6627
6681
  });
6628
6682
  }
@@ -6633,7 +6687,7 @@ function isLoopbackHost(host) {
6633
6687
  const h = host.toLowerCase();
6634
6688
  return h === "127.0.0.1" || h === "localhost" || h === "::1" || h === "0.0.0.0" || h === "::";
6635
6689
  }
6636
- function proxyEnvPointsAtSelf(value, selfPort) {
6690
+ function parseProxyEndpoint(value) {
6637
6691
  let parsed;
6638
6692
  try {
6639
6693
  parsed = new URL(value);
@@ -6642,12 +6696,25 @@ function proxyEnvPointsAtSelf(value, selfPort) {
6642
6696
  try {
6643
6697
  parsed = new URL(`http://${value}`);
6644
6698
  } catch {
6645
- return false;
6699
+ return null;
6646
6700
  }
6647
6701
  }
6648
6702
  const host = parsed.hostname.replace(/^\[|\]$/g, "");
6649
- if (!isLoopbackHost(host)) return false;
6650
- return Number.parseInt(parsed.port, 10) === selfPort;
6703
+ const port = Number.parseInt(parsed.port, 10);
6704
+ if (!host || !Number.isInteger(port)) return null;
6705
+ return { host, port };
6706
+ }
6707
+ function proxyEnvPointsAtSelf(value, selfPort) {
6708
+ const endpoint = parseProxyEndpoint(value);
6709
+ if (!endpoint) return false;
6710
+ return isLoopbackHost(endpoint.host) && endpoint.port === selfPort;
6711
+ }
6712
+ var clearedSelfProxyEnv = null;
6713
+ function recordClearedProxyEnv(cleared) {
6714
+ clearedSelfProxyEnv = cleared === null ? null : { ...cleared };
6715
+ }
6716
+ function getClearedProxyEnv() {
6717
+ return clearedSelfProxyEnv === null ? null : { ...clearedSelfProxyEnv };
6651
6718
  }
6652
6719
  function parseScutilProxy(stdout) {
6653
6720
  const values = /* @__PURE__ */ new Map();
@@ -6705,12 +6772,12 @@ function printSystemProxyHint(mixedPort) {
6705
6772
  }
6706
6773
  if (summary.pac) {
6707
6774
  const desc = summary.pac.wpad ? "WPAD \u81EA\u52A8\u53D1\u73B0" : `PAC \u6587\u4EF6${summary.pac.source ? `\uFF08${summary.pac.source}\uFF09` : ""}`;
6708
- console.log(colors.gray(`\u7CFB\u7EDF\u4EE3\u7406\u7531 ${desc} \u63A5\u7BA1\uFF0C\u662F\u5426\u8D70 mihomo \u7531\u811A\u672C\u51B3\u5B9A\uFF1B\u5982\u9700\u56FA\u5B9A\u5168\u91CF\u8D70\u4EE3\u7406\uFF0C\u53EF\u5728\u7CFB\u7EDF\u8BBE\u7F6E\u6539\u7528\u624B\u52A8\u4EE3\u7406 127.0.0.1:${mixedPort}`));
6775
+ console.log(colors.gray(`\u7CFB\u7EDF\u4EE3\u7406\u7531 ${desc} \u63A5\u7BA1\uFF0C\u662F\u5426\u8D70 mihomo-cli \u7531\u811A\u672C\u51B3\u5B9A\uFF1B\u5982\u9700\u56FA\u5B9A\u5168\u91CF\u8D70\u4EE3\u7406\uFF0C\u53EF\u5728\u7CFB\u7EDF\u8BBE\u7F6E\u6539\u7528\u624B\u52A8\u4EE3\u7406 127.0.0.1:${mixedPort}`));
6709
6776
  return;
6710
6777
  }
6711
6778
  if (summary.matched) {
6712
6779
  if (summary.diverged.length > 0) {
6713
- console.log(colors.yellow(`\u63D0\u793A: \u90E8\u5206\u7CFB\u7EDF\u4EE3\u7406\u5DF2\u6307\u5411 127.0.0.1:${mixedPort}\uFF0C\u4F46 ${summary.diverged.join("\u3001")} \u4ECD\u6307\u5411\u522B\u5904\u2014\u2014\u5BF9\u5E94\u6D41\u91CF\u53EF\u80FD\u4E0D\u7ECF mihomo`));
6780
+ console.log(colors.yellow(`\u63D0\u793A: \u90E8\u5206\u7CFB\u7EDF\u4EE3\u7406\u5DF2\u6307\u5411 127.0.0.1:${mixedPort}\uFF0C\u4F46 ${summary.diverged.join("\u3001")} \u4ECD\u6307\u5411\u522B\u5904\u2014\u2014\u5BF9\u5E94\u6D41\u91CF\u53EF\u80FD\u4E0D\u7ECF mihomo-cli`));
6714
6781
  } else {
6715
6782
  console.log(colors.gray(`\u7CFB\u7EDF\u4EE3\u7406\u5DF2\u6307\u5411 127.0.0.1:${mixedPort}`));
6716
6783
  }
@@ -6773,7 +6840,7 @@ async function probeProxyConnectivityUncached(port) {
6773
6840
  function connectivityHint(urgency) {
6774
6841
  if (urgency === "expired") return "\u8BA2\u9605\u5DF2\u8FC7\u671F\uFF0C\u8BF7\u7EED\u8D39\u6216\u66F4\u6362\u8BA2\u9605";
6775
6842
  if (urgency === "traffic-exhausted") return "\u8BA2\u9605\u6D41\u91CF\u5DF2\u7528\u5C3D\uFF0C\u8BF7\u7EED\u8D39\u6216\u66F4\u6362\u8BA2\u9605";
6776
- return "\u8282\u70B9\u53EF\u80FD\u5931\u6548\uFF0C\u53EF\u5728 Web UI \u4E2D\u5207\u6362\u8282\u70B9 (mihomo ui)";
6843
+ return "\u8282\u70B9\u53EF\u80FD\u5931\u6548\uFF0C\u53EF\u5728 Web UI \u4E2D\u5207\u6362\u8282\u70B9 (mihomo-cli ui)";
6777
6844
  }
6778
6845
  function trafficColor(line, urgency, total, upload, download) {
6779
6846
  if (urgency === "traffic-exhausted") return colors.red(line);
@@ -6840,7 +6907,7 @@ function buildStatusJson(args) {
6840
6907
  }
6841
6908
  async function printStatus(args = []) {
6842
6909
  assertKnownFlags(args, ["-j", "--json", "--no-probe"], "status [--json] [--no-probe]");
6843
- assertPositionalCount(args, 0, 1, "mihomo status [-j|--json] [--no-probe]");
6910
+ assertPositionalCount(args, 0, 1, "mihomo-cli status [-j|--json] [--no-probe]");
6844
6911
  const asJson = hasFlag(args, "-j", "--json");
6845
6912
  const skipProbe = hasFlag(args, "--no-probe");
6846
6913
  const service = getServiceStatus();
@@ -6914,11 +6981,11 @@ async function printStatus(args = []) {
6914
6981
  const abnormalExit = describeAbnormalExit(service);
6915
6982
  if (!running && service.installed && !service.disabled && abnormalExit) {
6916
6983
  console.log(colors.yellow(` \u5F02\u5E38: \u5185\u6838\u4E0A\u6B21\u5F02\u5E38\u9000\u51FA\uFF08${abnormalExit}\uFF09\uFF0Claunchd \u6B63\u5728\u53CD\u590D\u62C9\u8D77`));
6917
- console.log(colors.gray(" \u67E5\u770B\u539F\u56E0: mihomo logs 0 \u505C\u6B62\u91CD\u8BD5: mihomo stop"));
6984
+ console.log(colors.gray(" \u67E5\u770B\u539F\u56E0: mihomo-cli logs 0 \u505C\u6B62\u91CD\u8BD5: mihomo-cli stop"));
6918
6985
  }
6919
6986
  const kernelVersion = getKernelVersion();
6920
6987
  console.log(
6921
- kernelVersion ? `${colors.gray("\u5185\u6838: ")}${kernelVersion}` : `${colors.gray("\u5185\u6838: ")}${colors.yellow("\u672A\u5B89\u88C5")} ${colors.gray("(\u4E0B\u8F7D: mihomo kernel)")}`
6988
+ kernelVersion ? `${colors.gray("\u5185\u6838: ")}${kernelVersion}` : `${colors.gray("\u5185\u6838: ")}${colors.yellow("\u672A\u5B89\u88C5")} ${colors.gray("(\u4E0B\u8F7D: mihomo-cli kernel)")}`
6922
6989
  );
6923
6990
  if (pid) {
6924
6991
  console.log(`${colors.gray("PID: ")}${pid}`);
@@ -6946,7 +7013,7 @@ async function printStatus(args = []) {
6946
7013
  if (cached?.updated_at) {
6947
7014
  const rel = formatRelativeTime(cached.updated_at) || formatDate(cached.updated_at);
6948
7015
  if (isSubscriptionStale(cached)) {
6949
- console.log(colors.yellow(`\u66F4\u65B0: ${rel}\uFF08\u5DF2\u8D85\u8FC7 ${resolveUpdateInterval(cached.update_interval)} \u5C0F\u65F6\u95F4\u9694\uFF0C\u5EFA\u8BAE mihomo sub update\uFF09`));
7016
+ console.log(colors.yellow(`\u66F4\u65B0: ${rel}\uFF08\u5DF2\u8D85\u8FC7 ${resolveUpdateInterval(cached.update_interval)} \u5C0F\u65F6\u95F4\u9694\uFF0C\u5EFA\u8BAE mihomo-cli sub update\uFF09`));
6950
7017
  } else {
6951
7018
  console.log(`${colors.gray("\u66F4\u65B0: ")}${rel}`);
6952
7019
  }
@@ -6959,12 +7026,12 @@ async function printStatus(args = []) {
6959
7026
  console.log(`${colors.gray("\u5230\u671F: ")}${expireColor(formatTimestamp(cached.expire), urgency)}`);
6960
7027
  }
6961
7028
  } else {
6962
- console.log(`${colors.gray("\u8BA2\u9605: ")}\u672A\u914D\u7F6E ${colors.gray("(\u6DFB\u52A0: mihomo sub add <url>)")}`);
7029
+ console.log(`${colors.gray("\u8BA2\u9605: ")}\u672A\u914D\u7F6E ${colors.gray("(\u6DFB\u52A0: mihomo-cli sub add <url>)")}`);
6963
7030
  }
6964
7031
  printOverwriteLines(overwriteEnabled, overwriteFiles, overwriteBroken, activeSub);
6965
7032
  printServiceLines(service, legacy);
6966
7033
  if (kind === "tun") {
6967
- console.log(colors.gray("TUN \u4E3A\u4E34\u65F6\u8FDB\u7A0B\uFF1B\u505C\u6B62: mihomo stop\uFF08\u4E4B\u540E mihomo start \u6062\u590D Mixed\uFF09"));
7034
+ console.log(colors.gray("TUN \u4E3A\u4E34\u65F6\u8FDB\u7A0B\uFF1B\u505C\u6B62: mihomo-cli stop\uFF08\u4E4B\u540E mihomo-cli start \u6062\u590D Mixed\uFF09"));
6968
7035
  }
6969
7036
  console.log("");
6970
7037
  }
@@ -6999,10 +7066,10 @@ function printOverwriteLines(enabled, files, broken, activeSub) {
6999
7066
  }
7000
7067
  function printServiceLines(service, legacy) {
7001
7068
  if (!service.installed && !service.loaded) {
7002
- console.log(`${colors.gray("\u670D\u52A1: ")}${colors.yellow("\u672A\u5B89\u88C5")} ${colors.gray("(mihomo install \u5B89\u88C5\u540E\u53EF\u7528 Mixed \u6A21\u5F0F)")}`);
7069
+ console.log(`${colors.gray("\u670D\u52A1: ")}${colors.yellow("\u672A\u5B89\u88C5")} ${colors.gray("(mihomo-cli install \u5B89\u88C5\u540E\u53EF\u7528 Mixed \u6A21\u5F0F)")}`);
7003
7070
  } else if (!service.installed) {
7004
7071
  console.log(`${colors.gray("\u670D\u52A1: ")}${colors.yellow("\u5F02\u5E38")} ${colors.gray("(plist \u4E0D\u5B58\u5728\uFF0C\u4F46\u670D\u52A1\u4ECD\u5904\u88C5\u8F7D\u72B6\u6001)")}`);
7005
- console.log(colors.gray(" KeepAlive \u4F1A\u6301\u7EED\u62C9\u8D77\u5185\u6838\uFF0C\u6E05\u7406: mihomo uninstall"));
7072
+ console.log(colors.gray(" KeepAlive \u4F1A\u6301\u7EED\u62C9\u8D77\u5185\u6838\uFF0C\u6E05\u7406: mihomo-cli uninstall"));
7006
7073
  printAutoStart(service);
7007
7074
  } else {
7008
7075
  console.log(`${colors.gray("\u670D\u52A1: ")}${colors.green("\u5DF2\u5B89\u88C5")}`);
@@ -7010,7 +7077,7 @@ function printServiceLines(service, legacy) {
7010
7077
  }
7011
7078
  if (legacy) {
7012
7079
  console.log(colors.yellow(" \u5F02\u5E38: \u68C0\u6D4B\u5230\u65E7\u7248\u672C\u7684\u7CFB\u7EDF\u7EA7\u670D\u52A1\uFF08root LaunchDaemon\uFF09"));
7013
- console.log(colors.gray(" \u5B83\u4F1A\u62A2\u5360\u540C\u4E00\u7EC4\u7AEF\u53E3\uFF0C\u6E05\u7406: mihomo uninstall\uFF08\u9700\u4E00\u6B21\u7BA1\u7406\u5458\u5BC6\u7801\uFF09"));
7080
+ console.log(colors.gray(" \u5B83\u4F1A\u62A2\u5360\u540C\u4E00\u7EC4\u7AEF\u53E3\uFF0C\u6E05\u7406: mihomo-cli uninstall\uFF08\u9700\u4E00\u6B21\u7BA1\u7406\u5458\u5BC6\u7801\uFF09"));
7014
7081
  }
7015
7082
  }
7016
7083
  function printAutoStart(service) {
@@ -7023,19 +7090,19 @@ function resolveStartMode(args) {
7023
7090
  const modeArg = getNonFlagArg(args, 1);
7024
7091
  const modeToken = modeArg?.toLowerCase();
7025
7092
  if (modeToken !== void 0 && modeToken !== "tun" && modeToken !== "mixed") {
7026
- throw new CliError(`\u672A\u77E5\u7684\u542F\u52A8\u6A21\u5F0F: ${modeArg}`, { hint: "\u7528\u6CD5: mihomo start [tun|mixed]\uFF08\u9ED8\u8BA4 mixed\uFF09" });
7093
+ throw new CliError(`\u672A\u77E5\u7684\u542F\u52A8\u6A21\u5F0F: ${modeArg}`, { hint: "\u7528\u6CD5: mihomo-cli start [tun|mixed]\uFF08\u9ED8\u8BA4 mixed\uFF09" });
7027
7094
  }
7028
7095
  return modeToken === "tun" ? "tun" : "mixed";
7029
7096
  }
7030
7097
  async function cmdStart(args) {
7031
7098
  assertKnownFlags(args, ["-s", "--no-update", "-u", "--update-timeout"], "start [tun|mixed]");
7032
- assertPositionalCount(args, 1, 1, "mihomo start [tun|mixed] [-s] [-u ms]");
7099
+ assertPositionalCount(args, 1, 1, "mihomo-cli start [tun|mixed] [-s] [-u ms]");
7033
7100
  const targetMode = resolveStartMode(args);
7034
7101
  const skipUpdate = hasFlag(args, "-s", "--no-update");
7035
7102
  const updateTimeout = parseIntArg(args, "-u", "--update-timeout", DEFAULT_AUTO_UPDATE_TIMEOUT);
7036
7103
  let disabledAutoStartForTun = false;
7037
7104
  if (!hasKernel()) {
7038
- throw new CliError("\u672A\u627E\u5230\u5185\u6838", { hint: "\u4E0B\u8F7D\u5185\u6838: mihomo kernel" });
7105
+ throw new CliError("\u672A\u627E\u5230\u5185\u6838", { hint: "\u4E0B\u8F7D\u5185\u6838: mihomo-cli kernel" });
7039
7106
  }
7040
7107
  const serviceBefore = getServiceStatus();
7041
7108
  if (targetMode === "tun") {
@@ -7047,27 +7114,26 @@ async function cmdStart(args) {
7047
7114
  console.log("");
7048
7115
  }
7049
7116
  if (serviceBefore.loaded) {
7050
- throw new CliError("\u670D\u52A1\u6B63\u5728\u8FD0\u884C\uFF0C\u65E0\u6CD5\u542F\u52A8 TUN", {
7051
- hint: ["\u4E24\u8005\u4F1A\u62A2\u5360\u540C\u4E00\u7EC4\u7AEF\u53E3\u4E0E\u914D\u7F6E\u3002\u8BF7\u5148\u505C\u6B62\u670D\u52A1:", " mihomo stop", "", "TUN \u7528\u5B8C\u540E mihomo start \u53EF\u6062\u590D\u670D\u52A1"]
7052
- });
7117
+ throw tunBlockedByRunningService();
7053
7118
  }
7054
7119
  if (serviceBefore.installed && !serviceBefore.disabled) {
7055
7120
  disableServiceAutoStart();
7056
7121
  disabledAutoStartForTun = true;
7057
7122
  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"));
7058
- console.log(colors.gray("TUN \u7528\u5B8C\u540E mihomo start \u53EF\u6062\u590D"));
7123
+ console.log(colors.gray("TUN \u7528\u5B8C\u540E mihomo-cli start \u53EF\u6062\u590D"));
7059
7124
  console.log("");
7060
7125
  } else {
7061
7126
  recordServiceStopped();
7062
7127
  }
7128
+ captureStopEpochBaseline();
7063
7129
  } else if (!serviceBefore.installed) {
7064
7130
  if (serviceBefore.loaded) {
7065
7131
  throw new CliError("\u670D\u52A1\u5904\u4E8E\u5F02\u5E38\u72B6\u6001\uFF08plist \u4E0D\u5B58\u5728\uFF0C\u4F46\u4EFB\u52A1\u4ECD\u88C5\u8F7D\uFF09", {
7066
- hint: ["\u5148\u6E05\u7406\u6B8B\u7559\u4EFB\u52A1\uFF0C\u518D\u91CD\u65B0\u5B89\u88C5:", " mihomo uninstall", " mihomo install"]
7132
+ hint: ["\u5148\u6E05\u7406\u6B8B\u7559\u4EFB\u52A1\uFF0C\u518D\u91CD\u65B0\u5B89\u88C5:", " mihomo-cli uninstall", " mihomo-cli install"]
7067
7133
  });
7068
7134
  }
7069
7135
  throw new CliError("\u670D\u52A1\u672A\u5B89\u88C5", {
7070
- hint: ["Mixed \u6A21\u5F0F\u7531 launchd \u670D\u52A1\u6258\u7BA1\uFF0C\u9700\u5148\u5B89\u88C5:", " mihomo install", "", "\u4E34\u65F6\u4F7F\u7528\u53EF\u8D70 TUN: mihomo tun"]
7136
+ 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"]
7071
7137
  });
7072
7138
  }
7073
7139
  const sub = requireActiveSubscription();
@@ -7091,18 +7157,22 @@ async function cmdStart(args) {
7091
7157
  const lines = e.message.split("\n");
7092
7158
  const extraHint = [];
7093
7159
  if (targetMode === "tun" && disabledAutoStartForTun) {
7094
- 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 start");
7160
+ 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");
7095
7161
  }
7096
7162
  if (e instanceof CliError) {
7097
7163
  throw new CliError(e.message, { label: e.label, hint: [...e.hint, ...extraHint] });
7098
7164
  }
7099
7165
  throw new CliError(lines[0], { label: "\u542F\u52A8\u5931\u8D25", hint: [...lines.slice(1), ...extraHint] });
7100
7166
  }
7101
- await printStatus();
7167
+ try {
7168
+ await printStatus();
7169
+ } catch (e) {
7170
+ console.log(colors.yellow(`\u72B6\u6001\u5C55\u793A\u5931\u8D25\uFF08\u4E0D\u5F71\u54CD\u5DF2\u5B8C\u6210\u7684\u542F\u52A8\uFF09: ${e.message.split("\n")[0]}`));
7171
+ }
7102
7172
  if (targetMode === "mixed") {
7103
7173
  printSystemProxyHint(getPorts().mixed);
7104
7174
  } else {
7105
- console.log(colors.gray("TUN \u4E3A\u4E34\u65F6\u8FDB\u7A0B\uFF0C\u5173\u95ED\u7EC8\u7AEF\u4E0D\u4F1A\u505C\u6B62\uFF1B\u505C\u6B62: mihomo stop\uFF08\u4E4B\u540E mihomo start \u6062\u590D Mixed\uFF09"));
7175
+ 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"));
7106
7176
  }
7107
7177
  console.log("");
7108
7178
  }
@@ -7157,7 +7227,7 @@ async function restartToApply(args) {
7157
7227
 
7158
7228
  // src/commands/directory.ts
7159
7229
  function openDirectory(args) {
7160
- assertPositionalCount(args, 1, 2, "mihomo dir open [root|subs|logs|data|runtime|kernel]");
7230
+ assertPositionalCount(args, 1, 2, "mihomo-cli dir open [root|subs|logs|data|runtime|kernel]");
7161
7231
  const target = args[2];
7162
7232
  if (target === void 0 || target === "root") {
7163
7233
  console.log(`\u6B63\u5728\u6253\u5F00: \u6839\u76EE\u5F55 (${USER_DATA_DIR})`);
@@ -7191,7 +7261,7 @@ function printDirectoryInfo() {
7191
7261
  console.log(` ${key.padEnd(9)}${rel.padEnd(pathWidth + 2)}${colors.gray(val.label)}`);
7192
7262
  }
7193
7263
  console.log("");
7194
- console.log(`\u6253\u5F00\u76EE\u5F55: mihomo dir open [${Object.keys(DIRECTORY_TARGETS).join("|")}]`);
7264
+ console.log(`\u6253\u5F00\u76EE\u5F55: mihomo-cli dir open [${Object.keys(DIRECTORY_TARGETS).join("|")}]`);
7195
7265
  console.log(colors.gray(" \u81EA\u5B9A\u4E49\u6839\u76EE\u5F55: \u73AF\u5883\u53D8\u91CF MIHOMO_CLI_DIR"));
7196
7266
  console.log("");
7197
7267
  }
@@ -7204,13 +7274,18 @@ async function cmdDirectory(args) {
7204
7274
  if (action.startsWith("-")) {
7205
7275
  throw new CliError(`\u672A\u77E5\u7684\u9009\u9879: ${action}`, {
7206
7276
  label: "\u53C2\u6570\u9519\u8BEF",
7207
- hint: ["\u88F8 dir \u53EA\u5C55\u793A\u76EE\u5F55\u4FE1\u606F\uFF0C\u4E0D\u63A5\u53D7\u9009\u9879", "", "\u7528\u6CD5: mihomo dir open [root|subs|logs|data|runtime|kernel]"]
7277
+ hint: ["\u88F8 dir \u53EA\u5C55\u793A\u76EE\u5F55\u4FE1\u606F\uFF0C\u4E0D\u63A5\u53D7\u9009\u9879", "", "\u7528\u6CD5: mihomo-cli dir open [root|subs|logs|data|runtime|kernel]"]
7208
7278
  });
7209
7279
  }
7210
7280
  const names = SUBCOMMANDS.flatMap((c) => [c.name, ...c.aliases ?? []]);
7211
7281
  const suggestion = suggestSimilar(action, names);
7212
7282
  throw new CliError(`\u672A\u77E5\u7684\u76EE\u5F55\u5B50\u547D\u4EE4: ${action}`, {
7213
- hint: [...suggestion.length > 0 ? [`\u662F\u5426\u60F3\u8F93\u5165: ${suggestion.join(" / ")}?`] : [], "", "\u53EF\u7528\u5B50\u547D\u4EE4: open", "\u6253\u5F00\u6307\u5B9A\u76EE\u5F55: mihomo dir open <target>"]
7283
+ hint: [
7284
+ ...suggestion.length > 0 ? [`\u662F\u5426\u60F3\u8F93\u5165: ${suggestion.join(" / ")}?`] : [],
7285
+ "",
7286
+ "\u53EF\u7528\u5B50\u547D\u4EE4: open",
7287
+ "\u6253\u5F00\u6307\u5B9A\u76EE\u5F55: mihomo-cli dir open <target>"
7288
+ ]
7214
7289
  });
7215
7290
  }
7216
7291
  });
@@ -7228,6 +7303,7 @@ import { promisify as promisify3 } from "util";
7228
7303
  var GITHUB_REPO = "MetaCubeX/mihomo";
7229
7304
  var KERNEL_HTTP_TIMEOUT = 12e4;
7230
7305
  var KERNEL_DOWNLOAD_TIMEOUT = 18e4;
7306
+ var GH_DOWNLOAD_TIMEOUT = 1e5;
7231
7307
  var HTTP_CLIENT2 = createHttpClient({ timeout: KERNEL_HTTP_TIMEOUT });
7232
7308
  var execFileAsync2 = promisify3(execFile3);
7233
7309
  function withMirror(url, mirror) {
@@ -7253,15 +7329,23 @@ function assertTrustedAssetUrl(rawUrl) {
7253
7329
  \u4EC5\u5141\u8BB8: ${[...ALLOWED_ASSET_HOSTS].join(", ")}`);
7254
7330
  }
7255
7331
  }
7256
- function resolveDownloadChannel(input) {
7257
- if (input.isOverride && !input.mirror) return { kind: "direct" };
7332
+ function resolveDownloadChannels(input) {
7333
+ if (input.isOverride && !input.mirror) return [{ kind: "direct" }];
7258
7334
  if (input.isOverride && input.mirror) {
7259
- return input.proxyOverride ? { kind: "mirror", mirror: input.mirror, proxy: input.proxyOverride } : { kind: "mirror", mirror: input.mirror };
7335
+ return [input.proxyOverride ? { kind: "mirror", mirror: input.mirror, proxy: input.proxyOverride } : { kind: "mirror", mirror: input.mirror }];
7260
7336
  }
7261
- if (input.proxyOverride) return { kind: "proxy", proxy: input.proxyOverride };
7262
- if (input.ghAvailable) return { kind: "gh" };
7263
- if (input.proxyRunning && input.proxyPort !== null) return { kind: "proxy", proxy: `http://127.0.0.1:${input.proxyPort}` };
7264
- return { kind: "direct" };
7337
+ if (input.proxyOverride) return [{ kind: "proxy", proxy: input.proxyOverride }];
7338
+ if (input.proxyRunning && input.proxyPort !== null) {
7339
+ const proxyUrl = `http://127.0.0.1:${input.proxyPort}`;
7340
+ const channels = [{ kind: "proxy", proxy: proxyUrl }];
7341
+ if (input.ghAvailable) channels.push({ kind: "gh", proxy: proxyUrl });
7342
+ return channels;
7343
+ }
7344
+ if (input.ghAvailable) return [{ kind: "gh" }];
7345
+ return [{ kind: "direct" }];
7346
+ }
7347
+ function ghProbeNeeded(input) {
7348
+ return !input.forceDirect && input.proxyOverride === null;
7265
7349
  }
7266
7350
  function hasGh() {
7267
7351
  const result = spawnSync7("gh", ["--version"], { stdio: "ignore" });
@@ -7432,6 +7516,13 @@ function buildKernelCurlArgs(args) {
7432
7516
  "--fail-with-body",
7433
7517
  "--max-filesize",
7434
7518
  String(args.maxBytes),
7519
+ // 低速快速失败:url-test 可能选中「握手快但对 Azure 几乎无带宽」的节点(实测约
7520
+ // 20KB/s 慢传),20 秒内平均速度低于 50KB/s 即中止——命令层据此切换下一通道,
7521
+ // 不必干等到 --max-time
7522
+ "--speed-limit",
7523
+ "50000",
7524
+ "--speed-time",
7525
+ "20",
7435
7526
  "--progress-bar",
7436
7527
  "--connect-timeout",
7437
7528
  "30",
@@ -7444,13 +7535,38 @@ function buildKernelCurlArgs(args) {
7444
7535
  argv.push("-o", args.outputPath, args.url);
7445
7536
  return argv;
7446
7537
  }
7538
+ function describeCurlDownloadError(status) {
7539
+ if (status === 22) {
7540
+ return "\u955C\u50CF\u6216\u670D\u52A1\u5668\u8FD4\u56DE HTTP \u9519\u8BEF\uFF084xx/5xx\uFF09\uFF0C\u8BF7\u91CD\u8BD5\u6216\u6539\u7528\u5176\u4ED6\u901A\u9053\uFF08gh/\u672C\u673A\u4EE3\u7406/--mirror direct\uFF09";
7541
+ }
7542
+ if (status === 28) {
7543
+ return "\u8FDE\u63A5\u6216\u4F20\u8F93\u8D85\u65F6\uFF1A\u8282\u70B9\u53EF\u80FD\u4E0D\u53EF\u7528\u3001\u901F\u5EA6\u8FC7\u4F4E\u6216\u88AB\u9650\u901F\uFF08\u7ECF\u4EE3\u7406\u65F6\u901A\u5E38\u662F\u5F53\u524D\u8282\u70B9\u5BF9 GitHub \u5E26\u5BBD\u4E0D\u8DB3\uFF09";
7544
+ }
7545
+ return `\u4E0B\u8F7D\u5931\u8D25 (curl \u9000\u51FA\u7801 ${status})`;
7546
+ }
7447
7547
  function buildGhReleaseDownloadArgs(tag, assetName, dir) {
7448
7548
  return ["release", "download", tag, "--repo", GITHUB_REPO, "--pattern", assetName, "--dir", dir, "--clobber"];
7449
7549
  }
7550
+ function buildGhDownloadEnv(proxy) {
7551
+ const env = { ...process.env };
7552
+ if (proxy) {
7553
+ env.HTTPS_PROXY = proxy;
7554
+ env.https_proxy = proxy;
7555
+ }
7556
+ return env;
7557
+ }
7558
+ function resolveFallbackQueryOptions(channel) {
7559
+ if (channel.kind === "gh") {
7560
+ return channel.proxy ? { proxy: channel.proxy, useGh: false } : { proxy: null, useGh: true };
7561
+ }
7562
+ if (channel.kind === "proxy") return { proxy: channel.proxy, useGh: false };
7563
+ if (channel.kind === "mirror") return { proxy: channel.proxy ?? null, useGh: false };
7564
+ return { proxy: null, useGh: false };
7565
+ }
7450
7566
  async function downloadKernel(progressCallback, channel, releaseInfo) {
7451
7567
  ensureDirs();
7452
- const apiProxy = channel.kind === "proxy" ? channel.proxy : channel.kind === "mirror" ? channel.proxy ?? null : null;
7453
- const latest = releaseInfo || await getLatestRelease(GITHUB_REPO, { proxy: apiProxy, useGh: channel.kind === "gh" });
7568
+ const apiProxy = channel.kind === "proxy" ? channel.proxy : channel.kind === "mirror" ? channel.proxy ?? null : channel.kind === "gh" ? channel.proxy ?? null : null;
7569
+ const latest = releaseInfo || await getLatestRelease(GITHUB_REPO, resolveFallbackQueryOptions(channel));
7454
7570
  const arch = getArch();
7455
7571
  const platform = process.platform;
7456
7572
  const asset = findMatchingAsset(latest.assets, platform, arch);
@@ -7480,14 +7596,16 @@ async function downloadKernel(progressCallback, channel, releaseInfo) {
7480
7596
  }
7481
7597
  const ghResult = spawnSync7("gh", buildGhReleaseDownloadArgs(latest.tag_name, asset.name, tempDir), {
7482
7598
  stdio: "inherit",
7483
- timeout: KERNEL_DOWNLOAD_TIMEOUT + 3e4
7599
+ timeout: GH_DOWNLOAD_TIMEOUT,
7600
+ // gh 回退候选带本机代理:per-spawn 注入(buildGhDownloadEnv 注释述其与 D9 的关系)
7601
+ env: buildGhDownloadEnv(channel.proxy ?? null)
7484
7602
  });
7485
7603
  if (ghResult.error) {
7486
7604
  if (ghResult.error.code === "ENOENT") {
7487
7605
  throw new Error("\u672A\u627E\u5230 gh \u547D\u4EE4\uFF08\u9009\u62E9\u901A\u9053\u65F6\u660E\u660E\u53EF\u7528\uFF09\uFF0C\u8BF7\u91CD\u8BD5\u6216\u6539\u7528\u5176\u4ED6\u901A\u9053");
7488
7606
  }
7489
7607
  if (ghResult.error.code === "ETIMEDOUT") {
7490
- throw new Error(`\u4E0B\u8F7D\u8D85\u65F6\uFF08gh ${Math.floor((KERNEL_DOWNLOAD_TIMEOUT + 3e4) / 1e3)}s \u672A\u5B8C\u6210\uFF09\uFF0CGitHub \u76F4\u8FDE\u8FC7\u6162\u65F6\u6539\u7528: mihomo kernel --mirror`);
7608
+ throw new Error(`\u4E0B\u8F7D\u8D85\u65F6\uFF08gh ${Math.floor(GH_DOWNLOAD_TIMEOUT / 1e3)}s \u672A\u5B8C\u6210\uFF09`);
7491
7609
  }
7492
7610
  throw new Error(`\u4E0B\u8F7D\u5931\u8D25: ${ghResult.error.message}`);
7493
7611
  }
@@ -7513,10 +7631,7 @@ async function downloadKernel(progressCallback, channel, releaseInfo) {
7513
7631
  throw new Error(`\u4E0B\u8F7D\u5931\u8D25: ${curlResult.error.message}`);
7514
7632
  }
7515
7633
  if (curlResult.status !== 0) {
7516
- if (curlResult.status === 22) {
7517
- throw new Error("\u4E0B\u8F7D\u5931\u8D25: \u955C\u50CF\u6216\u670D\u52A1\u5668\u8FD4\u56DE HTTP \u9519\u8BEF\uFF084xx/5xx\uFF09\uFF0C\u8BF7\u91CD\u8BD5\u6216\u6539\u7528\u5176\u4ED6\u901A\u9053\uFF08gh/\u672C\u673A\u4EE3\u7406/--mirror direct\uFF09");
7518
- }
7519
- throw new Error(`\u4E0B\u8F7D\u5931\u8D25 (curl \u9000\u51FA\u7801 ${curlResult.status})`);
7634
+ throw new Error(`\u4E0B\u8F7D\u5931\u8D25: ${describeCurlDownloadError(curlResult.status ?? 1)}`);
7520
7635
  }
7521
7636
  }
7522
7637
  if (!fs11.existsSync(tempPath)) {
@@ -7575,38 +7690,77 @@ async function downloadKernel(progressCallback, channel, releaseInfo) {
7575
7690
 
7576
7691
  // src/commands/update.ts
7577
7692
  import { execFile as execFile4, spawn as spawn2 } from "child_process";
7693
+ import net from "net";
7578
7694
  import { promisify as promisify4 } from "util";
7579
7695
  var execFileAsync3 = promisify4(execFile4);
7696
+ function restoreProxyEnvForNpm(cleared, listening) {
7697
+ return cleared !== null && listening ? cleared : {};
7698
+ }
7699
+ var PROXY_PROBE_TIMEOUT_MS = 500;
7700
+ function isProxyPortListening(host, port) {
7701
+ const targetHost = host === "0.0.0.0" ? "127.0.0.1" : host === "::" ? "::1" : host;
7702
+ return new Promise((resolve) => {
7703
+ const socket = net.connect({ host: targetHost, port });
7704
+ let settled = false;
7705
+ const done = (ok) => {
7706
+ if (settled) return;
7707
+ settled = true;
7708
+ socket.destroy();
7709
+ resolve(ok);
7710
+ };
7711
+ socket.setTimeout(PROXY_PROBE_TIMEOUT_MS);
7712
+ socket.once("connect", () => done(true));
7713
+ socket.once("timeout", () => done(false));
7714
+ socket.once("error", () => done(false));
7715
+ });
7716
+ }
7717
+ async function buildNpmSpawnEnv() {
7718
+ const env = { ...process.env };
7719
+ const cleared = getClearedProxyEnv();
7720
+ const sample = cleared ? Object.values(cleared).find((v) => v) : void 0;
7721
+ const endpoint = sample ? parseProxyEndpoint(sample) : null;
7722
+ const listening = endpoint ? await isProxyPortListening(endpoint.host, endpoint.port) : false;
7723
+ Object.assign(env, restoreProxyEnvForNpm(cleared, listening));
7724
+ return env;
7725
+ }
7580
7726
  var NPM_VIEW_TIMEOUT_MS = 15e3;
7581
7727
  async function getLatestNpmVersion(timeoutMs = NPM_VIEW_TIMEOUT_MS) {
7582
7728
  try {
7583
- const { stdout } = await execFileAsync3("npm", ["view", PKG_NAME, "version"], { timeout: timeoutMs });
7729
+ const env = await buildNpmSpawnEnv();
7730
+ const { stdout } = await execFileAsync3("npm", ["view", PKG_NAME, "version"], { timeout: timeoutMs, env });
7584
7731
  const version = stdout.trim().split("\n").pop()?.trim();
7585
7732
  return version || null;
7586
7733
  } catch {
7587
7734
  return null;
7588
7735
  }
7589
7736
  }
7737
+ function resolveUpdateAction(current, latest) {
7738
+ if (latest === null) return "proceed";
7739
+ try {
7740
+ const cmp = compareVersions(current, latest);
7741
+ if (cmp > 0) return "ahead";
7742
+ if (cmp === 0) return "current";
7743
+ } catch {
7744
+ }
7745
+ return "proceed";
7746
+ }
7590
7747
  async function cmdUpdate(args = []) {
7591
7748
  assertKnownFlags(args.slice(1), [], "update");
7592
- assertPositionalCount(args, 0, 1, "mihomo update");
7749
+ assertPositionalCount(args, 0, 1, "mihomo-cli update");
7593
7750
  console.log(`\u5F53\u524D\u7248\u672C: ${colors.cyan(VERSION)}`);
7594
7751
  console.log("");
7595
7752
  const latest = await withSpinner("\u67E5\u8BE2 npm \u6700\u65B0\u7248\u672C", getLatestNpmVersion);
7753
+ const action = resolveUpdateAction(VERSION, latest);
7754
+ if (action === "ahead") {
7755
+ console.log(colors.yellow(`\u5F53\u524D\u7248\u672C (${VERSION}) \u9886\u5148\u4E8E npm \u6700\u65B0\u7248 (${latest ?? "\u672A\u77E5"})\uFF0C\u8DF3\u8FC7\u66F4\u65B0\uFF08\u907F\u514D\u964D\u7EA7\uFF09`));
7756
+ console.log(colors.gray("\u5982\u9700\u5F3A\u5236\u91CD\u88C5: npm install -g mihomo-cli"));
7757
+ return;
7758
+ }
7759
+ if (action === "current") {
7760
+ console.log(`\u5DF2\u662F\u6700\u65B0\u7248\u672C (${colors.green(VERSION)})\uFF0C\u65E0\u9700\u66F4\u65B0`);
7761
+ return;
7762
+ }
7596
7763
  if (latest) {
7597
- try {
7598
- const cmp = compareVersions(VERSION, latest);
7599
- if (cmp > 0) {
7600
- console.log(colors.yellow(`\u5F53\u524D\u7248\u672C (${VERSION}) \u9886\u5148\u4E8E npm \u6700\u65B0\u7248 (${latest})\uFF0C\u8DF3\u8FC7\u66F4\u65B0\uFF08\u907F\u514D\u964D\u7EA7\uFF09`));
7601
- console.log(colors.gray("\u5982\u9700\u5F3A\u5236\u91CD\u88C5: npm install -g mihomo-cli"));
7602
- return;
7603
- }
7604
- if (cmp === 0) {
7605
- console.log(`\u5DF2\u662F\u6700\u65B0\u7248\u672C (${colors.green(VERSION)})\uFF0C\u65E0\u9700\u66F4\u65B0`);
7606
- return;
7607
- }
7608
- } catch {
7609
- }
7610
7764
  console.log(`\u6700\u65B0\u7248\u672C: ${colors.cyan(latest)}`);
7611
7765
  } else {
7612
7766
  console.log(colors.yellow("\u65E0\u6CD5\u67E5\u8BE2\u6700\u65B0\u7248\u672C\uFF08\u7F51\u7EDC\u95EE\u9898\uFF1F\uFF09\uFF0C\u5C06\u76F4\u63A5\u5C1D\u8BD5\u91CD\u65B0\u5B89\u88C5"));
@@ -7614,8 +7768,9 @@ async function cmdUpdate(args = []) {
7614
7768
  console.log("");
7615
7769
  console.log("\u6B63\u5728\u66F4\u65B0 mihomo-cli...");
7616
7770
  console.log("");
7771
+ const installEnv = await buildNpmSpawnEnv();
7617
7772
  await new Promise((resolve, reject) => {
7618
- const npm = spawn2("npm", ["install", "-g", PKG_NAME], { stdio: "inherit" });
7773
+ const npm = spawn2("npm", ["install", "-g", PKG_NAME], { stdio: "inherit", env: installEnv });
7619
7774
  npm.on("close", (code) => {
7620
7775
  if (code === 0) {
7621
7776
  resolve();
@@ -7680,18 +7835,24 @@ async function collectChecks() {
7680
7835
  4e3
7681
7836
  ).catch(() => null) : Promise.resolve(null);
7682
7837
  if (!hasKernel()) {
7683
- push("\u5185\u6838", "fail", "\u672A\u5B89\u88C5", "mihomo kernel");
7838
+ push("\u5185\u6838", "fail", "\u672A\u5B89\u88C5", "mihomo-cli kernel");
7684
7839
  } else {
7685
7840
  const v = getKernelVersion();
7686
7841
  const r = spawnSync8(PATHS.mihomoBinary, ["-v"], { encoding: "utf8", timeout: 5e3 });
7687
7842
  if (r.status === 0 && /v?\d+\.\d+\.\d+/.test(`${r.stdout}${r.stderr}`)) {
7688
7843
  push("\u5185\u6838", "ok", v || "\u53EF\u6267\u884C");
7689
7844
  } else if (r.error) {
7690
- push("\u5185\u6838", "fail", `\u4E8C\u8FDB\u5236\u65E0\u6CD5\u6267\u884C\uFF08${r.error.message}\uFF09`, "\u91CD\u65B0\u4E0B\u8F7D: mihomo kernel");
7845
+ push("\u5185\u6838", "fail", `\u4E8C\u8FDB\u5236\u65E0\u6CD5\u6267\u884C\uFF08${r.error.message}\uFF09`, "\u91CD\u65B0\u4E0B\u8F7D: mihomo-cli kernel");
7691
7846
  } else {
7692
- push("\u5185\u6838", "fail", `\u4E8C\u8FDB\u5236\u65E0\u6CD5\u6267\u884C\uFF08\u9000\u51FA\u7801 ${r.status}\uFF09`, "\u91CD\u65B0\u4E0B\u8F7D: mihomo kernel");
7847
+ push("\u5185\u6838", "fail", `\u4E8C\u8FDB\u5236\u65E0\u6CD5\u6267\u884C\uFF08\u9000\u51FA\u7801 ${r.status}\uFF09`, "\u91CD\u65B0\u4E0B\u8F7D: mihomo-cli kernel");
7693
7848
  }
7694
7849
  }
7850
+ if (fs12.existsSync(KERNEL_SELF_UPDATE_DIR)) {
7851
+ push("\u5185\u6838\u81EA\u5347\u7EA7\u6B8B\u7559", "warn", `\u4E2D\u65AD\u7684\u4E0B\u8F7D\u6682\u5B58: ${KERNEL_SELF_UPDATE_DIR}`, `rm -rf ${KERNEL_SELF_UPDATE_DIR}`);
7852
+ }
7853
+ if (fs12.existsSync(KERNEL_SELF_BACKUP_DIR)) {
7854
+ push("\u5185\u6838\u81EA\u5347\u7EA7\u5907\u4EFD", "ok", `\u65E7\u5185\u6838\u526F\u672C\uFF08\u9762\u677F\u5347\u7EA7\u81EA\u52A8\u4FDD\u7559\uFF0C\u4F9B\u56DE\u6EDA\uFF1B\u786E\u8BA4\u65B0\u7248\u7A33\u5B9A\u540E\u53EF\u5220: rm -rf ${KERNEL_SELF_BACKUP_DIR}\uFF09`);
7855
+ }
7695
7856
  try {
7696
7857
  fs12.accessSync(USER_DATA_DIR, fs12.constants.W_OK);
7697
7858
  push("\u6570\u636E\u76EE\u5F55", "ok", USER_DATA_DIR);
@@ -7710,12 +7871,12 @@ async function collectChecks() {
7710
7871
  const subs = getSubscriptionsWithCache();
7711
7872
  const active = getActiveSubscription();
7712
7873
  if (subs.length === 0) {
7713
- push("\u8BA2\u9605", "warn", "\u672A\u914D\u7F6E", "mihomo sub add <url>");
7874
+ push("\u8BA2\u9605", "warn", "\u672A\u914D\u7F6E", "mihomo-cli sub add <url>");
7714
7875
  } else {
7715
7876
  push("\u8BA2\u9605", "ok", `${subs.length} \u4E2A${active ? `\uFF0C\u5F53\u524D: ${active.name}` : ""}`);
7716
7877
  if (active) {
7717
7878
  if (!readSubscriptionRawConfig(active.name)) {
7718
- push("\u8BA2\u9605\u914D\u7F6E", "fail", `\u5F53\u524D\u8BA2\u9605 "${active.name}" \u6709\u6761\u76EE\u4F46\u65E0\u914D\u7F6E\u6587\u4EF6`, `mihomo sub update ${active.name}`);
7879
+ push("\u8BA2\u9605\u914D\u7F6E", "fail", `\u5F53\u524D\u8BA2\u9605 "${active.name}" \u6709\u6761\u76EE\u4F46\u65E0\u914D\u7F6E\u6587\u4EF6`, `mihomo-cli sub update ${active.name}`);
7719
7880
  } else {
7720
7881
  push("\u8BA2\u9605\u914D\u7F6E", "ok", `"${active.name}" \u914D\u7F6E\u6587\u4EF6\u5B58\u5728`);
7721
7882
  }
@@ -7723,7 +7884,7 @@ async function collectChecks() {
7723
7884
  if (cached?.updated_at) {
7724
7885
  const rel = formatRelativeTime(cached.updated_at);
7725
7886
  if (isSubscriptionStale(cached)) {
7726
- push("\u8BA2\u9605\u65B0\u9C9C\u5EA6", "warn", `${rel ?? "\u672A\u77E5"}\u66F4\u65B0\uFF0C\u5DF2\u8D85\u8FC7 ${resolveUpdateInterval(cached.update_interval)} \u5C0F\u65F6\u95F4\u9694`, "mihomo sub update");
7887
+ push("\u8BA2\u9605\u65B0\u9C9C\u5EA6", "warn", `${rel ?? "\u672A\u77E5"}\u66F4\u65B0\uFF0C\u5DF2\u8D85\u8FC7 ${resolveUpdateInterval(cached.update_interval)} \u5C0F\u65F6\u95F4\u9694`, "mihomo-cli sub update");
7727
7888
  } else {
7728
7889
  push("\u8BA2\u9605\u65B0\u9C9C\u5EA6", "ok", rel ? `${rel}\u66F4\u65B0` : `\u66F4\u65B0\u65F6\u95F4\u8BB0\u5F55\u5F02\u5E38\uFF08${formatDate(cached.updated_at)}\uFF09`);
7729
7890
  }
@@ -7733,21 +7894,21 @@ async function collectChecks() {
7733
7894
  const service = getServiceStatus();
7734
7895
  const legacy = detectLegacySystemInstall();
7735
7896
  if (legacy) {
7736
- push("\u670D\u52A1", "fail", "\u68C0\u6D4B\u5230\u65E7\u7248\u672C\u7684\u7CFB\u7EDF\u7EA7\u670D\u52A1\uFF08root LaunchDaemon\uFF09\uFF0C\u4F1A\u62A2\u5360\u7AEF\u53E3", "mihomo uninstall\uFF08\u9700\u4E00\u6B21\u7BA1\u7406\u5458\u5BC6\u7801\uFF09");
7897
+ push("\u670D\u52A1", "fail", "\u68C0\u6D4B\u5230\u65E7\u7248\u672C\u7684\u7CFB\u7EDF\u7EA7\u670D\u52A1\uFF08root LaunchDaemon\uFF09\uFF0C\u4F1A\u62A2\u5360\u7AEF\u53E3", "mihomo-cli uninstall\uFF08\u9700\u4E00\u6B21\u7BA1\u7406\u5458\u5BC6\u7801\uFF09");
7737
7898
  } else if (!service.installed && !service.loaded) {
7738
- push("\u670D\u52A1", "warn", "\u672A\u5B89\u88C5\uFF08Mixed \u6A21\u5F0F\u9700\u8981\uFF09", "mihomo install");
7899
+ push("\u670D\u52A1", "warn", "\u672A\u5B89\u88C5\uFF08Mixed \u6A21\u5F0F\u9700\u8981\uFF09", "mihomo-cli install");
7739
7900
  } else if (!service.installed) {
7740
- push("\u670D\u52A1", "fail", "plist \u4E0D\u5B58\u5728\u4F46\u4EFB\u52A1\u4ECD\u88C5\u8F7D\uFF0CKeepAlive \u4F1A\u6301\u7EED\u62C9\u8D77\u5185\u6838", "mihomo uninstall");
7901
+ push("\u670D\u52A1", "fail", "plist \u4E0D\u5B58\u5728\u4F46\u4EFB\u52A1\u4ECD\u88C5\u8F7D\uFF0CKeepAlive \u4F1A\u6301\u7EED\u62C9\u8D77\u5185\u6838", "mihomo-cli uninstall");
7741
7902
  } else if (service.running) {
7742
7903
  const abnormalExit = describeAbnormalExit(service);
7743
7904
  push("\u670D\u52A1", "ok", `\u8FD0\u884C\u4E2D${service.disabled ? "\uFF08\u81EA\u542F\u5DF2\u5173\u95ED\uFF09" : ""}${abnormalExit ? `\uFF0C\u4E0A\u6B21\u5F02\u5E38\u9000\u51FA\uFF08${abnormalExit}\uFF09` : ""}`);
7744
7905
  if (abnormalExit) {
7745
- push("\u670D\u52A1\u7A33\u5B9A\u6027", "warn", `\u5185\u6838\u4E0A\u6B21\u5F02\u5E38\u9000\u51FA\uFF08${abnormalExit}\uFF09`, "mihomo logs 0 \u67E5\u770B\u539F\u56E0");
7906
+ push("\u670D\u52A1\u7A33\u5B9A\u6027", "warn", `\u5185\u6838\u4E0A\u6B21\u5F02\u5E38\u9000\u51FA\uFF08${abnormalExit}\uFF09`, "mihomo-cli logs 0 \u67E5\u770B\u539F\u56E0");
7746
7907
  }
7747
7908
  } else {
7748
7909
  const abnormalExit = describeAbnormalExit(service);
7749
7910
  if (!service.disabled && abnormalExit) {
7750
- push("\u670D\u52A1", "fail", `\u5185\u6838\u4E0A\u6B21\u5F02\u5E38\u9000\u51FA\uFF08${abnormalExit}\uFF09\uFF0Claunchd \u6B63\u5728\u53CD\u590D\u62C9\u8D77`, "mihomo logs 0 \u67E5\u770B\u539F\u56E0\uFF0Cmihomo stop \u505C\u6B62\u91CD\u8BD5");
7911
+ push("\u670D\u52A1", "fail", `\u5185\u6838\u4E0A\u6B21\u5F02\u5E38\u9000\u51FA\uFF08${abnormalExit}\uFF09\uFF0Claunchd \u6B63\u5728\u53CD\u590D\u62C9\u8D77`, "mihomo-cli logs 0 \u67E5\u770B\u539F\u56E0\uFF0Cmihomo-cli stop \u505C\u6B62\u91CD\u8BD5");
7751
7912
  } else {
7752
7913
  push("\u670D\u52A1", "ok", `\u5DF2\u5B89\u88C5\uFF0C\u672A\u8FD0\u884C${service.disabled ? "\uFF08\u81EA\u542F\u5DF2\u5173\u95ED\uFF09" : ""}`);
7753
7914
  }
@@ -7766,7 +7927,7 @@ async function collectChecks() {
7766
7927
  if (isPortListening(mixedPort)) {
7767
7928
  push("\u7AEF\u53E3", "ok", `${mixedPort} \u6B63\u5728\u76D1\u542C`);
7768
7929
  } else {
7769
- push("\u7AEF\u53E3", "fail", `\u5185\u6838\u5728\u8DD1\u4F46 ${mixedPort} \u672A\u76D1\u542C`, "mihomo logs 0 \u67E5\u770B\u539F\u56E0");
7930
+ push("\u7AEF\u53E3", "fail", `\u5185\u6838\u5728\u8DD1\u4F46 ${mixedPort} \u672A\u76D1\u542C`, "mihomo-cli logs 0 \u67E5\u770B\u539F\u56E0");
7770
7931
  }
7771
7932
  } else if (isPortListening(mixedPort)) {
7772
7933
  push("\u7AEF\u53E3", "warn", `${mixedPort} \u88AB\u5176\u4ED6\u8FDB\u7A0B\u5360\u7528\uFF0Cstart \u4F1A\u5931\u8D25`, `lsof -nP -iTCP:${mixedPort} \u67E5\u770B\u5360\u7528\u8005`);
@@ -7791,7 +7952,7 @@ async function collectChecks() {
7791
7952
  }
7792
7953
  } catch (e) {
7793
7954
  const notes = e instanceof CliError ? e.hint.filter((l) => l.trim().length > 0) : void 0;
7794
- push("\u914D\u7F6E\u6784\u5EFA", "fail", e.message.split("\n")[0], "\u4FEE\u6B63\u8BA2\u9605\u6216\u8986\u5199\u540E mihomo start", notes);
7955
+ push("\u914D\u7F6E\u6784\u5EFA", "fail", e.message.split("\n")[0], "\u4FEE\u6B63\u8BA2\u9605\u6216\u8986\u5199\u540E mihomo-cli start", notes);
7795
7956
  }
7796
7957
  } else {
7797
7958
  push("\u914D\u7F6E\u6784\u5EFA", "skip", active ? "\u672A\u5B89\u88C5\u5185\u6838\uFF0C\u8DF3\u8FC7\u6821\u9A8C" : "\u65E0\u8BA2\u9605\uFF0C\u8DF3\u8FC7");
@@ -7801,7 +7962,7 @@ async function collectChecks() {
7801
7962
  if (probe.ok) {
7802
7963
  push("\u4EE3\u7406\u8FDE\u901A", "ok", `HTTP ${probe.statusCode}\uFF08${probe.durationMs}ms\uFF09`);
7803
7964
  } else {
7804
- push("\u4EE3\u7406\u8FDE\u901A", "warn", `\u4E0D\u901A: ${probe.error}`, "\u8282\u70B9\u53EF\u80FD\u5931\u6548\uFF0Cmihomo ui \u5207\u6362\u8282\u70B9");
7965
+ push("\u4EE3\u7406\u8FDE\u901A", "warn", `\u4E0D\u901A: ${probe.error}`, "\u8282\u70B9\u53EF\u80FD\u5931\u6548\uFF0Cmihomo-cli ui \u5207\u6362\u8282\u70B9");
7805
7966
  }
7806
7967
  } else {
7807
7968
  push("\u4EE3\u7406\u8FDE\u901A", "skip", "\u672A\u8FD0\u884C");
@@ -7810,7 +7971,7 @@ async function collectChecks() {
7810
7971
  if (hasKernel() && kernelInfo === null) {
7811
7972
  push("\u5185\u6838\u7248\u672C", "skip", "GitHub \u4E0D\u53EF\u8FBE\uFF0C\u8DF3\u8FC7\u68C0\u67E5");
7812
7973
  } else if (kernelInfo?.needsUpdate) {
7813
- push("\u5185\u6838\u7248\u672C", "warn", `\u5F53\u524D ${kernelInfo.current}\uFF0C\u6700\u65B0 ${kernelInfo.latest}`, "mihomo kernel");
7974
+ push("\u5185\u6838\u7248\u672C", "warn", `\u5F53\u524D ${kernelInfo.current}\uFF0C\u6700\u65B0 ${kernelInfo.latest}`, "mihomo-cli kernel");
7814
7975
  } else if (kernelInfo) {
7815
7976
  push("\u5185\u6838\u7248\u672C", "ok", `${kernelInfo.current}\uFF08\u6700\u65B0\uFF09`);
7816
7977
  }
@@ -7818,7 +7979,7 @@ async function collectChecks() {
7818
7979
  if (latest === null) {
7819
7980
  push("CLI \u7248\u672C", "skip", "npm registry \u4E0D\u53EF\u8FBE\uFF0C\u8DF3\u8FC7\u68C0\u67E5");
7820
7981
  } else if (compareVersions(latest, VERSION) > 0) {
7821
- push("CLI \u7248\u672C", "warn", `\u5F53\u524D ${VERSION}\uFF0C\u6700\u65B0 ${latest}`, "mihomo update");
7982
+ push("CLI \u7248\u672C", "warn", `\u5F53\u524D ${VERSION}\uFF0C\u6700\u65B0 ${latest}`, "mihomo-cli update");
7822
7983
  } else {
7823
7984
  push("CLI \u7248\u672C", "ok", `${VERSION}\uFF08\u6700\u65B0\uFF09`);
7824
7985
  }
@@ -7826,7 +7987,7 @@ async function collectChecks() {
7826
7987
  }
7827
7988
  async function cmdDoctor(args = []) {
7828
7989
  assertKnownFlags(args.slice(1), [], "doctor");
7829
- assertPositionalCount(args, 0, 1, "mihomo doctor");
7990
+ assertPositionalCount(args, 0, 1, "mihomo-cli doctor");
7830
7991
  const checks = await collectChecks();
7831
7992
  console.log("");
7832
7993
  for (const c of checks) {
@@ -7899,7 +8060,7 @@ function parseMirrorArg(args) {
7899
8060
  if (mirrorCount > 1) {
7900
8061
  throw new CliError("--mirror \u53EA\u80FD\u6307\u5B9A\u4E00\u6B21", {
7901
8062
  label: "\u53C2\u6570\u9519\u8BEF",
7902
- hint: ["\u7528\u6CD5: mihomo kernel [--mirror [\u955C\u50CF]]", `\u53EF\u7528\u955C\u50CF: ${AVAILABLE_MIRRORS.join(", ")}`, "\u4E0D\u4F7F\u7528\u955C\u50CF: mihomo kernel --mirror direct"]
8063
+ hint: ["\u7528\u6CD5: mihomo-cli kernel [--mirror [\u955C\u50CF]]", `\u53EF\u7528\u955C\u50CF: ${AVAILABLE_MIRRORS.join(", ")}`, "\u4E0D\u4F7F\u7528\u955C\u50CF: mihomo-cli kernel --mirror direct"]
7903
8064
  });
7904
8065
  }
7905
8066
  const mirrorEq = args.find((a) => a.startsWith("--mirror="));
@@ -7909,7 +8070,7 @@ function parseMirrorArg(args) {
7909
8070
  if (inline === "" || mirrorIdx >= 0 && args[mirrorIdx + 1] === "") {
7910
8071
  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", {
7911
8072
  label: "\u53C2\u6570\u9519\u8BEF",
7912
- hint: ["\u7528\u6CD5: mihomo kernel [--mirror [\u955C\u50CF]]", `\u53EF\u7528\u955C\u50CF: ${AVAILABLE_MIRRORS.join(", ")}`, "\u4E0D\u4F7F\u7528\u955C\u50CF: mihomo kernel --mirror direct"]
8073
+ hint: ["\u7528\u6CD5: mihomo-cli kernel [--mirror [\u955C\u50CF]]", `\u53EF\u7528\u955C\u50CF: ${AVAILABLE_MIRRORS.join(", ")}`, "\u4E0D\u4F7F\u7528\u955C\u50CF: mihomo-cli kernel --mirror direct"]
7913
8074
  });
7914
8075
  }
7915
8076
  const nextArg = inline ?? args[mirrorIdx + 1];
@@ -7974,7 +8135,7 @@ function parseProxyArg(args) {
7974
8135
  if (hits.length > 1) {
7975
8136
  throw new CliError("--proxy \u53EA\u80FD\u6307\u5B9A\u4E00\u6B21", {
7976
8137
  label: "\u53C2\u6570\u9519\u8BEF",
7977
- hint: ["\u7528\u6CD5: mihomo kernel --proxy <\u7AEF\u53E3|\u5730\u5740>", "\u4F8B\u5982: mihomo kernel --proxy 7897"]
8138
+ hint: ["\u7528\u6CD5: mihomo-cli kernel --proxy <\u7AEF\u53E3|\u5730\u5740>", "\u4F8B\u5982: mihomo-cli kernel --proxy 7897"]
7978
8139
  });
7979
8140
  }
7980
8141
  const raw = hits[0].trim();
@@ -7989,14 +8150,71 @@ function parseProxyArg(args) {
7989
8150
 
7990
8151
  // src/commands/kernel.ts
7991
8152
  var KERNEL_VALUE_FLAGS = /* @__PURE__ */ new Set([...VALUE_FLAGS, "--mirror"]);
8153
+ function channelLabel(channel, isExplicitProxy) {
8154
+ switch (channel.kind) {
8155
+ case "gh":
8156
+ return channel.proxy ? `gh\uFF08\u7ECF\u672C\u673A\u4EE3\u7406 ${channel.proxy}\uFF09` : "gh";
8157
+ case "proxy":
8158
+ return `${isExplicitProxy ? "\u4EE3\u7406" : "\u672C\u673A\u4EE3\u7406"} ${channel.proxy}`;
8159
+ case "mirror":
8160
+ return `\u955C\u50CF ${channel.mirror}${channel.proxy ? `\uFF08\u7ECF\u4EE3\u7406 ${channel.proxy}\uFF09` : ""}`;
8161
+ case "direct":
8162
+ return "\u76F4\u8FDE";
8163
+ }
8164
+ }
8165
+ function printChannelLine(channel, isExplicitProxy) {
8166
+ if (channel.kind === "direct") return;
8167
+ if (channel.kind === "gh") {
8168
+ console.log(channel.proxy ? `\u4E0B\u8F7D\u901A\u9053: gh\uFF08GitHub CLI\uFF0C\u7ECF\u672C\u673A\u4EE3\u7406 ${channel.proxy}\uFF09` : "\u4E0B\u8F7D\u901A\u9053: gh\uFF08GitHub CLI\uFF09");
8169
+ } else if (channel.kind === "proxy") {
8170
+ console.log(`\u4E0B\u8F7D\u901A\u9053: ${isExplicitProxy ? "\u4EE3\u7406" : "\u672C\u673A\u4EE3\u7406"} ${channel.proxy}`);
8171
+ } else if (channel.kind === "mirror") {
8172
+ const host = channel.mirror.replace(/^https?:\/\//, "").replace(/\/$/, "");
8173
+ console.log(`\u955C\u50CF: ${host}${channel.proxy ? `\uFF08\u7ECF\u4EE3\u7406 ${channel.proxy}\uFF09` : ""}`);
8174
+ }
8175
+ console.log("");
8176
+ }
8177
+ function formatChannelSwitchLine(previousError) {
8178
+ const reason = previousError?.message.split("\n")[0] ?? "";
8179
+ 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:";
8180
+ }
8181
+ function buildDownloadFailureError(attempts, isExplicitProxy) {
8182
+ if (attempts.length === 1) {
8183
+ return new CliError(attempts[0].error.message, {
8184
+ label: "\u4E0B\u8F7D\u5931\u8D25",
8185
+ hint: [
8186
+ "",
8187
+ "\u53EF\u6539\u7528\u5176\u4ED6\u901A\u9053\u91CD\u8BD5:",
8188
+ " mihomo-cli kernel # \u81EA\u52A8\u9009\u62E9\uFF08\u672C\u673A\u4EE3\u7406\u4F18\u5148\u3001gh \u56DE\u9000\uFF09",
8189
+ " mihomo-cli kernel --mirror [\u955C\u50CF] # \u5F3A\u5236\u955C\u50CF\uFF08\u53EF\u7528 v6/v4/cdn \u7B49\u522B\u540D\uFF09",
8190
+ ` \u53EF\u7528\u955C\u50CF: ${AVAILABLE_MIRRORS.join(", ")}`,
8191
+ " mihomo-cli kernel --mirror direct # \u5F3A\u5236\u76F4\u8FDE",
8192
+ " mihomo-cli kernel --proxy <\u7AEF\u53E3> # \u7ECF\u672C\u673A\u5176\u4ED6\u4EE3\u7406\u5DE5\u5177\u51FA\u7F51"
8193
+ ]
8194
+ });
8195
+ }
8196
+ return new CliError("\u5168\u90E8\u4E0B\u8F7D\u901A\u9053\u5747\u5931\u8D25", {
8197
+ label: "\u4E0B\u8F7D\u5931\u8D25",
8198
+ hint: [
8199
+ ...attempts.map((a) => ` ${channelLabel(a.channel, isExplicitProxy)}: ${a.error.message.split("\n")[0]}`),
8200
+ "",
8201
+ "\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",
8202
+ "\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",
8203
+ "",
8204
+ "\u901A\u9053\u9009\u62E9\uFF1A\u672C\u673A\u4EE3\u7406\u5728\u8DD1\u65F6\u81EA\u52A8\u4F18\u5148\uFF08\u542B\u4F4E\u901F\u5FEB\u901F\u5931\u8D25\uFF09\uFF0C\u5931\u8D25\u56DE\u9000 gh\uFF08\u4ECD\u7ECF\u540C\u4E00\u672C\u673A\u4EE3\u7406\uFF09\uFF1B",
8205
+ "\u624B\u52A8\u6307\u5B9A: mihomo-cli kernel --mirror [\u955C\u50CF]\uFF08\u5F3A\u5236\u955C\u50CF\uFF09/ mihomo-cli kernel --mirror direct\uFF08\u5F3A\u5236\u76F4\u8FDE\uFF09",
8206
+ " mihomo-cli kernel --proxy <\u7AEF\u53E3>\uFF08\u7ECF\u672C\u673A\u5176\u4ED6\u4EE3\u7406\u5DE5\u5177\u51FA\u7F51\uFF09"
8207
+ ]
8208
+ });
8209
+ }
7992
8210
  async function cmdKernel(args) {
7993
8211
  const mirrorInfo = parseMirrorArg(args);
7994
8212
  const proxyInfo = parseProxyArg(args);
7995
- assertPositionalCount(args, 0, 1, "mihomo kernel [--mirror [\u955C\u50CF]] [--proxy <\u7AEF\u53E3|\u5730\u5740>]", KERNEL_VALUE_FLAGS);
8213
+ assertPositionalCount(args, 0, 1, "mihomo-cli kernel [--mirror [\u955C\u50CF]] [--proxy <\u7AEF\u53E3|\u5730\u5740>]", KERNEL_VALUE_FLAGS);
7996
8214
  if (proxyInfo.proxy && mirrorInfo.isOverride && !mirrorInfo.mirror) {
7997
8215
  throw new CliError("--mirror direct \u4E0E --proxy \u4E0D\u80FD\u540C\u65F6\u4F7F\u7528", {
7998
8216
  label: "\u53C2\u6570\u9519\u8BEF",
7999
- hint: ["--mirror direct \u5F3A\u5236\u4E0D\u7ECF\u4EFB\u4F55\u4EE3\u7406\u76F4\u8FDE\uFF0C\u9700\u8D70\u4EE3\u7406\u65F6\u53BB\u6389\u5B83:", " mihomo kernel --proxy <\u7AEF\u53E3|\u5730\u5740>"]
8217
+ hint: ["--mirror direct \u5F3A\u5236\u4E0D\u7ECF\u4EE3\u7406\u76F4\u8FDE\uFF0C\u9700\u8D70\u4EE3\u7406\u65F6\u53BB\u6389\u5B83:", " mihomo-cli kernel --proxy <\u7AEF\u53E3|\u5730\u5740>"]
8000
8218
  });
8001
8219
  }
8002
8220
  const proxyRunning = getRunningState().running;
@@ -8009,8 +8227,8 @@ async function cmdKernel(args) {
8009
8227
  }
8010
8228
  }
8011
8229
  const forceDirect = mirrorInfo.isOverride && !mirrorInfo.mirror;
8012
- const ghAvailable = hasGh();
8013
- const channel = resolveDownloadChannel({
8230
+ const ghAvailable = ghProbeNeeded({ forceDirect, proxyOverride: proxyInfo.proxy }) && hasGh();
8231
+ const channels = resolveDownloadChannels({
8014
8232
  mirror: mirrorInfo.mirror,
8015
8233
  isOverride: mirrorInfo.isOverride,
8016
8234
  ghAvailable,
@@ -8018,18 +8236,7 @@ async function cmdKernel(args) {
8018
8236
  proxyPort,
8019
8237
  proxyOverride: proxyInfo.proxy
8020
8238
  });
8021
- if (channel.kind === "gh") {
8022
- console.log("\u4E0B\u8F7D\u901A\u9053: gh\uFF08GitHub CLI \u76F4\u8FDE\uFF09");
8023
- console.log("");
8024
- } else if (channel.kind === "proxy") {
8025
- const label = proxyInfo.proxy ? "\u4EE3\u7406" : "\u672C\u673A\u4EE3\u7406";
8026
- console.log(`\u4E0B\u8F7D\u901A\u9053: ${label} ${channel.proxy}`);
8027
- console.log("");
8028
- } else if (channel.kind === "mirror") {
8029
- const host = channel.mirror.replace(/^https?:\/\//, "").replace(/\/$/, "");
8030
- console.log(`\u955C\u50CF: ${host}${channel.proxy ? `\uFF08\u7ECF\u4EE3\u7406 ${channel.proxy}\uFF09` : ""}`);
8031
- console.log("");
8032
- }
8239
+ printChannelLine(channels[0], proxyInfo.proxy !== null);
8033
8240
  const apiProxy = proxyInfo.proxy ?? (proxyRunning && !forceDirect && proxyPort !== null ? `http://127.0.0.1:${proxyPort}` : null);
8034
8241
  const useGh = apiProxy === null && !forceDirect && ghAvailable;
8035
8242
  let info;
@@ -8058,14 +8265,14 @@ async function cmdKernel(args) {
8058
8265
  }
8059
8266
  } else if (!mirrorInfo.mirror) {
8060
8267
  if (apiProxy) {
8061
- hint.push("", "\u63D0\u793A: \u7ECF\u4EE3\u7406\u67E5\u8BE2 GitHub \u5931\u8D25\uFF0C\u53EF\u68C0\u67E5\u4EE3\u7406\u662F\u5426\u53EF\u7528\uFF0C\u6216 mihomo kernel --mirror direct \u91CD\u8BD5\u76F4\u8FDE");
8268
+ hint.push("", "\u63D0\u793A: \u7ECF\u4EE3\u7406\u67E5\u8BE2 GitHub \u5931\u8D25\uFF0C\u53EF\u68C0\u67E5\u4EE3\u7406\u662F\u5426\u53EF\u7528\uFF0C\u6216 mihomo-cli kernel --mirror direct \u91CD\u8BD5\u76F4\u8FDE");
8062
8269
  } else {
8063
8270
  hint.push(
8064
8271
  "",
8065
8272
  "\u63D0\u793A: \u76F4\u8FDE\u5931\u8D25\u6216\u4E0B\u8F7D\u8FC7\u6162\u65F6\u53EF\u4F7F\u7528\u955C\u50CF\u6216\u4EE3\u7406:",
8066
- " mihomo kernel --mirror [\u955C\u50CF] # \u5F3A\u5236\u8D70\u955C\u50CF\uFF08\u88F8 --mirror \u56FA\u5B9A\u88F8\u57DF\uFF0C\u53EF\u7528 v6/v4/cdn \u7B49\u522B\u540D\uFF09",
8273
+ " mihomo-cli kernel --mirror [\u955C\u50CF] # \u5F3A\u5236\u8D70\u955C\u50CF\uFF08\u88F8 --mirror \u56FA\u5B9A\u88F8\u57DF\uFF0C\u53EF\u7528 v6/v4/cdn \u7B49\u522B\u540D\uFF09",
8067
8274
  ` \u53EF\u7528\u955C\u50CF: ${AVAILABLE_MIRRORS.join(", ")}`,
8068
- " mihomo kernel --proxy <\u7AEF\u53E3> # \u7ECF\u672C\u673A\u5176\u4ED6\u4EE3\u7406\u5DE5\u5177\u51FA\u7F51"
8275
+ " mihomo-cli kernel --proxy <\u7AEF\u53E3> # \u7ECF\u672C\u673A\u5176\u4ED6\u4EE3\u7406\u5DE5\u5177\u51FA\u7F51"
8069
8276
  );
8070
8277
  }
8071
8278
  } else if (apiProxy) {
@@ -8078,7 +8285,7 @@ async function cmdKernel(args) {
8078
8285
  "\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:",
8079
8286
  " \u5B89\u88C5\u5E76\u767B\u5F55 GitHub CLI\uFF08\u8BA4\u8BC1\u914D\u989D 5000 \u6B21/\u65F6\uFF09:",
8080
8287
  " brew install gh && gh auth login",
8081
- " \u6216\u7ECF\u672C\u673A\u4EE3\u7406\u5DE5\u5177\u67E5\u8BE2: mihomo kernel --proxy <\u7AEF\u53E3>"
8288
+ " \u6216\u7ECF\u672C\u673A\u4EE3\u7406\u5DE5\u5177\u67E5\u8BE2: mihomo-cli kernel --proxy <\u7AEF\u53E3>"
8082
8289
  );
8083
8290
  }
8084
8291
  throw new CliError(err.message, { label: "\u66F4\u65B0\u5931\u8D25", hint });
@@ -8093,25 +8300,28 @@ async function cmdKernel(args) {
8093
8300
  } else {
8094
8301
  console.log("\n\u6B63\u5728\u4E0B\u8F7D...");
8095
8302
  let result;
8096
- try {
8097
- result = await downloadKernel((msg) => console.log(msg), channel, info.release);
8098
- } catch (e) {
8099
- if (e instanceof CliError) throw e;
8100
- throw new CliError(e.message, {
8101
- label: "\u4E0B\u8F7D\u5931\u8D25",
8102
- hint: [
8103
- "",
8104
- "\u4E0B\u8F7D\u901A\u9053\u6309\u4F18\u5148\u7EA7\u81EA\u52A8\u9009\u62E9: gh\uFF08GitHub CLI\uFF09> \u672C\u673A\u4EE3\u7406 > \u76F4\u8FDE",
8105
- "\u624B\u52A8\u6307\u5B9A: mihomo kernel --mirror [\u955C\u50CF]\uFF08\u5F3A\u5236\u955C\u50CF\uFF09/ mihomo kernel --mirror direct\uFF08\u5F3A\u5236\u76F4\u8FDE\uFF09",
8106
- " mihomo kernel --proxy <\u7AEF\u53E3>\uFF08\u7ECF\u672C\u673A\u5176\u4ED6\u4EE3\u7406\u5DE5\u5177\u51FA\u7F51\uFF09"
8107
- ]
8108
- });
8303
+ const attempts = [];
8304
+ for (let attempt = 0; attempt < channels.length; attempt++) {
8305
+ if (attempt > 0) {
8306
+ console.log(formatChannelSwitchLine(attempts[attempt - 1]?.error ?? null));
8307
+ printChannelLine(channels[attempt], proxyInfo.proxy !== null);
8308
+ }
8309
+ try {
8310
+ result = await downloadKernel((msg) => console.log(msg), channels[attempt], info.release);
8311
+ } catch (e) {
8312
+ attempts.push({ channel: channels[attempt], error: e });
8313
+ continue;
8314
+ }
8315
+ break;
8316
+ }
8317
+ if (!result) {
8318
+ throw buildDownloadFailureError(attempts, proxyInfo.proxy !== null);
8109
8319
  }
8110
8320
  console.log(`
8111
8321
  \u5DF2\u66F4\u65B0\u5230 ${result.version}`);
8112
8322
  const state = getRunningState();
8113
8323
  if (state.running) {
8114
- const restartCommand = state.kind === "tun" ? "mihomo start tun" : "mihomo start";
8324
+ const restartCommand = state.kind === "tun" ? "mihomo-cli start tun" : "mihomo-cli start";
8115
8325
  console.log(colors.yellow(`\u63D0\u793A: \u8FD0\u884C\u4E2D\u7684\u5185\u6838\u4ECD\u662F\u65E7\u7248\u672C\uFF0C\u6267\u884C ${restartCommand} \u91CD\u542F\u540E\u751F\u6548`));
8116
8326
  }
8117
8327
  console.log(colors.gray("\u63D0\u793A: \u82E5\u4F7F\u7528\u5C40\u57DF\u7F51\u8282\u70B9\uFF08192.168/10.x/*.local\uFF09\uFF0C\u9996\u6B21\u542F\u52A8\u53EF\u80FD\u91CD\u65B0\u5F39\u51FA\u300C\u672C\u5730\u7F51\u7EDC\u300D\u6388\u6743"));
@@ -8121,7 +8331,7 @@ async function cmdKernel(args) {
8121
8331
  // src/commands/log.ts
8122
8332
  function cmdLogs(args) {
8123
8333
  assertKnownFlags(args, ["-f", "--follow", "-n", "--lines", "-o", "--open"], "logs [-f] [-n N] [\u7F16\u53F7] [-o]");
8124
- assertPositionalCount(args, 1, 1, "mihomo logs [\u7F16\u53F7] [-f] [-n N] [-o]");
8334
+ assertPositionalCount(args, 1, 1, "mihomo-cli logs [\u7F16\u53F7] [-f] [-n N] [-o]");
8125
8335
  const lines = parseIntArg(args, "-n", "--lines", 100);
8126
8336
  const openInViewer = hasFlag(args, "-o", "--open");
8127
8337
  const follow = hasFlag(args, "-f", "--follow");
@@ -8134,18 +8344,18 @@ function cmdLogs(args) {
8134
8344
  } else {
8135
8345
  const parsedIdx = parseInt(targetName, 10);
8136
8346
  if (Number.isNaN(parsedIdx) || parsedIdx < 1 || String(parsedIdx) !== targetName) {
8137
- throw new CliError(`\u65E0\u6548\u7684\u65E5\u5FD7\u7F16\u53F7 "${targetName}"`, { hint: "\u7528\u6CD5: mihomo logs <\u7F16\u53F7>\uFF080=\u5F53\u524D\uFF0C1+=\u5F52\u6863\uFF09\uFF1B\u67E5\u770B\u5217\u8868: mihomo logs" });
8347
+ throw new CliError(`\u65E0\u6548\u7684\u65E5\u5FD7\u7F16\u53F7 "${targetName}"`, { hint: "\u7528\u6CD5: mihomo-cli logs <\u7F16\u53F7>\uFF080=\u5F53\u524D\uFF0C1+=\u5F52\u6863\uFF09\uFF1B\u67E5\u770B\u5217\u8868: mihomo-cli logs" });
8138
8348
  }
8139
8349
  const archive = listLogs().archives[parsedIdx - 1];
8140
8350
  if (!archive) {
8141
- throw new CliError(`\u672A\u627E\u5230\u65E5\u5FD7 "${targetName}"`, { hint: '\u4F7F\u7528 "mihomo logs" \u67E5\u770B\u53EF\u7528\u65E5\u5FD7\u5217\u8868' });
8351
+ throw new CliError(`\u672A\u627E\u5230\u65E5\u5FD7 "${targetName}"`, { hint: '\u4F7F\u7528 "mihomo-cli logs" \u67E5\u770B\u53EF\u7528\u65E5\u5FD7\u5217\u8868' });
8142
8352
  }
8143
8353
  logPath = archive.path;
8144
8354
  }
8145
8355
  if (openInViewer) {
8146
8356
  if (follow) {
8147
8357
  throw new CliError("-o\uFF08\u7CFB\u7EDF\u67E5\u770B\u5668\u6253\u5F00\uFF09\u4E0E -f\uFF08\u7EC8\u7AEF\u8DDF\u968F\uFF09\u4E92\u65A5", {
8148
- hint: "\u8DDF\u968F\u8F93\u51FA\u8BF7\u53BB\u6389 -o: mihomo logs 0 -f"
8358
+ hint: "\u8DDF\u968F\u8F93\u51FA\u8BF7\u53BB\u6389 -o: mihomo-cli logs 0 -f"
8149
8359
  });
8150
8360
  }
8151
8361
  openLogFile(logPath);
@@ -8180,16 +8390,16 @@ function cmdLogs(args) {
8180
8390
  console.log(` ${num}. ${name}`);
8181
8391
  console.log(` \u65F6\u95F4: ${time} \u5927\u5C0F: ${size}`);
8182
8392
  if (!log.isCurrent) {
8183
- console.log(` \u67E5\u770B: mihomo logs ${archiveCounter} \u6216 mihomo logs ${archiveCounter} -o`);
8393
+ console.log(` \u67E5\u770B: mihomo-cli logs ${archiveCounter} \u6216 mihomo-cli logs ${archiveCounter} -o`);
8184
8394
  }
8185
8395
  console.log("");
8186
8396
  }
8187
8397
  console.log("\u7528\u6CD5:");
8188
- console.log(" mihomo logs 0 # \u67E5\u770B\u5F53\u524D\u65E5\u5FD7 (\u6700\u540E 100 \u884C)");
8189
- console.log(" mihomo logs 0 -f # \u5B9E\u65F6\u8DDF\u968F\u5F53\u524D\u65E5\u5FD7");
8190
- console.log(" mihomo logs 1 # \u67E5\u770B\u7B2C 1 \u4E2A\u5F52\u6863\u65E5\u5FD7\uFF08\u6700\u65B0\uFF09");
8191
- console.log(" mihomo logs 1 -n 200 # \u67E5\u770B 200 \u884C");
8192
- console.log(" mihomo logs 1 -o # \u7528\u7CFB\u7EDF\u9ED8\u8BA4\u7A0B\u5E8F\u6253\u5F00");
8398
+ console.log(" mihomo-cli logs 0 # \u67E5\u770B\u5F53\u524D\u65E5\u5FD7 (\u6700\u540E 100 \u884C)");
8399
+ console.log(" mihomo-cli logs 0 -f # \u5B9E\u65F6\u8DDF\u968F\u5F53\u524D\u65E5\u5FD7");
8400
+ console.log(" mihomo-cli logs 1 # \u67E5\u770B\u7B2C 1 \u4E2A\u5F52\u6863\u65E5\u5FD7\uFF08\u6700\u65B0\uFF09");
8401
+ console.log(" mihomo-cli logs 1 -n 200 # \u67E5\u770B 200 \u884C");
8402
+ console.log(" mihomo-cli logs 1 -o # \u7528\u7CFB\u7EDF\u9ED8\u8BA4\u7A0B\u5E8F\u6253\u5F00");
8193
8403
  console.log("");
8194
8404
  }
8195
8405
 
@@ -8238,13 +8448,13 @@ function printOverwriteList() {
8238
8448
  console.log("");
8239
8449
  }
8240
8450
  }
8241
- console.log("\u542F\u7528\u8986\u5199: mihomo ow on");
8242
- console.log("\u7981\u7528\u8986\u5199: mihomo ow off");
8451
+ console.log("\u542F\u7528\u8986\u5199: mihomo-cli ow on");
8452
+ console.log("\u7981\u7528\u8986\u5199: mihomo-cli ow off");
8243
8453
  console.log("\u505C\u7528\u5355\u4E2A\u6587\u4EF6: \u5728\u8BE5\u6587\u4EF6\u9876\u90E8\u5199 enabled: false");
8244
8454
  console.log("");
8245
8455
  }
8246
8456
  async function setOverwrite(enabled, args) {
8247
- assertPositionalCount(args, 0, 2, "mihomo ow [on|off]");
8457
+ assertPositionalCount(args, 0, 2, "mihomo-cli ow [on|off]");
8248
8458
  assertRestartOptionValues(args);
8249
8459
  if (isOverwriteEnabled() === enabled) {
8250
8460
  console.log(`\u8986\u5199\u914D\u7F6E\u5DF2\u662F${enabled ? "\u542F\u7528" : "\u7981\u7528"}\u72B6\u6001`);
@@ -8274,7 +8484,7 @@ async function cmdOverwrite(args) {
8274
8484
  if (action.startsWith("-")) {
8275
8485
  throw new CliError(`\u672A\u77E5\u7684\u9009\u9879: ${action}`, {
8276
8486
  label: "\u53C2\u6570\u9519\u8BEF",
8277
- hint: ["\u88F8 ow \u53EA\u67E5\u770B\u8986\u5199\u72B6\u6001\uFF0C\u4E0D\u63A5\u53D7\u9009\u9879", "", "\u7528\u6CD5: mihomo ow on|off [-s] [-u ms]"]
8487
+ hint: ["\u88F8 ow \u53EA\u67E5\u770B\u8986\u5199\u72B6\u6001\uFF0C\u4E0D\u63A5\u53D7\u9009\u9879", "", "\u7528\u6CD5: mihomo-cli ow on|off [-s] [-u ms]"]
8278
8488
  });
8279
8489
  }
8280
8490
  const names = SUBCOMMANDS2.flatMap((c) => [c.name, ...c.aliases ?? []]);
@@ -8302,7 +8512,7 @@ var RESET_TARGETS = [
8302
8512
  paths: () => fs13.existsSync(USER_DATA_DIR) ? fs13.readdirSync(USER_DATA_DIR).filter(isOverwriteFilename).map((f) => `${USER_DATA_DIR}/${f}`) : []
8303
8513
  },
8304
8514
  {
8305
- // 别名刻意不含 'config':用户从 `mihomo config` 命令得到的直觉是「运行配置」
8515
+ // 别名刻意不含 'config':用户从 `mihomo-cli config` 命令得到的直觉是「运行配置」
8306
8516
  // (那属于 runtime 目标),而这里删的是 settings(订阅列表/端口/密钥)——
8307
8517
  // 静默对撞会让 reset config -y 删超预期的数据。未知目标报错 + 目标列表兜底
8308
8518
  id: "settings",
@@ -8332,7 +8542,7 @@ async function cmdReset(args) {
8332
8542
  const names = args.slice(1).filter((a) => !a.startsWith("-"));
8333
8543
  if (names.length > 0 && args.includes("--full")) {
8334
8544
  throw new CliError("\u4E0D\u80FD\u540C\u65F6\u6307\u5B9A\u91CD\u7F6E\u76EE\u6807\u4E0E --full", {
8335
- hint: [`\u53EA\u5220\u6307\u5B9A\u76EE\u6807: mihomo reset ${names.join(" ")}`, "\u91CD\u7F6E\u5168\u90E8: mihomo reset --full"]
8545
+ hint: [`\u53EA\u5220\u6307\u5B9A\u76EE\u6807: mihomo-cli reset ${names.join(" ")}`, "\u91CD\u7F6E\u5168\u90E8: mihomo-cli reset --full"]
8336
8546
  });
8337
8547
  }
8338
8548
  const namedTargets = resolveResetTargets(names);
@@ -8355,7 +8565,7 @@ async function cmdReset(args) {
8355
8565
  if (serviceTargeted && serviceActive) {
8356
8566
  console.log(colors.yellow("\u5C06\u5378\u8F7D launchd \u670D\u52A1\uFF08Mixed \u6A21\u5F0F\u9700\u91CD\u65B0 install \u624D\u80FD\u4F7F\u7528\uFF09"));
8357
8567
  } else if (needsStop && serviceActive) {
8358
- console.log(colors.yellow("\u5C06\u505C\u6B62\u670D\u52A1\u5E76\u5173\u95ED\u767B\u5F55\u81EA\u542F\uFF08\u5B89\u88C5\u4FDD\u7559\uFF0Cmihomo start \u53EF\u91CD\u65B0\u542F\u52A8\uFF09"));
8568
+ console.log(colors.yellow("\u5C06\u505C\u6B62\u670D\u52A1\u5E76\u5173\u95ED\u767B\u5F55\u81EA\u542F\uFF08\u5B89\u88C5\u4FDD\u7559\uFF0Cmihomo-cli start \u53EF\u91CD\u65B0\u542F\u52A8\uFF09"));
8359
8569
  }
8360
8570
  if ((needsStop || serviceTargeted) && legacy) {
8361
8571
  console.log(colors.yellow("\u5C06\u6E05\u7406\u9057\u7559\u7684\u7CFB\u7EDF\u7EA7\u670D\u52A1\uFF08root LaunchDaemon\uFF0C\u9700\u8981\u4E00\u6B21\u7BA1\u7406\u5458\u5BC6\u7801\uFF09"));
@@ -8366,7 +8576,7 @@ async function cmdReset(args) {
8366
8576
  }
8367
8577
  if (!args.includes("-y") && !args.includes("--yes") && !await confirmOrThrow("\u786E\u8BA4?", {
8368
8578
  nonTtyMessage: "\u975E\u4EA4\u4E92\u73AF\u5883\u65E0\u6CD5\u786E\u8BA4",
8369
- hint: ["\u8DF3\u8FC7\u786E\u8BA4\u8BF7\u52A0 -y: mihomo reset ... -y"]
8579
+ hint: ["\u8DF3\u8FC7\u786E\u8BA4\u8BF7\u52A0 -y: mihomo-cli reset ... -y"]
8370
8580
  })) {
8371
8581
  console.log("\u5DF2\u53D6\u6D88");
8372
8582
  return;
@@ -8388,7 +8598,7 @@ async function cmdReset(args) {
8388
8598
  const reason = cleanup.scriptError ? `root \u6B8B\u7559\u6E05\u7406\u672A\u5B8C\u6210\uFF08${describeSudoFailure(cleanup.scriptError)}\uFF09\uFF0C\u53EF\u80FD\u4ECD\u6709\u6B8B\u7559\u8FDB\u7A0B` : cleanup.pidError ? describePidCleanupFailure(cleanup.pidError) : null;
8389
8599
  if (reason) {
8390
8600
  console.warn(colors.yellow(`\u8B66\u544A: ${reason}`));
8391
- console.warn(colors.gray("\u91CD\u8BD5\u6E05\u7406: mihomo stop"));
8601
+ console.warn(colors.gray("\u91CD\u8BD5\u6E05\u7406: mihomo-cli stop"));
8392
8602
  }
8393
8603
  }
8394
8604
  recordServiceStopped();
@@ -8449,14 +8659,14 @@ async function handleLegacyInstall() {
8449
8659
  function printRestoreSkipped() {
8450
8660
  console.log(colors.yellow("\u672A\u6062\u590D\u8FD0\u884C\uFF1A\u5B89\u88C5\u671F\u95F4\u68C0\u6D4B\u5230\u505C\u6B62\u64CD\u4F5C"));
8451
8661
  console.log(colors.gray(" \u53E6\u4E00\u4E2A\u7EC8\u7AEF\u5173\u95ED\u4E86\u670D\u52A1\u81EA\u542F\uFF0C\u5DF2\u6309\u6700\u540E\u4E00\u6761\u547D\u4EE4\u4FDD\u6301\u505C\u6B62"));
8452
- console.log(colors.gray(" \u542F\u52A8: mihomo start"));
8662
+ console.log(colors.gray(" \u542F\u52A8: mihomo-cli start"));
8453
8663
  console.log("");
8454
8664
  }
8455
8665
  async function cmdInstall(args) {
8456
8666
  assertKnownFlags(args.slice(1), [], "install");
8457
- assertPositionalCount(args, 0, 1, "mihomo install");
8667
+ assertPositionalCount(args, 0, 1, "mihomo-cli install");
8458
8668
  if (!hasKernel()) {
8459
- throw new CliError("\u672A\u627E\u5230\u5185\u6838", { hint: "\u4E0B\u8F7D\u5185\u6838: mihomo kernel" });
8669
+ throw new CliError("\u672A\u627E\u5230\u5185\u6838", { hint: "\u4E0B\u8F7D\u5185\u6838: mihomo-cli kernel" });
8460
8670
  }
8461
8671
  await handleLegacyInstall();
8462
8672
  const wasRunning = getServiceStatus().running;
@@ -8480,12 +8690,12 @@ async function cmdInstall(args) {
8480
8690
  }
8481
8691
  throw new CliError(e.message, {
8482
8692
  label: e.label,
8483
- 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 start \u91CD\u8BD5\u3002"]
8693
+ 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"]
8484
8694
  });
8485
8695
  }
8486
8696
  console.log(colors.green("\u5DF2\u6309\u539F\u72B6\u6001\u91CD\u65B0\u542F\u52A8"));
8487
8697
  } else {
8488
- console.log("\u542F\u52A8: mihomo start");
8698
+ console.log("\u542F\u52A8: mihomo-cli start");
8489
8699
  if (!fs14.existsSync(PATHS.configFile)) {
8490
8700
  console.log(colors.gray(" \u5C1A\u65E0\u8FD0\u884C\u65F6\u914D\u7F6E\uFF0Cstart \u4F1A\u5148\u8981\u6C42\u6DFB\u52A0\u8BA2\u9605"));
8491
8701
  }
@@ -8494,7 +8704,7 @@ async function cmdInstall(args) {
8494
8704
  }
8495
8705
  async function cmdUninstall(args) {
8496
8706
  assertKnownFlags(args.slice(1), [], "uninstall");
8497
- assertPositionalCount(args, 0, 1, "mihomo uninstall");
8707
+ assertPositionalCount(args, 0, 1, "mihomo-cli uninstall");
8498
8708
  const status = getServiceStatus();
8499
8709
  const legacy = detectLegacySystemInstall();
8500
8710
  const residue = getMihomoPids();
@@ -8521,9 +8731,9 @@ async function cmdUninstall(args) {
8521
8731
  console.log(colors.yellow(`\u4ECD\u6709\u5185\u6838\u8FDB\u7A0B\u6B8B\u7559 (PID ${remaining.join(", ")})`));
8522
8732
  console.log("\u624B\u52A8\u6E05\u7406: sudo pkill -9 mihomo");
8523
8733
  }
8524
- console.log(colors.gray("\u91CD\u65B0\u5B89\u88C5: mihomo install"));
8734
+ console.log(colors.gray("\u91CD\u65B0\u5B89\u88C5: mihomo-cli install"));
8525
8735
  console.log(colors.gray("\u5F7B\u5E95\u79FB\u9664 mihomo-cli:"));
8526
- console.log(colors.gray(" mihomo reset --full # \u5220\u9664\u5168\u90E8\u6570\u636E\uFF08\u8BA2\u9605/\u5185\u6838/\u65E5\u5FD7\u7B49\uFF09"));
8736
+ console.log(colors.gray(" mihomo-cli reset --full # \u5220\u9664\u5168\u90E8\u6570\u636E\uFF08\u8BA2\u9605/\u5185\u6838/\u65E5\u5FD7\u7B49\uFF09"));
8527
8737
  console.log(colors.gray(" npm uninstall -g mihomo-cli"));
8528
8738
  console.log("");
8529
8739
  }
@@ -8536,7 +8746,7 @@ function handleStopResult(result) {
8536
8746
  }
8537
8747
  async function cmdStop(args) {
8538
8748
  assertKnownFlags(args.slice(1), [], "stop");
8539
- assertPositionalCount(args, 0, 1, "mihomo stop");
8749
+ assertPositionalCount(args, 0, 1, "mihomo-cli stop");
8540
8750
  if (detectLegacySystemInstall()) {
8541
8751
  console.log(colors.yellow("\u68C0\u6D4B\u5230\u65E7\u7248\u672C\u5B89\u88C5\u7684\u7CFB\u7EDF\u7EA7\u670D\u52A1\uFF08root LaunchDaemon\uFF09\uFF0C\u505C\u6B62\u524D\u9700\u6E05\u7406"));
8542
8752
  console.log(colors.gray(" \u6E05\u7406\u9700\u8981\u4E00\u6B21\u7BA1\u7406\u5458\u5BC6\u7801\uFF08\u5220\u9664 root \u62E5\u6709\u7684\u6587\u4EF6\uFF09"));
@@ -8567,7 +8777,7 @@ async function cmdStop(args) {
8567
8777
  hint: ["\u8BF7\u624B\u52A8\u8FD0\u884C: sudo pkill -9 mihomo"]
8568
8778
  });
8569
8779
  }
8570
- console.log(`${colors.green("\u5DF2\u505C\u6B62")}${colors.gray("\uFF08\u5DF2\u5173\u95ED\u767B\u5F55\u81EA\u542F\uFF0Cmihomo start \u53EF\u91CD\u65B0\u542F\u52A8\uFF09")}`);
8780
+ console.log(`${colors.green("\u5DF2\u505C\u6B62")}${colors.gray("\uFF08\u5DF2\u5173\u95ED\u767B\u5F55\u81EA\u542F\uFF0Cmihomo-cli start \u53EF\u91CD\u65B0\u542F\u52A8\uFF09")}`);
8571
8781
  }
8572
8782
 
8573
8783
  // src/commands/subscription.ts
@@ -8575,7 +8785,7 @@ import { spawnSync as spawnSync9 } from "child_process";
8575
8785
  function printRestartHintIfRunning(variant = "update") {
8576
8786
  const state = getRunningState();
8577
8787
  if (state.running) {
8578
- const hintCommand = state.kind === "tun" ? "mihomo start tun" : "mihomo start";
8788
+ const hintCommand = state.kind === "tun" ? "mihomo-cli start tun" : "mihomo-cli start";
8579
8789
  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`;
8580
8790
  console.log(colors.yellow(message));
8581
8791
  console.log("");
@@ -8586,7 +8796,7 @@ function printSubscriptionList() {
8586
8796
  if (subs.length === 0) {
8587
8797
  console.log("\u6CA1\u6709\u8BA2\u9605");
8588
8798
  console.log("");
8589
- console.log("\u6DFB\u52A0\u8BA2\u9605: mihomo sub add <url> [name]");
8799
+ console.log("\u6DFB\u52A0\u8BA2\u9605: mihomo-cli sub add <url> [name]");
8590
8800
  console.log("");
8591
8801
  return;
8592
8802
  }
@@ -8614,13 +8824,13 @@ function printSubscriptionList() {
8614
8824
  }
8615
8825
  });
8616
8826
  if (subs.some((s) => s.updated_at == null) && subs.every((s) => formatTraffic(s.upload, s.download, s.total) === null)) {
8617
- console.log(colors.gray("\u63D0\u793A: \u66F4\u65B0\u8BA2\u9605\u540E\u53EF\u663E\u793A\u6D41\u91CF\u4E0E\u5230\u671F\u4FE1\u606F\uFF08mihomo sub update\uFF1B\u673A\u573A\u4E0D\u4E0B\u53D1\u7528\u91CF\u6570\u636E\u5219\u65E0\u6B64\u4FE1\u606F\uFF09"));
8827
+ console.log(colors.gray("\u63D0\u793A: \u66F4\u65B0\u8BA2\u9605\u540E\u53EF\u663E\u793A\u6D41\u91CF\u4E0E\u5230\u671F\u4FE1\u606F\uFF08mihomo-cli sub update\uFF1B\u673A\u573A\u4E0D\u4E0B\u53D1\u7528\u91CF\u6570\u636E\u5219\u65E0\u6B64\u4FE1\u606F\uFF09"));
8618
8828
  }
8619
8829
  console.log("");
8620
- console.log("\u5207\u6362\u8BA2\u9605: mihomo sub use <name>");
8621
- console.log("\u65B0\u589E\u8BA2\u9605: mihomo sub add <url> [name]");
8622
- console.log("\u66F4\u65B0\u8BA2\u9605: mihomo sub update [name]");
8623
- console.log("\u5220\u9664\u8BA2\u9605: mihomo sub remove <name>");
8830
+ console.log("\u5207\u6362\u8BA2\u9605: mihomo-cli sub use <name>");
8831
+ console.log("\u65B0\u589E\u8BA2\u9605: mihomo-cli sub add <url> [name]");
8832
+ console.log("\u66F4\u65B0\u8BA2\u9605: mihomo-cli sub update [name]");
8833
+ console.log("\u5220\u9664\u8BA2\u9605: mihomo-cli sub remove <name>");
8624
8834
  console.log("");
8625
8835
  }
8626
8836
  function readUrlFromClipboard() {
@@ -8635,7 +8845,7 @@ function readUrlFromClipboard() {
8635
8845
  }
8636
8846
  }
8637
8847
  async function subAdd(args) {
8638
- assertPositionalCount(args, 2, 2, "mihomo sub add <url> [name]");
8848
+ assertPositionalCount(args, 2, 2, "mihomo-cli sub add <url> [name]");
8639
8849
  let url = args[2]?.trim();
8640
8850
  const name = args[3] === void 0 ? "default" : args[3];
8641
8851
  if (!name.trim()) {
@@ -8653,10 +8863,10 @@ async function subAdd(args) {
8653
8863
  url = clipped;
8654
8864
  } else if (process.stdin.isTTY) {
8655
8865
  throw new CliError("\u526A\u8D34\u677F\u4E2D\u6CA1\u6709\u6709\u6548\u7684\u8BA2\u9605 URL", {
8656
- hint: ["\u5148\u590D\u5236\u8BA2\u9605\u94FE\u63A5\u540E\u91CD\u8BD5\uFF0C\u6216\u663E\u5F0F\u4F20\u5165:", " mihomo sub add <url> [name]"]
8866
+ hint: ["\u5148\u590D\u5236\u8BA2\u9605\u94FE\u63A5\u540E\u91CD\u8BD5\uFF0C\u6216\u663E\u5F0F\u4F20\u5165:", " mihomo-cli sub add <url> [name]"]
8657
8867
  });
8658
8868
  } else {
8659
- throw new CliError("\u8BF7\u63D0\u4F9B\u6709\u6548\u7684\u8BA2\u9605 URL", { hint: ["\u7528\u6CD5: mihomo sub add <url> [name]"] });
8869
+ throw new CliError("\u8BF7\u63D0\u4F9B\u6709\u6548\u7684\u8BA2\u9605 URL", { hint: ["\u7528\u6CD5: mihomo-cli sub add <url> [name]"] });
8660
8870
  }
8661
8871
  }
8662
8872
  if (!isValidHttpUrl(url)) {
@@ -8678,14 +8888,14 @@ async function subAdd(args) {
8678
8888
  printSubscriptionList();
8679
8889
  }
8680
8890
  async function subUpdate(args) {
8681
- assertPositionalCount(args, 1, 2, "mihomo sub update [name]");
8891
+ assertPositionalCount(args, 1, 2, "mihomo-cli sub update [name]");
8682
8892
  const nameArg = getNonFlagArg(args, 2);
8683
8893
  const subs = getSubscriptions();
8684
8894
  if (subs.length === 0) {
8685
8895
  throw new CliError("\u6CA1\u6709\u8BA2\u9605");
8686
8896
  }
8687
8897
  if (nameArg === "") {
8688
- throw new CliError("\u8BF7\u6307\u5B9A\u8BA2\u9605\u540D\u79F0", { hint: ["\u66F4\u65B0\u6240\u6709\u8BA2\u9605\u76F4\u63A5\u6267\u884C: mihomo sub update", `\u66F4\u65B0\u6307\u5B9A\u8BA2\u9605: mihomo sub update <\u540D\u79F0>`] });
8898
+ throw new CliError("\u8BF7\u6307\u5B9A\u8BA2\u9605\u540D\u79F0", { hint: ["\u66F4\u65B0\u6240\u6709\u8BA2\u9605\u76F4\u63A5\u6267\u884C: mihomo-cli sub update", `\u66F4\u65B0\u6307\u5B9A\u8BA2\u9605: mihomo-cli sub update <\u540D\u79F0>`] });
8689
8899
  }
8690
8900
  if (nameArg === null) {
8691
8901
  console.log(`\u66F4\u65B0\u6240\u6709 ${subs.length} \u4E2A\u8BA2\u9605...`);
@@ -8705,7 +8915,7 @@ async function subUpdate(args) {
8705
8915
  if (failedResults.length > 0) {
8706
8916
  const allFailed = failedResults.length === results.length;
8707
8917
  throw new CliError(allFailed ? "\u5168\u90E8\u8BA2\u9605\u66F4\u65B0\u5931\u8D25" : `${failedResults.length} \u4E2A\u8BA2\u9605\u66F4\u65B0\u5931\u8D25: ${failedResults.map((r) => r.name).join("\u3001")}`, {
8708
- hint: failedResults.map((r) => `\u91CD\u8BD5: mihomo sub update ${r.name}`)
8918
+ hint: failedResults.map((r) => `\u91CD\u8BD5: mihomo-cli sub update ${r.name}`)
8709
8919
  });
8710
8920
  }
8711
8921
  return;
@@ -8722,12 +8932,12 @@ async function subUpdate(args) {
8722
8932
  printSubscriptionList();
8723
8933
  }
8724
8934
  async function subUse(args) {
8725
- assertPositionalCount(args, 1, 2, "mihomo sub use <name>");
8935
+ assertPositionalCount(args, 1, 2, "mihomo-cli sub use <name>");
8726
8936
  assertRestartOptionValues(args);
8727
8937
  const name = getNonFlagArg(args, 2);
8728
8938
  const subs = getSubscriptions();
8729
8939
  if (subs.length === 0) {
8730
- throw new CliError("\u6CA1\u6709\u8BA2\u9605\uFF0C\u8BF7\u5148\u6DFB\u52A0\u8BA2\u9605", { hint: "mihomo sub add <url> [name]" });
8940
+ throw new CliError("\u6CA1\u6709\u8BA2\u9605\uFF0C\u8BF7\u5148\u6DFB\u52A0\u8BA2\u9605", { hint: "mihomo-cli sub add <url> [name]" });
8731
8941
  }
8732
8942
  if (!name) {
8733
8943
  throw new CliError("\u8BF7\u6307\u5B9A\u8BA2\u9605\u540D\u79F0", {
@@ -8753,11 +8963,11 @@ async function subUse(args) {
8753
8963
  printSubscriptionList();
8754
8964
  }
8755
8965
  async function subRemove(args) {
8756
- assertPositionalCount(args, 1, 2, "mihomo sub remove <name>");
8966
+ assertPositionalCount(args, 1, 2, "mihomo-cli sub remove <name>");
8757
8967
  const name = getNonFlagArg(args, 2);
8758
8968
  const subs = getSubscriptions();
8759
8969
  if (subs.length === 0) {
8760
- throw new CliError("\u6CA1\u6709\u8BA2\u9605\uFF0C\u8BF7\u5148\u6DFB\u52A0\u8BA2\u9605", { hint: "mihomo sub add <url> [name]" });
8970
+ throw new CliError("\u6CA1\u6709\u8BA2\u9605\uFF0C\u8BF7\u5148\u6DFB\u52A0\u8BA2\u9605", { hint: "mihomo-cli sub add <url> [name]" });
8761
8971
  }
8762
8972
  if (!name) {
8763
8973
  throw new CliError("\u8BF7\u6307\u5B9A\u8981\u5220\u9664\u7684\u8BA2\u9605\u540D\u79F0", {
@@ -8771,7 +8981,7 @@ async function subRemove(args) {
8771
8981
  console.log(`\u5C06\u5220\u9664\u8BA2\u9605 "${target.name}" (\u6A21\u7CCA\u5339\u914D "${name}")`);
8772
8982
  if (!await confirmOrThrow("\u6B64\u64CD\u4F5C\u4E0D\u53EF\u6062\u590D\uFF0C\u786E\u8BA4?", {
8773
8983
  nonTtyMessage: `\u6A21\u7CCA\u5339\u914D\u5230 "${target.name}"\uFF0C\u975E\u4EA4\u4E92\u73AF\u5883\u9700\u786E\u8BA4`,
8774
- hint: [`\u8BF7\u7528\u5B8C\u6574\u540D\u79F0: mihomo sub remove ${target.name}`, `\u6216\u8DF3\u8FC7\u786E\u8BA4: mihomo sub remove ${name} -y`]
8984
+ hint: [`\u8BF7\u7528\u5B8C\u6574\u540D\u79F0: mihomo-cli sub remove ${target.name}`, `\u6216\u8DF3\u8FC7\u786E\u8BA4: mihomo-cli sub remove ${name} -y`]
8775
8985
  })) {
8776
8986
  console.log("\u5DF2\u53D6\u6D88");
8777
8987
  return;
@@ -8808,13 +9018,13 @@ async function cmdSubscription(args) {
8808
9018
  if (action.startsWith("-")) {
8809
9019
  throw new CliError(`\u672A\u77E5\u7684\u9009\u9879: ${action}`, {
8810
9020
  label: "\u53C2\u6570\u9519\u8BEF",
8811
- hint: ["\u88F8 sub \u53EA\u5217\u51FA\u8BA2\u9605\uFF0C\u4E0D\u63A5\u53D7\u9009\u9879", "", "\u7528\u6CD5: mihomo sub [use|add|update|remove]\uFF08\u88F8 sub \u5373\u5217\u8868\uFF09"]
9021
+ hint: ["\u88F8 sub \u53EA\u5217\u51FA\u8BA2\u9605\uFF0C\u4E0D\u63A5\u53D7\u9009\u9879", "", "\u7528\u6CD5: mihomo-cli sub [use|add|update|remove]\uFF08\u88F8 sub \u5373\u5217\u8868\uFF09"]
8812
9022
  });
8813
9023
  }
8814
9024
  const names = SUBCOMMANDS3.flatMap((c) => [c.name, ...c.aliases ?? []]);
8815
9025
  const suggestion = suggestSimilar(action, names);
8816
9026
  throw new CliError(`\u672A\u77E5\u7684\u8BA2\u9605\u547D\u4EE4: ${action}`, {
8817
- hint: [...suggestion.length > 0 ? [`\u662F\u5426\u60F3\u8F93\u5165: ${suggestion.join(" / ")}?`] : [], "\u7528\u6CD5: mihomo sub [use|add|update|remove]\uFF08\u88F8 sub \u5373\u5217\u8868\uFF09"]
9027
+ hint: [...suggestion.length > 0 ? [`\u662F\u5426\u60F3\u8F93\u5165: ${suggestion.join(" / ")}?`] : [], "\u7528\u6CD5: mihomo-cli sub [use|add|update|remove]\uFF08\u88F8 sub \u5373\u5217\u8868\uFF09"]
8818
9028
  });
8819
9029
  }
8820
9030
  });
@@ -8839,11 +9049,11 @@ function copyToClipboard(text) {
8839
9049
  }
8840
9050
  function cmdUI(args) {
8841
9051
  assertKnownFlags(args.slice(1), ["-c", "--copy-secret"], "ui [zash|dash|yacd] [-c]");
8842
- assertPositionalCount(args, 1, 1, "mihomo ui [zash|dash|yacd] [-c]");
9052
+ assertPositionalCount(args, 1, 1, "mihomo-cli ui [zash|dash|yacd] [-c]");
8843
9053
  const uiName = resolveUiName(args);
8844
9054
  const copySecret = hasFlag(args, "-c", "--copy-secret");
8845
9055
  if (!getRunningState().running) {
8846
- console.log(colors.yellow("\u63D0\u793A: mihomo \u672A\u8FD0\u884C\uFF0CUI \u6682\u65F6\u65E0\u6CD5\u8FDE\u63A5\uFF08\u5148\u6267\u884C mihomo start \u542F\u52A8\uFF09"));
9056
+ console.log(colors.yellow("\u63D0\u793A: mihomo-cli \u672A\u8FD0\u884C\uFF0CUI \u6682\u65F6\u65E0\u6CD5\u8FDE\u63A5\uFF08\u5148\u6267\u884C mihomo-cli start \u542F\u52A8\uFF09"));
8847
9057
  console.log("");
8848
9058
  }
8849
9059
  const url = UI_URLS[uiName];
@@ -8863,7 +9073,7 @@ function cmdUI(args) {
8863
9073
  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")
8864
9074
  );
8865
9075
  } else {
8866
- console.log(colors.gray("\u5DF2\u914D\u7F6E\u8BBF\u95EE\u5BC6\u94A5\uFF0CUI \u8FDE\u63A5\u65F6\u9700\u8F93\u5165\uFF08mihomo ui -c \u53EF\u590D\u5236\u5230\u526A\u8D34\u677F\uFF09"));
9076
+ console.log(colors.gray("\u5DF2\u914D\u7F6E\u8BBF\u95EE\u5BC6\u94A5\uFF0CUI \u8FDE\u63A5\u65F6\u9700\u8F93\u5165\uFF08mihomo-cli ui -c \u53EF\u590D\u5236\u5230\u526A\u8D34\u677F\uFF09"));
8867
9077
  }
8868
9078
  }
8869
9079
  console.log("");
@@ -8933,7 +9143,7 @@ var COMMANDS = [
8933
9143
  },
8934
9144
  // === 订阅 ===
8935
9145
  {
8936
- // 顶层快捷方式:`mihomo use <name>` = `mihomo subscription use <name>`(与 tun 同范式)
9146
+ // 顶层快捷方式:`mihomo-cli use <name>` = `mihomo-cli subscription use <name>`(与 tun 同范式)
8937
9147
  name: "use",
8938
9148
  aliases: [],
8939
9149
  handler: cmdSubscription,
@@ -8988,7 +9198,7 @@ var COMMANDS = [
8988
9198
  aliases: [],
8989
9199
  handler: cmdKernel,
8990
9200
  group: "system",
8991
- usage: [{ signature: "kernel [--mirror [\u955C\u50CF]] [--proxy <\u7AEF\u53E3|\u5730\u5740>]", description: "\u66F4\u65B0\u5185\u6838\uFF08\u81EA\u52A8\u9009\u62E9\u901A\u9053\uFF1Agh > \u672C\u673A\u4EE3\u7406 > \u76F4\u8FDE\uFF09" }]
9201
+ usage: [{ signature: "kernel [--mirror [\u955C\u50CF]] [--proxy <\u7AEF\u53E3|\u5730\u5740>]", description: "\u66F4\u65B0\u5185\u6838\uFF08\u81EA\u52A8\u9009\u62E9\u901A\u9053\uFF1A\u672C\u673A\u4EE3\u7406 > gh > \u76F4\u8FDE\uFF09" }]
8992
9202
  },
8993
9203
  {
8994
9204
  name: "update",
@@ -9081,26 +9291,29 @@ function readSelfMixedPortEarly() {
9081
9291
  }
9082
9292
  function clearProxyEnv() {
9083
9293
  const selfPort = readSelfMixedPortEarly();
9294
+ const cleared = {};
9084
9295
  for (const key of PROXY_ENV_KEYS) {
9085
9296
  const value = process.env[key];
9086
9297
  if (value && proxyEnvPointsAtSelf(value, selfPort)) {
9298
+ cleared[key] = value;
9087
9299
  delete process.env[key];
9088
9300
  }
9089
9301
  }
9302
+ if (Object.keys(cleared).length > 0) recordClearedProxyEnv(cleared);
9090
9303
  }
9091
9304
  var GUARD_EXEMPT_COMMANDS = /* @__PURE__ */ new Set(["help", "version"]);
9092
9305
  function assertNotRoot(commandName) {
9093
9306
  const uid = process.getuid?.();
9094
9307
  if (uid !== 0) return;
9095
9308
  if (GUARD_EXEMPT_COMMANDS.has(commandName)) return;
9096
- throw new CliError("\u8BF7\u4E0D\u8981\u7528 sudo \u8FD0\u884C mihomo", {
9309
+ throw new CliError("\u8BF7\u4E0D\u8981\u7528 sudo \u8FD0\u884C mihomo-cli", {
9097
9310
  label: "\u8EAB\u4EFD\u9519\u8BEF",
9098
9311
  hint: [
9099
9312
  "\u670D\u52A1\u662F\u7528\u6237\u7EA7 LaunchAgent\uFF08\u57DF gui/<uid>\uFF09\u3002\u4EE5 root \u8FD0\u884C\u65F6\u57DF\u53D8\u6210 gui/0\uFF0C",
9100
9313
  "launchctl \u4E00\u5F8B\u8FD4\u56DE 125\uFF0C\u800C\u6240\u6709\u670D\u52A1\u64CD\u4F5C\u90FD\u4F1A\u628A\u5B83\u5F53\u6210\u300C\u672A\u88C5\u8F7D\u300D\u9759\u9ED8\u8DF3\u8FC7\u2014\u2014",
9101
9314
  "stop \u4F1A\u62A5\u300C\u5DF2\u505C\u6B62\u300D\u4F46\u5185\u6838\u88AB KeepAlive \u62C9\u56DE\u6765\uFF0Cinstall/start \u5219\u88C5\u5230\u9519\u8BEF\u7684\u57DF\u3002",
9102
9315
  "",
9103
- `\u8BF7\u53BB\u6389 sudo \u91CD\u8BD5: mihomo ${commandName}`,
9316
+ `\u8BF7\u53BB\u6389 sudo \u91CD\u8BD5: mihomo-cli ${commandName}`,
9104
9317
  "",
9105
9318
  "TUN \u6A21\u5F0F\u9700\u8981\u7684 root \u6743\u9650\u7531 CLI \u5185\u90E8\u6309\u9700\u7533\u8BF7\uFF0C\u65E0\u9700\u5728\u5916\u5C42\u52A0 sudo\u3002"
9106
9319
  ]
@@ -9147,7 +9360,7 @@ async function main() {
9147
9360
  if (!command) {
9148
9361
  const suggestion = suggestSimilar(token, allCommandTokens());
9149
9362
  throw new CliError(`\u672A\u77E5\u547D\u4EE4: ${token}`, {
9150
- hint: [suggestion.length > 0 ? `\u662F\u5426\u60F3\u8F93\u5165: ${suggestion.join(" / ")}?` : '\u4F7F\u7528 "mihomo help" \u67E5\u770B\u5E2E\u52A9']
9363
+ hint: [suggestion.length > 0 ? `\u662F\u5426\u60F3\u8F93\u5165: ${suggestion.join(" / ")}?` : '\u4F7F\u7528 "mihomo-cli help" \u67E5\u770B\u5E2E\u52A9']
9151
9364
  });
9152
9365
  }
9153
9366
  assertSupportedNodeVersion(command.name);
@@ -9160,7 +9373,7 @@ async function main() {
9160
9373
  }
9161
9374
  if (command.group === "meta") {
9162
9375
  assertKnownFlags(args.slice(1), [], command.name);
9163
- assertPositionalCount(args, command.name === "help" ? 1 : 0, 1, `mihomo ${command.name}`);
9376
+ assertPositionalCount(args, command.name === "help" ? 1 : 0, 1, `mihomo-cli ${command.name}`);
9164
9377
  }
9165
9378
  if (command.group !== "meta" && (args[1] === "-h" || args[1] === "--help" || args[1] === "help")) {
9166
9379
  printCommandHelp(command);