import { createServer } from 'node:http'; import path from 'node:path'; import fs from 'node:fs/promises'; import { createApp } from './app'; import { AuthService } from './lib/auth'; import { buildRuntimePaths, render3proxyConfig } from './lib/config'; import { LiveSyncServer } from './lib/liveSync'; import { ThreeProxyManager } from './lib/runtime'; import { StateStore } from './lib/store'; const port = Number(process.env.PORT ?? '3000'); const runtimeRootDir = path.resolve(process.env.RUNTIME_DIR ?? 'runtime'); const statePath = path.join(runtimeRootDir, 'state', 'panel-state.json'); const binaryPath = path.resolve(process.env.THREEPROXY_BINARY ?? '/usr/local/bin/3proxy'); const autoStart = process.env.AUTO_START_3PROXY === 'true'; const authLogin = process.env.PANEL_AUTH_LOGIN ?? 'admin'; const authPassword = process.env.PANEL_AUTH_PASSWORD ?? 'proxy-ui-demo'; const sessionTtlHours = Number(process.env.PANEL_SESSION_TTL_HOURS ?? '24'); const sessionTtlMs = Number.isFinite(sessionTtlHours) && sessionTtlHours > 0 ? sessionTtlHours * 60 * 60 * 1000 : 24 * 60 * 60 * 1000; const authSecret = process.env.PANEL_TOKEN_SECRET; const store = new StateStore(statePath); const runtimePaths = buildRuntimePaths(runtimeRootDir); const runtime = new ThreeProxyManager(binaryPath, runtimePaths.configPath, runtimePaths, autoStart); const auth = new AuthService({ login: authLogin, password: authPassword, ttlMs: sessionTtlMs, secret: authSecret, }); async function main() { const initialState = await store.read(); await fs.mkdir(path.dirname(runtimePaths.configPath), { recursive: true }); await fs.writeFile(runtimePaths.configPath, render3proxyConfig(initialState, runtimePaths), 'utf8'); await runtime.initialize(); const liveSync = new LiveSyncServer({ store, runtime, runtimePaths, auth }); await liveSync.initialize(); const app = createApp({ store, runtime, runtimeRootDir, auth, liveSync }); const server = createServer(app); liveSync.attach(server); server.listen(port, () => { console.log(`Panel server listening on http://0.0.0.0:${port}`); }); } main().catch((error) => { console.error(error); process.exitCode = 1; });