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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -23
app.py CHANGED
@@ -2,6 +2,7 @@ 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)
@@ -9,6 +10,39 @@ logger = logging.getLogger(__name__)
9
 
10
  app = FastAPI()
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  @app.get("/{full_path:path}")
13
  async def proxy(request: Request, full_path: str = ""):
14
  try:
@@ -33,33 +67,16 @@ async def proxy(request: Request, full_path: str = ""):
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',
@@ -67,7 +84,7 @@ async def proxy(request: Request, full_path: str = ""):
67
  }
68
  )
69
  else:
70
- # 对于非HTML内容,直接返回
71
  return Response(
72
  content=response.content,
73
  media_type=content_type,
 
2
  from fastapi.responses import Response, HTMLResponse
3
  import requests
4
  import logging
5
+ import re
6
 
7
  # 设置日志
8
  logging.basicConfig(level=logging.INFO)
 
10
 
11
  app = FastAPI()
12
 
13
+ def replace_paths(content):
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
+ ("url('/", "url('http://beibeioo.top/"),
26
+ # API 路径替换
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:
 
67
  }
68
  )
69
 
 
70
  content_type = response.headers.get('content-type', '')
71
  logger.info(f"Response content type: {content_type}")
72
 
73
+ # 处理不同类型的响应
74
+ if 'text/html' in content_type or 'application/javascript' in content_type or 'text/css' in content_type:
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': 'GET, POST, OPTIONS',
 
84
  }
85
  )
86
  else:
87
+ # 对于其他类型的内容(图片等),直接返回
88
  return Response(
89
  content=response.content,
90
  media_type=content_type,