Spaces:
Running
Running
Update server.js
Browse files
server.js
CHANGED
@@ -2,27 +2,32 @@ const express = require('express');
|
|
2 |
const proxy = require('express-http-proxy');
|
3 |
const url = require('url');
|
4 |
const app = express();
|
5 |
-
const targetUrl = 'https://chatgpt.com/';
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
app.use('/', proxy(targetUrl, {
|
8 |
proxyReqPathResolver: (req) => {
|
9 |
-
return req.
|
10 |
},
|
11 |
proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
|
12 |
proxyReqOpts.headers['Host'] = new URL(targetUrl).host;
|
13 |
proxyReqOpts.headers['Referer'] = targetUrl;
|
14 |
return proxyReqOpts;
|
15 |
},
|
16 |
-
userResDecorator:
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
});
|
23 |
}
|
24 |
-
return
|
25 |
-
}
|
26 |
}));
|
27 |
|
28 |
const port = 7860;
|
|
|
2 |
const proxy = require('express-http-proxy');
|
3 |
const url = require('url');
|
4 |
const app = express();
|
5 |
+
const targetUrl = 'https://chatgpt.com/';
|
6 |
+
|
7 |
+
// Функция для перезаписи URL в ответах
|
8 |
+
function rewriteURLs(body, req) {
|
9 |
+
const proxyUrl = req.protocol + '://' + req.get('host');
|
10 |
+
return body.toString().replace(new RegExp(targetUrl, 'g'), proxyUrl);
|
11 |
+
}
|
12 |
|
13 |
app.use('/', proxy(targetUrl, {
|
14 |
proxyReqPathResolver: (req) => {
|
15 |
+
return url.parse(req.url).path;
|
16 |
},
|
17 |
proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
|
18 |
proxyReqOpts.headers['Host'] = new URL(targetUrl).host;
|
19 |
proxyReqOpts.headers['Referer'] = targetUrl;
|
20 |
return proxyReqOpts;
|
21 |
},
|
22 |
+
userResDecorator: (proxyRes, proxyResData, userReq, userRes) => {
|
23 |
+
// Перезаписываем URL только для HTML-ответов
|
24 |
+
const contentType = proxyRes.headers['content-type'];
|
25 |
+
if (contentType && contentType.includes('text/html')) {
|
26 |
+
const body = rewriteURLs(proxyResData, userReq);
|
27 |
+
return body;
|
|
|
28 |
}
|
29 |
+
return proxyResData;
|
30 |
+
},
|
31 |
}));
|
32 |
|
33 |
const port = 7860;
|