feat: add netplan configuration models and backend API for applying netplan
All checks were successful
Build and Deploy MITM Webserver / build (push) Successful in 8s

This commit is contained in:
2025-11-22 23:25:39 +01:00
parent 9d8542a0a7
commit 0615d83134
3 changed files with 161 additions and 1 deletions

View File

@@ -1,3 +1,6 @@
from asyncio import subprocess
from http.client import HTTPException
import src.Models.netplan as netplan
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import os
@@ -25,4 +28,33 @@ def read_root():
@app.get("/api/nft/ruleset")
def read_root():
message = os.popen("sudo nft --json list ruleset").read().strip()
return {"message": message}
return {"message": message}
NETPLAN_FILE = "/etc/netplan/99-app.yaml"
@app.post("/netplan/apply")
def apply_netplan(config: netplan.NetworkConfig):
try:
data = {"network": config.dict(exclude_none=True)}
with open(NETPLAN_FILE, "w") as f:
yaml.dump(data, f, sort_keys=False)
# Safety: rollback if config breaks connectivity
result = subprocess.run(
["netplan", "try", "--timeout", "10"],
capture_output=True,
text=True,
)
if result.returncode != 0:
raise HTTPException(
status_code=500,
detail=f"Netplan failed: {result.stderr}"
)
return {"status": "ok", "output": result.stdout}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))