bobocup commited on
Commit
6d0d6b5
·
verified ·
1 Parent(s): 6d6e147

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -26
app.py CHANGED
@@ -1,7 +1,11 @@
1
  from fastapi import FastAPI, Request
2
- from fastapi.responses import Response
3
  import requests
4
- from urllib.parse import urljoin
 
 
 
 
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
- print(f"Forwarding to: {target_url}") # 调试日志
19
 
20
  # 转发请求
21
  response = requests.get(
22
  target_url,
23
  headers={
24
  'User-Agent': 'Mozilla/5.0',
25
- 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
26
- 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
 
27
  'Host': 'beibeioo.top'
28
  }
29
  )
30
 
31
- content_type = response.headers.get('content-type', 'text/html')
32
- content = response.content
 
33
 
34
- # 如果是 HTML 内容,替换所有资源路径
35
  if 'text/html' in content_type:
36
  content = response.text
37
- # 替换相对路径为绝对路径
38
- content = content.replace('href="/', 'href="https://beibeioo.top/')
39
- content = content.replace('src="/', 'src="https://beibeioo.top/')
40
- content = content.replace("href='/", "href='https://beibeioo.top/")
41
- content = content.replace("src='/", "src='https://beibeioo.top/")
42
- content = bytes(content, 'utf-8')
43
-
44
- return Response(
45
- content=content,
46
- status_code=response.status_code,
47
- media_type=content_type,
48
- headers={
49
- 'Access-Control-Allow-Origin': '*',
50
- 'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
51
- 'Access-Control-Allow-Headers': '*'
52
- }
53
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  except Exception as e:
55
- print(f"Error: {str(e)}") # 调试日志
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")