diff --git a/actions/wireguard b/actions/wireguard index ab2adcbf8..a8a309316 100755 --- a/actions/wireguard +++ b/actions/wireguard @@ -23,6 +23,8 @@ import argparse import json import subprocess +PUBLIC_KEY_HELP = 'Public key for the client' + SERVER_INTERFACE = 'wg0' @@ -32,14 +34,14 @@ def parse_arguments(): 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') + subparsers.add_parser('get-info', help='Get server and clients info') add_client = subparsers.add_parser('add-client', help='Add a client') - add_client.add_argument('publickey', help='Public key for the client') + add_client.add_argument('publickey', help=PUBLIC_KEY_HELP) remove_client = subparsers.add_parser('remove-client', help='Remove a client') - remove_client.add_argument('publickey', help='Public key for the client') + remove_client.add_argument('publickey', help=PUBLIC_KEY_HELP) subparsers.required = True return parser.parse_args() @@ -52,23 +54,39 @@ def subcommand_setup(_): check=True) -def subcommand_list_clients(_): - """List all clients.""" - clients = [] +def subcommand_get_info(_): + """Get server and clients info.""" output = subprocess.check_output( - ['wg', 'show', SERVER_INTERFACE, 'latest-handshakes']).decode().strip() - for client_info in output.split('\n'): - try: - public_key, latest_handshake = client_info.split() - except ValueError: - continue + ['wg', 'show', SERVER_INTERFACE, 'dump']).decode().strip() + lines = output.split('\n') + server_data = lines.pop(0).split() + server = { + 'private_key': server_data[0], + 'public_key': server_data[1], + 'listen_port': server_data[2], + 'fwmark': server_data[3], + } - clients.append({ - 'public_key': public_key, - 'latest_handshake': latest_handshake, - }) + clients = [] + for client_line in lines: + client_data = client_line.split() + client_info = { + 'public_key': client_data[0], + 'preshared_key': client_data[1], + 'endpoint': client_data[2], + 'allowed_ips': client_data[3], + 'latest_handshake': client_data[4], + 'transfer_rx': client_data[5], + 'transfer_tx': client_data[6], + 'persistent_keepalive': client_data[7], + } + clients.append(client_info) - print(json.dumps(clients)) + info = { + 'server': server, + 'clients': clients, + } + print(json.dumps(info)) def subcommand_add_client(arguments): diff --git a/plinth/modules/wireguard/__init__.py b/plinth/modules/wireguard/__init__.py index 00c1013e8..e81fe5bd4 100644 --- a/plinth/modules/wireguard/__init__.py +++ b/plinth/modules/wireguard/__init__.py @@ -18,6 +18,8 @@ FreedomBox app for wireguard. """ +import json + from django.urls import reverse_lazy from django.utils.translation import ugettext_lazy as _ @@ -98,3 +100,9 @@ def setup(helper, old_version=None): helper.install(managed_packages) helper.call('post', actions.superuser_run, 'wireguard', ['setup']) helper.call('post', app.enable) + + +def get_info(): + """Get server and clients info.""" + info = actions.superuser_run('wireguard', ['get-info']) + return json.loads(info) diff --git a/plinth/modules/wireguard/templates/wireguard.html b/plinth/modules/wireguard/templates/wireguard.html index 7565e6a96..c94ceef0d 100644 --- a/plinth/modules/wireguard/templates/wireguard.html +++ b/plinth/modules/wireguard/templates/wireguard.html @@ -26,23 +26,38 @@

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

+ id="server-clients-list"> - {% for client in server_clients %} + {% if server_clients %} + {% for client in server_clients %} + + + + + + {% endfor %} + + {% else %} - - - - {% endfor %} + {% endif %}
{% trans "Public Key" %} {% trans "Last Connected Time" %} {% trans "Delete" %}
+ + {{ client.public_key }} + + {{ client.latest_handshake }} + +
{{ client.public_key }}{{ client.latest_handshake }} - + + {% blocktrans trimmed %} + No peers configured to connect to this {{ box_name }} yet. + {% endblocktrans %}
. +# +{% endcomment %} + +{% load i18n %} + +{% block content %} + +

{{ title }}

+ +

{% trans "Connection Information" %}

+

{% trans "IP address to use:" %}

+

{% trans "Server endpoints:" %}

+

{% trans "Server's public key:" %} {{ server.public_key }}

+

{% trans "Pre-shared key:" %}

+ +

{% trans "Status" %}

+

{% trans "Client Public Key:" %} {{ client.public_key }}

+

{% trans "Data transmitted:" %} {{ client.transfer_tx }}

+

{% trans "Data received:" %} {{ client.transfer_rx }}

+

{% trans "Latest handshake:" %} {{ client.latest_handshake }}

+ +{% endblock %} diff --git a/plinth/modules/wireguard/urls.py b/plinth/modules/wireguard/urls.py index d7b72da6a..a454dbd4e 100644 --- a/plinth/modules/wireguard/urls.py +++ b/plinth/modules/wireguard/urls.py @@ -26,6 +26,8 @@ urlpatterns = [ url(r'^apps/wireguard/$', views.WireguardView.as_view(), name='index'), url(r'^apps/wireguard/client/add/$', views.AddClientView.as_view(), name='add-client'), + url(r'^apps/wireguard/client/(?P[^/]+)/show/$', + views.ShowClientView.as_view(), name='show-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 b47eefb6b..dcf5acfa1 100644 --- a/plinth/modules/wireguard/views.py +++ b/plinth/modules/wireguard/views.py @@ -18,8 +18,6 @@ Views for WireGuard application. """ -import json - from django.contrib import messages from django.contrib.messages.views import SuccessMessageMixin from django.shortcuts import redirect @@ -48,8 +46,8 @@ class WireguardView(AppView): 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) + info = wireguard.get_info() + context['server_clients'] = info['clients'] return context @@ -73,6 +71,23 @@ class AddClientView(SuccessMessageMixin, FormView): return super().form_valid(form) +class ShowClientView(SuccessMessageMixin, TemplateView): + """View to show a client's details.""" + template_name = 'wireguard_show_client.html' + + def get_context_data(self, **kwargs): + """Return additional context data for rendering the template.""" + context = super().get_context_data(**kwargs) + context['title'] = _('Show Client') + public_key = self.kwargs['public_key'] + info = wireguard.get_info() + context['server'] = info['server'] + for client in info['clients']: + if client['public_key'] == public_key: + context['client'] = client + return context + + class DeleteClientView(SuccessMessageMixin, TemplateView): """View to delete a client.""" template_name = 'wireguard_delete_client.html'