mihomo-cli 26.10.98 → 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 +36 -1
  2. package/README.md +117 -114
  3. package/dist/index.js +401 -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
  }
@@ -3508,7 +3511,7 @@ var DIRECTORY_TARGETS = {
3508
3511
  root: { path: null, label: "\u6839\u76EE\u5F55" },
3509
3512
  subs: { path: DIRS.subscriptions, label: "\u8BA2\u9605\u76EE\u5F55" },
3510
3513
  logs: { path: DIRS.logs, label: "\u65E5\u5FD7\u76EE\u5F55" },
3511
- data: { path: DIRS.data, label: "mihomo \u6570\u636E\u76EE\u5F55" },
3514
+ data: { path: DIRS.data, label: "mihomo-cli \u6570\u636E\u76EE\u5F55" },
3512
3515
  runtime: { path: DIRS.runtime, label: "\u8FD0\u884C\u65F6\u76EE\u5F55" },
3513
3516
  kernel: { path: DIRS.kernel, label: "\u5185\u6838\u76EE\u5F55" }
3514
3517
  };
@@ -3718,7 +3721,7 @@ function getPorts(settings = readSettings()) {
3718
3721
  function maskSingleUrl(url) {
3719
3722
  try {
3720
3723
  const parsed = new URL(url);
3721
- const tokenKeys = /* @__PURE__ */ new Set([
3724
+ const TOKEN_KEY_NAMES = [
3722
3725
  "token",
3723
3726
  "key",
3724
3727
  "secret",
@@ -3735,9 +3738,12 @@ function maskSingleUrl(url) {
3735
3738
  "email",
3736
3739
  "passwd",
3737
3740
  "apikey",
3738
- "api_key",
3739
3741
  "access"
3740
- ]);
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);
3741
3747
  for (const [key, value] of parsed.searchParams) {
3742
3748
  if (tokenKeys.has(key.toLowerCase()) || value.length >= 16) {
3743
3749
  parsed.searchParams.set(key, "***");
@@ -3826,7 +3832,7 @@ function getSubscriptions(settings = readSettings()) {
3826
3832
  const list = settings.subscriptions;
3827
3833
  if (!Array.isArray(list)) {
3828
3834
  if (list !== void 0) {
3829
- 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");
3830
3836
  }
3831
3837
  return [];
3832
3838
  }
@@ -3851,7 +3857,7 @@ function addSubscription(url, name = "default") {
3851
3857
  updateSettings((settings) => {
3852
3858
  const subs = getSubscriptions(settings);
3853
3859
  if (subs.some((s) => s.name === name)) {
3854
- 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`);
3855
3861
  }
3856
3862
  subs.push({ name, url });
3857
3863
  const updates = { subscriptions: subs };
@@ -3946,7 +3952,7 @@ function assertValidParsedKey(rawKey, parsed) {
3946
3952
  if (rawKey.includes("<") || rawKey.includes(">")) {
3947
3953
  throw new CliError(`\u8986\u5199\u952E "${rawKey}" \u7528\u4E86\u5DF2\u79FB\u9664\u7684\u5C16\u62EC\u53F7\u8F6C\u4E49`, {
3948
3954
  label: "\u8986\u5199\u914D\u7F6E\u9519\u8BEF",
3949
- 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"]
3950
3956
  });
3951
3957
  }
3952
3958
  if (parsed.key === "") {
@@ -3980,7 +3986,7 @@ function describeValueKind(value) {
3980
3986
  function protoKeyInOverwriteError() {
3981
3987
  return new CliError('\u8986\u5199\u91CC\u51FA\u73B0\u4E86 "__proto__" \u952E', {
3982
3988
  label: "\u8986\u5199\u914D\u7F6E\u9519\u8BEF",
3983
- 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"]
3984
3990
  });
3985
3991
  }
3986
3992
  function mergeConfigLevel(target, override, parseOperators) {
@@ -4279,7 +4285,7 @@ function toBrokenFile(file, filePath, e) {
4279
4285
  return { name: file, path: filePath, label: e.label, message: e.message, hint: e.hint };
4280
4286
  }
4281
4287
  const message = e.message || String(e);
4282
- 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"];
4283
4289
  if (/alias/i.test(message)) {
4284
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"');
4285
4291
  }
@@ -4396,7 +4402,7 @@ function applyOverwrite(baseConfig, files, opts) {
4396
4402
  } catch (e) {
4397
4403
  throw new CliError(`\u8986\u5199\u811A\u672C "${file.name}" \u6267\u884C\u5931\u8D25: ${e.message?.split("\n")[0] ?? String(e)}`, {
4398
4404
  label: "\u8986\u5199\u914D\u7F6E\u9519\u8BEF",
4399
- 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"]
4400
4406
  });
4401
4407
  }
4402
4408
  if (returned != null && typeof returned.then === "function") {
@@ -4442,7 +4448,7 @@ function shellQuote(s) {
4442
4448
  }
4443
4449
  function sanitizeTerminal(s) {
4444
4450
  const ansiEscape = /\x1b\[[0-9;]*[a-zA-Z]/g;
4445
- const controlChars = /[\x00-\x08\x0b\x0c\x0e-\x1f]/g;
4451
+ const controlChars = /[\x00-\x08\x0b\x0c\x0d\x0e-\x1f]/g;
4446
4452
  return s.replace(ansiEscape, "").replace(controlChars, "");
4447
4453
  }
4448
4454
 
@@ -4454,16 +4460,16 @@ function loadYamlSafe(content) {
4454
4460
  function parseConfigContent(content, errorMsg) {
4455
4461
  const label = errorMsg || "\u5185\u5BB9";
4456
4462
  if (!content?.trim()) {
4457
- throw new Error(`${label}\u4E3A\u7A7A`);
4463
+ throw new CliError(`${label}\u4E3A\u7A7A`, { label: "\u914D\u7F6E\u9519\u8BEF" });
4458
4464
  }
4459
4465
  let result;
4460
4466
  try {
4461
4467
  result = loadYamlSafe(content);
4462
4468
  } catch (e) {
4463
- 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" });
4464
4470
  }
4465
4471
  if (result == null || typeof result !== "object" || Array.isArray(result)) {
4466
- 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" });
4467
4473
  }
4468
4474
  return result;
4469
4475
  }
@@ -4518,7 +4524,7 @@ function assertConfigShape(config) {
4518
4524
  if (typeof name !== "string" || name === "") {
4519
4525
  throw new CliError(`${key}[${i}] \u7F3A\u5C11\u6709\u6548\u7684 name`, {
4520
4526
  label: "\u914D\u7F6E\u9519\u8BEF",
4521
- 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`]
4522
4528
  });
4523
4529
  }
4524
4530
  } else if (typeof item !== "string") {
@@ -4624,7 +4630,7 @@ function buildConfig(subRawContent, mode, scope) {
4624
4630
  return { config: merged, warnings: lockedWarnings, overwriteSummaries };
4625
4631
  }
4626
4632
  function renderLockedWarning(source, keys) {
4627
- 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`;
4628
4634
  }
4629
4635
  function writeMihomoConfig(configObj) {
4630
4636
  ensureDirs();
@@ -4647,12 +4653,12 @@ function buildKernelRejectHint(detail, overwriteSummaries, opts = {}) {
4647
4653
  `${HINT_INDENT}\u8BF7\u4FEE\u6B63\u8BA2\u9605\u6216\u8986\u5199\uFF1B\u5F53\u524D\u8FD0\u884C\u65F6\u914D\u7F6E\u672A\u6539\u52A8\u3002`,
4648
4654
  // 「未知键/字段」类报错的另一个真实根因是内核落后于订阅:机场开始用新协议字段,
4649
4655
  // 旧内核 -t 一律按不认识拒绝。只提示「修正订阅或覆写」会把方向带反
4650
- `${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`
4651
4657
  );
4652
4658
  return hint;
4653
4659
  }
4654
4660
  async function validateConfigWithKernel(config, overwriteSummaries) {
4655
- 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" });
4656
4662
  ensureDirs();
4657
4663
  const stageDir = fs4.mkdtempSync(path4.join(DIRS.runtime, "check-"));
4658
4664
  const stageFile = path4.join(stageDir, "config.yaml");
@@ -4753,10 +4759,11 @@ function formatBytes(bytes) {
4753
4759
  }
4754
4760
  function formatTraffic(upload, download, total) {
4755
4761
  if (download === void 0 && total === void 0) return null;
4756
- const used = (upload || 0) + (download || 0);
4762
+ const used = Number(upload || 0) + Number(download || 0);
4763
+ const totalNum = total === void 0 ? NaN : Number(total);
4757
4764
  let line = `${formatBytes(used)} / ${formatBytes(total)}`;
4758
- if (total && total > 0) {
4759
- 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)}%)`;
4760
4767
  }
4761
4768
  return line;
4762
4769
  }
@@ -4958,10 +4965,6 @@ function listLogs() {
4958
4965
  return result;
4959
4966
  }
4960
4967
 
4961
- // src/process-stop.ts
4962
- import { spawnSync as spawnSync4 } from "child_process";
4963
- import fs8 from "fs";
4964
-
4965
4968
  // src/process-probe.ts
4966
4969
  import { spawnSync as spawnSync2 } from "child_process";
4967
4970
  import fs6 from "fs";
@@ -5016,13 +5019,13 @@ function isMihomoProcess(pid) {
5016
5019
  function getMihomoPids() {
5017
5020
  const result = spawnSync2("pgrep", ["-f", MAIN_INSTANCE_PATTERN], { encoding: "utf8", timeout: 1e4 });
5018
5021
  if (result.error || result.status === null) {
5019
- throw new CliError("\u65E0\u6CD5\u63A2\u6D4B mihomo \u8FDB\u7A0B\uFF08pgrep \u6267\u884C\u5931\u8D25\uFF09", {
5020
- 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"]
5021
5024
  });
5022
5025
  }
5023
5026
  if (result.status !== 0 && result.status !== 1) {
5024
5027
  throw new CliError(`\u8FDB\u7A0B\u63A2\u6D4B\u5931\u8D25\uFF08pgrep \u9000\u51FA\u7801 ${result.status}\uFF09`, {
5025
- 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)
5026
5029
  });
5027
5030
  }
5028
5031
  const output = (result.stdout || "").trim();
@@ -5071,6 +5074,10 @@ function getStatus() {
5071
5074
  };
5072
5075
  }
5073
5076
 
5077
+ // src/process-stop.ts
5078
+ import { spawnSync as spawnSync4 } from "child_process";
5079
+ import fs8 from "fs";
5080
+
5074
5081
  // src/sudo.ts
5075
5082
  import { spawnSync as spawnSync3 } from "child_process";
5076
5083
  import fs7 from "fs";
@@ -5243,7 +5250,7 @@ async function stop() {
5243
5250
  if (result.remaining.length === 0) {
5244
5251
  warnPidCleanupFailed(result.pidError);
5245
5252
  if (result.scriptError) {
5246
- 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`));
5247
5254
  }
5248
5255
  }
5249
5256
  const remaining = getMihomoPids();
@@ -5344,10 +5351,14 @@ function getServiceStatus(options = {}) {
5344
5351
  async function waitServiceHealthy() {
5345
5352
  const deadline = Date.now() + SERVICE_OBSERVE_MS;
5346
5353
  const graceDeadline = deadline + SERVICE_HEALTH_GRACE_MS;
5347
- let last;
5354
+ let last = null;
5348
5355
  do {
5349
5356
  await sleep(SERVICE_HEALTH_INTERVAL_MS);
5350
- last = getServiceStatus({ withDisabled: false });
5357
+ try {
5358
+ last = getServiceStatus({ withDisabled: false });
5359
+ } catch {
5360
+ continue;
5361
+ }
5351
5362
  if (isCrashed(last)) {
5352
5363
  return { healthy: false, crashed: true, pid: null, exitCode: last.lastExitCode, terminatingSignal: last.lastTerminatingSignal };
5353
5364
  }
@@ -5355,18 +5366,34 @@ async function waitServiceHealthy() {
5355
5366
  return { healthy: false, crashed: false, pid: null, exitCode: last.lastExitCode, terminatingSignal: last.lastTerminatingSignal };
5356
5367
  }
5357
5368
  } while (Date.now() < deadline);
5369
+ if (last === null) return healthViaProcessProbe();
5358
5370
  if (last.running) return { healthy: true, crashed: false, pid: last.pid, exitCode: null, terminatingSignal: null };
5371
+ let graceQueried = false;
5359
5372
  while (Date.now() < graceDeadline) {
5360
5373
  await sleep(SERVICE_HEALTH_INTERVAL_MS);
5361
- last = getServiceStatus({ withDisabled: false });
5374
+ try {
5375
+ last = getServiceStatus({ withDisabled: false });
5376
+ } catch {
5377
+ continue;
5378
+ }
5379
+ graceQueried = true;
5362
5380
  if (isCrashed(last)) {
5363
5381
  return { healthy: false, crashed: true, pid: null, exitCode: last.lastExitCode, terminatingSignal: last.lastTerminatingSignal };
5364
5382
  }
5365
5383
  if (!last.loaded) break;
5366
5384
  if (last.running) return { healthy: true, crashed: false, pid: last.pid, exitCode: null, terminatingSignal: null };
5367
5385
  }
5386
+ if (!graceQueried) return healthViaProcessProbe();
5368
5387
  return { healthy: false, crashed: false, pid: last.pid, exitCode: last.lastExitCode, terminatingSignal: last.lastTerminatingSignal };
5369
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
+ }
5370
5397
  function describeExitCause(exitCode, terminatingSignal) {
5371
5398
  if (terminatingSignal !== null) return `\u88AB\u4FE1\u53F7\u7EC8\u6B62\uFF08${terminatingSignal}\uFF09`;
5372
5399
  if (exitCode !== null && exitCode !== 0) return `\u9000\u51FA\u7801 ${exitCode}`;
@@ -5413,7 +5440,7 @@ ${argsXml}
5413
5440
  }
5414
5441
  function ensureServiceSymlink() {
5415
5442
  if (!fs9.existsSync(PATHS.mihomoBinary)) {
5416
- 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" });
5417
5444
  }
5418
5445
  try {
5419
5446
  const current = fs9.readlinkSync(PATHS.serviceBinary);
@@ -5584,10 +5611,10 @@ async function startService() {
5584
5611
  assertServiceLabelSafe();
5585
5612
  ensureServiceSymlink();
5586
5613
  if (!isServiceInstalled()) {
5587
- 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" });
5588
5615
  }
5589
5616
  if (!fs9.existsSync(PATHS.configFile)) {
5590
- 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>" });
5591
5618
  }
5592
5619
  if (getConfigInfo()?.tun) {
5593
5620
  throw new CliError("\u8FD0\u884C\u65F6\u914D\u7F6E\u4E3A TUN \u6A21\u5F0F\uFF0C\u670D\u52A1\u65E0\u6CD5\u4F7F\u7528", {
@@ -5595,12 +5622,12 @@ async function startService() {
5595
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",
5596
5623
  "\u5F3A\u884C\u542F\u52A8\u53EA\u4F1A\u8BA9\u5185\u6838\u53CD\u590D\u5D29\u6E83\u91CD\u542F\u3002",
5597
5624
  "",
5598
- "\u6309 Mixed \u91CD\u5EFA\u914D\u7F6E\u5E76\u542F\u52A8: mihomo start mixed",
5599
- "\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"
5600
5627
  ]
5601
5628
  });
5602
5629
  }
5603
- 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" });
5604
5631
  bootoutService();
5605
5632
  await waitUntilUnloaded();
5606
5633
  rotateAndCleanupLogs();
@@ -5632,7 +5659,7 @@ async function stopService() {
5632
5659
  disableServiceAutoStart(SERVICE_LOCK_LAUNCHCTL_TIMEOUT_MS);
5633
5660
  });
5634
5661
  await waitUntilUnloaded();
5635
- 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" });
5636
5663
  }
5637
5664
  async function uninstallService() {
5638
5665
  assertServiceLabelSafe();
@@ -5646,7 +5673,7 @@ async function uninstallService() {
5646
5673
  } catch (e) {
5647
5674
  throw new CliError(`\u5220\u9664 plist \u5931\u8D25\uFF08${PATHS.userAgentPlist}\uFF09: ${e.message}`);
5648
5675
  }
5649
- await cleanupKernelsOrThrow({ mainOutcome: "\u670D\u52A1\u5DF2\u5378\u8F7D", retryCommand: "mihomo stop" });
5676
+ await cleanupKernelsOrThrow({ mainOutcome: "\u670D\u52A1\u5DF2\u5378\u8F7D", retryCommand: "mihomo-cli stop" });
5650
5677
  try {
5651
5678
  fs9.rmSync(PATHS.serviceBinary, { force: true });
5652
5679
  } catch {
@@ -5664,6 +5691,12 @@ function buildLegacyCleanupScript() {
5664
5691
  ` exit 3`,
5665
5692
  `fi`,
5666
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`,
5667
5700
  `chown "$SUDO_UID:$SUDO_GID" ${shellQuote(PATHS.logFile)} 2>/dev/null || true`,
5668
5701
  `chown -R "$SUDO_UID:$SUDO_GID" ${shellQuote(DIRS.data)} 2>/dev/null || true`,
5669
5702
  "exit 0",
@@ -5676,8 +5709,9 @@ function cleanupLegacySystemInstall() {
5676
5709
  action: "\u6E05\u7406\u9057\u7559\u7684\u7CFB\u7EDF\u7EA7\u670D\u52A1",
5677
5710
  file: "legacy-cleanup.sh",
5678
5711
  // 3 = 脚本内 bootout 真实失败(见 buildLegacyCleanupScript 的分级);
5712
+ // 4 = plist rm 后复核仍存在(rm -f 静默失败的可见化);
5679
5713
  // 具体退出码已由脚本 echo 到终端,故只指向「上方输出」
5680
- 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" }
5681
5715
  });
5682
5716
  }
5683
5717
  async function cleanupLegacyInstallOrThrow() {
@@ -5739,7 +5773,7 @@ function concludeHotReload(stopEpochBefore, stopEpochNow) {
5739
5773
  }
5740
5774
  async function restartService() {
5741
5775
  if (!isServiceInstalled()) {
5742
- 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" });
5743
5777
  }
5744
5778
  if (!logOversized() && await tryHotReload()) {
5745
5779
  return concludeHotReload(stopEpochBaseline(), readStopEpoch());
@@ -5804,14 +5838,14 @@ function suggestSimilar(input, candidates) {
5804
5838
  // src/commands/help.ts
5805
5839
  function printShortHelp() {
5806
5840
  console.log(`
5807
- ${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)
5808
5842
  `);
5809
5843
  const steps = [];
5810
- if (!hasKernel()) steps.push(["mihomo kernel", "\u4E0B\u8F7D\u5185\u6838"]);
5811
- if (getSubscriptions().length === 0) steps.push(["mihomo sub add <url>", "\u6DFB\u52A0\u8BA2\u9605"]);
5812
- 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"]);
5813
5847
  if (steps.length > 0) {
5814
- steps.push(["mihomo start", "\u542F\u52A8\u4EE3\u7406"]);
5848
+ steps.push(["mihomo-cli start", "\u542F\u52A8\u4EE3\u7406"]);
5815
5849
  const width = Math.max(...steps.map(([cmd]) => displayWidth(cmd)));
5816
5850
  console.log("\u5F00\u59CB\u4F7F\u7528:");
5817
5851
  steps.forEach(([cmd, desc], i) => {
@@ -5838,7 +5872,7 @@ var GROUP_TITLES = [
5838
5872
  ];
5839
5873
  function printHelp(commands) {
5840
5874
  const lines = [`
5841
- ${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]"];
5842
5876
  const allUsage = commands.flatMap((c) => c.usage);
5843
5877
  const signatureWidth = Math.max(...allUsage.map((u) => displayWidth(u.signature)));
5844
5878
  const formatUsage = (u) => ` ${colors.bold(padEndDisplay(u.signature, signatureWidth))} ${u.description}`;
@@ -5858,17 +5892,19 @@ ${colors.cyan(colors.bold(`mihomo-cli v${VERSION}`))}`, "", "\u547D\u4EE4\u522B\
5858
5892
  `${colors.cyan("\u793A\u4F8B:")}`,
5859
5893
  // 顺序即首次使用的依赖顺序(kernel → 订阅 → 服务 → 启动):install 在无内核时会被
5860
5894
  // 拦下提示先跑 kernel,示例若把 install 放第一条,照着敲的新用户第一步就撞墙
5861
- " mihomo kernel # \u4E0B\u8F7D\u5185\u6838\uFF08\u9996\u6B21\u4F7F\u7528\u7684\u7B2C\u4E00\u6B65\uFF09",
5862
- " mihomo sub add <url> # \u6DFB\u52A0\u8BA2\u9605 (sub \u662F subscription \u522B\u540D)",
5863
- " mihomo install # \u5B89\u88C5\u670D\u52A1\uFF08Mixed \u6A21\u5F0F\u7684\u524D\u7F6E\uFF0C\u53EA\u9700\u4E00\u6B21\uFF09",
5864
- " mihomo start # \u542F\u52A8\u4EE3\u7406\u5E76\u5F00\u542F\u767B\u5F55\u81EA\u542F",
5865
- " mihomo stop # \u505C\u6B62\u5E76\u5173\u95ED\u767B\u5F55\u81EA\u542F",
5866
- " mihomo start tun # \u4E34\u65F6 TUN \u900F\u660E\u4EE3\u7406\uFF08\u4E0D\u8D70\u670D\u52A1\uFF0C\u9700 sudo\uFF09",
5867
- " mihomo start -s # \u8DF3\u8FC7\u81EA\u52A8\u66F4\u65B0\u8BA2\u9605",
5868
- " 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",
5869
5903
  "",
5870
5904
  `${colors.cyan("\u5FEB\u6377\u547D\u4EE4:")}`,
5871
- " tun = start tun",
5905
+ " tun = start tun",
5906
+ " use <name> = subscription use <name>",
5907
+ " restart = start\uFF08start \u672C\u8EAB\u5373\u91CD\u542F\uFF09",
5872
5908
  "",
5873
5909
  `${colors.cyan("\u6A21\u5F0F\u8BF4\u660E:")}`,
5874
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",
@@ -5882,7 +5918,7 @@ ${colors.cyan(colors.bold(`mihomo-cli v${VERSION}`))}`, "", "\u547D\u4EE4\u522B\
5882
5918
  }
5883
5919
  function printCommandHelp(command) {
5884
5920
  const aliasText = command.aliases.length > 0 ? colors.gray(`\uFF08\u522B\u540D: ${command.aliases.join(", ")}\uFF09`) : "";
5885
- const lines = ["", `${colors.cyan(colors.bold(`mihomo ${command.name}`))}${aliasText}`];
5921
+ const lines = ["", `${colors.cyan(colors.bold(`mihomo-cli ${command.name}`))}${aliasText}`];
5886
5922
  if (command.usage.length > 0) {
5887
5923
  lines.push("", "\u7528\u6CD5:");
5888
5924
  const width = Math.max(...command.usage.map((u) => displayWidth(u.signature)));
@@ -5890,7 +5926,7 @@ function printCommandHelp(command) {
5890
5926
  lines.push(` ${colors.bold(padEndDisplay(u.signature, width))} ${u.description}`);
5891
5927
  }
5892
5928
  } else {
5893
- 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"));
5894
5930
  }
5895
5931
  console.log(lines.join("\n"));
5896
5932
  }
@@ -5907,7 +5943,7 @@ function printHelpEntry(args, commands, finder) {
5907
5943
  commands.flatMap((c) => [c.name, ...c.aliases])
5908
5944
  );
5909
5945
  throw new CliError(`\u672A\u77E5\u547D\u4EE4: ${token}`, {
5910
- 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']
5911
5947
  });
5912
5948
  }
5913
5949
  printCommandHelp(command);
@@ -5920,7 +5956,7 @@ function printVersion() {
5920
5956
  }
5921
5957
 
5922
5958
  // src/redact.ts
5923
- 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"]);
5924
5960
  var URL_PROVIDER_KEYS = /* @__PURE__ */ new Set(["proxy-providers", "rule-providers"]);
5925
5961
  function walk(node, withinProvider, state) {
5926
5962
  if (Array.isArray(node)) {
@@ -6152,7 +6188,7 @@ function getActiveSubscription() {
6152
6188
  function requireActiveSubscription() {
6153
6189
  const sub = getActiveSubscription();
6154
6190
  if (!sub) {
6155
- 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>" });
6156
6192
  }
6157
6193
  return sub;
6158
6194
  }
@@ -6258,7 +6294,7 @@ async function prepareConfigForStart(mode, subName = "default") {
6258
6294
  const rawContent = readSubscriptionRawConfig(subName);
6259
6295
  if (!rawContent) {
6260
6296
  throw new CliError(`\u8BA2\u9605 "${subName}" \u6709\u6761\u76EE\u4F46\u6CA1\u6709\u672C\u5730\u914D\u7F6E\u6587\u4EF6`, {
6261
- hint: `\u66F4\u65B0\u8BA2\u9605: mihomo sub update ${subName}`
6297
+ hint: `\u66F4\u65B0\u8BA2\u9605: mihomo-cli sub update ${subName}`
6262
6298
  });
6263
6299
  }
6264
6300
  const subUrl = getSubscriptions().find((s) => s.name === subName)?.url;
@@ -6357,16 +6393,16 @@ async function autoUpdateStaleSubscription(options = {}) {
6357
6393
  // src/commands/config.ts
6358
6394
  function cmdConfig(args = []) {
6359
6395
  assertKnownFlags(args.slice(1), ["-j", "--json", "--reveal"], "config [--json] [--reveal]");
6360
- assertPositionalCount(args, 0, 1, "mihomo config [--json] [--reveal]");
6396
+ assertPositionalCount(args, 0, 1, "mihomo-cli config [--json] [--reveal]");
6361
6397
  const asJson = hasFlag(args, "-j", "--json");
6362
6398
  const reveal = hasFlag(args, "--reveal");
6363
6399
  const active = getActiveSubscription();
6364
6400
  if (!active) {
6365
- 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>" });
6366
6402
  }
6367
6403
  const rawContent = readSubscriptionRawConfig(active.name);
6368
6404
  if (!rawContent) {
6369
- 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}` });
6370
6406
  }
6371
6407
  const mode = getConfigInfo()?.tun ? "tun" : "mixed";
6372
6408
  const { config, warnings } = buildConfig(rawContent, mode, { subName: active.name, subUrl: active.url });
@@ -6510,14 +6546,14 @@ exit 2
6510
6546
  async function startTun() {
6511
6547
  ensureDirs();
6512
6548
  if (!fs10.existsSync(PATHS.mihomoBinary)) {
6513
- 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" });
6514
6550
  }
6515
6551
  if (!fs10.existsSync(PATHS.configFile)) {
6516
6552
  throw new CliError("\u672A\u627E\u5230\u914D\u7F6E\u6587\u4EF6\uFF0C\u8BF7\u5148\u6DFB\u52A0\u8BA2\u9605\u5E76\u542F\u52A8");
6517
6553
  }
6518
6554
  if (getServiceStatus().loaded) {
6519
6555
  throw new CliError("\u53E6\u4E00\u7EC8\u7AEF\u5DF2\u542F\u52A8 Mixed \u670D\u52A1\uFF0CTUN \u672A\u542F\u52A8", {
6520
- 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"]
6521
6557
  });
6522
6558
  }
6523
6559
  rotateAndCleanupLogs();
@@ -6551,7 +6587,7 @@ async function startTun() {
6551
6587
  if (!finalPid || !isRunning()) {
6552
6588
  const tail = readLogTail();
6553
6589
  throw new CliError("TUN \u542F\u52A8\u5931\u8D25\uFF08\u5185\u6838\u672A\u80FD\u4FDD\u6301\u8FD0\u884C\uFF09", {
6554
- 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"]
6555
6591
  });
6556
6592
  }
6557
6593
  return { success: true, pid: finalPid, mode: "tun" };
@@ -6582,6 +6618,7 @@ function restartModeOnChange() {
6582
6618
  }
6583
6619
  async function launchOrRestart(mode) {
6584
6620
  if (mode === "tun") {
6621
+ assertTunStartNotRaced();
6585
6622
  const result = await startTun();
6586
6623
  return result.pid;
6587
6624
  }
@@ -6594,7 +6631,13 @@ async function launchOrRestart(mode) {
6594
6631
  ({ started } = await startService());
6595
6632
  }
6596
6633
  if (!started) throw cancelledByConcurrentStop();
6597
- if (hotReloaded) return getServiceStatus().pid;
6634
+ if (hotReloaded) {
6635
+ try {
6636
+ return getServiceStatus().pid;
6637
+ } catch {
6638
+ return null;
6639
+ }
6640
+ }
6598
6641
  try {
6599
6642
  return await assertServiceHealthy();
6600
6643
  } catch (e) {
@@ -6606,12 +6649,21 @@ function cancelledByConcurrentStop() {
6606
6649
  return new CliError("\u542F\u52A8\u5DF2\u53D6\u6D88\uFF1A\u671F\u95F4\u68C0\u6D4B\u5230\u505C\u6B62\u64CD\u4F5C", {
6607
6650
  label: "\u542F\u52A8\u5931\u8D25",
6608
6651
  hint: [
6609
- "\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",
6610
6653
  "",
6611
- "\u786E\u5B9E\u8981\u542F\u52A8: mihomo start"
6654
+ "\u786E\u5B9E\u8981\u542F\u52A8: mihomo-cli start"
6612
6655
  ]
6613
6656
  });
6614
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
+ }
6615
6667
  async function assertServiceHealthy(label = "\u542F\u52A8\u5931\u8D25") {
6616
6668
  const health = await waitServiceHealthy();
6617
6669
  if (health.healthy) return health.pid;
@@ -6621,10 +6673,10 @@ async function assertServiceHealthy(label = "\u542F\u52A8\u5931\u8D25") {
6621
6673
  throw new CliError(reason, {
6622
6674
  label,
6623
6675
  hint: [
6624
- ...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"] : [],
6625
6677
  ...tail.length > 0 ? ["", "--- \u65E5\u5FD7\u5C3E\u90E8 ---", ...tail] : ["", `\u65E5\u5FD7: ${PATHS.logFile}`],
6626
6678
  "",
6627
- "\u5B8C\u6574\u65E5\u5FD7: mihomo logs 0"
6679
+ "\u5B8C\u6574\u65E5\u5FD7: mihomo-cli logs 0"
6628
6680
  ]
6629
6681
  });
6630
6682
  }
@@ -6635,7 +6687,7 @@ function isLoopbackHost(host) {
6635
6687
  const h = host.toLowerCase();
6636
6688
  return h === "127.0.0.1" || h === "localhost" || h === "::1" || h === "0.0.0.0" || h === "::";
6637
6689
  }
6638
- function proxyEnvPointsAtSelf(value, selfPort) {
6690
+ function parseProxyEndpoint(value) {
6639
6691
  let parsed;
6640
6692
  try {
6641
6693
  parsed = new URL(value);
@@ -6644,12 +6696,25 @@ function proxyEnvPointsAtSelf(value, selfPort) {
6644
6696
  try {
6645
6697
  parsed = new URL(`http://${value}`);
6646
6698
  } catch {
6647
- return false;
6699
+ return null;
6648
6700
  }
6649
6701
  }
6650
6702
  const host = parsed.hostname.replace(/^\[|\]$/g, "");
6651
- if (!isLoopbackHost(host)) return false;
6652
- 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 };
6653
6718
  }
6654
6719
  function parseScutilProxy(stdout) {
6655
6720
  const values = /* @__PURE__ */ new Map();
@@ -6707,12 +6772,12 @@ function printSystemProxyHint(mixedPort) {
6707
6772
  }
6708
6773
  if (summary.pac) {
6709
6774
  const desc = summary.pac.wpad ? "WPAD \u81EA\u52A8\u53D1\u73B0" : `PAC \u6587\u4EF6${summary.pac.source ? `\uFF08${summary.pac.source}\uFF09` : ""}`;
6710
- 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}`));
6711
6776
  return;
6712
6777
  }
6713
6778
  if (summary.matched) {
6714
6779
  if (summary.diverged.length > 0) {
6715
- 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`));
6716
6781
  } else {
6717
6782
  console.log(colors.gray(`\u7CFB\u7EDF\u4EE3\u7406\u5DF2\u6307\u5411 127.0.0.1:${mixedPort}`));
6718
6783
  }
@@ -6775,7 +6840,7 @@ async function probeProxyConnectivityUncached(port) {
6775
6840
  function connectivityHint(urgency) {
6776
6841
  if (urgency === "expired") return "\u8BA2\u9605\u5DF2\u8FC7\u671F\uFF0C\u8BF7\u7EED\u8D39\u6216\u66F4\u6362\u8BA2\u9605";
6777
6842
  if (urgency === "traffic-exhausted") return "\u8BA2\u9605\u6D41\u91CF\u5DF2\u7528\u5C3D\uFF0C\u8BF7\u7EED\u8D39\u6216\u66F4\u6362\u8BA2\u9605";
6778
- 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)";
6779
6844
  }
6780
6845
  function trafficColor(line, urgency, total, upload, download) {
6781
6846
  if (urgency === "traffic-exhausted") return colors.red(line);
@@ -6842,7 +6907,7 @@ function buildStatusJson(args) {
6842
6907
  }
6843
6908
  async function printStatus(args = []) {
6844
6909
  assertKnownFlags(args, ["-j", "--json", "--no-probe"], "status [--json] [--no-probe]");
6845
- assertPositionalCount(args, 0, 1, "mihomo status [-j|--json] [--no-probe]");
6910
+ assertPositionalCount(args, 0, 1, "mihomo-cli status [-j|--json] [--no-probe]");
6846
6911
  const asJson = hasFlag(args, "-j", "--json");
6847
6912
  const skipProbe = hasFlag(args, "--no-probe");
6848
6913
  const service = getServiceStatus();
@@ -6916,11 +6981,11 @@ async function printStatus(args = []) {
6916
6981
  const abnormalExit = describeAbnormalExit(service);
6917
6982
  if (!running && service.installed && !service.disabled && abnormalExit) {
6918
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`));
6919
- 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"));
6920
6985
  }
6921
6986
  const kernelVersion = getKernelVersion();
6922
6987
  console.log(
6923
- 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)")}`
6924
6989
  );
6925
6990
  if (pid) {
6926
6991
  console.log(`${colors.gray("PID: ")}${pid}`);
@@ -6948,7 +7013,7 @@ async function printStatus(args = []) {
6948
7013
  if (cached?.updated_at) {
6949
7014
  const rel = formatRelativeTime(cached.updated_at) || formatDate(cached.updated_at);
6950
7015
  if (isSubscriptionStale(cached)) {
6951
- 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`));
6952
7017
  } else {
6953
7018
  console.log(`${colors.gray("\u66F4\u65B0: ")}${rel}`);
6954
7019
  }
@@ -6961,12 +7026,12 @@ async function printStatus(args = []) {
6961
7026
  console.log(`${colors.gray("\u5230\u671F: ")}${expireColor(formatTimestamp(cached.expire), urgency)}`);
6962
7027
  }
6963
7028
  } else {
6964
- 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>)")}`);
6965
7030
  }
6966
7031
  printOverwriteLines(overwriteEnabled, overwriteFiles, overwriteBroken, activeSub);
6967
7032
  printServiceLines(service, legacy);
6968
7033
  if (kind === "tun") {
6969
- 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"));
6970
7035
  }
6971
7036
  console.log("");
6972
7037
  }
@@ -7001,10 +7066,10 @@ function printOverwriteLines(enabled, files, broken, activeSub) {
7001
7066
  }
7002
7067
  function printServiceLines(service, legacy) {
7003
7068
  if (!service.installed && !service.loaded) {
7004
- 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)")}`);
7005
7070
  } else if (!service.installed) {
7006
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)")}`);
7007
- 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"));
7008
7073
  printAutoStart(service);
7009
7074
  } else {
7010
7075
  console.log(`${colors.gray("\u670D\u52A1: ")}${colors.green("\u5DF2\u5B89\u88C5")}`);
@@ -7012,7 +7077,7 @@ function printServiceLines(service, legacy) {
7012
7077
  }
7013
7078
  if (legacy) {
7014
7079
  console.log(colors.yellow(" \u5F02\u5E38: \u68C0\u6D4B\u5230\u65E7\u7248\u672C\u7684\u7CFB\u7EDF\u7EA7\u670D\u52A1\uFF08root LaunchDaemon\uFF09"));
7015
- 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"));
7016
7081
  }
7017
7082
  }
7018
7083
  function printAutoStart(service) {
@@ -7025,19 +7090,19 @@ function resolveStartMode(args) {
7025
7090
  const modeArg = getNonFlagArg(args, 1);
7026
7091
  const modeToken = modeArg?.toLowerCase();
7027
7092
  if (modeToken !== void 0 && modeToken !== "tun" && modeToken !== "mixed") {
7028
- 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" });
7029
7094
  }
7030
7095
  return modeToken === "tun" ? "tun" : "mixed";
7031
7096
  }
7032
7097
  async function cmdStart(args) {
7033
7098
  assertKnownFlags(args, ["-s", "--no-update", "-u", "--update-timeout"], "start [tun|mixed]");
7034
- assertPositionalCount(args, 1, 1, "mihomo start [tun|mixed] [-s] [-u ms]");
7099
+ assertPositionalCount(args, 1, 1, "mihomo-cli start [tun|mixed] [-s] [-u ms]");
7035
7100
  const targetMode = resolveStartMode(args);
7036
7101
  const skipUpdate = hasFlag(args, "-s", "--no-update");
7037
7102
  const updateTimeout = parseIntArg(args, "-u", "--update-timeout", DEFAULT_AUTO_UPDATE_TIMEOUT);
7038
7103
  let disabledAutoStartForTun = false;
7039
7104
  if (!hasKernel()) {
7040
- 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" });
7041
7106
  }
7042
7107
  const serviceBefore = getServiceStatus();
7043
7108
  if (targetMode === "tun") {
@@ -7049,27 +7114,26 @@ async function cmdStart(args) {
7049
7114
  console.log("");
7050
7115
  }
7051
7116
  if (serviceBefore.loaded) {
7052
- throw new CliError("\u670D\u52A1\u6B63\u5728\u8FD0\u884C\uFF0C\u65E0\u6CD5\u542F\u52A8 TUN", {
7053
- 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"]
7054
- });
7117
+ throw tunBlockedByRunningService();
7055
7118
  }
7056
7119
  if (serviceBefore.installed && !serviceBefore.disabled) {
7057
7120
  disableServiceAutoStart();
7058
7121
  disabledAutoStartForTun = true;
7059
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"));
7060
- 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"));
7061
7124
  console.log("");
7062
7125
  } else {
7063
7126
  recordServiceStopped();
7064
7127
  }
7128
+ captureStopEpochBaseline();
7065
7129
  } else if (!serviceBefore.installed) {
7066
7130
  if (serviceBefore.loaded) {
7067
7131
  throw new CliError("\u670D\u52A1\u5904\u4E8E\u5F02\u5E38\u72B6\u6001\uFF08plist \u4E0D\u5B58\u5728\uFF0C\u4F46\u4EFB\u52A1\u4ECD\u88C5\u8F7D\uFF09", {
7068
- 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"]
7069
7133
  });
7070
7134
  }
7071
7135
  throw new CliError("\u670D\u52A1\u672A\u5B89\u88C5", {
7072
- 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"]
7073
7137
  });
7074
7138
  }
7075
7139
  const sub = requireActiveSubscription();
@@ -7093,18 +7157,22 @@ async function cmdStart(args) {
7093
7157
  const lines = e.message.split("\n");
7094
7158
  const extraHint = [];
7095
7159
  if (targetMode === "tun" && disabledAutoStartForTun) {
7096
- 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");
7097
7161
  }
7098
7162
  if (e instanceof CliError) {
7099
7163
  throw new CliError(e.message, { label: e.label, hint: [...e.hint, ...extraHint] });
7100
7164
  }
7101
7165
  throw new CliError(lines[0], { label: "\u542F\u52A8\u5931\u8D25", hint: [...lines.slice(1), ...extraHint] });
7102
7166
  }
7103
- 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
+ }
7104
7172
  if (targetMode === "mixed") {
7105
7173
  printSystemProxyHint(getPorts().mixed);
7106
7174
  } else {
7107
- 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"));
7108
7176
  }
7109
7177
  console.log("");
7110
7178
  }
@@ -7159,7 +7227,7 @@ async function restartToApply(args) {
7159
7227
 
7160
7228
  // src/commands/directory.ts
7161
7229
  function openDirectory(args) {
7162
- 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]");
7163
7231
  const target = args[2];
7164
7232
  if (target === void 0 || target === "root") {
7165
7233
  console.log(`\u6B63\u5728\u6253\u5F00: \u6839\u76EE\u5F55 (${USER_DATA_DIR})`);
@@ -7193,7 +7261,7 @@ function printDirectoryInfo() {
7193
7261
  console.log(` ${key.padEnd(9)}${rel.padEnd(pathWidth + 2)}${colors.gray(val.label)}`);
7194
7262
  }
7195
7263
  console.log("");
7196
- 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("|")}]`);
7197
7265
  console.log(colors.gray(" \u81EA\u5B9A\u4E49\u6839\u76EE\u5F55: \u73AF\u5883\u53D8\u91CF MIHOMO_CLI_DIR"));
7198
7266
  console.log("");
7199
7267
  }
@@ -7206,13 +7274,18 @@ async function cmdDirectory(args) {
7206
7274
  if (action.startsWith("-")) {
7207
7275
  throw new CliError(`\u672A\u77E5\u7684\u9009\u9879: ${action}`, {
7208
7276
  label: "\u53C2\u6570\u9519\u8BEF",
7209
- 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]"]
7210
7278
  });
7211
7279
  }
7212
7280
  const names = SUBCOMMANDS.flatMap((c) => [c.name, ...c.aliases ?? []]);
7213
7281
  const suggestion = suggestSimilar(action, names);
7214
7282
  throw new CliError(`\u672A\u77E5\u7684\u76EE\u5F55\u5B50\u547D\u4EE4: ${action}`, {
7215
- 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
+ ]
7216
7289
  });
7217
7290
  }
7218
7291
  });
@@ -7263,13 +7336,17 @@ function resolveDownloadChannels(input) {
7263
7336
  }
7264
7337
  if (input.proxyOverride) return [{ kind: "proxy", proxy: input.proxyOverride }];
7265
7338
  if (input.proxyRunning && input.proxyPort !== null) {
7266
- const channels = [{ kind: "proxy", proxy: `http://127.0.0.1:${input.proxyPort}` }];
7267
- if (input.ghAvailable) channels.push({ kind: "gh" });
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 });
7268
7342
  return channels;
7269
7343
  }
7270
7344
  if (input.ghAvailable) return [{ kind: "gh" }];
7271
7345
  return [{ kind: "direct" }];
7272
7346
  }
7347
+ function ghProbeNeeded(input) {
7348
+ return !input.forceDirect && input.proxyOverride === null;
7349
+ }
7273
7350
  function hasGh() {
7274
7351
  const result = spawnSync7("gh", ["--version"], { stdio: "ignore" });
7275
7352
  return !result.error && result.status === 0;
@@ -7470,10 +7547,26 @@ function describeCurlDownloadError(status) {
7470
7547
  function buildGhReleaseDownloadArgs(tag, assetName, dir) {
7471
7548
  return ["release", "download", tag, "--repo", GITHUB_REPO, "--pattern", assetName, "--dir", dir, "--clobber"];
7472
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
+ }
7473
7566
  async function downloadKernel(progressCallback, channel, releaseInfo) {
7474
7567
  ensureDirs();
7475
- const apiProxy = channel.kind === "proxy" ? channel.proxy : channel.kind === "mirror" ? channel.proxy ?? null : null;
7476
- 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));
7477
7570
  const arch = getArch();
7478
7571
  const platform = process.platform;
7479
7572
  const asset = findMatchingAsset(latest.assets, platform, arch);
@@ -7503,14 +7596,16 @@ async function downloadKernel(progressCallback, channel, releaseInfo) {
7503
7596
  }
7504
7597
  const ghResult = spawnSync7("gh", buildGhReleaseDownloadArgs(latest.tag_name, asset.name, tempDir), {
7505
7598
  stdio: "inherit",
7506
- timeout: GH_DOWNLOAD_TIMEOUT
7599
+ timeout: GH_DOWNLOAD_TIMEOUT,
7600
+ // gh 回退候选带本机代理:per-spawn 注入(buildGhDownloadEnv 注释述其与 D9 的关系)
7601
+ env: buildGhDownloadEnv(channel.proxy ?? null)
7507
7602
  });
7508
7603
  if (ghResult.error) {
7509
7604
  if (ghResult.error.code === "ENOENT") {
7510
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");
7511
7606
  }
7512
7607
  if (ghResult.error.code === "ETIMEDOUT") {
7513
- throw new Error(`\u4E0B\u8F7D\u8D85\u65F6\uFF08gh ${Math.floor(GH_DOWNLOAD_TIMEOUT / 1e3)}s \u672A\u5B8C\u6210\uFF09\uFF0C\u5C06\u5C1D\u8BD5\u4E0B\u4E00\u901A\u9053`);
7608
+ throw new Error(`\u4E0B\u8F7D\u8D85\u65F6\uFF08gh ${Math.floor(GH_DOWNLOAD_TIMEOUT / 1e3)}s \u672A\u5B8C\u6210\uFF09`);
7514
7609
  }
7515
7610
  throw new Error(`\u4E0B\u8F7D\u5931\u8D25: ${ghResult.error.message}`);
7516
7611
  }
@@ -7595,38 +7690,77 @@ async function downloadKernel(progressCallback, channel, releaseInfo) {
7595
7690
 
7596
7691
  // src/commands/update.ts
7597
7692
  import { execFile as execFile4, spawn as spawn2 } from "child_process";
7693
+ import net from "net";
7598
7694
  import { promisify as promisify4 } from "util";
7599
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
+ }
7600
7726
  var NPM_VIEW_TIMEOUT_MS = 15e3;
7601
7727
  async function getLatestNpmVersion(timeoutMs = NPM_VIEW_TIMEOUT_MS) {
7602
7728
  try {
7603
- 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 });
7604
7731
  const version = stdout.trim().split("\n").pop()?.trim();
7605
7732
  return version || null;
7606
7733
  } catch {
7607
7734
  return null;
7608
7735
  }
7609
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
+ }
7610
7747
  async function cmdUpdate(args = []) {
7611
7748
  assertKnownFlags(args.slice(1), [], "update");
7612
- assertPositionalCount(args, 0, 1, "mihomo update");
7749
+ assertPositionalCount(args, 0, 1, "mihomo-cli update");
7613
7750
  console.log(`\u5F53\u524D\u7248\u672C: ${colors.cyan(VERSION)}`);
7614
7751
  console.log("");
7615
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
+ }
7616
7763
  if (latest) {
7617
- try {
7618
- const cmp = compareVersions(VERSION, latest);
7619
- if (cmp > 0) {
7620
- 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`));
7621
- console.log(colors.gray("\u5982\u9700\u5F3A\u5236\u91CD\u88C5: npm install -g mihomo-cli"));
7622
- return;
7623
- }
7624
- if (cmp === 0) {
7625
- console.log(`\u5DF2\u662F\u6700\u65B0\u7248\u672C (${colors.green(VERSION)})\uFF0C\u65E0\u9700\u66F4\u65B0`);
7626
- return;
7627
- }
7628
- } catch {
7629
- }
7630
7764
  console.log(`\u6700\u65B0\u7248\u672C: ${colors.cyan(latest)}`);
7631
7765
  } else {
7632
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"));
@@ -7634,8 +7768,9 @@ async function cmdUpdate(args = []) {
7634
7768
  console.log("");
7635
7769
  console.log("\u6B63\u5728\u66F4\u65B0 mihomo-cli...");
7636
7770
  console.log("");
7771
+ const installEnv = await buildNpmSpawnEnv();
7637
7772
  await new Promise((resolve, reject) => {
7638
- const npm = spawn2("npm", ["install", "-g", PKG_NAME], { stdio: "inherit" });
7773
+ const npm = spawn2("npm", ["install", "-g", PKG_NAME], { stdio: "inherit", env: installEnv });
7639
7774
  npm.on("close", (code) => {
7640
7775
  if (code === 0) {
7641
7776
  resolve();
@@ -7700,33 +7835,23 @@ async function collectChecks() {
7700
7835
  4e3
7701
7836
  ).catch(() => null) : Promise.resolve(null);
7702
7837
  if (!hasKernel()) {
7703
- push("\u5185\u6838", "fail", "\u672A\u5B89\u88C5", "mihomo kernel");
7838
+ push("\u5185\u6838", "fail", "\u672A\u5B89\u88C5", "mihomo-cli kernel");
7704
7839
  } else {
7705
7840
  const v = getKernelVersion();
7706
7841
  const r = spawnSync8(PATHS.mihomoBinary, ["-v"], { encoding: "utf8", timeout: 5e3 });
7707
7842
  if (r.status === 0 && /v?\d+\.\d+\.\d+/.test(`${r.stdout}${r.stderr}`)) {
7708
7843
  push("\u5185\u6838", "ok", v || "\u53EF\u6267\u884C");
7709
7844
  } else if (r.error) {
7710
- 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");
7711
7846
  } else {
7712
- 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");
7713
7848
  }
7714
7849
  }
7715
- const leftovers = [];
7716
- if (fs12.existsSync(KERNEL_SELF_BACKUP_DIR)) {
7717
- leftovers.push({ dir: KERNEL_SELF_BACKUP_DIR, note: "\u65E7\u5185\u6838\u5907\u4EFD\uFF1A\u9700\u8981\u624B\u52A8\u56DE\u6EDA\u65F6\u53EF\u4FDD\u7559\uFF0C\u5426\u5219\u53EF\u5220" });
7718
- }
7719
7850
  if (fs12.existsSync(KERNEL_SELF_UPDATE_DIR)) {
7720
- leftovers.push({ dir: KERNEL_SELF_UPDATE_DIR, note: "\u4E2D\u65AD\u7684\u4E0B\u8F7D\u6682\u5B58\uFF1A\u53EF\u5B89\u5168\u5220\u9664" });
7721
- }
7722
- if (leftovers.length > 0) {
7723
- push(
7724
- "\u5185\u6838\u81EA\u5347\u7EA7\u6B8B\u7559",
7725
- "warn",
7726
- `\u53D1\u73B0 ${leftovers.length} \u4E2A\u9762\u677F\u81EA\u5347\u7EA7\u9057\u7559\u76EE\u5F55`,
7727
- `rm -rf ${leftovers.map((l) => l.dir).join(" ")}`,
7728
- leftovers.map((l) => ` ${l.dir}\uFF1A${l.note}`)
7729
- );
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`);
7730
7855
  }
7731
7856
  try {
7732
7857
  fs12.accessSync(USER_DATA_DIR, fs12.constants.W_OK);
@@ -7746,12 +7871,12 @@ async function collectChecks() {
7746
7871
  const subs = getSubscriptionsWithCache();
7747
7872
  const active = getActiveSubscription();
7748
7873
  if (subs.length === 0) {
7749
- push("\u8BA2\u9605", "warn", "\u672A\u914D\u7F6E", "mihomo sub add <url>");
7874
+ push("\u8BA2\u9605", "warn", "\u672A\u914D\u7F6E", "mihomo-cli sub add <url>");
7750
7875
  } else {
7751
7876
  push("\u8BA2\u9605", "ok", `${subs.length} \u4E2A${active ? `\uFF0C\u5F53\u524D: ${active.name}` : ""}`);
7752
7877
  if (active) {
7753
7878
  if (!readSubscriptionRawConfig(active.name)) {
7754
- 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}`);
7755
7880
  } else {
7756
7881
  push("\u8BA2\u9605\u914D\u7F6E", "ok", `"${active.name}" \u914D\u7F6E\u6587\u4EF6\u5B58\u5728`);
7757
7882
  }
@@ -7759,7 +7884,7 @@ async function collectChecks() {
7759
7884
  if (cached?.updated_at) {
7760
7885
  const rel = formatRelativeTime(cached.updated_at);
7761
7886
  if (isSubscriptionStale(cached)) {
7762
- 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");
7763
7888
  } else {
7764
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`);
7765
7890
  }
@@ -7769,21 +7894,21 @@ async function collectChecks() {
7769
7894
  const service = getServiceStatus();
7770
7895
  const legacy = detectLegacySystemInstall();
7771
7896
  if (legacy) {
7772
- 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");
7773
7898
  } else if (!service.installed && !service.loaded) {
7774
- 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");
7775
7900
  } else if (!service.installed) {
7776
- 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");
7777
7902
  } else if (service.running) {
7778
7903
  const abnormalExit = describeAbnormalExit(service);
7779
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` : ""}`);
7780
7905
  if (abnormalExit) {
7781
- 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");
7782
7907
  }
7783
7908
  } else {
7784
7909
  const abnormalExit = describeAbnormalExit(service);
7785
7910
  if (!service.disabled && abnormalExit) {
7786
- 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");
7787
7912
  } else {
7788
7913
  push("\u670D\u52A1", "ok", `\u5DF2\u5B89\u88C5\uFF0C\u672A\u8FD0\u884C${service.disabled ? "\uFF08\u81EA\u542F\u5DF2\u5173\u95ED\uFF09" : ""}`);
7789
7914
  }
@@ -7802,7 +7927,7 @@ async function collectChecks() {
7802
7927
  if (isPortListening(mixedPort)) {
7803
7928
  push("\u7AEF\u53E3", "ok", `${mixedPort} \u6B63\u5728\u76D1\u542C`);
7804
7929
  } else {
7805
- 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");
7806
7931
  }
7807
7932
  } else if (isPortListening(mixedPort)) {
7808
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`);
@@ -7827,7 +7952,7 @@ async function collectChecks() {
7827
7952
  }
7828
7953
  } catch (e) {
7829
7954
  const notes = e instanceof CliError ? e.hint.filter((l) => l.trim().length > 0) : void 0;
7830
- 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);
7831
7956
  }
7832
7957
  } else {
7833
7958
  push("\u914D\u7F6E\u6784\u5EFA", "skip", active ? "\u672A\u5B89\u88C5\u5185\u6838\uFF0C\u8DF3\u8FC7\u6821\u9A8C" : "\u65E0\u8BA2\u9605\uFF0C\u8DF3\u8FC7");
@@ -7837,7 +7962,7 @@ async function collectChecks() {
7837
7962
  if (probe.ok) {
7838
7963
  push("\u4EE3\u7406\u8FDE\u901A", "ok", `HTTP ${probe.statusCode}\uFF08${probe.durationMs}ms\uFF09`);
7839
7964
  } else {
7840
- 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");
7841
7966
  }
7842
7967
  } else {
7843
7968
  push("\u4EE3\u7406\u8FDE\u901A", "skip", "\u672A\u8FD0\u884C");
@@ -7846,7 +7971,7 @@ async function collectChecks() {
7846
7971
  if (hasKernel() && kernelInfo === null) {
7847
7972
  push("\u5185\u6838\u7248\u672C", "skip", "GitHub \u4E0D\u53EF\u8FBE\uFF0C\u8DF3\u8FC7\u68C0\u67E5");
7848
7973
  } else if (kernelInfo?.needsUpdate) {
7849
- 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");
7850
7975
  } else if (kernelInfo) {
7851
7976
  push("\u5185\u6838\u7248\u672C", "ok", `${kernelInfo.current}\uFF08\u6700\u65B0\uFF09`);
7852
7977
  }
@@ -7854,7 +7979,7 @@ async function collectChecks() {
7854
7979
  if (latest === null) {
7855
7980
  push("CLI \u7248\u672C", "skip", "npm registry \u4E0D\u53EF\u8FBE\uFF0C\u8DF3\u8FC7\u68C0\u67E5");
7856
7981
  } else if (compareVersions(latest, VERSION) > 0) {
7857
- 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");
7858
7983
  } else {
7859
7984
  push("CLI \u7248\u672C", "ok", `${VERSION}\uFF08\u6700\u65B0\uFF09`);
7860
7985
  }
@@ -7862,7 +7987,7 @@ async function collectChecks() {
7862
7987
  }
7863
7988
  async function cmdDoctor(args = []) {
7864
7989
  assertKnownFlags(args.slice(1), [], "doctor");
7865
- assertPositionalCount(args, 0, 1, "mihomo doctor");
7990
+ assertPositionalCount(args, 0, 1, "mihomo-cli doctor");
7866
7991
  const checks = await collectChecks();
7867
7992
  console.log("");
7868
7993
  for (const c of checks) {
@@ -7935,7 +8060,7 @@ function parseMirrorArg(args) {
7935
8060
  if (mirrorCount > 1) {
7936
8061
  throw new CliError("--mirror \u53EA\u80FD\u6307\u5B9A\u4E00\u6B21", {
7937
8062
  label: "\u53C2\u6570\u9519\u8BEF",
7938
- 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"]
7939
8064
  });
7940
8065
  }
7941
8066
  const mirrorEq = args.find((a) => a.startsWith("--mirror="));
@@ -7945,7 +8070,7 @@ function parseMirrorArg(args) {
7945
8070
  if (inline === "" || mirrorIdx >= 0 && args[mirrorIdx + 1] === "") {
7946
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", {
7947
8072
  label: "\u53C2\u6570\u9519\u8BEF",
7948
- 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"]
7949
8074
  });
7950
8075
  }
7951
8076
  const nextArg = inline ?? args[mirrorIdx + 1];
@@ -8010,7 +8135,7 @@ function parseProxyArg(args) {
8010
8135
  if (hits.length > 1) {
8011
8136
  throw new CliError("--proxy \u53EA\u80FD\u6307\u5B9A\u4E00\u6B21", {
8012
8137
  label: "\u53C2\u6570\u9519\u8BEF",
8013
- 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"]
8014
8139
  });
8015
8140
  }
8016
8141
  const raw = hits[0].trim();
@@ -8025,21 +8150,22 @@ function parseProxyArg(args) {
8025
8150
 
8026
8151
  // src/commands/kernel.ts
8027
8152
  var KERNEL_VALUE_FLAGS = /* @__PURE__ */ new Set([...VALUE_FLAGS, "--mirror"]);
8028
- function channelLabel(channel) {
8153
+ function channelLabel(channel, isExplicitProxy) {
8029
8154
  switch (channel.kind) {
8030
8155
  case "gh":
8031
- return "gh";
8156
+ return channel.proxy ? `gh\uFF08\u7ECF\u672C\u673A\u4EE3\u7406 ${channel.proxy}\uFF09` : "gh";
8032
8157
  case "proxy":
8033
- return `\u4EE3\u7406 ${channel.proxy}`;
8158
+ return `${isExplicitProxy ? "\u4EE3\u7406" : "\u672C\u673A\u4EE3\u7406"} ${channel.proxy}`;
8034
8159
  case "mirror":
8035
- return `\u955C\u50CF ${channel.mirror}`;
8160
+ return `\u955C\u50CF ${channel.mirror}${channel.proxy ? `\uFF08\u7ECF\u4EE3\u7406 ${channel.proxy}\uFF09` : ""}`;
8036
8161
  case "direct":
8037
8162
  return "\u76F4\u8FDE";
8038
8163
  }
8039
8164
  }
8040
8165
  function printChannelLine(channel, isExplicitProxy) {
8166
+ if (channel.kind === "direct") return;
8041
8167
  if (channel.kind === "gh") {
8042
- console.log("\u4E0B\u8F7D\u901A\u9053: gh\uFF08GitHub CLI\uFF09");
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");
8043
8169
  } else if (channel.kind === "proxy") {
8044
8170
  console.log(`\u4E0B\u8F7D\u901A\u9053: ${isExplicitProxy ? "\u4EE3\u7406" : "\u672C\u673A\u4EE3\u7406"} ${channel.proxy}`);
8045
8171
  } else if (channel.kind === "mirror") {
@@ -8048,14 +8174,47 @@ function printChannelLine(channel, isExplicitProxy) {
8048
8174
  }
8049
8175
  console.log("");
8050
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
+ }
8051
8210
  async function cmdKernel(args) {
8052
8211
  const mirrorInfo = parseMirrorArg(args);
8053
8212
  const proxyInfo = parseProxyArg(args);
8054
- 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);
8055
8214
  if (proxyInfo.proxy && mirrorInfo.isOverride && !mirrorInfo.mirror) {
8056
8215
  throw new CliError("--mirror direct \u4E0E --proxy \u4E0D\u80FD\u540C\u65F6\u4F7F\u7528", {
8057
8216
  label: "\u53C2\u6570\u9519\u8BEF",
8058
- hint: ["--mirror direct \u5F3A\u5236\u4E0D\u7ECF\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>"]
8059
8218
  });
8060
8219
  }
8061
8220
  const proxyRunning = getRunningState().running;
@@ -8068,7 +8227,7 @@ async function cmdKernel(args) {
8068
8227
  }
8069
8228
  }
8070
8229
  const forceDirect = mirrorInfo.isOverride && !mirrorInfo.mirror;
8071
- const ghAvailable = hasGh();
8230
+ const ghAvailable = ghProbeNeeded({ forceDirect, proxyOverride: proxyInfo.proxy }) && hasGh();
8072
8231
  const channels = resolveDownloadChannels({
8073
8232
  mirror: mirrorInfo.mirror,
8074
8233
  isOverride: mirrorInfo.isOverride,
@@ -8106,14 +8265,14 @@ async function cmdKernel(args) {
8106
8265
  }
8107
8266
  } else if (!mirrorInfo.mirror) {
8108
8267
  if (apiProxy) {
8109
- 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");
8110
8269
  } else {
8111
8270
  hint.push(
8112
8271
  "",
8113
8272
  "\u63D0\u793A: \u76F4\u8FDE\u5931\u8D25\u6216\u4E0B\u8F7D\u8FC7\u6162\u65F6\u53EF\u4F7F\u7528\u955C\u50CF\u6216\u4EE3\u7406:",
8114
- " 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",
8115
8274
  ` \u53EF\u7528\u955C\u50CF: ${AVAILABLE_MIRRORS.join(", ")}`,
8116
- " 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"
8117
8276
  );
8118
8277
  }
8119
8278
  } else if (apiProxy) {
@@ -8126,7 +8285,7 @@ async function cmdKernel(args) {
8126
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:",
8127
8286
  " \u5B89\u88C5\u5E76\u767B\u5F55 GitHub CLI\uFF08\u8BA4\u8BC1\u914D\u989D 5000 \u6B21/\u65F6\uFF09:",
8128
8287
  " brew install gh && gh auth login",
8129
- " \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>"
8130
8289
  );
8131
8290
  }
8132
8291
  throw new CliError(err.message, { label: "\u66F4\u65B0\u5931\u8D25", hint });
@@ -8141,40 +8300,28 @@ async function cmdKernel(args) {
8141
8300
  } else {
8142
8301
  console.log("\n\u6B63\u5728\u4E0B\u8F7D...");
8143
8302
  let result;
8144
- const failures = [];
8303
+ const attempts = [];
8145
8304
  for (let attempt = 0; attempt < channels.length; attempt++) {
8146
8305
  if (attempt > 0) {
8147
- console.log("\u4E0A\u4E00\u901A\u9053\u5931\u8D25\uFF0C\u5207\u6362\u4E3A:");
8306
+ console.log(formatChannelSwitchLine(attempts[attempt - 1]?.error ?? null));
8148
8307
  printChannelLine(channels[attempt], proxyInfo.proxy !== null);
8149
8308
  }
8150
8309
  try {
8151
8310
  result = await downloadKernel((msg) => console.log(msg), channels[attempt], info.release);
8152
8311
  } catch (e) {
8153
- failures.push(` ${channelLabel(channels[attempt])}: ${e.message.split("\n")[0]}`);
8312
+ attempts.push({ channel: channels[attempt], error: e });
8154
8313
  continue;
8155
8314
  }
8156
8315
  break;
8157
8316
  }
8158
8317
  if (!result) {
8159
- throw new CliError("\u5168\u90E8\u4E0B\u8F7D\u901A\u9053\u5747\u5931\u8D25", {
8160
- label: "\u4E0B\u8F7D\u5931\u8D25",
8161
- hint: [
8162
- ...failures,
8163
- "",
8164
- "\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",
8165
- "\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",
8166
- "",
8167
- "\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\u56DE\u9000 gh\uFF1B",
8168
- "\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",
8169
- " mihomo kernel --proxy <\u7AEF\u53E3>\uFF08\u7ECF\u672C\u673A\u5176\u4ED6\u4EE3\u7406\u5DE5\u5177\u51FA\u7F51\uFF09"
8170
- ]
8171
- });
8318
+ throw buildDownloadFailureError(attempts, proxyInfo.proxy !== null);
8172
8319
  }
8173
8320
  console.log(`
8174
8321
  \u5DF2\u66F4\u65B0\u5230 ${result.version}`);
8175
8322
  const state = getRunningState();
8176
8323
  if (state.running) {
8177
- const restartCommand = state.kind === "tun" ? "mihomo start tun" : "mihomo start";
8324
+ const restartCommand = state.kind === "tun" ? "mihomo-cli start tun" : "mihomo-cli start";
8178
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`));
8179
8326
  }
8180
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"));
@@ -8184,7 +8331,7 @@ async function cmdKernel(args) {
8184
8331
  // src/commands/log.ts
8185
8332
  function cmdLogs(args) {
8186
8333
  assertKnownFlags(args, ["-f", "--follow", "-n", "--lines", "-o", "--open"], "logs [-f] [-n N] [\u7F16\u53F7] [-o]");
8187
- 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]");
8188
8335
  const lines = parseIntArg(args, "-n", "--lines", 100);
8189
8336
  const openInViewer = hasFlag(args, "-o", "--open");
8190
8337
  const follow = hasFlag(args, "-f", "--follow");
@@ -8197,18 +8344,18 @@ function cmdLogs(args) {
8197
8344
  } else {
8198
8345
  const parsedIdx = parseInt(targetName, 10);
8199
8346
  if (Number.isNaN(parsedIdx) || parsedIdx < 1 || String(parsedIdx) !== targetName) {
8200
- 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" });
8201
8348
  }
8202
8349
  const archive = listLogs().archives[parsedIdx - 1];
8203
8350
  if (!archive) {
8204
- 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' });
8205
8352
  }
8206
8353
  logPath = archive.path;
8207
8354
  }
8208
8355
  if (openInViewer) {
8209
8356
  if (follow) {
8210
8357
  throw new CliError("-o\uFF08\u7CFB\u7EDF\u67E5\u770B\u5668\u6253\u5F00\uFF09\u4E0E -f\uFF08\u7EC8\u7AEF\u8DDF\u968F\uFF09\u4E92\u65A5", {
8211
- 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"
8212
8359
  });
8213
8360
  }
8214
8361
  openLogFile(logPath);
@@ -8243,16 +8390,16 @@ function cmdLogs(args) {
8243
8390
  console.log(` ${num}. ${name}`);
8244
8391
  console.log(` \u65F6\u95F4: ${time} \u5927\u5C0F: ${size}`);
8245
8392
  if (!log.isCurrent) {
8246
- 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`);
8247
8394
  }
8248
8395
  console.log("");
8249
8396
  }
8250
8397
  console.log("\u7528\u6CD5:");
8251
- console.log(" mihomo logs 0 # \u67E5\u770B\u5F53\u524D\u65E5\u5FD7 (\u6700\u540E 100 \u884C)");
8252
- console.log(" mihomo logs 0 -f # \u5B9E\u65F6\u8DDF\u968F\u5F53\u524D\u65E5\u5FD7");
8253
- console.log(" mihomo logs 1 # \u67E5\u770B\u7B2C 1 \u4E2A\u5F52\u6863\u65E5\u5FD7\uFF08\u6700\u65B0\uFF09");
8254
- console.log(" mihomo logs 1 -n 200 # \u67E5\u770B 200 \u884C");
8255
- 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");
8256
8403
  console.log("");
8257
8404
  }
8258
8405
 
@@ -8301,13 +8448,13 @@ function printOverwriteList() {
8301
8448
  console.log("");
8302
8449
  }
8303
8450
  }
8304
- console.log("\u542F\u7528\u8986\u5199: mihomo ow on");
8305
- 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");
8306
8453
  console.log("\u505C\u7528\u5355\u4E2A\u6587\u4EF6: \u5728\u8BE5\u6587\u4EF6\u9876\u90E8\u5199 enabled: false");
8307
8454
  console.log("");
8308
8455
  }
8309
8456
  async function setOverwrite(enabled, args) {
8310
- assertPositionalCount(args, 0, 2, "mihomo ow [on|off]");
8457
+ assertPositionalCount(args, 0, 2, "mihomo-cli ow [on|off]");
8311
8458
  assertRestartOptionValues(args);
8312
8459
  if (isOverwriteEnabled() === enabled) {
8313
8460
  console.log(`\u8986\u5199\u914D\u7F6E\u5DF2\u662F${enabled ? "\u542F\u7528" : "\u7981\u7528"}\u72B6\u6001`);
@@ -8337,7 +8484,7 @@ async function cmdOverwrite(args) {
8337
8484
  if (action.startsWith("-")) {
8338
8485
  throw new CliError(`\u672A\u77E5\u7684\u9009\u9879: ${action}`, {
8339
8486
  label: "\u53C2\u6570\u9519\u8BEF",
8340
- 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]"]
8341
8488
  });
8342
8489
  }
8343
8490
  const names = SUBCOMMANDS2.flatMap((c) => [c.name, ...c.aliases ?? []]);
@@ -8365,7 +8512,7 @@ var RESET_TARGETS = [
8365
8512
  paths: () => fs13.existsSync(USER_DATA_DIR) ? fs13.readdirSync(USER_DATA_DIR).filter(isOverwriteFilename).map((f) => `${USER_DATA_DIR}/${f}`) : []
8366
8513
  },
8367
8514
  {
8368
- // 别名刻意不含 'config':用户从 `mihomo config` 命令得到的直觉是「运行配置」
8515
+ // 别名刻意不含 'config':用户从 `mihomo-cli config` 命令得到的直觉是「运行配置」
8369
8516
  // (那属于 runtime 目标),而这里删的是 settings(订阅列表/端口/密钥)——
8370
8517
  // 静默对撞会让 reset config -y 删超预期的数据。未知目标报错 + 目标列表兜底
8371
8518
  id: "settings",
@@ -8395,7 +8542,7 @@ async function cmdReset(args) {
8395
8542
  const names = args.slice(1).filter((a) => !a.startsWith("-"));
8396
8543
  if (names.length > 0 && args.includes("--full")) {
8397
8544
  throw new CliError("\u4E0D\u80FD\u540C\u65F6\u6307\u5B9A\u91CD\u7F6E\u76EE\u6807\u4E0E --full", {
8398
- 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"]
8399
8546
  });
8400
8547
  }
8401
8548
  const namedTargets = resolveResetTargets(names);
@@ -8418,7 +8565,7 @@ async function cmdReset(args) {
8418
8565
  if (serviceTargeted && serviceActive) {
8419
8566
  console.log(colors.yellow("\u5C06\u5378\u8F7D launchd \u670D\u52A1\uFF08Mixed \u6A21\u5F0F\u9700\u91CD\u65B0 install \u624D\u80FD\u4F7F\u7528\uFF09"));
8420
8567
  } else if (needsStop && serviceActive) {
8421
- 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"));
8422
8569
  }
8423
8570
  if ((needsStop || serviceTargeted) && legacy) {
8424
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"));
@@ -8429,7 +8576,7 @@ async function cmdReset(args) {
8429
8576
  }
8430
8577
  if (!args.includes("-y") && !args.includes("--yes") && !await confirmOrThrow("\u786E\u8BA4?", {
8431
8578
  nonTtyMessage: "\u975E\u4EA4\u4E92\u73AF\u5883\u65E0\u6CD5\u786E\u8BA4",
8432
- hint: ["\u8DF3\u8FC7\u786E\u8BA4\u8BF7\u52A0 -y: mihomo reset ... -y"]
8579
+ hint: ["\u8DF3\u8FC7\u786E\u8BA4\u8BF7\u52A0 -y: mihomo-cli reset ... -y"]
8433
8580
  })) {
8434
8581
  console.log("\u5DF2\u53D6\u6D88");
8435
8582
  return;
@@ -8451,7 +8598,7 @@ async function cmdReset(args) {
8451
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;
8452
8599
  if (reason) {
8453
8600
  console.warn(colors.yellow(`\u8B66\u544A: ${reason}`));
8454
- console.warn(colors.gray("\u91CD\u8BD5\u6E05\u7406: mihomo stop"));
8601
+ console.warn(colors.gray("\u91CD\u8BD5\u6E05\u7406: mihomo-cli stop"));
8455
8602
  }
8456
8603
  }
8457
8604
  recordServiceStopped();
@@ -8512,14 +8659,14 @@ async function handleLegacyInstall() {
8512
8659
  function printRestoreSkipped() {
8513
8660
  console.log(colors.yellow("\u672A\u6062\u590D\u8FD0\u884C\uFF1A\u5B89\u88C5\u671F\u95F4\u68C0\u6D4B\u5230\u505C\u6B62\u64CD\u4F5C"));
8514
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"));
8515
- console.log(colors.gray(" \u542F\u52A8: mihomo start"));
8662
+ console.log(colors.gray(" \u542F\u52A8: mihomo-cli start"));
8516
8663
  console.log("");
8517
8664
  }
8518
8665
  async function cmdInstall(args) {
8519
8666
  assertKnownFlags(args.slice(1), [], "install");
8520
- assertPositionalCount(args, 0, 1, "mihomo install");
8667
+ assertPositionalCount(args, 0, 1, "mihomo-cli install");
8521
8668
  if (!hasKernel()) {
8522
- 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" });
8523
8670
  }
8524
8671
  await handleLegacyInstall();
8525
8672
  const wasRunning = getServiceStatus().running;
@@ -8543,12 +8690,12 @@ async function cmdInstall(args) {
8543
8690
  }
8544
8691
  throw new CliError(e.message, {
8545
8692
  label: e.label,
8546
- 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"]
8547
8694
  });
8548
8695
  }
8549
8696
  console.log(colors.green("\u5DF2\u6309\u539F\u72B6\u6001\u91CD\u65B0\u542F\u52A8"));
8550
8697
  } else {
8551
- console.log("\u542F\u52A8: mihomo start");
8698
+ console.log("\u542F\u52A8: mihomo-cli start");
8552
8699
  if (!fs14.existsSync(PATHS.configFile)) {
8553
8700
  console.log(colors.gray(" \u5C1A\u65E0\u8FD0\u884C\u65F6\u914D\u7F6E\uFF0Cstart \u4F1A\u5148\u8981\u6C42\u6DFB\u52A0\u8BA2\u9605"));
8554
8701
  }
@@ -8557,7 +8704,7 @@ async function cmdInstall(args) {
8557
8704
  }
8558
8705
  async function cmdUninstall(args) {
8559
8706
  assertKnownFlags(args.slice(1), [], "uninstall");
8560
- assertPositionalCount(args, 0, 1, "mihomo uninstall");
8707
+ assertPositionalCount(args, 0, 1, "mihomo-cli uninstall");
8561
8708
  const status = getServiceStatus();
8562
8709
  const legacy = detectLegacySystemInstall();
8563
8710
  const residue = getMihomoPids();
@@ -8584,9 +8731,9 @@ async function cmdUninstall(args) {
8584
8731
  console.log(colors.yellow(`\u4ECD\u6709\u5185\u6838\u8FDB\u7A0B\u6B8B\u7559 (PID ${remaining.join(", ")})`));
8585
8732
  console.log("\u624B\u52A8\u6E05\u7406: sudo pkill -9 mihomo");
8586
8733
  }
8587
- console.log(colors.gray("\u91CD\u65B0\u5B89\u88C5: mihomo install"));
8734
+ console.log(colors.gray("\u91CD\u65B0\u5B89\u88C5: mihomo-cli install"));
8588
8735
  console.log(colors.gray("\u5F7B\u5E95\u79FB\u9664 mihomo-cli:"));
8589
- 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"));
8590
8737
  console.log(colors.gray(" npm uninstall -g mihomo-cli"));
8591
8738
  console.log("");
8592
8739
  }
@@ -8599,7 +8746,7 @@ function handleStopResult(result) {
8599
8746
  }
8600
8747
  async function cmdStop(args) {
8601
8748
  assertKnownFlags(args.slice(1), [], "stop");
8602
- assertPositionalCount(args, 0, 1, "mihomo stop");
8749
+ assertPositionalCount(args, 0, 1, "mihomo-cli stop");
8603
8750
  if (detectLegacySystemInstall()) {
8604
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"));
8605
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"));
@@ -8630,7 +8777,7 @@ async function cmdStop(args) {
8630
8777
  hint: ["\u8BF7\u624B\u52A8\u8FD0\u884C: sudo pkill -9 mihomo"]
8631
8778
  });
8632
8779
  }
8633
- 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")}`);
8634
8781
  }
8635
8782
 
8636
8783
  // src/commands/subscription.ts
@@ -8638,7 +8785,7 @@ import { spawnSync as spawnSync9 } from "child_process";
8638
8785
  function printRestartHintIfRunning(variant = "update") {
8639
8786
  const state = getRunningState();
8640
8787
  if (state.running) {
8641
- const hintCommand = state.kind === "tun" ? "mihomo start tun" : "mihomo start";
8788
+ const hintCommand = state.kind === "tun" ? "mihomo-cli start tun" : "mihomo-cli start";
8642
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`;
8643
8790
  console.log(colors.yellow(message));
8644
8791
  console.log("");
@@ -8649,7 +8796,7 @@ function printSubscriptionList() {
8649
8796
  if (subs.length === 0) {
8650
8797
  console.log("\u6CA1\u6709\u8BA2\u9605");
8651
8798
  console.log("");
8652
- console.log("\u6DFB\u52A0\u8BA2\u9605: mihomo sub add <url> [name]");
8799
+ console.log("\u6DFB\u52A0\u8BA2\u9605: mihomo-cli sub add <url> [name]");
8653
8800
  console.log("");
8654
8801
  return;
8655
8802
  }
@@ -8677,13 +8824,13 @@ function printSubscriptionList() {
8677
8824
  }
8678
8825
  });
8679
8826
  if (subs.some((s) => s.updated_at == null) && subs.every((s) => formatTraffic(s.upload, s.download, s.total) === null)) {
8680
- 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"));
8681
8828
  }
8682
8829
  console.log("");
8683
- console.log("\u5207\u6362\u8BA2\u9605: mihomo sub use <name>");
8684
- console.log("\u65B0\u589E\u8BA2\u9605: mihomo sub add <url> [name]");
8685
- console.log("\u66F4\u65B0\u8BA2\u9605: mihomo sub update [name]");
8686
- 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>");
8687
8834
  console.log("");
8688
8835
  }
8689
8836
  function readUrlFromClipboard() {
@@ -8698,7 +8845,7 @@ function readUrlFromClipboard() {
8698
8845
  }
8699
8846
  }
8700
8847
  async function subAdd(args) {
8701
- assertPositionalCount(args, 2, 2, "mihomo sub add <url> [name]");
8848
+ assertPositionalCount(args, 2, 2, "mihomo-cli sub add <url> [name]");
8702
8849
  let url = args[2]?.trim();
8703
8850
  const name = args[3] === void 0 ? "default" : args[3];
8704
8851
  if (!name.trim()) {
@@ -8716,10 +8863,10 @@ async function subAdd(args) {
8716
8863
  url = clipped;
8717
8864
  } else if (process.stdin.isTTY) {
8718
8865
  throw new CliError("\u526A\u8D34\u677F\u4E2D\u6CA1\u6709\u6709\u6548\u7684\u8BA2\u9605 URL", {
8719
- 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]"]
8720
8867
  });
8721
8868
  } else {
8722
- 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]"] });
8723
8870
  }
8724
8871
  }
8725
8872
  if (!isValidHttpUrl(url)) {
@@ -8741,14 +8888,14 @@ async function subAdd(args) {
8741
8888
  printSubscriptionList();
8742
8889
  }
8743
8890
  async function subUpdate(args) {
8744
- assertPositionalCount(args, 1, 2, "mihomo sub update [name]");
8891
+ assertPositionalCount(args, 1, 2, "mihomo-cli sub update [name]");
8745
8892
  const nameArg = getNonFlagArg(args, 2);
8746
8893
  const subs = getSubscriptions();
8747
8894
  if (subs.length === 0) {
8748
8895
  throw new CliError("\u6CA1\u6709\u8BA2\u9605");
8749
8896
  }
8750
8897
  if (nameArg === "") {
8751
- 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>`] });
8752
8899
  }
8753
8900
  if (nameArg === null) {
8754
8901
  console.log(`\u66F4\u65B0\u6240\u6709 ${subs.length} \u4E2A\u8BA2\u9605...`);
@@ -8768,7 +8915,7 @@ async function subUpdate(args) {
8768
8915
  if (failedResults.length > 0) {
8769
8916
  const allFailed = failedResults.length === results.length;
8770
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")}`, {
8771
- hint: failedResults.map((r) => `\u91CD\u8BD5: mihomo sub update ${r.name}`)
8918
+ hint: failedResults.map((r) => `\u91CD\u8BD5: mihomo-cli sub update ${r.name}`)
8772
8919
  });
8773
8920
  }
8774
8921
  return;
@@ -8785,12 +8932,12 @@ async function subUpdate(args) {
8785
8932
  printSubscriptionList();
8786
8933
  }
8787
8934
  async function subUse(args) {
8788
- assertPositionalCount(args, 1, 2, "mihomo sub use <name>");
8935
+ assertPositionalCount(args, 1, 2, "mihomo-cli sub use <name>");
8789
8936
  assertRestartOptionValues(args);
8790
8937
  const name = getNonFlagArg(args, 2);
8791
8938
  const subs = getSubscriptions();
8792
8939
  if (subs.length === 0) {
8793
- 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]" });
8794
8941
  }
8795
8942
  if (!name) {
8796
8943
  throw new CliError("\u8BF7\u6307\u5B9A\u8BA2\u9605\u540D\u79F0", {
@@ -8816,11 +8963,11 @@ async function subUse(args) {
8816
8963
  printSubscriptionList();
8817
8964
  }
8818
8965
  async function subRemove(args) {
8819
- assertPositionalCount(args, 1, 2, "mihomo sub remove <name>");
8966
+ assertPositionalCount(args, 1, 2, "mihomo-cli sub remove <name>");
8820
8967
  const name = getNonFlagArg(args, 2);
8821
8968
  const subs = getSubscriptions();
8822
8969
  if (subs.length === 0) {
8823
- 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]" });
8824
8971
  }
8825
8972
  if (!name) {
8826
8973
  throw new CliError("\u8BF7\u6307\u5B9A\u8981\u5220\u9664\u7684\u8BA2\u9605\u540D\u79F0", {
@@ -8834,7 +8981,7 @@ async function subRemove(args) {
8834
8981
  console.log(`\u5C06\u5220\u9664\u8BA2\u9605 "${target.name}" (\u6A21\u7CCA\u5339\u914D "${name}")`);
8835
8982
  if (!await confirmOrThrow("\u6B64\u64CD\u4F5C\u4E0D\u53EF\u6062\u590D\uFF0C\u786E\u8BA4?", {
8836
8983
  nonTtyMessage: `\u6A21\u7CCA\u5339\u914D\u5230 "${target.name}"\uFF0C\u975E\u4EA4\u4E92\u73AF\u5883\u9700\u786E\u8BA4`,
8837
- 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`]
8838
8985
  })) {
8839
8986
  console.log("\u5DF2\u53D6\u6D88");
8840
8987
  return;
@@ -8871,13 +9018,13 @@ async function cmdSubscription(args) {
8871
9018
  if (action.startsWith("-")) {
8872
9019
  throw new CliError(`\u672A\u77E5\u7684\u9009\u9879: ${action}`, {
8873
9020
  label: "\u53C2\u6570\u9519\u8BEF",
8874
- 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"]
8875
9022
  });
8876
9023
  }
8877
9024
  const names = SUBCOMMANDS3.flatMap((c) => [c.name, ...c.aliases ?? []]);
8878
9025
  const suggestion = suggestSimilar(action, names);
8879
9026
  throw new CliError(`\u672A\u77E5\u7684\u8BA2\u9605\u547D\u4EE4: ${action}`, {
8880
- 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"]
8881
9028
  });
8882
9029
  }
8883
9030
  });
@@ -8902,11 +9049,11 @@ function copyToClipboard(text) {
8902
9049
  }
8903
9050
  function cmdUI(args) {
8904
9051
  assertKnownFlags(args.slice(1), ["-c", "--copy-secret"], "ui [zash|dash|yacd] [-c]");
8905
- assertPositionalCount(args, 1, 1, "mihomo ui [zash|dash|yacd] [-c]");
9052
+ assertPositionalCount(args, 1, 1, "mihomo-cli ui [zash|dash|yacd] [-c]");
8906
9053
  const uiName = resolveUiName(args);
8907
9054
  const copySecret = hasFlag(args, "-c", "--copy-secret");
8908
9055
  if (!getRunningState().running) {
8909
- 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"));
8910
9057
  console.log("");
8911
9058
  }
8912
9059
  const url = UI_URLS[uiName];
@@ -8926,7 +9073,7 @@ function cmdUI(args) {
8926
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")
8927
9074
  );
8928
9075
  } else {
8929
- 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"));
8930
9077
  }
8931
9078
  }
8932
9079
  console.log("");
@@ -8996,7 +9143,7 @@ var COMMANDS = [
8996
9143
  },
8997
9144
  // === 订阅 ===
8998
9145
  {
8999
- // 顶层快捷方式:`mihomo use <name>` = `mihomo subscription use <name>`(与 tun 同范式)
9146
+ // 顶层快捷方式:`mihomo-cli use <name>` = `mihomo-cli subscription use <name>`(与 tun 同范式)
9000
9147
  name: "use",
9001
9148
  aliases: [],
9002
9149
  handler: cmdSubscription,
@@ -9051,7 +9198,7 @@ var COMMANDS = [
9051
9198
  aliases: [],
9052
9199
  handler: cmdKernel,
9053
9200
  group: "system",
9054
- 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" }]
9055
9202
  },
9056
9203
  {
9057
9204
  name: "update",
@@ -9144,26 +9291,29 @@ function readSelfMixedPortEarly() {
9144
9291
  }
9145
9292
  function clearProxyEnv() {
9146
9293
  const selfPort = readSelfMixedPortEarly();
9294
+ const cleared = {};
9147
9295
  for (const key of PROXY_ENV_KEYS) {
9148
9296
  const value = process.env[key];
9149
9297
  if (value && proxyEnvPointsAtSelf(value, selfPort)) {
9298
+ cleared[key] = value;
9150
9299
  delete process.env[key];
9151
9300
  }
9152
9301
  }
9302
+ if (Object.keys(cleared).length > 0) recordClearedProxyEnv(cleared);
9153
9303
  }
9154
9304
  var GUARD_EXEMPT_COMMANDS = /* @__PURE__ */ new Set(["help", "version"]);
9155
9305
  function assertNotRoot(commandName) {
9156
9306
  const uid = process.getuid?.();
9157
9307
  if (uid !== 0) return;
9158
9308
  if (GUARD_EXEMPT_COMMANDS.has(commandName)) return;
9159
- throw new CliError("\u8BF7\u4E0D\u8981\u7528 sudo \u8FD0\u884C mihomo", {
9309
+ throw new CliError("\u8BF7\u4E0D\u8981\u7528 sudo \u8FD0\u884C mihomo-cli", {
9160
9310
  label: "\u8EAB\u4EFD\u9519\u8BEF",
9161
9311
  hint: [
9162
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",
9163
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",
9164
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",
9165
9315
  "",
9166
- `\u8BF7\u53BB\u6389 sudo \u91CD\u8BD5: mihomo ${commandName}`,
9316
+ `\u8BF7\u53BB\u6389 sudo \u91CD\u8BD5: mihomo-cli ${commandName}`,
9167
9317
  "",
9168
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"
9169
9319
  ]
@@ -9210,7 +9360,7 @@ async function main() {
9210
9360
  if (!command) {
9211
9361
  const suggestion = suggestSimilar(token, allCommandTokens());
9212
9362
  throw new CliError(`\u672A\u77E5\u547D\u4EE4: ${token}`, {
9213
- 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']
9214
9364
  });
9215
9365
  }
9216
9366
  assertSupportedNodeVersion(command.name);
@@ -9223,7 +9373,7 @@ async function main() {
9223
9373
  }
9224
9374
  if (command.group === "meta") {
9225
9375
  assertKnownFlags(args.slice(1), [], command.name);
9226
- assertPositionalCount(args, command.name === "help" ? 1 : 0, 1, `mihomo ${command.name}`);
9376
+ assertPositionalCount(args, command.name === "help" ? 1 : 0, 1, `mihomo-cli ${command.name}`);
9227
9377
  }
9228
9378
  if (command.group !== "meta" && (args[1] === "-h" || args[1] === "--help" || args[1] === "help")) {
9229
9379
  printCommandHelp(command);