Update main.py
Browse files
main.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from starlette.responses import JSONResponse
|
| 4 |
-
import
|
| 5 |
|
| 6 |
app = FastAPI()
|
|
|
|
| 7 |
|
| 8 |
# Add CORS middleware
|
| 9 |
app.add_middleware(
|
|
@@ -16,19 +16,25 @@ app.add_middleware(
|
|
| 16 |
|
| 17 |
@app.post("/start")
|
| 18 |
def start_vpn():
|
|
|
|
| 19 |
try:
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
return JSONResponse(content={"
|
|
|
|
|
|
|
| 24 |
|
| 25 |
@app.post("/stop")
|
| 26 |
def stop_vpn():
|
|
|
|
| 27 |
try:
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
return JSONResponse(content={"
|
|
|
|
|
|
|
| 32 |
|
| 33 |
if __name__ == "__main__":
|
| 34 |
import uvicorn
|
|
|
|
| 1 |
from fastapi import FastAPI
|
|
|
|
| 2 |
from starlette.responses import JSONResponse
|
| 3 |
+
from openvpn_api import VPN
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
+
vpn = None
|
| 7 |
|
| 8 |
# Add CORS middleware
|
| 9 |
app.add_middleware(
|
|
|
|
| 16 |
|
| 17 |
@app.post("/start")
|
| 18 |
def start_vpn():
|
| 19 |
+
global vpn
|
| 20 |
try:
|
| 21 |
+
vpn = VPN('localhost', 7505) # Ensure your OpenVPN server's management interface is set up on this IP and port.
|
| 22 |
+
vpn.connect()
|
| 23 |
+
# You can add more controls or checks here
|
| 24 |
+
return JSONResponse(content={"status": "connected"}, status_code=200)
|
| 25 |
+
except Exception as e:
|
| 26 |
+
return JSONResponse(content={"error": f"Failed to connect to VPN: {str(e)}"}, status_code=500)
|
| 27 |
|
| 28 |
@app.post("/stop")
|
| 29 |
def stop_vpn():
|
| 30 |
+
global vpn
|
| 31 |
try:
|
| 32 |
+
if vpn:
|
| 33 |
+
vpn.disconnect()
|
| 34 |
+
vpn = None
|
| 35 |
+
return JSONResponse(content={"status": "disconnected"}, status_code=200)
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return JSONResponse(content={"error": f"Failed to disconnect from VPN: {str(e)}"}, status_code=500)
|
| 38 |
|
| 39 |
if __name__ == "__main__":
|
| 40 |
import uvicorn
|