diff --git a/wg_dashboard_backend/db/wireguard.py b/wg_dashboard_backend/db/wireguard.py index bb4270c..71187fe 100644 --- a/wg_dashboard_backend/db/wireguard.py +++ b/wg_dashboard_backend/db/wireguard.py @@ -50,9 +50,32 @@ def peer_remove(sess: Session, peer: schemas.WGPeer) -> bool: sess.delete(db_peer) sess.commit() + server_update_configuration(sess, peer.server_id) + return True +def peer_edit(sess: Session, peer: schemas.WGPeer): + # Retrieve server from db + server: models.WGServer = get_server_by_id(sess, peer.server_id) + + # Generate peer configuration + peer.configuration = script.wireguard.generate_config(dict( + peer=peer, + server=server + )) + + # Update database record for Peer + sess.query(models.WGPeer) \ + .filter_by(id=peer.id) \ + .update(peer.dict(exclude={"id"})) + sess.commit() + + server_update_configuration(sess, server.id) + + return peer + + def peer_key_pair_generate(sess: Session, peer: schemas.WGPeer) -> schemas.WGPeer: db_peer: models.WGPeer = peer_query_get_by_address(sess, peer.address, peer.server).one() private_key, public_key = script.wireguard.generate_keys() @@ -159,3 +182,11 @@ def server_post_down_set(sess: Session, server: schemas.WGServer) -> bool: def server_endpoint_set(sess: Session, server: schemas.WGServer) -> bool: return server_update_field(sess, server.interface, server, {"endpoint"}) + + +def server_update_configuration(sess: Session, server_id: int) -> bool: + # Generate server configuration + server: models.WGServer = sess.query(models.WGServer).filter_by(id=server_id).one() + server.configuration = script.wireguard.generate_config(server) + sess.add(server) + sess.commit() diff --git a/wg_dashboard_backend/routers/v1/peer.py b/wg_dashboard_backend/routers/v1/peer.py index e57e1a0..7f9f4d4 100644 --- a/wg_dashboard_backend/routers/v1/peer.py +++ b/wg_dashboard_backend/routers/v1/peer.py @@ -69,6 +69,9 @@ def add_peer( if script.wireguard.is_running(server): script.wireguard.add_peer(server, peer) + # Update server configuration + db.wireguard.server_update_configuration(sess, db_peer.server_id) + return schemas.WGPeer.from_orm(db_peer) @@ -94,27 +97,7 @@ def edit_peer( peer: schemas.WGPeer, sess: Session = Depends(middleware.get_db) ): - # Retrieve server from db - server: models.WGServer = db.wireguard.get_server_by_id(sess, peer.server_id) - - # Generate peer configuration - peer.configuration = script.wireguard.generate_config(dict( - peer=peer, - server=server - )) - # Update database record for Peer - sess.query(models.WGPeer)\ - .filter_by(id=peer.id)\ - .update(peer.dict(exclude={"id"})) + peer = db.wireguard.peer_edit(sess, peer) - # Generate server configuration - server.configuration = script.wireguard.generate_config(server) - sess.add(server) - - sess.commit() - - return dict( - peer=peer, - server_configuration=server.configuration - ) + return peer diff --git a/wg_dashboard_backend/routers/v1/server.py b/wg_dashboard_backend/routers/v1/server.py index bed6eb1..185c501 100644 --- a/wg_dashboard_backend/routers/v1/server.py +++ b/wg_dashboard_backend/routers/v1/server.py @@ -158,3 +158,11 @@ def edit_server( return server + +@router.get("/config/{server_id}", response_model=str) +def server_config( + server_id: int, + sess: Session = Depends(middleware.get_db) +): + + return db.wireguard.get_server_by_id(sess, server_id=server_id).configuration diff --git a/wg_dashboard_backend/script/wireguard.py b/wg_dashboard_backend/script/wireguard.py index 7e0de11..668d8f7 100644 --- a/wg_dashboard_backend/script/wireguard.py +++ b/wg_dashboard_backend/script/wireguard.py @@ -109,9 +109,9 @@ def is_running(server: schemas.WGServer): try: output = _run_wg(server, ["show", server.interface]) if output is None or b'Unable to access interface: No such device' in output: - _LOGGER.warning("Unable to access interface: No such device. " + _LOGGER.warning("Unable to access interface: No such device. (%s)" "This may indicate that there is a bug somewhere, " - "or that you have manually deleted parts of the database") + "or that you have manually deleted parts of the database", server.interface) return False except Exception as e: if b'No such device' in e.output: diff --git a/wg_dashboard_frontend/src/app/interfaces/server.ts b/wg_dashboard_frontend/src/app/interfaces/server.ts index 5c28a10..dd8a801 100644 --- a/wg_dashboard_frontend/src/app/interfaces/server.ts +++ b/wg_dashboard_frontend/src/app/interfaces/server.ts @@ -1,6 +1,7 @@ import { Peer } from './peer'; export interface Server { + id: number; address: string; interface: string; listen_port: string; diff --git a/wg_dashboard_frontend/src/app/page/components/modal-confirm/modal-confirm.component.ts b/wg_dashboard_frontend/src/app/page/components/modal-confirm/modal-confirm.component.ts index 767be8b..03f4b99 100644 --- a/wg_dashboard_frontend/src/app/page/components/modal-confirm/modal-confirm.component.ts +++ b/wg_dashboard_frontend/src/app/page/components/modal-confirm/modal-confirm.component.ts @@ -37,6 +37,7 @@ export class ModalConfirmComponent implements OnInit { } open($event){ + $event.stopPropagation(); if (this.noConfirm) { this.onConfirm.emit($event); return true; @@ -54,6 +55,7 @@ export class ModalConfirmComponent implements OnInit { } cancel($event){ + $event.stopPropagation(); this.onCancel.emit($event); this.shown = false } diff --git a/wg_dashboard_frontend/src/app/page/dashboard/peer/peer.component.ts b/wg_dashboard_frontend/src/app/page/dashboard/peer/peer.component.ts index fc50bf3..09a4290 100644 --- a/wg_dashboard_frontend/src/app/page/dashboard/peer/peer.component.ts +++ b/wg_dashboard_frontend/src/app/page/dashboard/peer/peer.component.ts @@ -16,6 +16,7 @@ export class PeerComponent implements OnInit { @Input('server') server: Server; @Input('selectedPeer') selectedPeer: Peer; @Input('onEvent') editPeerEmitter: EventEmitter = new EventEmitter(); + @Input('cbOnPeerUpdate') cbOnPeerUpdate: Function; constructor(public serverAPI: ServerService) { } @@ -28,6 +29,7 @@ export class PeerComponent implements OnInit { if (msg.type === 'edit') { this.edit(); + } else if (msg.type == 'delete') { this.delete(); } @@ -41,11 +43,13 @@ export class PeerComponent implements OnInit { // Submit the edit (True -> False) const idx = this.server.peers.indexOf(this.peer); - this.serverAPI.editPeer(this.peer).subscribe((data) => { - this.server.configuration = data.server_configuration; - Object.keys(data.peer).forEach(k => { - this.server.peers[idx][k] = data.peer[k]; + this.serverAPI.editPeer(this.peer).subscribe((peer) => { + Object.keys(peer).forEach(k => { + this.server.peers[idx][k] = peer[k]; }); + + // Query server for server configuration update + this.cbOnPeerUpdate(); }); } else if (!this.peer._edit) { @@ -63,6 +67,9 @@ export class PeerComponent implements OnInit { const idx = this.server.peers.indexOf(this.peer); this.serverAPI.deletePeer(this.peer).subscribe((apiServer) => { this.server.peers.splice(idx, 1); + + // Query server for server configuration update + this.cbOnPeerUpdate(); }); } diff --git a/wg_dashboard_frontend/src/app/page/dashboard/server/server.component.html b/wg_dashboard_frontend/src/app/page/dashboard/server/server.component.html index 86c181c..b6f3843 100644 --- a/wg_dashboard_frontend/src/app/page/dashboard/server/server.component.html +++ b/wg_dashboard_frontend/src/app/page/dashboard/server/server.component.html @@ -127,7 +127,7 @@ @@ -148,7 +148,12 @@ - + + diff --git a/wg_dashboard_frontend/src/app/page/dashboard/server/server.component.ts b/wg_dashboard_frontend/src/app/page/dashboard/server/server.component.ts index 6e19383..dffb40c 100644 --- a/wg_dashboard_frontend/src/app/page/dashboard/server/server.component.ts +++ b/wg_dashboard_frontend/src/app/page/dashboard/server/server.component.ts @@ -1,4 +1,4 @@ -import { Component, EventEmitter, Input, OnInit, ViewEncapsulation } from '@angular/core'; +import { Component, EventEmitter, Input, OnInit } from '@angular/core'; import { Server } from '../../../interfaces/server'; import { ServerService } from '../../../services/server.service'; import { DataService } from '../../../services/data.service'; @@ -47,6 +47,9 @@ export class ServerComponent implements OnInit { server_interface: this.server.interface }).subscribe((peer) => { this.server.peers.push(peer); + + // Query server for server configuration update + this.onPeerUpdate(); }); } @@ -58,7 +61,7 @@ export class ServerComponent implements OnInit { delete() { const index = this.servers.indexOf(this.server); - this.serverAPI.deleteServer(this.server).subscribe((apiServer) => { + this.serverAPI.deleteServer(this.server).subscribe(() => { this.servers.splice(index, 1); }); } @@ -72,6 +75,12 @@ export class ServerComponent implements OnInit { this.editPeerEmitter.emit({ type: 'open', peer }); } + onPeerUpdate(){ + this.serverAPI.serverConfig(this.server).subscribe((configuration) => { + this.server.configuration = configuration + }) + } + pInt(string: string) { return parseInt(string); } diff --git a/wg_dashboard_frontend/src/app/services/server.service.ts b/wg_dashboard_frontend/src/app/services/server.service.ts index 2e61b2c..a4ed45b 100644 --- a/wg_dashboard_frontend/src/app/services/server.service.ts +++ b/wg_dashboard_frontend/src/app/services/server.service.ts @@ -35,7 +35,7 @@ export class ServerService { return this.http.post(this.peerURL + '/add', server_interface); } - public editPeer(peer: Peer): Subscribable<{ peer: Peer, server_configuration: string }> { + public editPeer(peer: Peer): Subscribable { return this.http.post(this.peerURL + '/edit', peer); } @@ -84,8 +84,8 @@ export class ServerService { return this.http.post(this.peerURL + '/config', peer); } - public serverConfig(server: Server) { - return this.http.post(this.serverURL + '/config', server); + public serverConfig(server: Server): Subscribable { + return this.http.get(this.serverURL + '/config/' + server.id.toString()); } public serverStats(server: Server) {