wireguard: Add ability to set private key in client addition

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa authored and James Valleroy committed 2020-01-18 13:53:24 -05:00
1 parent ad53848983
commit 9242a9404f
3 files changed
+47 -32

No files matched your search

+5
View File
@@ -43,6 +43,11 @@ class AddServerForm(forms.Form):
label=_('Public key of the server'), strip=True,
help_text=_('Public key of the server.'))
client_private_key = forms.CharField(
label=_('Private key of the client'), strip=True,
help_text=_('Optional. A new key is generated if left blank.'),
required=False)
pre_shared_key = forms.CharField(
label=_('Pre-shared key'), strip=True, required=False,
help_text=_('Optional: a shared secret key provided by the server to '
+25 -18
View File
@@ -18,6 +18,7 @@
Views for WireGuard application.
"""
import json
import tempfile
import urllib.parse
@@ -161,22 +162,25 @@ class AddServerView(SuccessMessageMixin, FormView):
endpoint = form.cleaned_data.get('endpoint')
client_ip_address = form.cleaned_data.get('client_ip_address')
public_key = form.cleaned_data.get('public_key')
client_private_key = form.cleaned_data.get('client_private_key')
pre_shared_key = form.cleaned_data.get('pre_shared_key')
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
args = [
'add-server', '--endpoint', endpoint, '--client-ip',
client_ip_address, '--public-key', public_key
]
secret_args = {}
if client_private_key:
secret_args['client_private_key'] = client_private_key
if pre_shared_key:
optional_psk_file = tempfile.NamedTemporaryFile()
optional_psk_file.write(pre_shared_key.encode())
args += ['--pre-shared-key', optional_psk_file.name]
secret_args['pre_shared_key'] = pre_shared_key
if all_outgoing_traffic:
args.append('--all-outgoing')
actions.superuser_run('wireguard', args)
if optional_psk_file:
optional_psk_file.close()
actions.superuser_run('wireguard', args,
input=json.dumps(secret_args).encode())
return super().form_valid(form)
@@ -238,22 +242,25 @@ class EditServerView(SuccessMessageMixin, FormView):
endpoint = form.cleaned_data.get('endpoint')
client_ip_address = form.cleaned_data.get('client_ip_address')
public_key = form.cleaned_data.get('public_key')
client_private_key = form.client_data.get('client_private_key')
pre_shared_key = form.cleaned_data.get('pre_shared_key')
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
args = [
'modify-server', '--endpoint', endpoint, '--client-ip',
client_ip_address, '--public-key', public_key
]
secret_args = {}
if client_private_key:
secret_args['client_private_key'] = client_private_key
if pre_shared_key:
optional_psk_file = tempfile.NamedTemporaryFile()
optional_psk_file.write(pre_shared_key.encode())
args += ['--pre-shared-key', optional_psk_file.name]
secret_args['pre_shared_key'] = pre_shared_key
if all_outgoing_traffic:
args.append('--all-outgoing')
actions.superuser_run('wireguard', args)
if optional_psk_file:
optional_psk_file.close()
actions.superuser_run('wireguard', args,
input=json.dumps(secret_args).encode())
return super().form_valid(form)