mihomo-cli 3.12.0 → 4.0.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 +43 -0
- package/README.md +6 -63
- package/dist/index.js +111 -760
- 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 fs4 from "fs";
|
|
21
21
|
|
|
22
|
-
// node_modules/js-yaml/dist/js-yaml.mjs
|
|
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 {
|
|
@@ -3167,11 +3167,7 @@ var DIRS = {
|
|
|
3167
3167
|
subscriptions: path.join(USER_DATA_DIR, "subscriptions"),
|
|
3168
3168
|
logs: path.join(USER_DATA_DIR, "logs"),
|
|
3169
3169
|
data: path.join(USER_DATA_DIR, "data"),
|
|
3170
|
-
runtime: path.join(USER_DATA_DIR, "runtime")
|
|
3171
|
-
// ssh 隧道运行态。刻意独立于 runtime/:clearRuntime() 会在 stop() 成功路径 rmrf 整个
|
|
3172
|
-
// runtime 目录,隧道状态放那里会被 `mihomo stop` 连同 config.yaml 一起抹掉,
|
|
3173
|
-
// 于是「谁起的」标记丢失、手动起的隧道再也无法被识别
|
|
3174
|
-
ssh: path.join(USER_DATA_DIR, "ssh")
|
|
3170
|
+
runtime: path.join(USER_DATA_DIR, "runtime")
|
|
3175
3171
|
};
|
|
3176
3172
|
var PATHS = {
|
|
3177
3173
|
mihomoBinary: path.join(DIRS.kernel, "mihomo"),
|
|
@@ -3377,25 +3373,34 @@ function writeSubscriptionCache(cache) {
|
|
|
3377
3373
|
atomicWriteFileSync(PATHS.subscriptionsCacheFile, JSON.stringify(cache, null, 2), { mode: 384 });
|
|
3378
3374
|
}
|
|
3379
3375
|
function saveSubscriptionCache(subName, data) {
|
|
3380
|
-
|
|
3381
|
-
|
|
3382
|
-
|
|
3376
|
+
ensureDirs();
|
|
3377
|
+
withFileLock(PATHS.subscriptionsCacheFile, () => {
|
|
3378
|
+
const cache = readSubscriptionCache();
|
|
3379
|
+
cache[subName] = { ...cache[subName], ...data };
|
|
3380
|
+
writeSubscriptionCache(cache);
|
|
3381
|
+
});
|
|
3383
3382
|
}
|
|
3384
|
-
function
|
|
3385
|
-
|
|
3386
|
-
|
|
3387
|
-
|
|
3388
|
-
|
|
3389
|
-
|
|
3390
|
-
|
|
3391
|
-
}
|
|
3392
|
-
return list.filter(isValid);
|
|
3383
|
+
function deleteSubscriptionCache(subName) {
|
|
3384
|
+
ensureDirs();
|
|
3385
|
+
withFileLock(PATHS.subscriptionsCacheFile, () => {
|
|
3386
|
+
const cache = readSubscriptionCache();
|
|
3387
|
+
if (!cache[subName]) return;
|
|
3388
|
+
delete cache[subName];
|
|
3389
|
+
writeSubscriptionCache(cache);
|
|
3390
|
+
});
|
|
3393
3391
|
}
|
|
3394
3392
|
function isValidSubscription(s) {
|
|
3395
3393
|
return s != null && typeof s === "object" && typeof s.name === "string" && typeof s.url === "string";
|
|
3396
3394
|
}
|
|
3397
3395
|
function getSubscriptions() {
|
|
3398
|
-
|
|
3396
|
+
const list = readSettings().subscriptions;
|
|
3397
|
+
if (!Array.isArray(list)) {
|
|
3398
|
+
if (list !== void 0) {
|
|
3399
|
+
console.warn("\u8B66\u544A: settings.json \u7684 subscriptions \u4E0D\u662F\u5217\u8868\uFF0C\u5DF2\u5FFD\u7565\uFF08\u53EF\u7528 mihomo sub add \u91CD\u65B0\u6DFB\u52A0\uFF09");
|
|
3400
|
+
}
|
|
3401
|
+
return [];
|
|
3402
|
+
}
|
|
3403
|
+
return list.filter(isValidSubscription);
|
|
3399
3404
|
}
|
|
3400
3405
|
function getSubscriptionsWithCache() {
|
|
3401
3406
|
const subs = getSubscriptions();
|
|
@@ -3406,17 +3411,6 @@ function getSubscriptionsWithCache() {
|
|
|
3406
3411
|
}));
|
|
3407
3412
|
}
|
|
3408
3413
|
var SAFE_NAME_RE = /^[\w\-\p{Unified_Ideograph}]{1,64}$/u;
|
|
3409
|
-
function isValidSshConfig(t) {
|
|
3410
|
-
return t != null && typeof t === "object" && typeof t.name === "string" && typeof t.host === "string" && Number.isInteger(t.port);
|
|
3411
|
-
}
|
|
3412
|
-
function getSshTunnels() {
|
|
3413
|
-
return readSettingsList("ssh", isValidSshConfig, "\u53EF\u7528 mihomo ssh add \u91CD\u65B0\u6DFB\u52A0");
|
|
3414
|
-
}
|
|
3415
|
-
function validateSshName(name) {
|
|
3416
|
-
if (!name || !SAFE_NAME_RE.test(name)) {
|
|
3417
|
-
throw new CliError(`\u96A7\u9053\u540D\u79F0\u65E0\u6548: "${name}"\uFF0C\u53EA\u5141\u8BB8\u5B57\u6BCD\u3001\u6570\u5B57\u3001\u4E0B\u5212\u7EBF\u3001\u77ED\u6A2A\u7EBF\u548C\u4E2D\u6587\uFF08\u6700\u957F 64 \u5B57\u7B26\uFF09`);
|
|
3418
|
-
}
|
|
3419
|
-
}
|
|
3420
3414
|
function validateSubscriptionName(name) {
|
|
3421
3415
|
if (!name || !SAFE_NAME_RE.test(name)) {
|
|
3422
3416
|
throw new CliError(`\u8BA2\u9605\u540D\u79F0\u65E0\u6548: "${name}"\uFF0C\u53EA\u5141\u8BB8\u5B57\u6BCD\u3001\u6570\u5B57\u3001\u4E0B\u5212\u7EBF\u3001\u77ED\u6A2A\u7EBF\u548C\u4E2D\u6587\uFF08\u6700\u957F 64 \u5B57\u7B26\uFF09`);
|
|
@@ -3459,11 +3453,7 @@ function removeSubscription(name) {
|
|
|
3459
3453
|
return updates;
|
|
3460
3454
|
});
|
|
3461
3455
|
if (!found) return null;
|
|
3462
|
-
|
|
3463
|
-
if (cache[name]) {
|
|
3464
|
-
delete cache[name];
|
|
3465
|
-
writeSubscriptionCache(cache);
|
|
3466
|
-
}
|
|
3456
|
+
deleteSubscriptionCache(name);
|
|
3467
3457
|
try {
|
|
3468
3458
|
fs2.rmSync(getSubscriptionRawConfigPath(name), { force: true });
|
|
3469
3459
|
} catch {
|
|
@@ -3737,9 +3727,6 @@ function listOverwriteFile() {
|
|
|
3737
3727
|
|
|
3738
3728
|
// src/utils.ts
|
|
3739
3729
|
var sleepBuf = new Int32Array(new SharedArrayBuffer(4));
|
|
3740
|
-
function sleepSync(ms) {
|
|
3741
|
-
Atomics.wait(sleepBuf, 0, 0, ms);
|
|
3742
|
-
}
|
|
3743
3730
|
function sleep(ms) {
|
|
3744
3731
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
3745
3732
|
}
|
|
@@ -3819,27 +3806,21 @@ function parseIntArg(args, short, long, defaultValue) {
|
|
|
3819
3806
|
}
|
|
3820
3807
|
return defaultValue;
|
|
3821
3808
|
}
|
|
3822
|
-
function
|
|
3823
|
-
if (!args) return
|
|
3824
|
-
|
|
3825
|
-
|
|
3826
|
-
|
|
3827
|
-
|
|
3828
|
-
|
|
3829
|
-
|
|
3830
|
-
|
|
3831
|
-
|
|
3832
|
-
const value = args[i].slice(long.length + 1);
|
|
3833
|
-
if (!value) throw new CliError(`\u9009\u9879 ${long} \u7F3A\u5C11\u503C`, { hint: [`\u4F8B\u5982: ${long}=<\u503C>`] });
|
|
3834
|
-
return value;
|
|
3835
|
-
}
|
|
3836
|
-
}
|
|
3837
|
-
return null;
|
|
3809
|
+
function assertNoRemovedSshFlag(args) {
|
|
3810
|
+
if (!args?.some((a) => a === "--no-ssh" || a.startsWith("--no-ssh="))) return;
|
|
3811
|
+
throw new CliError("--no-ssh \u5DF2\u79FB\u9664\uFF08v4.0.0\uFF09", {
|
|
3812
|
+
label: "\u53C2\u6570\u9519\u8BEF",
|
|
3813
|
+
hint: [
|
|
3814
|
+
"ssh \u96A7\u9053\u529F\u80FD\u5DF2\u6574\u4F53\u79FB\u9664\uFF0C\u8BE5\u9009\u9879\u4E0D\u518D\u6709\u5BF9\u5E94\u884C\u4E3A\u3002",
|
|
3815
|
+
"\u5982\u4ECD\u9700\u5185\u7F51\u51FA\u53E3\uFF1A\u81EA\u884C\u8FD0\u884C ssh -D 127.0.0.1:<\u7AEF\u53E3> -N <\u4E3B\u673A>\uFF0C",
|
|
3816
|
+
"\u8282\u70B9\u4E0E\u5206\u6D41\u89C4\u5219\u5199\u5728 overwrite.yaml \u91CC\uFF08\u5199\u6CD5\u89C1 CHANGELOG \u7684 4.0.0 \u5347\u7EA7\u987B\u77E5\uFF09\u3002"
|
|
3817
|
+
]
|
|
3818
|
+
});
|
|
3838
3819
|
}
|
|
3839
|
-
var VALUE_FLAGS = /* @__PURE__ */ new Set(["-n", "--lines", "-u", "--update-timeout"
|
|
3820
|
+
var VALUE_FLAGS = /* @__PURE__ */ new Set(["-n", "--lines", "-u", "--update-timeout"]);
|
|
3840
3821
|
function extractStartOptions(args) {
|
|
3841
3822
|
if (!args) return [];
|
|
3842
|
-
const BOOL_FLAGS = /* @__PURE__ */ new Set(["-s", "--no-update"
|
|
3823
|
+
const BOOL_FLAGS = /* @__PURE__ */ new Set(["-s", "--no-update"]);
|
|
3843
3824
|
const out = [];
|
|
3844
3825
|
for (let i = 0; i < args.length; i++) {
|
|
3845
3826
|
const a = args[i];
|
|
@@ -4256,7 +4237,6 @@ function getConfigInfo() {
|
|
|
4256
4237
|
return {
|
|
4257
4238
|
proxies: proxies ? proxies.length : 0,
|
|
4258
4239
|
proxyGroups: proxyGroups ? proxyGroups.length : 0,
|
|
4259
|
-
mode: cfg.mode || "rule",
|
|
4260
4240
|
mixedPort: cfg["mixed-port"] || null,
|
|
4261
4241
|
httpPort: cfg.port || null,
|
|
4262
4242
|
socksPort: cfg["socks-port"] || null,
|
|
@@ -4343,7 +4323,6 @@ ${colors.cyan(colors.bold(`mihomo-cli v${VERSION}`))}`, "", "\u547D\u4EE4\u522B\
|
|
|
4343
4323
|
" mihomo start -s # \u8DF3\u8FC7\u81EA\u52A8\u66F4\u65B0\u8BA2\u9605",
|
|
4344
4324
|
" mihomo start -u 30000 # \u81EA\u52A8\u66F4\u65B0\u8D85\u65F6 30 \u79D2 (\u9ED8\u8BA4 10s)",
|
|
4345
4325
|
" mihomo daemon on # \u5F00\u542F\u4FDD\u6D3B\uFF08\u5F00\u673A\u81EA\u542F + \u5D29\u6E83\u91CD\u542F\uFF09",
|
|
4346
|
-
" mihomo ssh add work --host m4 --port 1080 # \u52A0 ssh \u96A7\u9053\u51FA\u53E3",
|
|
4347
4326
|
" mihomo sub add <url> # \u6DFB\u52A0\u8BA2\u9605 (sub \u662F subscription \u522B\u540D)",
|
|
4348
4327
|
" mihomo ui # \u6253\u5F00 Web UI",
|
|
4349
4328
|
"",
|
|
@@ -5192,7 +5171,7 @@ function killAllMihomo(forceSudo = false) {
|
|
|
5192
5171
|
}
|
|
5193
5172
|
}
|
|
5194
5173
|
}
|
|
5195
|
-
function cleanupAll(forceSudo = false) {
|
|
5174
|
+
async function cleanupAll(forceSudo = false) {
|
|
5196
5175
|
const pids = getMihomoPids();
|
|
5197
5176
|
if (pids.length === 0) {
|
|
5198
5177
|
clearPid();
|
|
@@ -5225,19 +5204,19 @@ function cleanupAll(forceSudo = false) {
|
|
|
5225
5204
|
}
|
|
5226
5205
|
for (let i = 0; i < PROCESS_WAIT_ATTEMPTS; i++) {
|
|
5227
5206
|
if (getMihomoPids().length === 0) break;
|
|
5228
|
-
|
|
5207
|
+
await sleep(PROCESS_WAIT_INTERVAL);
|
|
5229
5208
|
}
|
|
5230
5209
|
clearPid();
|
|
5231
5210
|
return { killed: killedCount, failed: failedPids.length, remaining: getMihomoPids() };
|
|
5232
5211
|
}
|
|
5233
|
-
function stop(forceSudo = false) {
|
|
5212
|
+
async function stop(forceSudo = false) {
|
|
5234
5213
|
const allPids = getMihomoPids();
|
|
5235
5214
|
if (allPids.length === 0) {
|
|
5236
5215
|
clearPid();
|
|
5237
5216
|
clearRuntime();
|
|
5238
5217
|
return { success: true, notRunning: true };
|
|
5239
5218
|
}
|
|
5240
|
-
const result = cleanupAll(forceSudo);
|
|
5219
|
+
const result = await cleanupAll(forceSudo);
|
|
5241
5220
|
const remaining = getMihomoPids();
|
|
5242
5221
|
if (remaining.length > 0) {
|
|
5243
5222
|
console.log("");
|
|
@@ -5330,7 +5309,7 @@ async function startMixedMode(staleState) {
|
|
|
5330
5309
|
console.log("\u6216\u8005\u5207\u6362\u5230 TUN \u6A21\u5F0F\uFF0C\u542F\u52A8\u65F6\u4F1A\u81EA\u52A8\u6E05\u7406");
|
|
5331
5310
|
throw new Error("\u5B58\u5728\u9700\u8981 root \u6743\u9650\u6E05\u7406\u7684\u6B8B\u7559");
|
|
5332
5311
|
}
|
|
5333
|
-
const cleanupResult = cleanupAll();
|
|
5312
|
+
const cleanupResult = await cleanupAll();
|
|
5334
5313
|
if (cleanupResult.killed > 0) {
|
|
5335
5314
|
console.log(`\u6E05\u7406\u4E86 ${cleanupResult.killed} \u4E2A\u6B8B\u7559\u8FDB\u7A0B`);
|
|
5336
5315
|
}
|
|
@@ -5428,363 +5407,6 @@ async function launchOrRestart(mode) {
|
|
|
5428
5407
|
return result.pid;
|
|
5429
5408
|
}
|
|
5430
5409
|
|
|
5431
|
-
// src/ssh.ts
|
|
5432
|
-
import { spawn as spawn2 } from "child_process";
|
|
5433
|
-
import fs11 from "fs";
|
|
5434
|
-
import net from "net";
|
|
5435
|
-
import path7 from "path";
|
|
5436
|
-
|
|
5437
|
-
// src/lifecycle.ts
|
|
5438
|
-
var cleanupFns = /* @__PURE__ */ new Set();
|
|
5439
|
-
var silentSigint = false;
|
|
5440
|
-
function setSilentSigint(value) {
|
|
5441
|
-
silentSigint = value;
|
|
5442
|
-
}
|
|
5443
|
-
function isSilentSigint() {
|
|
5444
|
-
return silentSigint;
|
|
5445
|
-
}
|
|
5446
|
-
function registerCleanup(fn) {
|
|
5447
|
-
cleanupFns.add(fn);
|
|
5448
|
-
return () => {
|
|
5449
|
-
cleanupFns.delete(fn);
|
|
5450
|
-
};
|
|
5451
|
-
}
|
|
5452
|
-
function runCleanup() {
|
|
5453
|
-
for (const fn of cleanupFns) {
|
|
5454
|
-
try {
|
|
5455
|
-
fn();
|
|
5456
|
-
} catch {
|
|
5457
|
-
}
|
|
5458
|
-
}
|
|
5459
|
-
cleanupFns.clear();
|
|
5460
|
-
}
|
|
5461
|
-
|
|
5462
|
-
// src/ssh.ts
|
|
5463
|
-
var PORT_PROBE_TIMEOUT_MS = 300;
|
|
5464
|
-
var STOP_WAIT_ATTEMPTS = 20;
|
|
5465
|
-
var STOP_WAIT_INTERVAL = 100;
|
|
5466
|
-
var START_WAIT_ATTEMPTS = 40;
|
|
5467
|
-
var START_WAIT_INTERVAL = 500;
|
|
5468
|
-
function getReservedPorts() {
|
|
5469
|
-
const reserved = /* @__PURE__ */ new Map();
|
|
5470
|
-
const mixedPort = Number(BASE_CONFIG["mixed-port"]);
|
|
5471
|
-
if (Number.isInteger(mixedPort)) reserved.set(mixedPort, "mihomo \u6DF7\u5408\u4EE3\u7406\u7AEF\u53E3");
|
|
5472
|
-
reserved.set(CONTROLLER_PORT, "mihomo \u63A7\u5236\u5668\u7AEF\u53E3");
|
|
5473
|
-
return reserved;
|
|
5474
|
-
}
|
|
5475
|
-
var SAFE_HOST_RE = /^[A-Za-z0-9][A-Za-z0-9._@-]*$/;
|
|
5476
|
-
function validateSshHost(host) {
|
|
5477
|
-
if (!host) {
|
|
5478
|
-
throw new CliError("\u7F3A\u5C11 --host", { hint: ["\u4F8B\u5982: mihomo ssh add work --host m4 --port 1080"] });
|
|
5479
|
-
}
|
|
5480
|
-
if (host.startsWith("-")) {
|
|
5481
|
-
throw new CliError(`\u4E3B\u673A\u540D\u65E0\u6548: "${host}"`, {
|
|
5482
|
-
label: "\u53C2\u6570\u9519\u8BEF",
|
|
5483
|
-
hint: ['\u4E3B\u673A\u540D\u4E0D\u80FD\u4EE5 "-" \u5F00\u5934\u2014\u2014\u5B83\u4F1A\u88AB ssh \u5F53\u4F5C\u9009\u9879\u89E3\u6790\uFF08\u5982 -oProxyCommand=... \u53EF\u6267\u884C\u4EFB\u610F\u547D\u4EE4\uFF09\u3002']
|
|
5484
|
-
});
|
|
5485
|
-
}
|
|
5486
|
-
if (!SAFE_HOST_RE.test(host)) {
|
|
5487
|
-
throw new CliError(`\u4E3B\u673A\u540D\u65E0\u6548: "${host}"\uFF0C\u53EA\u5141\u8BB8\u5B57\u6BCD\u3001\u6570\u5B57\u3001\u70B9\u3001\u4E0B\u5212\u7EBF\u3001\u77ED\u6A2A\u7EBF\u548C @`, {
|
|
5488
|
-
label: "\u53C2\u6570\u9519\u8BEF",
|
|
5489
|
-
hint: ["\u53EF\u7528 ssh \u522B\u540D\uFF08~/.ssh/config \u91CC\u7684 Host\uFF09\u6216 user@hostname\u3002"]
|
|
5490
|
-
});
|
|
5491
|
-
}
|
|
5492
|
-
}
|
|
5493
|
-
function validateSshPort(port, exclude) {
|
|
5494
|
-
if (!Number.isInteger(port) || port < 1 || port > 65535) {
|
|
5495
|
-
throw new CliError(`\u7AEF\u53E3\u65E0\u6548: ${port}\uFF0C\u9700\u4E3A 1-65535 \u7684\u6574\u6570`);
|
|
5496
|
-
}
|
|
5497
|
-
const reservedLabel = getReservedPorts().get(port);
|
|
5498
|
-
if (reservedLabel) {
|
|
5499
|
-
throw new CliError(`\u7AEF\u53E3 ${port} \u5DF2\u88AB ${reservedLabel} \u5360\u7528`, {
|
|
5500
|
-
label: "\u7AEF\u53E3\u51B2\u7A81",
|
|
5501
|
-
hint: ["\u8BF7\u6362\u4E00\u4E2A\u7AEF\u53E3\uFF0C\u4F8B\u5982 1080\u3002"]
|
|
5502
|
-
});
|
|
5503
|
-
}
|
|
5504
|
-
const conflict = getSshTunnels().find((t) => t.port === port && t.name !== exclude);
|
|
5505
|
-
if (conflict) {
|
|
5506
|
-
throw new CliError(`\u7AEF\u53E3 ${port} \u5DF2\u88AB\u96A7\u9053 "${conflict.name}" \u4F7F\u7528`, { label: "\u7AEF\u53E3\u51B2\u7A81" });
|
|
5507
|
-
}
|
|
5508
|
-
}
|
|
5509
|
-
function findSshTunnel(name) {
|
|
5510
|
-
return getSshTunnels().find((t) => t.name === name);
|
|
5511
|
-
}
|
|
5512
|
-
function addSshTunnel(config) {
|
|
5513
|
-
validateSshName(config.name);
|
|
5514
|
-
validateSshHost(config.host);
|
|
5515
|
-
validateSshPort(config.port);
|
|
5516
|
-
let duplicate = false;
|
|
5517
|
-
updateSettings(() => {
|
|
5518
|
-
const tunnels = [...getSshTunnels()];
|
|
5519
|
-
if (tunnels.some((t) => t.name === config.name)) {
|
|
5520
|
-
duplicate = true;
|
|
5521
|
-
return {};
|
|
5522
|
-
}
|
|
5523
|
-
tunnels.push(config);
|
|
5524
|
-
return { ssh: tunnels };
|
|
5525
|
-
});
|
|
5526
|
-
if (duplicate) {
|
|
5527
|
-
throw new CliError(`\u96A7\u9053 "${config.name}" \u5DF2\u5B58\u5728\uFF0C\u8BF7\u6362\u4E2A\u540D\u79F0\uFF0C\u6216\u5148\u5220\u9664\uFF08mihomo ssh rm ${config.name}\uFF09`);
|
|
5528
|
-
}
|
|
5529
|
-
}
|
|
5530
|
-
function removeSshTunnel(name) {
|
|
5531
|
-
let found = false;
|
|
5532
|
-
updateSettings(() => {
|
|
5533
|
-
const tunnels = [...getSshTunnels()];
|
|
5534
|
-
const idx = tunnels.findIndex((t) => t.name === name);
|
|
5535
|
-
if (idx < 0) return {};
|
|
5536
|
-
found = true;
|
|
5537
|
-
tunnels.splice(idx, 1);
|
|
5538
|
-
return { ssh: tunnels };
|
|
5539
|
-
});
|
|
5540
|
-
if (!found) return false;
|
|
5541
|
-
clearSshRuntime(name);
|
|
5542
|
-
return true;
|
|
5543
|
-
}
|
|
5544
|
-
function getSshRuntimePath(name) {
|
|
5545
|
-
validateSshName(name);
|
|
5546
|
-
return path7.join(DIRS.ssh, `${name}.json`);
|
|
5547
|
-
}
|
|
5548
|
-
function readSshRuntime(name) {
|
|
5549
|
-
try {
|
|
5550
|
-
const raw = fs11.readFileSync(getSshRuntimePath(name), "utf8");
|
|
5551
|
-
const parsed = JSON.parse(raw);
|
|
5552
|
-
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) return null;
|
|
5553
|
-
const r = parsed;
|
|
5554
|
-
if (!Number.isInteger(r.pid) || r.pid <= 0) return null;
|
|
5555
|
-
return {
|
|
5556
|
-
pid: r.pid,
|
|
5557
|
-
started_by: r.started_by === "manual" ? "manual" : "auto",
|
|
5558
|
-
started_at: typeof r.started_at === "string" ? r.started_at : "",
|
|
5559
|
-
port: Number.isInteger(r.port) ? r.port : 0
|
|
5560
|
-
};
|
|
5561
|
-
} catch {
|
|
5562
|
-
return null;
|
|
5563
|
-
}
|
|
5564
|
-
}
|
|
5565
|
-
function writeSshRuntime(name, runtime) {
|
|
5566
|
-
ensureDirs();
|
|
5567
|
-
atomicWriteFileSync(getSshRuntimePath(name), JSON.stringify(runtime, null, 2), { mode: 384 });
|
|
5568
|
-
}
|
|
5569
|
-
function clearSshRuntime(name) {
|
|
5570
|
-
try {
|
|
5571
|
-
fs11.rmSync(getSshRuntimePath(name), { force: true });
|
|
5572
|
-
} catch {
|
|
5573
|
-
}
|
|
5574
|
-
}
|
|
5575
|
-
function buildSshArgs(tunnel) {
|
|
5576
|
-
return [
|
|
5577
|
-
"-D",
|
|
5578
|
-
`127.0.0.1:${tunnel.port}`,
|
|
5579
|
-
"-N",
|
|
5580
|
-
"-o",
|
|
5581
|
-
"ExitOnForwardFailure=yes",
|
|
5582
|
-
"-o",
|
|
5583
|
-
"BatchMode=yes",
|
|
5584
|
-
"-o",
|
|
5585
|
-
"ServerAliveInterval=30",
|
|
5586
|
-
"-o",
|
|
5587
|
-
"ServerAliveCountMax=3",
|
|
5588
|
-
"-o",
|
|
5589
|
-
"ConnectTimeout=15",
|
|
5590
|
-
tunnel.host
|
|
5591
|
-
];
|
|
5592
|
-
}
|
|
5593
|
-
function commandNeedle(port) {
|
|
5594
|
-
return `-D 127.0.0.1:${port}`;
|
|
5595
|
-
}
|
|
5596
|
-
function getSshLogPath(name) {
|
|
5597
|
-
validateSshName(name);
|
|
5598
|
-
return path7.join(DIRS.logs, `ssh-${name}.log`);
|
|
5599
|
-
}
|
|
5600
|
-
function isSshProcessAlive(pid, port) {
|
|
5601
|
-
return isProcessRunning(pid) && isProcessCommandMatching(pid, commandNeedle(port));
|
|
5602
|
-
}
|
|
5603
|
-
function isPortListening(port, timeoutMs = PORT_PROBE_TIMEOUT_MS) {
|
|
5604
|
-
return new Promise((resolve) => {
|
|
5605
|
-
const socket = new net.Socket();
|
|
5606
|
-
let settled = false;
|
|
5607
|
-
const finish = (result) => {
|
|
5608
|
-
if (settled) return;
|
|
5609
|
-
settled = true;
|
|
5610
|
-
socket.destroy();
|
|
5611
|
-
resolve(result);
|
|
5612
|
-
};
|
|
5613
|
-
socket.setTimeout(timeoutMs);
|
|
5614
|
-
socket.once("connect", () => finish(true));
|
|
5615
|
-
socket.once("timeout", () => finish(false));
|
|
5616
|
-
socket.once("error", () => finish(false));
|
|
5617
|
-
socket.connect(port, "127.0.0.1");
|
|
5618
|
-
});
|
|
5619
|
-
}
|
|
5620
|
-
async function getSshStatus(config) {
|
|
5621
|
-
const runtime = readSshRuntime(config.name);
|
|
5622
|
-
const alive = runtime !== null && isSshProcessAlive(runtime.pid, runtime.port || config.port);
|
|
5623
|
-
if (!alive) {
|
|
5624
|
-
return { config, state: "stopped", pid: null, started_by: null, started_at: null };
|
|
5625
|
-
}
|
|
5626
|
-
const listening = await isPortListening(config.port);
|
|
5627
|
-
return {
|
|
5628
|
-
config,
|
|
5629
|
-
state: listening ? "running" : "dead-port",
|
|
5630
|
-
pid: runtime.pid,
|
|
5631
|
-
started_by: runtime.started_by,
|
|
5632
|
-
started_at: runtime.started_at || null
|
|
5633
|
-
};
|
|
5634
|
-
}
|
|
5635
|
-
async function getAllSshStatus() {
|
|
5636
|
-
return Promise.all(getSshTunnels().map(getSshStatus));
|
|
5637
|
-
}
|
|
5638
|
-
async function startSshTunnel(name, options) {
|
|
5639
|
-
const config = findSshTunnel(name);
|
|
5640
|
-
if (!config) {
|
|
5641
|
-
throw new CliError(`\u672A\u627E\u5230\u96A7\u9053 "${name}"`, { hint: ["\u67E5\u770B\u5168\u90E8\u96A7\u9053: mihomo ssh"] });
|
|
5642
|
-
}
|
|
5643
|
-
const existing = readSshRuntime(name);
|
|
5644
|
-
if (existing && isSshProcessAlive(existing.pid, existing.port || config.port)) {
|
|
5645
|
-
if (options.startedBy === "manual" && existing.started_by === "auto") {
|
|
5646
|
-
writeSshRuntime(name, { ...existing, started_by: "manual" });
|
|
5647
|
-
}
|
|
5648
|
-
return { alreadyRunning: true, pid: existing.pid };
|
|
5649
|
-
}
|
|
5650
|
-
if (existing) clearSshRuntime(name);
|
|
5651
|
-
if (await isPortListening(config.port)) {
|
|
5652
|
-
throw new CliError(`\u7AEF\u53E3 ${config.port} \u5DF2\u88AB\u5360\u7528`, {
|
|
5653
|
-
label: "\u65E0\u6CD5\u542F\u52A8\u96A7\u9053",
|
|
5654
|
-
hint: [`\u53EF\u80FD\u5DF2\u6709\u96A7\u9053\u6216\u5176\u4ED6\u7A0B\u5E8F\u5728\u76D1\u542C 127.0.0.1:${config.port}\u3002`, `\u67E5\u770B\u5360\u7528: lsof -nP -iTCP:${config.port} -sTCP:LISTEN`]
|
|
5655
|
-
});
|
|
5656
|
-
}
|
|
5657
|
-
ensureDirs();
|
|
5658
|
-
const logPath = getSshLogPath(name);
|
|
5659
|
-
const logFd = fs11.openSync(logPath, "w");
|
|
5660
|
-
const child = spawn2("ssh", buildSshArgs(config), {
|
|
5661
|
-
detached: true,
|
|
5662
|
-
stdio: ["ignore", logFd, logFd]
|
|
5663
|
-
});
|
|
5664
|
-
child.on("error", () => {
|
|
5665
|
-
});
|
|
5666
|
-
fs11.closeSync(logFd);
|
|
5667
|
-
child.unref();
|
|
5668
|
-
const pid = child.pid;
|
|
5669
|
-
if (!pid) {
|
|
5670
|
-
throw new CliError("\u65E0\u6CD5\u521B\u5EFA ssh \u8FDB\u7A0B", { label: "\u542F\u52A8\u96A7\u9053\u5931\u8D25", hint: ["\u8BF7\u786E\u8BA4\u5DF2\u5B89\u88C5 ssh \u5BA2\u6237\u7AEF\u3002"] });
|
|
5671
|
-
}
|
|
5672
|
-
writeSshRuntime(name, {
|
|
5673
|
-
pid,
|
|
5674
|
-
started_by: options.startedBy,
|
|
5675
|
-
started_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
5676
|
-
port: config.port
|
|
5677
|
-
});
|
|
5678
|
-
const unregisterCleanup = registerCleanup(() => {
|
|
5679
|
-
try {
|
|
5680
|
-
process.kill(pid, "SIGKILL");
|
|
5681
|
-
} catch {
|
|
5682
|
-
}
|
|
5683
|
-
clearSshRuntime(name);
|
|
5684
|
-
});
|
|
5685
|
-
try {
|
|
5686
|
-
for (let i = 0; i < START_WAIT_ATTEMPTS; i++) {
|
|
5687
|
-
if (!isProcessRunning(pid)) break;
|
|
5688
|
-
if (await isPortListening(config.port)) {
|
|
5689
|
-
unregisterCleanup();
|
|
5690
|
-
return { alreadyRunning: false, pid };
|
|
5691
|
-
}
|
|
5692
|
-
await new Promise((resolve) => setTimeout(resolve, START_WAIT_INTERVAL));
|
|
5693
|
-
}
|
|
5694
|
-
} finally {
|
|
5695
|
-
unregisterCleanup();
|
|
5696
|
-
}
|
|
5697
|
-
clearSshRuntime(name);
|
|
5698
|
-
if (isProcessRunning(pid)) {
|
|
5699
|
-
try {
|
|
5700
|
-
process.kill(pid, "SIGKILL");
|
|
5701
|
-
} catch {
|
|
5702
|
-
}
|
|
5703
|
-
}
|
|
5704
|
-
throw new CliError("ssh \u96A7\u9053\u672A\u80FD\u5EFA\u7ACB", {
|
|
5705
|
-
label: "\u542F\u52A8\u96A7\u9053\u5931\u8D25",
|
|
5706
|
-
hint: [...readLogTail(logPath), `\u5B8C\u6574\u65E5\u5FD7: ${logPath}`]
|
|
5707
|
-
});
|
|
5708
|
-
}
|
|
5709
|
-
function readLogTail(logPath, maxLines = 5) {
|
|
5710
|
-
try {
|
|
5711
|
-
const content = fs11.readFileSync(logPath, "utf8").trim();
|
|
5712
|
-
if (!content) return [];
|
|
5713
|
-
return content.split("\n").slice(-maxLines).map((line) => ` ${line.trim()}`);
|
|
5714
|
-
} catch {
|
|
5715
|
-
return [];
|
|
5716
|
-
}
|
|
5717
|
-
}
|
|
5718
|
-
function stopSshTunnel(name) {
|
|
5719
|
-
const config = findSshTunnel(name);
|
|
5720
|
-
const runtime = readSshRuntime(name);
|
|
5721
|
-
if (!runtime) {
|
|
5722
|
-
clearSshRuntime(name);
|
|
5723
|
-
return { notRunning: true, pid: null };
|
|
5724
|
-
}
|
|
5725
|
-
const port = runtime.port || config?.port || 0;
|
|
5726
|
-
const { pid } = runtime;
|
|
5727
|
-
if (!isSshProcessAlive(pid, port)) {
|
|
5728
|
-
clearSshRuntime(name);
|
|
5729
|
-
return { notRunning: true, pid: null };
|
|
5730
|
-
}
|
|
5731
|
-
try {
|
|
5732
|
-
process.kill(pid, "SIGTERM");
|
|
5733
|
-
} catch {
|
|
5734
|
-
}
|
|
5735
|
-
for (let i = 0; i < STOP_WAIT_ATTEMPTS; i++) {
|
|
5736
|
-
if (!isProcessRunning(pid)) break;
|
|
5737
|
-
sleepSync(STOP_WAIT_INTERVAL);
|
|
5738
|
-
}
|
|
5739
|
-
if (isProcessRunning(pid) && isProcessCommandMatching(pid, commandNeedle(port))) {
|
|
5740
|
-
try {
|
|
5741
|
-
process.kill(pid, "SIGKILL");
|
|
5742
|
-
} catch {
|
|
5743
|
-
}
|
|
5744
|
-
for (let i = 0; i < STOP_WAIT_ATTEMPTS; i++) {
|
|
5745
|
-
if (!isProcessRunning(pid)) break;
|
|
5746
|
-
sleepSync(STOP_WAIT_INTERVAL);
|
|
5747
|
-
}
|
|
5748
|
-
}
|
|
5749
|
-
clearSshRuntime(name);
|
|
5750
|
-
return { notRunning: false, pid };
|
|
5751
|
-
}
|
|
5752
|
-
async function startAutoSshTunnels() {
|
|
5753
|
-
const outcomes = [];
|
|
5754
|
-
for (const tunnel of getSshTunnels()) {
|
|
5755
|
-
if (!tunnel.auto) continue;
|
|
5756
|
-
try {
|
|
5757
|
-
const result = await startSshTunnel(tunnel.name, { startedBy: "auto" });
|
|
5758
|
-
outcomes.push({ name: tunnel.name, ok: true, alreadyRunning: result.alreadyRunning });
|
|
5759
|
-
} catch (e) {
|
|
5760
|
-
outcomes.push({
|
|
5761
|
-
name: tunnel.name,
|
|
5762
|
-
ok: false,
|
|
5763
|
-
error: e instanceof CliError ? e : new CliError(e.message)
|
|
5764
|
-
});
|
|
5765
|
-
}
|
|
5766
|
-
}
|
|
5767
|
-
return outcomes;
|
|
5768
|
-
}
|
|
5769
|
-
function stopAutoSshTunnels() {
|
|
5770
|
-
const stopped = [];
|
|
5771
|
-
for (const tunnel of getSshTunnels()) {
|
|
5772
|
-
const runtime = readSshRuntime(tunnel.name);
|
|
5773
|
-
if (runtime?.started_by !== "auto") continue;
|
|
5774
|
-
const result = stopSshTunnel(tunnel.name);
|
|
5775
|
-
if (!result.notRunning) stopped.push(tunnel.name);
|
|
5776
|
-
}
|
|
5777
|
-
return stopped;
|
|
5778
|
-
}
|
|
5779
|
-
function stopAllSshTunnels() {
|
|
5780
|
-
const stopped = [];
|
|
5781
|
-
for (const tunnel of getSshTunnels()) {
|
|
5782
|
-
const result = stopSshTunnel(tunnel.name);
|
|
5783
|
-
if (!result.notRunning) stopped.push(tunnel.name);
|
|
5784
|
-
}
|
|
5785
|
-
return stopped;
|
|
5786
|
-
}
|
|
5787
|
-
|
|
5788
5410
|
// src/commands/status.ts
|
|
5789
5411
|
async function printStatus() {
|
|
5790
5412
|
const state = getRunningState();
|
|
@@ -5848,17 +5470,6 @@ async function printStatus() {
|
|
|
5848
5470
|
if (isDaemonEnabled()) {
|
|
5849
5471
|
console.log(`${colors.gray("\u4FDD\u6D3B: ")}${colors.green("\u5DF2\u542F\u7528")} ${colors.gray("(\u5F00\u673A\u81EA\u542F + \u5D29\u6E83\u91CD\u542F)")}`);
|
|
5850
5472
|
}
|
|
5851
|
-
const tunnels = getSshTunnels();
|
|
5852
|
-
if (tunnels.length > 0) {
|
|
5853
|
-
const statuses = await getAllSshStatus();
|
|
5854
|
-
const parts = statuses.map((s) => {
|
|
5855
|
-
const label = `${s.config.name}:${s.config.port}`;
|
|
5856
|
-
if (s.state === "running") return colors.green(label);
|
|
5857
|
-
if (s.state === "dead-port") return colors.yellow(`${label} \u5047\u6D3B`);
|
|
5858
|
-
return colors.gray(`${label} \u672A\u8FD0\u884C`);
|
|
5859
|
-
});
|
|
5860
|
-
console.log(`${colors.gray("\u96A7\u9053: ")}${parts.join(", ")}`);
|
|
5861
|
-
}
|
|
5862
5473
|
console.log("");
|
|
5863
5474
|
}
|
|
5864
5475
|
|
|
@@ -5868,51 +5479,24 @@ function handleStopResult(result) {
|
|
|
5868
5479
|
throw new CliError(result.remaining.join(", "), { label: "\u90E8\u5206\u8FDB\u7A0B\u672A\u7EC8\u6B62", hint: "\u8BF7\u624B\u52A8\u8FD0\u884C: sudo pkill -9 mihomo" });
|
|
5869
5480
|
}
|
|
5870
5481
|
}
|
|
5871
|
-
function stopAutoSshTunnelsWithLog() {
|
|
5872
|
-
const stopped = stopAutoSshTunnels();
|
|
5873
|
-
if (stopped.length > 0) {
|
|
5874
|
-
console.log(`${colors.green("\u5DF2\u505C\u6B62\u96A7\u9053")}: ${stopped.join(", ")}`);
|
|
5875
|
-
}
|
|
5876
|
-
}
|
|
5877
5482
|
async function cmdStop(args) {
|
|
5483
|
+
assertNoRemovedSshFlag(args);
|
|
5878
5484
|
if (isDaemonEnabled()) {
|
|
5879
5485
|
console.log(colors.yellow("\u4FDD\u6D3B\u5DF2\u542F\u7528\uFF0C\u4EE3\u7406\u7531 launchd \u6258\u7BA1"));
|
|
5880
5486
|
console.log("\u76F4\u63A5\u505C\u6B62\u4F1A\u88AB\u81EA\u52A8\u91CD\u65B0\u62C9\u8D77\uFF0C\u8BF7\u7528: mihomo daemon off");
|
|
5881
5487
|
return;
|
|
5882
5488
|
}
|
|
5883
|
-
const skipSsh = hasFlag(args, "--no-ssh");
|
|
5884
5489
|
const pids = getMihomoPids();
|
|
5885
5490
|
if (pids.length === 0) {
|
|
5886
5491
|
console.log(colors.yellow("\u4E0D\u5728\u8FD0\u884C"));
|
|
5887
|
-
if (!skipSsh) stopAutoSshTunnelsWithLog();
|
|
5888
5492
|
return;
|
|
5889
5493
|
}
|
|
5890
5494
|
console.log(`\u505C\u6B62 ${pids.length} \u4E2A\u8FDB\u7A0B...`);
|
|
5891
|
-
handleStopResult(stop());
|
|
5495
|
+
handleStopResult(await stop());
|
|
5892
5496
|
console.log(colors.green("\u5DF2\u505C\u6B62\u8FDB\u7A0B"));
|
|
5893
|
-
if (!skipSsh) stopAutoSshTunnelsWithLog();
|
|
5894
5497
|
}
|
|
5895
5498
|
|
|
5896
5499
|
// src/commands/start.ts
|
|
5897
|
-
async function startAutoSshTunnelsWithWarning() {
|
|
5898
|
-
const outcomes = await startAutoSshTunnels();
|
|
5899
|
-
if (outcomes.length === 0) return;
|
|
5900
|
-
const started = outcomes.filter((o) => o.ok && !o.alreadyRunning);
|
|
5901
|
-
if (started.length > 0) {
|
|
5902
|
-
console.log(`${colors.green("\u5DF2\u542F\u52A8\u96A7\u9053")}: ${started.map((o) => o.name).join(", ")}`);
|
|
5903
|
-
}
|
|
5904
|
-
for (const failed of outcomes.filter((o) => !o.ok)) {
|
|
5905
|
-
console.log("");
|
|
5906
|
-
console.log(colors.yellow(`\u8B66\u544A: \u96A7\u9053 "${failed.name}" \u542F\u52A8\u5931\u8D25`));
|
|
5907
|
-
console.log(colors.gray(` ${failed.error?.message ?? "\u672A\u77E5\u9519\u8BEF"}`));
|
|
5908
|
-
for (const line of failed.error?.hint ?? []) {
|
|
5909
|
-
if (line.trim()) console.log(colors.gray(line.startsWith(" ") ? line : ` ${line}`));
|
|
5910
|
-
}
|
|
5911
|
-
console.log(colors.gray(" \u5185\u7F51\u5206\u6D41\u89C4\u5219\u5C06\u4E0D\u53EF\u7528\uFF0C\u5176\u4F59\u6D41\u91CF\u6B63\u5E38"));
|
|
5912
|
-
console.log(colors.gray(` \u6392\u67E5: mihomo ssh status ${failed.name}`));
|
|
5913
|
-
console.log("");
|
|
5914
|
-
}
|
|
5915
|
-
}
|
|
5916
5500
|
function resolveStartMode(args) {
|
|
5917
5501
|
const modeArg = getNonFlagArg(args, 1);
|
|
5918
5502
|
const modeToken = modeArg?.toLowerCase();
|
|
@@ -5922,6 +5506,7 @@ function resolveStartMode(args) {
|
|
|
5922
5506
|
return modeToken === "tun" ? "tun" : "mixed";
|
|
5923
5507
|
}
|
|
5924
5508
|
async function cmdStart(args) {
|
|
5509
|
+
assertNoRemovedSshFlag(args);
|
|
5925
5510
|
const targetMode = resolveStartMode(args);
|
|
5926
5511
|
if (!hasKernel()) {
|
|
5927
5512
|
throw new CliError('\u672A\u627E\u5230\u5185\u6838\uFF0C\u8BF7\u8FD0\u884C "mihomo kernel"');
|
|
@@ -5931,7 +5516,6 @@ async function cmdStart(args) {
|
|
|
5931
5516
|
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" });
|
|
5932
5517
|
}
|
|
5933
5518
|
const skipUpdate = hasFlag(args, "-s", "--no-update");
|
|
5934
|
-
const skipSsh = hasFlag(args, "--no-ssh");
|
|
5935
5519
|
const updateTimeout = parseIntArg(args, "-u", "--update-timeout", DEFAULT_AUTO_UPDATE_TIMEOUT);
|
|
5936
5520
|
const sub = requireActiveSubscription("\u6CA1\u6709\u8BA2\u9605\uFF0C\u8BF7\u5148\u6DFB\u52A0\u8BA2\u9605");
|
|
5937
5521
|
if (!skipUpdate) {
|
|
@@ -5956,7 +5540,7 @@ async function cmdStart(args) {
|
|
|
5956
5540
|
const count = status.allProcesses.length > 0 ? status.allProcesses.length : 1;
|
|
5957
5541
|
console.log(`\u505C\u6B62 ${count} \u4E2A\u8FDB\u7A0B...`);
|
|
5958
5542
|
}
|
|
5959
|
-
handleStopResult(stop());
|
|
5543
|
+
handleStopResult(await stop());
|
|
5960
5544
|
if (hasProcess) {
|
|
5961
5545
|
console.log(`${colors.green("\u5DF2\u505C\u6B62\u8FDB\u7A0B")}
|
|
5962
5546
|
`);
|
|
@@ -5974,9 +5558,6 @@ async function cmdStart(args) {
|
|
|
5974
5558
|
const lines = e.message.split("\n");
|
|
5975
5559
|
throw new CliError(lines[0], { label: "\u542F\u52A8\u5931\u8D25", hint: lines.slice(1) });
|
|
5976
5560
|
}
|
|
5977
|
-
if (!skipSsh) {
|
|
5978
|
-
await startAutoSshTunnelsWithWarning();
|
|
5979
|
-
}
|
|
5980
5561
|
await printStatus();
|
|
5981
5562
|
}
|
|
5982
5563
|
|
|
@@ -6105,10 +5686,31 @@ async function cmdDaemon(args) {
|
|
|
6105
5686
|
}
|
|
6106
5687
|
|
|
6107
5688
|
// src/open.ts
|
|
6108
|
-
import { spawn as
|
|
5689
|
+
import { spawn as spawn2 } from "child_process";
|
|
5690
|
+
|
|
5691
|
+
// src/lifecycle.ts
|
|
5692
|
+
var cleanupFns = /* @__PURE__ */ new Set();
|
|
5693
|
+
var silentSigint = false;
|
|
5694
|
+
function setSilentSigint(value) {
|
|
5695
|
+
silentSigint = value;
|
|
5696
|
+
}
|
|
5697
|
+
function isSilentSigint() {
|
|
5698
|
+
return silentSigint;
|
|
5699
|
+
}
|
|
5700
|
+
function runCleanup() {
|
|
5701
|
+
for (const fn of cleanupFns) {
|
|
5702
|
+
try {
|
|
5703
|
+
fn();
|
|
5704
|
+
} catch {
|
|
5705
|
+
}
|
|
5706
|
+
}
|
|
5707
|
+
cleanupFns.clear();
|
|
5708
|
+
}
|
|
5709
|
+
|
|
5710
|
+
// src/open.ts
|
|
6109
5711
|
function openUrl(url) {
|
|
6110
5712
|
try {
|
|
6111
|
-
const child =
|
|
5713
|
+
const child = spawn2("open", ["--", url], { stdio: "ignore", detached: true });
|
|
6112
5714
|
child.unref();
|
|
6113
5715
|
child.on("error", () => {
|
|
6114
5716
|
});
|
|
@@ -6139,7 +5741,7 @@ function viewLogWithTail(logPath, options) {
|
|
|
6139
5741
|
if (follow) tailArgs.push("-f");
|
|
6140
5742
|
tailArgs.push("-n", lines.toString());
|
|
6141
5743
|
tailArgs.push(logPath);
|
|
6142
|
-
const tail =
|
|
5744
|
+
const tail = spawn2("tail", tailArgs, { stdio: "inherit" });
|
|
6143
5745
|
if (follow) setSilentSigint(true);
|
|
6144
5746
|
tail.on("close", (code, signal) => process.exit(signal ? 0 : code ?? 0));
|
|
6145
5747
|
tail.on("error", (e) => {
|
|
@@ -6209,10 +5811,10 @@ async function cmdDirectory(args) {
|
|
|
6209
5811
|
|
|
6210
5812
|
// src/kernel.ts
|
|
6211
5813
|
import { spawnSync as spawnSync5 } from "child_process";
|
|
6212
|
-
import
|
|
6213
|
-
import
|
|
5814
|
+
import fs11 from "fs";
|
|
5815
|
+
import path7 from "path";
|
|
6214
5816
|
|
|
6215
|
-
// node_modules/compare-versions/lib/esm/utils.js
|
|
5817
|
+
// ../../../node_modules/compare-versions/lib/esm/utils.js
|
|
6216
5818
|
var semver = /^[v^~<>=]*?(\d+)(?:\.([x*]|\d+)(?:\.([x*]|\d+)(?:\.([x*]|\d+))?(?:-([\da-z\-]+(?:\.[\da-z\-]+)*))?(?:\+[\da-z\-]+(?:\.[\da-z\-]+)*)?)?)?$/i;
|
|
6217
5819
|
var validateAndParse = (version) => {
|
|
6218
5820
|
if (typeof version !== "string") {
|
|
@@ -6250,7 +5852,7 @@ var compareSegments = (a, b) => {
|
|
|
6250
5852
|
return 0;
|
|
6251
5853
|
};
|
|
6252
5854
|
|
|
6253
|
-
// node_modules/compare-versions/lib/esm/compareVersions.js
|
|
5855
|
+
// ../../../node_modules/compare-versions/lib/esm/compareVersions.js
|
|
6254
5856
|
var compareVersions = (v1, v2) => {
|
|
6255
5857
|
const n1 = validateAndParse(v1);
|
|
6256
5858
|
const n2 = validateAndParse(v2);
|
|
@@ -6353,10 +5955,10 @@ async function checkUpdate() {
|
|
|
6353
5955
|
}
|
|
6354
5956
|
function findBinaryInDir(dir, maxDepth = 4) {
|
|
6355
5957
|
if (maxDepth <= 0) return null;
|
|
6356
|
-
const files =
|
|
5958
|
+
const files = fs11.readdirSync(dir);
|
|
6357
5959
|
for (const f of files) {
|
|
6358
|
-
const fullPath =
|
|
6359
|
-
const stat =
|
|
5960
|
+
const fullPath = path7.join(dir, f);
|
|
5961
|
+
const stat = fs11.lstatSync(fullPath);
|
|
6360
5962
|
if (stat.isDirectory()) {
|
|
6361
5963
|
const found = findBinaryInDir(fullPath, maxDepth - 1);
|
|
6362
5964
|
if (found) return found;
|
|
@@ -6384,7 +5986,7 @@ async function downloadKernel(progressCallback, mirror, releaseInfo) {
|
|
|
6384
5986
|
}
|
|
6385
5987
|
assertTrustedAssetUrl(asset.browser_download_url);
|
|
6386
5988
|
const downloadUrl = withMirror(asset.browser_download_url, mirror);
|
|
6387
|
-
const tempPath =
|
|
5989
|
+
const tempPath = path7.join(DIRS.kernel, path7.basename(asset.name));
|
|
6388
5990
|
const sizeMB = (asset.size / 1024 / 1024).toFixed(2);
|
|
6389
5991
|
if (mirror && progressCallback) {
|
|
6390
5992
|
progressCallback("\u63D0\u793A: \u7ECF\u7B2C\u4E09\u65B9\u955C\u50CF\u4E2D\u8F6C\u4E0B\u8F7D\uFF0C\u65E0\u6CD5\u9A8C\u8BC1\u6765\u6E90\u5B8C\u6574\u6027\uFF0C\u5EFA\u8BAE\u76F4\u8FDE\u6216\u81EA\u884C\u6821\u9A8C\u4EA7\u7269");
|
|
@@ -6422,19 +6024,19 @@ async function downloadKernel(progressCallback, mirror, releaseInfo) {
|
|
|
6422
6024
|
}
|
|
6423
6025
|
if (curlResult.status !== 0) {
|
|
6424
6026
|
try {
|
|
6425
|
-
|
|
6027
|
+
fs11.unlinkSync(tempPath);
|
|
6426
6028
|
} catch {
|
|
6427
6029
|
}
|
|
6428
6030
|
throw new Error(`\u4E0B\u8F7D\u5931\u8D25 (curl \u9000\u51FA\u7801 ${curlResult.status})`);
|
|
6429
6031
|
}
|
|
6430
|
-
if (!
|
|
6032
|
+
if (!fs11.existsSync(tempPath)) {
|
|
6431
6033
|
throw new Error("\u4E0B\u8F7D\u5931\u8D25: \u6587\u4EF6\u672A\u751F\u6210");
|
|
6432
6034
|
}
|
|
6433
6035
|
if (Number.isFinite(asset.size) && asset.size > 0) {
|
|
6434
|
-
const actual =
|
|
6036
|
+
const actual = fs11.statSync(tempPath).size;
|
|
6435
6037
|
if (actual !== asset.size) {
|
|
6436
6038
|
try {
|
|
6437
|
-
|
|
6039
|
+
fs11.unlinkSync(tempPath);
|
|
6438
6040
|
} catch {
|
|
6439
6041
|
}
|
|
6440
6042
|
throw new Error(
|
|
@@ -6474,17 +6076,17 @@ async function downloadKernel(progressCallback, mirror, releaseInfo) {
|
|
|
6474
6076
|
if (tarResult.error) throw tarResult.error;
|
|
6475
6077
|
if (tarResult.status !== 0) throw new Error(`tar \u9000\u51FA\u7801 ${tarResult.status}`);
|
|
6476
6078
|
} else if (tempPath.endsWith(".gz")) {
|
|
6477
|
-
const baseName =
|
|
6478
|
-
const outputPath =
|
|
6079
|
+
const baseName = path7.basename(tempPath, ".gz");
|
|
6080
|
+
const outputPath = path7.join(extractPath, baseName);
|
|
6479
6081
|
const gzipResult = spawnSync5("gzip", ["-dc", tempPath], { maxBuffer: 256 * 1024 * 1024, timeout: 6e4 });
|
|
6480
6082
|
if (gzipResult.error) throw gzipResult.error;
|
|
6481
6083
|
if (gzipResult.status !== 0) throw new Error(`gzip \u9000\u51FA\u7801 ${gzipResult.status}`);
|
|
6482
|
-
|
|
6084
|
+
fs11.writeFileSync(outputPath, gzipResult.stdout, { mode: 493 });
|
|
6483
6085
|
extractedBinary = outputPath;
|
|
6484
6086
|
}
|
|
6485
6087
|
} catch (e) {
|
|
6486
6088
|
try {
|
|
6487
|
-
|
|
6089
|
+
fs11.unlinkSync(tempPath);
|
|
6488
6090
|
} catch {
|
|
6489
6091
|
}
|
|
6490
6092
|
throw new Error(`\u89E3\u538B\u5931\u8D25: ${e.message}`);
|
|
@@ -6492,23 +6094,23 @@ async function downloadKernel(progressCallback, mirror, releaseInfo) {
|
|
|
6492
6094
|
const foundBinary = extractedBinary || findBinaryInDir(extractPath);
|
|
6493
6095
|
if (!foundBinary) {
|
|
6494
6096
|
try {
|
|
6495
|
-
|
|
6097
|
+
fs11.unlinkSync(tempPath);
|
|
6496
6098
|
} catch {
|
|
6497
6099
|
}
|
|
6498
6100
|
throw new Error("\u89E3\u538B\u540E\u672A\u627E\u5230\u53EF\u6267\u884C\u6587\u4EF6");
|
|
6499
6101
|
}
|
|
6500
6102
|
const targetPath = PATHS.mihomoBinary;
|
|
6501
6103
|
if (foundBinary !== targetPath) {
|
|
6502
|
-
if (
|
|
6503
|
-
|
|
6104
|
+
if (fs11.existsSync(targetPath)) {
|
|
6105
|
+
fs11.chmodSync(targetPath, 493);
|
|
6504
6106
|
try {
|
|
6505
|
-
|
|
6107
|
+
fs11.unlinkSync(targetPath);
|
|
6506
6108
|
} catch {
|
|
6507
6109
|
}
|
|
6508
6110
|
}
|
|
6509
|
-
|
|
6111
|
+
fs11.renameSync(foundBinary, targetPath);
|
|
6510
6112
|
}
|
|
6511
|
-
|
|
6113
|
+
fs11.chmodSync(targetPath, 493);
|
|
6512
6114
|
if (progressCallback) {
|
|
6513
6115
|
progressCallback("\u6821\u9A8C\u5185\u6838...");
|
|
6514
6116
|
}
|
|
@@ -6516,11 +6118,11 @@ async function downloadKernel(progressCallback, mirror, releaseInfo) {
|
|
|
6516
6118
|
const checkOutput = `${check.stdout || ""}${check.stderr || ""}`.trim();
|
|
6517
6119
|
if (check.error || check.status !== 0 || !/v?\d+\.\d+\.\d+/.test(checkOutput)) {
|
|
6518
6120
|
try {
|
|
6519
|
-
|
|
6121
|
+
fs11.unlinkSync(targetPath);
|
|
6520
6122
|
} catch {
|
|
6521
6123
|
}
|
|
6522
6124
|
try {
|
|
6523
|
-
|
|
6125
|
+
fs11.unlinkSync(tempPath);
|
|
6524
6126
|
} catch {
|
|
6525
6127
|
}
|
|
6526
6128
|
throw new Error(`\u5185\u6838\u81EA\u68C0\u5931\u8D25\uFF08\u53EF\u80FD\u4E0B\u8F7D\u635F\u574F\u6216\u67B6\u6784\u4E0D\u5339\u914D\uFF09\uFF0C\u5DF2\u5220\u9664
|
|
@@ -6528,7 +6130,7 @@ async function downloadKernel(progressCallback, mirror, releaseInfo) {
|
|
|
6528
6130
|
\u8F93\u51FA: ${checkOutput || "(\u7A7A)"}`);
|
|
6529
6131
|
}
|
|
6530
6132
|
try {
|
|
6531
|
-
|
|
6133
|
+
fs11.unlinkSync(tempPath);
|
|
6532
6134
|
} catch {
|
|
6533
6135
|
}
|
|
6534
6136
|
clearKernelVersionCache();
|
|
@@ -6668,7 +6270,7 @@ function cmdLogs(args) {
|
|
|
6668
6270
|
}
|
|
6669
6271
|
|
|
6670
6272
|
// src/commands/overwrite.ts
|
|
6671
|
-
import
|
|
6273
|
+
import path8 from "path";
|
|
6672
6274
|
function printOverwriteList() {
|
|
6673
6275
|
const info = listOverwriteFile();
|
|
6674
6276
|
const statusText = info.enabled ? colors.green("\u5DF2\u542F\u7528") : colors.yellow("\u5DF2\u7981\u7528");
|
|
@@ -6678,8 +6280,8 @@ function printOverwriteList() {
|
|
|
6678
6280
|
if (info.files.length === 0) {
|
|
6679
6281
|
console.log("\u6682\u65E0\u8986\u5199\u6587\u4EF6");
|
|
6680
6282
|
console.log("");
|
|
6681
|
-
console.log(`\u7528\u6CD5\u793A\u4F8B: \u521B\u5EFA\u6587\u4EF6 ${
|
|
6682
|
-
console.log(` \u6216 ${
|
|
6283
|
+
console.log(`\u7528\u6CD5\u793A\u4F8B: \u521B\u5EFA\u6587\u4EF6 ${path8.join(info.dir, "overwrite.yaml")}`);
|
|
6284
|
+
console.log(` \u6216 ${path8.join(info.dir, "overwrite.dns.yaml")}`);
|
|
6683
6285
|
console.log("");
|
|
6684
6286
|
} else {
|
|
6685
6287
|
console.log(`${colors.cyan("\u8986\u5199\u6587\u4EF6")} (${info.files.length} \u4E2A\uFF0C\u6309\u987A\u5E8F\u52A0\u8F7D):`);
|
|
@@ -6735,7 +6337,7 @@ async function cmdOverwrite(args) {
|
|
|
6735
6337
|
}
|
|
6736
6338
|
|
|
6737
6339
|
// src/commands/reset.ts
|
|
6738
|
-
import
|
|
6340
|
+
import fs12 from "fs";
|
|
6739
6341
|
var RESET_TARGETS = [
|
|
6740
6342
|
{
|
|
6741
6343
|
id: "subs",
|
|
@@ -6768,22 +6370,6 @@ var RESET_TARGETS = [
|
|
|
6768
6370
|
paths: () => [DIRS.runtime],
|
|
6769
6371
|
needsStop: true
|
|
6770
6372
|
},
|
|
6771
|
-
{
|
|
6772
|
-
// 隧道要在删 pid 文件之前先停进程(onBefore):文件一删就再也找不到那些 ssh 进程,
|
|
6773
|
-
// 它们会继续占着端口跑下去,且 CLI 无任何路径能停掉。
|
|
6774
|
-
// **必须排在 settings 之前**:onAfter 会 writeSettings 重建 settings.json,
|
|
6775
|
-
// 排在 settings 之后会让 `reset --full` 留下一个 {}(同 subs 的处理,见执行顺序注释)
|
|
6776
|
-
id: "ssh",
|
|
6777
|
-
aliases: ["ssh"],
|
|
6778
|
-
label: "\u96A7\u9053",
|
|
6779
|
-
// 只删运行态:v3.12.0 起 CLI 不再生成任何 ssh 配置文件,节点/规则都在用户自己
|
|
6780
|
-
// 维护的 overwrite.yaml 里(归 overwrites 目标管),这里没有额外文件要清
|
|
6781
|
-
paths: () => [DIRS.ssh],
|
|
6782
|
-
needsStop: false,
|
|
6783
|
-
onBefore: () => stopAllSshTunnels(),
|
|
6784
|
-
// 同步清空 settings 里的隧道列表:只删运行态会留下「列表在但状态没了」的半重置状态
|
|
6785
|
-
onAfter: () => writeSettings({ ssh: void 0 })
|
|
6786
|
-
},
|
|
6787
6373
|
{
|
|
6788
6374
|
id: "settings",
|
|
6789
6375
|
aliases: ["setting", "settings", "config"],
|
|
@@ -6811,8 +6397,8 @@ var RESET_TARGETS = [
|
|
|
6811
6397
|
label: "\u8986\u5199",
|
|
6812
6398
|
paths: () => {
|
|
6813
6399
|
const dir = USER_DATA_DIR;
|
|
6814
|
-
if (!
|
|
6815
|
-
return
|
|
6400
|
+
if (!fs12.existsSync(dir)) return [];
|
|
6401
|
+
return fs12.readdirSync(dir).filter(isOverwriteFilename).map((f) => `${dir}/${f}`);
|
|
6816
6402
|
},
|
|
6817
6403
|
needsStop: false
|
|
6818
6404
|
},
|
|
@@ -6875,7 +6461,7 @@ async function cmdReset(args) {
|
|
|
6875
6461
|
}
|
|
6876
6462
|
targets = matched;
|
|
6877
6463
|
} else {
|
|
6878
|
-
targets = RESET_TARGETS.filter((t) => !["settings", "kernel", "overwrites", "daemon"
|
|
6464
|
+
targets = RESET_TARGETS.filter((t) => !["settings", "kernel", "overwrites", "daemon"].includes(t.id));
|
|
6879
6465
|
}
|
|
6880
6466
|
for (const t of targets) {
|
|
6881
6467
|
if (t.checkEmpty?.()) {
|
|
@@ -6917,7 +6503,7 @@ async function cmdReset(args) {
|
|
|
6917
6503
|
}
|
|
6918
6504
|
if (needsStop && getMihomoPids().length > 0) {
|
|
6919
6505
|
console.log("\u505C\u6B62\u8FDB\u7A0B...");
|
|
6920
|
-
const cleanup = cleanupAll();
|
|
6506
|
+
const cleanup = await cleanupAll();
|
|
6921
6507
|
for (let i = 0; i < PROCESS_WAIT_ATTEMPTS; i++) {
|
|
6922
6508
|
if (getMihomoPids().length === 0) break;
|
|
6923
6509
|
await new Promise((r) => setTimeout(r, PROCESS_WAIT_INTERVAL));
|
|
@@ -6935,9 +6521,8 @@ async function cmdReset(args) {
|
|
|
6935
6521
|
}
|
|
6936
6522
|
}
|
|
6937
6523
|
for (const t of targets) {
|
|
6938
|
-
t.onBefore?.();
|
|
6939
6524
|
for (const p of t.paths()) {
|
|
6940
|
-
if (
|
|
6525
|
+
if (fs12.existsSync(p)) {
|
|
6941
6526
|
try {
|
|
6942
6527
|
rmrf(p);
|
|
6943
6528
|
} catch (e) {
|
|
@@ -6954,225 +6539,6 @@ async function cmdReset(args) {
|
|
|
6954
6539
|
console.log(colors.green(`\u5DF2\u91CD\u7F6E: ${targets.map((t) => t.label).join("\u3001")}`));
|
|
6955
6540
|
}
|
|
6956
6541
|
|
|
6957
|
-
// src/commands/ssh.ts
|
|
6958
|
-
import fs14 from "fs";
|
|
6959
|
-
function warnLegacySshConfigFiles() {
|
|
6960
|
-
if (!fs14.existsSync(USER_DATA_DIR)) return;
|
|
6961
|
-
const legacy = fs14.readdirSync(USER_DATA_DIR).filter((f) => /^ssh\..+\.ya?ml$/.test(f));
|
|
6962
|
-
if (legacy.length === 0) return;
|
|
6963
|
-
console.log(colors.yellow(`\u8B66\u544A: \u53D1\u73B0 ${legacy.length} \u4E2A\u5DF2\u5931\u6548\u7684 ssh \u914D\u7F6E\u6587\u4EF6`));
|
|
6964
|
-
for (const f of legacy) {
|
|
6965
|
-
console.log(colors.gray(` ${f}`));
|
|
6966
|
-
}
|
|
6967
|
-
console.log(colors.gray(" v3.12.0 \u8D77 CLI \u4E0D\u518D\u52A0\u8F7D\u5B83\u4EEC\uFF0C\u5176\u4E2D\u7684\u5206\u7EC4\u4E0E\u89C4\u5219\u5DF2\u4E0D\u751F\u6548"));
|
|
6968
|
-
console.log(colors.gray(" \u9700\u8981\u7684\u8BDD\u628A\u5185\u5BB9\u5E76\u5165 overwrite.yaml\uFF08\u542B socks5 \u8282\u70B9\u5B9A\u4E49\uFF09\uFF0C\u7136\u540E\u81EA\u884C\u5220\u9664"));
|
|
6969
|
-
console.log("");
|
|
6970
|
-
}
|
|
6971
|
-
function printOverwriteSnippet(config) {
|
|
6972
|
-
console.log(colors.gray("\u628A\u96A7\u9053\u63A5\u5165\u5206\u6D41\uFF1A\u5728 overwrite.yaml \u91CC\u52A0\u4E0A\uFF08\u6539\u6210\u4F60\u7684\u5185\u7F51\u57DF\u540D\uFF09"));
|
|
6973
|
-
console.log(colors.gray(` ~proxies:`));
|
|
6974
|
-
console.log(colors.gray(` - {name: SSH-${config.name}, type: socks5, server: 127.0.0.1, port: ${config.port}}`));
|
|
6975
|
-
console.log(colors.gray(` +rules:`));
|
|
6976
|
-
console.log(colors.gray(` - DOMAIN-SUFFIX,example.internal,SSH-${config.name}`));
|
|
6977
|
-
}
|
|
6978
|
-
function formatState(status) {
|
|
6979
|
-
switch (status.state) {
|
|
6980
|
-
case "running":
|
|
6981
|
-
return colors.green(`\u8FD0\u884C\u4E2D (PID ${status.pid})`);
|
|
6982
|
-
case "dead-port":
|
|
6983
|
-
return colors.yellow(`\u5047\u6D3B (PID ${status.pid}\uFF0C\u7AEF\u53E3 ${status.config.port} \u4E0D\u901A)`);
|
|
6984
|
-
default:
|
|
6985
|
-
return colors.yellow("\u672A\u8FD0\u884C");
|
|
6986
|
-
}
|
|
6987
|
-
}
|
|
6988
|
-
function getSshNameArg(args) {
|
|
6989
|
-
return getNonFlagArg(args, 2);
|
|
6990
|
-
}
|
|
6991
|
-
function requireSshNameArg(args, usage) {
|
|
6992
|
-
const name = getSshNameArg(args);
|
|
6993
|
-
if (!name) {
|
|
6994
|
-
const tunnels = getSshTunnels();
|
|
6995
|
-
throw new CliError("\u8BF7\u6307\u5B9A\u96A7\u9053\u540D\u79F0", {
|
|
6996
|
-
hint: [usage, ...tunnels.length > 0 ? ["", "\u53EF\u7528\u96A7\u9053:", ...tunnels.map((t) => ` ${t.name}`)] : []]
|
|
6997
|
-
});
|
|
6998
|
-
}
|
|
6999
|
-
return name;
|
|
7000
|
-
}
|
|
7001
|
-
async function printSshList() {
|
|
7002
|
-
const tunnels = getSshTunnels();
|
|
7003
|
-
console.log("");
|
|
7004
|
-
warnLegacySshConfigFiles();
|
|
7005
|
-
if (tunnels.length === 0) {
|
|
7006
|
-
console.log("\u6CA1\u6709\u914D\u7F6E\u96A7\u9053");
|
|
7007
|
-
console.log("");
|
|
7008
|
-
console.log("\u6DFB\u52A0\u96A7\u9053: mihomo ssh add <\u540D\u5B57> --host <ssh\u4E3B\u673A> --port <\u7AEF\u53E3>");
|
|
7009
|
-
console.log(colors.gray(" \u4F8B\u5982: mihomo ssh add work --host m4 --port 1080"));
|
|
7010
|
-
console.log(colors.gray(" \u96A7\u9053\u628A\u5185\u7F51\u51FA\u53E3\u66B4\u9732\u4E3A\u672C\u5730 SOCKS5\uFF0C\u8282\u70B9\u4E0E\u5206\u6D41\u89C4\u5219\u5199\u8FDB overwrite.yaml"));
|
|
7011
|
-
console.log("");
|
|
7012
|
-
return;
|
|
7013
|
-
}
|
|
7014
|
-
const statuses = await getAllSshStatus();
|
|
7015
|
-
console.log(colors.cyan("\u96A7\u9053\u5217\u8868:"));
|
|
7016
|
-
console.log("");
|
|
7017
|
-
for (const status of statuses) {
|
|
7018
|
-
const { config } = status;
|
|
7019
|
-
const autoLabel = config.auto ? colors.gray(" [auto]") : "";
|
|
7020
|
-
console.log(` ${colors.bold(config.name)}${autoLabel}`);
|
|
7021
|
-
console.log(` ${colors.gray("\u51FA\u53E3: ")}${config.host} \u2192 127.0.0.1:${config.port}`);
|
|
7022
|
-
console.log(` ${colors.gray("\u72B6\u6001: ")}${formatState(status)}`);
|
|
7023
|
-
if (status.started_by) {
|
|
7024
|
-
console.log(` ${colors.gray("\u6765\u6E90: ")}${status.started_by === "auto" ? "\u968F start \u62C9\u8D77" : "\u624B\u52A8\u542F\u52A8"}`);
|
|
7025
|
-
}
|
|
7026
|
-
}
|
|
7027
|
-
console.log("");
|
|
7028
|
-
console.log("\u542F\u52A8: mihomo ssh up [\u540D\u5B57] \u505C\u6B62: mihomo ssh down [\u540D\u5B57]");
|
|
7029
|
-
console.log("\u72B6\u6001: mihomo ssh status \u5220\u9664: mihomo ssh rm <\u540D\u5B57>");
|
|
7030
|
-
console.log("");
|
|
7031
|
-
}
|
|
7032
|
-
function sshAdd(args) {
|
|
7033
|
-
const name = requireSshNameArg(args, "\u7528\u6CD5: mihomo ssh add <\u540D\u5B57> --host <ssh\u4E3B\u673A> --port <\u7AEF\u53E3> [--no-auto]");
|
|
7034
|
-
const host = parseStringArg(args, "--host");
|
|
7035
|
-
if (!host) {
|
|
7036
|
-
throw new CliError("\u7F3A\u5C11 --host", {
|
|
7037
|
-
hint: ["\u7528\u6CD5: mihomo ssh add <\u540D\u5B57> --host <ssh\u4E3B\u673A> --port <\u7AEF\u53E3>", "", "\u4E3B\u673A\u53EF\u7528 ~/.ssh/config \u91CC\u7684\u522B\u540D\uFF0C\u4F8B\u5982 --host m4"]
|
|
7038
|
-
});
|
|
7039
|
-
}
|
|
7040
|
-
const portRaw = parseStringArg(args, "--port");
|
|
7041
|
-
if (!portRaw) {
|
|
7042
|
-
throw new CliError("\u7F3A\u5C11 --port", { hint: ["\u7528\u6CD5: mihomo ssh add <\u540D\u5B57> --host <ssh\u4E3B\u673A> --port <\u7AEF\u53E3>"] });
|
|
7043
|
-
}
|
|
7044
|
-
if (!/^\d+$/.test(portRaw.trim())) {
|
|
7045
|
-
throw new CliError(`\u7AEF\u53E3\u65E0\u6548: "${portRaw}"\uFF0C\u9700\u4E3A 1-65535 \u7684\u6574\u6570`);
|
|
7046
|
-
}
|
|
7047
|
-
const port = Number(portRaw.trim());
|
|
7048
|
-
const auto = !hasFlag(args, "--no-auto");
|
|
7049
|
-
const config = { name, host, port, auto };
|
|
7050
|
-
addSshTunnel(config);
|
|
7051
|
-
console.log(`${colors.green("\u5DF2\u6DFB\u52A0\u96A7\u9053")} ${name} \xB7 ${host} \u2192 127.0.0.1:${port}${auto ? " \xB7 auto" : ""}`);
|
|
7052
|
-
console.log("");
|
|
7053
|
-
printOverwriteSnippet(config);
|
|
7054
|
-
console.log("");
|
|
7055
|
-
console.log(`\u542F\u52A8\u96A7\u9053: mihomo ssh up ${name}`);
|
|
7056
|
-
}
|
|
7057
|
-
async function sshUp(args) {
|
|
7058
|
-
const name = getSshNameArg(args);
|
|
7059
|
-
const targets = name ? [resolveSshTunnel(name)] : getSshTunnels();
|
|
7060
|
-
if (targets.length === 0) {
|
|
7061
|
-
throw new CliError("\u6CA1\u6709\u914D\u7F6E\u96A7\u9053", { hint: ["\u6DFB\u52A0\u96A7\u9053: mihomo ssh add <\u540D\u5B57> --host <ssh\u4E3B\u673A> --port <\u7AEF\u53E3>"] });
|
|
7062
|
-
}
|
|
7063
|
-
for (const config of targets) {
|
|
7064
|
-
const result = await startSshTunnel(config.name, { startedBy: "manual" });
|
|
7065
|
-
if (result.alreadyRunning) {
|
|
7066
|
-
console.log(`${colors.gray("\u5DF2\u5728\u8FD0\u884C")} ${config.name} (PID ${result.pid})`);
|
|
7067
|
-
} else {
|
|
7068
|
-
console.log(`${colors.green("\u5DF2\u542F\u52A8\u96A7\u9053")} ${config.name} \xB7 ${config.host} \u2192 127.0.0.1:${config.port} (PID ${result.pid})`);
|
|
7069
|
-
}
|
|
7070
|
-
}
|
|
7071
|
-
}
|
|
7072
|
-
function sshDown(args) {
|
|
7073
|
-
const name = getSshNameArg(args);
|
|
7074
|
-
const targets = name ? [resolveSshTunnel(name)] : getSshTunnels();
|
|
7075
|
-
if (targets.length === 0) {
|
|
7076
|
-
throw new CliError("\u6CA1\u6709\u914D\u7F6E\u96A7\u9053");
|
|
7077
|
-
}
|
|
7078
|
-
let stopped = 0;
|
|
7079
|
-
for (const config of targets) {
|
|
7080
|
-
const result = stopSshTunnel(config.name);
|
|
7081
|
-
if (result.notRunning) {
|
|
7082
|
-
console.log(`${colors.yellow("\u4E0D\u5728\u8FD0\u884C")} ${config.name}`);
|
|
7083
|
-
} else {
|
|
7084
|
-
console.log(`${colors.green("\u5DF2\u505C\u6B62\u96A7\u9053")} ${config.name} (PID ${result.pid})`);
|
|
7085
|
-
stopped++;
|
|
7086
|
-
}
|
|
7087
|
-
}
|
|
7088
|
-
if (stopped > 0) {
|
|
7089
|
-
console.log("");
|
|
7090
|
-
console.log(colors.gray("\u6CE8\u610F: overwrite.yaml \u91CC\u6307\u5411\u8BE5\u7AEF\u53E3\u7684\u8282\u70B9\u73B0\u5728\u8FDE\u4E0D\u901A"));
|
|
7091
|
-
}
|
|
7092
|
-
}
|
|
7093
|
-
async function sshStatus(args) {
|
|
7094
|
-
const name = getSshNameArg(args);
|
|
7095
|
-
if (!name) {
|
|
7096
|
-
await printSshList();
|
|
7097
|
-
return;
|
|
7098
|
-
}
|
|
7099
|
-
const config = resolveSshTunnel(name);
|
|
7100
|
-
const status = await getSshStatus(config);
|
|
7101
|
-
console.log("");
|
|
7102
|
-
console.log(`${colors.gray("\u96A7\u9053: ")}${config.name}${config.auto ? colors.gray(" [auto]") : ""}`);
|
|
7103
|
-
console.log(`${colors.gray("\u51FA\u53E3: ")}${config.host} \u2192 127.0.0.1:${config.port}`);
|
|
7104
|
-
console.log(`${colors.gray("\u72B6\u6001: ")}${formatState(status)}`);
|
|
7105
|
-
if (status.started_by) {
|
|
7106
|
-
console.log(`${colors.gray("\u6765\u6E90: ")}${status.started_by === "auto" ? "\u968F start \u62C9\u8D77" : "\u624B\u52A8\u542F\u52A8"}`);
|
|
7107
|
-
}
|
|
7108
|
-
console.log("");
|
|
7109
|
-
if (status.state === "dead-port") {
|
|
7110
|
-
console.log(colors.yellow("ssh \u8FDB\u7A0B\u8FD8\u5728\uFF0C\u4F46\u7AEF\u53E3\u6CA1\u6709\u76D1\u542C\u2014\u2014\u5185\u7F51\u5206\u6D41\u5F53\u524D\u4E0D\u53EF\u7528"));
|
|
7111
|
-
console.log(`\u91CD\u542F\u96A7\u9053: mihomo ssh down ${config.name} && mihomo ssh up ${config.name}`);
|
|
7112
|
-
console.log(colors.gray(` \u65E5\u5FD7: ${getSshLogPath(config.name)}`));
|
|
7113
|
-
console.log("");
|
|
7114
|
-
} else if (status.state === "stopped") {
|
|
7115
|
-
console.log(`\u542F\u52A8\u96A7\u9053: mihomo ssh up ${config.name}`);
|
|
7116
|
-
console.log("");
|
|
7117
|
-
}
|
|
7118
|
-
}
|
|
7119
|
-
async function sshRemove(args) {
|
|
7120
|
-
const name = requireSshNameArg(args, "\u7528\u6CD5: mihomo ssh rm <\u540D\u5B57> [-y]");
|
|
7121
|
-
const config = resolveSshTunnel(name);
|
|
7122
|
-
const skipConfirm = hasFlag(args, "-y", "--yes");
|
|
7123
|
-
if (!skipConfirm) {
|
|
7124
|
-
if (!await confirmOrThrow(`\u786E\u8BA4\u5220\u9664\u96A7\u9053 "${config.name}"?`, {
|
|
7125
|
-
nonTtyMessage: "\u5220\u9664\u96A7\u9053\u9700\u8981\u786E\u8BA4",
|
|
7126
|
-
hint: [`\u8DF3\u8FC7\u786E\u8BA4: mihomo ssh rm ${config.name} -y`]
|
|
7127
|
-
})) {
|
|
7128
|
-
console.log("\u5DF2\u53D6\u6D88");
|
|
7129
|
-
return;
|
|
7130
|
-
}
|
|
7131
|
-
}
|
|
7132
|
-
stopSshTunnel(config.name);
|
|
7133
|
-
removeSshTunnel(config.name);
|
|
7134
|
-
console.log(`${colors.green("\u5DF2\u5220\u9664\u96A7\u9053")} ${config.name}`);
|
|
7135
|
-
console.log("");
|
|
7136
|
-
console.log(colors.gray(`\u5982 overwrite.yaml \u91CC\u5199\u8FC7\u6307\u5411 127.0.0.1:${config.port} \u7684\u8282\u70B9\uFF0C\u8BF7\u81EA\u884C\u79FB\u9664`));
|
|
7137
|
-
}
|
|
7138
|
-
function resolveSshTunnel(name) {
|
|
7139
|
-
const config = findSshTunnel(name);
|
|
7140
|
-
if (!config) {
|
|
7141
|
-
const tunnels = getSshTunnels();
|
|
7142
|
-
const suggestion = suggestSimilar(
|
|
7143
|
-
name,
|
|
7144
|
-
tunnels.map((t) => t.name)
|
|
7145
|
-
);
|
|
7146
|
-
throw new CliError(`\u672A\u627E\u5230\u96A7\u9053 "${name}"`, {
|
|
7147
|
-
hint: [
|
|
7148
|
-
...suggestion.length > 0 ? [`\u662F\u5426\u60F3\u8F93\u5165: ${suggestion.join(" / ")}?`] : [],
|
|
7149
|
-
...tunnels.length > 0 ? ["", "\u53EF\u7528\u96A7\u9053:", ...tunnels.map((t) => ` ${t.name}`)] : ["", "\u6DFB\u52A0\u96A7\u9053: mihomo ssh add <\u540D\u5B57> --host <ssh\u4E3B\u673A> --port <\u7AEF\u53E3>"]
|
|
7150
|
-
]
|
|
7151
|
-
});
|
|
7152
|
-
}
|
|
7153
|
-
return config;
|
|
7154
|
-
}
|
|
7155
|
-
var SUBCOMMANDS4 = [
|
|
7156
|
-
{ name: "add", handler: sshAdd },
|
|
7157
|
-
{ name: "up", aliases: ["start"], handler: sshUp },
|
|
7158
|
-
{ name: "down", aliases: ["stop"], handler: sshDown },
|
|
7159
|
-
{ name: "status", handler: sshStatus },
|
|
7160
|
-
{ name: "remove", aliases: ["rm", "delete"], handler: sshRemove }
|
|
7161
|
-
];
|
|
7162
|
-
async function cmdSsh(args) {
|
|
7163
|
-
await dispatchSubcommand(args, SUBCOMMANDS4, {
|
|
7164
|
-
// 无 action → 列表;未知 action → 报错(不静默回落,否则 `ssh upp` 会看似成功)
|
|
7165
|
-
fallback: printSshList,
|
|
7166
|
-
onUnknown: (action) => {
|
|
7167
|
-
const names = SUBCOMMANDS4.flatMap((c) => [c.name, ...c.aliases ?? []]);
|
|
7168
|
-
const suggestion = suggestSimilar(action, names);
|
|
7169
|
-
throw new CliError(`\u672A\u77E5\u7684 ssh \u5B50\u547D\u4EE4: ${action}`, {
|
|
7170
|
-
hint: [...suggestion.length > 0 ? [`\u662F\u5426\u60F3\u8F93\u5165: ${suggestion.join(" / ")}?`] : [], "", "\u53EF\u7528\u5B50\u547D\u4EE4: add, up, down, status, rm\uFF08\u88F8 ssh \u5373\u5217\u8868\uFF09"]
|
|
7171
|
-
});
|
|
7172
|
-
}
|
|
7173
|
-
});
|
|
7174
|
-
}
|
|
7175
|
-
|
|
7176
6542
|
// src/commands/subscription.ts
|
|
7177
6543
|
function printRestartHintIfRunning() {
|
|
7178
6544
|
if (getRunningState().running) {
|
|
@@ -7336,18 +6702,18 @@ async function subRemove(args) {
|
|
|
7336
6702
|
console.log("");
|
|
7337
6703
|
printSubscriptionList();
|
|
7338
6704
|
}
|
|
7339
|
-
var
|
|
6705
|
+
var SUBCOMMANDS4 = [
|
|
7340
6706
|
{ name: "add", handler: subAdd },
|
|
7341
6707
|
{ name: "update", handler: subUpdate },
|
|
7342
6708
|
{ name: "use", handler: subUse },
|
|
7343
6709
|
{ name: "remove", aliases: ["rm", "delete"], handler: subRemove }
|
|
7344
6710
|
];
|
|
7345
6711
|
async function cmdSubscription(args) {
|
|
7346
|
-
await dispatchSubcommand(args,
|
|
6712
|
+
await dispatchSubcommand(args, SUBCOMMANDS4, {
|
|
7347
6713
|
// 无子命令 → 列表;未知子命令 → 报错
|
|
7348
6714
|
fallback: printSubscriptionList,
|
|
7349
6715
|
onUnknown: (action) => {
|
|
7350
|
-
const names =
|
|
6716
|
+
const names = SUBCOMMANDS4.flatMap((c) => [c.name, ...c.aliases ?? []]);
|
|
7351
6717
|
const suggestion = suggestSimilar(action, names);
|
|
7352
6718
|
throw new CliError(`\u672A\u77E5\u7684\u8BA2\u9605\u547D\u4EE4: ${action}`, {
|
|
7353
6719
|
hint: [...suggestion.length > 0 ? [`\u662F\u5426\u60F3\u8F93\u5165: ${suggestion.join(" / ")}?`] : [], "\u7528\u6CD5: mihomo sub [use|add|update|remove]\uFF08\u88F8 sub \u5373\u5217\u8868\uFF09"]
|
|
@@ -7380,7 +6746,7 @@ function cmdUI(args) {
|
|
|
7380
6746
|
}
|
|
7381
6747
|
|
|
7382
6748
|
// src/commands/update.ts
|
|
7383
|
-
import { execFile, spawn as
|
|
6749
|
+
import { execFile, spawn as spawn3 } from "child_process";
|
|
7384
6750
|
import { promisify } from "util";
|
|
7385
6751
|
var execFileAsync = promisify(execFile);
|
|
7386
6752
|
var NPM_VIEW_TIMEOUT_MS = 15e3;
|
|
@@ -7411,7 +6777,7 @@ async function cmdUpdate() {
|
|
|
7411
6777
|
console.log("\u6B63\u5728\u66F4\u65B0 mihomo-cli...");
|
|
7412
6778
|
console.log("");
|
|
7413
6779
|
await new Promise((resolve, reject) => {
|
|
7414
|
-
const npm =
|
|
6780
|
+
const npm = spawn3("npm", ["install", "-g", PKG_NAME], { stdio: "inherit" });
|
|
7415
6781
|
npm.on("close", (code) => {
|
|
7416
6782
|
if (code === 0) {
|
|
7417
6783
|
resolve();
|
|
@@ -7448,7 +6814,7 @@ var COMMANDS = [
|
|
|
7448
6814
|
aliases: ["up"],
|
|
7449
6815
|
handler: cmdStart,
|
|
7450
6816
|
group: "control",
|
|
7451
|
-
usage: ["start [tun|mixed] [-s] [-u ms]
|
|
6817
|
+
usage: ["start [tun|mixed] [-s] [-u ms] \u542F\u52A8/\u5207\u6362\u4EE3\u7406 (\u9ED8\u8BA4 mixed)"]
|
|
7452
6818
|
},
|
|
7453
6819
|
{
|
|
7454
6820
|
name: "tun",
|
|
@@ -7463,7 +6829,7 @@ var COMMANDS = [
|
|
|
7463
6829
|
aliases: ["down"],
|
|
7464
6830
|
handler: cmdStop,
|
|
7465
6831
|
group: "control",
|
|
7466
|
-
usage: ["stop
|
|
6832
|
+
usage: ["stop \u505C\u6B62\u4EE3\u7406"]
|
|
7467
6833
|
},
|
|
7468
6834
|
{
|
|
7469
6835
|
name: "status",
|
|
@@ -7540,21 +6906,6 @@ var COMMANDS = [
|
|
|
7540
6906
|
group: "system",
|
|
7541
6907
|
usage: ["daemon [on|off] \u5F00\u673A\u81EA\u542F + \u5D29\u6E83\u91CD\u542F\uFF08\u4EC5 Mixed\uFF0C\u9700\u7BA1\u7406\u5458\u5BC6\u7801\uFF09\uFF1B\u65E0\u53C2\u770B\u72B6\u6001"]
|
|
7542
6908
|
},
|
|
7543
|
-
{
|
|
7544
|
-
// 无别名:`tun` 被 TUN 模式快捷命令占用,`ssh` 本身已够短。
|
|
7545
|
-
// (注册表重复 token 会在模块加载期直接抛错)
|
|
7546
|
-
name: "ssh",
|
|
7547
|
-
aliases: [],
|
|
7548
|
-
handler: cmdSsh,
|
|
7549
|
-
group: "system",
|
|
7550
|
-
usage: [
|
|
7551
|
-
"ssh \u5217\u51FA ssh \u96A7\u9053\uFF08\u672C\u5730 SOCKS5 \u7AEF\u53E3\uFF09",
|
|
7552
|
-
"ssh add <\u540D\u5B57> --host <\u4E3B\u673A> --port <\u7AEF\u53E3> [--no-auto]",
|
|
7553
|
-
"ssh up|down [\u540D\u5B57] \u542F\u52A8/\u505C\u6B62\u96A7\u9053\uFF08\u65E0\u53C2\u5373\u5168\u90E8\uFF09",
|
|
7554
|
-
"ssh status [\u540D\u5B57] \u67E5\u770B\u96A7\u9053\u72B6\u6001\uFF08\u771F\u5B9E\u63A2\u6D4B\u7AEF\u53E3\uFF09",
|
|
7555
|
-
"ssh rm <\u540D\u5B57> [-y] \u5220\u9664\u96A7\u9053"
|
|
7556
|
-
]
|
|
7557
|
-
},
|
|
7558
6909
|
{
|
|
7559
6910
|
name: "update",
|
|
7560
6911
|
aliases: [],
|