mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-01-21 07:55:00 +00:00
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:
parent
e00c28f36e
commit
415e1eb4ba
@ -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()
|
||||
|
||||
@ -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>
|
||||
|
||||
@ -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 %}
|
||||
@ -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'),
|
||||
]
|
||||
|
||||
@ -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')
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user