| | from fastapi import FastAPI, Request |
| | from fastapi.responses import Response, HTMLResponse |
| | from fastapi.middleware.cors import CORSMiddleware |
| | import requests |
| | import logging |
| |
|
| | logging.basicConfig(level=logging.INFO) |
| | logger = logging.getLogger(__name__) |
| |
|
| | app = FastAPI() |
| |
|
| | app.add_middleware( |
| | CORSMiddleware, |
| | allow_origins=["*"], |
| | allow_credentials=True, |
| | allow_methods=["*"], |
| | allow_headers=["*"], |
| | ) |
| |
|
| | @app.get("/{full_path:path}") |
| | @app.post("/{full_path:path}") |
| | async def proxy(request: Request, full_path: str = ""): |
| | try: |
| | |
| | target_url = f"http://beibeioo.top/{full_path}" |
| | logger.info(f"Incoming request: {full_path}") |
| | logger.info(f"Forwarding to: {target_url}") |
| |
|
| | |
| | headers = { |
| | 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36', |
| | 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', |
| | 'Accept-Language': 'en-US,en;q=0.5', |
| | 'Connection': 'keep-alive', |
| | 'Host': 'beibeioo.top' |
| | } |
| |
|
| | |
| | response = requests.get(target_url, headers=headers) |
| | content_type = response.headers.get('content-type', '') |
| | |
| | |
| | if full_path.endswith(('.js', '.css', '.png', '.jpg', '.jpeg', '.gif', '.ico')): |
| | logger.info(f"Serving static file: {full_path}") |
| | return Response( |
| | content=response.content, |
| | media_type=response.headers.get('content-type', 'application/octet-stream'), |
| | headers={'Access-Control-Allow-Origin': '*'} |
| | ) |
| | |
| | |
| | elif 'text/html' in content_type: |
| | content = response.text |
| | |
| | |
| | replacements = [ |
| | ('"/assets/', '"assets/'), |
| | ('"/static/', '"static/'), |
| | ('"/logo.png', '"logo.png'), |
| | ('href="/', 'href="'), |
| | ('src="/', 'src="'), |
| | ] |
| | |
| | for old, new in replacements: |
| | content = content.replace(old, new) |
| | |
| | |
| | base_tag = f'<base href="{request.base_url}">' |
| | if '<head>' in content: |
| | content = content.replace('<head>', f'<head>{base_tag}') |
| | |
| | return HTMLResponse( |
| | content=content, |
| | headers={ |
| | 'Access-Control-Allow-Origin': '*', |
| | 'Content-Security-Policy': "default-src * 'unsafe-inline' 'unsafe-eval'; img-src * data: blob:;" |
| | } |
| | ) |
| | |
| | |
| | else: |
| | return Response( |
| | content=response.content, |
| | media_type=content_type, |
| | headers={'Access-Control-Allow-Origin': '*'} |
| | ) |
| |
|
| | except Exception as e: |
| | logger.error(f"Error occurred: {str(e)}") |
| | return Response(content=str(e), status_code=500) |
| |
|
| | if __name__ == "__main__": |
| | import uvicorn |
| | uvicorn.run(app, host="0.0.0.0", port=7860) |