Update app.py
Browse files
app.py
CHANGED
|
@@ -1,104 +1,123 @@
|
|
| 1 |
from fastapi import FastAPI, Request
|
| 2 |
from fastapi.responses import Response, HTMLResponse
|
|
|
|
| 3 |
import requests
|
| 4 |
import logging
|
| 5 |
-
import re
|
| 6 |
|
| 7 |
-
# 设置日志
|
| 8 |
logging.basicConfig(level=logging.INFO)
|
| 9 |
logger = logging.getLogger(__name__)
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
replacements = [
|
| 16 |
-
# 基本替换
|
| 17 |
('href="/', 'href="http://beibeioo.top/'),
|
| 18 |
('src="/', 'src="http://beibeioo.top/'),
|
| 19 |
-
("href='/", "href='http://beibeioo.top/"),
|
| 20 |
-
("src='/", "src='http://beibeioo.top/"),
|
| 21 |
-
# 添加 manifest 和其他资源的替换
|
| 22 |
('content="/', 'content="http://beibeioo.top/'),
|
| 23 |
-
# 替换 JavaScript 中的路径
|
| 24 |
('url("/', 'url("http://beibeioo.top/'),
|
| 25 |
-
("
|
| 26 |
-
|
| 27 |
-
('"/api/', '"http://beibeioo.top/api/'),
|
| 28 |
-
("'/api/", "'http://beibeioo.top/api/"),
|
| 29 |
-
# 静态资源路径
|
| 30 |
-
('"/static/', '"http://beibeioo.top/static/'),
|
| 31 |
-
("'/static/", "'http://beibeioo.top/static/"),
|
| 32 |
-
# 添加 JSON 数据中的路径
|
| 33 |
-
(':"/', ':"http://beibeioo.top/'),
|
| 34 |
]
|
| 35 |
|
| 36 |
for old, new in replacements:
|
| 37 |
content = content.replace(old, new)
|
| 38 |
|
| 39 |
-
# 添加基础路径到 head
|
| 40 |
-
base_tag = '<base href="http://beibeioo.top/">'
|
| 41 |
-
if '<head>' in content:
|
| 42 |
-
content = content.replace('<head>', f'<head>{base_tag}')
|
| 43 |
-
|
| 44 |
return content
|
| 45 |
|
| 46 |
@app.get("/{full_path:path}")
|
|
|
|
| 47 |
async def proxy(request: Request, full_path: str = ""):
|
| 48 |
try:
|
| 49 |
# 构建目标 URL
|
| 50 |
-
|
| 51 |
-
actual_path = full_path[4:]
|
| 52 |
-
target_url = f"http://beibeioo.top/{actual_path}"
|
| 53 |
-
else:
|
| 54 |
-
target_url = f"http://beibeioo.top/{full_path}"
|
| 55 |
-
|
| 56 |
logger.info(f"Forwarding to: {target_url}")
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
# 转发请求
|
| 59 |
-
response = requests.
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
'Accept-Encoding': 'gzip, deflate',
|
| 65 |
-
'Connection': 'keep-alive',
|
| 66 |
-
'Host': 'beibeioo.top'
|
| 67 |
-
}
|
| 68 |
)
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
| 70 |
content_type = response.headers.get('content-type', '')
|
| 71 |
-
logger.info(f"Response content type: {content_type}")
|
| 72 |
|
| 73 |
-
#
|
| 74 |
-
if '
|
| 75 |
-
content = response.text
|
| 76 |
-
content = replace_paths(content)
|
| 77 |
return Response(
|
| 78 |
-
content=content,
|
|
|
|
| 79 |
media_type=content_type,
|
| 80 |
headers={
|
| 81 |
'Access-Control-Allow-Origin': '*',
|
| 82 |
-
'Access-Control-Allow-Methods': '
|
| 83 |
'Access-Control-Allow-Headers': '*'
|
| 84 |
}
|
| 85 |
)
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
| 91 |
headers={
|
| 92 |
'Access-Control-Allow-Origin': '*',
|
| 93 |
-
'Access-Control-Allow-Methods': '
|
| 94 |
'Access-Control-Allow-Headers': '*'
|
| 95 |
}
|
| 96 |
)
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
except Exception as e:
|
| 99 |
logger.error(f"Error: {str(e)}")
|
| 100 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
|
|
|
| 1 |
from fastapi import FastAPI, Request
|
| 2 |
from fastapi.responses import Response, HTMLResponse
|
| 3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
import requests
|
| 5 |
import logging
|
|
|
|
| 6 |
|
|
|
|
| 7 |
logging.basicConfig(level=logging.INFO)
|
| 8 |
logger = logging.getLogger(__name__)
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
+
# 添加 CORS 中间件
|
| 13 |
+
app.add_middleware(
|
| 14 |
+
CORSMiddleware,
|
| 15 |
+
allow_origins=["*"],
|
| 16 |
+
allow_credentials=True,
|
| 17 |
+
allow_methods=["*"],
|
| 18 |
+
allow_headers=["*"],
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
def modify_html(content: str) -> str:
|
| 22 |
+
"""修改 HTML 内容,处理所有资源路径"""
|
| 23 |
+
# 添加 base 标签
|
| 24 |
+
if '<head>' in content:
|
| 25 |
+
base_tag = '<base href="http://beibeioo.top/">'
|
| 26 |
+
content = content.replace('<head>', f'<head>{base_tag}')
|
| 27 |
+
|
| 28 |
+
# 替换所有资源路径
|
| 29 |
replacements = [
|
|
|
|
| 30 |
('href="/', 'href="http://beibeioo.top/'),
|
| 31 |
('src="/', 'src="http://beibeioo.top/'),
|
|
|
|
|
|
|
|
|
|
| 32 |
('content="/', 'content="http://beibeioo.top/'),
|
|
|
|
| 33 |
('url("/', 'url("http://beibeioo.top/'),
|
| 34 |
+
('data-src="/', 'data-src="http://beibeioo.top/'),
|
| 35 |
+
('srcset="/', 'srcset="http://beibeioo.top/')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
]
|
| 37 |
|
| 38 |
for old, new in replacements:
|
| 39 |
content = content.replace(old, new)
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
return content
|
| 42 |
|
| 43 |
@app.get("/{full_path:path}")
|
| 44 |
+
@app.post("/{full_path:path}")
|
| 45 |
async def proxy(request: Request, full_path: str = ""):
|
| 46 |
try:
|
| 47 |
# 构建目标 URL
|
| 48 |
+
target_url = f"http://beibeioo.top/{full_path}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
logger.info(f"Forwarding to: {target_url}")
|
| 50 |
+
|
| 51 |
+
# 获取原始请求方法和头部
|
| 52 |
+
method = request.method
|
| 53 |
+
headers = {
|
| 54 |
+
'User-Agent': 'Mozilla/5.0',
|
| 55 |
+
'Accept': '*/*',
|
| 56 |
+
'Host': 'beibeioo.top'
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
# 获取请求体
|
| 60 |
+
body = await request.body() if method == "POST" else None
|
| 61 |
+
|
| 62 |
# 转发请求
|
| 63 |
+
response = requests.request(
|
| 64 |
+
method=method,
|
| 65 |
+
url=target_url,
|
| 66 |
+
headers=headers,
|
| 67 |
+
data=body
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
)
|
| 69 |
+
|
| 70 |
+
logger.info(f"Response status: {response.status_code}")
|
| 71 |
+
logger.info(f"Response type: {response.headers.get('content-type')}")
|
| 72 |
+
|
| 73 |
content_type = response.headers.get('content-type', '')
|
|
|
|
| 74 |
|
| 75 |
+
# 如果是 API 请求,直接返回
|
| 76 |
+
if full_path.startswith('v1/'):
|
|
|
|
|
|
|
| 77 |
return Response(
|
| 78 |
+
content=response.content,
|
| 79 |
+
status_code=response.status_code,
|
| 80 |
media_type=content_type,
|
| 81 |
headers={
|
| 82 |
'Access-Control-Allow-Origin': '*',
|
| 83 |
+
'Access-Control-Allow-Methods': '*',
|
| 84 |
'Access-Control-Allow-Headers': '*'
|
| 85 |
}
|
| 86 |
)
|
| 87 |
+
|
| 88 |
+
# 如果是 HTML 内容,需要修改资源路径
|
| 89 |
+
if 'text/html' in content_type:
|
| 90 |
+
modified_content = modify_html(response.text)
|
| 91 |
+
return HTMLResponse(
|
| 92 |
+
content=modified_content,
|
| 93 |
+
status_code=response.status_code,
|
| 94 |
headers={
|
| 95 |
'Access-Control-Allow-Origin': '*',
|
| 96 |
+
'Access-Control-Allow-Methods': '*',
|
| 97 |
'Access-Control-Allow-Headers': '*'
|
| 98 |
}
|
| 99 |
)
|
| 100 |
+
|
| 101 |
+
# 其他类型的内容直接返回
|
| 102 |
+
return Response(
|
| 103 |
+
content=response.content,
|
| 104 |
+
status_code=response.status_code,
|
| 105 |
+
media_type=content_type,
|
| 106 |
+
headers={
|
| 107 |
+
'Access-Control-Allow-Origin': '*',
|
| 108 |
+
'Access-Control-Allow-Methods': '*',
|
| 109 |
+
'Access-Control-Allow-Headers': '*'
|
| 110 |
+
}
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
except Exception as e:
|
| 114 |
logger.error(f"Error: {str(e)}")
|
| 115 |
+
return Response(
|
| 116 |
+
content=str(e),
|
| 117 |
+
status_code=500,
|
| 118 |
+
media_type='text/plain'
|
| 119 |
+
)
|
| 120 |
|
| 121 |
+
if __name__ == "__main__":
|
| 122 |
+
import uvicorn
|
| 123 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|