Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,11 @@
|
|
| 1 |
from fastapi import FastAPI, Request
|
| 2 |
-
from fastapi.responses import Response
|
| 3 |
import requests
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
|
@@ -15,44 +19,67 @@ async def proxy(request: Request, full_path: str = ""):
|
|
| 15 |
else:
|
| 16 |
target_url = f"http://beibeioo.top/{full_path}"
|
| 17 |
|
| 18 |
-
|
| 19 |
|
| 20 |
# 转发请求
|
| 21 |
response = requests.get(
|
| 22 |
target_url,
|
| 23 |
headers={
|
| 24 |
'User-Agent': 'Mozilla/5.0',
|
| 25 |
-
'Accept': '
|
| 26 |
-
'Accept-
|
|
|
|
| 27 |
'Host': 'beibeioo.top'
|
| 28 |
}
|
| 29 |
)
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
|
|
|
| 33 |
|
| 34 |
-
#
|
| 35 |
if 'text/html' in content_type:
|
| 36 |
content = response.text
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
content = content.replace("
|
| 42 |
-
content =
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
except Exception as e:
|
| 55 |
-
|
| 56 |
return {"error": str(e)}
|
| 57 |
|
| 58 |
@app.get("/healthz")
|
|
|
|
| 1 |
from fastapi import FastAPI, Request
|
| 2 |
+
from fastapi.responses import Response, HTMLResponse
|
| 3 |
import requests
|
| 4 |
+
import logging
|
| 5 |
+
|
| 6 |
+
# 设置日志
|
| 7 |
+
logging.basicConfig(level=logging.INFO)
|
| 8 |
+
logger = logging.getLogger(__name__)
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
|
|
|
| 19 |
else:
|
| 20 |
target_url = f"http://beibeioo.top/{full_path}"
|
| 21 |
|
| 22 |
+
logger.info(f"Forwarding to: {target_url}")
|
| 23 |
|
| 24 |
# 转发请求
|
| 25 |
response = requests.get(
|
| 26 |
target_url,
|
| 27 |
headers={
|
| 28 |
'User-Agent': 'Mozilla/5.0',
|
| 29 |
+
'Accept': '*/*',
|
| 30 |
+
'Accept-Encoding': 'gzip, deflate',
|
| 31 |
+
'Connection': 'keep-alive',
|
| 32 |
'Host': 'beibeioo.top'
|
| 33 |
}
|
| 34 |
)
|
| 35 |
|
| 36 |
+
# 获取内容类型
|
| 37 |
+
content_type = response.headers.get('content-type', '')
|
| 38 |
+
logger.info(f"Response content type: {content_type}")
|
| 39 |
|
| 40 |
+
# 获取响应内容
|
| 41 |
if 'text/html' in content_type:
|
| 42 |
content = response.text
|
| 43 |
+
logger.info(f"Response length: {len(content)}")
|
| 44 |
+
logger.info(f"First 200 chars: {content[:200]}") # 打印前200个字符用于调试
|
| 45 |
+
|
| 46 |
+
# 替换所有相对路径为绝对路径
|
| 47 |
+
content = content.replace('href="/', f'href="http://beibeioo.top/')
|
| 48 |
+
content = content.replace('src="/', f'src="http://beibeioo.top/')
|
| 49 |
+
content = content.replace("href='/", f"href='http://beibeioo.top/")
|
| 50 |
+
content = content.replace("src='/", f"src='http://beibeioo.top/")
|
| 51 |
+
|
| 52 |
+
# 如果是API响应,可能需要特殊处理
|
| 53 |
+
if not content.strip():
|
| 54 |
+
return Response(
|
| 55 |
+
content=response.content,
|
| 56 |
+
media_type='application/json',
|
| 57 |
+
headers={'Access-Control-Allow-Origin': '*'}
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
return HTMLResponse(
|
| 61 |
+
content=content,
|
| 62 |
+
status_code=response.status_code,
|
| 63 |
+
headers={
|
| 64 |
+
'Access-Control-Allow-Origin': '*',
|
| 65 |
+
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
|
| 66 |
+
'Access-Control-Allow-Headers': '*'
|
| 67 |
+
}
|
| 68 |
+
)
|
| 69 |
+
else:
|
| 70 |
+
# 对于非HTML内容,直接返回
|
| 71 |
+
return Response(
|
| 72 |
+
content=response.content,
|
| 73 |
+
media_type=content_type,
|
| 74 |
+
headers={
|
| 75 |
+
'Access-Control-Allow-Origin': '*',
|
| 76 |
+
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
|
| 77 |
+
'Access-Control-Allow-Headers': '*'
|
| 78 |
+
}
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
except Exception as e:
|
| 82 |
+
logger.error(f"Error: {str(e)}")
|
| 83 |
return {"error": str(e)}
|
| 84 |
|
| 85 |
@app.get("/healthz")
|