File size: 1,007 Bytes
219d368
 
4f8d3c0
219d368
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f8d3c0
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from fastapi import FastAPI, Request
import httpx
import uvicorn

app = FastAPI()

BASE_URL = "https://bg5-mianban.hf.space/"

@app.api_route("/{full_path:path}", methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"])
async def proxy(full_path: str, request: Request):
    # 解析出目标 URL
    if full_path.startswith("https/"):
        target_url = f"https://{full_path[6:]}"  # 去掉 "https/"
    else:
        return {"error": "Invalid URL format. Please use 'https/' prefix."}

    # 使用 httpx 转发请求
    async with httpx.AsyncClient() as client:
        response = await client.request(
            request.method,
            target_url,
            headers=request.headers.raw,
            data=await request.body(),
            cookies=request.cookies,
            allow_redirects=False
        )
    
    # 返回响应
    return response.content, response.status_code, response.headers.items()

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=7860, reload=True)