mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 20:00:47 +00:00
Fix bug
This commit is contained in:
+12
-11
@@ -205,7 +205,7 @@ function getPort443Owner(sudoPassword) {
|
||||
if (IS_WIN) {
|
||||
const psCmd = `powershell -NonInteractive -WindowStyle Hidden -Command "` +
|
||||
`$c = Get-NetTCPConnection -LocalPort 443 -State Listen -ErrorAction SilentlyContinue | Select-Object -First 1; ` +
|
||||
`if ($c) { $c.OwningProcess } else { 0 }"`;
|
||||
`if ($c) { $c.OwningProcess } else { 0 }"`;
|
||||
exec(psCmd, { windowsHide: true }, (err, stdout) => {
|
||||
if (err) return resolve(null);
|
||||
const pid = parseInt(stdout.trim(), 10);
|
||||
@@ -216,14 +216,14 @@ function getPort443Owner(sudoPassword) {
|
||||
});
|
||||
});
|
||||
} else {
|
||||
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+/);
|
||||
const pid = parseInt(parts[1], 10);
|
||||
if (!isNaN(pid)) return resolve({ pid, name: "node" });
|
||||
}
|
||||
resolve(null);
|
||||
// Only find process actually LISTENING on TCP port 443
|
||||
exec("lsof -nP -iTCP:443 -sTCP:LISTEN -t", { windowsHide: true }, (err, stdout) => {
|
||||
if (err || !stdout?.trim()) return resolve(null);
|
||||
const pid = parseInt(stdout.trim().split("\n")[0], 10);
|
||||
if (!pid || isNaN(pid)) return resolve(null);
|
||||
exec(`ps -p ${pid} -o comm=`, { windowsHide: true }, (e2, out2) => {
|
||||
resolve({ pid, name: (out2?.trim() || "unknown") });
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -391,8 +391,9 @@ async function startServer(apiKey, sudoPassword) {
|
||||
const portStatus = await checkPort443Free();
|
||||
if (portStatus === "in-use" || portStatus === "no-permission") {
|
||||
const owner = await getPort443Owner(sudoPassword);
|
||||
if (owner && owner.name === "node") {
|
||||
log(`Killing orphan node process on port 443 (PID ${owner.pid})...`);
|
||||
const ownerIsNode = owner && (owner.name === "node" || owner.name.includes("node"));
|
||||
if (ownerIsNode) {
|
||||
log(`Killing orphan node process on port 443 (PID ${owner.pid}, name=${owner.name})...`);
|
||||
try {
|
||||
const { execWithPassword } = require("./dns/dnsConfig");
|
||||
await execWithPassword(`kill -9 ${owner.pid}`, sudoPassword);
|
||||
|
||||
+4
-4
@@ -219,12 +219,13 @@ const server = https.createServer(sslOptions, async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Kill any process occupying LOCAL_PORT before binding
|
||||
// Kill only processes LISTENING on LOCAL_PORT (not outbound connections)
|
||||
function killPort(port) {
|
||||
try {
|
||||
const pids = execSync(`lsof -ti :${port}`, { encoding: "utf-8", windowsHide: true }).trim();
|
||||
const pids = execSync(`lsof -nP -iTCP:${port} -sTCP:LISTEN -t`, { encoding: "utf-8", windowsHide: true }).trim();
|
||||
if (!pids) return;
|
||||
const pidList = pids.split("\n");
|
||||
const pidList = pids.split("\n").filter(p => p && Number(p) !== process.pid);
|
||||
if (pidList.length === 0) return;
|
||||
pidList.forEach(pid => {
|
||||
try {
|
||||
process.kill(Number(pid), "SIGKILL");
|
||||
@@ -235,7 +236,6 @@ function killPort(port) {
|
||||
});
|
||||
log(`Killed ${pidList.length} process(es) on port ${port}`);
|
||||
} catch (e) {
|
||||
// lsof exits with status 1 when no process found — that's fine
|
||||
if (e.status !== 1) throw e;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user