Update app.py
Browse files
app.py
CHANGED
|
@@ -21,61 +21,64 @@ app.add_middleware(
|
|
| 21 |
@app.post("/{full_path:path}")
|
| 22 |
async def proxy(request: Request, full_path: str = ""):
|
| 23 |
try:
|
|
|
|
| 24 |
target_url = f"http://beibeioo.top/{full_path}"
|
|
|
|
| 25 |
logger.info(f"Forwarding to: {target_url}")
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
)
|
| 36 |
|
|
|
|
|
|
|
| 37 |
content_type = response.headers.get('content-type', '')
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
# 处理不同类型的请求
|
| 42 |
-
if full_path.startswith('v1/'):
|
| 43 |
-
# API 请求
|
| 44 |
-
return Response(content=response.content, media_type=content_type)
|
| 45 |
-
elif full_path.startswith('assets/') or full_path.endswith(('.js', '.css', '.png', '.jpg', '.jpeg', '.gif')):
|
| 46 |
-
# 静态资源请求
|
| 47 |
logger.info(f"Serving static file: {full_path}")
|
| 48 |
return Response(
|
| 49 |
content=response.content,
|
| 50 |
-
media_type=
|
| 51 |
headers={'Access-Control-Allow-Origin': '*'}
|
| 52 |
)
|
|
|
|
|
|
|
| 53 |
elif 'text/html' in content_type:
|
| 54 |
-
# HTML 内容
|
| 55 |
content = response.text
|
| 56 |
|
| 57 |
-
#
|
| 58 |
replacements = [
|
| 59 |
-
('
|
| 60 |
-
('
|
| 61 |
-
('
|
| 62 |
-
('
|
| 63 |
-
('"/
|
| 64 |
-
('"/api/', '"https://bobocup-api.hf.space/api/'),
|
| 65 |
-
('"/v1/', '"https://bobocup-api.hf.space/v1/')
|
| 66 |
]
|
| 67 |
|
| 68 |
for old, new in replacements:
|
| 69 |
content = content.replace(old, new)
|
| 70 |
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
return HTMLResponse(
|
| 74 |
content=content,
|
| 75 |
-
headers={
|
|
|
|
|
|
|
|
|
|
| 76 |
)
|
|
|
|
|
|
|
| 77 |
else:
|
| 78 |
-
# 其他类型的内容
|
| 79 |
return Response(
|
| 80 |
content=response.content,
|
| 81 |
media_type=content_type,
|
|
|
|
| 21 |
@app.post("/{full_path:path}")
|
| 22 |
async def proxy(request: Request, full_path: str = ""):
|
| 23 |
try:
|
| 24 |
+
# 构建目标 URL
|
| 25 |
target_url = f"http://beibeioo.top/{full_path}"
|
| 26 |
+
logger.info(f"Incoming request: {full_path}")
|
| 27 |
logger.info(f"Forwarding to: {target_url}")
|
| 28 |
|
| 29 |
+
# 设置请求头
|
| 30 |
+
headers = {
|
| 31 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
|
| 32 |
+
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
|
| 33 |
+
'Accept-Language': 'en-US,en;q=0.5',
|
| 34 |
+
'Connection': 'keep-alive',
|
| 35 |
+
'Host': 'beibeioo.top'
|
| 36 |
+
}
|
|
|
|
| 37 |
|
| 38 |
+
# 发送请求
|
| 39 |
+
response = requests.get(target_url, headers=headers)
|
| 40 |
content_type = response.headers.get('content-type', '')
|
| 41 |
+
|
| 42 |
+
# 如果是静态资源请求
|
| 43 |
+
if full_path.endswith(('.js', '.css', '.png', '.jpg', '.jpeg', '.gif', '.ico')):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
logger.info(f"Serving static file: {full_path}")
|
| 45 |
return Response(
|
| 46 |
content=response.content,
|
| 47 |
+
media_type=response.headers.get('content-type', 'application/octet-stream'),
|
| 48 |
headers={'Access-Control-Allow-Origin': '*'}
|
| 49 |
)
|
| 50 |
+
|
| 51 |
+
# 如果是 HTML 内容
|
| 52 |
elif 'text/html' in content_type:
|
|
|
|
| 53 |
content = response.text
|
| 54 |
|
| 55 |
+
# 替换所有资源路径,包括 assets 目录
|
| 56 |
replacements = [
|
| 57 |
+
('"/assets/', '"assets/'), # 改为相对路径
|
| 58 |
+
('"/static/', '"static/'), # 改为相对路径
|
| 59 |
+
('"/logo.png', '"logo.png'), # 改为相对路径
|
| 60 |
+
('href="/', 'href="'), # 改为相对路径
|
| 61 |
+
('src="/', 'src="'), # 改为相对路径
|
|
|
|
|
|
|
| 62 |
]
|
| 63 |
|
| 64 |
for old, new in replacements:
|
| 65 |
content = content.replace(old, new)
|
| 66 |
|
| 67 |
+
# 添加基础路径
|
| 68 |
+
base_tag = f'<base href="{request.base_url}">'
|
| 69 |
+
if '<head>' in content:
|
| 70 |
+
content = content.replace('<head>', f'<head>{base_tag}')
|
| 71 |
|
| 72 |
return HTMLResponse(
|
| 73 |
content=content,
|
| 74 |
+
headers={
|
| 75 |
+
'Access-Control-Allow-Origin': '*',
|
| 76 |
+
'Content-Security-Policy': "default-src * 'unsafe-inline' 'unsafe-eval'; img-src * data: blob:;"
|
| 77 |
+
}
|
| 78 |
)
|
| 79 |
+
|
| 80 |
+
# 其他类型的内容
|
| 81 |
else:
|
|
|
|
| 82 |
return Response(
|
| 83 |
content=response.content,
|
| 84 |
media_type=content_type,
|