feat(m4): LAN playtest run script (devbox run serve) (#80)
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.
This commit is contained in:
+27
-13
@@ -25,7 +25,7 @@ import net from 'node:net'
|
||||
import path from 'node:path'
|
||||
import tls from 'node:tls'
|
||||
import { sleep } from './lib.mjs'
|
||||
import { ensureDevCert } from './tlsdev.mjs'
|
||||
import { ensureCert } from './tlsdev.mjs'
|
||||
|
||||
// Every TLS client in this process (global fetch, the global WebSocket, node:http2) must trust the
|
||||
// certificate that was generated milliseconds ago. NODE_EXTRA_CA_CERTS can only be read at startup,
|
||||
@@ -37,12 +37,13 @@ function trustCert(certPath) {
|
||||
if (!current.includes(pem)) tls.setDefaultCACertificates([...current, pem])
|
||||
}
|
||||
|
||||
function config({ prefix, port, backendPort, stateDir, distDir, certPath, keyPath }) {
|
||||
function config({ prefix, port, backendPort, stateDir, distDir, certPath, keyPath,
|
||||
bindHost = '127.0.0.1', serverName = 'localhost', tag = 'test only' }) {
|
||||
const temp = p => path.join(prefix, p)
|
||||
// `alias` needs the tiles root with its trailing slash; state dirs are harness temp dirs, so no
|
||||
// quoting surprises (nginx does not take a quoted path here).
|
||||
const tilesRoot = path.join(stateDir, 'tiles')
|
||||
return `# generated by tools/nginx.mjs; test only
|
||||
return `# generated by tools/nginx.mjs; ${tag}
|
||||
daemon off;
|
||||
worker_processes 1;
|
||||
pid ${temp('nginx.pid')};
|
||||
@@ -71,12 +72,12 @@ http {
|
||||
server {
|
||||
# TLS only, h2 by ALPN (#66): the production listener is 443/TLS and a browser will not speak
|
||||
# h2 to a plaintext port. http2 on is the nginx >= 1.25.1 form of the old 'listen ... http2' directive.
|
||||
listen 127.0.0.1:${port} ssl;
|
||||
listen ${bindHost}:${port} ssl;
|
||||
http2 on;
|
||||
ssl_certificate ${certPath};
|
||||
ssl_certificate_key ${keyPath};
|
||||
ssl_protocols TLSv1.2 TLSv1.3;
|
||||
server_name localhost;
|
||||
server_name ${serverName};
|
||||
|
||||
location = /healthz { proxy_pass http://127.0.0.1:${backendPort}; }
|
||||
|
||||
@@ -135,11 +136,11 @@ http {
|
||||
`
|
||||
}
|
||||
|
||||
async function portIsFree(port) {
|
||||
async function portIsFree(port, host = '127.0.0.1') {
|
||||
return await new Promise(resolve => {
|
||||
const srv = net.createServer()
|
||||
srv.once('error', () => resolve(false))
|
||||
srv.listen(port, '127.0.0.1', () => srv.close(() => resolve(true)))
|
||||
srv.listen(port, host, () => srv.close(() => resolve(true)))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -161,25 +162,38 @@ function probeHealth(origin) {
|
||||
// stopTestNginx(); the config, certificate and logs live under <stateDir>/nginx/ for post-mortem
|
||||
// greps. `distDir` is the static client root (`/`); a caller that only proves the API surface can
|
||||
// leave it missing (every unknown path is then a plain 404).
|
||||
export async function startTestNginx({ backendPort, stateDir, port, distDir }) {
|
||||
if (!(await portIsFree(port))) throw new Error(`test nginx port ${port} is already in use`)
|
||||
//
|
||||
// `bindHost`/`serverName` default to loopback (every test stack); `devbox run serve` passes the
|
||||
// detected LAN address so nginx, not the server, is the LAN listener. `certDir`/`certPath` let the
|
||||
// caller supply a persistent certificate instead of the per-run loopback one; `tag` labels the
|
||||
// generated config header.
|
||||
export async function startTestNginx({ backendPort, stateDir, port, distDir,
|
||||
bindHost = '127.0.0.1', serverName = 'localhost',
|
||||
originHost, certDir, certPath, keyPath, certOpts, tag }) {
|
||||
if (!(await portIsFree(port, bindHost))) {
|
||||
throw new Error(`test nginx port ${port} on ${bindHost} is already in use`)
|
||||
}
|
||||
const prefix = path.join(stateDir, 'nginx')
|
||||
fs.mkdirSync(prefix, { recursive: true })
|
||||
for (const d of ['client_body', 'proxy', 'fastcgi', 'uwsgi', 'scgi']) {
|
||||
fs.mkdirSync(path.join(prefix, d), { recursive: true })
|
||||
}
|
||||
const { certPath, keyPath } = ensureDevCert(path.join(prefix, 'tls'))
|
||||
if (!certPath || !keyPath) {
|
||||
const cert = ensureCert(certDir ?? path.join(prefix, 'tls'), certOpts)
|
||||
certPath = cert.certPath
|
||||
keyPath = cert.keyPath
|
||||
}
|
||||
trustCert(certPath)
|
||||
const docRoot = distDir ?? path.join(stateDir, 'dist')
|
||||
const confPath = path.join(prefix, 'nginx.conf')
|
||||
fs.writeFileSync(confPath, config({ prefix, port, backendPort, stateDir, distDir: docRoot,
|
||||
certPath, keyPath }))
|
||||
certPath, keyPath, bindHost, serverName, tag }))
|
||||
const logFd = fs.openSync(path.join(prefix, 'stderr.log'), 'a')
|
||||
const child = spawn('nginx', ['-p', prefix, '-c', confPath, '-e', path.join(prefix, 'error.log')],
|
||||
{ stdio: ['ignore', logFd, logFd] })
|
||||
const handle = {
|
||||
port, origin: `https://127.0.0.1:${port}`, prefix, confPath, child, certPath, keyPath,
|
||||
distDir: docRoot,
|
||||
port, origin: `https://${originHost ?? bindHost}:${port}`, prefix, confPath, child, certPath,
|
||||
keyPath, distDir: docRoot, bindHost,
|
||||
log: () => {
|
||||
const read = f => { try { return fs.readFileSync(path.join(prefix, f), 'utf8') } catch { return '' } }
|
||||
return `--- access.log ---\n${read('access.log')}\n--- error.log ---\n${read('error.log')}\n` +
|
||||
|
||||
Reference in New Issue
Block a user