Refactor execSync and spawn calls to include windowsHide option for better compatibility on Windows environments.

This commit is contained in:
decolua
2026-04-14 11:48:59 +07:00
parent 04cdb75839
commit 1fa05eb2ab
8 changed files with 45 additions and 34 deletions
+2 -2
View File
@@ -29,10 +29,10 @@ function checkCertInstalledMac(certPath) {
try {
const fingerprint = getCertFingerprint(certPath).replace(/:/g, "");
// security verify-cert returns 0 only if cert is trusted by system policy
exec(`security verify-cert -c "${certPath}" -p ssl -k /Library/Keychains/System.keychain 2>/dev/null`, (error) => {
exec(`security verify-cert -c "${certPath}" -p ssl -k /Library/Keychains/System.keychain 2>/dev/null`, { windowsHide: true }, (error) => {
if (!error) return resolve(true);
// Fallback: check if fingerprint appears in System keychain with trust
exec(`security dump-trust-settings -d 2>/dev/null | grep -i "${fingerprint}"`, (err2, stdout2) => {
exec(`security dump-trust-settings -d 2>/dev/null | grep -i "${fingerprint}"`, { windowsHide: true }, (err2, stdout2) => {
resolve(!err2 && !!stdout2?.trim());
});
});
+3 -3
View File
@@ -63,7 +63,7 @@ function executeElevatedPowerShell(psScriptPath, timeoutMs = 30000) {
function isSudoAvailable() {
if (IS_WIN) return false;
try {
execSync("command -v sudo", { stdio: "ignore" });
execSync("command -v sudo", { stdio: "ignore", windowsHide: true });
return true;
} catch {
return false;
@@ -78,8 +78,8 @@ function execWithPassword(command, password) {
return new Promise((resolve, reject) => {
const useSudo = isSudoAvailable();
const child = useSudo
? spawn("sudo", ["-S", "sh", "-c", command], { stdio: ["pipe", "pipe", "pipe"] })
: spawn("sh", ["-c", command], { stdio: ["ignore", "pipe", "pipe"] });
? spawn("sudo", ["-S", "sh", "-c", command], { stdio: ["pipe", "pipe", "pipe"], windowsHide: true })
: spawn("sh", ["-c", command], { stdio: ["ignore", "pipe", "pipe"], windowsHide: true });
let stdout = "";
let stderr = "";
+7 -6
View File
@@ -76,7 +76,7 @@ function getProcessUsingPort443() {
if (processMatch) return processMatch[1].replace(".exe", "");
}
} else {
const result = execSync("lsof -i :443", { encoding: "utf8" });
const result = execSync("lsof -i :443", { encoding: "utf8", windowsHide: true });
const lines = result.trim().split("\n");
if (lines.length > 1) return lines[1].split(/\s+/)[0];
}
@@ -110,9 +110,9 @@ function killProcess(pid, force = false, sudoPassword = null) {
const cmd = `pkill -${sig} -P ${pid} 2>/dev/null; kill -${sig} ${pid} 2>/dev/null`;
if (sudoPassword) {
const { execWithPassword } = require("./dns/dnsConfig");
execWithPassword(cmd, sudoPassword).catch(() => exec(cmd, () => { }));
execWithPassword(cmd, sudoPassword).catch(() => exec(cmd, { windowsHide: true }, () => { }));
} else {
exec(cmd, () => { });
exec(cmd, { windowsHide: true }, () => { });
}
}
}
@@ -216,7 +216,7 @@ function getPort443Owner(sudoPassword) {
});
});
} else {
exec(`ps aux | grep "[s]erver.js"`, (err, stdout) => {
exec(`ps aux | grep "[s]erver.js"`, { windowsHide: true }, (err, stdout) => {
if (!stdout?.trim()) return resolve(null);
for (const line of stdout.split("\n")) {
const parts = line.trim().split(/\s+/);
@@ -252,7 +252,7 @@ async function killLeftoverMitm(sudoPassword) {
const { execWithPassword } = require("./dns/dnsConfig");
await execWithPassword(`pkill -SIGKILL -f "${escaped}" 2>/dev/null || true`, sudoPassword).catch(() => { });
} else {
exec(`pkill -SIGKILL -f "${escaped}" 2>/dev/null || true`, () => { });
exec(`pkill -SIGKILL -f "${escaped}" 2>/dev/null || true`, { windowsHide: true }, () => { });
}
await new Promise(r => setTimeout(r, 500));
} catch { /* ignore */ }
@@ -489,7 +489,7 @@ async function startServer(apiKey, sudoPassword) {
].join(" ");
serverProcess = spawn(
"sudo", ["-S", "-E", "sh", "-c", inlineCmd],
{ detached: false, stdio: ["pipe", "pipe", "pipe"] }
{ detached: false, windowsHide: true, stdio: ["pipe", "pipe", "pipe"] }
);
serverProcess.stdin.write(`${sudoPassword}\n`);
serverProcess.stdin.end();
@@ -497,6 +497,7 @@ async function startServer(apiKey, sudoPassword) {
// Docker/minimal images: no sudo — same as Windows-style direct spawn
serverProcess = spawn(process.execPath, [SERVER_PATH], {
detached: false,
windowsHide: true,
stdio: ["ignore", "pipe", "pipe"],
env: {
...process.env,
+1 -1
View File
@@ -222,7 +222,7 @@ const server = https.createServer(sslOptions, async (req, res) => {
// Kill any process occupying LOCAL_PORT before binding
function killPort(port) {
try {
const pids = execSync(`lsof -ti :${port}`, { encoding: "utf-8" }).trim();
const pids = execSync(`lsof -ti :${port}`, { encoding: "utf-8", windowsHide: true }).trim();
if (!pids) return;
const pidList = pids.split("\n");
pidList.forEach(pid => {