mihomo-cli 3.9.1 → 3.10.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 +42 -0
- package/README.md +18 -29
- package/dist/index.js +122 -954
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -19,7 +19,7 @@ var colors = {
|
|
|
19
19
|
import { spawnSync } from "child_process";
|
|
20
20
|
import fs5 from "fs";
|
|
21
21
|
|
|
22
|
-
//
|
|
22
|
+
// node_modules/js-yaml/dist/js-yaml.mjs
|
|
23
23
|
var NOT_RESOLVED = /* @__PURE__ */ Symbol("NOT_RESOLVED");
|
|
24
24
|
function defineScalarTag(tagName, options) {
|
|
25
25
|
return {
|
|
@@ -3078,7 +3078,6 @@ var LAUNCH_DAEMON_LABEL = RAW_DAEMON_LABEL && isValidDaemonLabel(RAW_DAEMON_LABE
|
|
|
3078
3078
|
var CONTROLLER_PORT = 9090;
|
|
3079
3079
|
var CONTROLLER_ADDR = `127.0.0.1:${CONTROLLER_PORT}`;
|
|
3080
3080
|
var CONTROLLER_BASE_URL = `http://${CONTROLLER_ADDR}`;
|
|
3081
|
-
var TEST_CONTROLLER_ADDR = "127.0.0.1:29090";
|
|
3082
3081
|
var TUN_CONFIG = {
|
|
3083
3082
|
tun: {
|
|
3084
3083
|
enable: true,
|
|
@@ -3089,13 +3088,6 @@ var TUN_CONFIG = {
|
|
|
3089
3088
|
"strict-route": true
|
|
3090
3089
|
}
|
|
3091
3090
|
};
|
|
3092
|
-
var TEST_CONFIG = {
|
|
3093
|
-
"mixed-port": 27890,
|
|
3094
|
-
"allow-lan": false,
|
|
3095
|
-
"external-controller": TEST_CONTROLLER_ADDR,
|
|
3096
|
-
"log-level": "error",
|
|
3097
|
-
"geodata-mode": true
|
|
3098
|
-
};
|
|
3099
3091
|
var BASE_CONFIG = {
|
|
3100
3092
|
"mixed-port": 7890,
|
|
3101
3093
|
"allow-lan": false,
|
|
@@ -3119,13 +3111,6 @@ var BASE_CONFIG = {
|
|
|
3119
3111
|
var DEFAULT_UPDATE_INTERVAL_HOURS = 12;
|
|
3120
3112
|
var DEFAULT_UPDATE_INTERVAL_HOURS_GITHUB = 6;
|
|
3121
3113
|
var DEFAULT_AUTO_UPDATE_TIMEOUT = 1e4;
|
|
3122
|
-
var DEFAULT_TEST_TIMEOUT = 2e3;
|
|
3123
|
-
var DEFAULT_TEST_CONCURRENCY = 100;
|
|
3124
|
-
var DEFAULT_TEST_URL = "http://www.gstatic.com/generate_204";
|
|
3125
|
-
var DEFAULT_CLEAN_ROUNDS = 2;
|
|
3126
|
-
var AUTO_CLEAN_THRESHOLD = 100;
|
|
3127
|
-
var AUTO_CLEAN_THRESHOLD_GITHUB = 50;
|
|
3128
|
-
var AUTO_CLEAN_COOLDOWN_HOURS = 12;
|
|
3129
3114
|
|
|
3130
3115
|
// src/errors.ts
|
|
3131
3116
|
var TimeoutError = class extends Error {
|
|
@@ -3365,24 +3350,8 @@ function maskSingleUrl(url) {
|
|
|
3365
3350
|
return url;
|
|
3366
3351
|
}
|
|
3367
3352
|
}
|
|
3368
|
-
function looksLikeMultiUrl(url) {
|
|
3369
|
-
if (!url.includes(",")) return false;
|
|
3370
|
-
const parts = url.split(",").map((u) => u.trim()).filter(Boolean);
|
|
3371
|
-
if (parts.length < 2) return false;
|
|
3372
|
-
return parts.every((p) => {
|
|
3373
|
-
try {
|
|
3374
|
-
const u = new URL(p);
|
|
3375
|
-
return u.protocol === "http:" || u.protocol === "https:";
|
|
3376
|
-
} catch {
|
|
3377
|
-
return false;
|
|
3378
|
-
}
|
|
3379
|
-
});
|
|
3380
|
-
}
|
|
3381
3353
|
function maskUrl(url) {
|
|
3382
3354
|
if (!url) return url;
|
|
3383
|
-
if (looksLikeMultiUrl(url)) {
|
|
3384
|
-
return url.split(",").map((u) => maskSingleUrl(u.trim())).join(", ");
|
|
3385
|
-
}
|
|
3386
3355
|
return maskSingleUrl(url);
|
|
3387
3356
|
}
|
|
3388
3357
|
function readSubscriptionCache() {
|
|
@@ -3689,20 +3658,6 @@ function summarizeMatch(match) {
|
|
|
3689
3658
|
}
|
|
3690
3659
|
return parts.length > 0 ? parts.join(", ") : void 0;
|
|
3691
3660
|
}
|
|
3692
|
-
function splitUrlsLocal(url) {
|
|
3693
|
-
const isValidHttp = (u) => {
|
|
3694
|
-
try {
|
|
3695
|
-
const parsed = new URL(u.trim());
|
|
3696
|
-
return parsed.protocol === "http:" || parsed.protocol === "https:";
|
|
3697
|
-
} catch {
|
|
3698
|
-
return false;
|
|
3699
|
-
}
|
|
3700
|
-
};
|
|
3701
|
-
if (!url.includes(",")) return [url.trim()];
|
|
3702
|
-
const parts = url.split(",").map((u) => u.trim()).filter(Boolean);
|
|
3703
|
-
if (parts.length > 1 && parts.every(isValidHttp)) return parts;
|
|
3704
|
-
return [url.trim()];
|
|
3705
|
-
}
|
|
3706
3661
|
function hostMatchesDomain(host, domain) {
|
|
3707
3662
|
const h = host.toLowerCase();
|
|
3708
3663
|
const d = domain.toLowerCase();
|
|
@@ -3719,16 +3674,13 @@ function matchesScope(match, scope) {
|
|
|
3719
3674
|
if (match["url-domain"]) {
|
|
3720
3675
|
const domains = Array.isArray(match["url-domain"]) ? match["url-domain"] : [match["url-domain"]];
|
|
3721
3676
|
if (!scope?.subUrl) return false;
|
|
3722
|
-
|
|
3723
|
-
|
|
3724
|
-
|
|
3725
|
-
|
|
3726
|
-
|
|
3727
|
-
}
|
|
3677
|
+
let host;
|
|
3678
|
+
try {
|
|
3679
|
+
host = new URL(scope.subUrl.trim()).hostname;
|
|
3680
|
+
} catch {
|
|
3681
|
+
return false;
|
|
3728
3682
|
}
|
|
3729
|
-
if (
|
|
3730
|
-
const ok = domains.some((d) => hosts.some((h) => hostMatchesDomain(h, d)));
|
|
3731
|
-
if (!ok) return false;
|
|
3683
|
+
if (!domains.some((d) => hostMatchesDomain(host, d))) return false;
|
|
3732
3684
|
}
|
|
3733
3685
|
return true;
|
|
3734
3686
|
}
|
|
@@ -3975,30 +3927,17 @@ function parseStringArg(args, long, short) {
|
|
|
3975
3927
|
}
|
|
3976
3928
|
return null;
|
|
3977
3929
|
}
|
|
3978
|
-
var VALUE_FLAGS = /* @__PURE__ */ new Set([
|
|
3979
|
-
"-t",
|
|
3980
|
-
"--timeout",
|
|
3981
|
-
"-j",
|
|
3982
|
-
"--concurrency",
|
|
3983
|
-
"-r",
|
|
3984
|
-
"--rounds",
|
|
3985
|
-
"-n",
|
|
3986
|
-
"--lines",
|
|
3987
|
-
"-u",
|
|
3988
|
-
"--update-timeout",
|
|
3989
|
-
"--host",
|
|
3990
|
-
"--port"
|
|
3991
|
-
]);
|
|
3930
|
+
var VALUE_FLAGS = /* @__PURE__ */ new Set(["-n", "--lines", "-u", "--update-timeout", "--host", "--port"]);
|
|
3992
3931
|
function extractStartOptions(args) {
|
|
3993
3932
|
if (!args) return [];
|
|
3994
|
-
const BOOL_FLAGS = /* @__PURE__ */ new Set(["-s", "--no-update", "--no-
|
|
3933
|
+
const BOOL_FLAGS = /* @__PURE__ */ new Set(["-s", "--no-update", "--no-ssh"]);
|
|
3995
3934
|
const out = [];
|
|
3996
3935
|
for (let i = 0; i < args.length; i++) {
|
|
3997
3936
|
const a = args[i];
|
|
3998
3937
|
if (VALUE_FLAGS.has(a)) {
|
|
3999
3938
|
out.push(a);
|
|
4000
3939
|
if (i + 1 < args.length) out.push(args[++i]);
|
|
4001
|
-
} else if (BOOL_FLAGS.has(a) || /^--
|
|
3940
|
+
} else if (BOOL_FLAGS.has(a) || /^--update-timeout=/.test(a)) {
|
|
4002
3941
|
out.push(a);
|
|
4003
3942
|
}
|
|
4004
3943
|
}
|
|
@@ -4070,20 +4009,21 @@ function normalizeMirrorUrl(val) {
|
|
|
4070
4009
|
}
|
|
4071
4010
|
function parseMirrorArg(args) {
|
|
4072
4011
|
if (!args || args.length < 2) {
|
|
4073
|
-
return { mirror: null, isOverride: false
|
|
4012
|
+
return { mirror: null, isOverride: false };
|
|
4074
4013
|
}
|
|
4075
|
-
if (args.
|
|
4076
|
-
|
|
4014
|
+
if (args.some((a) => a === "--mirror-all" || a.startsWith("--mirror-all="))) {
|
|
4015
|
+
throw new CliError("--mirror-all \u5DF2\u79FB\u9664\uFF08v3.10.0\uFF09", {
|
|
4016
|
+
label: "\u53C2\u6570\u9519\u8BEF",
|
|
4017
|
+
hint: [
|
|
4018
|
+
"\u7248\u672C\u67E5\u8BE2\uFF08GitHub API\uFF09\u73B0\u5728\u6052\u76F4\u8FDE\uFF0C\u955C\u50CF\u53EA\u4F5C\u7528\u4E8E\u5185\u6838\u4EA7\u7269\u4E0B\u8F7D\u3002",
|
|
4019
|
+
"API \u82E5\u8D70\u955C\u50CF\uFF0C\u4E0B\u8F7D\u5730\u5740\u5C31\u7531\u955C\u50CF\u8BF4\u4E86\u7B97\uFF0C\u800C\u5185\u6838\u968F\u540E\u4EE5 root \u8FD0\u884C\u3002",
|
|
4020
|
+
"",
|
|
4021
|
+
"\u6539\u7528: mihomo kernel --mirror [\u955C\u50CF]"
|
|
4022
|
+
]
|
|
4023
|
+
});
|
|
4077
4024
|
}
|
|
4078
|
-
|
|
4079
|
-
|
|
4080
|
-
if (mirrorAllIdx >= 0 || mirrorAllEq) {
|
|
4081
|
-
const inline = mirrorAllEq?.slice("--mirror-all=".length);
|
|
4082
|
-
const nextArg = inline ?? args[mirrorAllIdx + 1];
|
|
4083
|
-
if (!nextArg || nextArg.startsWith("-")) {
|
|
4084
|
-
return { mirror: DEFAULT_MIRROR, isOverride: true, type: "all" };
|
|
4085
|
-
}
|
|
4086
|
-
return { mirror: normalizeMirrorUrl(nextArg), isOverride: true, type: "all" };
|
|
4025
|
+
if (args.includes("--no-mirror") || args.includes("--direct")) {
|
|
4026
|
+
return { mirror: null, isOverride: true };
|
|
4087
4027
|
}
|
|
4088
4028
|
const mirrorEq = args.find((a) => a.startsWith("--mirror="));
|
|
4089
4029
|
const mirrorIdx = args.indexOf("--mirror");
|
|
@@ -4091,11 +4031,11 @@ function parseMirrorArg(args) {
|
|
|
4091
4031
|
const inline = mirrorEq?.slice("--mirror=".length);
|
|
4092
4032
|
const nextArg = inline ?? args[mirrorIdx + 1];
|
|
4093
4033
|
if (!nextArg || nextArg.startsWith("-")) {
|
|
4094
|
-
return { mirror: DEFAULT_MIRROR, isOverride: true
|
|
4034
|
+
return { mirror: DEFAULT_MIRROR, isOverride: true };
|
|
4095
4035
|
}
|
|
4096
|
-
return { mirror: normalizeMirrorUrl(nextArg), isOverride: true
|
|
4036
|
+
return { mirror: normalizeMirrorUrl(nextArg), isOverride: true };
|
|
4097
4037
|
}
|
|
4098
|
-
return { mirror: null, isOverride: false
|
|
4038
|
+
return { mirror: null, isOverride: false };
|
|
4099
4039
|
}
|
|
4100
4040
|
|
|
4101
4041
|
// src/config.ts
|
|
@@ -5038,29 +4978,6 @@ function listLogs() {
|
|
|
5038
4978
|
result.archives.sort((a, b) => b.mtime.getTime() - a.mtime.getTime());
|
|
5039
4979
|
return result;
|
|
5040
4980
|
}
|
|
5041
|
-
function isPathUnderDir(filePath, baseDir) {
|
|
5042
|
-
const resolvedPath = path5.resolve(filePath);
|
|
5043
|
-
const resolvedBase = path5.resolve(baseDir);
|
|
5044
|
-
return resolvedPath === resolvedBase || resolvedPath.startsWith(resolvedBase + path5.sep);
|
|
5045
|
-
}
|
|
5046
|
-
function getLogPathByName(name) {
|
|
5047
|
-
const logsDir = DIRS.logs;
|
|
5048
|
-
let targetName = name;
|
|
5049
|
-
if (!name.endsWith(".log")) targetName = `mihomo.${name}.log`;
|
|
5050
|
-
if (!targetName.startsWith("mihomo.")) targetName = `mihomo.${targetName}`;
|
|
5051
|
-
const filePath = path5.join(logsDir, targetName);
|
|
5052
|
-
if (fs6.existsSync(filePath) && isPathUnderDir(filePath, logsDir)) return filePath;
|
|
5053
|
-
if (fs6.existsSync(logsDir)) {
|
|
5054
|
-
const files = fs6.readdirSync(logsDir);
|
|
5055
|
-
for (const file of files) {
|
|
5056
|
-
if (file.includes(name)) {
|
|
5057
|
-
const candidatePath = path5.join(logsDir, file);
|
|
5058
|
-
if (isPathUnderDir(candidatePath, logsDir)) return candidatePath;
|
|
5059
|
-
}
|
|
5060
|
-
}
|
|
5061
|
-
}
|
|
5062
|
-
return null;
|
|
5063
|
-
}
|
|
5064
4981
|
function openUrl(url) {
|
|
5065
4982
|
try {
|
|
5066
4983
|
const child = spawn("open", ["--", url], { stdio: "ignore", detached: true });
|
|
@@ -5390,11 +5307,7 @@ async function readBodyWithLimit(response, controller, limit) {
|
|
|
5390
5307
|
|
|
5391
5308
|
// src/subscription.ts
|
|
5392
5309
|
function isGithubUrl(url) {
|
|
5393
|
-
|
|
5394
|
-
if (isMultiUrl(url)) {
|
|
5395
|
-
return splitUrls(url).every((u) => githubRe.test(u));
|
|
5396
|
-
}
|
|
5397
|
-
return githubRe.test(url);
|
|
5310
|
+
return /github\.com|raw\.githubusercontent\.com/i.test(url);
|
|
5398
5311
|
}
|
|
5399
5312
|
function getDefaultUpdateInterval(url) {
|
|
5400
5313
|
return isGithubUrl(url) ? DEFAULT_UPDATE_INTERVAL_HOURS_GITHUB : DEFAULT_UPDATE_INTERVAL_HOURS;
|
|
@@ -5403,11 +5316,6 @@ function resolveUpdateInterval(url, cachedInterval) {
|
|
|
5403
5316
|
return cachedInterval && cachedInterval > 0 ? cachedInterval : getDefaultUpdateInterval(url);
|
|
5404
5317
|
}
|
|
5405
5318
|
var HTTP_CLIENT = createHttpClient({ timeout: 6e4 });
|
|
5406
|
-
function isMultiUrl(url) {
|
|
5407
|
-
if (!url.includes(",")) return false;
|
|
5408
|
-
const parts = url.split(",").map((u) => u.trim()).filter(Boolean);
|
|
5409
|
-
return parts.length > 1 && parts.every(isValidHttpUrl);
|
|
5410
|
-
}
|
|
5411
5319
|
function isValidHttpUrl(url) {
|
|
5412
5320
|
try {
|
|
5413
5321
|
const u = new URL(url.trim());
|
|
@@ -5416,28 +5324,6 @@ function isValidHttpUrl(url) {
|
|
|
5416
5324
|
return false;
|
|
5417
5325
|
}
|
|
5418
5326
|
}
|
|
5419
|
-
function splitUrls(url) {
|
|
5420
|
-
if (!isMultiUrl(url)) return [url.trim()];
|
|
5421
|
-
return url.split(",").map((u) => u.trim()).filter(Boolean);
|
|
5422
|
-
}
|
|
5423
|
-
function loadSubscriptionConfig(subName) {
|
|
5424
|
-
const rawContent = readSubscriptionRawConfig(subName);
|
|
5425
|
-
if (!rawContent) {
|
|
5426
|
-
throw new CliError(`\u672A\u627E\u5230\u8BA2\u9605\u914D\u7F6E "${subName}"\uFF0C\u8BF7\u5148\u66F4\u65B0\u8BA2\u9605\uFF08mihomo sub update ${subName}\uFF09`);
|
|
5427
|
-
}
|
|
5428
|
-
const raw = parseYamlOrJson(rawContent, "\u8BA2\u9605\u5185\u5BB9");
|
|
5429
|
-
return {
|
|
5430
|
-
raw,
|
|
5431
|
-
proxies: raw.proxies || [],
|
|
5432
|
-
proxyGroups: raw["proxy-groups"] || []
|
|
5433
|
-
};
|
|
5434
|
-
}
|
|
5435
|
-
function saveSubscriptionConfig(subName, parsed) {
|
|
5436
|
-
normalizeProxyNamesBeforeSave(parsed);
|
|
5437
|
-
parsed.raw.proxies = parsed.proxies;
|
|
5438
|
-
parsed.raw["proxy-groups"] = parsed.proxyGroups;
|
|
5439
|
-
saveSubscriptionRawConfig(subName, dumpYaml(parsed.raw));
|
|
5440
|
-
}
|
|
5441
5327
|
function parseUserInfo(header) {
|
|
5442
5328
|
if (!header) return null;
|
|
5443
5329
|
const info = {};
|
|
@@ -5601,65 +5487,6 @@ async function downloadSubscription(url, subName = "default", signal, persist =
|
|
|
5601
5487
|
username: meta.username
|
|
5602
5488
|
};
|
|
5603
5489
|
}
|
|
5604
|
-
async function downloadMergedSubscription(urls, subName, signal, persist = true) {
|
|
5605
|
-
const internal = new AbortController();
|
|
5606
|
-
const combinedSignal = signal ? AbortSignal.any([signal, internal.signal]) : internal.signal;
|
|
5607
|
-
const responses = await Promise.all(
|
|
5608
|
-
urls.map(async (url, index) => {
|
|
5609
|
-
try {
|
|
5610
|
-
const response = await HTTP_CLIENT.get(url, { responseType: "text", signal: combinedSignal });
|
|
5611
|
-
return { url, index, response, error: null };
|
|
5612
|
-
} catch (e) {
|
|
5613
|
-
internal.abort();
|
|
5614
|
-
return { url, index, response: null, error: e };
|
|
5615
|
-
}
|
|
5616
|
-
})
|
|
5617
|
-
);
|
|
5618
|
-
const failures = responses.filter((r) => r.error);
|
|
5619
|
-
if (failures.length > 0) {
|
|
5620
|
-
const isAbort = (e) => e.name === "AbortError" || /abort/i.test(e.message);
|
|
5621
|
-
const real = failures.find((r) => !isAbort(r.error)) ?? failures[0];
|
|
5622
|
-
const maskedUrl = maskUrl(real.url);
|
|
5623
|
-
throw new Error(`\u5408\u5E76\u8BA2\u9605\u7B2C ${real.index + 1} \u4E2A URL \u83B7\u53D6\u5931\u8D25: ${real.error.message}
|
|
5624
|
-
URL: ${maskedUrl}`);
|
|
5625
|
-
}
|
|
5626
|
-
const parsed = responses.map((r, i) => {
|
|
5627
|
-
const content = r.response?.data;
|
|
5628
|
-
if (!content?.trim()) throw new Error(`\u5408\u5E76\u8BA2\u9605\u7B2C ${i + 1} \u4E2A URL \u5185\u5BB9\u4E3A\u7A7A`);
|
|
5629
|
-
return parseYamlOrJson(content, `\u5408\u5E76\u8BA2\u9605\u7B2C ${i + 1} \u4E2A`);
|
|
5630
|
-
});
|
|
5631
|
-
const base = parsed[0];
|
|
5632
|
-
const baseProxies = base.proxies || [];
|
|
5633
|
-
const seenNames = new Set(baseProxies.map((p) => p.name));
|
|
5634
|
-
for (let i = 1; i < parsed.length; i++) {
|
|
5635
|
-
const extraProxies = parsed[i].proxies || [];
|
|
5636
|
-
for (const proxy of extraProxies) {
|
|
5637
|
-
if (!seenNames.has(proxy.name)) {
|
|
5638
|
-
baseProxies.push(proxy);
|
|
5639
|
-
seenNames.add(proxy.name);
|
|
5640
|
-
}
|
|
5641
|
-
}
|
|
5642
|
-
}
|
|
5643
|
-
base.proxies = baseProxies;
|
|
5644
|
-
const mergedContent = dumpYaml(base);
|
|
5645
|
-
assertLooksLikeSubscription(base, urls.map((u) => maskUrl(u)).join(", "));
|
|
5646
|
-
if (persist) {
|
|
5647
|
-
saveSubscriptionRawConfig(subName, mergedContent);
|
|
5648
|
-
}
|
|
5649
|
-
const meta = extractSubscriptionMeta(responses[0].response?.headers);
|
|
5650
|
-
if (persist) {
|
|
5651
|
-
saveSubscriptionMeta(subName, meta);
|
|
5652
|
-
}
|
|
5653
|
-
const proxyGroups = base["proxy-groups"];
|
|
5654
|
-
return {
|
|
5655
|
-
proxies: baseProxies.length,
|
|
5656
|
-
proxyGroups: proxyGroups ? proxyGroups.length : 0,
|
|
5657
|
-
userInfo: meta.userInfo,
|
|
5658
|
-
updateInterval: meta.updateInterval,
|
|
5659
|
-
webPageUrl: meta.webPageUrl,
|
|
5660
|
-
username: meta.username
|
|
5661
|
-
};
|
|
5662
|
-
}
|
|
5663
5490
|
function prepareConfigForStart(mode, subName = "default") {
|
|
5664
5491
|
const rawContent = readSubscriptionRawConfig(subName);
|
|
5665
5492
|
if (!rawContent) {
|
|
@@ -5693,12 +5520,7 @@ function needsAutoUpdate(sub) {
|
|
|
5693
5520
|
}
|
|
5694
5521
|
async function tryUpdateOne(sub, signal) {
|
|
5695
5522
|
try {
|
|
5696
|
-
|
|
5697
|
-
if (isMultiUrl(sub.url)) {
|
|
5698
|
-
info = await downloadMergedSubscription(splitUrls(sub.url), sub.name, signal);
|
|
5699
|
-
} else {
|
|
5700
|
-
info = await downloadSubscription(sub.url, sub.name, signal);
|
|
5701
|
-
}
|
|
5523
|
+
const info = await downloadSubscription(sub.url, sub.name, signal);
|
|
5702
5524
|
return { name: sub.name, success: true, proxies: info.proxies, proxyGroups: info.proxyGroups };
|
|
5703
5525
|
} catch (e) {
|
|
5704
5526
|
return { name: sub.name, success: false, error: e.message };
|
|
@@ -5751,187 +5573,6 @@ async function autoUpdateStaleSubscription(options = {}) {
|
|
|
5751
5573
|
}
|
|
5752
5574
|
return { total: staleSubs.length, updated: updatedCount, failed: staleSubs.length - updatedCount };
|
|
5753
5575
|
}
|
|
5754
|
-
async function testProxyDelay(proxyName, timeout, testUrl, client, apiBase = CONTROLLER_BASE_URL) {
|
|
5755
|
-
const encodedName = encodeURIComponent(proxyName);
|
|
5756
|
-
const url = `${apiBase}/proxies/${encodedName}/delay?timeout=${timeout}&url=${encodeURIComponent(testUrl)}`;
|
|
5757
|
-
try {
|
|
5758
|
-
const response = await client.get(url);
|
|
5759
|
-
const data = JSON.parse(response.data);
|
|
5760
|
-
if (data.delay && data.delay > 0) {
|
|
5761
|
-
return { name: proxyName, delay: data.delay };
|
|
5762
|
-
}
|
|
5763
|
-
return { name: proxyName, delay: null, error: data.message || "no delay" };
|
|
5764
|
-
} catch (e) {
|
|
5765
|
-
const err = e;
|
|
5766
|
-
let errorMsg = "timeout";
|
|
5767
|
-
if (err.response?.data?.message) {
|
|
5768
|
-
errorMsg = String(err.response.data.message);
|
|
5769
|
-
} else if (err.message) {
|
|
5770
|
-
errorMsg = err.message;
|
|
5771
|
-
}
|
|
5772
|
-
return { name: proxyName, delay: null, error: errorMsg };
|
|
5773
|
-
}
|
|
5774
|
-
}
|
|
5775
|
-
async function testSubscriptionProxies(subName, options = {}) {
|
|
5776
|
-
const {
|
|
5777
|
-
timeout = DEFAULT_TEST_TIMEOUT,
|
|
5778
|
-
concurrency = DEFAULT_TEST_CONCURRENCY,
|
|
5779
|
-
testUrl = DEFAULT_TEST_URL,
|
|
5780
|
-
apiBase = CONTROLLER_BASE_URL,
|
|
5781
|
-
onResult
|
|
5782
|
-
} = options;
|
|
5783
|
-
const { proxies } = options.parsed || loadSubscriptionConfig(subName);
|
|
5784
|
-
if (proxies.length === 0) {
|
|
5785
|
-
return { total: 0, alive: 0, dead: 0, results: [] };
|
|
5786
|
-
}
|
|
5787
|
-
const secret = apiBase === CONTROLLER_BASE_URL ? readSettings().controller_secret : void 0;
|
|
5788
|
-
const client = createHttpClient({ timeout: timeout + 3e3, secret });
|
|
5789
|
-
const results = new Array(proxies.length);
|
|
5790
|
-
let completedCount = 0;
|
|
5791
|
-
let nextIndex = 0;
|
|
5792
|
-
async function runNext() {
|
|
5793
|
-
while (nextIndex < proxies.length) {
|
|
5794
|
-
const idx = nextIndex++;
|
|
5795
|
-
const result = await testProxyDelay(proxies[idx].name, timeout, testUrl, client, apiBase);
|
|
5796
|
-
results[idx] = result;
|
|
5797
|
-
onResult?.(result, completedCount, proxies.length);
|
|
5798
|
-
completedCount++;
|
|
5799
|
-
}
|
|
5800
|
-
}
|
|
5801
|
-
const workers = Array.from({ length: Math.min(concurrency, proxies.length) }, () => runNext());
|
|
5802
|
-
await Promise.all(workers);
|
|
5803
|
-
const alive = results.filter((r) => r.delay !== null).length;
|
|
5804
|
-
return { total: results.length, alive, dead: results.length - alive, results };
|
|
5805
|
-
}
|
|
5806
|
-
function normalizeProxyNamesBeforeSave(parsed) {
|
|
5807
|
-
const { proxies, proxyGroups } = parsed;
|
|
5808
|
-
const renameMap = /* @__PURE__ */ new Map();
|
|
5809
|
-
const usedNames = /* @__PURE__ */ new Set();
|
|
5810
|
-
for (const proxy of proxies) {
|
|
5811
|
-
const shortened = proxy.name.replace(/_github\.com\/[^_]+/, "");
|
|
5812
|
-
if (shortened !== proxy.name && !usedNames.has(shortened)) {
|
|
5813
|
-
renameMap.set(proxy.name, shortened);
|
|
5814
|
-
usedNames.add(shortened);
|
|
5815
|
-
} else {
|
|
5816
|
-
usedNames.add(proxy.name);
|
|
5817
|
-
}
|
|
5818
|
-
}
|
|
5819
|
-
if (renameMap.size === 0) return 0;
|
|
5820
|
-
for (const proxy of proxies) {
|
|
5821
|
-
const newName = renameMap.get(proxy.name);
|
|
5822
|
-
if (newName) proxy.name = newName;
|
|
5823
|
-
}
|
|
5824
|
-
for (const group of proxyGroups) {
|
|
5825
|
-
if (Array.isArray(group.proxies)) {
|
|
5826
|
-
group.proxies = group.proxies.map((name) => renameMap.get(name) || name);
|
|
5827
|
-
}
|
|
5828
|
-
}
|
|
5829
|
-
const rules = parsed.raw.rules;
|
|
5830
|
-
if (Array.isArray(rules)) {
|
|
5831
|
-
parsed.raw.rules = rules.map((rule) => {
|
|
5832
|
-
if (typeof rule !== "string") return rule;
|
|
5833
|
-
const parts = rule.split(",");
|
|
5834
|
-
if (parts.length < 2) return rule;
|
|
5835
|
-
const targetIdx = parts[parts.length - 1].trim().toLowerCase() === "no-resolve" && parts.length >= 3 ? parts.length - 2 : parts.length - 1;
|
|
5836
|
-
const target = parts[targetIdx].trim();
|
|
5837
|
-
const renamed = renameMap.get(target);
|
|
5838
|
-
if (renamed) parts[targetIdx] = renamed;
|
|
5839
|
-
return parts.join(",");
|
|
5840
|
-
});
|
|
5841
|
-
}
|
|
5842
|
-
return renameMap.size;
|
|
5843
|
-
}
|
|
5844
|
-
function cleanDeadProxies(parsed, deadNames) {
|
|
5845
|
-
const { proxies, proxyGroups } = parsed;
|
|
5846
|
-
const originalCount = proxies.length;
|
|
5847
|
-
parsed.proxies = proxies.filter((p) => !deadNames.has(p.name));
|
|
5848
|
-
const removedProxies = originalCount - parsed.proxies.length;
|
|
5849
|
-
let updatedGroups = 0;
|
|
5850
|
-
const removedGroupNames = /* @__PURE__ */ new Set();
|
|
5851
|
-
for (const group of proxyGroups) {
|
|
5852
|
-
if (Array.isArray(group.proxies)) {
|
|
5853
|
-
const before = group.proxies.length;
|
|
5854
|
-
group.proxies = group.proxies.filter((name) => !deadNames.has(name));
|
|
5855
|
-
if (group.proxies.length < before) {
|
|
5856
|
-
updatedGroups++;
|
|
5857
|
-
}
|
|
5858
|
-
const hasOtherSource = group.use || group["include-all"] || group["include-all-proxies"];
|
|
5859
|
-
if (group.proxies.length === 0 && !hasOtherSource) {
|
|
5860
|
-
removedGroupNames.add(group.name);
|
|
5861
|
-
}
|
|
5862
|
-
}
|
|
5863
|
-
}
|
|
5864
|
-
if (removedGroupNames.size > 0) {
|
|
5865
|
-
parsed.proxyGroups = proxyGroups.filter((g) => !removedGroupNames.has(g.name));
|
|
5866
|
-
for (const group of parsed.proxyGroups) {
|
|
5867
|
-
if (Array.isArray(group.proxies)) {
|
|
5868
|
-
group.proxies = group.proxies.filter((name) => !removedGroupNames.has(name));
|
|
5869
|
-
}
|
|
5870
|
-
}
|
|
5871
|
-
}
|
|
5872
|
-
const removedTargets = /* @__PURE__ */ new Set([...removedGroupNames, ...deadNames]);
|
|
5873
|
-
if (removedTargets.size > 0) {
|
|
5874
|
-
const rules = parsed.raw.rules;
|
|
5875
|
-
if (Array.isArray(rules)) {
|
|
5876
|
-
parsed.raw.rules = rules.filter((rule) => {
|
|
5877
|
-
if (typeof rule !== "string") return true;
|
|
5878
|
-
return !removedTargets.has(getRuleTarget(rule));
|
|
5879
|
-
});
|
|
5880
|
-
}
|
|
5881
|
-
}
|
|
5882
|
-
return { removedProxies, updatedGroups, removedGroups: removedGroupNames.size };
|
|
5883
|
-
}
|
|
5884
|
-
async function autoCleanSubscription(subName, options = {}) {
|
|
5885
|
-
const parsed = loadSubscriptionConfig(subName);
|
|
5886
|
-
const { onResult, onRetryRound, rounds = DEFAULT_CLEAN_ROUNDS, ...testOptions } = options;
|
|
5887
|
-
const wrapOnResult = (round) => onResult ? (r, i, t) => onResult(r, i, t, round) : void 0;
|
|
5888
|
-
const summary = await testSubscriptionProxies(subName, {
|
|
5889
|
-
...testOptions,
|
|
5890
|
-
parsed,
|
|
5891
|
-
onResult: wrapOnResult(1)
|
|
5892
|
-
});
|
|
5893
|
-
let removedProxies = 0;
|
|
5894
|
-
let updatedGroups = 0;
|
|
5895
|
-
let removedGroups = 0;
|
|
5896
|
-
let skipped = false;
|
|
5897
|
-
if (summary.dead > 0) {
|
|
5898
|
-
if (summary.alive === 0 || summary.alive / summary.total < 0.01) {
|
|
5899
|
-
skipped = true;
|
|
5900
|
-
} else {
|
|
5901
|
-
const deadNames = new Set(summary.results.filter((r) => r.delay === null).map((r) => r.name));
|
|
5902
|
-
const deadProxies = parsed.proxies.filter((p) => deadNames.has(p.name));
|
|
5903
|
-
for (let retry = 0; retry < rounds - 1; retry++) {
|
|
5904
|
-
const round = retry + 2;
|
|
5905
|
-
const retryTargets = deadProxies.filter((p) => deadNames.has(p.name));
|
|
5906
|
-
if (retryTargets.length === 0) break;
|
|
5907
|
-
onRetryRound?.(round, retryTargets.length);
|
|
5908
|
-
const retryParsed = { raw: {}, proxies: retryTargets, proxyGroups: [] };
|
|
5909
|
-
const retrySummary = await testSubscriptionProxies(subName, {
|
|
5910
|
-
...testOptions,
|
|
5911
|
-
parsed: retryParsed,
|
|
5912
|
-
onResult: wrapOnResult(round)
|
|
5913
|
-
});
|
|
5914
|
-
for (const r of retrySummary.results) {
|
|
5915
|
-
if (r.delay !== null) {
|
|
5916
|
-
deadNames.delete(r.name);
|
|
5917
|
-
}
|
|
5918
|
-
}
|
|
5919
|
-
}
|
|
5920
|
-
summary.dead = deadNames.size;
|
|
5921
|
-
summary.alive = summary.total - summary.dead;
|
|
5922
|
-
if (deadNames.size > 0) {
|
|
5923
|
-
const cleanResult = cleanDeadProxies(parsed, deadNames);
|
|
5924
|
-
removedProxies = cleanResult.removedProxies;
|
|
5925
|
-
updatedGroups = cleanResult.updatedGroups;
|
|
5926
|
-
removedGroups = cleanResult.removedGroups;
|
|
5927
|
-
}
|
|
5928
|
-
}
|
|
5929
|
-
}
|
|
5930
|
-
if (!skipped && removedProxies > 0) {
|
|
5931
|
-
saveSubscriptionConfig(subName, parsed);
|
|
5932
|
-
}
|
|
5933
|
-
return { summary, removedProxies, updatedGroups, removedGroups, skipped };
|
|
5934
|
-
}
|
|
5935
5576
|
|
|
5936
5577
|
// src/commands/shared.ts
|
|
5937
5578
|
import readline from "readline";
|
|
@@ -5962,78 +5603,6 @@ async function launchOrRestart(mode) {
|
|
|
5962
5603
|
return result.pid;
|
|
5963
5604
|
}
|
|
5964
5605
|
|
|
5965
|
-
// src/progress.ts
|
|
5966
|
-
var IS_TTY = process.stdout.isTTY === true;
|
|
5967
|
-
var BAR_WIDTH = 20;
|
|
5968
|
-
function createProgressPrinter(totalRounds = 1) {
|
|
5969
|
-
let alive = 0;
|
|
5970
|
-
let dead = 0;
|
|
5971
|
-
const resultMap = /* @__PURE__ */ new Map();
|
|
5972
|
-
function render(done, total) {
|
|
5973
|
-
if (!IS_TTY) return;
|
|
5974
|
-
const pct = Math.round(done / total * 100);
|
|
5975
|
-
const filled = Math.round(done / total * BAR_WIDTH);
|
|
5976
|
-
const bar = "\u2588".repeat(filled) + "\u2591".repeat(BAR_WIDTH - filled);
|
|
5977
|
-
process.stdout.write(`\r${bar} ${done}/${total} (${pct}%) | ${colors.green(`\u2713${alive}`)} ${colors.red(`\u2717${dead}`)}`);
|
|
5978
|
-
}
|
|
5979
|
-
return {
|
|
5980
|
-
onResult(result, index, total, round = 1) {
|
|
5981
|
-
if (resultMap.size === 0 && totalRounds > 1) {
|
|
5982
|
-
console.log(`--- \u7B2C 1 \u8F6E\u6D4B\u8BD5 (${total} \u4E2A\u8282\u70B9) ---`);
|
|
5983
|
-
}
|
|
5984
|
-
const prev = resultMap.get(result.name);
|
|
5985
|
-
if (prev) {
|
|
5986
|
-
if (prev.result.delay !== null) alive--;
|
|
5987
|
-
else dead--;
|
|
5988
|
-
}
|
|
5989
|
-
if (result.delay !== null) alive++;
|
|
5990
|
-
else dead++;
|
|
5991
|
-
resultMap.set(result.name, { result, round });
|
|
5992
|
-
render(index + 1, total);
|
|
5993
|
-
},
|
|
5994
|
-
onRetryRound(round, count) {
|
|
5995
|
-
if (IS_TTY) {
|
|
5996
|
-
process.stdout.write("\n");
|
|
5997
|
-
}
|
|
5998
|
-
console.log(`--- \u7B2C ${round} \u8F6E\u91CD\u8BD5 (${count} \u4E2A\u8282\u70B9) ---`);
|
|
5999
|
-
alive = 0;
|
|
6000
|
-
dead = 0;
|
|
6001
|
-
},
|
|
6002
|
-
finish() {
|
|
6003
|
-
if (IS_TTY) {
|
|
6004
|
-
process.stdout.write("\n");
|
|
6005
|
-
}
|
|
6006
|
-
console.log("");
|
|
6007
|
-
if (!IS_TTY) return;
|
|
6008
|
-
const entries = [...resultMap.values()];
|
|
6009
|
-
entries.sort((a, b) => a.result.name.localeCompare(b.result.name));
|
|
6010
|
-
const total = entries.length;
|
|
6011
|
-
console.log("\u8282\u70B9\u6700\u7EC8\u72B6\u6001:");
|
|
6012
|
-
for (let i = 0; i < entries.length; i++) {
|
|
6013
|
-
const { result, round } = entries[i];
|
|
6014
|
-
const prefix = `[${i + 1}/${total}]`;
|
|
6015
|
-
if (result.delay !== null) {
|
|
6016
|
-
const delayColor = result.delay < 300 ? colors.green : result.delay < 800 ? colors.yellow : colors.red;
|
|
6017
|
-
const retryNote = round > 1 ? colors.gray(` (\u7B2C${round}\u8F6E\u901A\u8FC7)`) : "";
|
|
6018
|
-
console.log(`${prefix} ${colors.green("\u2713")} ${result.name} ${delayColor(`${result.delay}ms`)}${retryNote}`);
|
|
6019
|
-
} else {
|
|
6020
|
-
console.log(`${prefix} ${colors.red("\u2717")} ${result.name} ${colors.gray(result.error || "timeout")}`);
|
|
6021
|
-
}
|
|
6022
|
-
}
|
|
6023
|
-
console.log("");
|
|
6024
|
-
}
|
|
6025
|
-
};
|
|
6026
|
-
}
|
|
6027
|
-
function formatCleanSummary(result) {
|
|
6028
|
-
const parts = [`\u79FB\u9664 ${result.removedProxies} \u4E2A\u8282\u70B9`];
|
|
6029
|
-
if (result.removedGroups > 0) parts.push(`\u5220\u9664 ${result.removedGroups} \u4E2A\u7A7A\u5206\u7EC4`);
|
|
6030
|
-
if (result.updatedGroups > 0) parts.push(`\u66F4\u65B0 ${result.updatedGroups} \u4E2A\u5206\u7EC4`);
|
|
6031
|
-
return parts.join(", ");
|
|
6032
|
-
}
|
|
6033
|
-
function formatTestSummary(summary) {
|
|
6034
|
-
return `\u7ED3\u679C: ${colors.green(`${summary.alive} \u5B58\u6D3B`)} / ${colors.red(`${summary.dead} \u5931\u8D25`)} / ${summary.total} \u603B\u8BA1`;
|
|
6035
|
-
}
|
|
6036
|
-
|
|
6037
5606
|
// src/ssh.ts
|
|
6038
5607
|
import { spawn as spawn2 } from "child_process";
|
|
6039
5608
|
import fs8 from "fs";
|
|
@@ -6049,10 +5618,6 @@ function getReservedPorts() {
|
|
|
6049
5618
|
const mixedPort = Number(BASE_CONFIG["mixed-port"]);
|
|
6050
5619
|
if (Number.isInteger(mixedPort)) reserved.set(mixedPort, "mihomo \u6DF7\u5408\u4EE3\u7406\u7AEF\u53E3");
|
|
6051
5620
|
reserved.set(CONTROLLER_PORT, "mihomo \u63A7\u5236\u5668\u7AEF\u53E3");
|
|
6052
|
-
const testPort = Number(TEST_CONFIG["mixed-port"]);
|
|
6053
|
-
if (Number.isInteger(testPort)) reserved.set(testPort, "\u6D4B\u901F\u5B9E\u4F8B\u4EE3\u7406\u7AEF\u53E3");
|
|
6054
|
-
const testController = Number(TEST_CONTROLLER_ADDR.split(":")[1]);
|
|
6055
|
-
if (Number.isInteger(testController)) reserved.set(testController, "\u6D4B\u901F\u5B9E\u4F8B\u63A7\u5236\u5668\u7AEF\u53E3");
|
|
6056
5621
|
return reserved;
|
|
6057
5622
|
}
|
|
6058
5623
|
var SAFE_HOST_RE = /^[A-Za-z0-9][A-Za-z0-9._@-]*$/;
|
|
@@ -6501,7 +6066,35 @@ async function startAutoSshTunnelsWithWarning() {
|
|
|
6501
6066
|
console.log("");
|
|
6502
6067
|
}
|
|
6503
6068
|
}
|
|
6069
|
+
var REMOVED_OPTIONS = {
|
|
6070
|
+
"-r": "--rounds",
|
|
6071
|
+
"--rounds": "--rounds",
|
|
6072
|
+
"-t": "--timeout",
|
|
6073
|
+
"--timeout": "--timeout",
|
|
6074
|
+
"-j": "--concurrency",
|
|
6075
|
+
"--concurrency": "--concurrency",
|
|
6076
|
+
"--no-clean": "--no-clean"
|
|
6077
|
+
};
|
|
6078
|
+
function assertNoRemovedOptions(args) {
|
|
6079
|
+
const hit = args.find((a) => {
|
|
6080
|
+
const name = a.startsWith("--") && a.includes("=") ? a.slice(0, a.indexOf("=")) : a;
|
|
6081
|
+
return Object.hasOwn(REMOVED_OPTIONS, name);
|
|
6082
|
+
});
|
|
6083
|
+
if (!hit) return;
|
|
6084
|
+
throw new CliError(`\u9009\u9879 ${hit} \u5DF2\u79FB\u9664\uFF08v3.10.0\uFF0C\u968F\u8282\u70B9\u6D4B\u901F\u6E05\u7406\u4E00\u5E76\u5220\u9664\uFF09`, {
|
|
6085
|
+
label: "\u53C2\u6570\u9519\u8BEF",
|
|
6086
|
+
hint: [
|
|
6087
|
+
"start \u4E0D\u518D\u5728\u542F\u52A8\u540E\u81EA\u52A8\u6D4B\u901F\u6E05\u7406\u8282\u70B9\uFF0C\u6545\u8D85\u65F6/\u5E76\u53D1/\u8F6E\u6B21\u9009\u9879\u5747\u5DF2\u5931\u6548\u3002",
|
|
6088
|
+
"",
|
|
6089
|
+
"\u8282\u70B9\u6D4B\u901F\u8BF7\u7528 Web \u9762\u677F: mihomo ui",
|
|
6090
|
+
"\u81EA\u52A8\u9009\u8DEF\u8BF7\u5728\u8BA2\u9605\u91CC\u914D\u7F6E url-test \u5206\u7EC4\uFF0C\u7531\u5185\u6838\u6301\u7EED\u6D4B\u901F\u3002",
|
|
6091
|
+
"",
|
|
6092
|
+
"start \u73B0\u6709\u9009\u9879: -s/--no-update, -u/--update-timeout <ms>, --no-ssh"
|
|
6093
|
+
]
|
|
6094
|
+
});
|
|
6095
|
+
}
|
|
6504
6096
|
async function cmdStart(args) {
|
|
6097
|
+
assertNoRemovedOptions(args);
|
|
6505
6098
|
const modeToken = args[1] && !args[1].startsWith("-") ? args[1].toLowerCase() : void 0;
|
|
6506
6099
|
if (modeToken !== void 0 && modeToken !== "tun" && modeToken !== "mixed") {
|
|
6507
6100
|
throw new CliError(`\u672A\u77E5\u7684\u542F\u52A8\u6A21\u5F0F: ${args[1]}`, { hint: "\u7528\u6CD5: mihomo start [tun|mixed]\uFF08\u9ED8\u8BA4 mixed\uFF09" });
|
|
@@ -6514,11 +6107,7 @@ async function cmdStart(args) {
|
|
|
6514
6107
|
if (targetMode === "tun" && daemonEnabled) {
|
|
6515
6108
|
throw new CliError("\u4FDD\u6D3B\u5DF2\u542F\u7528\uFF08\u4EC5\u652F\u6301 Mixed \u6A21\u5F0F\uFF09\uFF0C\u65E0\u6CD5\u542F\u52A8 TUN", { hint: "\u8BF7\u5148\u5173\u95ED\u4FDD\u6D3B: mihomo daemon off" });
|
|
6516
6109
|
}
|
|
6517
|
-
const rounds = parseIntArg(args, "-r", "--rounds", DEFAULT_CLEAN_ROUNDS);
|
|
6518
|
-
const timeout = parseIntArg(args, "-t", "--timeout", DEFAULT_TEST_TIMEOUT);
|
|
6519
|
-
const concurrency = parseIntArg(args, "-j", "--concurrency", DEFAULT_TEST_CONCURRENCY);
|
|
6520
6110
|
const skipUpdate = hasFlag(args, "-s", "--no-update");
|
|
6521
|
-
const skipClean = hasFlag(args, "--no-clean");
|
|
6522
6111
|
const skipSsh = hasFlag(args, "--no-ssh");
|
|
6523
6112
|
const updateTimeout = parseIntArg(args, "-u", "--update-timeout", DEFAULT_AUTO_UPDATE_TIMEOUT);
|
|
6524
6113
|
const sub = requireActiveSubscription("\u6CA1\u6709\u8BA2\u9605\uFF0C\u8BF7\u5148\u6DFB\u52A0\u8BA2\u9605");
|
|
@@ -6564,45 +6153,6 @@ async function cmdStart(args) {
|
|
|
6564
6153
|
if (!skipSsh) {
|
|
6565
6154
|
await startAutoSshTunnelsWithWarning();
|
|
6566
6155
|
}
|
|
6567
|
-
const cleanThreshold = isGithubUrl(sub.url) ? AUTO_CLEAN_THRESHOLD_GITHUB : AUTO_CLEAN_THRESHOLD;
|
|
6568
|
-
if (!skipClean && configInfo.proxies > cleanThreshold) {
|
|
6569
|
-
const cache = readSubscriptionCache();
|
|
6570
|
-
const lastCleanAt = cache[sub.name]?.last_auto_clean_at;
|
|
6571
|
-
const withinCooldown = !!lastCleanAt && Date.now() - new Date(lastCleanAt).getTime() < AUTO_CLEAN_COOLDOWN_HOURS * 60 * 60 * 1e3;
|
|
6572
|
-
if (!withinCooldown) {
|
|
6573
|
-
console.log("");
|
|
6574
|
-
console.log(`\u8282\u70B9\u6570 ${configInfo.proxies} \u8D85\u8FC7 ${cleanThreshold}\uFF0C\u81EA\u52A8\u6E05\u7406\uFF08${AUTO_CLEAN_COOLDOWN_HOURS}h \u5185\u4EC5\u4E00\u6B21\uFF0C--no-clean \u8DF3\u8FC7\uFF09...`);
|
|
6575
|
-
console.log("");
|
|
6576
|
-
await sleep(1e3);
|
|
6577
|
-
const progress = createProgressPrinter(rounds);
|
|
6578
|
-
const cleanResult = await autoCleanSubscription(sub.name, {
|
|
6579
|
-
timeout,
|
|
6580
|
-
concurrency,
|
|
6581
|
-
rounds,
|
|
6582
|
-
onResult: progress.onResult,
|
|
6583
|
-
onRetryRound: progress.onRetryRound
|
|
6584
|
-
});
|
|
6585
|
-
progress.finish();
|
|
6586
|
-
console.log(formatTestSummary(cleanResult.summary));
|
|
6587
|
-
if (cleanResult.skipped) {
|
|
6588
|
-
console.log(colors.yellow("\u5B58\u6D3B\u8282\u70B9\u4E0D\u8DB3 1%\uFF0C\u8DF3\u8FC7\u6E05\u7406\u3002\u8BF7\u68C0\u67E5\u539F\u59CB\u8BA2\u9605\u662F\u5426\u6709\u6548"));
|
|
6589
|
-
} else if (cleanResult.removedProxies > 0) {
|
|
6590
|
-
console.log(`${colors.green("\u5DF2\u6E05\u7406")}: ${formatCleanSummary(cleanResult)}`);
|
|
6591
|
-
console.log("");
|
|
6592
|
-
console.log("\u91CD\u65B0\u52A0\u8F7D\u914D\u7F6E...");
|
|
6593
|
-
if (!daemonEnabled) handleStopResult(stop());
|
|
6594
|
-
try {
|
|
6595
|
-
configInfo = prepareConfigForStart(targetMode, sub.name);
|
|
6596
|
-
const pid = await launchOrRestart(targetMode);
|
|
6597
|
-
console.log(`${colors.green("\u5DF2\u91CD\u542F")}${pid ? ` (PID ${pid})` : ""} \xB7 ${formatProxySummary(configInfo)}`);
|
|
6598
|
-
} catch (e) {
|
|
6599
|
-
if (e instanceof CliError) throw e;
|
|
6600
|
-
throw new CliError(e.message.split("\n")[0], { label: "\u91CD\u542F\u5931\u8D25" });
|
|
6601
|
-
}
|
|
6602
|
-
}
|
|
6603
|
-
saveSubscriptionCache(sub.name, { last_auto_clean_at: (/* @__PURE__ */ new Date()).toISOString() });
|
|
6604
|
-
}
|
|
6605
|
-
}
|
|
6606
6156
|
await printStatus();
|
|
6607
6157
|
}
|
|
6608
6158
|
|
|
@@ -6627,13 +6177,6 @@ async function confirmPrompt(question) {
|
|
|
6627
6177
|
});
|
|
6628
6178
|
return answer.toLowerCase() === "y" || answer.toLowerCase() === "yes";
|
|
6629
6179
|
}
|
|
6630
|
-
function requireRunning() {
|
|
6631
|
-
const state = getRunningState();
|
|
6632
|
-
if (!state.running) {
|
|
6633
|
-
const hint = state.daemon ? "mihomo daemon on" : "mihomo start";
|
|
6634
|
-
throw new CliError(`mihomo \u672A\u8FD0\u884C\uFF0C\u8BF7\u5148\u542F\u52A8 (${hint})`);
|
|
6635
|
-
}
|
|
6636
|
-
}
|
|
6637
6180
|
async function restartToApply(args) {
|
|
6638
6181
|
if (!isRestartNeededOnChange()) return false;
|
|
6639
6182
|
const currentMode = getRuntimeMode();
|
|
@@ -6759,32 +6302,18 @@ function openDirectory(args) {
|
|
|
6759
6302
|
throw new CliError(`\u672A\u77E5\u7684\u76EE\u5F55\u76EE\u6807 "${target}"`, { hint });
|
|
6760
6303
|
}
|
|
6761
6304
|
function printDirectoryInfo() {
|
|
6305
|
+
const entries = Object.entries(DIRECTORY_TARGETS).filter(([key]) => key !== "root");
|
|
6306
|
+
const pathWidth = Math.max(...entries.map(([, val]) => val.path.replace(USER_DATA_DIR, ".").length));
|
|
6762
6307
|
console.log("");
|
|
6763
|
-
console.log(
|
|
6764
|
-
console.log(` \u6839\u76EE\u5F55: ${USER_DATA_DIR}`);
|
|
6765
|
-
console.log(` \u5168\u5C40\u8BBE\u7F6E: ${PATHS.settingsFile}`);
|
|
6766
|
-
console.log(` \u5185\u6838\u76EE\u5F55: ${DIRS.kernel}`);
|
|
6767
|
-
console.log(` \u5185\u6838\u6587\u4EF6: ${PATHS.mihomoBinary}`);
|
|
6768
|
-
console.log(` \u8BA2\u9605\u76EE\u5F55: ${DIRS.subscriptions}`);
|
|
6769
|
-
console.log(" - cache.json (\u8BA2\u9605\u7F13\u5B58\uFF1A\u66F4\u65B0\u65F6\u95F4\u3001\u6D41\u91CF\u7B49)");
|
|
6770
|
-
console.log(" - xxx.yaml (\u8BA2\u9605\u539F\u59CB\u914D\u7F6E)");
|
|
6771
|
-
console.log(` \u8FD0\u884C\u65F6\u76EE\u5F55: ${DIRS.runtime}`);
|
|
6772
|
-
console.log(" - config.yaml (\u542F\u52A8\u65F6\u751F\u6210\uFF0Cstop \u81EA\u52A8\u6E05\u9664)");
|
|
6773
|
-
console.log(" - pid (PID \u6587\u4EF6\uFF0Cstop \u81EA\u52A8\u6E05\u9664)");
|
|
6774
|
-
console.log(` \u65E5\u5FD7\u6587\u4EF6: ${PATHS.logFile}`);
|
|
6775
|
-
console.log(` mihomo \u6570\u636E: ${DIRS.data}`);
|
|
6776
|
-
console.log(" - cache.db, Geo*.dat \u7B49 (mihomo \u81EA\u884C\u7BA1\u7406)");
|
|
6308
|
+
console.log(`\u6570\u636E\u76EE\u5F55: ${USER_DATA_DIR}`);
|
|
6777
6309
|
console.log("");
|
|
6778
|
-
|
|
6779
|
-
|
|
6780
|
-
|
|
6781
|
-
|
|
6782
|
-
console.log(" mihomo dir open data \u6253\u5F00 mihomo \u6570\u636E\u76EE\u5F55");
|
|
6783
|
-
console.log(" mihomo dir open runtime \u6253\u5F00\u8FD0\u884C\u65F6\u76EE\u5F55");
|
|
6784
|
-
console.log(" mihomo dir open kernel \u6253\u5F00\u5185\u6838\u76EE\u5F55");
|
|
6310
|
+
for (const [key, val] of entries) {
|
|
6311
|
+
const rel = val.path.replace(USER_DATA_DIR, ".");
|
|
6312
|
+
console.log(` ${key.padEnd(9)}${rel.padEnd(pathWidth + 2)}${colors.gray(val.label)}`);
|
|
6313
|
+
}
|
|
6785
6314
|
console.log("");
|
|
6786
|
-
console.log(
|
|
6787
|
-
console.log("
|
|
6315
|
+
console.log(`\u6253\u5F00\u76EE\u5F55: mihomo dir open [${Object.keys(DIRECTORY_TARGETS).join("|")}]`);
|
|
6316
|
+
console.log(colors.gray(" \u81EA\u5B9A\u4E49\u6839\u76EE\u5F55: \u73AF\u5883\u53D8\u91CF MIHOMO_CLI_DIR"));
|
|
6788
6317
|
console.log("");
|
|
6789
6318
|
}
|
|
6790
6319
|
var SUBCOMMANDS2 = [
|
|
@@ -6814,7 +6343,7 @@ import { spawnSync as spawnSync4 } from "child_process";
|
|
|
6814
6343
|
import fs9 from "fs";
|
|
6815
6344
|
import path8 from "path";
|
|
6816
6345
|
|
|
6817
|
-
//
|
|
6346
|
+
// node_modules/compare-versions/lib/esm/utils.js
|
|
6818
6347
|
var semver = /^[v^~<>=]*?(\d+)(?:\.([x*]|\d+)(?:\.([x*]|\d+)(?:\.([x*]|\d+))?(?:-([\da-z\-]+(?:\.[\da-z\-]+)*))?(?:\+[\da-z\-]+(?:\.[\da-z\-]+)*)?)?)?$/i;
|
|
6819
6348
|
var validateAndParse = (version) => {
|
|
6820
6349
|
if (typeof version !== "string") {
|
|
@@ -6852,7 +6381,7 @@ var compareSegments = (a, b) => {
|
|
|
6852
6381
|
return 0;
|
|
6853
6382
|
};
|
|
6854
6383
|
|
|
6855
|
-
//
|
|
6384
|
+
// node_modules/compare-versions/lib/esm/compareVersions.js
|
|
6856
6385
|
var compareVersions = (v1, v2) => {
|
|
6857
6386
|
const n1 = validateAndParse(v1);
|
|
6858
6387
|
const n2 = validateAndParse(v2);
|
|
@@ -6875,7 +6404,7 @@ var KERNEL_HTTP_TIMEOUT = 12e4;
|
|
|
6875
6404
|
var KERNEL_DOWNLOAD_TIMEOUT = 18e4;
|
|
6876
6405
|
var HTTP_CLIENT2 = createHttpClient({ timeout: KERNEL_HTTP_TIMEOUT });
|
|
6877
6406
|
function withMirror(url, mirror) {
|
|
6878
|
-
if (mirror &&
|
|
6407
|
+
if (mirror && url.startsWith("https://github.com/")) {
|
|
6879
6408
|
return mirror + url;
|
|
6880
6409
|
}
|
|
6881
6410
|
return url;
|
|
@@ -6918,8 +6447,8 @@ function findMatchingAsset(assets, platform, arch) {
|
|
|
6918
6447
|
});
|
|
6919
6448
|
return standardAsset || matchingAssets[0];
|
|
6920
6449
|
}
|
|
6921
|
-
async function getLatestRelease(repo
|
|
6922
|
-
const url =
|
|
6450
|
+
async function getLatestRelease(repo) {
|
|
6451
|
+
const url = `https://api.github.com/repos/${repo}/releases`;
|
|
6923
6452
|
const response = await HTTP_CLIENT2.get(url, { responseType: "json" });
|
|
6924
6453
|
const releases = response.data;
|
|
6925
6454
|
if (!Array.isArray(releases) || releases.length === 0) {
|
|
@@ -6930,9 +6459,9 @@ async function getLatestRelease(repo, mirror) {
|
|
|
6930
6459
|
);
|
|
6931
6460
|
return stableReleases.length > 0 ? stableReleases[0] : releases[0];
|
|
6932
6461
|
}
|
|
6933
|
-
async function checkUpdate(
|
|
6462
|
+
async function checkUpdate() {
|
|
6934
6463
|
const currentVersion = getKernelVersion();
|
|
6935
|
-
const latest = await getLatestRelease(GITHUB_REPO
|
|
6464
|
+
const latest = await getLatestRelease(GITHUB_REPO);
|
|
6936
6465
|
const latestVersion = latest.tag_name;
|
|
6937
6466
|
let needsUpdate = false;
|
|
6938
6467
|
const currentDisplay = currentVersion || "\u672A\u5B89\u88C5";
|
|
@@ -6972,7 +6501,7 @@ function findBinaryInDir(dir, maxDepth = 4) {
|
|
|
6972
6501
|
}
|
|
6973
6502
|
async function downloadKernel(progressCallback, mirror, releaseInfo) {
|
|
6974
6503
|
ensureDirs();
|
|
6975
|
-
const latest = releaseInfo || await getLatestRelease(GITHUB_REPO
|
|
6504
|
+
const latest = releaseInfo || await getLatestRelease(GITHUB_REPO);
|
|
6976
6505
|
const arch = getArch();
|
|
6977
6506
|
const platform = process.platform;
|
|
6978
6507
|
const asset = findMatchingAsset(latest.assets, platform, arch);
|
|
@@ -7142,14 +6671,12 @@ async function cmdKernel(args) {
|
|
|
7142
6671
|
const mirrorInfo = parseMirrorArg(args);
|
|
7143
6672
|
const effectiveMirror = mirrorInfo.mirror;
|
|
7144
6673
|
if (effectiveMirror) {
|
|
7145
|
-
|
|
7146
|
-
console.log(`\u955C\u50CF: ${effectiveMirror}${mirrorDesc}`);
|
|
6674
|
+
console.log(`\u955C\u50CF: ${effectiveMirror} (\u4EC5\u4E0B\u8F7D\u8D70\u955C\u50CF\uFF0C\u7248\u672C\u67E5\u8BE2\u6052\u76F4\u8FDE)`);
|
|
7147
6675
|
console.log("");
|
|
7148
6676
|
}
|
|
7149
6677
|
console.log("\u68C0\u67E5\u5185\u6838\u66F4\u65B0...");
|
|
7150
6678
|
try {
|
|
7151
|
-
const
|
|
7152
|
-
const info = await checkUpdate(apiMirror);
|
|
6679
|
+
const info = await checkUpdate();
|
|
7153
6680
|
console.log(`\u5F53\u524D: ${info.current}`);
|
|
7154
6681
|
console.log(`\u6700\u65B0: ${info.latest}`);
|
|
7155
6682
|
if (!info.needsUpdate) {
|
|
@@ -7177,8 +6704,7 @@ async function cmdKernel(args) {
|
|
|
7177
6704
|
hint.push(
|
|
7178
6705
|
"",
|
|
7179
6706
|
"\u63D0\u793A: \u76F4\u8FDE\u5931\u8D25\u6216\u4E0B\u8F7D\u8FC7\u6162\u65F6\u53EF\u4F7F\u7528\u955C\u50CF:",
|
|
7180
|
-
" mihomo kernel --mirror [\u955C\u50CF]
|
|
7181
|
-
" mihomo kernel --mirror-all [\u955C\u50CF] # API \u548C\u4E0B\u8F7D\u90FD\u8D70\u955C\u50CF",
|
|
6707
|
+
" mihomo kernel --mirror [\u955C\u50CF] # \u4E0B\u8F7D\u8D70\u955C\u50CF\uFF08\u9ED8\u8BA4 v6.gh-proxy.org\uFF09",
|
|
7182
6708
|
` \u53EF\u7528\u955C\u50CF: ${AVAILABLE_MIRRORS.join(", ")}`
|
|
7183
6709
|
);
|
|
7184
6710
|
}
|
|
@@ -7205,19 +6731,14 @@ function cmdLogs(args) {
|
|
|
7205
6731
|
logPath = getLogPath();
|
|
7206
6732
|
} else {
|
|
7207
6733
|
const parsedIdx = parseInt(targetName, 10);
|
|
7208
|
-
if (
|
|
7209
|
-
|
|
7210
|
-
const archive = archiveLogs.archives[parsedIdx - 1];
|
|
7211
|
-
if (!archive) {
|
|
7212
|
-
throw new CliError(`\u672A\u627E\u5230\u65E5\u5FD7 "${targetName}"`, { hint: '\u4F7F\u7528 "mihomo logs" \u67E5\u770B\u53EF\u7528\u65E5\u5FD7\u5217\u8868' });
|
|
7213
|
-
}
|
|
7214
|
-
logPath = archive.path;
|
|
7215
|
-
} else {
|
|
7216
|
-
logPath = getLogPathByName(targetName);
|
|
6734
|
+
if (Number.isNaN(parsedIdx) || parsedIdx < 1 || String(parsedIdx) !== targetName) {
|
|
6735
|
+
throw new CliError(`\u65E0\u6548\u7684\u65E5\u5FD7\u7F16\u53F7 "${targetName}"`, { hint: "\u7528\u6CD5: mihomo logs <\u7F16\u53F7>\uFF080=\u5F53\u524D\uFF0C1+=\u5F52\u6863\uFF09\uFF1B\u67E5\u770B\u5217\u8868: mihomo logs" });
|
|
7217
6736
|
}
|
|
7218
|
-
|
|
7219
|
-
|
|
7220
|
-
|
|
6737
|
+
const archive = listLogs().archives[parsedIdx - 1];
|
|
6738
|
+
if (!archive) {
|
|
6739
|
+
throw new CliError(`\u672A\u627E\u5230\u65E5\u5FD7 "${targetName}"`, { hint: '\u4F7F\u7528 "mihomo logs" \u67E5\u770B\u53EF\u7528\u65E5\u5FD7\u5217\u8868' });
|
|
6740
|
+
}
|
|
6741
|
+
logPath = archive.path;
|
|
7221
6742
|
}
|
|
7222
6743
|
if (openInViewer) {
|
|
7223
6744
|
openLogFile(logPath);
|
|
@@ -7780,181 +7301,7 @@ async function cmdSsh(args) {
|
|
|
7780
7301
|
});
|
|
7781
7302
|
}
|
|
7782
7303
|
|
|
7783
|
-
// src/test-instance.ts
|
|
7784
|
-
import { spawn as spawn3 } from "child_process";
|
|
7785
|
-
import fs11 from "fs";
|
|
7786
|
-
import path10 from "path";
|
|
7787
|
-
function isProxyValid(proxy) {
|
|
7788
|
-
if (proxy === null || typeof proxy !== "object") return false;
|
|
7789
|
-
if (!proxy.name || !proxy.server || !proxy.port) return false;
|
|
7790
|
-
if (!proxy.type) return false;
|
|
7791
|
-
if (proxy.type === "ss" && typeof proxy.cipher === "string" && proxy.cipher.startsWith("2022-blake3")) {
|
|
7792
|
-
const pw = String(proxy.password || "");
|
|
7793
|
-
if (!/^[A-Za-z0-9+/\-_]+=*$/.test(pw) || pw.length < 20) return false;
|
|
7794
|
-
}
|
|
7795
|
-
return true;
|
|
7796
|
-
}
|
|
7797
|
-
var TEST_DIR = path10.join(USER_DATA_DIR, "test");
|
|
7798
|
-
var TEST_DIRS = {
|
|
7799
|
-
data: path10.join(TEST_DIR, "data"),
|
|
7800
|
-
runtime: path10.join(TEST_DIR, "runtime")
|
|
7801
|
-
};
|
|
7802
|
-
var TEST_PATHS = {
|
|
7803
|
-
configFile: path10.join(TEST_DIRS.runtime, "config.yaml"),
|
|
7804
|
-
pidFile: path10.join(TEST_DIRS.runtime, "pid"),
|
|
7805
|
-
logFile: path10.join(TEST_DIR, "test.log")
|
|
7806
|
-
};
|
|
7807
|
-
var TEST_API = `http://${TEST_CONFIG["external-controller"]}`;
|
|
7808
|
-
function ensureTestDirs() {
|
|
7809
|
-
for (const dir of Object.values(TEST_DIRS)) {
|
|
7810
|
-
fs11.mkdirSync(dir, { recursive: true, mode: 448 });
|
|
7811
|
-
}
|
|
7812
|
-
}
|
|
7813
|
-
function cleanupTestDir() {
|
|
7814
|
-
rmrf(TEST_DIR);
|
|
7815
|
-
}
|
|
7816
|
-
function buildTestConfig(subName) {
|
|
7817
|
-
ensureTestDirs();
|
|
7818
|
-
const rawContent = readSubscriptionRawConfig(subName);
|
|
7819
|
-
if (!rawContent) {
|
|
7820
|
-
throw new CliError(`\u672A\u627E\u5230\u8BA2\u9605\u914D\u7F6E "${subName}"\uFF0C\u8BF7\u5148\u66F4\u65B0\u8BA2\u9605\uFF08mihomo sub update ${subName}\uFF09`);
|
|
7821
|
-
}
|
|
7822
|
-
const parsed = parseYamlOrJson(rawContent, "\u8BA2\u9605\u5185\u5BB9");
|
|
7823
|
-
const proxies = (parsed.proxies || []).filter(isProxyValid);
|
|
7824
|
-
if (proxies.length === 0) {
|
|
7825
|
-
throw new CliError(`\u8BA2\u9605 "${subName}" \u6CA1\u6709\u6709\u6548\u8282\u70B9`);
|
|
7826
|
-
}
|
|
7827
|
-
const nameCount = /* @__PURE__ */ new Map();
|
|
7828
|
-
for (const proxy of proxies) {
|
|
7829
|
-
const count = (nameCount.get(proxy.name) || 0) + 1;
|
|
7830
|
-
nameCount.set(proxy.name, count);
|
|
7831
|
-
if (count > 1) {
|
|
7832
|
-
proxy.name = `${proxy.name} #${count}`;
|
|
7833
|
-
}
|
|
7834
|
-
}
|
|
7835
|
-
const config = {
|
|
7836
|
-
...TEST_CONFIG,
|
|
7837
|
-
proxies,
|
|
7838
|
-
"proxy-groups": [
|
|
7839
|
-
{
|
|
7840
|
-
name: "PROXY",
|
|
7841
|
-
type: "select",
|
|
7842
|
-
proxies: proxies.map((p) => p.name)
|
|
7843
|
-
}
|
|
7844
|
-
],
|
|
7845
|
-
rules: ["MATCH,PROXY"]
|
|
7846
|
-
};
|
|
7847
|
-
const content = dumpYaml(config);
|
|
7848
|
-
fs11.writeFileSync(TEST_PATHS.configFile, content, { mode: 384 });
|
|
7849
|
-
}
|
|
7850
|
-
async function startTestInstance() {
|
|
7851
|
-
const binary = PATHS.mihomoBinary;
|
|
7852
|
-
if (!fs11.existsSync(binary)) throw new CliError('\u672A\u627E\u5230 mihomo \u5185\u6838\uFF0C\u8BF7\u5148\u8FD0\u884C "mihomo kernel" \u4E0B\u8F7D');
|
|
7853
|
-
stopTestInstance();
|
|
7854
|
-
const logFd = fs11.openSync(TEST_PATHS.logFile, "a");
|
|
7855
|
-
const child = spawn3(binary, ["-d", TEST_DIRS.data, "-f", TEST_PATHS.configFile], {
|
|
7856
|
-
detached: true,
|
|
7857
|
-
stdio: ["ignore", logFd, logFd]
|
|
7858
|
-
});
|
|
7859
|
-
child.on("error", () => {
|
|
7860
|
-
});
|
|
7861
|
-
fs11.closeSync(logFd);
|
|
7862
|
-
child.unref();
|
|
7863
|
-
const pid = child.pid;
|
|
7864
|
-
if (!pid) throw new CliError("\u6D4B\u8BD5\u5B9E\u4F8B\u542F\u52A8\u5931\u8D25\uFF1A\u65E0\u6CD5\u521B\u5EFA\u8FDB\u7A0B\uFF08\u5185\u6838\u4E8C\u8FDB\u5236\u53EF\u80FD\u4E0D\u53EF\u6267\u884C\uFF09");
|
|
7865
|
-
spawnedTestPid = pid;
|
|
7866
|
-
fs11.writeFileSync(TEST_PATHS.pidFile, pid.toString(), { mode: 384 });
|
|
7867
|
-
const client = createHttpClient({ timeout: 2e3 });
|
|
7868
|
-
let ready = false;
|
|
7869
|
-
for (let i = 0; i < 60; i++) {
|
|
7870
|
-
if (!isProcessRunning(pid)) break;
|
|
7871
|
-
try {
|
|
7872
|
-
await client.get(`${TEST_API}/version`);
|
|
7873
|
-
ready = true;
|
|
7874
|
-
break;
|
|
7875
|
-
} catch {
|
|
7876
|
-
await sleep(500);
|
|
7877
|
-
}
|
|
7878
|
-
}
|
|
7879
|
-
if (!isProcessRunning(pid)) {
|
|
7880
|
-
let errorDetail = "";
|
|
7881
|
-
try {
|
|
7882
|
-
errorDetail = fs11.readFileSync(TEST_PATHS.logFile, "utf8").slice(-1e3);
|
|
7883
|
-
} catch {
|
|
7884
|
-
}
|
|
7885
|
-
throw new CliError(`\u6D4B\u8BD5\u5B9E\u4F8B\u542F\u52A8\u5931\u8D25${errorDetail ? `
|
|
7886
|
-
${errorDetail}` : ""}`);
|
|
7887
|
-
}
|
|
7888
|
-
if (!ready) {
|
|
7889
|
-
throw new CliError("\u6D4B\u8BD5\u5B9E\u4F8B\u542F\u52A8\u8D85\u65F6\uFF0CAPI \u672A\u54CD\u5E94");
|
|
7890
|
-
}
|
|
7891
|
-
}
|
|
7892
|
-
var spawnedTestPid = null;
|
|
7893
|
-
function stopTestInstance() {
|
|
7894
|
-
let pid = null;
|
|
7895
|
-
try {
|
|
7896
|
-
const fromFile = parseInt(fs11.readFileSync(TEST_PATHS.pidFile, "utf8").trim(), 10);
|
|
7897
|
-
if (fromFile > 0) pid = fromFile;
|
|
7898
|
-
} catch {
|
|
7899
|
-
}
|
|
7900
|
-
if (pid === null) pid = spawnedTestPid;
|
|
7901
|
-
if (pid === null) return;
|
|
7902
|
-
if (pid > 0 && isProcessRunning(pid) && isProcessCommandMatching(pid, TEST_PATHS.configFile)) {
|
|
7903
|
-
process.kill(pid, "SIGKILL");
|
|
7904
|
-
for (let i = 0; i < 20; i++) {
|
|
7905
|
-
if (!isProcessRunning(pid)) break;
|
|
7906
|
-
sleepSync(100);
|
|
7907
|
-
}
|
|
7908
|
-
}
|
|
7909
|
-
spawnedTestPid = null;
|
|
7910
|
-
try {
|
|
7911
|
-
fs11.unlinkSync(TEST_PATHS.pidFile);
|
|
7912
|
-
} catch {
|
|
7913
|
-
}
|
|
7914
|
-
}
|
|
7915
|
-
async function withTestInstance(subName, fn) {
|
|
7916
|
-
cleanupTestDir();
|
|
7917
|
-
buildTestConfig(subName);
|
|
7918
|
-
const unregister = registerCleanup(() => {
|
|
7919
|
-
stopTestInstance();
|
|
7920
|
-
cleanupTestDir();
|
|
7921
|
-
});
|
|
7922
|
-
try {
|
|
7923
|
-
await startTestInstance();
|
|
7924
|
-
return await fn(TEST_API);
|
|
7925
|
-
} finally {
|
|
7926
|
-
unregister();
|
|
7927
|
-
stopTestInstance();
|
|
7928
|
-
cleanupTestDir();
|
|
7929
|
-
}
|
|
7930
|
-
}
|
|
7931
|
-
|
|
7932
7304
|
// src/commands/subscription.ts
|
|
7933
|
-
function githubRepoUrl(rawUrl) {
|
|
7934
|
-
const match = rawUrl.match(/raw\.githubusercontent\.com\/([^/]+\/[^/]+)/);
|
|
7935
|
-
if (match) return `https://github.com/${match[1]}`;
|
|
7936
|
-
return null;
|
|
7937
|
-
}
|
|
7938
|
-
function resolveTestTarget(args) {
|
|
7939
|
-
const subs = getSubscriptions();
|
|
7940
|
-
if (subs.length === 0) {
|
|
7941
|
-
throw new CliError("\u6CA1\u6709\u8BA2\u9605");
|
|
7942
|
-
}
|
|
7943
|
-
const nameArg = getNonFlagArg(args, 2);
|
|
7944
|
-
const timeout = parseIntArg(args, "-t", "--timeout", DEFAULT_TEST_TIMEOUT);
|
|
7945
|
-
const concurrency = parseIntArg(args, "-j", "--concurrency", DEFAULT_TEST_CONCURRENCY);
|
|
7946
|
-
let target;
|
|
7947
|
-
if (nameArg) {
|
|
7948
|
-
target = resolveSubscription(subs, nameArg);
|
|
7949
|
-
} else {
|
|
7950
|
-
const activeSub = getActiveSubscription();
|
|
7951
|
-
if (!activeSub) {
|
|
7952
|
-
throw new CliError("\u6CA1\u6709\u6D3B\u8DC3\u8BA2\u9605\uFF0C\u8BF7\u6307\u5B9A\u8BA2\u9605\u540D\u79F0");
|
|
7953
|
-
}
|
|
7954
|
-
target = activeSub;
|
|
7955
|
-
}
|
|
7956
|
-
return { target, timeout, concurrency };
|
|
7957
|
-
}
|
|
7958
7305
|
function printRestartHintIfRunning() {
|
|
7959
7306
|
if (getRunningState().running) {
|
|
7960
7307
|
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"));
|
|
@@ -7975,9 +7322,8 @@ function printSubscriptionList() {
|
|
|
7975
7322
|
subs.forEach((s, i) => {
|
|
7976
7323
|
const time = formatDate(s.updated_at);
|
|
7977
7324
|
const defaultMark = activeSub && s.name === activeSub.name ? colors.green(" [\u4F7F\u7528\u4E2D]") : "";
|
|
7978
|
-
const mergeBadge = isMultiUrl(s.url) ? colors.cyan(` [\u5408\u5E76 ${splitUrls(s.url).length} \u6E90]`) : "";
|
|
7979
7325
|
const interval = resolveUpdateInterval(s.url, s.update_interval);
|
|
7980
|
-
console.log(` ${i + 1}. ${s.name}${defaultMark}
|
|
7326
|
+
console.log(` ${i + 1}. ${s.name}${defaultMark}`);
|
|
7981
7327
|
console.log(` ${colors.gray("\u66F4\u65B0: ")}${time} (\u95F4\u9694: ${interval}h)`);
|
|
7982
7328
|
if (s.username) {
|
|
7983
7329
|
console.log(` ${colors.gray("\u7528\u6237: ")}${s.username}`);
|
|
@@ -8005,9 +7351,6 @@ function printSubscriptionList() {
|
|
|
8005
7351
|
console.log("\u65B0\u589E\u8BA2\u9605: mihomo sub add <url> [name]");
|
|
8006
7352
|
console.log("\u66F4\u65B0\u8BA2\u9605: mihomo sub update [name]");
|
|
8007
7353
|
console.log("\u5220\u9664\u8BA2\u9605: mihomo sub remove <name>");
|
|
8008
|
-
console.log("\u6D4B\u8BD5\u8282\u70B9: mihomo sub test [name]");
|
|
8009
|
-
console.log("\u6E05\u7406\u8282\u70B9: mihomo sub clean [name]");
|
|
8010
|
-
console.log("\u6253\u5F00\u9875\u9762: mihomo sub web [name]");
|
|
8011
7354
|
console.log("");
|
|
8012
7355
|
}
|
|
8013
7356
|
async function subAdd(args) {
|
|
@@ -8016,45 +7359,19 @@ async function subAdd(args) {
|
|
|
8016
7359
|
if (!url) {
|
|
8017
7360
|
throw new CliError("\u8BF7\u63D0\u4F9B\u6709\u6548\u7684\u8BA2\u9605 URL");
|
|
8018
7361
|
}
|
|
8019
|
-
if (
|
|
8020
|
-
|
|
8021
|
-
|
|
8022
|
-
|
|
8023
|
-
|
|
8024
|
-
|
|
8025
|
-
|
|
8026
|
-
|
|
8027
|
-
|
|
8028
|
-
|
|
8029
|
-
|
|
8030
|
-
|
|
8031
|
-
|
|
8032
|
-
try {
|
|
8033
|
-
const info = await downloadMergedSubscription(urls, name);
|
|
8034
|
-
setDefaultSubscription(name);
|
|
8035
|
-
console.log(`\u5DF2\u6DFB\u52A0\u5E76\u5207\u6362\u5230 "${name}" (${formatProxySummary(info)}, \u5408\u5E76 ${urls.length} \u6E90)`);
|
|
8036
|
-
} catch (e) {
|
|
8037
|
-
removeSubscription(name);
|
|
8038
|
-
if (e instanceof CliError) throw new CliError(e.message, { label: "\u6DFB\u52A0\u5931\u8D25", hint: e.hint });
|
|
8039
|
-
throw new CliError(e.message, { label: "\u6DFB\u52A0\u5931\u8D25" });
|
|
8040
|
-
}
|
|
8041
|
-
} else {
|
|
8042
|
-
if (!isValidHttpUrl(url)) {
|
|
8043
|
-
throw new CliError("\u8BF7\u63D0\u4F9B\u6709\u6548\u7684\u8BA2\u9605 URL\uFF08\u9700\u4EE5 http:// \u6216 https:// \u5F00\u5934\uFF09");
|
|
8044
|
-
}
|
|
8045
|
-
console.log(`\u6DFB\u52A0\u8BA2\u9605: ${name}`);
|
|
8046
|
-
addSubscription(url, name);
|
|
8047
|
-
try {
|
|
8048
|
-
const info = await downloadSubscription(url, name);
|
|
8049
|
-
setDefaultSubscription(name);
|
|
8050
|
-
const repoUrl = githubRepoUrl(url);
|
|
8051
|
-
if (repoUrl) saveSubscriptionCache(name, { web_page_url: repoUrl });
|
|
8052
|
-
console.log(`\u5DF2\u6DFB\u52A0\u5E76\u5207\u6362\u5230 "${name}" (${formatProxySummary(info)})`);
|
|
8053
|
-
} catch (e) {
|
|
8054
|
-
removeSubscription(name);
|
|
8055
|
-
if (e instanceof CliError) throw new CliError(e.message, { label: "\u6DFB\u52A0\u5931\u8D25", hint: e.hint });
|
|
8056
|
-
throw new CliError(e.message, { label: "\u6DFB\u52A0\u5931\u8D25" });
|
|
8057
|
-
}
|
|
7362
|
+
if (!isValidHttpUrl(url)) {
|
|
7363
|
+
throw new CliError("\u8BF7\u63D0\u4F9B\u6709\u6548\u7684\u8BA2\u9605 URL\uFF08\u9700\u4EE5 http:// \u6216 https:// \u5F00\u5934\uFF09");
|
|
7364
|
+
}
|
|
7365
|
+
console.log(`\u6DFB\u52A0\u8BA2\u9605: ${name}`);
|
|
7366
|
+
addSubscription(url, name);
|
|
7367
|
+
try {
|
|
7368
|
+
const info = await downloadSubscription(url, name);
|
|
7369
|
+
setDefaultSubscription(name);
|
|
7370
|
+
console.log(`\u5DF2\u6DFB\u52A0\u5E76\u5207\u6362\u5230 "${name}" (${formatProxySummary(info)})`);
|
|
7371
|
+
} catch (e) {
|
|
7372
|
+
removeSubscription(name);
|
|
7373
|
+
if (e instanceof CliError) throw new CliError(e.message, { label: "\u6DFB\u52A0\u5931\u8D25", hint: e.hint });
|
|
7374
|
+
throw new CliError(e.message, { label: "\u6DFB\u52A0\u5931\u8D25" });
|
|
8058
7375
|
}
|
|
8059
7376
|
console.log("");
|
|
8060
7377
|
printSubscriptionList();
|
|
@@ -8116,39 +7433,6 @@ async function subUse(args) {
|
|
|
8116
7433
|
console.log("");
|
|
8117
7434
|
printSubscriptionList();
|
|
8118
7435
|
}
|
|
8119
|
-
async function subWeb(args) {
|
|
8120
|
-
const name = args[2];
|
|
8121
|
-
const subs = getSubscriptionsWithCache();
|
|
8122
|
-
if (subs.length === 0) {
|
|
8123
|
-
throw new CliError("\u6CA1\u6709\u8BA2\u9605");
|
|
8124
|
-
}
|
|
8125
|
-
let target;
|
|
8126
|
-
if (name) {
|
|
8127
|
-
target = resolveSubscription(subs, name);
|
|
8128
|
-
} else {
|
|
8129
|
-
target = getActiveSubscription() || subs[0];
|
|
8130
|
-
}
|
|
8131
|
-
const cached = subs.find((s) => s.name === target.name);
|
|
8132
|
-
let webPageUrl = cached?.web_page_url;
|
|
8133
|
-
if (!webPageUrl) {
|
|
8134
|
-
console.log("\u8BA2\u9605\u4FE1\u606F\u4E2D\u7F3A\u5C11\u9875\u9762\u5730\u5740\uFF0C\u6B63\u5728\u67E5\u8BE2\u8BA2\u9605...");
|
|
8135
|
-
try {
|
|
8136
|
-
const info = isMultiUrl(target.url) ? await downloadMergedSubscription(splitUrls(target.url), target.name, void 0, false) : await downloadSubscription(target.url, target.name, void 0, false);
|
|
8137
|
-
if (!info.webPageUrl) {
|
|
8138
|
-
throw new CliError("\u8BE5\u8BA2\u9605\u6CA1\u6709\u63D0\u4F9B\u9875\u9762\u5730\u5740");
|
|
8139
|
-
}
|
|
8140
|
-
webPageUrl = info.webPageUrl;
|
|
8141
|
-
} catch (e) {
|
|
8142
|
-
if (e instanceof CliError) throw e;
|
|
8143
|
-
throw new CliError(e.message, { label: "\u67E5\u8BE2\u5931\u8D25" });
|
|
8144
|
-
}
|
|
8145
|
-
}
|
|
8146
|
-
console.log(`\u6253\u5F00\u8BA2\u9605\u9875\u9762: ${webPageUrl}`);
|
|
8147
|
-
const opened = openUrl(webPageUrl);
|
|
8148
|
-
if (!opened) {
|
|
8149
|
-
console.log("\u8BF7\u624B\u52A8\u8BBF\u95EE\u4E0A\u9762\u7684\u5730\u5740");
|
|
8150
|
-
}
|
|
8151
|
-
}
|
|
8152
7436
|
async function subRemove(args) {
|
|
8153
7437
|
const name = getNonFlagArg(args, 2);
|
|
8154
7438
|
const subs = getSubscriptions();
|
|
@@ -8181,143 +7465,32 @@ async function subRemove(args) {
|
|
|
8181
7465
|
console.log("");
|
|
8182
7466
|
printSubscriptionList();
|
|
8183
7467
|
}
|
|
8184
|
-
async function subClean(args) {
|
|
8185
|
-
const { target, timeout, concurrency } = resolveTestTarget(args);
|
|
8186
|
-
const rounds = parseIntArg(args, "-r", "--rounds", DEFAULT_CLEAN_ROUNDS);
|
|
8187
|
-
console.log(`\u6E05\u7406\u8BA2\u9605 "${target.name}"...`);
|
|
8188
|
-
console.log(`\u8D85\u65F6: ${timeout}ms \u5E76\u53D1: ${concurrency}`);
|
|
8189
|
-
console.log("");
|
|
8190
|
-
const progress = createProgressPrinter(rounds);
|
|
8191
|
-
const result = await withTestInstance(target.name, async (apiBase) => {
|
|
8192
|
-
return autoCleanSubscription(target.name, {
|
|
8193
|
-
timeout,
|
|
8194
|
-
concurrency,
|
|
8195
|
-
rounds,
|
|
8196
|
-
apiBase,
|
|
8197
|
-
onResult: progress.onResult,
|
|
8198
|
-
onRetryRound: progress.onRetryRound
|
|
8199
|
-
});
|
|
8200
|
-
});
|
|
8201
|
-
progress.finish();
|
|
8202
|
-
console.log(formatTestSummary(result.summary));
|
|
8203
|
-
if (result.skipped) {
|
|
8204
|
-
console.log("");
|
|
8205
|
-
console.log(colors.yellow("\u5B58\u6D3B\u8282\u70B9\u4E0D\u8DB3 1%\uFF0C\u8DF3\u8FC7\u6E05\u7406\u3002\u8BF7\u68C0\u67E5\u539F\u59CB\u8BA2\u9605\u662F\u5426\u6709\u6548"));
|
|
8206
|
-
} else if (result.removedProxies > 0) {
|
|
8207
|
-
console.log(`${colors.green("\u5DF2\u6E05\u7406")}: ${formatCleanSummary(result)}`);
|
|
8208
|
-
if (getRunningState().running) {
|
|
8209
|
-
console.log("");
|
|
8210
|
-
console.log("\u63D0\u793A: \u9700\u8981\u91CD\u542F mihomo \u4F7F\u66F4\u6539\u751F\u6548 (mihomo start)");
|
|
8211
|
-
}
|
|
8212
|
-
}
|
|
8213
|
-
}
|
|
8214
|
-
async function subTest(args) {
|
|
8215
|
-
const { target, timeout, concurrency } = resolveTestTarget(args);
|
|
8216
|
-
console.log(`\u6D4B\u8BD5\u8BA2\u9605 "${target.name}" \u7684\u8282\u70B9\u8FDE\u901A\u6027...`);
|
|
8217
|
-
console.log(`\u8D85\u65F6: ${timeout}ms \u5E76\u53D1: ${concurrency}`);
|
|
8218
|
-
console.log("");
|
|
8219
|
-
const progress = createProgressPrinter();
|
|
8220
|
-
const summary = await withTestInstance(target.name, async (apiBase) => {
|
|
8221
|
-
return testSubscriptionProxies(target.name, {
|
|
8222
|
-
timeout,
|
|
8223
|
-
concurrency,
|
|
8224
|
-
apiBase,
|
|
8225
|
-
onResult: progress.onResult
|
|
8226
|
-
});
|
|
8227
|
-
});
|
|
8228
|
-
progress.finish();
|
|
8229
|
-
console.log(formatTestSummary(summary));
|
|
8230
|
-
}
|
|
8231
7468
|
var SUBCOMMANDS5 = [
|
|
8232
7469
|
{ name: "list", handler: printSubscriptionList },
|
|
8233
7470
|
{ name: "add", handler: subAdd },
|
|
8234
7471
|
{ name: "update", handler: subUpdate },
|
|
8235
7472
|
{ name: "use", handler: subUse },
|
|
8236
|
-
{ name: "
|
|
8237
|
-
{ name: "remove", aliases: ["rm", "delete"], handler: subRemove },
|
|
8238
|
-
{ name: "clean", handler: subClean },
|
|
8239
|
-
{ name: "test", handler: subTest }
|
|
7473
|
+
{ name: "remove", aliases: ["rm", "delete"], handler: subRemove }
|
|
8240
7474
|
];
|
|
8241
7475
|
async function cmdSubscription(args) {
|
|
8242
7476
|
await dispatchSubcommand(args, SUBCOMMANDS5, {
|
|
8243
7477
|
// 无子命令 → 列表;未知子命令 → 报错
|
|
8244
7478
|
fallback: printSubscriptionList,
|
|
8245
7479
|
onUnknown: (action) => {
|
|
7480
|
+
if (action === "test" || action === "clean") {
|
|
7481
|
+
throw new CliError(`sub ${action} \u5DF2\u79FB\u9664\uFF08v3.10.0\uFF09`, {
|
|
7482
|
+
hint: ["\u8282\u70B9\u6D4B\u901F\u6539\u7528 Web \u9762\u677F: mihomo ui\uFF08zash / metacubexd / yacd \u5747\u5185\u7F6E\u9010\u8282\u70B9\u6D4B\u5EF6\u8FDF\uFF09", "\u81EA\u52A8\u9009\u8DEF\u8BF7\u5728\u8BA2\u9605\u91CC\u914D\u7F6E url-test \u5206\u7EC4\uFF0C\u7531\u5185\u6838\u6301\u7EED\u6D4B\u901F\u3002"]
|
|
7483
|
+
});
|
|
7484
|
+
}
|
|
8246
7485
|
const names = SUBCOMMANDS5.flatMap((c) => [c.name, ...c.aliases ?? []]);
|
|
8247
7486
|
const suggestion = suggestSimilar(action, names);
|
|
8248
7487
|
throw new CliError(`\u672A\u77E5\u7684\u8BA2\u9605\u547D\u4EE4: ${action}`, {
|
|
8249
|
-
hint: [...suggestion.length > 0 ? [`\u662F\u5426\u60F3\u8F93\u5165: ${suggestion.join(" / ")}?`] : [], "\u7528\u6CD5: mihomo sub [list|use|add|update|remove
|
|
7488
|
+
hint: [...suggestion.length > 0 ? [`\u662F\u5426\u60F3\u8F93\u5165: ${suggestion.join(" / ")}?`] : [], "\u7528\u6CD5: mihomo sub [list|use|add|update|remove]"]
|
|
8250
7489
|
});
|
|
8251
7490
|
}
|
|
8252
7491
|
});
|
|
8253
7492
|
}
|
|
8254
7493
|
|
|
8255
|
-
// src/commands/test.ts
|
|
8256
|
-
async function cmdTest(args) {
|
|
8257
|
-
const timeout = parseIntArg(args, "-t", "--timeout", DEFAULT_TEST_TIMEOUT);
|
|
8258
|
-
const concurrency = parseIntArg(args, "-j", "--concurrency", DEFAULT_TEST_CONCURRENCY);
|
|
8259
|
-
requireRunning();
|
|
8260
|
-
const activeSub = requireActiveSubscription("\u6CA1\u6709\u6D3B\u8DC3\u8BA2\u9605");
|
|
8261
|
-
console.log(`\u6D4B\u8BD5 "${activeSub.name}" \u8282\u70B9\u8FDE\u901A\u6027...`);
|
|
8262
|
-
console.log(`\u8D85\u65F6: ${timeout}ms \u5E76\u53D1: ${concurrency}`);
|
|
8263
|
-
console.log("");
|
|
8264
|
-
const progress = createProgressPrinter();
|
|
8265
|
-
const summary = await testSubscriptionProxies(activeSub.name, {
|
|
8266
|
-
timeout,
|
|
8267
|
-
concurrency,
|
|
8268
|
-
onResult: progress.onResult
|
|
8269
|
-
});
|
|
8270
|
-
progress.finish();
|
|
8271
|
-
console.log(formatTestSummary(summary));
|
|
8272
|
-
}
|
|
8273
|
-
async function cmdClean(args) {
|
|
8274
|
-
const timeout = parseIntArg(args, "-t", "--timeout", DEFAULT_TEST_TIMEOUT);
|
|
8275
|
-
const concurrency = parseIntArg(args, "-j", "--concurrency", DEFAULT_TEST_CONCURRENCY);
|
|
8276
|
-
const rounds = parseIntArg(args, "-r", "--rounds", DEFAULT_CLEAN_ROUNDS);
|
|
8277
|
-
requireRunning();
|
|
8278
|
-
const activeSub = requireActiveSubscription("\u6CA1\u6709\u6D3B\u8DC3\u8BA2\u9605");
|
|
8279
|
-
console.log(`\u6E05\u7406 "${activeSub.name}" \u5931\u8D25\u8282\u70B9...`);
|
|
8280
|
-
console.log(`\u8D85\u65F6: ${timeout}ms \u5E76\u53D1: ${concurrency}`);
|
|
8281
|
-
console.log("");
|
|
8282
|
-
const progress = createProgressPrinter(rounds);
|
|
8283
|
-
const result = await autoCleanSubscription(activeSub.name, {
|
|
8284
|
-
timeout,
|
|
8285
|
-
concurrency,
|
|
8286
|
-
rounds,
|
|
8287
|
-
onResult: progress.onResult,
|
|
8288
|
-
onRetryRound: progress.onRetryRound
|
|
8289
|
-
});
|
|
8290
|
-
progress.finish();
|
|
8291
|
-
console.log(formatTestSummary(result.summary));
|
|
8292
|
-
if (result.skipped) {
|
|
8293
|
-
console.log("");
|
|
8294
|
-
console.log("\u5B58\u6D3B\u8282\u70B9\u4E0D\u8DB3 1%\uFF0C\u8DF3\u8FC7\u6E05\u7406\u3002\u8BF7\u68C0\u67E5\u539F\u59CB\u8BA2\u9605\u662F\u5426\u6709\u6548");
|
|
8295
|
-
} else if (result.removedProxies === 0) {
|
|
8296
|
-
console.log("\u6240\u6709\u8282\u70B9\u6B63\u5E38\uFF0C\u65E0\u9700\u6E05\u7406");
|
|
8297
|
-
} else {
|
|
8298
|
-
console.log(`${colors.green("\u5DF2\u6E05\u7406")}: ${formatCleanSummary(result)}`);
|
|
8299
|
-
console.log("");
|
|
8300
|
-
console.log("\u91CD\u542F mihomo \u4F7F\u66F4\u6539\u751F\u6548...");
|
|
8301
|
-
const mode = getRuntimeMode();
|
|
8302
|
-
const daemonManaged = isDaemonEnabled();
|
|
8303
|
-
try {
|
|
8304
|
-
if (!daemonManaged) {
|
|
8305
|
-
if (hasRootResidue()) {
|
|
8306
|
-
throw new CliError("\u4E3B\u5B9E\u4F8B\u4EE5 root \u8FD0\u884C\uFF08TUN\uFF09\uFF0C\u505C\u6B62\u5B83\u9700\u8981 sudo", { hint: "\u8BF7\u6539\u7528 mihomo sub clean\uFF08\u9694\u79BB\u5B9E\u4F8B\u6D4B\u901F\uFF0C\u65E0\u9700\u505C\u6B62\u4E3B\u5B9E\u4F8B\uFF09" });
|
|
8307
|
-
}
|
|
8308
|
-
handleStopResult(stop());
|
|
8309
|
-
}
|
|
8310
|
-
const configInfo = prepareConfigForStart(mode, activeSub.name);
|
|
8311
|
-
const pid = await launchOrRestart(mode);
|
|
8312
|
-
const label = daemonManaged ? "\u5DF2\u91CD\u542F (\u4FDD\u6D3B)" : "\u5DF2\u91CD\u542F";
|
|
8313
|
-
console.log(`${colors.green(label)}${pid ? ` (PID ${pid})` : ""} \xB7 ${formatProxySummary(configInfo)}`);
|
|
8314
|
-
} catch (e) {
|
|
8315
|
-
if (e instanceof CliError) throw e;
|
|
8316
|
-
throw new CliError(e.message.split("\n")[0], { label: "\u91CD\u542F\u5931\u8D25" });
|
|
8317
|
-
}
|
|
8318
|
-
}
|
|
8319
|
-
}
|
|
8320
|
-
|
|
8321
7494
|
// src/commands/ui.ts
|
|
8322
7495
|
function cmdUI(args) {
|
|
8323
7496
|
const uiName = args[1] || "zash";
|
|
@@ -8342,7 +7515,7 @@ function cmdUI(args) {
|
|
|
8342
7515
|
}
|
|
8343
7516
|
|
|
8344
7517
|
// src/commands/update.ts
|
|
8345
|
-
import { execFile, spawn as
|
|
7518
|
+
import { execFile, spawn as spawn3 } from "child_process";
|
|
8346
7519
|
import { promisify } from "util";
|
|
8347
7520
|
var execFileAsync = promisify(execFile);
|
|
8348
7521
|
var NPM_VIEW_TIMEOUT_MS = 15e3;
|
|
@@ -8373,7 +7546,7 @@ async function cmdUpdate() {
|
|
|
8373
7546
|
console.log("\u6B63\u5728\u66F4\u65B0 mihomo-cli...");
|
|
8374
7547
|
console.log("");
|
|
8375
7548
|
await new Promise((resolve, reject) => {
|
|
8376
|
-
const npm =
|
|
7549
|
+
const npm = spawn3("npm", ["install", "-g", PKG_NAME], { stdio: "inherit" });
|
|
8377
7550
|
npm.on("close", (code) => {
|
|
8378
7551
|
if (code === 0) {
|
|
8379
7552
|
resolve();
|
|
@@ -8410,7 +7583,7 @@ var COMMANDS = [
|
|
|
8410
7583
|
aliases: ["up"],
|
|
8411
7584
|
handler: cmdStart,
|
|
8412
7585
|
group: "control",
|
|
8413
|
-
usage: ["start [tun|mixed] [-s] [-u ms]
|
|
7586
|
+
usage: ["start [tun|mixed] [-s] [-u ms] [--no-ssh] \u542F\u52A8/\u5207\u6362\u4EE3\u7406 (\u9ED8\u8BA4 mixed)"]
|
|
8414
7587
|
},
|
|
8415
7588
|
{
|
|
8416
7589
|
name: "tun",
|
|
@@ -8467,10 +7640,7 @@ var COMMANDS = [
|
|
|
8467
7640
|
"subscription use <name> \u5207\u6362\u5F53\u524D\u8BA2\u9605",
|
|
8468
7641
|
"subscription add <url> [name] \u6DFB\u52A0\u8BA2\u9605",
|
|
8469
7642
|
"subscription update [name] \u66F4\u65B0\u8BA2\u9605\uFF08\u65E0\u53C2\u66F4\u65B0\u6240\u6709\uFF09",
|
|
8470
|
-
"subscription remove <name> \u5220\u9664\u8BA2\u9605\uFF08\u6A21\u7CCA\u5339\u914D\u9700\u786E\u8BA4\uFF0C-y \u8DF3\u8FC7\uFF09"
|
|
8471
|
-
"subscription web [name] \u6253\u5F00\u8BA2\u9605\u9875\u9762",
|
|
8472
|
-
"subscription test [name] \u6D4B\u8BD5\u8282\u70B9\uFF08\u72EC\u7ACB\u5B9E\u4F8B\uFF0C\u65E0\u9700\u8FD0\u884C\uFF09",
|
|
8473
|
-
"subscription clean [name] \u6D4B\u901F\u6E05\u7406\uFF08\u72EC\u7ACB\u5B9E\u4F8B\uFF0C\u4E0D\u52A8\u4E3B\u5B9E\u4F8B\uFF09[-r N] [-t ms] [-j N]"
|
|
7643
|
+
"subscription remove <name> \u5220\u9664\u8BA2\u9605\uFF08\u6A21\u7CCA\u5339\u914D\u9700\u786E\u8BA4\uFF0C-y \u8DF3\u8FC7\uFF09"
|
|
8474
7644
|
]
|
|
8475
7645
|
},
|
|
8476
7646
|
{
|
|
@@ -8481,20 +7651,6 @@ var COMMANDS = [
|
|
|
8481
7651
|
group: "subscription",
|
|
8482
7652
|
usage: []
|
|
8483
7653
|
},
|
|
8484
|
-
{
|
|
8485
|
-
name: "test",
|
|
8486
|
-
aliases: [],
|
|
8487
|
-
handler: cmdTest,
|
|
8488
|
-
group: "subscription",
|
|
8489
|
-
usage: ["test [-t ms] [-j N] \u6D4B\u8BD5\u5F53\u524D\u8282\u70B9\uFF08\u7ECF\u8FD0\u884C\u4E2D\u7684\u4E3B\u5B9E\u4F8B\uFF09"]
|
|
8490
|
-
},
|
|
8491
|
-
{
|
|
8492
|
-
name: "clean",
|
|
8493
|
-
aliases: [],
|
|
8494
|
-
handler: cmdClean,
|
|
8495
|
-
group: "subscription",
|
|
8496
|
-
usage: ["clean [-t ms] [-j N] [-r N] \u6E05\u7406\u5931\u8D25\u8282\u70B9\u5E76\u91CD\u542F\uFF08\u7ECF\u4E3B\u5B9E\u4F8B\uFF09"]
|
|
8497
|
-
},
|
|
8498
7654
|
// === 配置 ===
|
|
8499
7655
|
{
|
|
8500
7656
|
name: "overwrite",
|
|
@@ -8524,7 +7680,7 @@ var COMMANDS = [
|
|
|
8524
7680
|
aliases: ["dir", "dirs", "directories"],
|
|
8525
7681
|
handler: cmdDirectory,
|
|
8526
7682
|
group: "config",
|
|
8527
|
-
usage: ["directory \u663E\u793A\u6570\u636E\u76EE\u5F55\u4F4D\u7F6E\uFF08\u522B\u540D dir\uFF09", "directory open [target] \u6253\u5F00\u76EE\u5F55: root|subs|logs|runtime
|
|
7683
|
+
usage: ["directory \u663E\u793A\u6570\u636E\u76EE\u5F55\u4F4D\u7F6E\uFF08\u522B\u540D dir\uFF09", "directory open [target] \u6253\u5F00\u76EE\u5F55: root|subs|logs|data|runtime|kernel"]
|
|
8528
7684
|
},
|
|
8529
7685
|
{
|
|
8530
7686
|
name: "open",
|
|
@@ -8540,7 +7696,7 @@ var COMMANDS = [
|
|
|
8540
7696
|
aliases: [],
|
|
8541
7697
|
handler: cmdKernel,
|
|
8542
7698
|
group: "system",
|
|
8543
|
-
usage: ["kernel [--mirror [\u955C\u50CF]]
|
|
7699
|
+
usage: ["kernel [--mirror [\u955C\u50CF]] \u66F4\u65B0\u5185\u6838\uFF08\u9ED8\u8BA4\u76F4\u8FDE\uFF0C--mirror \u8D70\u955C\u50CF\u4E0B\u8F7D\uFF09"]
|
|
8544
7700
|
},
|
|
8545
7701
|
{
|
|
8546
7702
|
name: "daemon",
|
|
@@ -8663,6 +7819,15 @@ function assertSupportedPlatform(commandName) {
|
|
|
8663
7819
|
]
|
|
8664
7820
|
});
|
|
8665
7821
|
}
|
|
7822
|
+
var REMOVED_COMMANDS = {
|
|
7823
|
+
test: ["\u8282\u70B9\u6D4B\u901F\u6539\u7528 Web \u9762\u677F: mihomo ui\uFF08zash / metacubexd / yacd \u5747\u5185\u7F6E\u9010\u8282\u70B9\u6D4B\u5EF6\u8FDF\uFF09", "\u81EA\u52A8\u9009\u8DEF\u8BF7\u5728\u8BA2\u9605\u91CC\u914D\u7F6E url-test \u5206\u7EC4\uFF0C\u7531\u5185\u6838\u6301\u7EED\u6D4B\u901F\u3002"],
|
|
7824
|
+
clean: [
|
|
7825
|
+
"\u6309\u6D4B\u901F\u7ED3\u679C\u5220\u8282\u70B9\u7684\u529F\u80FD\u5DF2\u79FB\u9664\uFF1A\u4E00\u6B21\u6296\u52A8\u5C31\u53EF\u80FD\u6C38\u4E45\u6539\u5199\u8BA2\u9605\u6587\u4EF6\uFF0C\u8BEF\u5220\u53EA\u80FD\u91CD\u4E0B\u8BA2\u9605\u3002",
|
|
7826
|
+
"\u5931\u6548\u8282\u70B9\u4EA4\u7ED9\u5185\u6838\u7684 url-test / fallback \u5206\u7EC4\u81EA\u52A8\u7ED5\u5F00\u5373\u53EF\u3002",
|
|
7827
|
+
"",
|
|
7828
|
+
"\u67E5\u770B\u8282\u70B9\u5EF6\u8FDF: mihomo ui"
|
|
7829
|
+
]
|
|
7830
|
+
};
|
|
8666
7831
|
async function main() {
|
|
8667
7832
|
clearProxyEnv();
|
|
8668
7833
|
const args = process.argv.slice(2);
|
|
@@ -8676,6 +7841,9 @@ async function main() {
|
|
|
8676
7841
|
const token = args[0].toLowerCase();
|
|
8677
7842
|
const command = findCommand(token);
|
|
8678
7843
|
if (!command) {
|
|
7844
|
+
if (Object.hasOwn(REMOVED_COMMANDS, token)) {
|
|
7845
|
+
throw new CliError(`${token} \u5DF2\u79FB\u9664\uFF08v3.10.0\uFF09`, { hint: REMOVED_COMMANDS[token] });
|
|
7846
|
+
}
|
|
8679
7847
|
const suggestion = suggestSimilar(token, allCommandTokens());
|
|
8680
7848
|
throw new CliError(`\u672A\u77E5\u547D\u4EE4: ${token}`, {
|
|
8681
7849
|
hint: [suggestion.length > 0 ? `\u662F\u5426\u60F3\u8F93\u5165: ${suggestion.join(" / ")}?` : '\u4F7F\u7528 "mihomo help" \u67E5\u770B\u5E2E\u52A9']
|