wireguard: List peers in client section

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-09 17:40:26 -04:00
parent 901f89f393
commit 36fdedb9a7
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
5 changed files with 96 additions and 55 deletions

View File

@ -34,7 +34,8 @@ def parse_arguments():
subparsers = parser.add_subparsers(dest='subcommand', help='Sub command') subparsers = parser.add_subparsers(dest='subcommand', help='Sub command')
subparsers.add_parser('setup', help='Setup WireGuard') subparsers.add_parser('setup', help='Setup WireGuard')
subparsers.add_parser('get-info', help='Get server and clients info') subparsers.add_parser('get-info',
help='Get info for each configured interface')
add_client = subparsers.add_parser('add-client', help='Add a client') add_client = subparsers.add_parser('add-client', help='Add a client')
add_client.add_argument('publickey', help=PUBLIC_KEY_HELP) add_client.add_argument('publickey', help=PUBLIC_KEY_HELP)
@ -70,39 +71,38 @@ def subcommand_setup(_):
def subcommand_get_info(_): def subcommand_get_info(_):
"""Get server and clients info.""" """Get info for each configured interface."""
output = subprocess.check_output( output = subprocess.check_output(
['wg', 'show', SERVER_INTERFACE, 'dump']).decode().strip() ['wg', 'show', 'all', 'dump']).decode().strip()
lines = output.split('\n') lines = output.split('\n')
server_data = lines.pop(0).split() interfaces = {}
server = { for line in lines:
'private_key': server_data[0], fields = line.split()
'public_key': server_data[1], interface_name = fields[0]
'listen_port': server_data[2], if interface_name in interfaces:
'fwmark': server_data[3], peer = {
} 'public_key': fields[1],
'preshared_key': fields[2],
'endpoint': fields[3],
'allowed_ips': fields[4],
'latest_handshake': fields[5],
'transfer_rx': fields[6],
'transfer_tx': fields[7],
'persistent_keepalive': fields[8],
}
interfaces[interface_name]['peers'].append(peer)
clients = [] else:
for client_line in lines: interfaces[interface_name] = {
client_data = client_line.split() 'interface_name': interface_name,
client_info = { 'private_key': fields[1],
'public_key': client_data[0], 'public_key': fields[2],
'preshared_key': client_data[1], 'listen_port': fields[3],
'endpoint': client_data[2], 'fwmark': fields[4],
'allowed_ips': client_data[3], 'peers': [],
'latest_handshake': client_data[4], }
'transfer_rx': client_data[5],
'transfer_tx': client_data[6],
'persistent_keepalive': client_data[7],
}
clients.append(client_info)
# TODO: Add servers info from other interfaces. print(json.dumps(interfaces))
info = {
'server': server,
'clients': clients,
}
print(json.dumps(info))
def subcommand_add_client(arguments): def subcommand_add_client(arguments):

View File

@ -59,6 +59,8 @@ port_forwarding_info = [('UDP', 51820)]
app = None app = None
SERVER_INTERFACE = 'wg0'
class WireguardApp(app_module.App): class WireguardApp(app_module.App):
"""FreedomBox app for wireguard.""" """FreedomBox app for wireguard."""
@ -103,6 +105,18 @@ def setup(helper, old_version=None):
def get_info(): def get_info():
"""Get server and clients info.""" """Return server and clients info."""
info = actions.superuser_run('wireguard', ['get-info']) output = actions.superuser_run('wireguard', ['get-info'])
return json.loads(info) info = json.loads(output)
my_server_info = info.pop(SERVER_INTERFACE)
my_client_servers = [interface['peers'][0] or {}
for interface in info.values()]
return {
'my_server': {
'public_key': my_server_info['public_key'],
'clients': my_server_info['peers'],
},
'my_client': {
'servers': my_client_servers,
},
}

View File

