Spaces:
Running
Running
const express = require('express'); | |
const proxy = require('express-http-proxy'); | |
const url = require('url'); | |
const app = express(); | |
const targetUrl = 'https://chatgpt.com/'; | |
// Функция для перезаписи URL в HTML-ответах | |
function rewriteHtmlUrls(body, req) { | |
const parsedUrl = url.parse(req.url); | |
return body.toString().replace( | |
new RegExp('(https?:)?//' + url.parse(targetUrl).host, 'g'), | |
req.protocol + '://' + req.headers.host + parsedUrl.pathname | |
); | |
} | |
app.use('/', proxy(targetUrl, { | |
proxyReqPathResolver: (req) => { | |
return req.originalUrl; | |
}, | |
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 = rewriteHtmlUrls(proxyResData, userReq); | |
return body; | |
} | |
return proxyResData; | |
}, | |
})); | |
const port = 7860; | |
app.listen(port, () => { | |
console.log(`Сервер запущен на порту ${port}`); | |
}); |