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.
159 lines
6.7 KiB
JavaScript
159 lines
6.7 KiB
JavaScript
// M4 #80: the LAN playtest composition. Starts the real server behind the real TLS/HTTP2 nginx on
|
|
// the machine's LAN address (not loopback), proves h2 + a trusted TLS chain on `/healthz` and `/`,
|
|
// a POST /join with the GM key, and a WebSocket upgrade on /ws; then Ctrl-C equivalent (stop),
|
|
// restart on the same address, and proves the state dir, GM key and certificate came back
|
|
// unchanged. Also asserts the server itself never listens off 127.0.0.1 (nginx is the LAN face).
|
|
//
|
|
// `/` is a stub dist here, exactly like tools/vhost.test.mjs: `devbox run test` must not need the
|
|
// client's npm build (the real Vite output is exercised by `devbox run client-test` / `e2e`).
|
|
// `devbox run serve` builds the real dist before calling the same stack.
|
|
import test from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import fs from 'node:fs'
|
|
import http2 from 'node:http2'
|
|
import net from 'node:net'
|
|
import path from 'node:path'
|
|
import { listeningSockets } from './lib.mjs'
|
|
import { makeStateDir, openAs } from './withserver.mjs'
|
|
import { detectLanIp, freePortOn, startLanServe } from './lanserve.mjs'
|
|
|
|
const STUB_MARK = 'lan-serve smoke stub'
|
|
|
|
function writeStubDist(dir) {
|
|
fs.mkdirSync(path.join(dir, 'assets'), { recursive: true })
|
|
const indexHtml = `<!doctype html>
|
|
<html lang="en"><head><meta charset="utf-8"><title>${STUB_MARK}</title></head>
|
|
<body><p>${STUB_MARK}</p></body></html>
|
|
`
|
|
fs.writeFileSync(path.join(dir, 'index.html'), indexHtml)
|
|
return { dir }
|
|
}
|
|
|
|
// One HTTP/2 request over the LAN origin; resolves with the ALPN, TLS authorization, status/headers
|
|
// and the UTF-8 body.
|
|
function h2Request(origin, urlPath, { method = 'GET', body, headers = {} } = {}) {
|
|
const client = http2.connect(origin)
|
|
return new Promise((resolve, reject) => {
|
|
client.once('error', reject)
|
|
const sendHeaders = {
|
|
':method': method,
|
|
':path': urlPath,
|
|
...headers,
|
|
}
|
|
if (body !== undefined) sendHeaders['content-length'] = Buffer.byteLength(body)
|
|
const req = client.request(sendHeaders)
|
|
let resHeaders = null
|
|
const chunks = []
|
|
req.setEncoding('utf8')
|
|
req.on('response', h => { resHeaders = h })
|
|
req.on('data', c => chunks.push(c))
|
|
req.on('error', reject)
|
|
req.on('end', () => {
|
|
const result = {
|
|
alpn: client.alpnProtocol,
|
|
authorized: client.socket?.authorized === true,
|
|
status: Number(resHeaders?.[':status']),
|
|
headers: resHeaders,
|
|
body: chunks.join(''),
|
|
}
|
|
client.close()
|
|
resolve(result)
|
|
})
|
|
if (body !== undefined) req.end(body)
|
|
else req.end()
|
|
})
|
|
}
|
|
|
|
async function canBind(host, port) {
|
|
const srv = net.createServer()
|
|
try {
|
|
await new Promise((res, rej) => { srv.once('error', rej); srv.listen(port, host, res) })
|
|
return true
|
|
} catch { return false } finally {
|
|
await new Promise(res => { try { srv.close(res) } catch { res() } })
|
|
}
|
|
}
|
|
|
|
test('serve: LAN TLS+h2 composition, /join, /ws, and persistent state across a restart', async () => {
|
|
const lanIp = detectLanIp()
|
|
assert.ok(lanIp, 'no non-loopback IPv4 address detected: this smoke test needs a LAN address ' +
|
|
'(set FFF_VTT_LAN_HOST on a host where `ip route get 1.1.1.1` has no source)')
|
|
const stateDir = makeStateDir('fffvtt-serve-')
|
|
const dist = writeStubDist(path.join(stateDir, 'dist'))
|
|
const port = await freePortOn(lanIp)
|
|
let handle = null
|
|
let restarted = null
|
|
try {
|
|
handle = await startLanServe({ stateDir, host: lanIp, port, distDir: dist.dir })
|
|
assert.equal(handle.origin, `https://${lanIp}:${port}`)
|
|
assert.equal(handle.publicOrigin, handle.origin)
|
|
assert.match(handle.gmKey, /^[A-Z0-9]{4}(-[A-Z0-9]{4}){4}$/)
|
|
const gmKey1 = handle.gmKey
|
|
const cert1 = fs.readFileSync(handle.certPath, 'utf8')
|
|
assert.match(cert1, /BEGIN CERTIFICATE/)
|
|
|
|
// The stack is reachable on the LAN address, over h2, with the TLS chain verified.
|
|
const health = await h2Request(handle.origin, '/healthz')
|
|
assert.equal(health.alpn, 'h2', 'ALPN must negotiate h2')
|
|
assert.equal(health.authorized, true, 'TLS chain must verify against the served certificate')
|
|
assert.equal(health.status, 200)
|
|
assert.equal(health.body, '{"status":"ok"}')
|
|
|
|
const index = await h2Request(handle.origin, '/')
|
|
assert.equal(index.alpn, 'h2')
|
|
assert.equal(index.authorized, true)
|
|
assert.equal(index.status, 200)
|
|
assert.match(index.body, new RegExp(STUB_MARK))
|
|
|
|
// POST /join with the GM key, over the same LAN h2 origin.
|
|
const joinBody = new URLSearchParams({ key: handle.gmKey }).toString()
|
|
const joined = await h2Request(handle.origin, '/join', {
|
|
method: 'POST',
|
|
body: joinBody,
|
|
headers: { 'content-type': 'application/x-www-form-urlencoded' },
|
|
})
|
|
assert.equal(joined.status, 303, `join status body=${joined.body}`)
|
|
const setCookie = joined.headers['set-cookie']
|
|
const firstCookie = Array.isArray(setCookie) ? setCookie[0] : setCookie
|
|
assert.match(String(firstCookie ?? ''), /^fff_session=[^;]+;/)
|
|
|
|
// The signed-in WebSocket upgrade on /ws through nginx (the credential is the join cookie).
|
|
const gm = await openAs(handle.origin, handle.gmKey)
|
|
await gm.ws.waitText('hello')
|
|
await gm.ws.waitText('walls')
|
|
gm.ws.close()
|
|
await gm.ws.closed
|
|
|
|
// The server never listens on the LAN address: only 127.0.0.1, and only nginx holds the LAN port.
|
|
const serverSockets = listeningSockets(handle.serverPort)
|
|
assert.ok(serverSockets.length > 0, 'server should listen on its loopback port')
|
|
for (const s of serverSockets) {
|
|
assert.equal(s.addr, '127.0.0.1', `server leaked a ${s.addr} listener (DESIGN 3.4 T15)`)
|
|
}
|
|
const lanSockets = listeningSockets(port)
|
|
assert.ok(lanSockets.some(s => s.addr === lanIp), `nginx should hold ${lanIp}:${port}`)
|
|
|
|
// Ctrl-C: both processes stop and the LAN port is released.
|
|
await handle.stop()
|
|
handle = null
|
|
assert.equal(await canBind(lanIp, port), true, 'the LAN port must be released after stop')
|
|
|
|
// Restart on the same state dir + address: same GM key, same certificate, still works.
|
|
restarted = await startLanServe({ stateDir, host: lanIp, port, distDir: dist.dir })
|
|
assert.equal(restarted.gmKey, gmKey1)
|
|
assert.equal(restarted.certPath, path.join(stateDir, 'tls', 'cert.pem'))
|
|
assert.equal(fs.readFileSync(restarted.certPath, 'utf8'), cert1,
|
|
'the certificate must persist so the phone trusts it once')
|
|
const health2 = await h2Request(restarted.origin, '/healthz')
|
|
assert.equal(health2.status, 200)
|
|
const gm2 = await openAs(restarted.origin, restarted.gmKey)
|
|
await gm2.ws.waitText('hello')
|
|
gm2.ws.close()
|
|
await gm2.ws.closed
|
|
} finally {
|
|
if (handle) await handle.stop()
|
|
if (restarted) await restarted.stop()
|
|
fs.rmSync(stateDir, { recursive: true, force: true })
|
|
}
|
|
})
|