wireguard: Allow deleting a client

Signed-off-by: James Valleroy <jvalleroy@mailbox.org>
Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
James Valleroy 2019-09-05 15:49:04 -04:00
parent e00c28f36e
commit 415e1eb4ba
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
5 changed files with 89 additions and 7 deletions

View File

@ -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()

View File

@ -30,13 +30,17 @@
<tr>
<th>{% trans "Public Key" %}</th>
<th>{% trans "Last Connected Time" %}</th>
<th>{% trans "Edit" %}</th>
<th>{% trans "Delete" %}</th>
</tr>
{% for client in server_clients %}
<tr>
<td>{{ client.public_key }}</td>
<td>{{ client.latest_handshake }}</td>
<td>Edit</td>
<td><a class="btn btn-sm btn-default"
href="{% url 'wireguard:delete-client' client.public_key %}">
<span class="fa fa-trash-o" aria-hidden="true">
</span>
</td>
</tr>
{% endfor %}
</table>

View File

@ -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 <http://www.gnu.org/licenses/>.
#
{% endcomment %}
{% load bootstrap %}
{% load i18n %}
{% block content %}
<h3>{{ title }}</h3>
<p>
{% trans "Are you sure that you want to delete this client?" %}
</p>
<p>
<b>{{ public_key }}</b>
</p>
<form class="form" method="post">
{% csrf_token %}
<input type="submit" class="btn btn-danger"
value="{% trans "Delete Client" %}"/>
</form>
{% endblock %}

View File

@ -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<public_key>[^/]+)/delete/$',
views.DeleteClientView.as_view(), name='delete-client'),
]

View File

@ -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')