From 69e418ada3eee8cd6832a7d6923b1849272ac319 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Sat, 18 Jan 2020 00:05:42 -0800 Subject: [PATCH] wireguard: Generate private key if needed when editing server - Refactor code that edits the connection to server. Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- plinth/modules/wireguard/utils.py | 27 +++++++++++++++++++++++---- plinth/modules/wireguard/views.py | 6 +----- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/plinth/modules/wireguard/utils.py b/plinth/modules/wireguard/utils.py index 86c404001..4a4f26119 100644 --- a/plinth/modules/wireguard/utils.py +++ b/plinth/modules/wireguard/utils.py @@ -114,7 +114,13 @@ def get_info(): } -def find_next_interface(): +def _generate_private_key(): + """Return a private key generated by 'wg' command.""" + process = subprocess.run(['wg', 'genkey'], check=True, capture_output=True) + return process.stdout.decode().strip() + + +def _find_next_interface(): """Find next unused wireguard interface name.""" output = subprocess.check_output(['wg', 'show', 'interfaces']).decode().strip() @@ -130,16 +136,29 @@ def find_next_interface(): def add_server(settings): """Add information for connecting to a server.""" - interface_name = find_next_interface() + interface_name = _find_next_interface() settings['common']['name'] = 'WireGuard-Client-' + interface_name settings['common']['interface'] = interface_name + if not settings['wireguard']['private_key']: + settings['wireguard']['private_key'] = _generate_private_key() + network.add_connection(settings) +def edit_server(interface, settings): + """Edit information for a connecting to a server.""" + settings['common']['interface'] = interface + settings['common']['name'] = 'WireGuard-Client-' + interface + if not settings['wireguard']['private_key']: + settings['wireguard']['private_key'] = _generate_private_key() + + connection = network.get_connection_by_interface_name(interface) + network.edit_connection(connection, settings) + + def setup_server(): """Setup a server connection that clients can connect to.""" - process = subprocess.run(['wg', 'genkey'], check=True, capture_output=True) - private_key = process.stdout.decode().strip() + private_key = _generate_private_key() settings = { 'common': { 'name': 'WireGuard-Server-wg0', diff --git a/plinth/modules/wireguard/views.py b/plinth/modules/wireguard/views.py index 519bcab11..b02ae65f1 100644 --- a/plinth/modules/wireguard/views.py +++ b/plinth/modules/wireguard/views.py @@ -239,12 +239,8 @@ class EditServerView(SuccessMessageMixin, FormView): def form_valid(self, form): """Update the server.""" - settings = form.get_settings() interface = self.kwargs['interface'] - settings['common']['interface'] = interface - settings['common']['name'] = 'WireGuard-Client-' + interface - connection = network.get_connection_by_interface_name(interface) - network.edit_connection(connection, settings) + utils.edit_server(interface, form.get_settings()) return super().form_valid(form)