One command builds the client dist and server binary, then starts the loopback server behind the DESIGN 11.1 TLS/HTTP2 nginx on the detected LAN address. State, GM key and certificate persist under run/lan so a table and a phone's trust survive Ctrl-C/restart; the certificate is regenerated when the address changes. Prints the URL, GM key and Android/laptop trust steps. tools/nginx.mjs and tools/tlsdev.mjs gained the bind/SAN knobs the LAN needs (test defaults unchanged). tools/serve.test.mjs proves h2 + verified TLS on /healthz and /, POST /join, a /ws upgrade on the LAN address, loopback-only server binding, and identical key/cert after a restart.
74 lines
3.5 KiB
JavaScript
74 lines
3.5 KiB
JavaScript
// The TLS material for the test nginx (#66, DESIGN 11.1 / TESTING 11.1) and the `serve` playtest
|
|
// stack (M4 #80). A self-signed certificate is GENERATED at run time, never committed: browsers
|
|
// only negotiate HTTP/2 over TLS, and the session cookie is `Secure`, so the browser e2e, the
|
|
// harnesses and a phone on the LAN have to speak https.
|
|
//
|
|
// One directory per run (`run/tls`, gitignored) is shared by every harness file in the run: the
|
|
// certificate is a trust anchor for the Node side through NODE_EXTRA_CA_CERTS (set by tools/test.sh
|
|
// and client/e2e/run.sh), and nginx loads the same pair. For `devbox run serve` the same helper
|
|
// writes a PERSISTENT pair under the state dir with the LAN IP in the SANs, so the phone trusts it
|
|
// once and keeps trusting it across restarts.
|
|
import { spawnSync } from 'node:child_process'
|
|
import fs from 'node:fs'
|
|
import path from 'node:path'
|
|
|
|
const SUBJECT = '/CN=fff-vtt test nginx'
|
|
const SAN = 'DNS:localhost,IP:127.0.0.1'
|
|
const DAYS = 3
|
|
|
|
function generate(dir, { subject, sans, days }) {
|
|
fs.mkdirSync(dir, { recursive: true })
|
|
const certPath = path.join(dir, 'cert.pem')
|
|
const keyPath = path.join(dir, 'key.pem')
|
|
const r = spawnSync('openssl', [
|
|
'req', '-x509', '-newkey', 'ec', '-pkeyopt', 'ec_paramgen_curve:prime256v1',
|
|
'-nodes', '-sha256', '-days', String(days),
|
|
'-keyout', keyPath, '-out', certPath,
|
|
'-subj', subject,
|
|
// CA:TRUE so the pair is usable as a trust anchor (NODE_EXTRA_CA_CERTS, Android's user CA
|
|
// store) as well as a leaf; serverAuth + digitalSignature so a browser/h2 client accepts it
|
|
// as a TLS server certificate.
|
|
'-addext', `subjectAltName=${sans}`,
|
|
'-addext', 'basicConstraints=critical,CA:TRUE',
|
|
'-addext', 'keyUsage=critical,digitalSignature,keyCertSign',
|
|
'-addext', 'extendedKeyUsage=serverAuth',
|
|
], { encoding: 'utf8' })
|
|
if (r.status !== 0) {
|
|
throw new Error(`openssl could not generate the certificate (is the openssl devbox ` +
|
|
`package installed?):\n${r.stdout}\n${r.stderr}`)
|
|
}
|
|
return { certPath, keyPath }
|
|
}
|
|
|
|
// Ensures <dir>/{cert,key}.pem exist and returns their paths. Idempotent: an existing pair is
|
|
// reused, so a run's harness files start from the same anchor. The generation parameters are
|
|
// recorded next to the pair: if a later call asks for different SANs (the laptop's LAN address
|
|
// changed), the stale pair is regenerated instead of silently not matching the new address.
|
|
export function ensureCert(dir, opts = {}) {
|
|
const subject = opts.subject ?? SUBJECT
|
|
const sans = opts.sans ?? SAN
|
|
const days = opts.days ?? DAYS
|
|
const certPath = path.join(dir, 'cert.pem')
|
|
const keyPath = path.join(dir, 'key.pem')
|
|
const stampPath = path.join(dir, '.cert-opts')
|
|
const want = JSON.stringify({ subject, sans, days })
|
|
if (fs.existsSync(certPath) && fs.existsSync(keyPath)) {
|
|
let have = null
|
|
try { have = fs.readFileSync(stampPath, 'utf8') } catch {}
|
|
if (have === want) return { certPath, keyPath, regenerated: false }
|
|
// No stamp: an existing pair from an older helper. Reuse it; its defaults are the loopback
|
|
// ones this function produced before.
|
|
if (have === null && subject === SUBJECT && sans === SAN && days === DAYS) {
|
|
return { certPath, keyPath, regenerated: false }
|
|
}
|
|
}
|
|
const made = generate(dir, { subject, sans, days })
|
|
fs.writeFileSync(stampPath, want)
|
|
return { ...made, regenerated: true }
|
|
}
|
|
|
|
// The test certificates: loopback SANs, short lifetime, one directory per run.
|
|
export function ensureDevCert(dir) {
|
|
return ensureCert(dir)
|
|
}
|