(req, res, body)
| 74 | const log = { apiRoutes: {}, rpcTools: {}, errors: [] }; |
| 75 | |
| 76 | const forward = async (req, res, body) => { |
| 77 | const route = req.url.split('?')[0]; |
| 78 | if (route === '/rpc' && body.length > 0) { |
| 79 | try { |
| 80 | const parsed = JSON.parse(body.toString('utf8')); |
| 81 | const tool = parsed?.params?.name; |
| 82 | if (typeof tool === 'string') { |
| 83 | log.rpcTools[tool] = (log.rpcTools[tool] ?? 0) + 1; |
| 84 | } |
| 85 | } catch { |
| 86 | // Ein unlesbarer Rumpf ist die Sache des Servers, nicht des Proxys. |
| 87 | } |
| 88 | } else if (route.startsWith('/api')) { |
| 89 | log.apiRoutes[route] = (log.apiRoutes[route] ?? 0) + 1; |
| 90 | } |
| 91 | |
| 92 | const headers = { ...req.headers }; |
| 93 | // Die beiden Koepfe, um die es geht. Alles andere geht unveraendert durch. |
| 94 | headers.host = `127.0.0.1:${upstreamPort}`; |
| 95 | delete headers.origin; |
| 96 | delete headers.referer; |
| 97 | delete headers['accept-encoding']; |
| 98 | |
| 99 | const upstream = http.request( |
| 100 | { |
| 101 | host: '127.0.0.1', |
| 102 | port: upstreamPort, |
| 103 | method: req.method, |
| 104 | path: req.url, |
| 105 | headers, |
| 106 | }, |
| 107 | (upstreamRes) => { |
| 108 | res.writeHead(upstreamRes.statusCode ?? 502, upstreamRes.headers); |
| 109 | upstreamRes.pipe(res); |
| 110 | }, |
| 111 | ); |
| 112 | upstream.on('error', (error) => { |
| 113 | log.errors.push(`${req.method} ${req.url}: ${error.message}`); |
| 114 | res.writeHead(502, { 'Content-Type': 'application/json' }); |
| 115 | res.end(JSON.stringify({ error: `upstream unreachable: ${error.message}` })); |
| 116 | }); |
| 117 | if (body.length > 0) { |
| 118 | upstream.write(body); |
| 119 | } |
| 120 | upstream.end(); |
| 121 | }; |
| 122 | |
| 123 | const server = http.createServer((req, res) => { |
| 124 | const route = (req.url ?? '/').split('?')[0]; |
no test coverage detected