@ -26,36 +26,36 @@
<p>{% trans "Peers allowed to connect to this server" %}</p> <p>{% trans "Peers allowed to connect to this server" %}</p>
<table class="table table-bordered table-condensed table-striped" <table class="table table-bordered table-condensed table-striped"
id="server-clients-list"> id="server-peers-list">
<tr> <tr>
<th>{% trans "Public Key" %}</th> <th>{% trans "Public Key" %}</th>
<th>{% trans "Last Connected Time" %}</th> <th>{% trans "Last Connected Time" %}</th>
<th>{% trans "Delete" %}</th> <th>{% trans "Delete" %}</th>
</tr> </tr>
{% if server_clients %} {% if server_peers %}
{% for client in server_clients %} {% for peer in server_peers %}
<tr> <tr>
<td> <td>
<a href="{% url 'wireguard:show-client' client.public_key %}"> <a href="{% url 'wireguard:show-client' peer.public_key %}">
{{ client.public_key }} {{ peer.public_key }}
</a> </a>
</td> </td>
<td>{{ client.latest_handshake }}</td> <td>{{ peer.latest_handshake }}</td>
<td><a class="btn btn-sm btn-default" <td><a class="btn btn-sm btn-default"
href="{% url 'wireguard:delete-client' client.public_key %}"> href="{% url 'wireguard:delete-client' peer.public_key %}">
<span class="fa fa-trash-o" aria-hidden="true"> <span class="fa fa-trash-o" aria-hidden="true">
</span> </span>
</td> </td>
</tr> </tr>
{% endfor %} {% endfor %}
{% else %} {% else %}
<tr> <tr>
<td> <td>
{% blocktrans trimmed %} {% blocktrans trimmed %}
No peers configured to connect to this {{ box_name }} yet. No peers configured to connect to this {{ box_name }} yet.
{% endblocktrans %} {% endblocktrans %}
</td> </td>
</tr> </tr>
{% endif %} {% endif %}
</table> </table>
@ -69,11 +69,35 @@
<h3>{% trans "Client" %}</h3> <h3>{% trans "Client" %}</h3>
<p>{% trans "Peer servers that FreedomBox will connect to" %}</p> <p>{% trans "Peer servers that FreedomBox will connect to" %}</p>
<ul>{% trans "endpoint" %}</ul> <table class="table table-bordered table-condensed table-striped"
<ul>{% trans "public key" %}</ul> id="client-peers-list">
<ul>{% trans "last connected time" %}</ul> <tr>
<ul>{% trans "edit" %}</ul> <th>{% trans "Endpoint" %}</th>
<p>{% trans "No connections to remove servers are configured yet." %}</p> <th>{% trans "Public Key" %}</th>
<th>{% trans "Last Connected Time" %}</th>
<th>{% trans "Edit" %}</th>
</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>
{% endfor %}
{% else %}
<tr>
<td>
{% blocktrans trimmed %}
No connections to remote servers are configured yet.
{% endblocktrans %}
</td>
</tr>
{% endif %}
</table>
<a title="{% trans 'Add a new server' %}" <a title="{% trans 'Add a new server' %}"
role="button" class="btn btn-default" role="button" class="btn btn-default"
href="{% url 'wireguard:add-server' %}"> href="{% url 'wireguard:add-server' %}">

View File

@ -27,7 +27,7 @@
<h4>{% trans "Connection Information" %}</h4> <h4>{% trans "Connection Information" %}</h4>
<p>{% trans "IP address to use:" %}</p> <p>{% trans "IP address to use:" %}</p>
<p>{% trans "Server endpoints:" %}</p> <p>{% trans "Server endpoints:" %}</p>
<p>{% trans "Server's public key:" %} {{ server.public_key }}</p> <p>{% trans "Server's public key:" %} {{ my_server.public_key }}</p>
<p>{% trans "Pre-shared key:" %}</p> <p>{% trans "Pre-shared key:" %}</p>
<h4>{% trans "Status" %}</h4> <h4>{% trans "Status" %}</h4>

View File

@ -47,7 +47,8 @@ class WireguardView(AppView):
"""Return additional context for rendering the template.""" """Return additional context for rendering the template."""
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
info = wireguard.get_info() info = wireguard.get_info()
context['server_clients'] = info['clients'] context['server_peers'] = info['my_server']['clients']
context['client_peers'] = info['my_client']['servers']
return context return context
@ -79,12 +80,14 @@ class ShowClientView(SuccessMessageMixin, TemplateView):
"""Return additional context data for rendering the template.""" """Return additional context data for rendering the template."""
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context['title'] = _('Show Client') context['title'] = _('Show Client')
public_key = self.kwargs['public_key'] public_key = self.kwargs['public_key']
info = wireguard.get_info() info = wireguard.get_info()
context['server'] = info['server'] context.update(info)
for client in info['clients']: for client in info['my_server']['clients']:
if client['public_key'] == public_key: if client['public_key'] == public_key:
context['client'] = client context['client'] = client
return context return context