asemxin commited on
Commit ·
d08373b
1
Parent(s): fc2d7d3
fix: add catch-all proxy route for /callback/*
Browse files- image_proxy.py +19 -6
image_proxy.py
CHANGED
|
@@ -69,17 +69,25 @@ def proxy_image(image_key):
|
|
| 69 |
log(f"❌ 请求失败: {e}")
|
| 70 |
return make_response(str(e), 502)
|
| 71 |
|
| 72 |
-
@app.route('/open-apis/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE'])
|
| 73 |
-
def
|
| 74 |
-
"""代理
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
token = request.args.get('tenant_access_token', '')
|
| 76 |
-
headers =
|
| 77 |
-
headers.pop('Host', None)
|
| 78 |
|
| 79 |
if token and 'Authorization' not in headers:
|
| 80 |
headers['Authorization'] = f'Bearer {token}'
|
| 81 |
|
| 82 |
-
url = f"
|
| 83 |
params = {k: v for k, v in request.args.items() if k != 'tenant_access_token'}
|
| 84 |
|
| 85 |
try:
|
|
@@ -91,9 +99,14 @@ def proxy_other(path):
|
|
| 91 |
data=request.get_data(),
|
| 92 |
timeout=30
|
| 93 |
)
|
|
|
|
|
|
|
|
|
|
| 94 |
return Response(resp.content, status=resp.status_code,
|
|
|
|
| 95 |
content_type=resp.headers.get('Content-Type', 'application/json'))
|
| 96 |
except Exception as e:
|
|
|
|
| 97 |
return make_response(str(e), 502)
|
| 98 |
|
| 99 |
@app.route('/health')
|
|
|
|
| 69 |
log(f"❌ 请求失败: {e}")
|
| 70 |
return make_response(str(e), 502)
|
| 71 |
|
| 72 |
+
@app.route('/open-apis/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH'])
|
| 73 |
+
def proxy_open_apis(path):
|
| 74 |
+
"""代理 /open-apis/* 请求(透传,修复 token)"""
|
| 75 |
+
return _proxy_to_feishu(f"/open-apis/{path}")
|
| 76 |
+
|
| 77 |
+
@app.route('/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH'])
|
| 78 |
+
def proxy_catch_all(path):
|
| 79 |
+
"""catch-all: 透传所有其他请求(如 /callback/ws/endpoint)"""
|
| 80 |
+
return _proxy_to_feishu(f"/{path}")
|
| 81 |
+
|
| 82 |
+
def _proxy_to_feishu(path):
|
| 83 |
+
"""通用代理:转发请求到 open.feishu.cn"""
|
| 84 |
token = request.args.get('tenant_access_token', '')
|
| 85 |
+
headers = {k: v for k, v in request.headers if k.lower() not in ('host', 'content-length')}
|
|
|
|
| 86 |
|
| 87 |
if token and 'Authorization' not in headers:
|
| 88 |
headers['Authorization'] = f'Bearer {token}'
|
| 89 |
|
| 90 |
+
url = f"https://open.feishu.cn{path}"
|
| 91 |
params = {k: v for k, v in request.args.items() if k != 'tenant_access_token'}
|
| 92 |
|
| 93 |
try:
|
|
|
|
| 99 |
data=request.get_data(),
|
| 100 |
timeout=30
|
| 101 |
)
|
| 102 |
+
# 透传所有响应 headers
|
| 103 |
+
excluded_headers = {'content-encoding', 'content-length', 'transfer-encoding', 'connection'}
|
| 104 |
+
response_headers = {k: v for k, v in resp.headers.items() if k.lower() not in excluded_headers}
|
| 105 |
return Response(resp.content, status=resp.status_code,
|
| 106 |
+
headers=response_headers,
|
| 107 |
content_type=resp.headers.get('Content-Type', 'application/json'))
|
| 108 |
except Exception as e:
|
| 109 |
+
log(f"❌ 代理失败 {path}: {e}")
|
| 110 |
return make_response(str(e), 502)
|
| 111 |
|
| 112 |
@app.route('/health')
|