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.
150 lines
5.6 KiB
JavaScript
150 lines
5.6 KiB
JavaScript
// The `devbox run serve` stack (M4 #80): the real server on loopback behind the TLS/HTTP2 nginx
|
|
// vhost of DESIGN 11.1, listening on the laptop's LAN address, with PERSISTENT state and TLS
|
|
// material under one directory so a table survives Ctrl-C and a phone trusts the certificate once.
|
|
//
|
|
// This is the dev playtest path, not the M6 NixOS deployment: no systemd, no hardening, no
|
|
// backups. It composes the pieces the test harnesses already use (tools/withserver.mjs for the
|
|
// server, tools/nginx.mjs for the vhost, tools/tlsdev.mjs for the certificate) but with the knobs
|
|
// those helpers need for the LAN: a non-loopback bind for nginx, a long-lived certificate whose
|
|
// SAN is the LAN address, and a state dir it never deletes.
|
|
//
|
|
// The server itself still binds 127.0.0.1 (DESIGN 3.4 T15 / config.BindAddress): nginx is the only
|
|
// LAN listener, so the playtest keeps the same trust boundary as production.
|
|
import { spawnSync } from 'node:child_process'
|
|
import fs from 'node:fs'
|
|
import net from 'node:net'
|
|
import os from 'node:os'
|
|
import path from 'node:path'
|
|
import { projectRoot } from './lib.mjs'
|
|
import { gmKeyOf, startServer, stopServer } from './withserver.mjs'
|
|
import { startTestNginx, stopTestNginx } from './nginx.mjs'
|
|
import { ensureCert } from './tlsdev.mjs'
|
|
|
|
export const DEFAULT_STATE_DIR = 'run/lan'
|
|
export const DEFAULT_LAN_PORT = 8443
|
|
export const DEFAULT_DIST = path.join(projectRoot, 'client', 'dist')
|
|
export const DEFAULT_SERVER_BIN = path.join(projectRoot, 'run', 'fff-vtt')
|
|
|
|
const isIPv4 = s => /^\d{1,3}(\.\d{1,3}){3}$/.test(s)
|
|
|
|
// The LAN address a phone should dial. `ip route get` names the interface the default route uses,
|
|
// which is the one a same-Wi-Fi phone can reach; networkInterfaces is the portable fallback.
|
|
export function detectLanIp() {
|
|
if (process.platform === 'linux') {
|
|
const r = spawnSync('ip', ['-4', 'route', 'get', '1.1.1.1'], { encoding: 'utf8' })
|
|
if (r.status === 0) {
|
|
const m = /\bsrc\s+(\d{1,3}(?:\.\d{1,3}){3})\b/.exec(r.stdout)
|
|
if (m && !m[1].startsWith('127.')) return m[1]
|
|
}
|
|
}
|
|
const candidates = []
|
|
for (const list of Object.values(os.networkInterfaces())) {
|
|
for (const a of list ?? []) {
|
|
if (a.family !== 'IPv4' || a.internal) continue
|
|
candidates.push(a.address)
|
|
}
|
|
}
|
|
const privateFirst = candidates.find(a => /^(10\.|192\.168\.|172\.(1[6-9]|2\d|3[01])\.)/.test(a))
|
|
return privateFirst ?? candidates[0] ?? null
|
|
}
|
|
|
|
// A port free on a specific interface (freePort() in lib.mjs probes loopback only, which does not
|
|
// prove the LAN address can bind it).
|
|
export async function freePortOn(host) {
|
|
const srv = net.createServer()
|
|
await new Promise((res, rej) => { srv.once('error', rej); srv.listen(0, host, res) })
|
|
const { port } = srv.address()
|
|
await new Promise(res => srv.close(res))
|
|
return port
|
|
}
|
|
|
|
function certSans(address) {
|
|
return isIPv4(address) ? `IP:${address}` : `DNS:${address}`
|
|
}
|
|
|
|
/**
|
|
* Starts the playtest stack. Resolves once nginx answers /healthz on the LAN address.
|
|
*
|
|
* opts: { stateDir = 'run/lan', host = detectLanIp(), port = 8443, distDir = client/dist,
|
|
* bin = run/fff-vtt, serverPort = <free loopback> }
|
|
*
|
|
* `host` is the nginx bind address. `0.0.0.0` binds every interface; the certificate and the
|
|
* printed URL then use the detected LAN address. The server always listens on 127.0.0.1.
|
|
*/
|
|
export async function startLanServe(opts = {}) {
|
|
const bindHost = opts.host ?? detectLanIp()
|
|
if (!bindHost) {
|
|
throw new Error('no non-loopback IPv4 address found; pass --host=<address> ' +
|
|
'(or FFF_VTT_LAN_HOST) with the laptop\'s LAN IP')
|
|
}
|
|
const originAddress = bindHost === '0.0.0.0' ? (detectLanIp() ?? (() => {
|
|
throw new Error('--host=0.0.0.0 needs a detected LAN address for the URL/certificate; ' +
|
|
'pass the LAN IP explicitly instead')
|
|
})()) : bindHost
|
|
const stateDir = path.resolve(opts.stateDir ?? DEFAULT_STATE_DIR)
|
|
const port = opts.port ?? DEFAULT_LAN_PORT
|
|
const distDir = path.resolve(opts.distDir ?? DEFAULT_DIST)
|
|
const bin = opts.bin
|
|
if (bin !== undefined && !fs.existsSync(bin)) {
|
|
throw new Error(`server binary missing at ${bin}: run \`devbox run serve\` (it builds the ` +
|
|
`server and client first) or \`nim c ... -o:run/fff-vtt server/main.nim\``)
|
|
}
|
|
fs.mkdirSync(stateDir, { recursive: true })
|
|
|
|
// Persistent certificate: the phone trusts it once; a changed LAN address regenerates it (and the
|
|
// caller is told to trust the new one again).
|
|
const cert = ensureCert(path.join(stateDir, 'tls'), {
|
|
subject: '/CN=fff-vtt lan',
|
|
sans: certSans(originAddress),
|
|
days: 825,
|
|
})
|
|
|
|
const gmKey = gmKeyOf(stateDir, bin)
|
|
const serverPort = opts.serverPort ?? await freePortOn('127.0.0.1')
|
|
const publicOrigin = `https://${originAddress}:${port}`
|
|
|
|
const server = await startServer({ stateDir, port: serverPort, publicOrigin, bin })
|
|
let nginx
|
|
try {
|
|
nginx = await startTestNginx({
|
|
backendPort: server.port,
|
|
stateDir,
|
|
port,
|
|
distDir,
|
|
bindHost,
|
|
serverName: originAddress,
|
|
originHost: originAddress,
|
|
certPath: cert.certPath,
|
|
keyPath: cert.keyPath,
|
|
tag: 'dev serve (LAN playtest)',
|
|
})
|
|
} catch (e) {
|
|
await stopServer(server)
|
|
throw e
|
|
}
|
|
|
|
let stopped = false
|
|
return {
|
|
stateDir,
|
|
host: originAddress,
|
|
bindHost,
|
|
port,
|
|
origin: nginx.origin,
|
|
publicOrigin,
|
|
gmKey,
|
|
certPath: cert.certPath,
|
|
keyPath: cert.keyPath,
|
|
certRegenerated: cert.regenerated,
|
|
distDir,
|
|
serverPort: server.port,
|
|
server,
|
|
nginx,
|
|
async stop() {
|
|
if (stopped) return
|
|
stopped = true
|
|
await stopTestNginx(nginx)
|
|
await stopServer(server)
|
|
},
|
|
}
|
|
}
|