File size: 1,004 Bytes
8821488
59a3d9e
8821488
 
5fa8a0c
 
 
ac6ba74
5fa8a0c
 
 
 
 
 
 
 
8821488
 
 
 
 
 
 
ac6ba74
8821488
 
 
 
 
 
 
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
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from starlette.responses import JSONResponse
import subprocess

app = FastAPI()

# Add CORS middleware
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.post("/start")
def start_vpn():
    try:
        subprocess.run(["openvpn", "--config", "/path/to/config.conf"], check=True)
        return JSONResponse(content={"status": "started"}, status_code=200)
    except:
        return JSONResponse(content={"error": "Failed to start VPN"}, status_code=500)

@app.post("/stop")
def stop_vpn():
    try:
        subprocess.run(["pkill", "openvpn"], check=True)
        return JSONResponse(content={"status": "stopped"}, status_code=200)
    except:
        return JSONResponse(content={"error": "Failed to stop VPN"}, status_code=500)

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=5000)