File size: 1,376 Bytes
31e85b5
 
 
 
 
 
 
 
 
 
 
 
 
 
9722d0e
31e85b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from flask import Flask, request, Response
import requests
import os

app = Flask(__name__)


TARGET_DOMAIN = os.getenv("TARGET_API")

@app.route('/', defaults={'path': ''})
@app.route('/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE'])
def proxy(path):
    try:
        target_url = f"{TARGET_DOMAIN}/{path}"
        #print(f"Proxying to: {target_url}")
        headers = {key: value for (key, value) in request.headers if key.lower() not in ('host', 'content-length')}
        headers['Accept-Encoding'] = 'identity'  # 禁止压缩

        resp = requests.request(
            method=request.method,
            url=target_url,
            headers=headers,
            data=request.get_data(),
            cookies=request.cookies,
            params=request.args,
            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]
        headers.append(('Content-Type', 'application/json; charset=utf-8'))

        return Response(resp.iter_content(chunk_size=1024), headers=headers, status=resp.status_code)

    except Exception as e:
        return str(e), 500




if __name__ == '__main__':
    # 必须指定host和port
    app.run(host='0.0.0.0', port=7860, debug=False)