File size: 1,251 Bytes
8821488 1ebd440 7815665 5fa8a0c 7815665 5fa8a0c ac6ba74 5fa8a0c 8821488 7815665 8821488 7815665 ac6ba74 8821488 7815665 8821488 7815665 5fa8a0c 8821488 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
from fastapi import FastAPI
from starlette.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from openvpn_api import VPN
app = FastAPI()
vpn = None
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.post("/start")
def start_vpn():
global vpn
try:
vpn = VPN('localhost', 7505) # Ensure your OpenVPN server's management interface is set up on this IP and port.
vpn.connect()
# You can add more controls or checks here
return JSONResponse(content={"status": "connected"}, status_code=200)
except Exception as e:
return JSONResponse(content={"error": f"Failed to connect to VPN: {str(e)}"}, status_code=500)
@app.post("/stop")
def stop_vpn():
global vpn
try:
if vpn:
vpn.disconnect()
vpn = None
return JSONResponse(content={"status": "disconnected"}, status_code=200)
except Exception as e:
return JSONResponse(content={"error": f"Failed to disconnect from VPN: {str(e)}"}, status_code=500)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=5000)
|