Mythus commited on
Commit
f14963b
·
verified ·
1 Parent(s): 87b4a88

Update run.py

Browse files
Files changed (1) hide show
  1. run.py +15 -36
run.py CHANGED
@@ -1,48 +1,27 @@
1
- from fastapi import FastAPI, Request
2
- from fastapi.responses import Response
 
 
3
  from fastapi_proxy_lib.core.http import ForwardHttpProxy
4
  from fastapi_proxy_lib.core.tool import default_proxy_filter
5
  from httpx import AsyncClient
6
- import re
7
 
8
  proxy = ForwardHttpProxy(AsyncClient(), proxy_filter=default_proxy_filter)
9
 
10
- app = FastAPI()
 
 
 
 
11
 
12
- # Handle HTTP(S) requests through the proxy
13
- @app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS"])
14
- async def handle_proxy(request: Request, path: str = ""):
15
- """
16
- Handles HTTP requests and forwards them to the destination URL.
17
- """
18
- # Extract the destination URL from the request headers
19
- target_url = request.headers.get("host", "")
20
-
21
- if target_url:
22
- scheme = "https" if request.url.port == 443 else "http"
23
- # Reconstruct the full URL
24
- forward_url = f"{scheme}://{target_url}{request.url.path}"
25
-
26
- # Forward the request to the actual destination using ForwardHttpProxy
27
- return await proxy.proxy(request=request, path=forward_url)
28
- return Response(content="Host header not found", status_code=400)
29
 
 
 
 
30
 
31
- # Handle HTTPS requests with CONNECT method (to establish a tunnel)
32
- @app.api_route("/{path:path}", methods=["CONNECT"])
33
- async def handle_connect(request: Request):
34
- """
35
- Handles CONNECT method for HTTPS proxying.
36
- This is for forwarding HTTPS requests via curl -x.
37
- """
38
- # Extract the target host and port from the request's URL
39
- match = re.match(r"^([^:]+):(\d+)$", request.url.path)
40
- if match:
41
- host, port = match.groups()
42
- return await proxy.proxy(request=request, path=f"https://{host}:{port}")
43
-
44
- return Response(content="Invalid CONNECT request", status_code=400)
45
 
46
  if __name__ == '__main__':
47
  import uvicorn
48
- uvicorn.run("run:app", host="0.0.0.0", port=8080)
 
1
+ from contextlib import asynccontextmanager
2
+ from typing import AsyncIterator
3
+
4
+ from fastapi import FastAPI
5
  from fastapi_proxy_lib.core.http import ForwardHttpProxy
6
  from fastapi_proxy_lib.core.tool import default_proxy_filter
7
  from httpx import AsyncClient
8
+ from starlette.requests import Request
9
 
10
  proxy = ForwardHttpProxy(AsyncClient(), proxy_filter=default_proxy_filter)
11
 
12
+ @asynccontextmanager
13
+ async def close_proxy_event(_: FastAPI) -> AsyncIterator[None]:
14
+ """Close proxy."""
15
+ yield
16
+ await proxy.aclose()
17
 
18
+ app = FastAPI(lifespan=close_proxy_event)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
+ @app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS", "CONNECT"])
21
+ async def _(request: Request, path: str = ""):
22
+ return await proxy.proxy(request=request, path=path)
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  if __name__ == '__main__':
26
  import uvicorn
27
+ uvicorn.run("run:app", host="0.0.0.0", port="8080")