tronbyt/src/server.ts
Corey Johnson 383f6a8143 Switch from unix socket to TCP proxy
Proxy to Go server on 127.0.0.1:8000 instead of unix socket.
Go sees localhost connections as trusted for auto-login.
Removes all the unix socket, IP forwarding, and socket path
plumbing complexity.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-10 19:32:42 -07:00

38 lines
1.1 KiB
TypeScript

import { createProxy, type WsData } from './proxy'
import { isHealthy, isRunning, shutdown, spawn } from './binary'
const DATA_DIR = process.env.DATA_DIR!
const PORT = Number(process.env.PORT) || 3000
const { proxyFetch, websocket } = createProxy(isHealthy, isRunning)
const server = Bun.serve({
port: PORT,
hostname: '::',
idleTimeout: 255,
fetch(req, server) {
if (req.headers.get('upgrade')?.toLowerCase() === 'websocket') {
const url = new URL(req.url)
const protocolHeader = req.headers.get('sec-websocket-protocol')
const protocols = protocolHeader ? protocolHeader.split(',').map((p) => p.trim()) : []
const headers: Record<string, string> = {}
if (protocolHeader) headers['sec-websocket-protocol'] = protocolHeader
if (server.upgrade(req, { data: { path: url.pathname + url.search, protocols }, headers })) return
return new Response('WebSocket upgrade failed', { status: 500 })
}
return proxyFetch(req)
},
websocket,
})
console.log(`Listening on port ${server.port}`)
process.on('SIGTERM', shutdown)
process.on('SIGINT', shutdown)
spawn(DATA_DIR)