Update version to 0.2.76 and enhance MITM manager for cross-platform compatibility

This commit is contained in:
decolua
2026-02-10 19:40:06 +07:00
parent b179dc2647
commit c3baf52988
2 changed files with 52 additions and 27 deletions

View File

@@ -1,8 +1,10 @@
const { spawn } = require("child_process"); const { spawn, exec } = require("child_process");
const path = require("path"); const path = require("path");
const fs = require("fs"); const fs = require("fs");
const os = require("os"); const os = require("os");
const { addDNSEntry, removeDNSEntry } = require("./dns/dnsConfig"); const { addDNSEntry, removeDNSEntry, checkDNSEntry } = require("./dns/dnsConfig");
const IS_WIN = process.platform === "win32";
const { generateCert } = require("./cert/generate"); const { generateCert } = require("./cert/generate");
const { installCert } = require("./cert/install"); const { installCert } = require("./cert/install");
@@ -26,6 +28,16 @@ function isProcessAlive(pid) {
} }
} }
// Cross-platform process kill
function killProcess(pid, force = false) {
if (IS_WIN) {
const flag = force ? "/F " : "";
exec(`taskkill ${flag}/PID ${pid}`, () => {});
} else {
process.kill(pid, force ? "SIGKILL" : "SIGTERM");
}
}
/** /**
* Get MITM status * Get MITM status
*/ */
@@ -51,14 +63,8 @@ async function getMitmStatus() {
} }
} }
// Check DNS configuration // Check DNS configuration (cross-platform via dnsConfig)
let dnsConfigured = false; const dnsConfigured = checkDNSEntry();
try {
const hostsContent = fs.readFileSync("/etc/hosts", "utf-8");
dnsConfigured = hostsContent.includes("daily-cloudcode-pa.googleapis.com");
} catch {
// Ignore
}
// Check cert // Check cert
const certDir = path.join(os.homedir(), ".9router", "mitm"); const certDir = path.join(os.homedir(), ".9router", "mitm");
@@ -92,9 +98,22 @@ async function startMitm(apiKey, sudoPassword) {
console.log("Adding DNS entry..."); console.log("Adding DNS entry...");
await addDNSEntry(sudoPassword); await addDNSEntry(sudoPassword);
// 4. Start MITM server // 4. Start MITM server (port 443 requires elevated privileges)
console.log("Starting MITM server..."); console.log("Starting MITM server...");
const serverPath = path.join(process.cwd(), "src/mitm/server.js"); const serverPath = path.join(process.cwd(), "src/mitm/server.js");
if (IS_WIN) {
// Windows: spawn via powershell elevated to bind port 443
const nodePath = process.execPath;
const envArgs = `$env:ROUTER_API_KEY='${apiKey}'; $env:NODE_ENV='production'; & '${nodePath}' '${serverPath}'`;
serverProcess = spawn("powershell", [
"-Command",
`Start-Process powershell -ArgumentList '-NoProfile','-Command','${envArgs.replace(/'/g, "''")}' -Verb RunAs -PassThru`
], {
detached: false,
stdio: ["ignore", "pipe", "pipe"]
});
} else {
serverProcess = spawn("node", [serverPath], { serverProcess = spawn("node", [serverPath], {
env: { env: {
...process.env, ...process.env,
@@ -104,6 +123,7 @@ async function startMitm(apiKey, sudoPassword) {
detached: false, detached: false,
stdio: ["ignore", "pipe", "pipe"] stdio: ["ignore", "pipe", "pipe"]
}); });
}
serverPid = serverProcess.pid; serverPid = serverProcess.pid;
@@ -173,10 +193,10 @@ async function stopMitm(sudoPassword) {
const proc = serverProcess; const proc = serverProcess;
if (proc && !proc.killed) { if (proc && !proc.killed) {
console.log("Stopping MITM server..."); console.log("Stopping MITM server...");
proc.kill("SIGTERM"); killProcess(proc.pid, false);
await new Promise(resolve => setTimeout(resolve, 1000)); await new Promise(resolve => setTimeout(resolve, 1000));
if (!proc.killed) { if (isProcessAlive(proc.pid)) {
proc.kill("SIGKILL"); killProcess(proc.pid, true);
} }
serverProcess = null; serverProcess = null;
serverPid = null; serverPid = null;
@@ -187,10 +207,10 @@ async function stopMitm(sudoPassword) {
const savedPid = parseInt(fs.readFileSync(PID_FILE, "utf-8").trim(), 10); const savedPid = parseInt(fs.readFileSync(PID_FILE, "utf-8").trim(), 10);
if (savedPid && isProcessAlive(savedPid)) { if (savedPid && isProcessAlive(savedPid)) {
console.log(`Killing MITM server (PID: ${savedPid})...`); console.log(`Killing MITM server (PID: ${savedPid})...`);
process.kill(savedPid, "SIGTERM"); killProcess(savedPid, false);
await new Promise(resolve => setTimeout(resolve, 1000)); await new Promise(resolve => setTimeout(resolve, 1000));
if (isProcessAlive(savedPid)) { if (isProcessAlive(savedPid)) {
process.kill(savedPid, "SIGKILL"); killProcess(savedPid, true);
} }
} }
} }

View File

@@ -210,5 +210,10 @@ server.on("error", (error) => {
process.exit(1); process.exit(1);
}); });
process.on("SIGTERM", () => { server.close(() => process.exit(0)); }); // Graceful shutdown (SIGBREAK for Windows, SIGTERM/SIGINT for Unix)
process.on("SIGINT", () => { server.close(() => process.exit(0)); }); const shutdown = () => { server.close(() => process.exit(0)); };
process.on("SIGTERM", shutdown);
process.on("SIGINT", shutdown);
if (process.platform === "win32") {
process.on("SIGBREAK", shutdown);
}