mihomo-cli 4.3.0 → 4.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +51 -0
- package/README.md +69 -17
- package/dist/index.js +900 -787
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -3076,9 +3076,8 @@ var RAW_SERVICE_LABEL = process.env.MIHOMO_CLI_DAEMON_LABEL;
|
|
|
3076
3076
|
var RAW_SERVICE_LABEL_INPUT = RAW_SERVICE_LABEL;
|
|
3077
3077
|
var SERVICE_LABEL = RAW_SERVICE_LABEL && isValidServiceLabel(RAW_SERVICE_LABEL) ? RAW_SERVICE_LABEL : DEFAULT_SERVICE_LABEL;
|
|
3078
3078
|
var SERVICE_BINARY_NAME = "mihomo-cli-service";
|
|
3079
|
+
var DEFAULT_MIXED_PORT = 7890;
|
|
3079
3080
|
var CONTROLLER_PORT = 9090;
|
|
3080
|
-
var CONTROLLER_ADDR = `127.0.0.1:${CONTROLLER_PORT}`;
|
|
3081
|
-
var CONTROLLER_BASE_URL = `http://${CONTROLLER_ADDR}`;
|
|
3082
3081
|
var TUN_CONFIG = {
|
|
3083
3082
|
tun: {
|
|
3084
3083
|
enable: true,
|
|
@@ -3090,9 +3089,9 @@ var TUN_CONFIG = {
|
|
|
3090
3089
|
}
|
|
3091
3090
|
};
|
|
3092
3091
|
var BASE_CONFIG = {
|
|
3093
|
-
|
|
3092
|
+
// 注意:mixed-port 与 external-controller 不在此表——它们来自 settings.ports(getPorts),
|
|
3093
|
+
// config.ts 单独写入 systemConfig,订阅/覆写恒不可改
|
|
3094
3094
|
"allow-lan": false,
|
|
3095
|
-
"external-controller": CONTROLLER_ADDR,
|
|
3096
3095
|
"unified-delay": true,
|
|
3097
3096
|
"tcp-concurrent": true,
|
|
3098
3097
|
"geo-auto-update": true,
|
|
@@ -3110,7 +3109,6 @@ var BASE_CONFIG = {
|
|
|
3110
3109
|
}
|
|
3111
3110
|
};
|
|
3112
3111
|
var DEFAULT_UPDATE_INTERVAL_HOURS = 12;
|
|
3113
|
-
var DEFAULT_UPDATE_INTERVAL_HOURS_GITHUB = 6;
|
|
3114
3112
|
var DEFAULT_AUTO_UPDATE_TIMEOUT = 1e4;
|
|
3115
3113
|
|
|
3116
3114
|
// src/errors.ts
|
|
@@ -3327,6 +3325,14 @@ function recoverCorruptedSettings() {
|
|
|
3327
3325
|
settingsCache = {};
|
|
3328
3326
|
return settingsCache;
|
|
3329
3327
|
}
|
|
3328
|
+
function isValidSettingsContent(content) {
|
|
3329
|
+
try {
|
|
3330
|
+
const parsed = JSON.parse(content);
|
|
3331
|
+
return parsed !== null && typeof parsed === "object" && !Array.isArray(parsed);
|
|
3332
|
+
} catch {
|
|
3333
|
+
return false;
|
|
3334
|
+
}
|
|
3335
|
+
}
|
|
3330
3336
|
function writeSettings(settings) {
|
|
3331
3337
|
ensureDirs();
|
|
3332
3338
|
return withFileLock(PATHS.settingsFile, () => writeSettingsUnlocked(settings));
|
|
@@ -3353,6 +3359,32 @@ function updateSettings(mutate) {
|
|
|
3353
3359
|
function invalidateSettingsCache() {
|
|
3354
3360
|
settingsCache = null;
|
|
3355
3361
|
}
|
|
3362
|
+
function validatePort(value, key) {
|
|
3363
|
+
if (value === void 0) return void 0;
|
|
3364
|
+
if (typeof value !== "number" || !Number.isInteger(value) || value < 1 || value > 65535) {
|
|
3365
|
+
throw new CliError(`settings.json \u7684 ${key} \u9700\u4E3A 1-65535 \u7684\u6574\u6570\uFF0C\u5F53\u524D\u662F ${JSON.stringify(value)}`, {
|
|
3366
|
+
label: "\u914D\u7F6E\u9519\u8BEF",
|
|
3367
|
+
hint: ["\u793A\u4F8B:", ' "ports": { "mixed": 17890, "controller": 19090 }', "\u4E24\u4E2A\u952E\u5747\u53EF\u9009\uFF1B\u5220\u6389 ports \u5219\u56DE\u5230\u9ED8\u8BA4\u7AEF\u53E3 7890/9090"]
|
|
3368
|
+
});
|
|
3369
|
+
}
|
|
3370
|
+
return value;
|
|
3371
|
+
}
|
|
3372
|
+
function getPorts() {
|
|
3373
|
+
const ports = readSettings().ports;
|
|
3374
|
+
if (ports === void 0) return { mixed: DEFAULT_MIXED_PORT, controller: CONTROLLER_PORT };
|
|
3375
|
+
if (ports === null || typeof ports !== "object" || Array.isArray(ports)) {
|
|
3376
|
+
throw new CliError('settings.json \u7684 ports \u9700\u4E3A\u5BF9\u8C61\uFF0C\u5982 { "mixed": 17890, "controller": 19090 }', { label: "\u914D\u7F6E\u9519\u8BEF" });
|
|
3377
|
+
}
|
|
3378
|
+
const mixed = validatePort(ports.mixed, "ports.mixed");
|
|
3379
|
+
const controller = validatePort(ports.controller, "ports.controller");
|
|
3380
|
+
if (mixed !== void 0 && controller !== void 0 && mixed === controller) {
|
|
3381
|
+
throw new CliError(`ports.mixed \u4E0E ports.controller \u4E0D\u80FD\u76F8\u540C\uFF08\u5F53\u524D\u5747\u4E3A ${mixed}\uFF09`, {
|
|
3382
|
+
label: "\u914D\u7F6E\u9519\u8BEF",
|
|
3383
|
+
hint: ["\u6DF7\u5408\u7AEF\u53E3\u4E0E\u63A7\u5236\u5668\u7AEF\u53E3\u5404\u9700\u72EC\u7ACB\u7AEF\u53E3\uFF0C\u76F8\u540C\u4F1A\u5BFC\u81F4\u5185\u6838\u542F\u52A8\u5931\u8D25"]
|
|
3384
|
+
});
|
|
3385
|
+
}
|
|
3386
|
+
return { mixed: mixed ?? DEFAULT_MIXED_PORT, controller: controller ?? CONTROLLER_PORT };
|
|
3387
|
+
}
|
|
3356
3388
|
function maskSingleUrl(url) {
|
|
3357
3389
|
try {
|
|
3358
3390
|
const parsed = new URL(url);
|
|
@@ -4275,8 +4307,9 @@ function buildConfig(subRawContent, mode, scope) {
|
|
|
4275
4307
|
systemConfig[key] = value;
|
|
4276
4308
|
}
|
|
4277
4309
|
}
|
|
4278
|
-
|
|
4279
|
-
systemConfig["
|
|
4310
|
+
const ports = getPorts();
|
|
4311
|
+
systemConfig["external-controller"] = `127.0.0.1:${ports.controller}`;
|
|
4312
|
+
systemConfig["mixed-port"] = ports.mixed;
|
|
4280
4313
|
delete withOverwrites["mixed-port"];
|
|
4281
4314
|
delete withOverwrites.port;
|
|
4282
4315
|
delete withOverwrites["socks-port"];
|
|
@@ -4990,17 +5023,18 @@ function logOversized() {
|
|
|
4990
5023
|
async function tryHotReload() {
|
|
4991
5024
|
const status = getServiceStatus();
|
|
4992
5025
|
if (!status.running || status.pid === null) return false;
|
|
5026
|
+
const baseUrl = `http://127.0.0.1:${getPorts().controller}`;
|
|
4993
5027
|
const controller = new AbortController();
|
|
4994
5028
|
const timer = setTimeout(() => controller.abort(), HOT_RELOAD_TIMEOUT_MS);
|
|
4995
5029
|
const secret = readSettings().controller_secret;
|
|
4996
5030
|
const headers = { "Content-Type": "application/json" };
|
|
4997
5031
|
if (secret) headers.Authorization = `Bearer ${secret}`;
|
|
4998
5032
|
try {
|
|
4999
|
-
const probe = await fetch(`${
|
|
5033
|
+
const probe = await fetch(`${baseUrl}/version`, { headers, signal: controller.signal });
|
|
5000
5034
|
if (!probe.ok) return false;
|
|
5001
5035
|
const info = await probe.json();
|
|
5002
5036
|
if (typeof info?.version !== "string") return false;
|
|
5003
|
-
const res = await fetch(`${
|
|
5037
|
+
const res = await fetch(`${baseUrl}/configs?force=true`, {
|
|
5004
5038
|
method: "PUT",
|
|
5005
5039
|
headers,
|
|
5006
5040
|
body: "{}",
|
|
@@ -5119,223 +5153,9 @@ function printVersion() {
|
|
|
5119
5153
|
}
|
|
5120
5154
|
|
|
5121
5155
|
// src/commands/completion.ts
|
|
5122
|
-
|
|
5123
|
-
|
|
5124
|
-
|
|
5125
|
-
{ word: "stop", desc: "\u505C\u6B62\u4EE3\u7406\u5E76\u5173\u95ED\u767B\u5F55\u81EA\u542F" },
|
|
5126
|
-
{ word: "uninstall", desc: "\u5378\u8F7D\u670D\u52A1" },
|
|
5127
|
-
{ word: "status", desc: "\u67E5\u770B\u72B6\u6001\uFF08--json \u673A\u5668\u53EF\u8BFB\uFF09" },
|
|
5128
|
-
{ word: "logs", desc: "\u65E5\u5FD7\u5217\u8868/\u67E5\u770B" },
|
|
5129
|
-
{ word: "ui", desc: "\u6253\u5F00 Web UI" },
|
|
5130
|
-
{ word: "subscription", desc: "\u8BA2\u9605\u7BA1\u7406" },
|
|
5131
|
-
{ word: "overwrite", desc: "\u8986\u5199\u914D\u7F6E" },
|
|
5132
|
-
{ word: "directory", desc: "\u6570\u636E\u76EE\u5F55" },
|
|
5133
|
-
{ word: "kernel", desc: "\u66F4\u65B0\u5185\u6838" },
|
|
5134
|
-
{ word: "update", desc: "\u66F4\u65B0 mihomo-cli" },
|
|
5135
|
-
{ word: "reset", desc: "\u91CD\u7F6E\u7528\u6237\u6570\u636E" },
|
|
5136
|
-
{ word: "doctor", desc: "\u4F53\u68C0\u8BCA\u65AD" },
|
|
5137
|
-
{ word: "completion", desc: "\u8F93\u51FA shell \u8865\u5168\u811A\u672C" },
|
|
5138
|
-
{ word: "use", desc: "\u5207\u6362\u8BA2\u9605\uFF08subscription use \u5FEB\u6377\u65B9\u5F0F\uFF09" },
|
|
5139
|
-
{ word: "tun", desc: "\u4E34\u65F6 TUN \u900F\u660E\u4EE3\u7406\uFF08= start tun\uFF09" },
|
|
5140
|
-
{ word: "help", desc: "\u663E\u793A\u5E2E\u52A9" },
|
|
5141
|
-
{ word: "version", desc: "\u663E\u793A\u7248\u672C" }
|
|
5142
|
-
];
|
|
5143
|
-
var SUBCOMMANDS = {
|
|
5144
|
-
subscription: [
|
|
5145
|
-
{ word: "use", desc: "\u5207\u6362\u8BA2\u9605" },
|
|
5146
|
-
{ word: "add", desc: "\u6DFB\u52A0\u8BA2\u9605" },
|
|
5147
|
-
{ word: "update", desc: "\u66F4\u65B0\u8BA2\u9605" },
|
|
5148
|
-
{ word: "remove", desc: "\u5220\u9664\u8BA2\u9605" },
|
|
5149
|
-
{ word: "rm", desc: "\u5220\u9664\u8BA2\u9605" },
|
|
5150
|
-
{ word: "delete", desc: "\u5220\u9664\u8BA2\u9605" }
|
|
5151
|
-
],
|
|
5152
|
-
overwrite: [
|
|
5153
|
-
{ word: "on", desc: "\u542F\u7528\u8986\u5199" },
|
|
5154
|
-
{ word: "off", desc: "\u7981\u7528\u8986\u5199" },
|
|
5155
|
-
{ word: "enable", desc: "\u542F\u7528\u8986\u5199" },
|
|
5156
|
-
{ word: "disable", desc: "\u7981\u7528\u8986\u5199" }
|
|
5157
|
-
],
|
|
5158
|
-
directory: [{ word: "open", desc: "\u6253\u5F00\u76EE\u5F55" }]
|
|
5159
|
-
};
|
|
5160
|
-
var DIR_TARGETS = ["root", "subs", "logs", "data", "runtime", "kernel"];
|
|
5161
|
-
var UI_NAMES = ["zash", "dash", "yacd"];
|
|
5162
|
-
var SHELLS = ["zsh", "bash", "fish"];
|
|
5163
|
-
function buildZsh() {
|
|
5164
|
-
const lines = [
|
|
5165
|
-
"#compdef mihomo mhm mh mihomo-cli",
|
|
5166
|
-
"",
|
|
5167
|
-
'# mihomo-cli zsh \u8865\u5168\uFF08mihomo completion zsh \u751F\u6210\uFF1B\u5B89\u88C5: \u5199\u5165 ~/.zsh/completions/_mihomo \u6216 eval "$(mihomo completion zsh)"\uFF09',
|
|
5168
|
-
"",
|
|
5169
|
-
"_mihomo() {",
|
|
5170
|
-
" local -a commands subcmds",
|
|
5171
|
-
" commands=(",
|
|
5172
|
-
...COMMAND_WORDS.map((c) => ` '${c.word}:${c.desc.replace(/'/g, "''")}'`),
|
|
5173
|
-
" )",
|
|
5174
|
-
"",
|
|
5175
|
-
" _arguments -C \\",
|
|
5176
|
-
" '1: :->cmd' \\",
|
|
5177
|
-
" '*::arg:->args'",
|
|
5178
|
-
"",
|
|
5179
|
-
" case $state in",
|
|
5180
|
-
" cmd)",
|
|
5181
|
-
" _describe 'command' commands",
|
|
5182
|
-
" ;;",
|
|
5183
|
-
" args)",
|
|
5184
|
-
" case ${words[1]} in"
|
|
5185
|
-
];
|
|
5186
|
-
for (const [group, subs] of Object.entries(SUBCOMMANDS)) {
|
|
5187
|
-
const aliases = group === "subscription" ? "subscription|sub|subs|subscriptions" : group === "overwrite" ? "overwrite|ow" : "directory|dir|dirs|directories";
|
|
5188
|
-
lines.push(` ${aliases})`);
|
|
5189
|
-
lines.push(" subcmds=(");
|
|
5190
|
-
for (const s of subs) lines.push(` '${s.word}:${s.desc.replace(/'/g, "''")}'`);
|
|
5191
|
-
lines.push(" )");
|
|
5192
|
-
lines.push(" if (( CURRENT == 2 )); then");
|
|
5193
|
-
lines.push(" _describe 'subcommand' subcmds");
|
|
5194
|
-
lines.push(" else");
|
|
5195
|
-
lines.push(" _files");
|
|
5196
|
-
lines.push(" fi");
|
|
5197
|
-
lines.push(" ;;");
|
|
5198
|
-
}
|
|
5199
|
-
lines.push(
|
|
5200
|
-
" directory|dir|dirs|directories)",
|
|
5201
|
-
" if (( CURRENT == 3 )); then",
|
|
5202
|
-
` _values 'target' ${DIR_TARGETS.join(" ")}`,
|
|
5203
|
-
" fi",
|
|
5204
|
-
" ;;",
|
|
5205
|
-
" logs)",
|
|
5206
|
-
" _arguments '-f[\u5B9E\u65F6\u8DDF\u968F]' '-n[\u663E\u793A\u884C\u6570]:\u884C\u6570:' '-o[\u7CFB\u7EDF\u9ED8\u8BA4\u7A0B\u5E8F\u6253\u5F00]' '1::\u7F16\u53F7:'",
|
|
5207
|
-
" ;;",
|
|
5208
|
-
" ui)",
|
|
5209
|
-
` _values 'ui' ${UI_NAMES.join(" ")}`,
|
|
5210
|
-
" ;;",
|
|
5211
|
-
" kernel)",
|
|
5212
|
-
" _arguments '--mirror[\u8D70\u955C\u50CF\u4E0B\u8F7D]:\u955C\u50CF:' '--no-mirror[\u76F4\u8FDE\u4E0B\u8F7D]'",
|
|
5213
|
-
" ;;",
|
|
5214
|
-
" reset)",
|
|
5215
|
-
` _values 'target' subs logs data runtime settings kernel overwrites service`,
|
|
5216
|
-
" _arguments '-y[\u8DF3\u8FC7\u786E\u8BA4]' '--full[\u5220\u5168\u90E8]'",
|
|
5217
|
-
" ;;",
|
|
5218
|
-
" completion)",
|
|
5219
|
-
` _values 'shell' ${SHELLS.join(" ")}`,
|
|
5220
|
-
" ;;",
|
|
5221
|
-
" *)",
|
|
5222
|
-
" _files",
|
|
5223
|
-
" ;;",
|
|
5224
|
-
" esac",
|
|
5225
|
-
" ;;",
|
|
5226
|
-
" esac",
|
|
5227
|
-
"}",
|
|
5228
|
-
"",
|
|
5229
|
-
'_mihomo "$@"'
|
|
5230
|
-
);
|
|
5231
|
-
return lines.join("\n");
|
|
5232
|
-
}
|
|
5233
|
-
function buildBash() {
|
|
5234
|
-
const top = COMMAND_WORDS.map((c) => c.word).join(" ");
|
|
5235
|
-
return `# mihomo-cli bash \u8865\u5168\uFF08mihomo completion bash \u751F\u6210\uFF1B\u5B89\u88C5: eval "$(mihomo completion bash)"\uFF09
|
|
5236
|
-
|
|
5237
|
-
_mihomo_completions() {
|
|
5238
|
-
local cur prev
|
|
5239
|
-
COMPREPLY=()
|
|
5240
|
-
cur="\${COMP_WORDS[COMP_CWORD]}"
|
|
5241
|
-
prev="\${COMP_WORDS[COMP_CWORD-1]}"
|
|
5242
|
-
|
|
5243
|
-
if [[ \${COMP_CWORD} -eq 1 ]]; then
|
|
5244
|
-
COMPREPLY=( $(compgen -W "${top}" -- "\${cur}") )
|
|
5245
|
-
return 0
|
|
5246
|
-
fi
|
|
5247
|
-
|
|
5248
|
-
case "\${COMP_WORDS[1]}" in
|
|
5249
|
-
subscription|sub|subs|subscriptions)
|
|
5250
|
-
if [[ \${COMP_CWORD} -eq 2 ]]; then
|
|
5251
|
-
COMPREPLY=( $(compgen -W "use add update remove rm delete" -- "\${cur}") )
|
|
5252
|
-
fi
|
|
5253
|
-
;;
|
|
5254
|
-
overwrite|ow)
|
|
5255
|
-
if [[ \${COMP_CWORD} -eq 2 ]]; then
|
|
5256
|
-
COMPREPLY=( $(compgen -W "on off enable disable" -- "\${cur}") )
|
|
5257
|
-
fi
|
|
5258
|
-
;;
|
|
5259
|
-
directory|dir|dirs|directories)
|
|
5260
|
-
if [[ \${COMP_CWORD} -eq 2 ]]; then
|
|
5261
|
-
COMPREPLY=( $(compgen -W "open" -- "\${cur}") )
|
|
5262
|
-
elif [[ "\${COMP_WORDS[2]}" == "open" ]]; then
|
|
5263
|
-
COMPREPLY=( $(compgen -W "${DIR_TARGETS.join(" ")}" -- "\${cur}") )
|
|
5264
|
-
fi
|
|
5265
|
-
;;
|
|
5266
|
-
logs)
|
|
5267
|
-
COMPREPLY=( $(compgen -W "-f -n -o --help" -- "\${cur}") )
|
|
5268
|
-
;;
|
|
5269
|
-
ui)
|
|
5270
|
-
COMPREPLY=( $(compgen -W "${UI_NAMES.join(" ")}" -- "\${cur}") )
|
|
5271
|
-
;;
|
|
5272
|
-
kernel)
|
|
5273
|
-
COMPREPLY=( $(compgen -W "--mirror --no-mirror" -- "\${cur}") )
|
|
5274
|
-
;;
|
|
5275
|
-
reset)
|
|
5276
|
-
COMPREPLY=( $(compgen -W "subs logs data runtime settings kernel overwrites service --full -y" -- "\${cur}") )
|
|
5277
|
-
;;
|
|
5278
|
-
completion)
|
|
5279
|
-
COMPREPLY=( $(compgen -W "${SHELLS.join(" ")}" -- "\${cur}") )
|
|
5280
|
-
;;
|
|
5281
|
-
esac
|
|
5282
|
-
}
|
|
5283
|
-
complete -F _mihomo_completions mihomo mhm mh mihomo-cli
|
|
5284
|
-
`;
|
|
5285
|
-
}
|
|
5286
|
-
function buildFish() {
|
|
5287
|
-
const lines = [
|
|
5288
|
-
"# mihomo-cli fish \u8865\u5168\uFF08mihomo completion fish \u751F\u6210\uFF1B\u5B89\u88C5: mihomo completion fish | source\uFF09",
|
|
5289
|
-
"",
|
|
5290
|
-
"for cmd in mihomo mhm mh mihomo-cli"
|
|
5291
|
-
];
|
|
5292
|
-
for (const c of COMMAND_WORDS) {
|
|
5293
|
-
lines.push(` complete -c $cmd -f -a '${c.word}' -d '${c.desc.replace(/'/g, "\\'")}'`);
|
|
5294
|
-
}
|
|
5295
|
-
const subAliases = {
|
|
5296
|
-
subscription: "subscription sub subs subscriptions",
|
|
5297
|
-
overwrite: "overwrite ow",
|
|
5298
|
-
directory: "directory dir dirs directories"
|
|
5299
|
-
};
|
|
5300
|
-
for (const [group, subs] of Object.entries(SUBCOMMANDS)) {
|
|
5301
|
-
for (const s of subs) {
|
|
5302
|
-
lines.push(` complete -c $cmd -n "__fish_seen_subcommand_from ${subAliases[group]}" -a '${s.word}' -d '${s.desc}'`);
|
|
5303
|
-
}
|
|
5304
|
-
}
|
|
5305
|
-
lines.push(` complete -c $cmd -n "__fish_seen_subcommand_from directory dir dirs directories open" -a '${DIR_TARGETS.join(" ")}'`);
|
|
5306
|
-
lines.push(` complete -c $cmd -n "__fish_seen_subcommand_from ui" -a '${UI_NAMES.join(" ")}'`);
|
|
5307
|
-
lines.push(` complete -c $cmd -n "__fish_seen_subcommand_from completion" -a '${SHELLS.join(" ")}'`);
|
|
5308
|
-
lines.push(` complete -c $cmd -n "__fish_seen_subcommand_from kernel" -a '\\--mirror' -d '\u8D70\u955C\u50CF\u4E0B\u8F7D'`);
|
|
5309
|
-
lines.push(` complete -c $cmd -n "__fish_seen_subcommand_from kernel" -a '\\--no-mirror' -d '\u76F4\u8FDE\u4E0B\u8F7D'`);
|
|
5310
|
-
lines.push("end");
|
|
5311
|
-
return lines.join("\n");
|
|
5312
|
-
}
|
|
5313
|
-
function buildCompletionScript(shell) {
|
|
5314
|
-
switch (shell) {
|
|
5315
|
-
case "zsh":
|
|
5316
|
-
return buildZsh();
|
|
5317
|
-
case "bash":
|
|
5318
|
-
return buildBash();
|
|
5319
|
-
case "fish":
|
|
5320
|
-
return buildFish();
|
|
5321
|
-
default: {
|
|
5322
|
-
const suggestion = suggestSimilar(shell, [...SHELLS]);
|
|
5323
|
-
throw new CliError(`\u672A\u77E5\u7684 shell: ${shell}`, {
|
|
5324
|
-
label: "\u53C2\u6570\u9519\u8BEF",
|
|
5325
|
-
hint: [...suggestion.length > 0 ? [`\u662F\u5426\u60F3\u8F93\u5165: ${suggestion.join(" / ")}?`] : [], `\u7528\u6CD5: mihomo completion <${SHELLS.join("|")}>`]
|
|
5326
|
-
});
|
|
5327
|
-
}
|
|
5328
|
-
}
|
|
5329
|
-
}
|
|
5330
|
-
function cmdCompletion(args) {
|
|
5331
|
-
const shell = args[1];
|
|
5332
|
-
if (!shell) {
|
|
5333
|
-
throw new CliError("\u8BF7\u6307\u5B9A shell", {
|
|
5334
|
-
hint: [`\u7528\u6CD5: mihomo completion <${SHELLS.join("|")}>`, '\u5B89\u88C5: eval "$(mihomo completion zsh)"\uFF0C\u6216\u5199\u5165\u5BF9\u5E94 shell \u7684\u8865\u5168\u76EE\u5F55']
|
|
5335
|
-
});
|
|
5336
|
-
}
|
|
5337
|
-
console.log(buildCompletionScript(shell));
|
|
5338
|
-
}
|
|
5156
|
+
import fs10 from "fs";
|
|
5157
|
+
import os2 from "os";
|
|
5158
|
+
import path8 from "path";
|
|
5339
5159
|
|
|
5340
5160
|
// src/open.ts
|
|
5341
5161
|
import { spawn } from "child_process";
|
|
@@ -5507,8 +5327,8 @@ function getRuntimeMode() {
|
|
|
5507
5327
|
if (isServiceInstalled()) return "mixed";
|
|
5508
5328
|
return getConfigInfo()?.tun ? "tun" : "mixed";
|
|
5509
5329
|
}
|
|
5510
|
-
function getRunningState() {
|
|
5511
|
-
const service = getServiceStatus();
|
|
5330
|
+
function getRunningState(serviceStatus) {
|
|
5331
|
+
const service = serviceStatus ?? getServiceStatus();
|
|
5512
5332
|
if (service.running) {
|
|
5513
5333
|
return { running: true, pid: service.pid, kind: "service", processInfo: null };
|
|
5514
5334
|
}
|
|
@@ -5639,14 +5459,15 @@ async function withSpinner(label, fn) {
|
|
|
5639
5459
|
}
|
|
5640
5460
|
|
|
5641
5461
|
// src/subscription.ts
|
|
5642
|
-
function
|
|
5643
|
-
return
|
|
5462
|
+
function resolveUpdateInterval(cachedInterval) {
|
|
5463
|
+
return cachedInterval && cachedInterval > 0 ? cachedInterval : DEFAULT_UPDATE_INTERVAL_HOURS;
|
|
5644
5464
|
}
|
|
5645
|
-
function
|
|
5646
|
-
|
|
5647
|
-
|
|
5648
|
-
|
|
5649
|
-
|
|
5465
|
+
function isSubscriptionStale(cached, nowMs = Date.now()) {
|
|
5466
|
+
if (!cached.updated_at) return false;
|
|
5467
|
+
const t = new Date(cached.updated_at).getTime();
|
|
5468
|
+
if (Number.isNaN(t) || t > nowMs) return false;
|
|
5469
|
+
const ageHours = (nowMs - t) / 36e5;
|
|
5470
|
+
return ageHours > resolveUpdateInterval(cached.update_interval);
|
|
5650
5471
|
}
|
|
5651
5472
|
var HTTP_CLIENT = createHttpClient({ timeout: 6e4 });
|
|
5652
5473
|
function isValidHttpUrl(url) {
|
|
@@ -5850,7 +5671,7 @@ function needsAutoUpdate(sub) {
|
|
|
5850
5671
|
const lastUpdate = new Date(sub.updated_at).getTime();
|
|
5851
5672
|
if (Number.isNaN(lastUpdate)) return true;
|
|
5852
5673
|
if (lastUpdate > Date.now()) return true;
|
|
5853
|
-
const intervalHours = resolveUpdateInterval(sub.
|
|
5674
|
+
const intervalHours = resolveUpdateInterval(sub.update_interval);
|
|
5854
5675
|
const intervalMs = intervalHours * 60 * 60 * 1e3;
|
|
5855
5676
|
return Date.now() - lastUpdate > intervalMs;
|
|
5856
5677
|
}
|
|
@@ -5877,7 +5698,7 @@ async function autoUpdateStaleSubscription(options = {}) {
|
|
|
5877
5698
|
}
|
|
5878
5699
|
if (staleSubs.length === 1) {
|
|
5879
5700
|
const sub = staleSubs[0];
|
|
5880
|
-
const interval = resolveUpdateInterval(sub.
|
|
5701
|
+
const interval = resolveUpdateInterval(sub.update_interval);
|
|
5881
5702
|
console.log(`\u8BA2\u9605 "${sub.name}" \u8D85\u8FC7 ${interval} \u5C0F\u65F6\u672A\u66F4\u65B0\uFF0C\u6B63\u5728\u66F4\u65B0...`);
|
|
5882
5703
|
} else {
|
|
5883
5704
|
console.log(`\u68C0\u67E5\u5230 ${staleSubs.length} \u4E2A\u8BA2\u9605\u9700\u8981\u66F4\u65B0\uFF0C\u6B63\u5728\u5E76\u884C\u66F4\u65B0...`);
|
|
@@ -5918,7 +5739,7 @@ var PROBE_URL = "https://www.gstatic.com/generate_204";
|
|
|
5918
5739
|
function isProbeSuccessStatus(code) {
|
|
5919
5740
|
return code !== null && code >= 200 && code < 300;
|
|
5920
5741
|
}
|
|
5921
|
-
async function probeProxyConnectivity(port, timeoutMs =
|
|
5742
|
+
async function probeProxyConnectivity(port, timeoutMs = 2e3) {
|
|
5922
5743
|
const start = Date.now();
|
|
5923
5744
|
try {
|
|
5924
5745
|
const { stdout } = await execFileAsync(
|
|
@@ -6000,6 +5821,8 @@ function buildStatusJson(args) {
|
|
|
6000
5821
|
...args.cached?.download !== void 0 ? { download: args.cached.download } : {},
|
|
6001
5822
|
...args.cached?.total !== void 0 ? { total: args.cached.total } : {},
|
|
6002
5823
|
...args.cached?.expire !== void 0 ? { expire: args.cached.expire } : {},
|
|
5824
|
+
...args.cached?.updated_at !== void 0 ? { updatedAt: args.cached.updated_at } : {},
|
|
5825
|
+
stale: args.cached ? isSubscriptionStale(args.cached) : false,
|
|
6003
5826
|
urgency
|
|
6004
5827
|
} : null,
|
|
6005
5828
|
overwrite: { enabled: args.overwriteEnabled, files: args.overwriteFiles },
|
|
@@ -6015,8 +5838,9 @@ function buildStatusJson(args) {
|
|
|
6015
5838
|
}
|
|
6016
5839
|
async function printStatus(args = []) {
|
|
6017
5840
|
const asJson = hasFlag(args, "-j", "--json");
|
|
6018
|
-
const
|
|
5841
|
+
const skipProbe = hasFlag(args, "--no-probe");
|
|
6019
5842
|
const service = getServiceStatus();
|
|
5843
|
+
const state = getRunningState(service);
|
|
6020
5844
|
const info = getConfigInfo();
|
|
6021
5845
|
const overwriteEnabled = isOverwriteEnabled();
|
|
6022
5846
|
const overwriteFiles = listOverwriteFile().files;
|
|
@@ -6025,7 +5849,7 @@ async function printStatus(args = []) {
|
|
|
6025
5849
|
const legacy = detectLegacySystemInstall();
|
|
6026
5850
|
const { running, pid, kind } = state;
|
|
6027
5851
|
let probe = null;
|
|
6028
|
-
if (running && info?.mixedPort) {
|
|
5852
|
+
if (running && info?.mixedPort && !skipProbe) {
|
|
6029
5853
|
probe = await probeProxyConnectivity(info.mixedPort);
|
|
6030
5854
|
}
|
|
6031
5855
|
if (asJson) {
|
|
@@ -6103,6 +5927,14 @@ async function printStatus(args = []) {
|
|
|
6103
5927
|
subLine += ` (${formatProxySummary(info)})`;
|
|
6104
5928
|
}
|
|
6105
5929
|
console.log(subLine);
|
|
5930
|
+
if (cached?.updated_at) {
|
|
5931
|
+
const rel = formatRelativeTime(cached.updated_at) || formatDate(cached.updated_at);
|
|
5932
|
+
if (isSubscriptionStale(cached)) {
|
|
5933
|
+
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`));
|
|
5934
|
+
} else {
|
|
5935
|
+
console.log(`${colors.gray("\u66F4\u65B0: ")}${rel}`);
|
|
5936
|
+
}
|
|
5937
|
+
}
|
|
6106
5938
|
const traffic = cached ? formatTraffic(cached.upload, cached.download, cached.total) : null;
|
|
6107
5939
|
if (traffic) {
|
|
6108
5940
|
console.log(`${colors.gray("\u6D41\u91CF: ")}${trafficColor(traffic, urgency, cached?.total, cached?.upload, cached?.download)}`);
|
|
@@ -6215,6 +6047,10 @@ async function cmdStart(args) {
|
|
|
6215
6047
|
throw new CliError(lines[0], { label: "\u542F\u52A8\u5931\u8D25", hint: lines.slice(1) });
|
|
6216
6048
|
}
|
|
6217
6049
|
await printStatus();
|
|
6050
|
+
if (targetMode === "mixed") {
|
|
6051
|
+
console.log(colors.gray(`\u63D0\u793A: Mixed \u6A21\u5F0F\u9700\u5728\u7CFB\u7EDF\u8BBE\u7F6E\u914D\u7F6E HTTP/SOCKS \u4EE3\u7406 127.0.0.1:${getPorts().mixed}\uFF08TUN \u6A21\u5F0F\u65E0\u9700\uFF09`));
|
|
6052
|
+
console.log("");
|
|
6053
|
+
}
|
|
6218
6054
|
}
|
|
6219
6055
|
|
|
6220
6056
|
// src/commands/shared.ts
|
|
@@ -6308,12 +6144,12 @@ function printDirectoryInfo() {
|
|
|
6308
6144
|
console.log(colors.gray(" \u81EA\u5B9A\u4E49\u6839\u76EE\u5F55: \u73AF\u5883\u53D8\u91CF MIHOMO_CLI_DIR"));
|
|
6309
6145
|
console.log("");
|
|
6310
6146
|
}
|
|
6311
|
-
var
|
|
6147
|
+
var SUBCOMMANDS = [{ name: "open", description: "\u6253\u5F00\u76EE\u5F55", handler: openDirectory }];
|
|
6312
6148
|
async function cmdDirectory(args) {
|
|
6313
|
-
await dispatchSubcommand(args,
|
|
6149
|
+
await dispatchSubcommand(args, SUBCOMMANDS, {
|
|
6314
6150
|
fallback: printDirectoryInfo,
|
|
6315
6151
|
onUnknown: (action) => {
|
|
6316
|
-
const names =
|
|
6152
|
+
const names = SUBCOMMANDS.flatMap((c) => [c.name, ...c.aliases ?? []]);
|
|
6317
6153
|
const suggestion = suggestSimilar(action, names);
|
|
6318
6154
|
throw new CliError(`\u672A\u77E5\u7684\u76EE\u5F55\u5B50\u547D\u4EE4: ${action}`, {
|
|
6319
6155
|
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>"]
|
|
@@ -6322,82 +6158,729 @@ async function cmdDirectory(args) {
|
|
|
6322
6158
|
});
|
|
6323
6159
|
}
|
|
6324
6160
|
|
|
6325
|
-
// src/commands/
|
|
6326
|
-
import
|
|
6327
|
-
|
|
6328
|
-
|
|
6329
|
-
|
|
6330
|
-
|
|
6331
|
-
|
|
6332
|
-
|
|
6333
|
-
|
|
6334
|
-
|
|
6335
|
-
|
|
6336
|
-
|
|
6337
|
-
|
|
6338
|
-
|
|
6339
|
-
checks.push({ name, status, detail, fix });
|
|
6340
|
-
};
|
|
6341
|
-
if (!hasKernel()) {
|
|
6342
|
-
push("\u5185\u6838", "fail", "\u672A\u5B89\u88C5", "mihomo kernel");
|
|
6343
|
-
} else {
|
|
6344
|
-
const v = getKernelVersion();
|
|
6345
|
-
const r = spawnSync5(PATHS.mihomoBinary, ["-v"], { encoding: "utf8", timeout: 5e3 });
|
|
6346
|
-
if (r.status === 0 && /v?\d+\.\d+\.\d+/.test(`${r.stdout}${r.stderr}`)) {
|
|
6347
|
-
push("\u5185\u6838", "ok", v || "\u53EF\u6267\u884C");
|
|
6348
|
-
} else {
|
|
6349
|
-
push("\u5185\u6838", "fail", `\u4E8C\u8FDB\u5236\u65E0\u6CD5\u6267\u884C\uFF08\u9000\u51FA\u7801 ${r.status}\uFF09`, "\u91CD\u65B0\u4E0B\u8F7D: mihomo kernel");
|
|
6350
|
-
}
|
|
6351
|
-
}
|
|
6352
|
-
try {
|
|
6353
|
-
fs10.accessSync(USER_DATA_DIR, fs10.constants.W_OK);
|
|
6354
|
-
push("\u6570\u636E\u76EE\u5F55", "ok", USER_DATA_DIR);
|
|
6355
|
-
} catch {
|
|
6356
|
-
push("\u6570\u636E\u76EE\u5F55", "fail", `\u4E0D\u53EF\u5199: ${USER_DATA_DIR}`, "\u68C0\u67E5\u76EE\u5F55\u6743\u9650");
|
|
6357
|
-
}
|
|
6358
|
-
if (fs10.existsSync(PATHS.settingsFile)) {
|
|
6359
|
-
try {
|
|
6360
|
-
const parsed = JSON.parse(fs10.readFileSync(PATHS.settingsFile, "utf8"));
|
|
6361
|
-
if (parsed !== null && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
6362
|
-
push("\u8BBE\u7F6E\u6587\u4EF6", "ok", "\u683C\u5F0F\u6709\u6548");
|
|
6363
|
-
} else {
|
|
6364
|
-
push("\u8BBE\u7F6E\u6587\u4EF6", "warn", "\u975E\u5BF9\u8C61\uFF0C\u8BFB\u53D6\u65F6\u4F1A\u56DE\u9000\u9ED8\u8BA4\u5E76\u5907\u4EFD\u4E3A .bak", "\u5220\u9664\u6216\u4FEE\u590D settings.json");
|
|
6365
|
-
}
|
|
6366
|
-
} catch {
|
|
6367
|
-
push("\u8BBE\u7F6E\u6587\u4EF6", "warn", "JSON \u635F\u574F\uFF0C\u8BFB\u53D6\u65F6\u4F1A\u56DE\u9000\u9ED8\u8BA4\u5E76\u5907\u4EFD\u4E3A .bak", "\u5220\u9664\u6216\u4FEE\u590D settings.json");
|
|
6368
|
-
}
|
|
6369
|
-
} else {
|
|
6370
|
-
push("\u8BBE\u7F6E\u6587\u4EF6", "ok", "\u672A\u521B\u5EFA\uFF08\u4F7F\u7528\u9ED8\u8BA4\u8BBE\u7F6E\uFF09");
|
|
6371
|
-
}
|
|
6372
|
-
const subs = getSubscriptionsWithCache();
|
|
6373
|
-
const active = getActiveSubscription();
|
|
6374
|
-
if (subs.length === 0) {
|
|
6375
|
-
push("\u8BA2\u9605", "warn", "\u672A\u914D\u7F6E", "mihomo sub add <url>");
|
|
6161
|
+
// src/commands/overwrite.ts
|
|
6162
|
+
import path7 from "path";
|
|
6163
|
+
function printOverwriteList() {
|
|
6164
|
+
const info = listOverwriteFile();
|
|
6165
|
+
const statusText = info.enabled ? colors.green("\u5DF2\u542F\u7528") : colors.yellow("\u5DF2\u7981\u7528");
|
|
6166
|
+
console.log(`${colors.gray("\u72B6\u6001: ")}${statusText}`);
|
|
6167
|
+
console.log(`${colors.gray("\u4F4D\u7F6E: ")}${info.dir}`);
|
|
6168
|
+
console.log("");
|
|
6169
|
+
if (info.files.length === 0) {
|
|
6170
|
+
console.log("\u6682\u65E0\u8986\u5199\u6587\u4EF6");
|
|
6171
|
+
console.log("");
|
|
6172
|
+
console.log(`\u7528\u6CD5\u793A\u4F8B: \u521B\u5EFA\u6587\u4EF6 ${path7.join(info.dir, "overwrite.yaml")}`);
|
|
6173
|
+
console.log(` \u6216 ${path7.join(info.dir, "overwrite.dns.yaml")}`);
|
|
6174
|
+
console.log("");
|
|
6376
6175
|
} else {
|
|
6377
|
-
|
|
6378
|
-
|
|
6379
|
-
|
|
6380
|
-
|
|
6381
|
-
}
|
|
6382
|
-
|
|
6176
|
+
console.log(`${colors.cyan("\u8986\u5199\u6587\u4EF6")} (${info.files.length} \u4E2A\uFF0C\u6309\u987A\u5E8F\u52A0\u8F7D):`);
|
|
6177
|
+
console.log("");
|
|
6178
|
+
info.files.forEach((f, i) => {
|
|
6179
|
+
const num = i < 10 ? ` ${i}` : `${i}`;
|
|
6180
|
+
console.log(` ${num}. ${f.name}`);
|
|
6181
|
+
if (f.scope) {
|
|
6182
|
+
console.log(` ${colors.gray("\u4F5C\u7528\u57DF: ")}${f.scope}`);
|
|
6383
6183
|
}
|
|
6384
|
-
|
|
6385
|
-
|
|
6386
|
-
const rel = formatRelativeTime(cached.updated_at);
|
|
6387
|
-
const intervalH = resolveUpdateInterval(cached.url, cached.update_interval);
|
|
6388
|
-
const ageH = (Date.now() - new Date(cached.updated_at).getTime()) / 36e5;
|
|
6389
|
-
if (ageH > intervalH) {
|
|
6390
|
-
push("\u8BA2\u9605\u65B0\u9C9C\u5EA6", "warn", `${rel ?? "\u672A\u77E5"}\u524D\u66F4\u65B0\uFF0C\u5DF2\u8D85\u8FC7 ${intervalH} \u5C0F\u65F6\u95F4\u9694`, "mihomo sub update");
|
|
6391
|
-
} else {
|
|
6392
|
-
push("\u8BA2\u9605\u65B0\u9C9C\u5EA6", "ok", `${rel ?? "\u672A\u77E5"}\u524D\u66F4\u65B0`);
|
|
6393
|
-
}
|
|
6184
|
+
if (f.keys.length > 0) {
|
|
6185
|
+
console.log(` ${colors.gray("\u5B57\u6BB5: ")}${f.keys.join(", ")}`);
|
|
6394
6186
|
}
|
|
6395
|
-
}
|
|
6187
|
+
});
|
|
6188
|
+
console.log("");
|
|
6396
6189
|
}
|
|
6397
|
-
|
|
6398
|
-
|
|
6399
|
-
|
|
6400
|
-
|
|
6190
|
+
console.log("\u542F\u7528\u8986\u5199: mihomo ow on");
|
|
6191
|
+
console.log("\u7981\u7528\u8986\u5199: mihomo ow off");
|
|
6192
|
+
console.log("");
|
|
6193
|
+
}
|
|
6194
|
+
async function setOverwrite(enabled, args) {
|
|
6195
|
+
if (isOverwriteEnabled() === enabled) {
|
|
6196
|
+
console.log(`\u8986\u5199\u914D\u7F6E\u5DF2\u662F${enabled ? "\u542F\u7528" : "\u7981\u7528"}\u72B6\u6001`);
|
|
6197
|
+
console.log("");
|
|
6198
|
+
printOverwriteList();
|
|
6199
|
+
return;
|
|
6200
|
+
}
|
|
6201
|
+
setOverwriteEnabled(enabled);
|
|
6202
|
+
console.log(`\u5DF2${enabled ? "\u542F\u7528" : "\u7981\u7528"}\u8986\u5199\u914D\u7F6E`);
|
|
6203
|
+
if (await restartToApply(args)) return;
|
|
6204
|
+
console.log("");
|
|
6205
|
+
printOverwriteList();
|
|
6206
|
+
}
|
|
6207
|
+
var SUBCOMMANDS2 = [
|
|
6208
|
+
{ name: "on", aliases: ["enable"], description: "\u542F\u7528\u8986\u5199", handler: (args) => setOverwrite(true, args) },
|
|
6209
|
+
{ name: "off", aliases: ["disable"], description: "\u7981\u7528\u8986\u5199", handler: (args) => setOverwrite(false, args) }
|
|
6210
|
+
];
|
|
6211
|
+
async function cmdOverwrite(args) {
|
|
6212
|
+
await dispatchSubcommand(args, SUBCOMMANDS2, {
|
|
6213
|
+
// 无子命令 → 列表;未知子命令 → 报错(与 sub/dir 同构,避免 `ow onn` 静默当成 list)
|
|
6214
|
+
fallback: () => {
|
|
6215
|
+
console.log("");
|
|
6216
|
+
printOverwriteList();
|
|
6217
|
+
},
|
|
6218
|
+
onUnknown: (action) => {
|
|
6219
|
+
const names = SUBCOMMANDS2.flatMap((c) => [c.name, ...c.aliases ?? []]);
|
|
6220
|
+
const suggestion = suggestSimilar(action, names);
|
|
6221
|
+
throw new CliError(`\u672A\u77E5\u7684\u8986\u5199\u5B50\u547D\u4EE4: ${action}`, {
|
|
6222
|
+
hint: [...suggestion.length > 0 ? [`\u662F\u5426\u60F3\u8F93\u5165: ${suggestion.join(" / ")}?`] : [], "", "\u53EF\u7528\u5B50\u547D\u4EE4: on, off"]
|
|
6223
|
+
});
|
|
6224
|
+
}
|
|
6225
|
+
});
|
|
6226
|
+
}
|
|
6227
|
+
|
|
6228
|
+
// src/commands/subscription.ts
|
|
6229
|
+
import { spawnSync as spawnSync5 } from "child_process";
|
|
6230
|
+
function printRestartHintIfRunning() {
|
|
6231
|
+
if (getRunningState().running) {
|
|
6232
|
+
console.log(colors.yellow("\u63D0\u793A: \u8FD0\u884C\u4E2D\u7684\u5B9E\u4F8B\u4ECD\u4F7F\u7528\u65E7\u914D\u7F6E\uFF0C\u6267\u884C mihomo start \u4F7F\u66F4\u65B0\u751F\u6548"));
|
|
6233
|
+
console.log("");
|
|
6234
|
+
}
|
|
6235
|
+
}
|
|
6236
|
+
function printSubscriptionList() {
|
|
6237
|
+
const subs = getSubscriptionsWithCache();
|
|
6238
|
+
if (subs.length === 0) {
|
|
6239
|
+
console.log("\u6CA1\u6709\u8BA2\u9605");
|
|
6240
|
+
console.log("");
|
|
6241
|
+
console.log("\u6DFB\u52A0\u8BA2\u9605: mihomo sub add <url> [name]");
|
|
6242
|
+
console.log("");
|
|
6243
|
+
return;
|
|
6244
|
+
}
|
|
6245
|
+
const activeSub = getActiveSubscription();
|
|
6246
|
+
console.log(colors.cyan("\u8BA2\u9605\u5217\u8868:"));
|
|
6247
|
+
subs.forEach((s, i) => {
|
|
6248
|
+
const rel = formatRelativeTime(s.updated_at);
|
|
6249
|
+
const time = rel ? `${formatDate(s.updated_at)}\uFF08${rel}\uFF09` : formatDate(s.updated_at);
|
|
6250
|
+
const defaultMark = activeSub && s.name === activeSub.name ? colors.green(" [\u4F7F\u7528\u4E2D]") : "";
|
|
6251
|
+
const interval = resolveUpdateInterval(s.update_interval);
|
|
6252
|
+
console.log(` ${i + 1}. ${s.name}${defaultMark}`);
|
|
6253
|
+
console.log(` ${colors.gray("\u66F4\u65B0: ")}${time} (\u95F4\u9694: ${interval}h)`);
|
|
6254
|
+
if (s.username) {
|
|
6255
|
+
console.log(` ${colors.gray("\u7528\u6237: ")}${s.username}`);
|
|
6256
|
+
}
|
|
6257
|
+
const traffic = formatTraffic(s.upload, s.download, s.total);
|
|
6258
|
+
if (traffic) {
|
|
6259
|
+
console.log(` ${colors.gray("\u6D41\u91CF: ")}${traffic}`);
|
|
6260
|
+
}
|
|
6261
|
+
if (s.expire !== void 0) {
|
|
6262
|
+
console.log(` ${colors.gray("\u5230\u671F: ")}${formatTimestamp(s.expire)}`);
|
|
6263
|
+
}
|
|
6264
|
+
if (s.web_page_url) {
|
|
6265
|
+
console.log(` ${colors.gray("\u9875\u9762: ")}${s.web_page_url}`);
|
|
6266
|
+
}
|
|
6267
|
+
});
|
|
6268
|
+
console.log("");
|
|
6269
|
+
console.log("\u5207\u6362\u8BA2\u9605: mihomo sub use <name>");
|
|
6270
|
+
console.log("\u65B0\u589E\u8BA2\u9605: mihomo sub add <url> [name]");
|
|
6271
|
+
console.log("\u66F4\u65B0\u8BA2\u9605: mihomo sub update [name]");
|
|
6272
|
+
console.log("\u5220\u9664\u8BA2\u9605: mihomo sub remove <name>");
|
|
6273
|
+
console.log("");
|
|
6274
|
+
}
|
|
6275
|
+
function assertNotFlagLike(name, usage) {
|
|
6276
|
+
if (name.startsWith("-")) {
|
|
6277
|
+
throw new CliError(`\u540D\u79F0\u4E0D\u80FD\u4EE5 "-" \u5F00\u5934: "${name}"`, {
|
|
6278
|
+
label: "\u53C2\u6570\u9519\u8BEF",
|
|
6279
|
+
hint: ['\u4EE5 "-" \u5F00\u5934\u7684\u540D\u79F0\u4F1A\u4E0E\u547D\u4EE4\u884C\u9009\u9879\u6DF7\u6DC6\uFF0C\u5220\u9664\u65F6\u65E0\u6CD5\u6307\u5B9A\u3002', `\u7528\u6CD5: ${usage}`]
|
|
6280
|
+
});
|
|
6281
|
+
}
|
|
6282
|
+
}
|
|
6283
|
+
function readUrlFromClipboard() {
|
|
6284
|
+
if (!process.stdin.isTTY) return null;
|
|
6285
|
+
try {
|
|
6286
|
+
const r = spawnSync5("pbpaste", [], { encoding: "utf8", timeout: 3e3 });
|
|
6287
|
+
if (r.status !== 0) return null;
|
|
6288
|
+
const text = (r.stdout || "").trim();
|
|
6289
|
+
return isValidHttpUrl(text) ? text : null;
|
|
6290
|
+
} catch {
|
|
6291
|
+
return null;
|
|
6292
|
+
}
|
|
6293
|
+
}
|
|
6294
|
+
async function subAdd(args) {
|
|
6295
|
+
let url = args[2]?.trim();
|
|
6296
|
+
const name = args[3] || "default";
|
|
6297
|
+
if (!url) {
|
|
6298
|
+
const clipped = readUrlFromClipboard();
|
|
6299
|
+
if (clipped) {
|
|
6300
|
+
console.log("\u672A\u63D0\u4F9B URL\uFF0C\u4ECE\u526A\u8D34\u677F\u8BFB\u53D6\u5230:");
|
|
6301
|
+
console.log(` ${maskUrl(clipped)}`);
|
|
6302
|
+
if (!await confirmPrompt(`\u6DFB\u52A0\u4E3A\u8BA2\u9605 "${name}"?`)) {
|
|
6303
|
+
console.log("\u5DF2\u53D6\u6D88");
|
|
6304
|
+
return;
|
|
6305
|
+
}
|
|
6306
|
+
url = clipped;
|
|
6307
|
+
} else if (process.stdin.isTTY) {
|
|
6308
|
+
throw new CliError("\u526A\u8D34\u677F\u4E2D\u6CA1\u6709\u6709\u6548\u7684\u8BA2\u9605 URL", {
|
|
6309
|
+
hint: ["\u5148\u590D\u5236\u8BA2\u9605\u94FE\u63A5\u540E\u91CD\u8BD5\uFF0C\u6216\u663E\u5F0F\u4F20\u5165:", " mihomo sub add <url> [name]"]
|
|
6310
|
+
});
|
|
6311
|
+
} else {
|
|
6312
|
+
throw new CliError("\u8BF7\u63D0\u4F9B\u6709\u6548\u7684\u8BA2\u9605 URL", { hint: ["\u7528\u6CD5: mihomo sub add <url> [name]"] });
|
|
6313
|
+
}
|
|
6314
|
+
}
|
|
6315
|
+
if (!isValidHttpUrl(url)) {
|
|
6316
|
+
throw new CliError("\u8BF7\u63D0\u4F9B\u6709\u6548\u7684\u8BA2\u9605 URL\uFF08\u9700\u4EE5 http:// \u6216 https:// \u5F00\u5934\uFF09");
|
|
6317
|
+
}
|
|
6318
|
+
assertNotFlagLike(name, "mihomo sub add <url> [name]");
|
|
6319
|
+
console.log(`\u6DFB\u52A0\u8BA2\u9605: ${name}`);
|
|
6320
|
+
addSubscription(url, name);
|
|
6321
|
+
try {
|
|
6322
|
+
const info = await withSpinner("\u4E0B\u8F7D\u8BA2\u9605", () => downloadSubscription(url, name));
|
|
6323
|
+
setDefaultSubscription(name);
|
|
6324
|
+
console.log(`\u5DF2\u6DFB\u52A0\u5E76\u5207\u6362\u5230 "${name}" (${formatProxySummary(info)})`);
|
|
6325
|
+
} catch (e) {
|
|
6326
|
+
removeSubscription(name);
|
|
6327
|
+
if (e instanceof CliError) throw new CliError(e.message, { label: "\u6DFB\u52A0\u5931\u8D25", hint: e.hint });
|
|
6328
|
+
throw new CliError(e.message, { label: "\u6DFB\u52A0\u5931\u8D25" });
|
|
6329
|
+
}
|
|
6330
|
+
console.log("");
|
|
6331
|
+
printSubscriptionList();
|
|
6332
|
+
}
|
|
6333
|
+
async function subUpdate(args) {
|
|
6334
|
+
const name = args[2];
|
|
6335
|
+
const subs = getSubscriptions();
|
|
6336
|
+
if (subs.length === 0) {
|
|
6337
|
+
throw new CliError("\u6CA1\u6709\u8BA2\u9605");
|
|
6338
|
+
}
|
|
6339
|
+
if (!name) {
|
|
6340
|
+
console.log(`\u66F4\u65B0\u6240\u6709 ${subs.length} \u4E2A\u8BA2\u9605...`);
|
|
6341
|
+
const results = await withSpinner("\u5E76\u884C\u66F4\u65B0\u4E2D", () => Promise.all(subs.map((sub) => tryUpdateOne(sub))));
|
|
6342
|
+
let ok = 0;
|
|
6343
|
+
for (const r of results) {
|
|
6344
|
+
if (r.success) ok++;
|
|
6345
|
+
printUpdateResult(r);
|
|
6346
|
+
}
|
|
6347
|
+
if (ok === 0) throw new CliError("\u5168\u90E8\u8BA2\u9605\u66F4\u65B0\u5931\u8D25");
|
|
6348
|
+
console.log("");
|
|
6349
|
+
printRestartHintIfRunning();
|
|
6350
|
+
printSubscriptionList();
|
|
6351
|
+
return;
|
|
6352
|
+
}
|
|
6353
|
+
const target = resolveSubscription(subs, name);
|
|
6354
|
+
console.log(`\u66F4\u65B0\u8BA2\u9605: ${target.name}`);
|
|
6355
|
+
const result = await withSpinner("\u4E0B\u8F7D\u8BA2\u9605", () => tryUpdateOne(target));
|
|
6356
|
+
if (!result.success) {
|
|
6357
|
+
throw new CliError((result.error || "").split("\n")[0], { label: "\u66F4\u65B0\u5931\u8D25" });
|
|
6358
|
+
}
|
|
6359
|
+
console.log(`\u5DF2\u66F4\u65B0 (${formatProxySummary(result)})`);
|
|
6360
|
+
console.log("");
|
|
6361
|
+
printRestartHintIfRunning();
|
|
6362
|
+
printSubscriptionList();
|
|
6363
|
+
}
|
|
6364
|
+
async function subUse(args) {
|
|
6365
|
+
const name = args[2];
|
|
6366
|
+
const subs = getSubscriptions();
|
|
6367
|
+
if (subs.length === 0) {
|
|
6368
|
+
throw new CliError("\u6CA1\u6709\u8BA2\u9605\uFF0C\u8BF7\u5148\u6DFB\u52A0\u8BA2\u9605", { hint: "mihomo sub add <url> [name]" });
|
|
6369
|
+
}
|
|
6370
|
+
if (!name) {
|
|
6371
|
+
throw new CliError("\u8BF7\u6307\u5B9A\u8BA2\u9605\u540D\u79F0", {
|
|
6372
|
+
hint: ["", "\u53EF\u7528\u8BA2\u9605:", ...subs.map((s) => ` ${s.name}`)]
|
|
6373
|
+
});
|
|
6374
|
+
}
|
|
6375
|
+
const target = resolveSubscription(subs, name);
|
|
6376
|
+
const currentDefault = getActiveSubscription();
|
|
6377
|
+
const isAlreadyDefault = currentDefault && currentDefault.name === target.name;
|
|
6378
|
+
if (isAlreadyDefault) {
|
|
6379
|
+
console.log(`"${target.name}" \u5DF2\u662F\u5F53\u524D\u4F7F\u7528\u7684\u8BA2\u9605`);
|
|
6380
|
+
console.log("");
|
|
6381
|
+
printSubscriptionList();
|
|
6382
|
+
return;
|
|
6383
|
+
}
|
|
6384
|
+
const success = setDefaultSubscription(target.name);
|
|
6385
|
+
if (!success) {
|
|
6386
|
+
throw new CliError(`\u672A\u627E\u5230\u8BA2\u9605 "${name}"`);
|
|
6387
|
+
}
|
|
6388
|
+
console.log(`\u5DF2\u5207\u6362\u5230 "${target.name}"`);
|
|
6389
|
+
if (await restartToApply(args)) return;
|
|
6390
|
+
console.log("");
|
|
6391
|
+
printSubscriptionList();
|
|
6392
|
+
}
|
|
6393
|
+
async function subRemove(args) {
|
|
6394
|
+
const name = getNonFlagArg(args, 2);
|
|
6395
|
+
const subs = getSubscriptions();
|
|
6396
|
+
if (!name) {
|
|
6397
|
+
throw new CliError("\u8BF7\u6307\u5B9A\u8981\u5220\u9664\u7684\u8BA2\u9605\u540D\u79F0", {
|
|
6398
|
+
hint: subs.length > 0 ? ["", "\u53EF\u7528\u8BA2\u9605:", ...subs.map((s) => ` ${s.name}`)] : void 0
|
|
6399
|
+
});
|
|
6400
|
+
}
|
|
6401
|
+
const target = resolveSubscription(subs, name);
|
|
6402
|
+
const isExact = target.name === name;
|
|
6403
|
+
const skipConfirm = hasFlag(args, "-y", "--yes");
|
|
6404
|
+
if (!isExact && !skipConfirm) {
|
|
6405
|
+
console.log(`\u5C06\u5220\u9664\u8BA2\u9605 "${target.name}" (\u6A21\u7CCA\u5339\u914D "${name}")`);
|
|
6406
|
+
if (!await confirmOrThrow("\u6B64\u64CD\u4F5C\u4E0D\u53EF\u6062\u590D\uFF0C\u786E\u8BA4?", {
|
|
6407
|
+
nonTtyMessage: `\u6A21\u7CCA\u5339\u914D\u5230 "${target.name}"\uFF0C\u975E\u4EA4\u4E92\u73AF\u5883\u9700\u786E\u8BA4`,
|
|
6408
|
+
hint: [`\u8BF7\u7528\u5B8C\u6574\u540D\u79F0: mihomo sub remove ${target.name}`, `\u6216\u8DF3\u8FC7\u786E\u8BA4: mihomo sub remove ${name} -y`]
|
|
6409
|
+
})) {
|
|
6410
|
+
console.log("\u5DF2\u53D6\u6D88");
|
|
6411
|
+
return;
|
|
6412
|
+
}
|
|
6413
|
+
}
|
|
6414
|
+
const switchedTo = removeSubscription(target.name);
|
|
6415
|
+
console.log(`\u5DF2\u5220\u9664\u8BA2\u9605 "${target.name}"`);
|
|
6416
|
+
if (switchedTo) {
|
|
6417
|
+
console.log(`\u5DF2\u81EA\u52A8\u5207\u6362\u5230 "${switchedTo}"`);
|
|
6418
|
+
}
|
|
6419
|
+
console.log("");
|
|
6420
|
+
printSubscriptionList();
|
|
6421
|
+
}
|
|
6422
|
+
var SUBCOMMANDS3 = [
|
|
6423
|
+
{ name: "add", description: "\u6DFB\u52A0\u8BA2\u9605", handler: subAdd },
|
|
6424
|
+
{ name: "update", description: "\u66F4\u65B0\u8BA2\u9605", handler: subUpdate },
|
|
6425
|
+
{ name: "use", description: "\u5207\u6362\u8BA2\u9605", handler: subUse },
|
|
6426
|
+
{ name: "remove", aliases: ["rm", "delete"], description: "\u5220\u9664\u8BA2\u9605", handler: subRemove }
|
|
6427
|
+
];
|
|
6428
|
+
async function cmdSubscription(args) {
|
|
6429
|
+
await dispatchSubcommand(args, SUBCOMMANDS3, {
|
|
6430
|
+
// 无子命令 → 列表;未知子命令 → 报错
|
|
6431
|
+
fallback: printSubscriptionList,
|
|
6432
|
+
onUnknown: (action) => {
|
|
6433
|
+
const names = SUBCOMMANDS3.flatMap((c) => [c.name, ...c.aliases ?? []]);
|
|
6434
|
+
const suggestion = suggestSimilar(action, names);
|
|
6435
|
+
throw new CliError(`\u672A\u77E5\u7684\u8BA2\u9605\u547D\u4EE4: ${action}`, {
|
|
6436
|
+
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"]
|
|
6437
|
+
});
|
|
6438
|
+
}
|
|
6439
|
+
});
|
|
6440
|
+
}
|
|
6441
|
+
|
|
6442
|
+
// src/commands/completion.ts
|
|
6443
|
+
function commandWords(commands) {
|
|
6444
|
+
return commands.filter((c) => !c.hidden).map((c) => ({ word: c.name, desc: c.usage[0]?.description ?? "" }));
|
|
6445
|
+
}
|
|
6446
|
+
function subWords(subs) {
|
|
6447
|
+
return subs.flatMap((s) => [{ word: s.name, desc: s.description ?? "" }, ...(s.aliases ?? []).map((a) => ({ word: a, desc: s.description ?? "" }))]);
|
|
6448
|
+
}
|
|
6449
|
+
function subGroups(commands) {
|
|
6450
|
+
const groupOf = (name, subs) => {
|
|
6451
|
+
const cmd = commands.find((c) => c.name === name);
|
|
6452
|
+
return { tokens: cmd ? [cmd.name, ...cmd.aliases] : [name], words: subWords(subs) };
|
|
6453
|
+
};
|
|
6454
|
+
return [groupOf("subscription", SUBCOMMANDS3), groupOf("overwrite", SUBCOMMANDS2), groupOf("directory", SUBCOMMANDS)];
|
|
6455
|
+
}
|
|
6456
|
+
var DIR_TARGETS = ["root", "subs", "logs", "data", "runtime", "kernel"];
|
|
6457
|
+
var UI_NAMES = ["zash", "dash", "yacd"];
|
|
6458
|
+
var SHELLS = ["zsh", "bash", "fish"];
|
|
6459
|
+
function buildZsh(commands, groups) {
|
|
6460
|
+
const lines = [
|
|
6461
|
+
"#compdef mihomo mhm mh mihomo-cli",
|
|
6462
|
+
"",
|
|
6463
|
+
'# mihomo-cli zsh \u8865\u5168\uFF08mihomo completion zsh \u751F\u6210\uFF1B\u5B89\u88C5: mihomo completion install zsh\uFF0C\u6216 eval "$(mihomo completion zsh)"\uFF09',
|
|
6464
|
+
"",
|
|
6465
|
+
"_mihomo() {",
|
|
6466
|
+
" local -a commands subcmds",
|
|
6467
|
+
" commands=(",
|
|
6468
|
+
...commandWords(commands).map((c) => ` '${c.word}:${c.desc.replace(/'/g, "''")}'`),
|
|
6469
|
+
" )",
|
|
6470
|
+
"",
|
|
6471
|
+
" _arguments -C \\",
|
|
6472
|
+
" '1: :->cmd' \\",
|
|
6473
|
+
" '*::arg:->args'",
|
|
6474
|
+
"",
|
|
6475
|
+
" case $state in",
|
|
6476
|
+
" cmd)",
|
|
6477
|
+
" _describe 'command' commands",
|
|
6478
|
+
" ;;",
|
|
6479
|
+
" args)",
|
|
6480
|
+
" case ${words[1]} in"
|
|
6481
|
+
];
|
|
6482
|
+
for (const group of groups) {
|
|
6483
|
+
const aliases = group.tokens.join("|");
|
|
6484
|
+
lines.push(` ${aliases})`);
|
|
6485
|
+
lines.push(" subcmds=(");
|
|
6486
|
+
for (const s of group.words) lines.push(` '${s.word}:${s.desc.replace(/'/g, "''")}'`);
|
|
6487
|
+
lines.push(" )");
|
|
6488
|
+
lines.push(" if (( CURRENT == 2 )); then");
|
|
6489
|
+
lines.push(" _describe 'subcommand' subcmds");
|
|
6490
|
+
lines.push(" else");
|
|
6491
|
+
lines.push(" _files");
|
|
6492
|
+
lines.push(" fi");
|
|
6493
|
+
lines.push(" ;;");
|
|
6494
|
+
}
|
|
6495
|
+
lines.push(
|
|
6496
|
+
" directory|dir|dirs|directories)",
|
|
6497
|
+
" if (( CURRENT == 3 )); then",
|
|
6498
|
+
` _values 'target' ${DIR_TARGETS.join(" ")}`,
|
|
6499
|
+
" fi",
|
|
6500
|
+
" ;;",
|
|
6501
|
+
" logs)",
|
|
6502
|
+
" _arguments '-f[\u5B9E\u65F6\u8DDF\u968F]' '-n[\u663E\u793A\u884C\u6570]:\u884C\u6570:' '-o[\u7CFB\u7EDF\u9ED8\u8BA4\u7A0B\u5E8F\u6253\u5F00]' '1::\u7F16\u53F7:'",
|
|
6503
|
+
" ;;",
|
|
6504
|
+
" ui)",
|
|
6505
|
+
` _values 'ui' ${UI_NAMES.join(" ")}`,
|
|
6506
|
+
" ;;",
|
|
6507
|
+
" kernel)",
|
|
6508
|
+
" _arguments '--mirror[\u8D70\u955C\u50CF\u4E0B\u8F7D]:\u955C\u50CF:' '--no-mirror[\u76F4\u8FDE\u4E0B\u8F7D]'",
|
|
6509
|
+
" ;;",
|
|
6510
|
+
" reset)",
|
|
6511
|
+
` _values 'target' subs logs data runtime settings kernel overwrites service`,
|
|
6512
|
+
" _arguments '-y[\u8DF3\u8FC7\u786E\u8BA4]' '--full[\u5220\u5168\u90E8]'",
|
|
6513
|
+
" ;;",
|
|
6514
|
+
" completion)",
|
|
6515
|
+
" if (( CURRENT == 2 )); then",
|
|
6516
|
+
" _values 'action' install",
|
|
6517
|
+
" elif (( CURRENT == 3 )) && [[ ${words[2]} == install ]]; then",
|
|
6518
|
+
` _values 'shell' ${SHELLS.join(" ")}`,
|
|
6519
|
+
" fi",
|
|
6520
|
+
" ;;",
|
|
6521
|
+
" *)",
|
|
6522
|
+
" _files",
|
|
6523
|
+
" ;;",
|
|
6524
|
+
" esac",
|
|
6525
|
+
" ;;",
|
|
6526
|
+
" esac",
|
|
6527
|
+
"}",
|
|
6528
|
+
"",
|
|
6529
|
+
'_mihomo "$@"'
|
|
6530
|
+
);
|
|
6531
|
+
return lines.join("\n");
|
|
6532
|
+
}
|
|
6533
|
+
function buildBash(commands, groups) {
|
|
6534
|
+
const top = commandWords(commands).map((c) => c.word).join(" ");
|
|
6535
|
+
const subCase = groups.map((group) => {
|
|
6536
|
+
const aliases = group.tokens.join("|");
|
|
6537
|
+
const words = group.words.map((w) => w.word).join(" ");
|
|
6538
|
+
if (group.tokens[0] === "directory") {
|
|
6539
|
+
return ` ${aliases})
|
|
6540
|
+
if [[ \${COMP_CWORD} -eq 2 ]]; then
|
|
6541
|
+
COMPREPLY=( $(compgen -W "open" -- "\${cur}") )
|
|
6542
|
+
elif [[ "\${COMP_WORDS[2]}" == "open" ]]; then
|
|
6543
|
+
COMPREPLY=( $(compgen -W "${DIR_TARGETS.join(" ")}" -- "\${cur}") )
|
|
6544
|
+
fi
|
|
6545
|
+
;;`;
|
|
6546
|
+
}
|
|
6547
|
+
return ` ${aliases})
|
|
6548
|
+
if [[ \${COMP_CWORD} -eq 2 ]]; then
|
|
6549
|
+
COMPREPLY=( $(compgen -W "${words}" -- "\${cur}") )
|
|
6550
|
+
fi
|
|
6551
|
+
;;`;
|
|
6552
|
+
}).join("\n");
|
|
6553
|
+
return `# mihomo-cli bash \u8865\u5168\uFF08mihomo completion bash \u751F\u6210\uFF1B\u5B89\u88C5: mihomo completion install bash\uFF0C\u6216 eval "$(mihomo completion bash)"\uFF09
|
|
6554
|
+
|
|
6555
|
+
_mihomo_completions() {
|
|
6556
|
+
local cur prev
|
|
6557
|
+
COMPREPLY=()
|
|
6558
|
+
cur="\${COMP_WORDS[COMP_CWORD]}"
|
|
6559
|
+
prev="\${COMP_WORDS[COMP_CWORD-1]}"
|
|
6560
|
+
|
|
6561
|
+
if [[ \${COMP_CWORD} -eq 1 ]]; then
|
|
6562
|
+
COMPREPLY=( $(compgen -W "${top}" -- "\${cur}") )
|
|
6563
|
+
return 0
|
|
6564
|
+
fi
|
|
6565
|
+
|
|
6566
|
+
case "\${COMP_WORDS[1]}" in
|
|
6567
|
+
${subCase}
|
|
6568
|
+
logs)
|
|
6569
|
+
COMPREPLY=( $(compgen -W "-f -n -o --help" -- "\${cur}") )
|
|
6570
|
+
;;
|
|
6571
|
+
ui)
|
|
6572
|
+
COMPREPLY=( $(compgen -W "${UI_NAMES.join(" ")}" -- "\${cur}") )
|
|
6573
|
+
;;
|
|
6574
|
+
kernel)
|
|
6575
|
+
COMPREPLY=( $(compgen -W "--mirror --no-mirror" -- "\${cur}") )
|
|
6576
|
+
;;
|
|
6577
|
+
reset)
|
|
6578
|
+
COMPREPLY=( $(compgen -W "subs logs data runtime settings kernel overwrites service --full -y" -- "\${cur}") )
|
|
6579
|
+
;;
|
|
6580
|
+
completion)
|
|
6581
|
+
if [[ \${COMP_CWORD} -eq 2 ]]; then
|
|
6582
|
+
COMPREPLY=( $(compgen -W "install" -- "\${cur}") )
|
|
6583
|
+
elif [[ \${COMP_CWORD} -eq 3 ]] && [[ "\${COMP_WORDS[2]}" == install ]]; then
|
|
6584
|
+
COMPREPLY=( $(compgen -W "${SHELLS.join(" ")}" -- "\${cur}") )
|
|
6585
|
+
fi
|
|
6586
|
+
;;
|
|
6587
|
+
esac
|
|
6588
|
+
}
|
|
6589
|
+
complete -F _mihomo_completions mihomo mhm mh mihomo-cli
|
|
6590
|
+
`;
|
|
6591
|
+
}
|
|
6592
|
+
function buildFish(commands, groups) {
|
|
6593
|
+
const lines = [
|
|
6594
|
+
"# mihomo-cli fish \u8865\u5168\uFF08mihomo completion fish \u751F\u6210\uFF1B\u5B89\u88C5: mihomo completion install fish\uFF0C\u6216 mihomo completion fish | source\uFF09",
|
|
6595
|
+
"",
|
|
6596
|
+
"for cmd in mihomo mhm mh mihomo-cli"
|
|
6597
|
+
];
|
|
6598
|
+
for (const c of commandWords(commands)) {
|
|
6599
|
+
lines.push(` complete -c $cmd -f -a '${c.word}' -d '${c.desc.replace(/'/g, "\\'")}'`);
|
|
6600
|
+
}
|
|
6601
|
+
for (const group of groups) {
|
|
6602
|
+
const seen = group.tokens.join(" ");
|
|
6603
|
+
for (const s of group.words) {
|
|
6604
|
+
lines.push(` complete -c $cmd -n "__fish_seen_subcommand_from ${seen}" -a '${s.word}' -d '${s.desc.replace(/'/g, "\\'")}'`);
|
|
6605
|
+
}
|
|
6606
|
+
}
|
|
6607
|
+
lines.push(` complete -c $cmd -n "__fish_seen_subcommand_from directory dir dirs directories open" -a '${DIR_TARGETS.join(" ")}'`);
|
|
6608
|
+
lines.push(` complete -c $cmd -n "__fish_seen_subcommand_from ui" -a '${UI_NAMES.join(" ")}'`);
|
|
6609
|
+
lines.push(` complete -c $cmd -n "__fish_seen_subcommand_from completion" -a 'install' -d '\u5B89\u88C5\u8865\u5168\u5230\u9ED8\u8BA4\u4F4D\u7F6E'`);
|
|
6610
|
+
lines.push(` complete -c $cmd -n "__fish_seen_subcommand_from completion; and __fish_seen_subcommand_from install" -a '${SHELLS.join(" ")}'`);
|
|
6611
|
+
lines.push(` complete -c $cmd -n "__fish_seen_subcommand_from kernel" -a '\\--mirror' -d '\u8D70\u955C\u50CF\u4E0B\u8F7D'`);
|
|
6612
|
+
lines.push(` complete -c $cmd -n "__fish_seen_subcommand_from kernel" -a '\\--no-mirror' -d '\u76F4\u8FDE\u4E0B\u8F7D'`);
|
|
6613
|
+
lines.push("end");
|
|
6614
|
+
return lines.join("\n");
|
|
6615
|
+
}
|
|
6616
|
+
function buildCompletionScript(shell, commands) {
|
|
6617
|
+
const groups = subGroups(commands);
|
|
6618
|
+
switch (shell) {
|
|
6619
|
+
case "zsh":
|
|
6620
|
+
return buildZsh(commands, groups);
|
|
6621
|
+
case "bash":
|
|
6622
|
+
return buildBash(commands, groups);
|
|
6623
|
+
case "fish":
|
|
6624
|
+
return buildFish(commands, groups);
|
|
6625
|
+
default: {
|
|
6626
|
+
const suggestion = suggestSimilar(shell, [...SHELLS]);
|
|
6627
|
+
throw new CliError(`\u672A\u77E5\u7684 shell: ${shell}`, {
|
|
6628
|
+
label: "\u53C2\u6570\u9519\u8BEF",
|
|
6629
|
+
hint: [...suggestion.length > 0 ? [`\u662F\u5426\u60F3\u8F93\u5165: ${suggestion.join(" / ")}?`] : [], `\u7528\u6CD5: mihomo completion <${SHELLS.join("|")}>`]
|
|
6630
|
+
});
|
|
6631
|
+
}
|
|
6632
|
+
}
|
|
6633
|
+
}
|
|
6634
|
+
var BASH_MARKER = "# >>> mihomo-cli completion (append)";
|
|
6635
|
+
function completionInstallPath(shell) {
|
|
6636
|
+
const home = os2.homedir();
|
|
6637
|
+
switch (shell) {
|
|
6638
|
+
case "zsh":
|
|
6639
|
+
return path8.join(home, ".zsh", "completions", "_mihomo");
|
|
6640
|
+
case "bash":
|
|
6641
|
+
return path8.join(home, ".bash_completion");
|
|
6642
|
+
case "fish":
|
|
6643
|
+
return path8.join(home, ".config", "fish", "completions", "mihomo.fish");
|
|
6644
|
+
default:
|
|
6645
|
+
return null;
|
|
6646
|
+
}
|
|
6647
|
+
}
|
|
6648
|
+
function installCompletion(shell, commands) {
|
|
6649
|
+
if (!shell) {
|
|
6650
|
+
throw new CliError("\u8BF7\u6307\u5B9A shell", { hint: [`\u7528\u6CD5: mihomo completion install <${SHELLS.join("|")}>`] });
|
|
6651
|
+
}
|
|
6652
|
+
const target = completionInstallPath(shell);
|
|
6653
|
+
if (!target) {
|
|
6654
|
+
buildCompletionScript(shell, commands);
|
|
6655
|
+
return;
|
|
6656
|
+
}
|
|
6657
|
+
const script = buildCompletionScript(shell, commands);
|
|
6658
|
+
console.log(`\u5B89\u88C5 ${shell} \u8865\u5168: ${target}`);
|
|
6659
|
+
if (shell === "bash") {
|
|
6660
|
+
const existing = fs10.existsSync(target) ? fs10.readFileSync(target, "utf8") : "";
|
|
6661
|
+
if (existing.includes(BASH_MARKER)) {
|
|
6662
|
+
console.log("\u5DF2\u5B89\u88C5\u8FC7\uFF08~/.bash_completion \u5DF2\u5305\u542B mihomo \u8865\u5168\uFF09\uFF0C\u8DF3\u8FC7");
|
|
6663
|
+
return;
|
|
6664
|
+
}
|
|
6665
|
+
fs10.mkdirSync(path8.dirname(target), { recursive: true });
|
|
6666
|
+
fs10.appendFileSync(target, `
|
|
6667
|
+
${BASH_MARKER}
|
|
6668
|
+
${script}${BASH_MARKER.replace(">>>", "<<<")}
|
|
6669
|
+
`);
|
|
6670
|
+
console.log(colors.green("\u5DF2\u8FFD\u52A0\u5230 ~/.bash_completion\uFF08\u91CD\u65B0\u6253\u5F00\u7EC8\u7AEF\u751F\u6548\uFF09"));
|
|
6671
|
+
return;
|
|
6672
|
+
}
|
|
6673
|
+
fs10.mkdirSync(path8.dirname(target), { recursive: true });
|
|
6674
|
+
fs10.writeFileSync(target, script, { mode: 420 });
|
|
6675
|
+
console.log(colors.green("\u5DF2\u5199\u5165\uFF08\u91CD\u65B0\u6253\u5F00\u7EC8\u7AEF\u751F\u6548\uFF09"));
|
|
6676
|
+
if (shell === "zsh") {
|
|
6677
|
+
console.log(colors.gray("\u82E5\u8865\u5168\u4E0D\u751F\u6548\uFF0C\u5728 ~/.zshrc \u4E2D\u52A0\u4E00\u884C: fpath=(~/.zsh/completions $fpath)"));
|
|
6678
|
+
}
|
|
6679
|
+
}
|
|
6680
|
+
function cmdCompletion(args, commands) {
|
|
6681
|
+
if (args[1] === "install") {
|
|
6682
|
+
installCompletion(args[2], commands);
|
|
6683
|
+
return;
|
|
6684
|
+
}
|
|
6685
|
+
const shell = args[1];
|
|
6686
|
+
if (!shell) {
|
|
6687
|
+
throw new CliError("\u8BF7\u6307\u5B9A shell", {
|
|
6688
|
+
hint: [
|
|
6689
|
+
`\u7528\u6CD5: mihomo completion <${SHELLS.join("|")}>`,
|
|
6690
|
+
`\u5B89\u88C5\u5230\u9ED8\u8BA4\u4F4D\u7F6E: mihomo completion install <${SHELLS.join("|")}>`,
|
|
6691
|
+
'\u4E34\u65F6\u542F\u7528: eval "$(mihomo completion zsh)"'
|
|
6692
|
+
]
|
|
6693
|
+
});
|
|
6694
|
+
}
|
|
6695
|
+
console.log(buildCompletionScript(shell, commands));
|
|
6696
|
+
}
|
|
6697
|
+
|
|
6698
|
+
// src/commands/doctor.ts
|
|
6699
|
+
import { spawnSync as spawnSync6 } from "child_process";
|
|
6700
|
+
import fs11 from "fs";
|
|
6701
|
+
|
|
6702
|
+
// node_modules/compare-versions/lib/esm/utils.js
|
|
6703
|
+
var semver = /^[v^~<>=]*?(\d+)(?:\.([x*]|\d+)(?:\.([x*]|\d+)(?:\.([x*]|\d+))?(?:-([\da-z\-]+(?:\.[\da-z\-]+)*))?(?:\+[\da-z\-]+(?:\.[\da-z\-]+)*)?)?)?$/i;
|
|
6704
|
+
var validateAndParse = (version) => {
|
|
6705
|
+
if (typeof version !== "string") {
|
|
6706
|
+
throw new TypeError("Invalid argument expected string");
|
|
6707
|
+
}
|
|
6708
|
+
const match = version.match(semver);
|
|
6709
|
+
if (!match) {
|
|
6710
|
+
throw new Error(`Invalid argument not valid semver ('${version}' received)`);
|
|
6711
|
+
}
|
|
6712
|
+
match.shift();
|
|
6713
|
+
return match;
|
|
6714
|
+
};
|
|
6715
|
+
var isWildcard = (s) => s === "*" || s === "x" || s === "X";
|
|
6716
|
+
var tryParse = (v) => {
|
|
6717
|
+
const n = parseInt(v, 10);
|
|
6718
|
+
return isNaN(n) ? v : n;
|
|
6719
|
+
};
|
|
6720
|
+
var forceType = (a, b) => typeof a !== typeof b ? [String(a), String(b)] : [a, b];
|
|
6721
|
+
var compareStrings = (a, b) => {
|
|
6722
|
+
if (isWildcard(a) || isWildcard(b))
|
|
6723
|
+
return 0;
|
|
6724
|
+
const [ap, bp] = forceType(tryParse(a), tryParse(b));
|
|
6725
|
+
if (ap > bp)
|
|
6726
|
+
return 1;
|
|
6727
|
+
if (ap < bp)
|
|
6728
|
+
return -1;
|
|
6729
|
+
return 0;
|
|
6730
|
+
};
|
|
6731
|
+
var compareSegments = (a, b) => {
|
|
6732
|
+
for (let i = 0; i < Math.max(a.length, b.length); i++) {
|
|
6733
|
+
const r = compareStrings(a[i] || "0", b[i] || "0");
|
|
6734
|
+
if (r !== 0)
|
|
6735
|
+
return r;
|
|
6736
|
+
}
|
|
6737
|
+
return 0;
|
|
6738
|
+
};
|
|
6739
|
+
|
|
6740
|
+
// node_modules/compare-versions/lib/esm/compareVersions.js
|
|
6741
|
+
var compareVersions = (v1, v2) => {
|
|
6742
|
+
const n1 = validateAndParse(v1);
|
|
6743
|
+
const n2 = validateAndParse(v2);
|
|
6744
|
+
const p1 = n1.pop();
|
|
6745
|
+
const p2 = n2.pop();
|
|
6746
|
+
const r = compareSegments(n1, n2);
|
|
6747
|
+
if (r !== 0)
|
|
6748
|
+
return r;
|
|
6749
|
+
if (p1 && p2) {
|
|
6750
|
+
return compareSegments(p1.split("."), p2.split("."));
|
|
6751
|
+
} else if (p1 || p2) {
|
|
6752
|
+
return p1 ? -1 : 1;
|
|
6753
|
+
}
|
|
6754
|
+
return 0;
|
|
6755
|
+
};
|
|
6756
|
+
|
|
6757
|
+
// src/commands/update.ts
|
|
6758
|
+
import { execFile as execFile2, spawn as spawn2 } from "child_process";
|
|
6759
|
+
import { promisify as promisify2 } from "util";
|
|
6760
|
+
var execFileAsync2 = promisify2(execFile2);
|
|
6761
|
+
var NPM_VIEW_TIMEOUT_MS = 15e3;
|
|
6762
|
+
async function getLatestNpmVersion(timeoutMs = NPM_VIEW_TIMEOUT_MS) {
|
|
6763
|
+
try {
|
|
6764
|
+
const { stdout } = await execFileAsync2("npm", ["view", PKG_NAME, "version"], { timeout: timeoutMs });
|
|
6765
|
+
const version = stdout.trim().split("\n").pop()?.trim();
|
|
6766
|
+
return version || null;
|
|
6767
|
+
} catch {
|
|
6768
|
+
return null;
|
|
6769
|
+
}
|
|
6770
|
+
}
|
|
6771
|
+
async function cmdUpdate() {
|
|
6772
|
+
console.log(`\u5F53\u524D\u7248\u672C: ${colors.cyan(VERSION)}`);
|
|
6773
|
+
console.log("");
|
|
6774
|
+
const latest = await withSpinner("\u67E5\u8BE2 npm \u6700\u65B0\u7248\u672C", getLatestNpmVersion);
|
|
6775
|
+
if (latest && latest === VERSION) {
|
|
6776
|
+
console.log(`\u5DF2\u662F\u6700\u65B0\u7248\u672C (${colors.green(VERSION)})\uFF0C\u65E0\u9700\u66F4\u65B0`);
|
|
6777
|
+
return;
|
|
6778
|
+
}
|
|
6779
|
+
if (latest) {
|
|
6780
|
+
console.log(`\u6700\u65B0\u7248\u672C: ${colors.cyan(latest)}`);
|
|
6781
|
+
} else {
|
|
6782
|
+
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"));
|
|
6783
|
+
}
|
|
6784
|
+
console.log("");
|
|
6785
|
+
console.log("\u6B63\u5728\u66F4\u65B0 mihomo-cli...");
|
|
6786
|
+
console.log("");
|
|
6787
|
+
await new Promise((resolve, reject) => {
|
|
6788
|
+
const npm = spawn2("npm", ["install", "-g", PKG_NAME], { stdio: "inherit" });
|
|
6789
|
+
npm.on("close", (code) => {
|
|
6790
|
+
if (code === 0) {
|
|
6791
|
+
resolve();
|
|
6792
|
+
} else {
|
|
6793
|
+
reject(new CliError("\u66F4\u65B0\u5931\u8D25\u3002\u82E5\u4E3A\u6743\u9650\u95EE\u9898\uFF08EACCES\uFF09\uFF0C\u8BF7\u68C0\u67E5 npm \u5168\u5C40\u76EE\u5F55\u6743\u9650\u6216\u4F7F\u7528 nvm \u7BA1\u7406 Node", { exitCode: code || 1 }));
|
|
6794
|
+
}
|
|
6795
|
+
});
|
|
6796
|
+
npm.on("error", (e) => {
|
|
6797
|
+
const perm = e.message.includes("EACCES") || e.message.includes("permission");
|
|
6798
|
+
reject(perm ? new CliError("\u6743\u9650\u4E0D\u8DB3\uFF08EACCES\uFF09\uFF0C\u8BF7\u68C0\u67E5 npm \u5168\u5C40\u76EE\u5F55\u6743\u9650\u6216\u4F7F\u7528 nvm \u7BA1\u7406 Node") : new CliError(`\u6267\u884C\u5931\u8D25: ${e.message}`));
|
|
6799
|
+
});
|
|
6800
|
+
});
|
|
6801
|
+
try {
|
|
6802
|
+
const { stdout } = await execFileAsync2("npm", ["list", "-g", PKG_NAME, "--json", "--depth=0"]);
|
|
6803
|
+
const result = JSON.parse(stdout);
|
|
6804
|
+
const newVersion = result.dependencies?.[PKG_NAME]?.version;
|
|
6805
|
+
console.log("");
|
|
6806
|
+
if (newVersion) {
|
|
6807
|
+
console.log(`\u66F4\u65B0\u5B8C\u6210\uFF0C\u6700\u65B0\u7248\u672C: ${colors.green(newVersion)}`);
|
|
6808
|
+
} else {
|
|
6809
|
+
console.log("\u66F4\u65B0\u5B8C\u6210");
|
|
6810
|
+
}
|
|
6811
|
+
} catch {
|
|
6812
|
+
console.log("");
|
|
6813
|
+
console.log("\u66F4\u65B0\u5B8C\u6210");
|
|
6814
|
+
}
|
|
6815
|
+
}
|
|
6816
|
+
|
|
6817
|
+
// src/commands/doctor.ts
|
|
6818
|
+
function isPortListening(port) {
|
|
6819
|
+
try {
|
|
6820
|
+
const r = spawnSync6("lsof", ["-nP", `-iTCP:${port}`, "-sTCP:LISTEN"], { encoding: "utf8", timeout: 5e3 });
|
|
6821
|
+
return r.status === 0 && (r.stdout || "").trim().length > 0;
|
|
6822
|
+
} catch {
|
|
6823
|
+
return false;
|
|
6824
|
+
}
|
|
6825
|
+
}
|
|
6826
|
+
async function collectChecks() {
|
|
6827
|
+
const checks = [];
|
|
6828
|
+
const push = (name, status, detail, fix) => {
|
|
6829
|
+
checks.push({ name, status, detail, fix });
|
|
6830
|
+
};
|
|
6831
|
+
if (!hasKernel()) {
|
|
6832
|
+
push("\u5185\u6838", "fail", "\u672A\u5B89\u88C5", "mihomo kernel");
|
|
6833
|
+
} else {
|
|
6834
|
+
const v = getKernelVersion();
|
|
6835
|
+
const r = spawnSync6(PATHS.mihomoBinary, ["-v"], { encoding: "utf8", timeout: 5e3 });
|
|
6836
|
+
if (r.status === 0 && /v?\d+\.\d+\.\d+/.test(`${r.stdout}${r.stderr}`)) {
|
|
6837
|
+
push("\u5185\u6838", "ok", v || "\u53EF\u6267\u884C");
|
|
6838
|
+
} else {
|
|
6839
|
+
push("\u5185\u6838", "fail", `\u4E8C\u8FDB\u5236\u65E0\u6CD5\u6267\u884C\uFF08\u9000\u51FA\u7801 ${r.status}\uFF09`, "\u91CD\u65B0\u4E0B\u8F7D: mihomo kernel");
|
|
6840
|
+
}
|
|
6841
|
+
}
|
|
6842
|
+
try {
|
|
6843
|
+
fs11.accessSync(USER_DATA_DIR, fs11.constants.W_OK);
|
|
6844
|
+
push("\u6570\u636E\u76EE\u5F55", "ok", USER_DATA_DIR);
|
|
6845
|
+
} catch {
|
|
6846
|
+
push("\u6570\u636E\u76EE\u5F55", "fail", `\u4E0D\u53EF\u5199: ${USER_DATA_DIR}`, "\u68C0\u67E5\u76EE\u5F55\u6743\u9650");
|
|
6847
|
+
}
|
|
6848
|
+
if (fs11.existsSync(PATHS.settingsFile)) {
|
|
6849
|
+
if (isValidSettingsContent(fs11.readFileSync(PATHS.settingsFile, "utf8"))) {
|
|
6850
|
+
push("\u8BBE\u7F6E\u6587\u4EF6", "ok", "\u683C\u5F0F\u6709\u6548");
|
|
6851
|
+
} else {
|
|
6852
|
+
push("\u8BBE\u7F6E\u6587\u4EF6", "warn", "\u683C\u5F0F\u635F\u574F\u6216\u975E\u5BF9\u8C61\uFF0C\u8BFB\u53D6\u65F6\u4F1A\u56DE\u9000\u9ED8\u8BA4\u5E76\u5907\u4EFD\u4E3A .bak", "\u5220\u9664\u6216\u4FEE\u590D settings.json");
|
|
6853
|
+
}
|
|
6854
|
+
} else {
|
|
6855
|
+
push("\u8BBE\u7F6E\u6587\u4EF6", "ok", "\u672A\u521B\u5EFA\uFF08\u4F7F\u7528\u9ED8\u8BA4\u8BBE\u7F6E\uFF09");
|
|
6856
|
+
}
|
|
6857
|
+
const subs = getSubscriptionsWithCache();
|
|
6858
|
+
const active = getActiveSubscription();
|
|
6859
|
+
if (subs.length === 0) {
|
|
6860
|
+
push("\u8BA2\u9605", "warn", "\u672A\u914D\u7F6E", "mihomo sub add <url>");
|
|
6861
|
+
} else {
|
|
6862
|
+
push("\u8BA2\u9605", "ok", `${subs.length} \u4E2A${active ? `\uFF0C\u5F53\u524D: ${active.name}` : ""}`);
|
|
6863
|
+
if (active) {
|
|
6864
|
+
if (!readSubscriptionRawConfig(active.name)) {
|
|
6865
|
+
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}`);
|
|
6866
|
+
} else {
|
|
6867
|
+
push("\u8BA2\u9605\u914D\u7F6E", "ok", `"${active.name}" \u914D\u7F6E\u6587\u4EF6\u5B58\u5728`);
|
|
6868
|
+
}
|
|
6869
|
+
const cached = subs.find((s) => s.name === active.name);
|
|
6870
|
+
if (cached?.updated_at) {
|
|
6871
|
+
const rel = formatRelativeTime(cached.updated_at);
|
|
6872
|
+
if (isSubscriptionStale(cached)) {
|
|
6873
|
+
push("\u8BA2\u9605\u65B0\u9C9C\u5EA6", "warn", `${rel ?? "\u672A\u77E5"}\u524D\u66F4\u65B0\uFF0C\u5DF2\u8D85\u8FC7 ${resolveUpdateInterval(cached.update_interval)} \u5C0F\u65F6\u95F4\u9694`, "mihomo sub update");
|
|
6874
|
+
} else {
|
|
6875
|
+
push("\u8BA2\u9605\u65B0\u9C9C\u5EA6", "ok", `${rel ?? "\u672A\u77E5"}\u524D\u66F4\u65B0`);
|
|
6876
|
+
}
|
|
6877
|
+
}
|
|
6878
|
+
}
|
|
6879
|
+
}
|
|
6880
|
+
const service = getServiceStatus();
|
|
6881
|
+
const legacy = detectLegacySystemInstall();
|
|
6882
|
+
if (legacy) {
|
|
6883
|
+
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");
|
|
6401
6884
|
} else if (!service.installed && !service.loaded) {
|
|
6402
6885
|
push("\u670D\u52A1", "warn", "\u672A\u5B89\u88C5\uFF08Mixed \u6A21\u5F0F\u9700\u8981\uFF09", "mihomo install");
|
|
6403
6886
|
} else if (!service.installed) {
|
|
@@ -6409,11 +6892,22 @@ function collectChecks() {
|
|
|
6409
6892
|
push("\u670D\u52A1\u7A33\u5B9A\u6027", "warn", `\u5185\u6838\u4E0A\u6B21\u5F02\u5E38\u9000\u51FA\uFF08\u9000\u51FA\u7801 ${service.lastExitCode}\uFF09`, "mihomo logs 0 \u67E5\u770B\u539F\u56E0");
|
|
6410
6893
|
}
|
|
6411
6894
|
} else {
|
|
6412
|
-
|
|
6895
|
+
if (!service.disabled && service.lastExitCode !== null && service.lastExitCode !== 0) {
|
|
6896
|
+
push("\u670D\u52A1", "fail", `\u5185\u6838\u4E0A\u6B21\u5F02\u5E38\u9000\u51FA\uFF08\u9000\u51FA\u7801 ${service.lastExitCode}\uFF09\uFF0Claunchd \u6B63\u5728\u53CD\u590D\u62C9\u8D77`, "mihomo logs 0 \u67E5\u770B\u539F\u56E0\uFF0Cmihomo stop \u505C\u6B62\u91CD\u8BD5");
|
|
6897
|
+
} else {
|
|
6898
|
+
push("\u670D\u52A1", "ok", `\u5DF2\u5B89\u88C5\uFF0C\u672A\u8FD0\u884C${service.disabled ? "\uFF08\u81EA\u542F\u5DF2\u5173\u95ED\uFF09" : ""}`);
|
|
6899
|
+
}
|
|
6413
6900
|
}
|
|
6414
6901
|
const state = getRunningState();
|
|
6415
6902
|
const info = getConfigInfo();
|
|
6416
|
-
|
|
6903
|
+
let mixedPortDefault = DEFAULT_MIXED_PORT;
|
|
6904
|
+
try {
|
|
6905
|
+
const ports = getPorts();
|
|
6906
|
+
mixedPortDefault = ports.mixed;
|
|
6907
|
+
} catch (e) {
|
|
6908
|
+
push("\u7AEF\u53E3\u914D\u7F6E", "fail", e.message, "\u4FEE\u6B63 settings.json \u7684 ports\uFF081-65535 \u6574\u6570\uFF0Cmixed \u4E0E controller \u4E0D\u80FD\u76F8\u540C\uFF09");
|
|
6909
|
+
}
|
|
6910
|
+
const mixedPort = info?.mixedPort ?? mixedPortDefault;
|
|
6417
6911
|
if (state.running) {
|
|
6418
6912
|
if (isPortListening(mixedPort)) {
|
|
6419
6913
|
push("\u7AEF\u53E3", "ok", `${mixedPort} \u6B63\u5728\u76D1\u542C`);
|
|
@@ -6437,61 +6931,41 @@ function collectChecks() {
|
|
|
6437
6931
|
push("\u914D\u7F6E\u6784\u5EFA", "warn", "\u65E0\u8BA2\u9605\uFF0C\u8DF3\u8FC7");
|
|
6438
6932
|
}
|
|
6439
6933
|
if (state.running && info?.mixedPort) {
|
|
6440
|
-
const probe =
|
|
6934
|
+
const probe = await probeProxyConnectivity(info.mixedPort);
|
|
6441
6935
|
if (probe.ok) {
|
|
6442
6936
|
push("\u4EE3\u7406\u8FDE\u901A", "ok", `HTTP ${probe.statusCode}\uFF08${probe.durationMs}ms\uFF09`);
|
|
6443
6937
|
} else {
|
|
6444
6938
|
push("\u4EE3\u7406\u8FDE\u901A", "warn", `\u4E0D\u901A: ${probe.error}`, "\u8282\u70B9\u53EF\u80FD\u5931\u6548\uFF0Cmihomo ui \u5207\u6362\u8282\u70B9");
|
|
6445
6939
|
}
|
|
6446
6940
|
} else {
|
|
6447
|
-
push("\u4EE3\u7406\u8FDE\u901A", "
|
|
6941
|
+
push("\u4EE3\u7406\u8FDE\u901A", "skip", "\u672A\u8FD0\u884C");
|
|
6448
6942
|
}
|
|
6449
|
-
|
|
6450
|
-
|
|
6451
|
-
|
|
6452
|
-
|
|
6453
|
-
|
|
6454
|
-
|
|
6455
|
-
|
|
6456
|
-
[
|
|
6457
|
-
"-x",
|
|
6458
|
-
`http://127.0.0.1:${port}`,
|
|
6459
|
-
"-s",
|
|
6460
|
-
"-o",
|
|
6461
|
-
"/dev/null",
|
|
6462
|
-
"-w",
|
|
6463
|
-
"%{http_code}",
|
|
6464
|
-
"--connect-timeout",
|
|
6465
|
-
"3",
|
|
6466
|
-
"--max-time",
|
|
6467
|
-
"5",
|
|
6468
|
-
"https://www.gstatic.com/generate_204"
|
|
6469
|
-
],
|
|
6470
|
-
{ encoding: "utf8", timeout: 8e3 }
|
|
6471
|
-
);
|
|
6472
|
-
const code = Number.parseInt((r.stdout || "").trim(), 10);
|
|
6473
|
-
const statusCode = Number.isFinite(code) ? code : null;
|
|
6474
|
-
const ok = statusCode !== null && statusCode >= 200 && statusCode < 300;
|
|
6475
|
-
return { ok, statusCode, error: ok ? null : `HTTP ${statusCode ?? "\u65E0\u54CD\u5E94"}`, durationMs: Date.now() - start };
|
|
6476
|
-
} catch (e) {
|
|
6477
|
-
return { ok: false, statusCode: null, error: e.message, durationMs: Date.now() - start };
|
|
6943
|
+
const latest = await getLatestNpmVersion(4e3);
|
|
6944
|
+
if (latest === null) {
|
|
6945
|
+
push("CLI \u7248\u672C", "skip", "npm registry \u4E0D\u53EF\u8FBE\uFF0C\u8DF3\u8FC7\u68C0\u67E5");
|
|
6946
|
+
} else if (compareVersions(latest, VERSION) > 0) {
|
|
6947
|
+
push("CLI \u7248\u672C", "warn", `\u5F53\u524D ${VERSION}\uFF0C\u6700\u65B0 ${latest}`, "mihomo update");
|
|
6948
|
+
} else {
|
|
6949
|
+
push("CLI \u7248\u672C", "ok", `${VERSION}\uFF08\u6700\u65B0\uFF09`);
|
|
6478
6950
|
}
|
|
6951
|
+
return checks;
|
|
6479
6952
|
}
|
|
6480
6953
|
async function cmdDoctor() {
|
|
6481
|
-
const checks = collectChecks();
|
|
6954
|
+
const checks = await collectChecks();
|
|
6482
6955
|
console.log("");
|
|
6483
6956
|
for (const c of checks) {
|
|
6484
|
-
const mark = c.status === "ok" ? colors.green("\u2713") : c.status === "warn" ? colors.yellow("!") : colors.red("\u2717");
|
|
6957
|
+
const mark = c.status === "ok" ? colors.green("\u2713") : c.status === "warn" ? colors.yellow("!") : c.status === "fail" ? colors.red("\u2717") : colors.gray("\xB7");
|
|
6485
6958
|
console.log(`${mark} ${colors.bold(c.name)}: ${c.detail}`);
|
|
6486
|
-
if (c.status !== "ok" && c.fix) {
|
|
6959
|
+
if (c.status !== "ok" && c.status !== "skip" && c.fix) {
|
|
6487
6960
|
console.log(colors.gray(` \u4FEE\u590D: ${c.fix}`));
|
|
6488
6961
|
}
|
|
6489
6962
|
}
|
|
6490
6963
|
const ok = checks.filter((c) => c.status === "ok").length;
|
|
6491
6964
|
const warn = checks.filter((c) => c.status === "warn").length;
|
|
6492
6965
|
const fail = checks.filter((c) => c.status === "fail").length;
|
|
6966
|
+
const skip = checks.filter((c) => c.status === "skip").length;
|
|
6493
6967
|
console.log("");
|
|
6494
|
-
console.log(`\u4F53\u68C0\u5B8C\u6210: ${ok} \u9879\u6B63\u5E38\uFF0C${warn} \u9879\u8B66\u544A\uFF0C${fail} \u9879\u5F02\u5E38`);
|
|
6968
|
+
console.log(`\u4F53\u68C0\u5B8C\u6210: ${ok} \u9879\u6B63\u5E38\uFF0C${warn} \u9879\u8B66\u544A\uFF0C${fail} \u9879\u5F02\u5E38${skip > 0 ? `\uFF0C${skip} \u9879\u8DF3\u8FC7` : ""}`);
|
|
6495
6969
|
console.log("");
|
|
6496
6970
|
if (fail > 0) {
|
|
6497
6971
|
throw new CliError(`\u53D1\u73B0 ${fail} \u9879\u5F02\u5E38`, {
|
|
@@ -6502,66 +6976,9 @@ async function cmdDoctor() {
|
|
|
6502
6976
|
}
|
|
6503
6977
|
|
|
6504
6978
|
// src/kernel.ts
|
|
6505
|
-
import { spawnSync as
|
|
6506
|
-
import
|
|
6507
|
-
import
|
|
6508
|
-
|
|
6509
|
-
// node_modules/compare-versions/lib/esm/utils.js
|
|
6510
|
-
var semver = /^[v^~<>=]*?(\d+)(?:\.([x*]|\d+)(?:\.([x*]|\d+)(?:\.([x*]|\d+))?(?:-([\da-z\-]+(?:\.[\da-z\-]+)*))?(?:\+[\da-z\-]+(?:\.[\da-z\-]+)*)?)?)?$/i;
|
|
6511
|
-
var validateAndParse = (version) => {
|
|
6512
|
-
if (typeof version !== "string") {
|
|
6513
|
-
throw new TypeError("Invalid argument expected string");
|
|
6514
|
-
}
|
|
6515
|
-
const match = version.match(semver);
|
|
6516
|
-
if (!match) {
|
|
6517
|
-
throw new Error(`Invalid argument not valid semver ('${version}' received)`);
|
|
6518
|
-
}
|
|
6519
|
-
match.shift();
|
|
6520
|
-
return match;
|
|
6521
|
-
};
|
|
6522
|
-
var isWildcard = (s) => s === "*" || s === "x" || s === "X";
|
|
6523
|
-
var tryParse = (v) => {
|
|
6524
|
-
const n = parseInt(v, 10);
|
|
6525
|
-
return isNaN(n) ? v : n;
|
|
6526
|
-
};
|
|
6527
|
-
var forceType = (a, b) => typeof a !== typeof b ? [String(a), String(b)] : [a, b];
|
|
6528
|
-
var compareStrings = (a, b) => {
|
|
6529
|
-
if (isWildcard(a) || isWildcard(b))
|
|
6530
|
-
return 0;
|
|
6531
|
-
const [ap, bp] = forceType(tryParse(a), tryParse(b));
|
|
6532
|
-
if (ap > bp)
|
|
6533
|
-
return 1;
|
|
6534
|
-
if (ap < bp)
|
|
6535
|
-
return -1;
|
|
6536
|
-
return 0;
|
|
6537
|
-
};
|
|
6538
|
-
var compareSegments = (a, b) => {
|
|
6539
|
-
for (let i = 0; i < Math.max(a.length, b.length); i++) {
|
|
6540
|
-
const r = compareStrings(a[i] || "0", b[i] || "0");
|
|
6541
|
-
if (r !== 0)
|
|
6542
|
-
return r;
|
|
6543
|
-
}
|
|
6544
|
-
return 0;
|
|
6545
|
-
};
|
|
6546
|
-
|
|
6547
|
-
// node_modules/compare-versions/lib/esm/compareVersions.js
|
|
6548
|
-
var compareVersions = (v1, v2) => {
|
|
6549
|
-
const n1 = validateAndParse(v1);
|
|
6550
|
-
const n2 = validateAndParse(v2);
|
|
6551
|
-
const p1 = n1.pop();
|
|
6552
|
-
const p2 = n2.pop();
|
|
6553
|
-
const r = compareSegments(n1, n2);
|
|
6554
|
-
if (r !== 0)
|
|
6555
|
-
return r;
|
|
6556
|
-
if (p1 && p2) {
|
|
6557
|
-
return compareSegments(p1.split("."), p2.split("."));
|
|
6558
|
-
} else if (p1 || p2) {
|
|
6559
|
-
return p1 ? -1 : 1;
|
|
6560
|
-
}
|
|
6561
|
-
return 0;
|
|
6562
|
-
};
|
|
6563
|
-
|
|
6564
|
-
// src/kernel.ts
|
|
6979
|
+
import { spawnSync as spawnSync7 } from "child_process";
|
|
6980
|
+
import fs12 from "fs";
|
|
6981
|
+
import path9 from "path";
|
|
6565
6982
|
var GITHUB_REPO = "MetaCubeX/mihomo";
|
|
6566
6983
|
var KERNEL_HTTP_TIMEOUT = 12e4;
|
|
6567
6984
|
var KERNEL_DOWNLOAD_TIMEOUT = 18e4;
|
|
@@ -6642,10 +7059,10 @@ async function checkUpdate() {
|
|
|
6642
7059
|
}
|
|
6643
7060
|
function findBinaryInDir(dir, maxDepth = 4) {
|
|
6644
7061
|
if (maxDepth <= 0) return null;
|
|
6645
|
-
const files =
|
|
7062
|
+
const files = fs12.readdirSync(dir);
|
|
6646
7063
|
for (const f of files) {
|
|
6647
|
-
const fullPath =
|
|
6648
|
-
const stat =
|
|
7064
|
+
const fullPath = path9.join(dir, f);
|
|
7065
|
+
const stat = fs12.lstatSync(fullPath);
|
|
6649
7066
|
if (stat.isDirectory()) {
|
|
6650
7067
|
const found = findBinaryInDir(fullPath, maxDepth - 1);
|
|
6651
7068
|
if (found) return found;
|
|
@@ -6673,7 +7090,7 @@ async function downloadKernel(progressCallback, mirror, releaseInfo) {
|
|
|
6673
7090
|
}
|
|
6674
7091
|
assertTrustedAssetUrl(asset.browser_download_url);
|
|
6675
7092
|
const downloadUrl = withMirror(asset.browser_download_url, mirror);
|
|
6676
|
-
const tempPath =
|
|
7093
|
+
const tempPath = path9.join(DIRS.kernel, path9.basename(asset.name));
|
|
6677
7094
|
const sizeMB = (asset.size / 1024 / 1024).toFixed(2);
|
|
6678
7095
|
if (mirror && progressCallback) {
|
|
6679
7096
|
progressCallback("\u63D0\u793A: \u7ECF\u7B2C\u4E09\u65B9\u955C\u50CF\u4E2D\u8F6C\u4E0B\u8F7D\uFF0C\u65E0\u6CD5\u9A8C\u8BC1\u6765\u6E90\u5B8C\u6574\u6027\uFF0C\u5EFA\u8BAE\u76F4\u8FDE\u6216\u81EA\u884C\u6821\u9A8C\u4EA7\u7269");
|
|
@@ -6682,7 +7099,7 @@ async function downloadKernel(progressCallback, mirror, releaseInfo) {
|
|
|
6682
7099
|
progressCallback(`\u4E0B\u8F7D\u5185\u6838: ${asset.name} (${sizeMB} MB)`);
|
|
6683
7100
|
}
|
|
6684
7101
|
const maxBytes = Number.isFinite(asset.size) && asset.size > 0 ? Math.floor(asset.size * 2 + 1024 * 1024) : 512 * 1024 * 1024;
|
|
6685
|
-
const curlResult =
|
|
7102
|
+
const curlResult = spawnSync7(
|
|
6686
7103
|
"curl",
|
|
6687
7104
|
[
|
|
6688
7105
|
"-L",
|
|
@@ -6711,19 +7128,19 @@ async function downloadKernel(progressCallback, mirror, releaseInfo) {
|
|
|
6711
7128
|
}
|
|
6712
7129
|
if (curlResult.status !== 0) {
|
|
6713
7130
|
try {
|
|
6714
|
-
|
|
7131
|
+
fs12.unlinkSync(tempPath);
|
|
6715
7132
|
} catch {
|
|
6716
7133
|
}
|
|
6717
7134
|
throw new Error(`\u4E0B\u8F7D\u5931\u8D25 (curl \u9000\u51FA\u7801 ${curlResult.status})`);
|
|
6718
7135
|
}
|
|
6719
|
-
if (!
|
|
7136
|
+
if (!fs12.existsSync(tempPath)) {
|
|
6720
7137
|
throw new Error("\u4E0B\u8F7D\u5931\u8D25: \u6587\u4EF6\u672A\u751F\u6210");
|
|
6721
7138
|
}
|
|
6722
7139
|
if (Number.isFinite(asset.size) && asset.size > 0) {
|
|
6723
|
-
const actual =
|
|
7140
|
+
const actual = fs12.statSync(tempPath).size;
|
|
6724
7141
|
if (actual !== asset.size) {
|
|
6725
7142
|
try {
|
|
6726
|
-
|
|
7143
|
+
fs12.unlinkSync(tempPath);
|
|
6727
7144
|
} catch {
|
|
6728
7145
|
}
|
|
6729
7146
|
throw new Error(
|
|
@@ -6739,7 +7156,7 @@ async function downloadKernel(progressCallback, mirror, releaseInfo) {
|
|
|
6739
7156
|
let extractedBinary = null;
|
|
6740
7157
|
try {
|
|
6741
7158
|
if (tempPath.endsWith(".tar.gz") || tempPath.endsWith(".tgz")) {
|
|
6742
|
-
const listResult =
|
|
7159
|
+
const listResult = spawnSync7("tar", ["-tzf", tempPath], { encoding: "utf8", timeout: 6e4 });
|
|
6743
7160
|
if (listResult.error) throw listResult.error;
|
|
6744
7161
|
if (listResult.status !== 0) throw new Error(`tar \u5217\u8868\u9000\u51FA\u7801 ${listResult.status}`);
|
|
6745
7162
|
for (const entry of (listResult.stdout || "").split("\n").filter(Boolean)) {
|
|
@@ -6747,7 +7164,7 @@ async function downloadKernel(progressCallback, mirror, releaseInfo) {
|
|
|
6747
7164
|
throw new Error(`\u5F52\u6863\u542B\u975E\u6CD5\u8DEF\u5F84\u6761\u76EE: ${entry}`);
|
|
6748
7165
|
}
|
|
6749
7166
|
}
|
|
6750
|
-
const typeResult =
|
|
7167
|
+
const typeResult = spawnSync7("tar", ["-tvzf", tempPath], { encoding: "utf8", timeout: 6e4 });
|
|
6751
7168
|
if (typeResult.error) throw typeResult.error;
|
|
6752
7169
|
if (typeResult.status !== 0) throw new Error(`tar \u5217\u8868\u9000\u51FA\u7801 ${typeResult.status}`);
|
|
6753
7170
|
for (const line of (typeResult.stdout || "").split("\n").filter(Boolean)) {
|
|
@@ -6756,24 +7173,24 @@ async function downloadKernel(progressCallback, mirror, releaseInfo) {
|
|
|
6756
7173
|
throw new Error(`\u5F52\u6863\u542B\u975E\u666E\u901A\u6587\u4EF6\u6761\u76EE\uFF08\u7C7B\u578B "${typeChar}"\uFF09: ${line}`);
|
|
6757
7174
|
}
|
|
6758
7175
|
}
|
|
6759
|
-
const tarResult =
|
|
7176
|
+
const tarResult = spawnSync7("tar", ["--no-same-owner", "-xzf", tempPath, "-C", extractPath], {
|
|
6760
7177
|
stdio: ["ignore", "ignore", "inherit"],
|
|
6761
7178
|
timeout: 6e4
|
|
6762
7179
|
});
|
|
6763
7180
|
if (tarResult.error) throw tarResult.error;
|
|
6764
7181
|
if (tarResult.status !== 0) throw new Error(`tar \u9000\u51FA\u7801 ${tarResult.status}`);
|
|
6765
7182
|
} else if (tempPath.endsWith(".gz")) {
|
|
6766
|
-
const baseName =
|
|
6767
|
-
const outputPath =
|
|
6768
|
-
const gzipResult =
|
|
7183
|
+
const baseName = path9.basename(tempPath, ".gz");
|
|
7184
|
+
const outputPath = path9.join(extractPath, baseName);
|
|
7185
|
+
const gzipResult = spawnSync7("gzip", ["-dc", tempPath], { maxBuffer: 256 * 1024 * 1024, timeout: 6e4 });
|
|
6769
7186
|
if (gzipResult.error) throw gzipResult.error;
|
|
6770
7187
|
if (gzipResult.status !== 0) throw new Error(`gzip \u9000\u51FA\u7801 ${gzipResult.status}`);
|
|
6771
|
-
|
|
7188
|
+
fs12.writeFileSync(outputPath, gzipResult.stdout, { mode: 493 });
|
|
6772
7189
|
extractedBinary = outputPath;
|
|
6773
7190
|
}
|
|
6774
7191
|
} catch (e) {
|
|
6775
7192
|
try {
|
|
6776
|
-
|
|
7193
|
+
fs12.unlinkSync(tempPath);
|
|
6777
7194
|
} catch {
|
|
6778
7195
|
}
|
|
6779
7196
|
throw new Error(`\u89E3\u538B\u5931\u8D25: ${e.message}`);
|
|
@@ -6781,35 +7198,35 @@ async function downloadKernel(progressCallback, mirror, releaseInfo) {
|
|
|
6781
7198
|
const foundBinary = extractedBinary || findBinaryInDir(extractPath);
|
|
6782
7199
|
if (!foundBinary) {
|
|
6783
7200
|
try {
|
|
6784
|
-
|
|
7201
|
+
fs12.unlinkSync(tempPath);
|
|
6785
7202
|
} catch {
|
|
6786
7203
|
}
|
|
6787
7204
|
throw new Error("\u89E3\u538B\u540E\u672A\u627E\u5230\u53EF\u6267\u884C\u6587\u4EF6");
|
|
6788
7205
|
}
|
|
6789
7206
|
const targetPath = PATHS.mihomoBinary;
|
|
6790
7207
|
if (foundBinary !== targetPath) {
|
|
6791
|
-
if (
|
|
6792
|
-
|
|
7208
|
+
if (fs12.existsSync(targetPath)) {
|
|
7209
|
+
fs12.chmodSync(targetPath, 493);
|
|
6793
7210
|
try {
|
|
6794
|
-
|
|
7211
|
+
fs12.unlinkSync(targetPath);
|
|
6795
7212
|
} catch {
|
|
6796
7213
|
}
|
|
6797
7214
|
}
|
|
6798
|
-
|
|
7215
|
+
fs12.renameSync(foundBinary, targetPath);
|
|
6799
7216
|
}
|
|
6800
|
-
|
|
7217
|
+
fs12.chmodSync(targetPath, 493);
|
|
6801
7218
|
if (progressCallback) {
|
|
6802
7219
|
progressCallback("\u6821\u9A8C\u5185\u6838...");
|
|
6803
7220
|
}
|
|
6804
|
-
const check =
|
|
7221
|
+
const check = spawnSync7(targetPath, ["-v"], { encoding: "utf8", timeout: 5e3 });
|
|
6805
7222
|
const checkOutput = `${check.stdout || ""}${check.stderr || ""}`.trim();
|
|
6806
7223
|
if (check.error || check.status !== 0 || !/v?\d+\.\d+\.\d+/.test(checkOutput)) {
|
|
6807
7224
|
try {
|
|
6808
|
-
|
|
7225
|
+
fs12.unlinkSync(targetPath);
|
|
6809
7226
|
} catch {
|
|
6810
7227
|
}
|
|
6811
7228
|
try {
|
|
6812
|
-
|
|
7229
|
+
fs12.unlinkSync(tempPath);
|
|
6813
7230
|
} catch {
|
|
6814
7231
|
}
|
|
6815
7232
|
throw new Error(`\u5185\u6838\u81EA\u68C0\u5931\u8D25\uFF08\u53EF\u80FD\u4E0B\u8F7D\u635F\u574F\u6216\u67B6\u6784\u4E0D\u5339\u914D\uFF09\uFF0C\u5DF2\u5220\u9664
|
|
@@ -6817,7 +7234,7 @@ async function downloadKernel(progressCallback, mirror, releaseInfo) {
|
|
|
6817
7234
|
\u8F93\u51FA: ${checkOutput || "(\u7A7A)"}`);
|
|
6818
7235
|
}
|
|
6819
7236
|
try {
|
|
6820
|
-
|
|
7237
|
+
fs12.unlinkSync(tempPath);
|
|
6821
7238
|
} catch {
|
|
6822
7239
|
}
|
|
6823
7240
|
clearKernelVersionCache();
|
|
@@ -6953,82 +7370,15 @@ function cmdLogs(args) {
|
|
|
6953
7370
|
if (!log.isCurrent) {
|
|
6954
7371
|
console.log(` \u67E5\u770B: mihomo logs ${archiveCounter} \u6216 mihomo logs ${archiveCounter} -o`);
|
|
6955
7372
|
}
|
|
6956
|
-
console.log("");
|
|
6957
|
-
}
|
|
6958
|
-
console.log("\u7528\u6CD5:");
|
|
6959
|
-
console.log(" mihomo logs 0 # \u67E5\u770B\u5F53\u524D\u65E5\u5FD7 (\u6700\u540E 100 \u884C)");
|
|
6960
|
-
console.log(" mihomo logs 0 -f # \u5B9E\u65F6\u8DDF\u968F\u5F53\u524D\u65E5\u5FD7");
|
|
6961
|
-
console.log(" mihomo logs 1 # \u67E5\u770B\u7B2C 1 \u4E2A\u5F52\u6863\u65E5\u5FD7\uFF08\u6700\u65B0\uFF09");
|
|
6962
|
-
console.log(" mihomo logs 1 -n 200 # \u67E5\u770B 200 \u884C");
|
|
6963
|
-
console.log(" mihomo logs 1 -o # \u7528\u7CFB\u7EDF\u9ED8\u8BA4\u7A0B\u5E8F\u6253\u5F00");
|
|
6964
|
-
console.log("");
|
|
6965
|
-
}
|
|
6966
|
-
|
|
6967
|
-
// src/commands/overwrite.ts
|
|
6968
|
-
import path8 from "path";
|
|
6969
|
-
function printOverwriteList() {
|
|
6970
|
-
const info = listOverwriteFile();
|
|
6971
|
-
const statusText = info.enabled ? colors.green("\u5DF2\u542F\u7528") : colors.yellow("\u5DF2\u7981\u7528");
|
|
6972
|
-
console.log(`${colors.gray("\u72B6\u6001: ")}${statusText}`);
|
|
6973
|
-
console.log(`${colors.gray("\u4F4D\u7F6E: ")}${info.dir}`);
|
|
6974
|
-
console.log("");
|
|
6975
|
-
if (info.files.length === 0) {
|
|
6976
|
-
console.log("\u6682\u65E0\u8986\u5199\u6587\u4EF6");
|
|
6977
|
-
console.log("");
|
|
6978
|
-
console.log(`\u7528\u6CD5\u793A\u4F8B: \u521B\u5EFA\u6587\u4EF6 ${path8.join(info.dir, "overwrite.yaml")}`);
|
|
6979
|
-
console.log(` \u6216 ${path8.join(info.dir, "overwrite.dns.yaml")}`);
|
|
6980
|
-
console.log("");
|
|
6981
|
-
} else {
|
|
6982
|
-
console.log(`${colors.cyan("\u8986\u5199\u6587\u4EF6")} (${info.files.length} \u4E2A\uFF0C\u6309\u987A\u5E8F\u52A0\u8F7D):`);
|
|
6983
|
-
console.log("");
|
|
6984
|
-
info.files.forEach((f, i) => {
|
|
6985
|
-
const num = i < 10 ? ` ${i}` : `${i}`;
|
|
6986
|
-
console.log(` ${num}. ${f.name}`);
|
|
6987
|
-
if (f.scope) {
|
|
6988
|
-
console.log(` ${colors.gray("\u4F5C\u7528\u57DF: ")}${f.scope}`);
|
|
6989
|
-
}
|
|
6990
|
-
if (f.keys.length > 0) {
|
|
6991
|
-
console.log(` ${colors.gray("\u5B57\u6BB5: ")}${f.keys.join(", ")}`);
|
|
6992
|
-
}
|
|
6993
|
-
});
|
|
6994
|
-
console.log("");
|
|
6995
|
-
}
|
|
6996
|
-
console.log("\u542F\u7528\u8986\u5199: mihomo ow on");
|
|
6997
|
-
console.log("\u7981\u7528\u8986\u5199: mihomo ow off");
|
|
6998
|
-
console.log("");
|
|
6999
|
-
}
|
|
7000
|
-
async function setOverwrite(enabled, args) {
|
|
7001
|
-
if (isOverwriteEnabled() === enabled) {
|
|
7002
|
-
console.log(`\u8986\u5199\u914D\u7F6E\u5DF2\u662F${enabled ? "\u542F\u7528" : "\u7981\u7528"}\u72B6\u6001`);
|
|
7003
|
-
console.log("");
|
|
7004
|
-
printOverwriteList();
|
|
7005
|
-
return;
|
|
7006
|
-
}
|
|
7007
|
-
setOverwriteEnabled(enabled);
|
|
7008
|
-
console.log(`\u5DF2${enabled ? "\u542F\u7528" : "\u7981\u7528"}\u8986\u5199\u914D\u7F6E`);
|
|
7009
|
-
if (await restartToApply(args)) return;
|
|
7010
|
-
console.log("");
|
|
7011
|
-
printOverwriteList();
|
|
7012
|
-
}
|
|
7013
|
-
var SUBCOMMANDS3 = [
|
|
7014
|
-
{ name: "on", aliases: ["enable"], handler: (args) => setOverwrite(true, args) },
|
|
7015
|
-
{ name: "off", aliases: ["disable"], handler: (args) => setOverwrite(false, args) }
|
|
7016
|
-
];
|
|
7017
|
-
async function cmdOverwrite(args) {
|
|
7018
|
-
await dispatchSubcommand(args, SUBCOMMANDS3, {
|
|
7019
|
-
// 无子命令 → 列表;未知子命令 → 报错(与 sub/dir 同构,避免 `ow onn` 静默当成 list)
|
|
7020
|
-
fallback: () => {
|
|
7021
|
-
console.log("");
|
|
7022
|
-
printOverwriteList();
|
|
7023
|
-
},
|
|
7024
|
-
onUnknown: (action) => {
|
|
7025
|
-
const names = SUBCOMMANDS3.flatMap((c) => [c.name, ...c.aliases ?? []]);
|
|
7026
|
-
const suggestion = suggestSimilar(action, names);
|
|
7027
|
-
throw new CliError(`\u672A\u77E5\u7684\u8986\u5199\u5B50\u547D\u4EE4: ${action}`, {
|
|
7028
|
-
hint: [...suggestion.length > 0 ? [`\u662F\u5426\u60F3\u8F93\u5165: ${suggestion.join(" / ")}?`] : [], "", "\u53EF\u7528\u5B50\u547D\u4EE4: on, off"]
|
|
7029
|
-
});
|
|
7030
|
-
}
|
|
7031
|
-
});
|
|
7373
|
+
console.log("");
|
|
7374
|
+
}
|
|
7375
|
+
console.log("\u7528\u6CD5:");
|
|
7376
|
+
console.log(" mihomo logs 0 # \u67E5\u770B\u5F53\u524D\u65E5\u5FD7 (\u6700\u540E 100 \u884C)");
|
|
7377
|
+
console.log(" mihomo logs 0 -f # \u5B9E\u65F6\u8DDF\u968F\u5F53\u524D\u65E5\u5FD7");
|
|
7378
|
+
console.log(" mihomo logs 1 # \u67E5\u770B\u7B2C 1 \u4E2A\u5F52\u6863\u65E5\u5FD7\uFF08\u6700\u65B0\uFF09");
|
|
7379
|
+
console.log(" mihomo logs 1 -n 200 # \u67E5\u770B 200 \u884C");
|
|
7380
|
+
console.log(" mihomo logs 1 -o # \u7528\u7CFB\u7EDF\u9ED8\u8BA4\u7A0B\u5E8F\u6253\u5F00");
|
|
7381
|
+
console.log("");
|
|
7032
7382
|
}
|
|
7033
7383
|
|
|
7034
7384
|
// src/commands/removed.ts
|
|
@@ -7039,30 +7389,30 @@ function removedCommand(name, version, hint) {
|
|
|
7039
7389
|
}
|
|
7040
7390
|
|
|
7041
7391
|
// src/commands/reset.ts
|
|
7042
|
-
import
|
|
7392
|
+
import fs14 from "fs";
|
|
7043
7393
|
|
|
7044
7394
|
// src/process-stop.ts
|
|
7045
|
-
import { spawnSync as
|
|
7046
|
-
import
|
|
7395
|
+
import { spawnSync as spawnSync8 } from "child_process";
|
|
7396
|
+
import fs13 from "fs";
|
|
7047
7397
|
var PROCESS_WAIT_ATTEMPTS = 50;
|
|
7048
7398
|
var PROCESS_WAIT_INTERVAL = 100;
|
|
7049
7399
|
var BATCH_KILL_THRESHOLD = 3;
|
|
7050
7400
|
function clearRuntime() {
|
|
7051
|
-
if (
|
|
7401
|
+
if (fs13.existsSync(DIRS.runtime)) {
|
|
7052
7402
|
rmrf(DIRS.runtime);
|
|
7053
7403
|
}
|
|
7054
7404
|
ensureDirs();
|
|
7055
7405
|
}
|
|
7056
7406
|
function clearPid() {
|
|
7057
|
-
if (!
|
|
7407
|
+
if (!fs13.existsSync(PATHS.pidFile)) return;
|
|
7058
7408
|
if (isPidFileOwnedByRoot()) {
|
|
7059
7409
|
try {
|
|
7060
|
-
|
|
7410
|
+
spawnSync8("sudo", ["rm", "-f", PATHS.pidFile], { stdio: "inherit", timeout: 1e4 });
|
|
7061
7411
|
} catch {
|
|
7062
7412
|
}
|
|
7063
7413
|
} else {
|
|
7064
7414
|
try {
|
|
7065
|
-
|
|
7415
|
+
fs13.unlinkSync(PATHS.pidFile);
|
|
7066
7416
|
} catch {
|
|
7067
7417
|
}
|
|
7068
7418
|
}
|
|
@@ -7080,7 +7430,7 @@ function killAllMihomo(forceSudo = false) {
|
|
|
7080
7430
|
const argv = forceSudo ? ["sudo", ["pkill", "-9", "-f", pattern]] : ["pkill", ["-9", "-f", pattern]];
|
|
7081
7431
|
const options = forceSudo ? { stdio: "inherit", timeout: 15e3 } : { timeout: 1e4 };
|
|
7082
7432
|
try {
|
|
7083
|
-
const result =
|
|
7433
|
+
const result = spawnSync8(argv[0], argv[1], options);
|
|
7084
7434
|
if (result.error) return false;
|
|
7085
7435
|
return result.status === 0 || result.status === 1;
|
|
7086
7436
|
} catch {
|
|
@@ -7209,8 +7559,8 @@ var RESET_TARGETS = [
|
|
|
7209
7559
|
label: "\u8986\u5199",
|
|
7210
7560
|
paths: () => {
|
|
7211
7561
|
const dir = USER_DATA_DIR;
|
|
7212
|
-
if (!
|
|
7213
|
-
return
|
|
7562
|
+
if (!fs14.existsSync(dir)) return [];
|
|
7563
|
+
return fs14.readdirSync(dir).filter(isOverwriteFilename).map((f) => `${dir}/${f}`);
|
|
7214
7564
|
},
|
|
7215
7565
|
needsStop: false
|
|
7216
7566
|
},
|
|
@@ -7351,7 +7701,7 @@ async function cmdReset(args) {
|
|
|
7351
7701
|
}
|
|
7352
7702
|
for (const t of targets) {
|
|
7353
7703
|
for (const p of t.paths()) {
|
|
7354
|
-
if (
|
|
7704
|
+
if (fs14.existsSync(p)) {
|
|
7355
7705
|
try {
|
|
7356
7706
|
rmrf(p);
|
|
7357
7707
|
} catch (e) {
|
|
@@ -7369,7 +7719,7 @@ async function cmdReset(args) {
|
|
|
7369
7719
|
}
|
|
7370
7720
|
|
|
7371
7721
|
// src/commands/service.ts
|
|
7372
|
-
import
|
|
7722
|
+
import fs15 from "fs";
|
|
7373
7723
|
async function handleLegacyInstall() {
|
|
7374
7724
|
if (!detectLegacySystemInstall()) return;
|
|
7375
7725
|
console.log(colors.yellow("\u68C0\u6D4B\u5230\u65E7\u7248\u672C\u5B89\u88C5\u7684\u7CFB\u7EDF\u7EA7\u670D\u52A1\uFF08root LaunchDaemon\uFF09"));
|
|
@@ -7404,7 +7754,7 @@ async function cmdInstall(_args) {
|
|
|
7404
7754
|
console.log(colors.green("\u5DF2\u6309\u539F\u72B6\u6001\u91CD\u65B0\u542F\u52A8"));
|
|
7405
7755
|
} else {
|
|
7406
7756
|
console.log("\u542F\u52A8: mihomo start");
|
|
7407
|
-
if (!
|
|
7757
|
+
if (!fs15.existsSync(PATHS.configFile)) {
|
|
7408
7758
|
console.log(colors.gray(" \u5C1A\u65E0\u8FD0\u884C\u65F6\u914D\u7F6E\uFF0Cstart \u4F1A\u5148\u8981\u6C42\u6DFB\u52A0\u8BA2\u9605"));
|
|
7409
7759
|
}
|
|
7410
7760
|
}
|
|
@@ -7438,6 +7788,9 @@ async function cmdUninstall(_args) {
|
|
|
7438
7788
|
console.log("\u624B\u52A8\u6E05\u7406: sudo pkill -9 mihomo");
|
|
7439
7789
|
}
|
|
7440
7790
|
console.log(colors.gray("\u91CD\u65B0\u5B89\u88C5: mihomo install"));
|
|
7791
|
+
console.log(colors.gray("\u5F7B\u5E95\u79FB\u9664 mihomo-cli:"));
|
|
7792
|
+
console.log(colors.gray(" mihomo reset --full # \u5220\u9664\u5168\u90E8\u6570\u636E\uFF08\u8BA2\u9605/\u5185\u6838/\u65E5\u5FD7\u7B49\uFF09"));
|
|
7793
|
+
console.log(colors.gray(" npm uninstall -g mihomo-cli"));
|
|
7441
7794
|
console.log("");
|
|
7442
7795
|
}
|
|
7443
7796
|
|
|
@@ -7480,198 +7833,11 @@ async function cmdStop(args) {
|
|
|
7480
7833
|
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")}`);
|
|
7481
7834
|
}
|
|
7482
7835
|
|
|
7483
|
-
// src/commands/subscription.ts
|
|
7484
|
-
function printRestartHintIfRunning() {
|
|
7485
|
-
if (getRunningState().running) {
|
|
7486
|
-
console.log(colors.yellow("\u63D0\u793A: \u8FD0\u884C\u4E2D\u7684\u5B9E\u4F8B\u4ECD\u4F7F\u7528\u65E7\u914D\u7F6E\uFF0C\u6267\u884C mihomo start \u4F7F\u66F4\u65B0\u751F\u6548"));
|
|
7487
|
-
console.log("");
|
|
7488
|
-
}
|
|
7489
|
-
}
|
|
7490
|
-
function printSubscriptionList() {
|
|
7491
|
-
const subs = getSubscriptionsWithCache();
|
|
7492
|
-
if (subs.length === 0) {
|
|
7493
|
-
console.log("\u6CA1\u6709\u8BA2\u9605");
|
|
7494
|
-
console.log("");
|
|
7495
|
-
console.log("\u6DFB\u52A0\u8BA2\u9605: mihomo sub add <url> [name]");
|
|
7496
|
-
console.log("");
|
|
7497
|
-
return;
|
|
7498
|
-
}
|
|
7499
|
-
const activeSub = getActiveSubscription();
|
|
7500
|
-
console.log(colors.cyan("\u8BA2\u9605\u5217\u8868:"));
|
|
7501
|
-
subs.forEach((s, i) => {
|
|
7502
|
-
const rel = formatRelativeTime(s.updated_at);
|
|
7503
|
-
const time = rel ? `${formatDate(s.updated_at)}\uFF08${rel}\uFF09` : formatDate(s.updated_at);
|
|
7504
|
-
const defaultMark = activeSub && s.name === activeSub.name ? colors.green(" [\u4F7F\u7528\u4E2D]") : "";
|
|
7505
|
-
const interval = resolveUpdateInterval(s.url, s.update_interval);
|
|
7506
|
-
console.log(` ${i + 1}. ${s.name}${defaultMark}`);
|
|
7507
|
-
console.log(` ${colors.gray("\u66F4\u65B0: ")}${time} (\u95F4\u9694: ${interval}h)`);
|
|
7508
|
-
if (s.username) {
|
|
7509
|
-
console.log(` ${colors.gray("\u7528\u6237: ")}${s.username}`);
|
|
7510
|
-
}
|
|
7511
|
-
const traffic = formatTraffic(s.upload, s.download, s.total);
|
|
7512
|
-
if (traffic) {
|
|
7513
|
-
console.log(` ${colors.gray("\u6D41\u91CF: ")}${traffic}`);
|
|
7514
|
-
}
|
|
7515
|
-
if (s.expire !== void 0) {
|
|
7516
|
-
console.log(` ${colors.gray("\u5230\u671F: ")}${formatTimestamp(s.expire)}`);
|
|
7517
|
-
}
|
|
7518
|
-
if (s.web_page_url) {
|
|
7519
|
-
console.log(` ${colors.gray("\u9875\u9762: ")}${s.web_page_url}`);
|
|
7520
|
-
}
|
|
7521
|
-
});
|
|
7522
|
-
console.log("");
|
|
7523
|
-
console.log("\u5207\u6362\u8BA2\u9605: mihomo sub use <name>");
|
|
7524
|
-
console.log("\u65B0\u589E\u8BA2\u9605: mihomo sub add <url> [name]");
|
|
7525
|
-
console.log("\u66F4\u65B0\u8BA2\u9605: mihomo sub update [name]");
|
|
7526
|
-
console.log("\u5220\u9664\u8BA2\u9605: mihomo sub remove <name>");
|
|
7527
|
-
console.log("");
|
|
7528
|
-
}
|
|
7529
|
-
function assertNotFlagLike(name, usage) {
|
|
7530
|
-
if (name.startsWith("-")) {
|
|
7531
|
-
throw new CliError(`\u540D\u79F0\u4E0D\u80FD\u4EE5 "-" \u5F00\u5934: "${name}"`, {
|
|
7532
|
-
label: "\u53C2\u6570\u9519\u8BEF",
|
|
7533
|
-
hint: ['\u4EE5 "-" \u5F00\u5934\u7684\u540D\u79F0\u4F1A\u4E0E\u547D\u4EE4\u884C\u9009\u9879\u6DF7\u6DC6\uFF0C\u5220\u9664\u65F6\u65E0\u6CD5\u6307\u5B9A\u3002', `\u7528\u6CD5: ${usage}`]
|
|
7534
|
-
});
|
|
7535
|
-
}
|
|
7536
|
-
}
|
|
7537
|
-
async function subAdd(args) {
|
|
7538
|
-
const url = args[2]?.trim();
|
|
7539
|
-
const name = args[3] || "default";
|
|
7540
|
-
if (!url) {
|
|
7541
|
-
throw new CliError("\u8BF7\u63D0\u4F9B\u6709\u6548\u7684\u8BA2\u9605 URL");
|
|
7542
|
-
}
|
|
7543
|
-
if (!isValidHttpUrl(url)) {
|
|
7544
|
-
throw new CliError("\u8BF7\u63D0\u4F9B\u6709\u6548\u7684\u8BA2\u9605 URL\uFF08\u9700\u4EE5 http:// \u6216 https:// \u5F00\u5934\uFF09");
|
|
7545
|
-
}
|
|
7546
|
-
assertNotFlagLike(name, "mihomo sub add <url> [name]");
|
|
7547
|
-
console.log(`\u6DFB\u52A0\u8BA2\u9605: ${name}`);
|
|
7548
|
-
addSubscription(url, name);
|
|
7549
|
-
try {
|
|
7550
|
-
const info = await withSpinner("\u4E0B\u8F7D\u8BA2\u9605", () => downloadSubscription(url, name));
|
|
7551
|
-
setDefaultSubscription(name);
|
|
7552
|
-
console.log(`\u5DF2\u6DFB\u52A0\u5E76\u5207\u6362\u5230 "${name}" (${formatProxySummary(info)})`);
|
|
7553
|
-
} catch (e) {
|
|
7554
|
-
removeSubscription(name);
|
|
7555
|
-
if (e instanceof CliError) throw new CliError(e.message, { label: "\u6DFB\u52A0\u5931\u8D25", hint: e.hint });
|
|
7556
|
-
throw new CliError(e.message, { label: "\u6DFB\u52A0\u5931\u8D25" });
|
|
7557
|
-
}
|
|
7558
|
-
console.log("");
|
|
7559
|
-
printSubscriptionList();
|
|
7560
|
-
}
|
|
7561
|
-
async function subUpdate(args) {
|
|
7562
|
-
const name = args[2];
|
|
7563
|
-
const subs = getSubscriptions();
|
|
7564
|
-
if (subs.length === 0) {
|
|
7565
|
-
throw new CliError("\u6CA1\u6709\u8BA2\u9605");
|
|
7566
|
-
}
|
|
7567
|
-
if (!name) {
|
|
7568
|
-
console.log(`\u66F4\u65B0\u6240\u6709 ${subs.length} \u4E2A\u8BA2\u9605...`);
|
|
7569
|
-
const results = await withSpinner("\u5E76\u884C\u66F4\u65B0\u4E2D", () => Promise.all(subs.map((sub) => tryUpdateOne(sub))));
|
|
7570
|
-
let ok = 0;
|
|
7571
|
-
for (const r of results) {
|
|
7572
|
-
if (r.success) ok++;
|
|
7573
|
-
printUpdateResult(r);
|
|
7574
|
-
}
|
|
7575
|
-
if (ok === 0) throw new CliError("\u5168\u90E8\u8BA2\u9605\u66F4\u65B0\u5931\u8D25");
|
|
7576
|
-
console.log("");
|
|
7577
|
-
printRestartHintIfRunning();
|
|
7578
|
-
printSubscriptionList();
|
|
7579
|
-
return;
|
|
7580
|
-
}
|
|
7581
|
-
const target = resolveSubscription(subs, name);
|
|
7582
|
-
console.log(`\u66F4\u65B0\u8BA2\u9605: ${target.name}`);
|
|
7583
|
-
const result = await withSpinner("\u4E0B\u8F7D\u8BA2\u9605", () => tryUpdateOne(target));
|
|
7584
|
-
if (!result.success) {
|
|
7585
|
-
throw new CliError((result.error || "").split("\n")[0], { label: "\u66F4\u65B0\u5931\u8D25" });
|
|
7586
|
-
}
|
|
7587
|
-
console.log(`\u5DF2\u66F4\u65B0 (${formatProxySummary(result)})`);
|
|
7588
|
-
console.log("");
|
|
7589
|
-
printRestartHintIfRunning();
|
|
7590
|
-
printSubscriptionList();
|
|
7591
|
-
}
|
|
7592
|
-
async function subUse(args) {
|
|
7593
|
-
const name = args[2];
|
|
7594
|
-
const subs = getSubscriptions();
|
|
7595
|
-
if (subs.length === 0) {
|
|
7596
|
-
throw new CliError("\u6CA1\u6709\u8BA2\u9605\uFF0C\u8BF7\u5148\u6DFB\u52A0\u8BA2\u9605", { hint: "mihomo sub add <url> [name]" });
|
|
7597
|
-
}
|
|
7598
|
-
if (!name) {
|
|
7599
|
-
throw new CliError("\u8BF7\u6307\u5B9A\u8BA2\u9605\u540D\u79F0", {
|
|
7600
|
-
hint: ["", "\u53EF\u7528\u8BA2\u9605:", ...subs.map((s) => ` ${s.name}`)]
|
|
7601
|
-
});
|
|
7602
|
-
}
|
|
7603
|
-
const target = resolveSubscription(subs, name);
|
|
7604
|
-
const currentDefault = getActiveSubscription();
|
|
7605
|
-
const isAlreadyDefault = currentDefault && currentDefault.name === target.name;
|
|
7606
|
-
if (isAlreadyDefault) {
|
|
7607
|
-
console.log(`"${target.name}" \u5DF2\u662F\u5F53\u524D\u4F7F\u7528\u7684\u8BA2\u9605`);
|
|
7608
|
-
console.log("");
|
|
7609
|
-
printSubscriptionList();
|
|
7610
|
-
return;
|
|
7611
|
-
}
|
|
7612
|
-
const success = setDefaultSubscription(target.name);
|
|
7613
|
-
if (!success) {
|
|
7614
|
-
throw new CliError(`\u672A\u627E\u5230\u8BA2\u9605 "${name}"`);
|
|
7615
|
-
}
|
|
7616
|
-
console.log(`\u5DF2\u5207\u6362\u5230 "${target.name}"`);
|
|
7617
|
-
if (await restartToApply(args)) return;
|
|
7618
|
-
console.log("");
|
|
7619
|
-
printSubscriptionList();
|
|
7620
|
-
}
|
|
7621
|
-
async function subRemove(args) {
|
|
7622
|
-
const name = getNonFlagArg(args, 2);
|
|
7623
|
-
const subs = getSubscriptions();
|
|
7624
|
-
if (!name) {
|
|
7625
|
-
throw new CliError("\u8BF7\u6307\u5B9A\u8981\u5220\u9664\u7684\u8BA2\u9605\u540D\u79F0", {
|
|
7626
|
-
hint: subs.length > 0 ? ["", "\u53EF\u7528\u8BA2\u9605:", ...subs.map((s) => ` ${s.name}`)] : void 0
|
|
7627
|
-
});
|
|
7628
|
-
}
|
|
7629
|
-
const target = resolveSubscription(subs, name);
|
|
7630
|
-
const isExact = target.name === name;
|
|
7631
|
-
const skipConfirm = hasFlag(args, "-y", "--yes");
|
|
7632
|
-
if (!isExact && !skipConfirm) {
|
|
7633
|
-
console.log(`\u5C06\u5220\u9664\u8BA2\u9605 "${target.name}" (\u6A21\u7CCA\u5339\u914D "${name}")`);
|
|
7634
|
-
if (!await confirmOrThrow("\u6B64\u64CD\u4F5C\u4E0D\u53EF\u6062\u590D\uFF0C\u786E\u8BA4?", {
|
|
7635
|
-
nonTtyMessage: `\u6A21\u7CCA\u5339\u914D\u5230 "${target.name}"\uFF0C\u975E\u4EA4\u4E92\u73AF\u5883\u9700\u786E\u8BA4`,
|
|
7636
|
-
hint: [`\u8BF7\u7528\u5B8C\u6574\u540D\u79F0: mihomo sub remove ${target.name}`, `\u6216\u8DF3\u8FC7\u786E\u8BA4: mihomo sub remove ${name} -y`]
|
|
7637
|
-
})) {
|
|
7638
|
-
console.log("\u5DF2\u53D6\u6D88");
|
|
7639
|
-
return;
|
|
7640
|
-
}
|
|
7641
|
-
}
|
|
7642
|
-
const switchedTo = removeSubscription(target.name);
|
|
7643
|
-
console.log(`\u5DF2\u5220\u9664\u8BA2\u9605 "${target.name}"`);
|
|
7644
|
-
if (switchedTo) {
|
|
7645
|
-
console.log(`\u5DF2\u81EA\u52A8\u5207\u6362\u5230 "${switchedTo}"`);
|
|
7646
|
-
}
|
|
7647
|
-
console.log("");
|
|
7648
|
-
printSubscriptionList();
|
|
7649
|
-
}
|
|
7650
|
-
var SUBCOMMANDS4 = [
|
|
7651
|
-
{ name: "add", handler: subAdd },
|
|
7652
|
-
{ name: "update", handler: subUpdate },
|
|
7653
|
-
{ name: "use", handler: subUse },
|
|
7654
|
-
{ name: "remove", aliases: ["rm", "delete"], handler: subRemove }
|
|
7655
|
-
];
|
|
7656
|
-
async function cmdSubscription(args) {
|
|
7657
|
-
await dispatchSubcommand(args, SUBCOMMANDS4, {
|
|
7658
|
-
// 无子命令 → 列表;未知子命令 → 报错
|
|
7659
|
-
fallback: printSubscriptionList,
|
|
7660
|
-
onUnknown: (action) => {
|
|
7661
|
-
const names = SUBCOMMANDS4.flatMap((c) => [c.name, ...c.aliases ?? []]);
|
|
7662
|
-
const suggestion = suggestSimilar(action, names);
|
|
7663
|
-
throw new CliError(`\u672A\u77E5\u7684\u8BA2\u9605\u547D\u4EE4: ${action}`, {
|
|
7664
|
-
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"]
|
|
7665
|
-
});
|
|
7666
|
-
}
|
|
7667
|
-
});
|
|
7668
|
-
}
|
|
7669
|
-
|
|
7670
7836
|
// src/commands/ui.ts
|
|
7671
|
-
import { spawnSync as
|
|
7837
|
+
import { spawnSync as spawnSync9 } from "child_process";
|
|
7672
7838
|
function copyToClipboard(text) {
|
|
7673
7839
|
try {
|
|
7674
|
-
return
|
|
7840
|
+
return spawnSync9("pbcopy", [], { input: text }).status === 0;
|
|
7675
7841
|
} catch {
|
|
7676
7842
|
return false;
|
|
7677
7843
|
}
|
|
@@ -7691,7 +7857,7 @@ function cmdUI(args) {
|
|
|
7691
7857
|
const secret = readSettings().controller_secret;
|
|
7692
7858
|
if (secret) {
|
|
7693
7859
|
console.log(
|
|
7694
|
-
copyToClipboard(secret) ? "\u5DF2\u914D\u7F6E\u8BBF\u95EE\u5BC6\u94A5\uFF08\u5DF2\u590D\u5236\u5230\u526A\u8D34\u677F\uFF0CUI \u8FDE\u63A5\u65F6\u7C98\u8D34\uFF09" :
|
|
7860
|
+
copyToClipboard(secret) ? "\u5DF2\u914D\u7F6E\u8BBF\u95EE\u5BC6\u94A5\uFF08\u5DF2\u590D\u5236\u5230\u526A\u8D34\u677F\uFF0CUI \u8FDE\u63A5\u65F6\u7C98\u8D34\uFF09" : `\u5DF2\u914D\u7F6E\u8BBF\u95EE\u5BC6\u94A5\uFF08UI \u8FDE\u63A5 127.0.0.1:${getPorts().controller} \u65F6\u9700\u8F93\u5165\uFF0C\u5BC6\u94A5\u89C1 settings.json\uFF09`
|
|
7695
7861
|
);
|
|
7696
7862
|
}
|
|
7697
7863
|
const success = openUrl(url);
|
|
@@ -7700,66 +7866,6 @@ function cmdUI(args) {
|
|
|
7700
7866
|
}
|
|
7701
7867
|
}
|
|
7702
7868
|
|
|
7703
|
-
// src/commands/update.ts
|
|
7704
|
-
import { execFile as execFile2, spawn as spawn2 } from "child_process";
|
|
7705
|
-
import { promisify as promisify2 } from "util";
|
|
7706
|
-
var execFileAsync2 = promisify2(execFile2);
|
|
7707
|
-
var NPM_VIEW_TIMEOUT_MS = 15e3;
|
|
7708
|
-
async function getLatestNpmVersion() {
|
|
7709
|
-
try {
|
|
7710
|
-
const { stdout } = await execFileAsync2("npm", ["view", PKG_NAME, "version"], { timeout: NPM_VIEW_TIMEOUT_MS });
|
|
7711
|
-
const version = stdout.trim().split("\n").pop()?.trim();
|
|
7712
|
-
return version || null;
|
|
7713
|
-
} catch {
|
|
7714
|
-
return null;
|
|
7715
|
-
}
|
|
7716
|
-
}
|
|
7717
|
-
async function cmdUpdate() {
|
|
7718
|
-
console.log(`\u5F53\u524D\u7248\u672C: ${colors.cyan(VERSION)}`);
|
|
7719
|
-
console.log("");
|
|
7720
|
-
const latest = await withSpinner("\u67E5\u8BE2 npm \u6700\u65B0\u7248\u672C", getLatestNpmVersion);
|
|
7721
|
-
if (latest && latest === VERSION) {
|
|
7722
|
-
console.log(`\u5DF2\u662F\u6700\u65B0\u7248\u672C (${colors.green(VERSION)})\uFF0C\u65E0\u9700\u66F4\u65B0`);
|
|
7723
|
-
return;
|
|
7724
|
-
}
|
|
7725
|
-
if (latest) {
|
|
7726
|
-
console.log(`\u6700\u65B0\u7248\u672C: ${colors.cyan(latest)}`);
|
|
7727
|
-
} else {
|
|
7728
|
-
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"));
|
|
7729
|
-
}
|
|
7730
|
-
console.log("");
|
|
7731
|
-
console.log("\u6B63\u5728\u66F4\u65B0 mihomo-cli...");
|
|
7732
|
-
console.log("");
|
|
7733
|
-
await new Promise((resolve, reject) => {
|
|
7734
|
-
const npm = spawn2("npm", ["install", "-g", PKG_NAME], { stdio: "inherit" });
|
|
7735
|
-
npm.on("close", (code) => {
|
|
7736
|
-
if (code === 0) {
|
|
7737
|
-
resolve();
|
|
7738
|
-
} else {
|
|
7739
|
-
reject(new CliError("\u66F4\u65B0\u5931\u8D25\u3002\u82E5\u4E3A\u6743\u9650\u95EE\u9898\uFF08EACCES\uFF09\uFF0C\u53EF\u5C1D\u8BD5: sudo npm install -g mihomo-cli", { exitCode: code || 1 }));
|
|
7740
|
-
}
|
|
7741
|
-
});
|
|
7742
|
-
npm.on("error", (e) => {
|
|
7743
|
-
const perm = e.message.includes("EACCES") || e.message.includes("permission");
|
|
7744
|
-
reject(perm ? new CliError("\u6743\u9650\u4E0D\u8DB3\uFF0C\u53EF\u5C1D\u8BD5: sudo npm install -g mihomo-cli") : new CliError(`\u6267\u884C\u5931\u8D25: ${e.message}`));
|
|
7745
|
-
});
|
|
7746
|
-
});
|
|
7747
|
-
try {
|
|
7748
|
-
const { stdout } = await execFileAsync2("npm", ["list", "-g", PKG_NAME, "--json", "--depth=0"]);
|
|
7749
|
-
const result = JSON.parse(stdout);
|
|
7750
|
-
const newVersion = result.dependencies?.[PKG_NAME]?.version;
|
|
7751
|
-
console.log("");
|
|
7752
|
-
if (newVersion) {
|
|
7753
|
-
console.log(`\u66F4\u65B0\u5B8C\u6210\uFF0C\u6700\u65B0\u7248\u672C: ${colors.green(newVersion)}`);
|
|
7754
|
-
} else {
|
|
7755
|
-
console.log("\u66F4\u65B0\u5B8C\u6210");
|
|
7756
|
-
}
|
|
7757
|
-
} catch {
|
|
7758
|
-
console.log("");
|
|
7759
|
-
console.log("\u66F4\u65B0\u5B8C\u6210");
|
|
7760
|
-
}
|
|
7761
|
-
}
|
|
7762
|
-
|
|
7763
7869
|
// src/commands/registry.ts
|
|
7764
7870
|
var COMMANDS = [
|
|
7765
7871
|
// === 控制 ===
|
|
@@ -7804,7 +7910,7 @@ var COMMANDS = [
|
|
|
7804
7910
|
aliases: [],
|
|
7805
7911
|
handler: printStatus,
|
|
7806
7912
|
group: "control",
|
|
7807
|
-
usage: [{ signature: "status", description: "\u67E5\u770B\u72B6\u6001" }]
|
|
7913
|
+
usage: [{ signature: "status [-j|--json] [--no-probe]", description: "\u67E5\u770B\u72B6\u6001" }]
|
|
7808
7914
|
},
|
|
7809
7915
|
// === 已移除(墓碑:显式报错指引迁移,不在帮助中列出) ===
|
|
7810
7916
|
{
|
|
@@ -7819,21 +7925,24 @@ var COMMANDS = [
|
|
|
7819
7925
|
"\u5F7B\u5E95\u79FB\u9664\u670D\u52A1: mihomo uninstall"
|
|
7820
7926
|
]),
|
|
7821
7927
|
group: "meta",
|
|
7822
|
-
usage: []
|
|
7928
|
+
usage: [],
|
|
7929
|
+
hidden: true
|
|
7823
7930
|
},
|
|
7824
7931
|
{
|
|
7825
7932
|
name: "up",
|
|
7826
7933
|
aliases: [],
|
|
7827
7934
|
handler: removedCommand("up", "v4.1.0", ["\u8BF7\u7528: mihomo start"]),
|
|
7828
7935
|
group: "meta",
|
|
7829
|
-
usage: []
|
|
7936
|
+
usage: [],
|
|
7937
|
+
hidden: true
|
|
7830
7938
|
},
|
|
7831
7939
|
{
|
|
7832
7940
|
name: "down",
|
|
7833
7941
|
aliases: [],
|
|
7834
7942
|
handler: removedCommand("down", "v4.1.0", ["\u8BF7\u7528: mihomo stop"]),
|
|
7835
7943
|
group: "meta",
|
|
7836
|
-
usage: []
|
|
7944
|
+
usage: [],
|
|
7945
|
+
hidden: true
|
|
7837
7946
|
},
|
|
7838
7947
|
// === 界面 ===
|
|
7839
7948
|
{
|
|
@@ -7844,13 +7953,14 @@ var COMMANDS = [
|
|
|
7844
7953
|
usage: [{ signature: "ui [zash|dash|yacd]", description: "\u6253\u5F00 Web UI\uFF08\u9ED8\u8BA4 zash\uFF09" }]
|
|
7845
7954
|
},
|
|
7846
7955
|
{
|
|
7847
|
-
// 已并入 `logs -f`;保留为隐藏别名过渡,不在帮助中列出(usage
|
|
7956
|
+
// 已并入 `logs -f`;保留为隐藏别名过渡,不在帮助中列出(usage 留空),也不进补全词表
|
|
7848
7957
|
name: "log",
|
|
7849
7958
|
aliases: [],
|
|
7850
7959
|
handler: cmdLogs,
|
|
7851
7960
|
rewrite: (args) => ["logs", "0", "-f", ...args.slice(1)],
|
|
7852
7961
|
group: "interface",
|
|
7853
|
-
usage: []
|
|
7962
|
+
usage: [],
|
|
7963
|
+
hidden: true
|
|
7854
7964
|
},
|
|
7855
7965
|
{
|
|
7856
7966
|
name: "logs",
|
|
@@ -7930,14 +8040,17 @@ var COMMANDS = [
|
|
|
7930
8040
|
aliases: [],
|
|
7931
8041
|
handler: cmdDoctor,
|
|
7932
8042
|
group: "system",
|
|
7933
|
-
usage: [{ signature: "doctor", description: "\u4F53\u68C0\u8BCA\u65AD\uFF08\u5185\u6838/\u670D\u52A1/\u7AEF\u53E3/\u8BA2\u9605/\u914D\u7F6E/\u8FDE\u901A\u6027\uFF0C\u6709\u5F02\u5E38\u9000\u51FA\u7801 1\uFF09" }]
|
|
8043
|
+
usage: [{ signature: "doctor", description: "\u4F53\u68C0\u8BCA\u65AD\uFF08\u5185\u6838/\u670D\u52A1/\u7AEF\u53E3/\u8BA2\u9605/\u914D\u7F6E/\u8FDE\u901A\u6027/CLI \u7248\u672C\uFF0C\u6709\u5F02\u5E38\u9000\u51FA\u7801 1\uFF09" }]
|
|
7934
8044
|
},
|
|
7935
8045
|
{
|
|
7936
8046
|
name: "completion",
|
|
7937
8047
|
aliases: [],
|
|
7938
|
-
handler: cmdCompletion,
|
|
8048
|
+
handler: (args) => cmdCompletion(args, COMMANDS),
|
|
7939
8049
|
group: "system",
|
|
7940
|
-
usage: [
|
|
8050
|
+
usage: [
|
|
8051
|
+
{ signature: "completion install <zsh|bash|fish>", description: "\u5B89\u88C5\u8865\u5168\u5230\u5BF9\u5E94 shell \u7684\u9ED8\u8BA4\u4F4D\u7F6E" },
|
|
8052
|
+
{ signature: "completion <zsh|bash|fish>", description: "\u8F93\u51FA\u8865\u5168\u811A\u672C\uFF08\u91CD\u5B9A\u5411\u6216 eval \u4F7F\u7528\uFF09" }
|
|
8053
|
+
]
|
|
7941
8054
|
},
|
|
7942
8055
|
// === meta(不在分组清单展示,help 末尾单列) ===
|
|
7943
8056
|
{
|