Create server.js
Browse files
server.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const http = require('http');
|
| 2 |
+
|
| 3 |
+
const server = http.createServer(async (req, res) => {
|
| 4 |
+
// Wyciągamy api_url z parametrów zapytania
|
| 5 |
+
const parsedUrl = new URL(req.url, `http://${req.headers.host}`);
|
| 6 |
+
const targetUrl = parsedUrl.searchParams.get("api_url");
|
| 7 |
+
|
| 8 |
+
if (!targetUrl) {
|
| 9 |
+
res.writeHead(400, { "Content-Type": "text/plain; charset=utf-8" });
|
| 10 |
+
res.end("Podaj api_url w parametrze");
|
| 11 |
+
return;
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
// Podmieniamy nagłówki dla Chomika
|
| 15 |
+
const headers = { ...req.headers };
|
| 16 |
+
delete headers["host"];
|
| 17 |
+
headers["origin"] = "https://chomikuj.pl";
|
| 18 |
+
headers["referer"] = "https://chomikuj.pl/";
|
| 19 |
+
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";
|
| 20 |
+
|
| 21 |
+
try {
|
| 22 |
+
// Przechwytujemy body dla POST (dane logowania)
|
| 23 |
+
let body = undefined;
|
| 24 |
+
if (req.method !== "GET" && req.method !== "HEAD") {
|
| 25 |
+
const chunks = [];
|
| 26 |
+
for await (const chunk of req) {
|
| 27 |
+
chunks.push(chunk);
|
| 28 |
+
}
|
| 29 |
+
body = Buffer.concat(chunks);
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
// Strzał do Chomika
|
| 33 |
+
const response = await fetch(targetUrl, {
|
| 34 |
+
method: req.method,
|
| 35 |
+
headers: headers,
|
| 36 |
+
body: body,
|
| 37 |
+
redirect: "manual", // Zatrzymujemy przekierowania, żeby złapać ciastka
|
| 38 |
+
});
|
| 39 |
+
|
| 40 |
+
// Przepisujemy nagłówki zwrotne
|
| 41 |
+
const responseHeaders = {};
|
| 42 |
+
response.headers.forEach((value, key) => {
|
| 43 |
+
responseHeaders[key] = value;
|
| 44 |
+
});
|
| 45 |
+
|
| 46 |
+
// Wstrzykujemy CORS dla Satana
|
| 47 |
+
responseHeaders["access-control-allow-origin"] = req.headers["origin"] || "*";
|
| 48 |
+
responseHeaders["access-control-allow-credentials"] = "true";
|
| 49 |
+
|
| 50 |
+
// Odsyłamy odpowiedź
|
| 51 |
+
res.writeHead(response.status, responseHeaders);
|
| 52 |
+
const resBuffer = await response.arrayBuffer();
|
| 53 |
+
res.end(Buffer.from(resBuffer));
|
| 54 |
+
|
| 55 |
+
} catch (e) {
|
| 56 |
+
res.writeHead(500, { "Content-Type": "text/plain; charset=utf-8" });
|
| 57 |
+
res.end("Błąd: " + e.message);
|
| 58 |
+
}
|
| 59 |
+
});
|
| 60 |
+
|
| 61 |
+
// WAŻNE: Hugging Face wymaga nasłuchiwania na porcie 7860
|
| 62 |
+
server.listen(7860, '0.0.0.0', () => {
|
| 63 |
+
console.log('Proxy Angela śmiga na porcie 7860');
|
| 64 |
+
});
|