// SoftwareCannery brintOS image server — HTTPS + Basic Auth, COOP/COEP for SAB. // Runs on the DO droplet; phones/laptops open https://: with the password. import https from 'node:https'; import { readFile } from 'node:fs/promises'; import { extname, join, normalize } from 'node:path'; import { fileURLToPath } from 'node:url'; const ROOT = fileURLToPath(new URL('./dist/', import.meta.url)); const PORT = process.env.PORT || 8443; const USER = process.env.BRINTOS_USER || 'softwarecannery'; const PASS = process.env.BRINTOS_PASS || ''; // REQUIRED, set in env const KEY = process.env.TLS_KEY || './tls/key.pem'; const CRT = process.env.TLS_CRT || './tls/cert.pem'; if (!PASS) { console.error('Set BRINTOS_PASS='); process.exit(1); } const TYPES = { '.html':'text/html','.js':'text/javascript','.mjs':'text/javascript','.wasm':'application/wasm','.json':'application/json','.png':'image/png','.css':'text/css' }; const AUTH = 'Basic ' + Buffer.from(`${USER}:${PASS}`).toString('base64'); const [key, cert] = await Promise.all([readFile(KEY), readFile(CRT)]); https.createServer({ key, cert }, async (req, res) => { if (req.headers.authorization !== AUTH) { res.writeHead(401, { 'www-authenticate': 'Basic realm="brintOS"', 'content-type': 'text/plain' }); res.end('auth required'); return; } let p = decodeURIComponent(new URL(req.url, 'https://x').pathname); if (p === '/' || p === '') p = '/index.html'; const file = normalize(join(ROOT, p)); const h = { 'cross-origin-opener-policy': 'same-origin', 'cross-origin-embedder-policy': 'require-corp', 'cross-origin-resource-policy': 'cross-origin', 'cache-control': 'no-store', 'x-content-type-options': 'nosniff', }; if (!file.startsWith(ROOT)) { res.writeHead(403, h); res.end(); return; } try { const body = await readFile(file); h['content-type'] = TYPES[extname(file)] || 'application/octet-stream'; res.writeHead(200, h); res.end(body); } catch { res.writeHead(404, { 'content-type': 'text/plain', ...h }); res.end('404 ' + p); } }).listen(PORT, '0.0.0.0', () => console.log(`brintOS image server https://0.0.0.0:${PORT} user=${USER}`));