File size: 1,794 Bytes
c263ea8
b483f64
 
3963725
b483f64
4eeec78
50a7ee3
 
 
 
 
 
 
 
 
 
 
 
 
 
3963725
50a7ee3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b483f64
3963725
50a7ee3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from flask import Flask, request, Response
import requests

app = Flask(__name__)

PROXY_TARGET = "https://chatpro.ai-pro.org"

@app.route('/')
def home():
    global PROXY_TARGET
    # 获取原始HTTP GET请求的参数
    resp = requests.get(f"{PROXY_TARGET}{request.full_path}")
    # 返回从目标网站获取的响应数据
    excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
    headers = [(name, value) for (name, value) in resp.raw.headers.items()
               if name.lower() not in excluded_headers]
    response = Response(resp.content, resp.status_code, headers)
    return response

@app.route('/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE'])
def proxy(path):
    global PROXY_TARGET
    # 构造目标URL
    url = f"{PROXY_TARGET}/{path}"
    # 根据原始请求的方法来发送对应的请求到目标服务器
    if request.method == 'GET':
        resp = requests.get(url, stream=True)
    elif request.method == 'POST':
        resp = requests.post(url, json=request.json, stream=True)
    elif request.method == 'PUT':
        resp = requests.put(url, json=request.json, stream=True)
    elif request.method == 'DELETE':
        resp = requests.delete(url, stream=True)
    
    # 将从目标网站获取的响应返回给客户端
    excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
    headers = [(name, value) for (name, value) in resp.raw.headers.items()
               if name.lower() not in excluded_headers]
    
    def generate():
        for chunk in resp.iter_content(chunk_size=8192):
            yield chunk
    
    return Response(generate(), resp.status_code, headers)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860, debug=True)