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/'; | |
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: function (proxyRes, proxyResData, userReq, userRes) { | |
let data = proxyResData.toString('utf8'); | |
if (proxyRes.headers['content-type'] && proxyRes.headers['content-type'].includes('text/html')) { | |
data = data.replace(/(https?:\/\/[^\/]+)/g, (match, p1) => { | |
if (p1 === targetUrl) return ''; | |
return new URL(p1, userReq.protocol + '://' + userReq.headers.host).href; | |
}); | |
} | |
return data; | |
} | |
})); | |
const port = 7860; | |
app.listen(port, () => { | |
console.log(`Сервер запущен на порту ${port}`); | |
}); |