Browse Source

* Fixed bug where server configuration did not update after peer update.

pull/29/head
Per-Arne Andersen 5 years ago
parent
commit
41abe44f45
  1. 31
      wg_dashboard_backend/db/wireguard.py
  2. 27
      wg_dashboard_backend/routers/v1/peer.py
  3. 8
      wg_dashboard_backend/routers/v1/server.py
  4. 4
      wg_dashboard_backend/script/wireguard.py
  5. 1
      wg_dashboard_frontend/src/app/interfaces/server.ts
  6. 2
      wg_dashboard_frontend/src/app/page/components/modal-confirm/modal-confirm.component.ts
  7. 15
      wg_dashboard_frontend/src/app/page/dashboard/peer/peer.component.ts
  8. 9
      wg_dashboard_frontend/src/app/page/dashboard/server/server.component.html
  9. 13
      wg_dashboard_frontend/src/app/page/dashboard/server/server.component.ts
  10. 6
      wg_dashboard_frontend/src/app/services/server.service.ts

31
wg_dashboard_backend/db/wireguard.py

@ -50,9 +50,32 @@ def peer_remove(sess: Session, peer: schemas.WGPeer) -> bool:
sess.delete(db_peer) sess.delete(db_peer)
sess.commit() sess.commit()
server_update_configuration(sess, peer.server_id)
return True 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: 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() db_peer: models.WGPeer = peer_query_get_by_address(sess, peer.address, peer.server).one()
private_key, public_key = script.wireguard.generate_keys() 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: def server_endpoint_set(sess: Session, server: schemas.WGServer) -> bool:
return server_update_field(sess, server.interface, server, {"endpoint"}) 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()

27
wg_dashboard_backend/routers/v1/peer.py

