Add expiring panel auth sessions

This commit is contained in:
2026-04-02 00:45:27 +03:00
parent e342693211
commit 69c97ea387
11 changed files with 514 additions and 93 deletions

View File

@@ -1,6 +1,7 @@
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 { ThreeProxyManager } from './lib/runtime';
import { StateStore } from './lib/store';
@@ -10,17 +11,30 @@ 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 app = createApp({ store, runtime, runtimeRootDir });
const app = createApp({ store, runtime, runtimeRootDir, auth });
app.listen(port, () => {
console.log(`Panel server listening on http://0.0.0.0:${port}`);
});