2025-09-27 16:06:32 +00:00
|
|
|
"""
|
2025-10-19 16:31:07 +00:00
|
|
|
FastAPI server for the VPN Access Server.
|
2025-09-27 16:06:32 +00:00
|
|
|
"""
|
|
|
|
|
|
2025-10-19 16:31:07 +00:00
|
|
|
import uvicorn
|
2025-09-27 16:06:32 +00:00
|
|
|
import os
|
2025-10-19 16:31:07 +00:00
|
|
|
from fastapi import FastAPI, HTTPException
|
|
|
|
|
from fastapi.responses import FileResponse
|
|
|
|
|
from pydantic import BaseModel
|
2025-09-27 16:06:32 +00:00
|
|
|
|
2025-10-18 14:06:35 +00:00
|
|
|
from util.client.generate_client import generate_client_config
|
|
|
|
|
|
2025-10-19 16:31:07 +00:00
|
|
|
app = FastAPI(
|
|
|
|
|
title="VPN Access Server API",
|
|
|
|
|
description="API for managing VPN clients and server operations.",
|
|
|
|
|
version="1.0.0",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
class ClientRequest(BaseModel):
|
|
|
|
|
username: str
|
|
|
|
|
email: str
|
|
|
|
|
|
|
|
|
|
@app.post("/api/client/generate", summary="Generate a new VPN client configuration")
|
|
|
|
|
def generate_client(request: ClientRequest):
|
|
|
|
|
"""
|
|
|
|
|
Generates a new OpenVPN client configuration (.ovpn) file.
|
|
|
|
|
|
|
|
|
|
This endpoint will:
|
|
|
|
|
1. Trigger `easyrsa` to generate a new client certificate and key.
|
|
|
|
|
2. Assemble the `.ovpn` file with the new certificate/key and the server's CA and TA keys.
|
|
|
|
|
3. Save the file to the server's client configuration directory.
|
|
|
|
|
"""
|
2025-10-19 16:33:35 +00:00
|
|
|
success, message = generate_client_config(request.username, request.email)
|
2025-10-19 16:31:07 +00:00
|
|
|
if not success:
|
|
|
|
|
raise HTTPException(status_code=500, detail=message)
|
|
|
|
|
return {"message": message}
|
|
|
|
|
|
|
|
|
|
@app.get("/api/client/get-config/{username}", summary="Download a client configuration file")
|
|
|
|
|
def get_client_config(username: str, email: str):
|
|
|
|
|
"""
|
|
|
|
|
Downloads the .ovpn configuration file for a specific client.
|
|
|
|
|
The file is sought in the `generated-clients` directory.
|
|
|
|
|
"""
|
|
|
|
|
file_path = os.path.join("generated-clients", f"{username}.ovpn")
|
|
|
|
|
if not os.path.isfile(file_path):
|
|
|
|
|
raise HTTPException(status_code=404, detail="Configuration file not found for this user. Please generate it first.")
|
|
|
|
|
|
|
|
|
|
return FileResponse(path=file_path, filename=f"{username}.ovpn", media_type='application/octet-stream')
|
2025-09-27 16:06:32 +00:00
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2025-10-19 16:31:07 +00:00
|
|
|
uvicorn.run(app, host="0.0.0.0", port=8443)
|