Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,6 @@ from fastapi import FastAPI, Request, Response
|
|
| 2 |
from fastapi.responses import StreamingResponse
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
import httpx
|
| 5 |
-
import asyncio
|
| 6 |
from typing import Optional
|
| 7 |
|
| 8 |
app = FastAPI()
|
|
@@ -22,7 +21,7 @@ TARGET_URL = "http://beibeioo.top"
|
|
| 22 |
# 创建一个全局的 httpx 客户端
|
| 23 |
async def get_http_client():
|
| 24 |
return httpx.AsyncClient(
|
| 25 |
-
timeout=30.0,
|
| 26 |
follow_redirects=True,
|
| 27 |
http2=True
|
| 28 |
)
|
|
@@ -30,25 +29,17 @@ async def get_http_client():
|
|
| 30 |
@app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "PATCH"])
|
| 31 |
async def proxy(path: str, request: Request):
|
| 32 |
try:
|
| 33 |
-
# 构建目标URL
|
| 34 |
url = f"{TARGET_URL}/{path}"
|
| 35 |
|
| 36 |
# 获取并处理请求头
|
| 37 |
headers = dict(request.headers)
|
| 38 |
-
# 删除可能导致问题的头部
|
| 39 |
headers.pop('host', None)
|
| 40 |
headers.pop('connection', None)
|
| 41 |
headers.pop('content-length', None)
|
| 42 |
headers.pop('transfer-encoding', None)
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
if 'referer' in headers:
|
| 46 |
-
headers['referer'] = headers['referer'].replace(str(request.base_url), TARGET_URL)
|
| 47 |
-
|
| 48 |
-
# 获取查询参数
|
| 49 |
params = dict(request.query_params)
|
| 50 |
-
|
| 51 |
-
# 获取请求体
|
| 52 |
body = await request.body()
|
| 53 |
|
| 54 |
async with await get_http_client() as client:
|
|
@@ -62,18 +53,18 @@ async def proxy(path: str, request: Request):
|
|
| 62 |
|
| 63 |
# 处理响应头
|
| 64 |
response_headers = dict(response.headers)
|
| 65 |
-
# 删除可能导致问题的响应头
|
| 66 |
response_headers.pop('transfer-encoding', None)
|
| 67 |
response_headers.pop('content-encoding', None)
|
|
|
|
| 68 |
|
| 69 |
-
#
|
| 70 |
for key, value in response_headers.items():
|
| 71 |
if isinstance(value, str) and TARGET_URL in value:
|
| 72 |
response_headers[key] = value.replace(TARGET_URL, str(request.base_url).rstrip('/'))
|
| 73 |
|
| 74 |
-
#
|
| 75 |
-
return
|
| 76 |
-
response.
|
| 77 |
status_code=response.status_code,
|
| 78 |
headers=response_headers,
|
| 79 |
media_type=response.headers.get('content-type')
|
|
|
|
| 2 |
from fastapi.responses import StreamingResponse
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
import httpx
|
|
|
|
| 5 |
from typing import Optional
|
| 6 |
|
| 7 |
app = FastAPI()
|
|
|
|
| 21 |
# 创建一个全局的 httpx 客户端
|
| 22 |
async def get_http_client():
|
| 23 |
return httpx.AsyncClient(
|
| 24 |
+
timeout=30.0,
|
| 25 |
follow_redirects=True,
|
| 26 |
http2=True
|
| 27 |
)
|
|
|
|
| 29 |
@app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "PATCH"])
|
| 30 |
async def proxy(path: str, request: Request):
|
| 31 |
try:
|
|
|
|
| 32 |
url = f"{TARGET_URL}/{path}"
|
| 33 |
|
| 34 |
# 获取并处理请求头
|
| 35 |
headers = dict(request.headers)
|
|
|
|
| 36 |
headers.pop('host', None)
|
| 37 |
headers.pop('connection', None)
|
| 38 |
headers.pop('content-length', None)
|
| 39 |
headers.pop('transfer-encoding', None)
|
| 40 |
|
| 41 |
+
# 获取查询参数和请求体
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
params = dict(request.query_params)
|
|
|
|
|
|
|
| 43 |
body = await request.body()
|
| 44 |
|
| 45 |
async with await get_http_client() as client:
|
|
|
|
| 53 |
|
| 54 |
# 处理响应头
|
| 55 |
response_headers = dict(response.headers)
|
|
|
|
| 56 |
response_headers.pop('transfer-encoding', None)
|
| 57 |
response_headers.pop('content-encoding', None)
|
| 58 |
+
response_headers.pop('content-length', None) # 移除 content-length 头
|
| 59 |
|
| 60 |
+
# 修改涉及原始域名的响应头
|
| 61 |
for key, value in response_headers.items():
|
| 62 |
if isinstance(value, str) and TARGET_URL in value:
|
| 63 |
response_headers[key] = value.replace(TARGET_URL, str(request.base_url).rstrip('/'))
|
| 64 |
|
| 65 |
+
# 使用 Response 而不是 StreamingResponse
|
| 66 |
+
return Response(
|
| 67 |
+
content=response.content,
|
| 68 |
status_code=response.status_code,
|
| 69 |
headers=response_headers,
|
| 70 |
media_type=response.headers.get('content-type')
|