ka1kuk commited on
Commit
8821488
1 Parent(s): 0fb3845

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +18 -19
main.py CHANGED
@@ -1,6 +1,6 @@
1
- from fastapi import FastAPI, HTTPException, Path
2
- from fastapi.middleware.cors import CORSMiddleware
3
- import httpx
4
 
5
  app = FastAPI()
6
 
@@ -13,23 +13,22 @@ app.add_middleware(
13
  allow_headers=["*"],
14
  )
15
 
16
- @app.get("/{url:path}")
17
- async def proxy(url: str = Path(..., title="The URL to be proxied", description="The entire URL to be proxied")):
18
- headers = {
19
- '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'
20
- }
 
 
21
 
22
- # Ensure the URL has a valid format
23
- if not url.startswith(('http://', 'https://')):
24
- raise HTTPException(status_code=400, detail="Invalid URL format")
25
-
26
- async with httpx.AsyncClient() as client:
27
- try:
28
- response = await client.get(url, headers=headers)
29
- return response.json() # Parse the content as JSON and return
30
- except (httpx.RequestError, ValueError) as e:
31
- raise HTTPException(status_code=500, detail="Unexpected error")
32
 
33
  if __name__ == "__main__":
34
  import uvicorn
35
- uvicorn.run(app, host="0.0.0.0", port=8000)
 
1
+ from fastapi import FastAPI
2
+ from starlette.responses import JSONResponse
3
+ import subprocess
4
 
5
  app = FastAPI()
6
 
 
13
  allow_headers=["*"],
14
  )
15
 
16
+ @app.post("/start")
17
+ def start_vpn():
18
+ try:
19
+ subprocess.run(["openvpn", "--config", "/path/to/config.conf"], check=True)
20
+ return JSONResponse(content={"status": "started"}, status_code=200)
21
+ except:
22
+ return JSONResponse(content={"error": "Failed to start VPN"}, status_code=500)
23
 
24
+ @app.post("/stop")
25
+ def stop_vpn():
26
+ try:
27
+ subprocess.run(["pkill", "openvpn"], check=True)
28
+ return JSONResponse(content={"status": "stopped"}, status_code=200)
29
+ except:
30
+ return JSONResponse(content={"error": "Failed to stop VPN"}, status_code=500)
 
 
 
31
 
32
  if __name__ == "__main__":
33
  import uvicorn
34
+ uvicorn.run(app, host="0.0.0.0", port=5000)