Download server/config.js from abr00kx/taper: direct link, hf CLI and curl.
- Browser
- Download file 8.55 kB
-
https://huggingface.co/abr00kx/taper/resolve/main/server/config.js
- Command line
-
hf download hf://abr00kx/taper/server/config.js
-
curl -L -o config.js https://huggingface.co/abr00kx/taper/resolve/main/server/config.js
8.55 kB
| // Configuration: every environment variable the server reads, parsed and | |
| // checked once at startup. Owns nothing at run time except `limits`, which | |
| // operators change through the admin socket. Trust boundary: the operator's | |
| // environment; anything invalid stops the server with a message. | |
| import { readFileSync } from 'node:fs'; | |
| import { resolve as resolvePath } from 'node:path'; | |
| import { fileURLToPath } from 'node:url'; | |
| import { randomBytes } from 'node:crypto'; | |
| import { RETENTION_CEILING_MS } from './events.js'; | |
| import { parseProxy } from './socks.js'; | |
| import { loadRoots } from './webauthn.js'; | |
| import { parseOperators } from './dms.js'; | |
| import { b64url, isLoopback } from './util.js'; | |
| // A configuration error at startup: say what is wrong, and stop. | |
| export const die = (message) => { console.error(message); process.exit(1); }; | |
| // Version 3 onion addresses only (56 base32 characters); v2 is long gone. | |
| const isOnion = (hostname) => /^(?:[a-z0-9-]+\.)?[a-z2-7]{56}\.onion$/.test(hostname); | |
| // A secret path needs at least 128 bits of randomness: 22 base64url | |
| // characters. The placeholder from the example configuration is not one. | |
| const isSecretPath = (p) => /^\/[A-Za-z0-9_\-/]*[A-Za-z0-9_-]{22,}[A-Za-z0-9_\-/]*$/.test(p) && !/REPLACE-WITH-/i.test(p); | |
| export function loadConfig(env = process.env) { | |
| // A whole number of milliseconds from the environment, or the fallback. | |
| const envMs = (name, fallback) => { | |
| const v = env[name]; | |
| if (v === undefined || v === '') return fallback; | |
| const n = Number(v); | |
| if (!Number.isSafeInteger(n) || n < 0) die(`${name} must be a whole number of milliseconds`); | |
| return n; | |
| }; | |
| const list = (v) => (v ?? '').split(',').map((x) => x.trim()).filter(Boolean); | |
| const RETENTION_MS = Math.min(Number(env.TAPER_RETENTION_MS ?? RETENTION_CEILING_MS), RETENTION_CEILING_MS); | |
| const cfg = { | |
| PORT: Number(env.PORT ?? 8080), | |
| HOST: env.HOST ?? '127.0.0.1', | |
| TLS_CERT: env.TAPER_TLS_CERT, | |
| TLS_KEY: env.TAPER_TLS_KEY, | |
| TOKEN_TTL_MS: Number(env.TAPER_TOKEN_TTL_MS ?? 7 * 86_400_000), | |
| TOKEN_MAX_MS: Number(env.TAPER_TOKEN_MAX_MS ?? 30 * 86_400_000), // however much it is used | |
| // TLS 1.3 only. Prefer hybrid post-quantum key exchange (X25519 + ML-KEM-768), | |
| // falling back to X25519 for clients that don't support it yet. | |
| TLS_GROUPS: 'X25519MLKEM768:X25519', | |
| SERVER_NAME: env.TAPER_SERVER_NAME ?? 'localhost', | |
| // The gateway and federation paths are secrets given to users and peers, not | |
| // fixed locations (Cryptography and Transport, "Baseline"). The defaults are | |
| // for local development; with TLS, the server refuses to start without them. | |
| GATEWAY_PATH: env.TAPER_GATEWAY_PATH ?? '/_taper/gateway/v1', | |
| GATEWAY_PATH_SET: Boolean(env.TAPER_GATEWAY_PATH), | |
| FED_PATH: env.TAPER_FEDERATION_PATH ?? '/_taper/federation/v1', | |
| // Static files for the cover website. Without it, a plain default page. | |
| COVER_DIR: env.TAPER_COVER_DIR ? resolvePath(env.TAPER_COVER_DIR) : null, | |
| // The web client, served under the secret gateway path (`{path}/`), so one | |
| // link is all a user needs. TAPER_CLIENT_DIR=off turns it off. | |
| CLIENT_DIR: env.TAPER_CLIENT_DIR === 'off' ? null | |
| : resolvePath(env.TAPER_CLIENT_DIR ?? fileURLToPath(new URL('../client/', import.meta.url))), | |
| RETENTION_MS, | |
| // The range a room's own max_lifetime may take. | |
| RETENTION_RANGE: { minMs: Math.min(envMs('TAPER_MIN_RETENTION_MS', 60_000), RETENTION_MS), maxMs: RETENTION_CEILING_MS }, | |
| HEARTBEAT_INTERVAL_MS: Number(env.TAPER_HEARTBEAT_MS ?? 30_000), | |
| // Dispatches are held for a random 25-100% of this window and sent together, | |
| // so delivery timing doesn't mirror sending timing (0 turns batching off). | |
| DISPATCH_BATCH_MS: Number(env.TAPER_DISPATCH_BATCH_MS ?? 200), | |
| MAX_UNAUTH_CONNS: Number(env.TAPER_MAX_UNAUTH_CONNS ?? 256), | |
| DATA_DIR: env.TAPER_DATA_DIR, | |
| KEY_DIR: env.TAPER_KEY_DIR, | |
| KEY_PERIOD_MS: Number(env.TAPER_KEY_PERIOD_MS ?? 86_400_000), | |
| ADMIN_SOCKET: env.TAPER_ADMIN_SOCKET, | |
| // Federation (allowlist only): TAPER_FEDERATION_PEERS="name=wss://host/path,...". | |
| PEERS: new Map(list(env.TAPER_FEDERATION_PEERS).map((entry) => { const i = entry.indexOf('='); return [entry.slice(0, i), entry.slice(i + 1)]; })), | |
| REQUEST_KEY_PERIOD_MS: Number(env.TAPER_REQUEST_KEY_PERIOD_MS ?? 7 * 86_400_000), | |
| // Tor (Cryptography and Transport, "Onion services"). With TAPER_TOR_SOCKS, | |
| // every outbound federation connection goes through Tor's SOCKS port. | |
| TOR_SOCKS: env.TAPER_TOR_SOCKS ? parseProxy(env.TAPER_TOR_SOCKS) : null, | |
| ALLOW_UNPINNED_PEER_ROTATION: env.TAPER_FEDERATION_TRUST_ROTATION === '1', | |
| MAX_MEDIA_BYTES: Number(env.TAPER_MAX_MEDIA_BYTES ?? 16 * 1024 * 1024), | |
| MAX_TOTAL_MEDIA_BYTES: Number(env.TAPER_MAX_MEDIA_TOTAL_BYTES ?? 128 * 1024 * 1024), | |
| CANARIES: new Set(list(env.TAPER_CANARIES)), | |
| OTK_CLAIM_LIMIT: Number(env.TAPER_OTK_CLAIM_LIMIT ?? 4), | |
| OTK_DEVICE_LIMIT: Number(env.TAPER_OTK_DEVICE_LIMIT ?? 16), | |
| // Limits an operator can change while the server runs (see "Operator tools"). | |
| limits: { | |
| rate_burst: 20, // requests a connection may send at once | |
| rate_per_sec: 10, // and per second after that | |
| budget_scale: Number(env.TAPER_BUDGET_SCALE ?? 1), | |
| register_pow_bits: Number(env.TAPER_REGISTER_POW_BITS ?? 20), | |
| registration_open: true, | |
| invite_only: env.TAPER_INVITE_ONLY === '1', | |
| }, | |
| }; | |
| cfg.TLS_OPTIONS = { minVersion: 'TLSv1.3', ecdhCurve: cfg.TLS_GROUPS }; | |
| if (cfg.PEERS.size) { | |
| // Said once, at start: operators should know what they are turning on. | |
| console.warn('Federation is EXPERIMENTAL in this release: media does not cross servers yet.'); | |
| } | |
| for (const [name, url] of cfg.PEERS) { | |
| let u; | |
| try { u = new URL(url); } catch { die(`Federation peer ${name}: invalid URL`); } | |
| // Plain ws:// is safe only where the transport itself is protected: an | |
| // onion service (end-to-end encrypted and authenticated by its address) | |
| // or this machine. Anything else, especially through a Tor exit, needs TLS. | |
| if (u.protocol === 'ws:' && !isOnion(u.hostname) && !isLoopback(u.hostname)) die(`Federation peer ${name}: plain ws:// is only allowed for .onion and loopback addresses; use wss://`); | |
| if (isOnion(u.hostname) && !cfg.TOR_SOCKS) die(`Federation peer ${name} is an onion service: set TAPER_TOR_SOCKS (for example 127.0.0.1:9050)`); | |
| } | |
| try { cfg.WEBAUTHN_ROOTS = loadRoots(env.TAPER_WEBAUTHN_ROOTS); } catch (e) { die(`TAPER_WEBAUTHN_ROOTS: ${e.message}`); } | |
| cfg.WEBAUTHN_ORIGINS = env.TAPER_WEBAUTHN_ORIGINS ? new Set(list(env.TAPER_WEBAUTHN_ORIGINS)) : null; | |
| cfg.TLS = Boolean(cfg.TLS_CERT && cfg.TLS_KEY); | |
| if (!cfg.TLS && !isLoopback(cfg.HOST)) die(`Refusing to listen on ${cfg.HOST} without TLS. Set TAPER_TLS_CERT and TAPER_TLS_KEY, or bind to 127.0.0.1.`); | |
| if (cfg.TLS) { | |
| const suggest = () => `/${b64url(randomBytes(24))}`; | |
| if (!isSecretPath(cfg.GATEWAY_PATH)) die(`With TLS, set TAPER_GATEWAY_PATH to a secret path (at least 22 random base64url characters), for example ${suggest()}`); | |
| if (cfg.PEERS.size && !isSecretPath(cfg.FED_PATH)) die(`With TLS and federation, set TAPER_FEDERATION_PATH to a secret path, for example ${suggest()}`); | |
| } | |
| // The dead man's switch's network path takes signed orders only, but it is a | |
| // secret like the others: no one should learn the server has one. | |
| cfg.DMS_PATH = env.TAPER_DMS_PATH || null; | |
| if (cfg.DMS_PATH && !isSecretPath(cfg.DMS_PATH)) die(`TAPER_DMS_PATH must be a secret path (at least 22 random base64url characters), for example /${b64url(randomBytes(24))}`); | |
| // /.well-known/taper/client reveals the gateway path, so with TLS it is off | |
| // unless the operator turns it on (TAPER_WELL_KNOWN=1). | |
| cfg.WELL_KNOWN = env.TAPER_WELL_KNOWN ? env.TAPER_WELL_KNOWN === '1' : !cfg.TLS; | |
| // Dead man's switch (dms.js, docs/dead-mans-switch.md). | |
| cfg.DMS = { | |
| wipePaths: (env.TAPER_DMS_WIPE_PATHS ?? '').split(':').filter(Boolean).map((p) => resolvePath(p)), | |
| readOperators: () => (env.TAPER_DMS_OPERATORS ? parseOperators(readFileSync(env.TAPER_DMS_OPERATORS, 'utf8')) : new Map()), | |
| quorum: envMs('TAPER_DMS_QUORUM', 1), | |
| windowMs: envMs('TAPER_DMS_WINDOW_MS', 3_600_000), | |
| deadhandMs: envMs('TAPER_DMS_DEADHAND_MS', 0), | |
| graceMs: envMs('TAPER_DMS_GRACE_MS', 86_400_000), | |
| duressDelayMs: envMs('TAPER_DMS_DURESS_DELAY_MS', 15 * 60_000), | |
| checkMs: envMs('TAPER_DMS_CHECK_MS', 60_000), | |
| }; | |
| return cfg; | |
| } | |