| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import http from "node:http"; |
| import https from "node:https"; |
| import { URL } from "node:url"; |
|
|
| const PORT = parseInt(process.env.NV_LOCAL_PORT || "8787", 10); |
| const TARGET = (process.env.NV_PROXY_TARGET || "https://ayoub5550-nvidia-proxy.ayoubteke11.workers.dev").replace(/\/+$/, ""); |
| const DEBUG = process.env.NV_LOCAL_DEBUG === "true"; |
|
|
| const targetUrl = new URL(TARGET); |
|
|
| const server = http.createServer((clientReq, clientRes) => { |
| |
| const chunks = []; |
| clientReq.on("data", (c) => chunks.push(c)); |
| clientReq.on("end", () => { |
| const body = Buffer.concat(chunks); |
| const path = clientReq.url || "/"; |
|
|
| if (DEBUG) { |
| console.error(`[nv-local] ${clientReq.method} ${path} (${body.length} bytes)`); |
| } |
|
|
| const outHeaders = { ...clientReq.headers }; |
| |
| outHeaders.host = targetUrl.host; |
| |
| delete outHeaders.connection; |
| delete outHeaders["transfer-encoding"]; |
| |
| if (body.length > 0) { |
| outHeaders["content-length"] = String(body.length); |
| } |
|
|
| const opts = { |
| hostname: targetUrl.hostname, |
| port: targetUrl.port || 443, |
| path: path, |
| method: clientReq.method, |
| headers: outHeaders, |
| }; |
|
|
| const proxyReq = https.request(opts, (proxyRes) => { |
| if (DEBUG) { |
| console.error(`[nv-local] <- ${proxyRes.statusCode}`); |
| } |
| |
| clientRes.writeHead(proxyRes.statusCode, proxyRes.headers); |
| |
| proxyRes.pipe(clientRes, { end: true }); |
| }); |
|
|
| proxyReq.on("error", (err) => { |
| console.error(`[nv-local] proxy error: ${err.message}`); |
| if (!clientRes.headersSent) { |
| clientRes.writeHead(502, { "content-type": "application/json" }); |
| } |
| clientRes.end(JSON.stringify({ error: "proxy_error", detail: err.message })); |
| }); |
|
|
| |
| if (body.length > 0) { |
| proxyReq.write(body); |
| } |
| proxyReq.end(); |
| }); |
| }); |
|
|
| server.listen(PORT, "127.0.0.1", () => { |
| console.log(`[nv-local] NVIDIA local proxy listening on http://127.0.0.1:${PORT} -> ${TARGET}`); |
| }); |
|
|