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 fs = require("fs");
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 { 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
*/
@@ -51,14 +63,8 @@ async function getMitmStatus() {
}
}
// Check DNS configuration
let dnsConfigured = false;
try {
const hostsContent = fs.readFileSync("/etc/hosts", "utf-8");
dnsConfigured = hostsContent.includes("daily-cloudcode-pa.googleapis.com");
} catch {
// Ignore
}
// Check DNS configuration (cross-platform via dnsConfig)
const dnsConfigured = checkDNSEntry();
// Check cert
const certDir = path.join(os.homedir(), ".9router", "mitm");
@@ -92,18 +98,32 @@ async function startMitm(apiKey, sudoPassword) {
console.log("Adding DNS entry...");
await addDNSEntry(sudoPassword);
// 4. Start MITM server
// 4. Start MITM server (port 443 requires elevated privileges)
console.log("Starting MITM server...");
const serverPath = path.join(process.cwd(), "src/mitm/server.js");
serverProcess = spawn("node", [serverPath], {
env: {
...process.env,
ROUTER_API_KEY: apiKey,
NODE_ENV: "production"
},
detached: false,
stdio: ["ignore", "pipe", "pipe"]
});
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], {
env: {
...process.env,
ROUTER_API_KEY: apiKey,
NODE_ENV: "production"
},
detached: false,
stdio: ["ignore", "pipe", "pipe"]
});
}
serverPid = serverProcess.pid;
@@ -173,10 +193,10 @@ async function stopMitm(sudoPassword) {
const proc = serverProcess;
if (proc && !proc.killed) {
console.log("Stopping MITM server...");
proc.kill("SIGTERM");
killProcess(proc.pid, false);
await new Promise(resolve => setTimeout(resolve, 1000));
if (!proc.killed) {
proc.kill("SIGKILL");
if (isProcessAlive(proc.pid)) {
killProcess(proc.pid, true);
}
serverProcess = null;
serverPid = null;
@@ -187,10 +207,10 @@ async function stopMitm(sudoPassword) {
const savedPid = parseInt(fs.readFileSync(PID_FILE, "utf-8").trim(), 10);
if (savedPid && isProcessAlive(savedPid)) {
console.log(`Killing MITM server (PID: ${savedPid})...`);
process.kill(savedPid, "SIGTERM");
killProcess(savedPid, false);
await new Promise(resolve => setTimeout(resolve, 1000));
if (isProcessAlive(savedPid)) {
process.kill(savedPid, "SIGKILL");
killProcess(savedPid, true);
}
}
}

View File

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