Rooni commited on
Commit
3a11796
·
verified ·
1 Parent(s): 7b1a25a

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +9 -16
server.js CHANGED
@@ -4,15 +4,6 @@ const url = require('url');
4
  const app = express();
5
  const targetUrl = 'https://chatgpt.com/';
6
 
7
- // Функция для перезаписи URL в HTML-ответах
8
- function rewriteHtmlUrls(body, req) {
9
- const parsedUrl = url.parse(req.url);
10
- return body.toString().replace(
11
- new RegExp('(https?:)?//' + url.parse(targetUrl).host, 'g'),
12
- req.protocol + '://' + req.headers.host + parsedUrl.pathname
13
- );
14
- }
15
-
16
  app.use('/', proxy(targetUrl, {
17
  proxyReqPathResolver: (req) => {
18
  return req.originalUrl;
@@ -22,14 +13,16 @@ app.use('/', proxy(targetUrl, {
22
  proxyReqOpts.headers['Referer'] = targetUrl;
23
  return proxyReqOpts;
24
  },
25
- userResDecorator: (proxyRes, proxyResData, userReq, userRes) => {
26
- const contentType = proxyRes.headers['content-type'];
27
- if (contentType && contentType.includes('text/html')) {
28
- const body = rewriteHtmlUrls(proxyResData, userReq);
29
- return body;
 
 
30
  }
31
- return proxyResData;
32
- },
33
  }));
34
 
35
  const port = 7860;
 
4
  const app = express();
5
  const targetUrl = 'https://chatgpt.com/';
6
 
 
 
 
 
 
 
 
 
 
7
  app.use('/', proxy(targetUrl, {
8
  proxyReqPathResolver: (req) => {
9
  return req.originalUrl;
 
13
  proxyReqOpts.headers['Referer'] = targetUrl;
14
  return proxyReqOpts;
15
  },
16
+ userResDecorator: function (proxyRes, proxyResData, userReq, userRes) {
17
+ let data = proxyResData.toString('utf8');
18
+ if (proxyRes.headers['content-type'] && proxyRes.headers['content-type'].includes('text/html')) {
19
+ data = data.replace(/(https?:\/\/[^\/]+)/g, (match, p1) => {
20
+ if (p1 === targetUrl) return '';
21
+ return new URL(p1, userReq.protocol + '://' + userReq.headers.host).href;
22
+ });
23
  }
24
+ return data;
25
+ }
26
  }));
27
 
28
  const port = 7860;