File size: 1,343 Bytes
4c34c0c
 
a32f715
3bbdb9c
4c34c0c
3bbdb9c
e66f400
822dfb3
e5564de
4c34c0c
 
822dfb3
e4e2ab1
1c9d1a9
e5564de
a9fe695
 
 
4c34c0c
 
3cd7e0d
1c9d1a9
4c34c0c
 
 
 
 
 
 
 
 
3cd7e0d
 
4c34c0c
 
 
0f7ed8a
871b55b
 
a9fe695
871b55b
 
a9fe695
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
34
35
36
37
38
39
40
41
42
43
from fastapi import FastAPI, Request, Response, status
import httpx
import uvicorn
import time
app = FastAPI()
time.sleep(10)
TARGET_BASE = "http://127.0.0.1:9222"  # 替换为你的目标服务器
resp = httpx.get(TARGET_BASE)
print(f'RESP:{resp.text}')
@app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"])
async def proxy(request: Request, path: str):
    resp = httpx.get(TARGET_BASE)
        # 强制Host头为127.0.0.1

    print(f'RESP:{resp.text}')
    # if path == "":
    #     # 拒绝根目录
    #     return Response(content="Not Found", status_code=status.HTTP_404_NOT_FOUND)
    url = f"{TARGET_BASE}/{path}"
    headers = dict(request.headers)
    host = headers["host"]
    headers["host"] = "127.0.0.1"
    body = await request.body()
    async with httpx.AsyncClient(follow_redirects=True) as client:
        resp = await client.request(
            request.method,
            url,
            headers=headers,
            content=body,
            params=request.query_params
        )
    headers = dict(resp.headers)
    headers["host"] = host
    return Response(
        content=resp.content,
        status_code=resp.status_code,
        headers=headers
    )


if __name__ == "__main__":
    # 运行FastAPI应用
    uvicorn.run("fun:app", host="0.0.0.0", port=8000)