Fix proxy and split server into modules #1

Merged
probablycorey merged 18 commits from probablycorey/fix-proxy-404s into main 2026-03-11 16:35:00 +00:00
2 changed files with 9 additions and 3 deletions
Showing only changes of commit 24b9629f0f - Show all commits

View File

@ -8,7 +8,7 @@ export interface WsData {
const upstreams = new Map<ServerWebSocket<WsData>, WebSocket>()
export function createProxy(socketPath: string, isHealthy: () => boolean, isRunning: () => boolean) {
async function proxyFetch(req: Request): Promise<Response> {
async function proxyFetch(req: Request, clientIP?: string): Promise<Response> {
const url = new URL(req.url)
if (url.pathname === '/ok') {
@ -20,10 +20,14 @@ export function createProxy(socketPath: string, isHealthy: () => boolean, isRunn
const hasBody = req.method !== 'GET' && req.method !== 'HEAD'
const body = hasBody ? await req.arrayBuffer() : undefined
const headers = new Headers(req.headers)
const forwardedFor = req.headers.get('x-forwarded-for')
headers.set('x-forwarded-for', forwardedFor ? `${forwardedFor}, ${clientIP}` : (clientIP ?? ''))
headers.set('x-real-ip', clientIP ?? '')
return fetch(`http://localhost${url.pathname}${url.search}`, {
method: req.method,
headers: req.headers,
headers,
body,
unix: socketPath,
}).then((r) => {

View File

@ -25,7 +25,9 @@ const server = Bun.serve({
return new Response('WebSocket upgrade failed', { status: 500 })
}
return proxyFetch(req)
const clientIP = req.headers.get('x-forwarded-for')?.split(',')[0]?.trim()
|| server.requestIP(req)?.address
return proxyFetch(req, clientIP)
},
websocket,