diff --git a/actions/wireguard b/actions/wireguard index 411db78c8..5d195a757 100755 --- a/actions/wireguard +++ b/actions/wireguard @@ -125,18 +125,16 @@ def subcommand_add_server(arguments): ['wg', 'show', 'interfaces']).decode().strip() interfaces = output.split() interface_num = 1 - for interface in interfaces: + new_interface_name = 'wg1' + while new_interface_name in interfaces: + interface_num += 1 new_interface_name = 'wg' + str(interface_num) - if interface == new_interface_name: - interface_num += 1 - else: - break subprocess.run( ['ip', 'link', 'add', 'dev', new_interface_name, 'type', 'wireguard'], check=True) - args = ['wg', 'set', interface, 'peer', arguments.public_key] + args = ['wg', 'set', new_interface_name, 'peer', arguments.public_key] if arguments.pre_shared_key: args += ['preshared-key', arguments.pre_shared_key] diff --git a/plinth/modules/wireguard/__init__.py b/plinth/modules/wireguard/__init__.py index 8e703fa3b..88ff30995 100644 --- a/plinth/modules/wireguard/__init__.py +++ b/plinth/modules/wireguard/__init__.py @@ -109,7 +109,7 @@ def get_info(): output = actions.superuser_run('wireguard', ['get-info']) info = json.loads(output) my_server_info = info.pop(SERVER_INTERFACE) - my_client_servers = [interface['peers'][0] or {} + my_client_servers = [interface['peers'] and interface['peers'][0] or {} for interface in info.values()] return { 'my_server': { diff --git a/plinth/modules/wireguard/templates/wireguard.html b/plinth/modules/wireguard/templates/wireguard.html index bbbcbd792..48ca39aa5 100644 --- a/plinth/modules/wireguard/templates/wireguard.html +++ b/plinth/modules/wireguard/templates/wireguard.html @@ -51,7 +51,7 @@ {% else %} - + {% blocktrans trimmed %} No peers configured to connect to this {{ box_name }} yet. {% endblocktrans %} @@ -70,7 +70,7 @@

{% trans "Client" %}

{% trans "Peer servers that FreedomBox will connect to" %}

+ id="client-peers-list"> @@ -79,21 +79,25 @@ {% if client_peers %} {% for peer in client_peers %} - - - - - - + + + + + + {% endfor %} {% else %} - + {% endif %}
{% trans "Endpoint" %} {% trans "Public Key" %}
{{ peer.endpoint }}{{ peer.public_key }}{{ peer.latest_handshake }}Edit
{{ peer.endpoint }} + + {{ peer.public_key }} + + {{ peer.latest_handshake }}Edit
+ {% blocktrans trimmed %} No connections to remote servers are configured yet. {% endblocktrans %} -
diff --git a/plinth/modules/wireguard/templates/wireguard_show_server.html b/plinth/modules/wireguard/templates/wireguard_show_server.html new file mode 100644 index 000000000..9ad01eaed --- /dev/null +++ b/plinth/modules/wireguard/templates/wireguard_show_server.html @@ -0,0 +1,35 @@ +{% 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 i18n %} + +{% block content %} + +

{{ title }}

+ +

{% trans "Server Information" %}

+

{% trans "Endpoint:" %} {{ server.endpoint }}

+

{% trans "Public Key:" %} {{ server.public_key }}

+

{% trans "Pre-shared key:" %} {{ server.preshared_key }}

+

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

+

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

+

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

+ +{% endblock %} diff --git a/plinth/modules/wireguard/urls.py b/plinth/modules/wireguard/urls.py index 2135867bc..fa2a1f159 100644 --- a/plinth/modules/wireguard/urls.py +++ b/plinth/modules/wireguard/urls.py @@ -32,4 +32,6 @@ urlpatterns = [ views.DeleteClientView.as_view(), name='delete-client'), url(r'^apps/wireguard/server/add/$', views.AddServerView.as_view(), name='add-server'), + url(r'^apps/wireguard/server/(?P[^/]+)/show/$', + views.ShowServerView.as_view(), name='show-server'), ] diff --git a/plinth/modules/wireguard/views.py b/plinth/modules/wireguard/views.py index c5bfd454b..dc8076206 100644 --- a/plinth/modules/wireguard/views.py +++ b/plinth/modules/wireguard/views.py @@ -140,3 +140,22 @@ class AddServerView(SuccessMessageMixin, FormView): actions.superuser_run('wireguard', args) return super().form_valid(form) + + +class ShowServerView(SuccessMessageMixin, TemplateView): + """View to show a server's details.""" + template_name = 'wireguard_show_server.html' + + def get_context_data(self, **kwargs): + """Return additional context data for rendering the template.""" + context = super().get_context_data(**kwargs) + context['title'] = _('Show Server') + + public_key = self.kwargs['public_key'] + info = wireguard.get_info() + context.update(info) + for server in info['my_client']['servers']: + if server['public_key'] == public_key: + context['server'] = server + + return context