Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, Response
|
2 |
+
import requests
|
3 |
+
import os
|
4 |
+
|
5 |
+
app = Flask(__name__)
|
6 |
+
|
7 |
+
# 代理的目标域名
|
8 |
+
TARGET_DOMAIN = os.environ.get("TARGET_DOMAIN", "https://api.chatanywhere.tech") # 允许通过环境变量配置
|
9 |
+
|
10 |
+
@app.route('/', defaults={'path': ''}, methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'])
|
11 |
+
@app.route('/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'])
|
12 |
+
def proxy(path):
|
13 |
+
"""
|
14 |
+
代理所有请求到目标域名。
|
15 |
+
"""
|
16 |
+
target_url = f"{TARGET_DOMAIN}/{path}?{request.query_string.decode()}"
|
17 |
+
|
18 |
+
try:
|
19 |
+
# 构造请求头
|
20 |
+
headers = dict(request.headers)
|
21 |
+
# 删除 Host 头,防止目标服务器出错
|
22 |
+
headers.pop('Host', None)
|
23 |
+
# 删除 Content-Length,让 requests 自动处理
|
24 |
+
headers.pop('Content-Length', None)
|
25 |
+
|
26 |
+
|
27 |
+
# 发送请求
|
28 |
+
response = requests.request(
|
29 |
+
method=request.method,
|
30 |
+
url=target_url,
|
31 |
+
headers=headers,
|
32 |
+
data=request.get_data(), # 获取原始请求体
|
33 |
+
stream=True, # 使用流式传输,处理大文件
|
34 |
+
allow_redirects=False # 禁止重定向,让客户端处理
|
35 |
+
)
|
36 |
+
|
37 |
+
|
38 |
+
# 构造响应
|
39 |
+
excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
|
40 |
+
response_headers = [(name, value) for (name, value) in response.headers.items()
|
41 |
+
if name.lower() not in excluded_headers]
|
42 |
+
|
43 |
+
proxy_response = Response(response.raw.read(), # 使用 raw.read() 读取流式响应
|
44 |
+
response.status_code,
|
45 |
+
headers=response_headers)
|
46 |
+
|
47 |
+
# 添加 CORS 头
|
48 |
+
proxy_response.headers['Access-Control-Allow-Origin'] = '*'
|
49 |
+
proxy_response.headers['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS, PUT, DELETE, PATCH'
|
50 |
+
proxy_response.headers['Access-Control-Allow-Headers'] = 'Content-Type, Authorization'
|
51 |
+
proxy_response.headers['Access-Control-Allow-Credentials'] = 'true'
|
52 |
+
|
53 |
+
return proxy_response
|
54 |
+
|
55 |
+
except requests.exceptions.RequestException as e:
|
56 |
+
print(f"代理请求出错: {e}")
|
57 |
+
return Response(f"代理服务器出错: {e}", status=500)
|
58 |
+
|
59 |
+
except Exception as e:
|
60 |
+
print(f"其他错误: {e}")
|
61 |
+
return Response(f"代理服务器出错: {e}", status=500)
|
62 |
+
|
63 |
+
|
64 |
+
@app.after_request
|
65 |
+
def after_request(response):
|
66 |
+
"""
|
67 |
+
处理 OPTIONS 请求,添加 CORS 头。
|
68 |
+
"""
|
69 |
+
if request.method == 'OPTIONS':
|
70 |
+
response.headers['Access-Control-Allow-Origin'] = '*'
|
71 |
+
response.headers['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS, PUT, DELETE, PATCH'
|
72 |
+
response.headers['Access-Control-Allow-Headers'] = 'Content-Type, Authorization'
|
73 |
+
response.headers['Access-Control-Allow-Credentials'] = 'true'
|
74 |
+
return response
|
75 |
+
|
76 |
+
|
77 |
+
if __name__ == '__main__':
|
78 |
+
port = int(os.environ.get("PORT", 8000))
|
79 |
+
print(f"Flask 服务器已启动,监听 {port} 端口")
|
80 |
+
app.run(debug=True, host='0.0.0.0', port=port)
|