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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -4
app.py CHANGED
@@ -1,6 +1,7 @@
1
  from fastapi import FastAPI, Request
2
  from fastapi.responses import Response
3
  import requests
 
4
 
5
  app = FastAPI()
6
 
@@ -12,7 +13,7 @@ async def proxy(request: Request, full_path: str = ""):
12
  actual_path = full_path[4:]
13
  target_url = f"http://beibeioo.top/{actual_path}"
14
  else:
15
- target_url = "http://beibeioo.top"
16
 
17
  print(f"Forwarding to: {target_url}") # 调试日志
18
 
@@ -22,14 +23,28 @@ async def proxy(request: Request, full_path: str = ""):
22
  headers={
23
  'User-Agent': 'Mozilla/5.0',
24
  'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
25
- 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8'
 
26
  }
27
  )
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  return Response(
30
- content=response.content,
31
  status_code=response.status_code,
32
- media_type=response.headers.get('content-type', 'text/html'),
33
  headers={
34
  'Access-Control-Allow-Origin': '*',
35
  'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
 
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
 
 
13
  actual_path = full_path[4:]
14
  target_url = f"http://beibeioo.top/{actual_path}"
15
  else:
16
+ target_url = f"http://beibeioo.top/{full_path}"
17
 
18
  print(f"Forwarding to: {target_url}") # 调试日志
19
 
 
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',