mihomo-cli 3.9.1 → 3.11.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 +62 -0
- package/README.md +20 -36
- package/dist/index.js +84 -1010
- 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
|
|
@@ -4513,8 +4453,7 @@ ${colors.cyan(colors.bold(`mihomo-cli v${VERSION}`))}`, "", "\u547D\u4EE4\u522B\
|
|
|
4513
4453
|
" mihomo ui # \u6253\u5F00 Web UI",
|
|
4514
4454
|
"",
|
|
4515
4455
|
`${colors.cyan("\u5FEB\u6377\u547D\u4EE4:")}`,
|
|
4516
|
-
" tun = start tun
|
|
4517
|
-
" up = start down = stop upd/upgrade = update",
|
|
4456
|
+
" tun = start tun up = start down = stop",
|
|
4518
4457
|
"",
|
|
4519
4458
|
`${colors.cyan("\u6A21\u5F0F\u8BF4\u660E:")}`,
|
|
4520
4459
|
" mixed HTTP + SOCKS5 \u6DF7\u5408\u7AEF\u53E3 (\u9ED8\u8BA4)",
|
|
@@ -5038,29 +4977,6 @@ function listLogs() {
|
|
|
5038
4977
|
result.archives.sort((a, b) => b.mtime.getTime() - a.mtime.getTime());
|
|
5039
4978
|
return result;
|
|
5040
4979
|
}
|
|
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
4980
|
function openUrl(url) {
|
|
5065
4981
|
try {
|
|
5066
4982
|
const child = spawn("open", ["--", url], { stdio: "ignore", detached: true });
|
|
@@ -5390,11 +5306,7 @@ async function readBodyWithLimit(response, controller, limit) {
|
|
|
5390
5306
|
|
|
5391
5307
|
// src/subscription.ts
|
|
5392
5308
|
function isGithubUrl(url) {
|
|
5393
|
-
|
|
5394
|
-
if (isMultiUrl(url)) {
|
|
5395
|
-
return splitUrls(url).every((u) => githubRe.test(u));
|
|
5396
|
-
}
|
|
5397
|
-
return githubRe.test(url);
|
|
5309
|
+
return /github\.com|raw\.githubusercontent\.com/i.test(url);
|
|
5398
5310
|
}
|
|
5399
5311
|
function getDefaultUpdateInterval(url) {
|
|
5400
5312
|
return isGithubUrl(url) ? DEFAULT_UPDATE_INTERVAL_HOURS_GITHUB : DEFAULT_UPDATE_INTERVAL_HOURS;
|
|
@@ -5403,11 +5315,6 @@ function resolveUpdateInterval(url, cachedInterval) {
|
|
|
5403
5315
|
return cachedInterval && cachedInterval > 0 ? cachedInterval : getDefaultUpdateInterval(url);
|
|
5404
5316
|
}
|
|
5405
5317
|
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
5318
|
function isValidHttpUrl(url) {
|
|
5412
5319
|
try {
|
|
5413
5320
|
const u = new URL(url.trim());
|
|
@@ -5416,28 +5323,6 @@ function isValidHttpUrl(url) {
|
|
|
5416
5323
|
return false;
|
|
5417
5324
|
}
|
|
5418
5325
|
}
|
|
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
5326
|
function parseUserInfo(header) {
|
|
5442
5327
|
if (!header) return null;
|
|
5443
5328
|
const info = {};
|
|
@@ -5601,65 +5486,6 @@ async function downloadSubscription(url, subName = "default", signal, persist =
|
|
|
5601
5486
|
username: meta.username
|
|
5602
5487
|
};
|
|
5603
5488
|
}
|
|
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
5489
|
function prepareConfigForStart(mode, subName = "default") {
|
|
5664
5490
|
const rawContent = readSubscriptionRawConfig(subName);
|
|
5665
5491
|
if (!rawContent) {
|
|
@@ -5693,12 +5519,7 @@ function needsAutoUpdate(sub) {
|
|
|
5693
5519
|
}
|
|
5694
5520
|
async function tryUpdateOne(sub, signal) {
|
|
5695
5521
|
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
|
-
}
|
|
5522
|
+
const info = await downloadSubscription(sub.url, sub.name, signal);
|
|
5702
5523
|
return { name: sub.name, success: true, proxies: info.proxies, proxyGroups: info.proxyGroups };
|
|
5703
5524
|
} catch (e) {
|
|
5704
5525
|
return { name: sub.name, success: false, error: e.message };
|
|
@@ -5751,187 +5572,6 @@ async function autoUpdateStaleSubscription(options = {}) {
|
|
|
5751
5572
|
}
|
|
5752
5573
|
return { total: staleSubs.length, updated: updatedCount, failed: staleSubs.length - updatedCount };
|
|
5753
5574
|
}
|
|
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
5575
|
|
|
5936
5576
|
// src/commands/shared.ts
|
|
5937
5577
|
import readline from "readline";
|
|
@@ -5962,78 +5602,6 @@ async function launchOrRestart(mode) {
|
|
|
5962
5602
|
return result.pid;
|
|
5963
5603
|
}
|
|
5964
5604
|
|
|
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
5605
|
// src/ssh.ts
|
|
6038
5606
|
import { spawn as spawn2 } from "child_process";
|
|
6039
5607
|
import fs8 from "fs";
|
|
@@ -6049,10 +5617,6 @@ function getReservedPorts() {
|
|
|
6049
5617
|
const mixedPort = Number(BASE_CONFIG["mixed-port"]);
|
|
6050
5618
|
if (Number.isInteger(mixedPort)) reserved.set(mixedPort, "mihomo \u6DF7\u5408\u4EE3\u7406\u7AEF\u53E3");
|
|
6051
5619
|
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
5620
|
return reserved;
|
|
6057
5621
|
}
|
|
6058
5622
|
var SAFE_HOST_RE = /^[A-Za-z0-9][A-Za-z0-9._@-]*$/;
|
|
@@ -6514,11 +6078,7 @@ async function cmdStart(args) {
|
|
|
6514
6078
|
if (targetMode === "tun" && daemonEnabled) {
|
|
6515
6079
|
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
6080
|
}
|
|
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
6081
|
const skipUpdate = hasFlag(args, "-s", "--no-update");
|
|
6521
|
-
const skipClean = hasFlag(args, "--no-clean");
|
|
6522
6082
|
const skipSsh = hasFlag(args, "--no-ssh");
|
|
6523
6083
|
const updateTimeout = parseIntArg(args, "-u", "--update-timeout", DEFAULT_AUTO_UPDATE_TIMEOUT);
|
|
6524
6084
|
const sub = requireActiveSubscription("\u6CA1\u6709\u8BA2\u9605\uFF0C\u8BF7\u5148\u6DFB\u52A0\u8BA2\u9605");
|
|
@@ -6564,45 +6124,6 @@ async function cmdStart(args) {
|
|
|
6564
6124
|
if (!skipSsh) {
|
|
6565
6125
|
await startAutoSshTunnelsWithWarning();
|
|
6566
6126
|
}
|
|
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
6127
|
await printStatus();
|
|
6607
6128
|
}
|
|
6608
6129
|
|
|
@@ -6627,13 +6148,6 @@ async function confirmPrompt(question) {
|
|
|
6627
6148
|
});
|
|
6628
6149
|
return answer.toLowerCase() === "y" || answer.toLowerCase() === "yes";
|
|
6629
6150
|
}
|
|
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
6151
|
async function restartToApply(args) {
|
|
6638
6152
|
if (!isRestartNeededOnChange()) return false;
|
|
6639
6153
|
const currentMode = getRuntimeMode();
|
|
@@ -6759,38 +6273,21 @@ function openDirectory(args) {
|
|
|
6759
6273
|
throw new CliError(`\u672A\u77E5\u7684\u76EE\u5F55\u76EE\u6807 "${target}"`, { hint });
|
|
6760
6274
|
}
|
|
6761
6275
|
function printDirectoryInfo() {
|
|
6276
|
+
const entries = Object.entries(DIRECTORY_TARGETS).filter(([key]) => key !== "root");
|
|
6277
|
+
const pathWidth = Math.max(...entries.map(([, val]) => val.path.replace(USER_DATA_DIR, ".").length));
|
|
6762
6278
|
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)");
|
|
6279
|
+
console.log(`\u6570\u636E\u76EE\u5F55: ${USER_DATA_DIR}`);
|
|
6777
6280
|
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");
|
|
6281
|
+
for (const [key, val] of entries) {
|
|
6282
|
+
const rel = val.path.replace(USER_DATA_DIR, ".");
|
|
6283
|
+
console.log(` ${key.padEnd(9)}${rel.padEnd(pathWidth + 2)}${colors.gray(val.label)}`);
|
|
6284
|
+
}
|
|
6785
6285
|
console.log("");
|
|
6786
|
-
console.log(
|
|
6787
|
-
console.log("
|
|
6286
|
+
console.log(`\u6253\u5F00\u76EE\u5F55: mihomo dir open [${Object.keys(DIRECTORY_TARGETS).join("|")}]`);
|
|
6287
|
+
console.log(colors.gray(" \u81EA\u5B9A\u4E49\u6839\u76EE\u5F55: \u73AF\u5883\u53D8\u91CF MIHOMO_CLI_DIR"));
|
|
6788
6288
|
console.log("");
|
|
6789
6289
|
}
|
|
6790
|
-
var SUBCOMMANDS2 = [
|
|
6791
|
-
{ name: "open", handler: openDirectory },
|
|
6792
|
-
{ name: "list", handler: printDirectoryInfo }
|
|
6793
|
-
];
|
|
6290
|
+
var SUBCOMMANDS2 = [{ name: "open", handler: openDirectory }];
|
|
6794
6291
|
async function cmdDirectory(args) {
|
|
6795
6292
|
await dispatchSubcommand(args, SUBCOMMANDS2, {
|
|
6796
6293
|
fallback: printDirectoryInfo,
|
|
@@ -6798,12 +6295,7 @@ async function cmdDirectory(args) {
|
|
|
6798
6295
|
const names = SUBCOMMANDS2.flatMap((c) => [c.name, ...c.aliases ?? []]);
|
|
6799
6296
|
const suggestion = suggestSimilar(action, names);
|
|
6800
6297
|
throw new CliError(`\u672A\u77E5\u7684\u76EE\u5F55\u5B50\u547D\u4EE4: ${action}`, {
|
|
6801
|
-
hint: [
|
|
6802
|
-
...suggestion.length > 0 ? [`\u662F\u5426\u60F3\u8F93\u5165: ${suggestion.join(" / ")}?`] : [],
|
|
6803
|
-
"",
|
|
6804
|
-
"\u53EF\u7528\u5B50\u547D\u4EE4: open, list",
|
|
6805
|
-
"\u6253\u5F00\u6307\u5B9A\u76EE\u5F55: mihomo dir open <target>"
|
|
6806
|
-
]
|
|
6298
|
+
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>"]
|
|
6807
6299
|
});
|
|
6808
6300
|
}
|
|
6809
6301
|
});
|
|
@@ -6814,7 +6306,7 @@ import { spawnSync as spawnSync4 } from "child_process";
|
|
|
6814
6306
|
import fs9 from "fs";
|
|
6815
6307
|
import path8 from "path";
|
|
6816
6308
|
|
|
6817
|
-
//
|
|
6309
|
+
// node_modules/compare-versions/lib/esm/utils.js
|
|
6818
6310
|
var semver = /^[v^~<>=]*?(\d+)(?:\.([x*]|\d+)(?:\.([x*]|\d+)(?:\.([x*]|\d+))?(?:-([\da-z\-]+(?:\.[\da-z\-]+)*))?(?:\+[\da-z\-]+(?:\.[\da-z\-]+)*)?)?)?$/i;
|
|
6819
6311
|
var validateAndParse = (version) => {
|
|
6820
6312
|
if (typeof version !== "string") {
|
|
@@ -6852,7 +6344,7 @@ var compareSegments = (a, b) => {
|
|
|
6852
6344
|
return 0;
|
|
6853
6345
|
};
|
|
6854
6346
|
|
|
6855
|
-
//
|
|
6347
|
+
// node_modules/compare-versions/lib/esm/compareVersions.js
|
|
6856
6348
|
var compareVersions = (v1, v2) => {
|
|
6857
6349
|
const n1 = validateAndParse(v1);
|
|
6858
6350
|
const n2 = validateAndParse(v2);
|
|
@@ -6875,7 +6367,7 @@ var KERNEL_HTTP_TIMEOUT = 12e4;
|
|
|
6875
6367
|
var KERNEL_DOWNLOAD_TIMEOUT = 18e4;
|
|
6876
6368
|
var HTTP_CLIENT2 = createHttpClient({ timeout: KERNEL_HTTP_TIMEOUT });
|
|
6877
6369
|
function withMirror(url, mirror) {
|
|
6878
|
-
if (mirror &&
|
|
6370
|
+
if (mirror && url.startsWith("https://github.com/")) {
|
|
6879
6371
|
return mirror + url;
|
|
6880
6372
|
}
|
|
6881
6373
|
return url;
|
|
@@ -6918,8 +6410,8 @@ function findMatchingAsset(assets, platform, arch) {
|
|
|
6918
6410
|
});
|
|
6919
6411
|
return standardAsset || matchingAssets[0];
|
|
6920
6412
|
}
|
|
6921
|
-
async function getLatestRelease(repo
|
|
6922
|
-
const url =
|
|
6413
|
+
async function getLatestRelease(repo) {
|
|
6414
|
+
const url = `https://api.github.com/repos/${repo}/releases`;
|
|
6923
6415
|
const response = await HTTP_CLIENT2.get(url, { responseType: "json" });
|
|
6924
6416
|
const releases = response.data;
|
|
6925
6417
|
if (!Array.isArray(releases) || releases.length === 0) {
|
|
@@ -6930,9 +6422,9 @@ async function getLatestRelease(repo, mirror) {
|
|
|
6930
6422
|
);
|
|
6931
6423
|
return stableReleases.length > 0 ? stableReleases[0] : releases[0];
|
|
6932
6424
|
}
|
|
6933
|
-
async function checkUpdate(
|
|
6425
|
+
async function checkUpdate() {
|
|
6934
6426
|
const currentVersion = getKernelVersion();
|
|
6935
|
-
const latest = await getLatestRelease(GITHUB_REPO
|
|
6427
|
+
const latest = await getLatestRelease(GITHUB_REPO);
|
|
6936
6428
|
const latestVersion = latest.tag_name;
|
|
6937
6429
|
let needsUpdate = false;
|
|
6938
6430
|
const currentDisplay = currentVersion || "\u672A\u5B89\u88C5";
|
|
@@ -6972,7 +6464,7 @@ function findBinaryInDir(dir, maxDepth = 4) {
|
|
|
6972
6464
|
}
|
|
6973
6465
|
async function downloadKernel(progressCallback, mirror, releaseInfo) {
|
|
6974
6466
|
ensureDirs();
|
|
6975
|
-
const latest = releaseInfo || await getLatestRelease(GITHUB_REPO
|
|
6467
|
+
const latest = releaseInfo || await getLatestRelease(GITHUB_REPO);
|
|
6976
6468
|
const arch = getArch();
|
|
6977
6469
|
const platform = process.platform;
|
|
6978
6470
|
const asset = findMatchingAsset(latest.assets, platform, arch);
|
|
@@ -7142,14 +6634,12 @@ async function cmdKernel(args) {
|
|
|
7142
6634
|
const mirrorInfo = parseMirrorArg(args);
|
|
7143
6635
|
const effectiveMirror = mirrorInfo.mirror;
|
|
7144
6636
|
if (effectiveMirror) {
|
|
7145
|
-
|
|
7146
|
-
console.log(`\u955C\u50CF: ${effectiveMirror}${mirrorDesc}`);
|
|
6637
|
+
console.log(`\u955C\u50CF: ${effectiveMirror} (\u4EC5\u4E0B\u8F7D\u8D70\u955C\u50CF\uFF0C\u7248\u672C\u67E5\u8BE2\u6052\u76F4\u8FDE)`);
|
|
7147
6638
|
console.log("");
|
|
7148
6639
|
}
|
|
7149
6640
|
console.log("\u68C0\u67E5\u5185\u6838\u66F4\u65B0...");
|
|
7150
6641
|
try {
|
|
7151
|
-
const
|
|
7152
|
-
const info = await checkUpdate(apiMirror);
|
|
6642
|
+
const info = await checkUpdate();
|
|
7153
6643
|
console.log(`\u5F53\u524D: ${info.current}`);
|
|
7154
6644
|
console.log(`\u6700\u65B0: ${info.latest}`);
|
|
7155
6645
|
if (!info.needsUpdate) {
|
|
@@ -7177,8 +6667,7 @@ async function cmdKernel(args) {
|
|
|
7177
6667
|
hint.push(
|
|
7178
6668
|
"",
|
|
7179
6669
|
"\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",
|
|
6670
|
+
" mihomo kernel --mirror [\u955C\u50CF] # \u4E0B\u8F7D\u8D70\u955C\u50CF\uFF08\u9ED8\u8BA4 v6.gh-proxy.org\uFF09",
|
|
7182
6671
|
` \u53EF\u7528\u955C\u50CF: ${AVAILABLE_MIRRORS.join(", ")}`
|
|
7183
6672
|
);
|
|
7184
6673
|
}
|
|
@@ -7201,23 +6690,18 @@ function cmdLogs(args) {
|
|
|
7201
6690
|
const openInViewer = hasFlag(args, "-o", "--open");
|
|
7202
6691
|
if (targetName) {
|
|
7203
6692
|
let logPath;
|
|
7204
|
-
if (targetName === "
|
|
6693
|
+
if (targetName === "0") {
|
|
7205
6694
|
logPath = getLogPath();
|
|
7206
6695
|
} else {
|
|
7207
6696
|
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);
|
|
6697
|
+
if (Number.isNaN(parsedIdx) || parsedIdx < 1 || String(parsedIdx) !== targetName) {
|
|
6698
|
+
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
6699
|
}
|
|
7218
|
-
|
|
7219
|
-
|
|
7220
|
-
|
|
6700
|
+
const archive = listLogs().archives[parsedIdx - 1];
|
|
6701
|
+
if (!archive) {
|
|
6702
|
+
throw new CliError(`\u672A\u627E\u5230\u65E5\u5FD7 "${targetName}"`, { hint: '\u4F7F\u7528 "mihomo logs" \u67E5\u770B\u53EF\u7528\u65E5\u5FD7\u5217\u8868' });
|
|
6703
|
+
}
|
|
6704
|
+
logPath = archive.path;
|
|
7221
6705
|
}
|
|
7222
6706
|
if (openInViewer) {
|
|
7223
6707
|
openLogFile(logPath);
|
|
@@ -7312,15 +6796,7 @@ async function setOverwrite(enabled, args) {
|
|
|
7312
6796
|
}
|
|
7313
6797
|
var SUBCOMMANDS3 = [
|
|
7314
6798
|
{ name: "on", aliases: ["enable"], handler: (args) => setOverwrite(true, args) },
|
|
7315
|
-
{ name: "off", aliases: ["disable"], handler: (args) => setOverwrite(false, args) }
|
|
7316
|
-
// list 显式注册:onUnknown 生效后,未注册的子命令会报错,不能再靠 fallback 兜住 `ow list`
|
|
7317
|
-
{
|
|
7318
|
-
name: "list",
|
|
7319
|
-
handler: () => {
|
|
7320
|
-
console.log("");
|
|
7321
|
-
printOverwriteList();
|
|
7322
|
-
}
|
|
7323
|
-
}
|
|
6799
|
+
{ name: "off", aliases: ["disable"], handler: (args) => setOverwrite(false, args) }
|
|
7324
6800
|
];
|
|
7325
6801
|
async function cmdOverwrite(args) {
|
|
7326
6802
|
await dispatchSubcommand(args, SUBCOMMANDS3, {
|
|
@@ -7333,7 +6809,7 @@ async function cmdOverwrite(args) {
|
|
|
7333
6809
|
const names = SUBCOMMANDS3.flatMap((c) => [c.name, ...c.aliases ?? []]);
|
|
7334
6810
|
const suggestion = suggestSimilar(action, names);
|
|
7335
6811
|
throw new CliError(`\u672A\u77E5\u7684\u8986\u5199\u5B50\u547D\u4EE4: ${action}`, {
|
|
7336
|
-
hint: [...suggestion.length > 0 ? [`\u662F\u5426\u60F3\u8F93\u5165: ${suggestion.join(" / ")}?`] : [], "", "\u53EF\u7528\u5B50\u547D\u4EE4: on, off
|
|
6812
|
+
hint: [...suggestion.length > 0 ? [`\u662F\u5426\u60F3\u8F93\u5165: ${suggestion.join(" / ")}?`] : [], "", "\u53EF\u7528\u5B50\u547D\u4EE4: on, off"]
|
|
7337
6813
|
});
|
|
7338
6814
|
}
|
|
7339
6815
|
});
|
|
@@ -7780,181 +7256,7 @@ async function cmdSsh(args) {
|
|
|
7780
7256
|
});
|
|
7781
7257
|
}
|
|
7782
7258
|
|
|
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
7259
|
// 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
7260
|
function printRestartHintIfRunning() {
|
|
7959
7261
|
if (getRunningState().running) {
|
|
7960
7262
|
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 +7277,8 @@ function printSubscriptionList() {
|
|
|
7975
7277
|
subs.forEach((s, i) => {
|
|
7976
7278
|
const time = formatDate(s.updated_at);
|
|
7977
7279
|
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
7280
|
const interval = resolveUpdateInterval(s.url, s.update_interval);
|
|
7980
|
-
console.log(` ${i + 1}. ${s.name}${defaultMark}
|
|
7281
|
+
console.log(` ${i + 1}. ${s.name}${defaultMark}`);
|
|
7981
7282
|
console.log(` ${colors.gray("\u66F4\u65B0: ")}${time} (\u95F4\u9694: ${interval}h)`);
|
|
7982
7283
|
if (s.username) {
|
|
7983
7284
|
console.log(` ${colors.gray("\u7528\u6237: ")}${s.username}`);
|
|
@@ -8005,9 +7306,6 @@ function printSubscriptionList() {
|
|
|
8005
7306
|
console.log("\u65B0\u589E\u8BA2\u9605: mihomo sub add <url> [name]");
|
|
8006
7307
|
console.log("\u66F4\u65B0\u8BA2\u9605: mihomo sub update [name]");
|
|
8007
7308
|
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
7309
|
console.log("");
|
|
8012
7310
|
}
|
|
8013
7311
|
async function subAdd(args) {
|
|
@@ -8016,45 +7314,19 @@ async function subAdd(args) {
|
|
|
8016
7314
|
if (!url) {
|
|
8017
7315
|
throw new CliError("\u8BF7\u63D0\u4F9B\u6709\u6548\u7684\u8BA2\u9605 URL");
|
|
8018
7316
|
}
|
|
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
|
-
}
|
|
7317
|
+
if (!isValidHttpUrl(url)) {
|
|
7318
|
+
throw new CliError("\u8BF7\u63D0\u4F9B\u6709\u6548\u7684\u8BA2\u9605 URL\uFF08\u9700\u4EE5 http:// \u6216 https:// \u5F00\u5934\uFF09");
|
|
7319
|
+
}
|
|
7320
|
+
console.log(`\u6DFB\u52A0\u8BA2\u9605: ${name}`);
|
|
7321
|
+
addSubscription(url, name);
|
|
7322
|
+
try {
|
|
7323
|
+
const info = await downloadSubscription(url, name);
|
|
7324
|
+
setDefaultSubscription(name);
|
|
7325
|
+
console.log(`\u5DF2\u6DFB\u52A0\u5E76\u5207\u6362\u5230 "${name}" (${formatProxySummary(info)})`);
|
|
7326
|
+
} catch (e) {
|
|
7327
|
+
removeSubscription(name);
|
|
7328
|
+
if (e instanceof CliError) throw new CliError(e.message, { label: "\u6DFB\u52A0\u5931\u8D25", hint: e.hint });
|
|
7329
|
+
throw new CliError(e.message, { label: "\u6DFB\u52A0\u5931\u8D25" });
|
|
8058
7330
|
}
|
|
8059
7331
|
console.log("");
|
|
8060
7332
|
printSubscriptionList();
|
|
@@ -8116,39 +7388,6 @@ async function subUse(args) {
|
|
|
8116
7388
|
console.log("");
|
|
8117
7389
|
printSubscriptionList();
|
|
8118
7390
|
}
|
|
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
7391
|
async function subRemove(args) {
|
|
8153
7392
|
const name = getNonFlagArg(args, 2);
|
|
8154
7393
|
const subs = getSubscriptions();
|
|
@@ -8181,62 +7420,12 @@ async function subRemove(args) {
|
|
|
8181
7420
|
console.log("");
|
|
8182
7421
|
printSubscriptionList();
|
|
8183
7422
|
}
|
|
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
7423
|
var SUBCOMMANDS5 = [
|
|
8232
7424
|
{ name: "list", handler: printSubscriptionList },
|
|
8233
7425
|
{ name: "add", handler: subAdd },
|
|
8234
7426
|
{ name: "update", handler: subUpdate },
|
|
8235
7427
|
{ name: "use", handler: subUse },
|
|
8236
|
-
{ name: "
|
|
8237
|
-
{ name: "remove", aliases: ["rm", "delete"], handler: subRemove },
|
|
8238
|
-
{ name: "clean", handler: subClean },
|
|
8239
|
-
{ name: "test", handler: subTest }
|
|
7428
|
+
{ name: "remove", aliases: ["rm", "delete"], handler: subRemove }
|
|
8240
7429
|
];
|
|
8241
7430
|
async function cmdSubscription(args) {
|
|
8242
7431
|
await dispatchSubcommand(args, SUBCOMMANDS5, {
|
|
@@ -8246,78 +7435,12 @@ async function cmdSubscription(args) {
|
|
|
8246
7435
|
const names = SUBCOMMANDS5.flatMap((c) => [c.name, ...c.aliases ?? []]);
|
|
8247
7436
|
const suggestion = suggestSimilar(action, names);
|
|
8248
7437
|
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
|
|
7438
|
+
hint: [...suggestion.length > 0 ? [`\u662F\u5426\u60F3\u8F93\u5165: ${suggestion.join(" / ")}?`] : [], "\u7528\u6CD5: mihomo sub [list|use|add|update|remove]"]
|
|
8250
7439
|
});
|
|
8251
7440
|
}
|
|
8252
7441
|
});
|
|
8253
7442
|
}
|
|
8254
7443
|
|
|
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
7444
|
// src/commands/ui.ts
|
|
8322
7445
|
function cmdUI(args) {
|
|
8323
7446
|
const uiName = args[1] || "zash";
|
|
@@ -8342,7 +7465,7 @@ function cmdUI(args) {
|
|
|
8342
7465
|
}
|
|
8343
7466
|
|
|
8344
7467
|
// src/commands/update.ts
|
|
8345
|
-
import { execFile, spawn as
|
|
7468
|
+
import { execFile, spawn as spawn3 } from "child_process";
|
|
8346
7469
|
import { promisify } from "util";
|
|
8347
7470
|
var execFileAsync = promisify(execFile);
|
|
8348
7471
|
var NPM_VIEW_TIMEOUT_MS = 15e3;
|
|
@@ -8373,7 +7496,7 @@ async function cmdUpdate() {
|
|
|
8373
7496
|
console.log("\u6B63\u5728\u66F4\u65B0 mihomo-cli...");
|
|
8374
7497
|
console.log("");
|
|
8375
7498
|
await new Promise((resolve, reject) => {
|
|
8376
|
-
const npm =
|
|
7499
|
+
const npm = spawn3("npm", ["install", "-g", PKG_NAME], { stdio: "inherit" });
|
|
8377
7500
|
npm.on("close", (code) => {
|
|
8378
7501
|
if (code === 0) {
|
|
8379
7502
|
resolve();
|
|
@@ -8410,7 +7533,7 @@ var COMMANDS = [
|
|
|
8410
7533
|
aliases: ["up"],
|
|
8411
7534
|
handler: cmdStart,
|
|
8412
7535
|
group: "control",
|
|
8413
|
-
usage: ["start [tun|mixed] [-s] [-u ms]
|
|
7536
|
+
usage: ["start [tun|mixed] [-s] [-u ms] [--no-ssh] \u542F\u52A8/\u5207\u6362\u4EE3\u7406 (\u9ED8\u8BA4 mixed)"]
|
|
8414
7537
|
},
|
|
8415
7538
|
{
|
|
8416
7539
|
name: "tun",
|
|
@@ -8467,34 +7590,9 @@ var COMMANDS = [
|
|
|
8467
7590
|
"subscription use <name> \u5207\u6362\u5F53\u524D\u8BA2\u9605",
|
|
8468
7591
|
"subscription add <url> [name] \u6DFB\u52A0\u8BA2\u9605",
|
|
8469
7592
|
"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]"
|
|
7593
|
+
"subscription remove <name> \u5220\u9664\u8BA2\u9605\uFF08\u6A21\u7CCA\u5339\u914D\u9700\u786E\u8BA4\uFF0C-y \u8DF3\u8FC7\uFF09"
|
|
8474
7594
|
]
|
|
8475
7595
|
},
|
|
8476
|
-
{
|
|
8477
|
-
name: "use",
|
|
8478
|
-
aliases: [],
|
|
8479
|
-
handler: cmdSubscription,
|
|
8480
|
-
rewrite: (args) => ["sub", "use", ...args.slice(1)],
|
|
8481
|
-
group: "subscription",
|
|
8482
|
-
usage: []
|
|
8483
|
-
},
|
|
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
7596
|
// === 配置 ===
|
|
8499
7597
|
{
|
|
8500
7598
|
name: "overwrite",
|
|
@@ -8503,36 +7601,12 @@ var COMMANDS = [
|
|
|
8503
7601
|
group: "config",
|
|
8504
7602
|
usage: ["overwrite \u67E5\u770B\u8986\u5199\u72B6\u6001\uFF08\u522B\u540D ow\uFF09", "overwrite on|off \u542F\u7528/\u7981\u7528\u8986\u5199\u914D\u7F6E"]
|
|
8505
7603
|
},
|
|
8506
|
-
{
|
|
8507
|
-
name: "on",
|
|
8508
|
-
aliases: [],
|
|
8509
|
-
handler: cmdOverwrite,
|
|
8510
|
-
rewrite: (args) => ["ow", "on", ...args.slice(1)],
|
|
8511
|
-
group: "config",
|
|
8512
|
-
usage: []
|
|
8513
|
-
},
|
|
8514
|
-
{
|
|
8515
|
-
name: "off",
|
|
8516
|
-
aliases: [],
|
|
8517
|
-
handler: cmdOverwrite,
|
|
8518
|
-
rewrite: (args) => ["ow", "off", ...args.slice(1)],
|
|
8519
|
-
group: "config",
|
|
8520
|
-
usage: []
|
|
8521
|
-
},
|
|
8522
7604
|
{
|
|
8523
7605
|
name: "directory",
|
|
8524
7606
|
aliases: ["dir", "dirs", "directories"],
|
|
8525
7607
|
handler: cmdDirectory,
|
|
8526
7608
|
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
|
|
8528
|
-
},
|
|
8529
|
-
{
|
|
8530
|
-
name: "open",
|
|
8531
|
-
aliases: [],
|
|
8532
|
-
handler: cmdDirectory,
|
|
8533
|
-
rewrite: (args) => ["dir", "open", ...args.slice(1)],
|
|
8534
|
-
group: "config",
|
|
8535
|
-
usage: []
|
|
7609
|
+
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"]
|
|
8536
7610
|
},
|
|
8537
7611
|
// === 系统 ===
|
|
8538
7612
|
{
|
|
@@ -8540,7 +7614,7 @@ var COMMANDS = [
|
|
|
8540
7614
|
aliases: [],
|
|
8541
7615
|
handler: cmdKernel,
|
|
8542
7616
|
group: "system",
|
|
8543
|
-
usage: ["kernel [--mirror [\u955C\u50CF]]
|
|
7617
|
+
usage: ["kernel [--mirror [\u955C\u50CF]] \u66F4\u65B0\u5185\u6838\uFF08\u9ED8\u8BA4\u76F4\u8FDE\uFF0C--mirror \u8D70\u955C\u50CF\u4E0B\u8F7D\uFF09"]
|
|
8544
7618
|
},
|
|
8545
7619
|
{
|
|
8546
7620
|
name: "daemon",
|
|
@@ -8566,7 +7640,7 @@ var COMMANDS = [
|
|
|
8566
7640
|
},
|
|
8567
7641
|
{
|
|
8568
7642
|
name: "update",
|
|
8569
|
-
aliases: [
|
|
7643
|
+
aliases: [],
|
|
8570
7644
|
handler: cmdUpdate,
|
|
8571
7645
|
group: "system",
|
|
8572
7646
|
usage: ["update \u66F4\u65B0 mihomo-cli (npm install -g)"]
|