const http = require('http'); const server = http.createServer(async (req, res) => { // Wyciągamy api_url z parametrów zapytania const parsedUrl = new URL(req.url, `http://${req.headers.host}`); const targetUrl = parsedUrl.searchParams.get("api_url"); if (!targetUrl) { res.writeHead(400, { "Content-Type": "text/plain; charset=utf-8" }); res.end("Podaj api_url w parametrze"); return; } // Podmieniamy nagłówki dla Chomika const headers = { ...req.headers }; delete headers["host"]; headers["origin"] = "https://chomikuj.pl"; headers["referer"] = "https://chomikuj.pl/"; headers["user-agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"; try { // Przechwytujemy body dla POST (dane logowania) let body = undefined; if (req.method !== "GET" && req.method !== "HEAD") { const chunks = []; for await (const chunk of req) { chunks.push(chunk); } body = Buffer.concat(chunks); } // Strzał do Chomika const response = await fetch(targetUrl, { method: req.method, headers: headers, body: body, redirect: "manual", // Zatrzymujemy przekierowania, żeby złapać ciastka }); // Przepisujemy nagłówki zwrotne const responseHeaders = {}; response.headers.forEach((value, key) => { responseHeaders[key] = value; }); // Wstrzykujemy CORS dla Satana responseHeaders["access-control-allow-origin"] = req.headers["origin"] || "*"; responseHeaders["access-control-allow-credentials"] = "true"; // Odsyłamy odpowiedź res.writeHead(response.status, responseHeaders); const resBuffer = await response.arrayBuffer(); res.end(Buffer.from(resBuffer)); } catch (e) { res.writeHead(500, { "Content-Type": "text/plain; charset=utf-8" }); res.end("Błąd: " + e.message); } }); // WAŻNE: Hugging Face wymaga nasłuchiwania na porcie 7860 server.listen(7860, '0.0.0.0', () => { console.log('Proxy Angela śmiga na porcie 7860'); });