ykl45 commited on
Commit
50a7ee3
·
verified ·
1 Parent(s): 4acab79

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +39 -41
main.py CHANGED
@@ -3,47 +3,45 @@ import requests
3
 
4
  app = Flask(__name__)
5
 
6
- # 目标网站的基础URL,修改为你想要代理的目标网站
7
- BASE_URL = 'https://chatgpt.vvqq.top'
8
-
9
- @app.route('/', defaults={'path': ''}, methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS'])
10
- @app.route('/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS'])
 
 
 
 
 
 
 
 
 
 
11
  def proxy(path):
12
- # 构建新的请求 URL,将原始请求的路径和查询参数附加到目标基础URL后
13
- target_url = f"{BASE_URL}/{path}"
14
- if request.query_string:
15
- target_url += f"?{request.query_string.decode('utf-8')}"
16
-
17
- print(f"Target URL: {target_url}")
18
-
19
- # 复制原始请求的头部信息,以便在新请求中使用
20
- headers = {key: value for key, value in request.headers if key != 'Host'}
21
- print(f"Headers: {headers}")
22
-
23
- # 如果存在,复制原始请求的正文
24
- body = request.get_data() if request.method not in ["GET", "HEAD"] else None
25
- print(f"Body: {body}")
26
-
27
- try:
28
- # 使用 requests 发送新请求到目标网站
29
- response = requests.request(
30
- method=request.method,
31
- url=target_url,
32
- headers=headers,
33
- data=body,
34
- allow_redirects=True
35
- )
36
-
37
- # 构建新的响应
38
- proxy_response = Response(response.content, response.status_code)
39
- for key, value in response.headers.items():
40
- proxy_response.headers[key] = value
41
-
42
- return proxy_response
43
-
44
- except requests.exceptions.RequestException as e:
45
- print(f"Request failed: {e}")
46
- return Response("Bad Gateway", status=502)
47
 
48
  if __name__ == '__main__':
49
- app.run(host='0.0.0.0', port=7860)
 
3
 
4
  app = Flask(__name__)
5
 
6
+ PROXY_TARGET = "https://chatgpt.vvqq.top/"
7
+
8
+ @app.route('/')
9
+ def home():
10
+ global PROXY_TARGET
11
+ # 获取原始HTTP GET请求的参数
12
+ resp = requests.get(f"{PROXY_TARGET}{request.full_path}")
13
+ # 返回从目标网站获取的响应数据
14
+ excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
15
+ headers = [(name, value) for (name, value) in resp.raw.headers.items()
16
+ if name.lower() not in excluded_headers]
17
+ response = Response(resp.content, resp.status_code, headers)
18
+ return response
19
+
20
+ @app.route('/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE'])
21
  def proxy(path):
22
+ global PROXY_TARGET
23
+ # 构造目标URL
24
+ url = f"{PROXY_TARGET}/{path}"
25
+ # 根据原始请求的方法来发送对应的请求到目标服务器
26
+ if request.method == 'GET':
27
+ resp = requests.get(url, stream=True)
28
+ elif request.method == 'POST':
29
+ resp = requests.post(url, json=request.json, stream=True)
30
+ elif request.method == 'PUT':
31
+ resp = requests.put(url, json=request.json, stream=True)
32
+ elif request.method == 'DELETE':
33
+ resp = requests.delete(url, stream=True)
34
+
35
+ # 将从目标网站获取的响应返回给客户端
36
+ excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
37
+ headers = [(name, value) for (name, value) in resp.raw.headers.items()
38
+ if name.lower() not in excluded_headers]
39
+
40
+ def generate():
41
+ for chunk in resp.iter_content(chunk_size=8192):
42
+ yield chunk
43
+
44
+ return Response(generate(), resp.status_code, headers)
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  if __name__ == '__main__':
47
+ app.run(host='0.0.0.0', port=7860, debug=True)