bobocup commited on
Commit
061dfa5
·
verified ·
1 Parent(s): 23df053

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -69
app.py CHANGED
@@ -9,7 +9,6 @@ logger = logging.getLogger(__name__)
9
 
10
  app = FastAPI()
11
 
12
- # 添加 CORS 中间件
13
  app.add_middleware(
14
  CORSMiddleware,
15
  allow_origins=["*"],
@@ -18,104 +17,77 @@ app.add_middleware(
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__":
 
9
 
10
  app = FastAPI()
11
 
 
12
  app.add_middleware(
13
  CORSMiddleware,
14
  allow_origins=["*"],
 
17
  allow_headers=["*"],
18
  )
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  @app.get("/{full_path:path}")
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
+ response = requests.get(
29
+ target_url,
30
+ headers={
31
+ 'User-Agent': 'Mozilla/5.0',
32
+ 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
33
+ 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
34
+ 'Host': 'beibeioo.top'
35
+ }
 
 
 
 
 
 
 
 
36
  )
37
 
38
+ content_type = response.headers.get('content-type', '')
39
  logger.info(f"Response status: {response.status_code}")
40
+ logger.info(f"Response type: {content_type}")
41
+ logger.info(f"Response content preview: {response.text[:500]}") # 打印响应内容的前500个字符
42
 
43
+ # 如果是 API 请求
 
 
44
  if full_path.startswith('v1/'):
45
  return Response(
46
  content=response.content,
47
+ media_type=content_type
 
 
 
 
 
 
48
  )
49
+
50
+ # 如果是 HTML 内容
51
  if 'text/html' in content_type:
52
+ content = response.text
53
+
54
+ # 添加调试信息
55
+ logger.info("Processing HTML content...")
56
+
57
+ # 替换所有资源路径为绝对路径
58
+ content = content.replace('href="/', 'href="http://beibeioo.top/')
59
+ content = content.replace('src="/', 'src="http://beibeioo.top/')
60
+
61
+ # 添加 base 标签
62
+ if '<head>' in content:
63
+ base_tag = '<base href="http://beibeioo.top/">'
64
+ content = content.replace('<head>', f'<head>{base_tag}')
65
+
66
+ # 打印修改后的内容预览
67
+ logger.info(f"Modified content preview: {content[:500]}")
68
+
69
  return HTMLResponse(
70
+ content=content,
 
71
  headers={
72
+ 'Content-Type': 'text/html; charset=utf-8',
73
+ 'Access-Control-Allow-Origin': '*'
 
74
  }
75
  )
76
+
77
+ # 其他类型的内容
78
  return Response(
79
  content=response.content,
 
80
  media_type=content_type,
81
  headers={
82
+ 'Access-Control-Allow-Origin': '*'
 
 
83
  }
84
  )
85
 
86
  except Exception as e:
87
+ logger.error(f"Error occurred: {str(e)}")
88
  return Response(
89
  content=str(e),
90
+ status_code=500
 
91
  )
92
 
93
  if __name__ == "__main__":