feat: add network API and netplan configuration models with example usage
All checks were successful
Build and Deploy MITM Webserver / build (push) Successful in 8s
All checks were successful
Build and Deploy MITM Webserver / build (push) Successful in 8s
This commit is contained in:
@@ -1,11 +1,20 @@
|
||||
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
|
||||
import src.Models.netplan as netplan
|
||||
import src.network_api as network_api
|
||||
from asyncio import subprocess
|
||||
from fastapi import HTTPException
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
app = FastAPI(
|
||||
title="MITM Webserver Backend",
|
||||
description="Backend API for MITM Webserver",
|
||||
version="1.0.0",
|
||||
docs_url="/api/docs",
|
||||
redoc_url="/api/redoc",
|
||||
openapi_url="/api/openapi.json",
|
||||
)
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
@@ -15,22 +24,47 @@ app.add_middleware(
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
|
||||
# ---------------------
|
||||
# Startup / Shutdown
|
||||
# ---------------------
|
||||
|
||||
|
||||
@app.on_event("shutdown")
|
||||
def shutdown_event():
|
||||
network_api.shutdown_network_api()
|
||||
|
||||
|
||||
# ---------------------
|
||||
# Basic Endpoints
|
||||
# ---------------------
|
||||
|
||||
@app.get("/api/hello")
|
||||
def read_root():
|
||||
def hello():
|
||||
return {"message": "Hello from FastAPI 🎉"}
|
||||
|
||||
|
||||
@app.get("/api/versions")
|
||||
def read_root():
|
||||
def versions():
|
||||
message = os.popen("python --version").read().strip()
|
||||
return {"message": message}
|
||||
|
||||
@app.get("/api/nft/ruleset")
|
||||
def read_root():
|
||||
def nft_ruleset():
|
||||
message = os.popen("sudo nft --json list ruleset").read().strip()
|
||||
return {"message": message}
|
||||
|
||||
|
||||
# ---------------------
|
||||
# Routers
|
||||
# ---------------------
|
||||
|
||||
app.include_router(network_api.router, prefix="/network", tags=["network"])
|
||||
|
||||
|
||||
# ---------------------
|
||||
# Netplan Apply
|
||||
# ---------------------
|
||||
|
||||
NETPLAN_FILE = "/etc/netplan/99-app.yaml"
|
||||
|
||||
@app.post("/netplan/apply")
|
||||
@@ -41,7 +75,6 @@ def apply_netplan(config: netplan.NetworkConfig):
|
||||
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,
|
||||
@@ -57,4 +90,4 @@ def apply_netplan(config: netplan.NetworkConfig):
|
||||
return {"status": "ok", "output": result.stdout}
|
||||
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
Reference in New Issue
Block a user