@ -69,6 +69,9 @@ def add_peer(
if script.wireguard.is_running(server): if script.wireguard.is_running(server):
script.wireguard.add_peer(server, peer) 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) return schemas.WGPeer.from_orm(db_peer)
@ -94,27 +97,7 @@ def edit_peer(
peer: schemas.WGPeer, peer: schemas.WGPeer,
sess: Session = Depends(middleware.get_db) 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 peer = db.wireguard.peer_edit(sess, peer)
sess.query(models.WGPeer)\
.filter_by(id=peer.id)\
.update(peer.dict(exclude={"id"}))
# Generate server configuration return peer
server.configuration = script.wireguard.generate_config(server)
sess.add(server)
sess.commit()
return dict(
peer=peer,
server_configuration=server.configuration
)

8
wg_dashboard_backend/routers/v1/server.py

@ -158,3 +158,11 @@ def edit_server(
return 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

4
wg_dashboard_backend/script/wireguard.py

@ -109,9 +109,9 @@ def is_running(server: schemas.WGServer):
try: try:
output = _run_wg(server, ["show", server.interface]) output = _run_wg(server, ["show", server.interface])
if output is None or b'Unable to access interface: No such device' in output: 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, " "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 return False
except Exception as e: except Exception as e:
if b'No such device' in e.output: if b'No such device' in e.output:

1
wg_dashboard_frontend/src/app/interfaces/server.ts

@ -1,6 +1,7 @@
import { Peer } from './peer'; import { Peer } from './peer';
export interface Server { export interface Server {
id: number;
address: string; address: string;
interface: string; interface: string;
listen_port: string; listen_port: string;

2
wg_dashboard_frontend/src/app/page/components/modal-confirm/modal-confirm.component.ts

@ -37,6 +37,7 @@ export class ModalConfirmComponent implements OnInit {
} }
open($event){ open($event){
$event.stopPropagation();
if (this.noConfirm) { if (this.noConfirm) {
this.onConfirm.emit($event); this.onConfirm.emit($event);
return true; return true;
@ -54,6 +55,7 @@ export class ModalConfirmComponent implements OnInit {
} }
cancel($event){ cancel($event){
$event.stopPropagation();
this.onCancel.emit($event); this.onCancel.emit($event);
this.shown = false this.shown = false
} }

15
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('server') server: Server;
@Input('selectedPeer') selectedPeer: Peer; @Input('selectedPeer') selectedPeer: Peer;
@Input('onEvent') editPeerEmitter: EventEmitter<any> = new EventEmitter<any>(); @Input('onEvent') editPeerEmitter: EventEmitter<any> = new EventEmitter<any>();
@Input('cbOnPeerUpdate') cbOnPeerUpdate: Function;
constructor(public serverAPI: ServerService) { } constructor(public serverAPI: ServerService) { }
@ -28,6 +29,7 @@ export class PeerComponent implements OnInit {
if (msg.type === 'edit') { if (msg.type === 'edit') {
this.edit(); this.edit();
} else if (msg.type == 'delete') { } else if (msg.type == 'delete') {
this.delete(); this.delete();
} }
@ -41,11 +43,13 @@ export class PeerComponent implements OnInit {
// Submit the edit (True -> False) // Submit the edit (True -> False)
const idx = this.server.peers.indexOf(this.peer); const idx = this.server.peers.indexOf(this.peer);
this.serverAPI.editPeer(this.peer).subscribe((data) => { this.serverAPI.editPeer(this.peer).subscribe((peer) => {
this.server.configuration = data.server_configuration; Object.keys(peer).forEach(k => {
Object.keys(data.peer).forEach(k => { this.server.peers[idx][k] = peer[k];
this.server.peers[idx][k] = data.peer[k];
}); });
// Query server for server configuration update
this.cbOnPeerUpdate();
}); });
} else if (!this.peer._edit) { } else if (!this.peer._edit) {
@ -63,6 +67,9 @@ export class PeerComponent implements OnInit {
const idx = this.server.peers.indexOf(this.peer); const idx = this.server.peers.indexOf(this.peer);
this.serverAPI.deletePeer(this.peer).subscribe((apiServer) => { this.serverAPI.deletePeer(this.peer).subscribe((apiServer) => {
this.server.peers.splice(idx, 1); this.server.peers.splice(idx, 1);
// Query server for server configuration update
this.cbOnPeerUpdate();
}); });
} }

9
wg_dashboard_frontend/src/app/page/dashboard/server/server.component.html

@ -127,7 +127,7 @@
<!-- Edit buttons --> <!-- Edit buttons -->
<app-modal-confirm <app-modal-confirm
[noConfirm]="true" [noConfirm]="true"
(onConfirm)="selectedPeer=peer; this.editPeerEmitter.emit({type: 'edit', peer: peer}); $event.stopPropagation();" (onConfirm)="selectedPeer=peer; this.editPeerEmitter.emit({type: 'edit', peer: peer});"
icon="edit" icon="edit"
hover="Edit {{peer.name}}"> hover="Edit {{peer.name}}">
</app-modal-confirm> </app-modal-confirm>
@ -148,7 +148,12 @@
</tr> </tr>
<tr [hidden]="peer !== selectedPeer"> <tr [hidden]="peer !== selectedPeer">
<td colspan="6"> <td colspan="6">
<app-peer [onEvent]="this.editPeerEmitter" [(peer)]="server.peers[idx]" [(server)]="server"></app-peer> <app-peer
[cbOnPeerUpdate]="onPeerUpdate"
[onEvent]="this.editPeerEmitter"
[(peer)]="server.peers[idx]"
[(server)]="server">
</app-peer>
</td> </td>
</tr> </tr>

13
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 { Server } from '../../../interfaces/server';
import { ServerService } from '../../../services/server.service'; import { ServerService } from '../../../services/server.service';
import { DataService } from '../../../services/data.service'; import { DataService } from '../../../services/data.service';
@ -47,6 +47,9 @@ export class ServerComponent implements OnInit {
server_interface: this.server.interface server_interface: this.server.interface
}).subscribe((peer) => { }).subscribe((peer) => {
this.server.peers.push(peer); this.server.peers.push(peer);
// Query server for server configuration update
this.onPeerUpdate();
}); });
} }
@ -58,7 +61,7 @@ export class ServerComponent implements OnInit {
delete() { delete() {
const index = this.servers.indexOf(this.server); 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); this.servers.splice(index, 1);
}); });
} }
@ -72,6 +75,12 @@ export class ServerComponent implements OnInit {
this.editPeerEmitter.emit({ type: 'open', peer }); this.editPeerEmitter.emit({ type: 'open', peer });
} }
onPeerUpdate(){
this.serverAPI.serverConfig(this.server).subscribe((configuration) => {
this.server.configuration = configuration
})
}
pInt(string: string) { pInt(string: string) {
return parseInt(string); return parseInt(string);
} }

6
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); return this.http.post(this.peerURL + '/add', server_interface);
} }
public editPeer(peer: Peer): Subscribable<{ peer: Peer, server_configuration: string }> { public editPeer(peer: Peer): Subscribable<Peer> {
return this.http.post(this.peerURL + '/edit', peer); return this.http.post(this.peerURL + '/edit', peer);
} }
@ -84,8 +84,8 @@ export class ServerService {
return this.http.post(this.peerURL + '/config', peer); return this.http.post(this.peerURL + '/config', peer);
} }
public serverConfig(server: Server) { public serverConfig(server: Server): Subscribable<string> {
return this.http.post(this.serverURL + '/config', server); return this.http.get(this.serverURL + '/config/' + server.id.toString());
} }
public serverStats(server: Server) { public serverStats(server: Server) {

Loading…
Cancel
Save