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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -38
app.py CHANGED
@@ -29,66 +29,62 @@ async def proxy(request: Request, full_path: str = ""):
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__":
94
  import uvicorn
 
29
  target_url,
30
  headers={
31
  'User-Agent': 'Mozilla/5.0',
32
+ 'Accept': '*/*',
 
33
  'Host': 'beibeioo.top'
34
  }
35
  )
36
 
37
  content_type = response.headers.get('content-type', '')
38
+ logger.info(f"Request path: {full_path}")
39
  logger.info(f"Response type: {content_type}")
 
40
 
41
+ # 处理不同类型的请求
42
  if full_path.startswith('v1/'):
43
+ # API 请求
44
+ return Response(content=response.content, media_type=content_type)
45
+ elif full_path.startswith('assets/') or full_path.endswith(('.js', '.css', '.png', '.jpg', '.jpeg', '.gif')):
46
+ # 静态资源请求
47
+ logger.info(f"Serving static file: {full_path}")
48
  return Response(
49
  content=response.content,
50
+ media_type=content_type,
51
+ headers={'Access-Control-Allow-Origin': '*'}
52
  )
53
+ elif 'text/html' in content_type:
54
+ # HTML 内容
 
55
  content = response.text
56
 
57
+ # 替换资源路径
58
+ replacements = [
59
+ ('href="/', 'href="https://bobocup-api.hf.space/'),
60
+ ('src="/', 'src="https://bobocup-api.hf.space/'),
61
+ ('content="/', 'content="https://bobocup-api.hf.space/'),
62
+ ('url("/', 'url("https://bobocup-api.hf.space/'),
63
+ ('"/assets/', '"https://bobocup-api.hf.space/assets/'),
64
+ ('"/api/', '"https://bobocup-api.hf.space/api/'),
65
+ ('"/v1/', '"https://bobocup-api.hf.space/v1/')
66
+ ]
67
 
68
+ for old, new in replacements:
69
+ content = content.replace(old, new)
 
70
 
71
+ logger.info("HTML content processed")
 
 
 
72
 
 
 
 
73
  return HTMLResponse(
74
  content=content,
75
+ headers={'Access-Control-Allow-Origin': '*'}
76
+ )
77
+ else:
78
+ # 其他类型的内容
79
+ return Response(
80
+ content=response.content,
81
+ media_type=content_type,
82
+ headers={'Access-Control-Allow-Origin': '*'}
83
  )
 
 
 
 
 
 
 
 
 
84
 
85
  except Exception as e:
86
  logger.error(f"Error occurred: {str(e)}")
87
+ return Response(content=str(e), status_code=500)
 
 
 
88
 
89
  if __name__ == "__main__":
90
  import uvicorn