Update version to 0.2.76 and enhance MITM manager for cross-platform compatibility
This commit is contained in:
parent
b179dc2647
commit
c3baf52988
2 changed files with 52 additions and 27 deletions
|
|
@ -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,18 +98,32 @@ 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");
|
||||||
serverProcess = spawn("node", [serverPath], {
|
|
||||||
env: {
|
if (IS_WIN) {
|
||||||
...process.env,
|
// Windows: spawn via powershell elevated to bind port 443
|
||||||
ROUTER_API_KEY: apiKey,
|
const nodePath = process.execPath;
|
||||||
NODE_ENV: "production"
|
const envArgs = `$env:ROUTER_API_KEY='${apiKey}'; $env:NODE_ENV='production'; & '${nodePath}' '${serverPath}'`;
|
||||||
},
|
serverProcess = spawn("powershell", [
|
||||||
detached: false,
|
"-Command",
|
||||||
stdio: ["ignore", "pipe", "pipe"]
|
`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;
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue