diff --git a/README.md b/README.md index 4f423fb..7053d4f 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -# wireguard-manager -The wireguard-manager provides a easy-to-use graphical interface to setup and manage wireguard server(s). +# wg-manager +The wg-manager provides a easy-to-use graphical interface to setup and manage WireGuard server(s). The following features is implemented: * Create/Delete/Modify Server * Create/Delete/Modify Users @@ -26,7 +26,7 @@ docker run -d \ -v wireguard-manager:/config \ -e PORT="8888" \ -e ADMIN_USERNAME="admin" \ --e ADMIN_PASSWORD="admin \ +-e ADMIN_PASSWORD="admin" \ perara/wireguard-manager ``` diff --git a/wg_dashboard_backend/routers/v1/server.py b/wg_dashboard_backend/routers/v1/server.py index 031183b..1f9be45 100644 --- a/wg_dashboard_backend/routers/v1/server.py +++ b/wg_dashboard_backend/routers/v1/server.py @@ -6,6 +6,7 @@ from sqlalchemy.orm import Session from starlette.responses import JSONResponse import const +import models import schemas import middleware import db.wireguard @@ -33,6 +34,7 @@ def add_interface( ): server.post_up = server.post_up if server.post_up != "" else const.DEFAULT_POST_UP server.post_down = server.post_up if server.post_up != "" else const.DEFAULT_POST_DOWN + peers = server.peers if server.peers else [] # Public/Private key try: @@ -40,12 +42,30 @@ def add_interface( if server.filter_query(sess).count() != 0: raise HTTPException(status_code=400, detail="The server interface %s already exists in the database" % server.interface) - keys = script.wireguard.generate_keys() - server.private_key = keys["private_key"] - server.public_key = keys["public_key"] - server.configuration = script.wireguard.generate_config(server) + if not server.private_key: + keys = script.wireguard.generate_keys() + server.private_key = keys["private_key"] + server.public_key = keys["public_key"] + server.configuration = script.wireguard.generate_config(server) + server.peers = [] server.sync(sess) + + if len(peers) > 0: + server.from_db(sess) + + for schemaPeer in peers: + schemaPeer.server_id = server.id + schemaPeer.configuration = script.wireguard.generate_config(dict( + peer=schemaPeer, + server=server + )) + dbPeer = models.WGPeer(**schemaPeer.dict()) + sess.add(dbPeer) + sess.commit() + + server.from_db(sess) + except ValueError as e: raise HTTPException(status_code=400, detail=str(e))