2026-02-08 19:10:35 +03:00
|
|
|
import os
|
|
|
|
|
import logging
|
2026-01-28 22:37:47 +03:00
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
|
from database import get_db
|
|
|
|
|
from utils.auth import verify_token
|
|
|
|
|
from services import generator
|
|
|
|
|
|
2026-02-08 19:10:35 +03:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
2026-01-28 22:37:47 +03:00
|
|
|
router = APIRouter(dependencies=[Depends(verify_token)])
|
|
|
|
|
|
|
|
|
|
@router.post("/server/configure")
|
|
|
|
|
def configure_server(db: Session = Depends(get_db)):
|
|
|
|
|
try:
|
|
|
|
|
# Generate to a temporary location or standard location
|
|
|
|
|
# As per plan, we behave like srvconf
|
|
|
|
|
output_path = "/etc/openvpn/server.conf"
|
2026-02-07 14:51:15 +03:00
|
|
|
|
|
|
|
|
# Ensure we can write to /etc/openvpn
|
|
|
|
|
if not os.path.exists(os.path.dirname(output_path)) or not os.access(os.path.dirname(output_path), os.W_OK):
|
|
|
|
|
# For local dev or non-root host, use staging
|
|
|
|
|
output_path = "staging/server.conf"
|
|
|
|
|
os.makedirs("staging", exist_ok=True)
|
|
|
|
|
logger.info(f"[SERVER] /etc/openvpn not writable, using staging path: {output_path}")
|
|
|
|
|
else:
|
|
|
|
|
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
|
|
|
|
|
2026-01-28 22:37:47 +03:00
|
|
|
|
|
|
|
|
content = generator.generate_server_config(db, output_path=output_path)
|
|
|
|
|
return {"message": "Server configuration generated", "path": output_path}
|
|
|
|
|
except Exception as e:
|
|
|
|
|
raise HTTPException(status_code=500, detail=str(e))
|