From e00c28f36e34c65c2c97a9372ba8869dd33ef027 Mon Sep 17 00:00:00 2001 From: James Valleroy Date: Thu, 5 Sep 2019 15:15:54 -0400 Subject: [PATCH] wireguard: Show list of added clients Signed-off-by: James Valleroy Reviewed-by: Sunil Mohan Adapa --- actions/wireguard | 26 +++++++++++++++++++ plinth/modules/wireguard/__init__.py | 2 ++ .../wireguard/templates/wireguard.html | 20 +++++++++++--- plinth/modules/wireguard/views.py | 15 +++++++++-- 4 files changed, 58 insertions(+), 5 deletions(-) diff --git a/actions/wireguard b/actions/wireguard index 261b90452..9d0fe0900 100755 --- a/actions/wireguard +++ b/actions/wireguard @@ -20,6 +20,7 @@ Configuration helper for WireGuard. """ import argparse +import json import subprocess SERVER_INTERFACE = 'wg0' @@ -30,6 +31,9 @@ def parse_arguments(): parser = argparse.ArgumentParser() subparsers = parser.add_subparsers(dest='subcommand', help='Sub command') + subparsers.add_parser('setup', help='Setup WireGuard') + subparsers.add_parser('list-clients', help='List all clients') + add_client = subparsers.add_parser('add-client', help='Add a client') add_client.add_argument('publickey', help='Public key for the client') @@ -37,6 +41,28 @@ def parse_arguments(): return parser.parse_args() +def subcommand_setup(_): + """Setup WireGuard.""" + subprocess.run( + ['ip', 'link', 'add', 'dev', SERVER_INTERFACE, 'type', 'wireguard'], + check=True) + + +def subcommand_list_clients(_): + """List all clients.""" + clients = [] + output = subprocess.check_output( + ['wg', 'show', SERVER_INTERFACE, 'latest-handshakes']).decode().strip() + for client_info in output.split('\n'): + public_key, latest_handshake = client_info.split() + clients.append({ + 'public_key': public_key, + 'latest_handshake': latest_handshake, + }) + + print(json.dumps(clients)) + + def subcommand_add_client(arguments): """Add a client.""" subprocess.run( diff --git a/plinth/modules/wireguard/__init__.py b/plinth/modules/wireguard/__init__.py index fd67c345e..00c1013e8 100644 --- a/plinth/modules/wireguard/__init__.py +++ b/plinth/modules/wireguard/__init__.py @@ -21,6 +21,7 @@ FreedomBox app for wireguard. from django.urls import reverse_lazy from django.utils.translation import ugettext_lazy as _ +from plinth import actions from plinth import app as app_module from plinth import cfg, frontpage, menu from plinth.modules.firewall.components import Firewall @@ -95,4 +96,5 @@ def init(): def setup(helper, old_version=None): """Install and configure the module.""" helper.install(managed_packages) + helper.call('post', actions.superuser_run, 'wireguard', ['setup']) helper.call('post', app.enable) diff --git a/plinth/modules/wireguard/templates/wireguard.html b/plinth/modules/wireguard/templates/wireguard.html index 499a3368f..d65f050c4 100644 --- a/plinth/modules/wireguard/templates/wireguard.html +++ b/plinth/modules/wireguard/templates/wireguard.html @@ -23,10 +23,24 @@ {% block configuration %}

{% trans "Server" %}

+

{% trans "Peers allowed to connect to this server" %}

-
    {% trans "public key" %}
-
    {% trans "last connected time" %}
-
    {% trans "edit" %}
+ + + + + + + {% for client in server_clients %} + + + + + + {% endfor %} +
{% trans "Public Key" %}{% trans "Last Connected Time" %}{% trans "Edit" %}
{{ client.public_key }}{{ client.latest_handshake }}Edit
+ diff --git a/plinth/modules/wireguard/views.py b/plinth/modules/wireguard/views.py index 1e72dd21d..1896d9f13 100644 --- a/plinth/modules/wireguard/views.py +++ b/plinth/modules/wireguard/views.py @@ -18,8 +18,10 @@ Views for WireGuard application. """ -from django.contrib import messages +import json + from django.contrib.messages.views import SuccessMessageMixin +from django.urls import reverse_lazy from django.utils.translation import ugettext as _ from django.views.generic import FormView @@ -41,11 +43,20 @@ class WireguardView(AppView): template_name = 'wireguard.html' port_forwarding_info = wireguard.port_forwarding_info + def get_context_data(self, **kwargs): + """Return additional context for rendering the template.""" + context = super().get_context_data(**kwargs) + clients_list = actions.superuser_run('wireguard', ['list-clients']) + context['server_clients'] = json.loads(clients_list) + return context + class AddClientView(SuccessMessageMixin, FormView): """View to add a client.""" form_class = forms.AddClientForm template_name = 'wireguard_add_client.html' + success_url = reverse_lazy('wireguard:index') + success_message = _('Added new client.') def get_context_data(self, **kwargs): """Return additional context for rendering the template.""" @@ -58,4 +69,4 @@ class AddClientView(SuccessMessageMixin, FormView): public_key = form.cleaned_data.get('public_key') actions.superuser_run( 'wireguard', ['add-client', public_key]) - messages.success(self.request, _('Added new client.')) + return super().form_valid(form)