From 415e1eb4ba49f91cd6a81b3f72c33466dc69c587 Mon Sep 17 00:00:00 2001 From: James Valleroy Date: Thu, 5 Sep 2019 15:49:04 -0400 Subject: [PATCH] wireguard: Allow deleting a client Signed-off-by: James Valleroy Reviewed-by: Sunil Mohan Adapa --- actions/wireguard | 17 +++++++- .../wireguard/templates/wireguard.html | 8 +++- .../templates/wireguard_delete_client.html | 42 +++++++++++++++++++ plinth/modules/wireguard/urls.py | 4 +- plinth/modules/wireguard/views.py | 25 +++++++++-- 5 files changed, 89 insertions(+), 7 deletions(-) create mode 100644 plinth/modules/wireguard/templates/wireguard_delete_client.html diff --git a/actions/wireguard b/actions/wireguard index 9d0fe0900..ab2adcbf8 100755 --- a/actions/wireguard +++ b/actions/wireguard @@ -37,6 +37,10 @@ def parse_arguments(): add_client = subparsers.add_parser('add-client', help='Add a client') add_client.add_argument('publickey', help='Public key for the client') + remove_client = subparsers.add_parser('remove-client', + help='Remove a client') + remove_client.add_argument('publickey', help='Public key for the client') + subparsers.required = True return parser.parse_args() @@ -54,7 +58,11 @@ def subcommand_list_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() + try: + public_key, latest_handshake = client_info.split() + except ValueError: + continue + clients.append({ 'public_key': public_key, 'latest_handshake': latest_handshake, @@ -70,6 +78,13 @@ def subcommand_add_client(arguments): check=True) +def subcommand_remove_client(arguments): + """Remove a client.""" + subprocess.run( + ['wg', 'set', SERVER_INTERFACE, 'peer', arguments.publickey, 'remove'], + check=True) + + def main(): """Parse arguments and perform all duties.""" arguments = parse_arguments() diff --git a/plinth/modules/wireguard/templates/wireguard.html b/plinth/modules/wireguard/templates/wireguard.html index d65f050c4..7565e6a96 100644 --- a/plinth/modules/wireguard/templates/wireguard.html +++ b/plinth/modules/wireguard/templates/wireguard.html @@ -30,13 +30,17 @@ {% trans "Public Key" %} {% trans "Last Connected Time" %} - {% trans "Edit" %} + {% trans "Delete" %} {% for client in server_clients %} {{ client.public_key }} {{ client.latest_handshake }} - Edit + + + {% endfor %} diff --git a/plinth/modules/wireguard/templates/wireguard_delete_client.html b/plinth/modules/wireguard/templates/wireguard_delete_client.html new file mode 100644 index 000000000..b69efd23b --- /dev/null +++ b/plinth/modules/wireguard/templates/wireguard_delete_client.html @@ -0,0 +1,42 @@ +{% extends "base.html" %} +{% comment %} +# +# This file is part of FreedomBox. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +{% endcomment %} + +{% load bootstrap %} +{% load i18n %} + +{% block content %} + +

{{ title }}

+ +

+ {% trans "Are you sure that you want to delete this client?" %} +

+

+ {{ public_key }} +

+ +
+ {% csrf_token %} + + +
+ +{% endblock %} diff --git a/plinth/modules/wireguard/urls.py b/plinth/modules/wireguard/urls.py index 96b2f5de0..d7b72da6a 100644 --- a/plinth/modules/wireguard/urls.py +++ b/plinth/modules/wireguard/urls.py @@ -25,5 +25,7 @@ from plinth.modules.wireguard import views urlpatterns = [ url(r'^apps/wireguard/$', views.WireguardView.as_view(), name='index'), url(r'^apps/wireguard/client/add/$', views.AddClientView.as_view(), - name='add-client') + name='add-client'), + url(r'^apps/wireguard/client/(?P[^/]+)/delete/$', + views.DeleteClientView.as_view(), name='delete-client'), ] diff --git a/plinth/modules/wireguard/views.py b/plinth/modules/wireguard/views.py index 1896d9f13..b47eefb6b 100644 --- a/plinth/modules/wireguard/views.py +++ b/plinth/modules/wireguard/views.py @@ -20,10 +20,12 @@ Views for WireGuard application. import json +from django.contrib import messages from django.contrib.messages.views import SuccessMessageMixin +from django.shortcuts import redirect from django.urls import reverse_lazy from django.utils.translation import ugettext as _ -from django.views.generic import FormView +from django.views.generic import FormView, TemplateView import plinth.modules.wireguard as wireguard from plinth import actions @@ -67,6 +69,23 @@ class AddClientView(SuccessMessageMixin, FormView): def form_valid(self, form): """Add the client.""" public_key = form.cleaned_data.get('public_key') - actions.superuser_run( - 'wireguard', ['add-client', public_key]) + actions.superuser_run('wireguard', ['add-client', public_key]) return super().form_valid(form) + + +class DeleteClientView(SuccessMessageMixin, TemplateView): + """View to delete a client.""" + template_name = 'wireguard_delete_client.html' + + def get_context_data(self, **kwargs): + """Return additional context data for rendering the template.""" + context = super().get_context_data(**kwargs) + context['title'] = _('Delete Client') + context['public_key'] = self.kwargs['public_key'] + return context + + def post(self, request, public_key): + """Delete the client.""" + actions.superuser_run('wireguard', ['remove-client', public_key]) + messages.success(request, _('Client deleted.')) + return redirect('wireguard:index')