diff --git a/src/server/index.js b/src/server/index.js index e83bba9..e2440a5 100644 --- a/src/server/index.js +++ b/src/server/index.js @@ -17,7 +17,7 @@ const APPLY_HISTORY_LIMIT = 10; fs.mkdirSync(settings.dataDir, { recursive: true }); -const SINGBOX_PID_FILE = path.join(settings.dataDir, 'singbox.pid'); +const SINGBOX_PID_FILE = path.join(settings.dataDir, "singbox.pid"); let singboxProcess = null; let singboxStartedAt = null; @@ -52,24 +52,34 @@ function parseSingboxLevel(line, fallback) { // ─── PID helpers ──────────────────────────────────────────────────────────── function saveSingboxPid(pid) { - try { fs.writeFileSync(SINGBOX_PID_FILE, String(pid), 'utf8'); } catch {} + try { + fs.writeFileSync(SINGBOX_PID_FILE, String(pid), "utf8"); + } catch {} } function readSingboxPid() { try { - const pid = parseInt(fs.readFileSync(SINGBOX_PID_FILE, 'utf8').trim(), 10); + const pid = parseInt(fs.readFileSync(SINGBOX_PID_FILE, "utf8").trim(), 10); return Number.isFinite(pid) && pid > 0 ? pid : null; - } catch { return null; } + } catch { + return null; + } } function removeSingboxPid() { - try { fs.unlinkSync(SINGBOX_PID_FILE); } catch {} + try { + fs.unlinkSync(SINGBOX_PID_FILE); + } catch {} } function isPidAlive(pid) { if (!pid) return false; - try { process.kill(pid, 0); return true; } - catch { return false; } + try { + process.kill(pid, 0); + return true; + } catch { + return false; + } } /** @@ -83,8 +93,14 @@ function attachExistingSingbox(pid) { let exitCb = null; singboxProcess = { pid, - kill: (sig = 'SIGTERM') => { try { process.kill(pid, sig); } catch {} }, - once: (event, cb) => { if (event === 'exit') exitCb = cb; }, + kill: (sig = "SIGTERM") => { + try { + process.kill(pid, sig); + } catch {} + }, + once: (event, cb) => { + if (event === "exit") exitCb = cb; + }, }; // Периодически проверяем, что процесс ещё жив @@ -95,13 +111,16 @@ function attachExistingSingbox(pid) { singboxProcess = null; singboxStartedAt = null; removeSingboxPid(); - pushLog('warning', `sing-box (pid=${pid}) завершился`); + pushLog("warning", `sing-box (pid=${pid}) завершился`); + } + if (exitCb) { + exitCb(null, null); + exitCb = null; } - if (exitCb) { exitCb(null, null); exitCb = null; } } }, 2000); - pushLog('info', `sing-box подхвачен при старте (pid=${pid})`); + pushLog("info", `sing-box подхвачен при старте (pid=${pid})`); } function captureStream(stream, fallbackLevel) {