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

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +16 -11
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.originalUrl;
10
  },
11
  proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
12
  proxyReqOpts.headers['Host'] = new URL(targetUrl).host;
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;
 
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;