wireguard: Add server information view

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-10 18:57:18 -04:00
parent 36fdedb9a7
commit 966b179756
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
6 changed files with 75 additions and 17 deletions

View File

@ -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]

View File

@ -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': {

View File

@ -51,7 +51,7 @@
{% else %}
<tr>
<td>
<td colspan="3">
{% blocktrans trimmed %}
No peers configured to connect to this {{ box_name }} yet.
{% endblocktrans %}
@ -70,7 +70,7 @@
<h3>{% trans "Client" %}</h3>
<p>{% trans "Peer servers that FreedomBox will connect to" %}</p>
<table class="table table-bordered table-condensed table-striped"
id="client-peers-list">
id="client-peers-list">
<tr>
<th>{% trans "Endpoint" %}</th>
<th>{% trans "Public Key" %}</th>
@ -79,21 +79,25 @@
</tr>
{% if client_peers %}
{% for peer in client_peers %}
<tr>
<td>{{ peer.endpoint }}</td>
<td>{{ peer.public_key }}</td>
<td>{{ peer.latest_handshake }}</td>
<td>Edit</td>
</tr>
<tr>
<td>{{ peer.endpoint }}</td>
<td>
<a href="{% url 'wireguard:show-server' peer.public_key %}">
{{ peer.public_key }}
</a>
</td>
<td>{{ peer.latest_handshake }}</td>
<td>Edit</td>
</tr>
{% endfor %}
{% else %}
<tr>
<td>
<td colspan="4">
{% blocktrans trimmed %}
No connections to remote servers are configured yet.
{% endblocktrans %}
</td>
</td>
</tr>
{% endif %}
</table>

View File

@ -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 <http://www.gnu.org/licenses/>.
#
{% endcomment %}
{% load i18n %}
{% block content %}
<h3>{{ title }}</h3>
<h4>{% trans "Server Information" %}</h4>
<p>{% trans "Endpoint:" %} {{ server.endpoint }}</p>
<p>{% trans "Public Key:" %} {{ server.public_key }}</p>
<p>{% trans "Pre-shared key:" %} {{ server.preshared_key }}</p>
<p>{% trans "Data transmitted:" %} {{ server.transfer_tx }}</p>
<p>{% trans "Data received:" %} {{ server.transfer_rx }}</p>
<p>{% trans "Latest handshake:" %} {{ server.latest_handshake }}</p>
{% endblock %}

View File

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

View File

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