BG5 commited on
Commit
d87880a
·
verified ·
1 Parent(s): e5564de

Update fun.py

Browse files
Files changed (1) hide show
  1. fun.py +31 -17
fun.py CHANGED
@@ -1,24 +1,20 @@
1
- from fastapi import FastAPI, Request, Response, status
2
  import httpx
3
  import uvicorn
4
- import time
 
 
5
  app = FastAPI()
6
- time.sleep(10)
7
- TARGET_BASE = "http://127.0.0.1:9222" # 替换为你的目标服务器
8
- resp = httpx.get(TARGET_BASE)
9
- print(f'RESP:{resp.text}')
10
  @app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"])
11
  async def proxy(request: Request, path: str):
12
- resp = httpx.get(TARGET_BASE)
13
- # 强制Host头为127.0.0.1
14
 
15
- print(f'RESP:{resp.text}')
16
- # if path == "":
17
- # # 拒绝根目录
18
- # return Response(content="Not Found", status_code=status.HTTP_404_NOT_FOUND)
19
  url = f"{TARGET_BASE}/{path}"
20
  headers = dict(request.headers)
21
- host = headers["host"]
22
  headers["host"] = "127.0.0.1"
23
  body = await request.body()
24
  async with httpx.AsyncClient(follow_redirects=True) as client:
@@ -29,15 +25,33 @@ async def proxy(request: Request, path: str):
29
  content=body,
30
  params=request.query_params
31
  )
32
- headers = dict(resp.headers)
33
- headers["host"] = host
34
  return Response(
35
  content=resp.content,
36
  status_code=resp.status_code,
37
- headers=headers
38
  )
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
  if __name__ == "__main__":
42
- # 运行FastAPI应用
43
  uvicorn.run("fun:app", host="0.0.0.0", port=8000)
 
1
+ from fastapi import FastAPI, Request, Response, status, WebSocket, WebSocketDisconnect
2
  import httpx
3
  import uvicorn
4
+ import asyncio
5
+ import websockets
6
+
7
  app = FastAPI()
8
+
9
+ TARGET_BASE = "http://127.0.0.1:9222" # 目标服务器(Chrome DevTools)
10
+ TARGET_WS_BASE = "ws://127.0.0.1:9222" # WebSocket 目标
11
+
12
  @app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"])
13
  async def proxy(request: Request, path: str):
 
 
14
 
 
 
 
 
15
  url = f"{TARGET_BASE}/{path}"
16
  headers = dict(request.headers)
17
+ # 强制 Host 头为 127.0.0.1,避免 Chrome 拒绝
18
  headers["host"] = "127.0.0.1"
19
  body = await request.body()
20
  async with httpx.AsyncClient(follow_redirects=True) as client:
 
25
  content=body,
26
  params=request.query_params
27
  )
 
 
28
  return Response(
29
  content=resp.content,
30
  status_code=resp.status_code,
31
+ headers=dict(resp.headers)
32
  )
33
 
34
+ @app.websocket("/{path:path}")
35
+ async def websocket_proxy(websocket: WebSocket, path: str):
36
+ await websocket.accept()
37
+ target_url = f"{TARGET_WS_BASE}/{path}"
38
+ try:
39
+ async with websockets.connect(target_url) as target_ws:
40
+ async def client_to_server():
41
+ while True:
42
+ data = await websocket.receive_text()
43
+ await target_ws.send(data)
44
+
45
+ async def server_to_client():
46
+ while True:
47
+ data = await target_ws.recv()
48
+ await websocket.send_text(data)
49
+
50
+ await asyncio.gather(client_to_server(), server_to_client())
51
+ except WebSocketDisconnect:
52
+ pass
53
+ except Exception:
54
+ await websocket.close()
55
 
56
  if __name__ == "__main__":
 
57
  uvicorn.run("fun:app", host="0.0.0.0", port=8000)