mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-23 04:09:49 +00:00
- Introduce default MITM router base URL and update related components to handle it.
- Add input for MITM router base URL in MitmServerCard component.
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
const { log, err } = require("../logger");
|
||||
|
||||
const ROUTER_BASE = "http://localhost:20128";
|
||||
const DEFAULT_LOCAL_ROUTER = "http://localhost:20128";
|
||||
const ROUTER_BASE = String(process.env.MITM_ROUTER_BASE || DEFAULT_LOCAL_ROUTER)
|
||||
.trim()
|
||||
.replace(/\/+$/, "") || DEFAULT_LOCAL_ROUTER;
|
||||
const API_KEY = process.env.ROUTER_API_KEY;
|
||||
|
||||
/**
|
||||
|
||||
+43
-4
@@ -15,6 +15,27 @@ const { isCertExpired } = require("./cert/rootCA");
|
||||
const { MITM_DIR } = require("./paths");
|
||||
const { log, err } = require("./logger");
|
||||
|
||||
const DEFAULT_MITM_ROUTER_BASE = "http://localhost:20128";
|
||||
|
||||
function shellQuoteSingle(str) {
|
||||
if (str == null || str === "") return "''";
|
||||
return `'${String(str).replace(/'/g, "'\\''")}'`;
|
||||
}
|
||||
|
||||
async function resolveMitmRouterBaseUrl() {
|
||||
if (!_getSettings) return DEFAULT_MITM_ROUTER_BASE;
|
||||
try {
|
||||
const s = await _getSettings();
|
||||
const raw = s && s.mitmRouterBaseUrl != null ? String(s.mitmRouterBaseUrl).trim() : "";
|
||||
if (!raw) return DEFAULT_MITM_ROUTER_BASE;
|
||||
const u = new URL(raw);
|
||||
if (u.protocol !== "http:" && u.protocol !== "https:") return DEFAULT_MITM_ROUTER_BASE;
|
||||
return raw.replace(/\/+$/, "");
|
||||
} catch {
|
||||
return DEFAULT_MITM_ROUTER_BASE;
|
||||
}
|
||||
}
|
||||
|
||||
const MITM_PORT = 443;
|
||||
const MITM_WIN_NODE_PORT = 8443;
|
||||
const PID_FILE = path.join(MITM_DIR, ".mitm.pid");
|
||||
@@ -427,7 +448,8 @@ async function startServer(apiKey, sudoPassword) {
|
||||
}
|
||||
|
||||
// Step 2: Spawn server (Root CA already installed in Step 1.5)
|
||||
log("🚀 Starting server...");
|
||||
const mitmRouterBase = await resolveMitmRouterBaseUrl();
|
||||
log(`🚀 Starting server... (router: ${mitmRouterBase})`);
|
||||
if (IS_WIN) {
|
||||
// Kill any process using port 443 before spawning
|
||||
try {
|
||||
@@ -444,7 +466,12 @@ async function startServer(apiKey, sudoPassword) {
|
||||
detached: false,
|
||||
windowsHide: true,
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
env: { ...process.env, ROUTER_API_KEY: apiKey, NODE_ENV: "production" },
|
||||
env: {
|
||||
...process.env,
|
||||
ROUTER_API_KEY: apiKey,
|
||||
NODE_ENV: "production",
|
||||
MITM_ROUTER_BASE: mitmRouterBase,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
@@ -452,7 +479,14 @@ async function startServer(apiKey, sudoPassword) {
|
||||
} else if (isSudoAvailable()) {
|
||||
// Pass HOME explicitly so os.homedir() resolves to the unprivileged user's home
|
||||
// instead of /root when sudo resets the environment.
|
||||
const inlineCmd = `HOME='${os.homedir()}' ROUTER_API_KEY='${apiKey}' NODE_ENV='production' '${process.execPath}' '${SERVER_PATH}'`;
|
||||
const inlineCmd = [
|
||||
`HOME=${shellQuoteSingle(os.homedir())}`,
|
||||
`ROUTER_API_KEY=${shellQuoteSingle(apiKey)}`,
|
||||
`MITM_ROUTER_BASE=${shellQuoteSingle(mitmRouterBase)}`,
|
||||
"NODE_ENV=production",
|
||||
shellQuoteSingle(process.execPath),
|
||||
shellQuoteSingle(SERVER_PATH),
|
||||
].join(" ");
|
||||
serverProcess = spawn(
|
||||
"sudo", ["-S", "-E", "sh", "-c", inlineCmd],
|
||||
{ detached: false, stdio: ["pipe", "pipe", "pipe"] }
|
||||
@@ -464,7 +498,12 @@ async function startServer(apiKey, sudoPassword) {
|
||||
serverProcess = spawn(process.execPath, [SERVER_PATH], {
|
||||
detached: false,
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
env: { ...process.env, ROUTER_API_KEY: apiKey, NODE_ENV: "production" },
|
||||
env: {
|
||||
...process.env,
|
||||
ROUTER_API_KEY: apiKey,
|
||||
NODE_ENV: "production",
|
||||
MITM_ROUTER_BASE: mitmRouterBase,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user