Files
security-command.org/apps/backend/src/config.ts
T

87 lines
3.7 KiB
TypeScript

export type BackendConfig = {
host: string;
port: number;
/** Port of the metrics endpoint; 0 disables it. Only published to the host's loopback. */
metricsPort: number;
spawnerSocket: string;
frontendDir: string;
contentDir: string;
jobsFile: string;
/** Directory the jobs service writes triggers for builtin jobs into; empty disables them. */
triggerDir: string;
/** Optional CSS appended to the theme of the frontend, which overrides its colors. */
themeFile: string;
/** Absolute URL of the site, for the sitemap. */
siteUrl: string;
/** Route name to content file, from LEGAL_PAGES like "impressum:impressum.txt". */
legalPages: Map<string, string>;
/** Directory for the status file shown in sandboxes; empty disables it. */
statusDir: string;
build: string;
/** Exact origins allowed to open terminal sessions. Empty allows every origin. */
allowedOrigins: string[];
maxSessions: number;
maxQueue: number;
powDifficulty: number;
sessionSeconds: number;
idleSeconds: number;
idleWarningSeconds: number;
graceSeconds: number;
outputRate: number;
};
function integer(env: NodeJS.ProcessEnv, name: string, fallback: number, minimum = 1): number
{
const value = Number(env[name] ?? fallback);
if (!Number.isInteger(value) || value < minimum) throw new Error(`${name} must be an integer of at least ${minimum}`);
return value;
}
function list(value: string | undefined): string[] {
return (value ?? '').split(',').map((entry) => entry.trim()).filter(Boolean);
}
function legalPages(value: string): Map<string, string>
{
const pages = new Map<string, string>();
for (const pair of list(value))
{
const [route = '', file = ''] = pair.split(':');
if (!/^[a-z0-9-]+$/.test(route) || !file) throw new Error(`LEGAL_PAGES entry "${pair}" must look like route:file`);
pages.set(route, file);
}
return pages;
}
export function loadConfig(env: NodeJS.ProcessEnv): BackendConfig
{
const config: BackendConfig = {
host: env.HOST ?? '0.0.0.0',
port: integer(env, 'PORT', 8080),
metricsPort: integer(env, 'METRICS_PORT', 9090, 0),
spawnerSocket: env.SPAWNER_SOCKET ?? '/run/spawner/spawner.sock',
frontendDir: env.FRONTEND_DIR ?? '/app/public',
contentDir: env.CONTENT_DIR ?? '/content',
jobsFile: env.JOBS_FILE ?? '/config/jobs.toml',
triggerDir: env.TRIGGER_DIR ?? '',
themeFile: env.THEME_FILE ?? '/config/theme.css',
siteUrl: env.SITE_URL ?? 'https://security-command.org',
legalPages: legalPages(env.LEGAL_PAGES ?? 'impressum:impressum.txt,datenschutz:datenschutz.txt'),
statusDir: env.STATUS_DIR ?? '',
build: env.BUILD_SHA ?? 'dev',
allowedOrigins: list(env.ALLOWED_ORIGINS),
maxSessions: integer(env, 'MAX_SESSIONS', 12),
maxQueue: integer(env, 'MAX_QUEUE', 20, 0),
powDifficulty: integer(env, 'POW_DIFFICULTY', 20, 0),
sessionSeconds: integer(env, 'SESSION_SECONDS', 20 * 60),
idleSeconds: integer(env, 'IDLE_SECONDS', 180),
idleWarningSeconds: integer(env, 'IDLE_WARNING_SECONDS', 30),
graceSeconds: integer(env, 'GRACE_SECONDS', 45),
outputRate: integer(env, 'OUTPUT_BYTES_PER_SECOND', 1024 * 1024),
};
if (config.idleWarningSeconds >= config.idleSeconds) throw new Error('IDLE_WARNING_SECONDS must be below IDLE_SECONDS');
if (config.powDifficulty > 28) throw new Error('POW_DIFFICULTY above 28 would take phones minutes');
return config;
}