From b56a9db335bfb229b8800804ffc05c7095209cff Mon Sep 17 00:00:00 2001 From: Per-Arne Andersen Date: Mon, 20 Jul 2020 02:24:43 +0200 Subject: [PATCH] * Added configuration dump capabilities which address #21 * Removed unused code. --- wg_dashboard_backend/routers/v1/user.py | 6 ----- wg_dashboard_backend/routers/v1/wg.py | 29 ++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/wg_dashboard_backend/routers/v1/user.py b/wg_dashboard_backend/routers/v1/user.py index 0ad8a46..10840f7 100644 --- a/wg_dashboard_backend/routers/v1/user.py +++ b/wg_dashboard_backend/routers/v1/user.py @@ -78,9 +78,3 @@ def create_user( )): raise HTTPException(status_code=400, detail="Could not create user") - return login_for_access_token(OAuth2PasswordRequestForm( - username=form_data.username, - password=form_data.password, - scope="" - ), sess) - diff --git a/wg_dashboard_backend/routers/v1/wg.py b/wg_dashboard_backend/routers/v1/wg.py index a7005e2..cad3aa1 100644 --- a/wg_dashboard_backend/routers/v1/wg.py +++ b/wg_dashboard_backend/routers/v1/wg.py @@ -1,8 +1,14 @@ -from fastapi import APIRouter +from datetime import datetime +from fastapi import APIRouter, Depends +from sqlalchemy.orm import Session +from zipfile import ZipFile +from io import BytesIO import middleware +from starlette.responses import StreamingResponse import schemas import script.wireguard +import db.wireguard router = APIRouter() @@ -23,3 +29,24 @@ def generate_key_pair(): private_key=private_key, public_key=public_key ) + + +@router.get("/dump") +def dump_database( + sess: Session = Depends(middleware.get_db) +): + in_memory = BytesIO() + zf = ZipFile(in_memory, mode="w") + for server in db.wireguard.server_get_all(sess): + zf.writestr(f"{server.interface}/{server.interface}.conf", server.configuration) + + for peer in server.peers: + zf.writestr(f"{server.interface}/peers/{peer.name}_{peer.address.replace('.','-')}.conf", server.configuration) + + zf.close() + in_memory.seek(0) + + now = datetime.now().strftime("%m-%d-%Y-%H:%M:%S") + return StreamingResponse(in_memory, media_type="application/zip", headers={ + "Content-Disposition": f'attachment; filename="wg-manager-dump-{now}.zip"' + })