From 28bc880dc5ced361b1fecb3165f1721dc79bf019 Mon Sep 17 00:00:00 2001 From: James Valleroy Date: Wed, 18 Sep 2019 06:47:20 -0400 Subject: [PATCH] wireguard: Write pre-shared key to tempfile Signed-off-by: James Valleroy Reviewed-by: Sunil Mohan Adapa --- plinth/modules/wireguard/views.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/plinth/modules/wireguard/views.py b/plinth/modules/wireguard/views.py index c919a5b88..299c6025e 100644 --- a/plinth/modules/wireguard/views.py +++ b/plinth/modules/wireguard/views.py @@ -18,6 +18,7 @@ Views for WireGuard application. """ +import tempfile import urllib.parse from django.contrib import messages @@ -164,14 +165,19 @@ class AddServerView(SuccessMessageMixin, FormView): all_outgoing_traffic = form.cleaned_data.get('all_outgoing_traffic') args = ['add-server', '--endpoint', endpoint, '--client-ip', client_ip_address, '--public-key', public_key] + optional_psk_file = None if pre_shared_key: - # TODO: pass pre-shared key through stdin - args += ['--pre-shared-key', pre_shared_key] + optional_psk_file = tempfile.NamedTemporaryFile() + optional_psk_file.write(pre_shared_key.encode()) + args += ['--pre-shared-key', optional_psk_file.name] if all_outgoing_traffic: args.append('--all-outgoing') actions.superuser_run('wireguard', args) + if optional_psk_file: + optional_psk_file.close() + return super().form_valid(form) @@ -236,14 +242,19 @@ class EditServerView(SuccessMessageMixin, FormView): all_outgoing_traffic = form.cleaned_data.get('all_outgoing_traffic') args = ['modify-server', '--endpoint', endpoint, '--client-ip', client_ip_address, '--public-key', public_key] + optional_psk_file = None if pre_shared_key: - # TODO: pass pre-shared key through stdin - args += ['--pre-shared-key', pre_shared_key] + optional_psk_file = tempfile.NamedTemporaryFile() + optional_psk_file.write(pre_shared_key.encode()) + args += ['--pre-shared-key', optional_psk_file.name] if all_outgoing_traffic: args.append('--all-outgoing') actions.superuser_run('wireguard', args) + if optional_psk_file: + optional_psk_file.close() + return super().form_valid(form)