Update main.py
Browse files
main.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
-
from fastapi import FastAPI
|
2 |
-
from
|
3 |
-
import
|
4 |
|
5 |
app = FastAPI()
|
6 |
|
@@ -13,23 +13,22 @@ app.add_middleware(
|
|
13 |
allow_headers=["*"],
|
14 |
)
|
15 |
|
16 |
-
@app.
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
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=
|
|
|
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)
|