- 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:
decolua
2026-04-04 11:25:58 +07:00
parent 2e740ad7e4
commit d84489dba4
11 changed files with 559 additions and 266 deletions
+4 -1
View File
@@ -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
View File
@@ -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,
},
});
}