Spaces:
Running
Running
File size: 1,350 Bytes
abed4cc 112d6b3 7b1a25a abed4cc dba8c99 3434477 dba8c99 7b1a25a 43f02ea 17ee36d dba8c99 17ee36d 43f02ea 7b1a25a 43f02ea dba8c99 7b1a25a dba8c99 43f02ea 17ee36d 43f02ea 17ee36d 43f02ea |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
const express = require('express');
const proxy = require('express-http-proxy');
const url = require('url');
const app = express();
const targetUrl = 'https://chatgpt.com/';
// Функция для перезаписи URL в ответах
function rewriteURLs(body, req) {
const proxyUrl = req.protocol + '://' + req.get('host');
const bodyString = body.toString('utf8'); // Преобразуем буфер в строку UTF-8
const newBody = bodyString.replace(new RegExp(targetUrl, 'g'), proxyUrl);
return Buffer.from(newBody, 'utf8'); // Преобразуем строку обратно в буфер UTF-8
}
app.use('/', proxy(targetUrl, {
proxyReqPathResolver: (req) => {
return url.parse(req.url).path;
},
proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
proxyReqOpts.headers['Host'] = new URL(targetUrl).host;
proxyReqOpts.headers['Referer'] = targetUrl;
return proxyReqOpts;
},
userResDecorator: (proxyRes, proxyResData, userReq, userRes) => {
const contentType = proxyRes.headers['content-type'];
if (contentType && contentType.includes('text/html')) {
const body = rewriteURLs(proxyResData, userReq);
return body;
}
return proxyResData;
},
}));
const port = 7860;
app.listen(port, () => {
console.log(`Сервер запущен на порту ${port}`);
}); |