import http from 'node:http'; import net from 'node:net'; import { refused, hostAllowed } from './address.mjs'; import { resolverFor } from './resolve.mjs'; import { isLoopbackHost, loopbackRefusal } from './loopback.mjs'; // The egress proxy is the browser's only way out. It resolves each host // itself and connects to the address it checked, so a name cannot pass // the check with one address and connect to another. A name with any // refused address is refused whole, because the browser could otherwise // be steered to the refused one. export function startProxy({ port, hosts, resolvers = [], loopbackPorts = [], onRefused }) { const addressesOf = resolverFor(resolvers); async function vet(host) { if (!hostAllowed(hosts, host)) return { reason: 'host not in egress.hosts' }; const addresses = net.isIP(host) ? [host] : await addressesOf(host); const bad = addresses.find(refused); if (bad) return { reason: `resolves to refused address ${bad}` }; return { address: addresses[0] }; } // Loopback is admitted only on the ports the profile lists, and only // for the requests that loopbackRefusal accepts. It never goes through // egress.hosts or a DNS lookup. function vetLoopback(host, port, method, headers) { const reason = loopbackRefusal({ ports: loopbackPorts, port, method, headers }); if (reason) return { reason }; return { address: host === '::1' ? '::1' : '127.0.0.1' }; } function refuse(host, reason) { onRefused({ host, reason }); } const server = http.createServer(async (req, res) => { let url; try { url = new URL(req.url); } catch { res.writeHead(400).end(); return; } // URL keeps the brackets around an IPv6 address. const host = url.hostname.replace(/^\[(.*)\]$/, '$1'); const verdict = isLoopbackHost(host) ? vetLoopback(host, url.protocol === 'http:' ? Number(url.port || 80) : 0, req.method, req.headers) : await vet(host).catch(err => ({ reason: err.code ?? String(err) })); if (!verdict.address) { refuse(host, verdict.reason); res.writeHead(403).end(); return; } const upstream = http.request({ host: verdict.address, port: url.port || 80, method: req.method, path: url.pathname + url.search, headers: req.headers, }, up => { res.writeHead(up.statusCode, up.headers); up.pipe(res); }); upstream.on('error', () => res.destroy()); req.pipe(upstream); }); server.on('connect', async (req, client, head) => { const [host, port] = splitHostPort(req.url); const verdict = isLoopbackHost(host) ? vetLoopback(host, port, 'CONNECT', req.headers) : await vet(host).catch(err => ({ reason: err.code ?? String(err) })); if (!verdict.address) { refuse(host, verdict.reason); client.end('HTTP/1.1 403 Forbidden\r\n\r\n'); return; } const upstream = net.connect(port, verdict.address, () => { client.write('HTTP/1.1 200 Connection Established\r\n\r\n'); upstream.write(head); upstream.pipe(client); client.pipe(upstream); }); upstream.on('error', () => client.destroy()); client.on('error', () => upstream.destroy()); }); return new Promise(resolve => server.listen(port, '127.0.0.1', () => resolve(server))); } function splitHostPort(authority) { const match = authority.match(/^\[(.+)\]:(\d+)$/) ?? authority.match(/^(.+):(\d+)$/); return match ? [match[1], Number(match[2])] : [authority, 443]; }