mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-01-21 07:55:00 +00:00
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:
parent
901f89f393
commit
36fdedb9a7
@ -34,7 +34,8 @@ def parse_arguments():
|
||||
subparsers = parser.add_subparsers(dest='subcommand', help='Sub command')
|
||||
|
||||
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.add_argument('publickey', help=PUBLIC_KEY_HELP)
|
||||
@ -70,39 +71,38 @@ def subcommand_setup(_):
|
||||
|
||||
|
||||
def subcommand_get_info(_):
|
||||
"""Get server and clients info."""
|
||||
"""Get info for each configured interface."""
|
||||
output = subprocess.check_output(
|
||||
['wg', 'show', SERVER_INTERFACE, 'dump']).decode().strip()
|
||||
['wg', 'show', 'all', '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],
|
||||
}
|
||||
interfaces = {}
|
||||
for line in lines:
|
||||
fields = line.split()
|
||||
interface_name = fields[0]
|
||||
if interface_name in interfaces:
|
||||
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 = []
|
||||
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)
|
||||
else:
|
||||
interfaces[interface_name] = {
|
||||
'interface_name': interface_name,
|
||||
'private_key': fields[1],
|
||||
'public_key': fields[2],
|
||||
'listen_port': fields[3],
|
||||
'fwmark': fields[4],
|
||||
'peers': [],
|
||||
}
|
||||
|
||||
# TODO: Add servers info from other interfaces.
|
||||
info = {
|
||||
'server': server,
|
||||
'clients': clients,
|
||||
}
|
||||
print(json.dumps(info))
|
||||
print(json.dumps(interfaces))
|
||||
|
||||
|
||||
def subcommand_add_client(arguments):
|
||||
|
||||
@ -59,6 +59,8 @@ port_forwarding_info = [('UDP', 51820)]
|
||||
|
||||
app = None
|
||||
|
||||
SERVER_INTERFACE = 'wg0'
|
||||
|
||||
|
||||
class WireguardApp(app_module.App):
|
||||
"""FreedomBox app for wireguard."""
|
||||
@ -103,6 +105,18 @@ def setup(helper, old_version=None):
|
||||
|
||||
|
||||
def get_info():
|
||||
"""Get server and clients info."""
|
||||
info = actions.superuser_run('wireguard', ['get-info'])
|
||||
return json.loads(info)
|
||||
"""Return server and clients 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 {}
|
||||
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,
|
||||
},
|
||||
}
|
||||
|
||||
@ -26,36 +26,36 @@
|
||||
|
||||
<p>{% trans "Peers allowed to connect to this server" %}</p>
|
||||
<table class="table table-bordered table-condensed table-striped"
|
||||
id="server-clients-list">
|
||||
id="server-peers-list">
|
||||
<tr>
|
||||
<th>{% trans "Public Key" %}</th>
|
||||
<th>{% trans "Last Connected Time" %}</th>
|
||||
<th>{% trans "Delete" %}</th>
|
||||
</tr>
|
||||
{% if server_clients %}
|
||||
{% for client in server_clients %}
|
||||
<tr>
|
||||
{% if server_peers %}
|
||||
{% for peer in server_peers %}
|
||||
<tr>
|
||||
<td>
|
||||
<a href="{% url 'wireguard:show-client' client.public_key %}">
|
||||
{{ client.public_key }}
|
||||
</a>
|
||||
</td>
|
||||
<td>{{ client.latest_handshake }}</td>
|
||||
<a href="{% url 'wireguard:show-client' peer.public_key %}">
|
||||
{{ peer.public_key }}
|
||||
</a>
|
||||
</td>
|
||||
<td>{{ peer.latest_handshake }}</td>
|
||||
<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>
|
||||
</td>
|
||||
</tr>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
|
||||
{% else %}
|
||||
<tr>
|
||||
<td>
|
||||
<td>
|
||||
{% blocktrans trimmed %}
|
||||
No peers configured to connect to this {{ box_name }} yet.
|
||||
{% endblocktrans %}
|
||||
</td>
|
||||
</td>
|
||||
</tr>
|
||||
{% endif %}
|
||||
</table>
|
||||
@ -69,11 +69,35 @@
|
||||
|
||||
<h3>{% trans "Client" %}</h3>
|
||||
<p>{% trans "Peer servers that FreedomBox will connect to" %}</p>
|
||||
<ul>{% trans "endpoint" %}</ul>
|
||||
<ul>{% trans "public key" %}</ul>
|
||||
<ul>{% trans "last connected time" %}</ul>
|
||||
<ul>{% trans "edit" %}</ul>
|
||||
<p>{% trans "No connections to remove servers are configured yet." %}</p>
|
||||
<table class="table table-bordered table-condensed table-striped"
|
||||
id="client-peers-list">
|
||||
<tr>
|
||||
<th>{% trans "Endpoint" %}</th>
|
||||
<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' %}"
|
||||
role="button" class="btn btn-default"
|
||||
href="{% url 'wireguard:add-server' %}">
|
||||
|
||||
@ -27,7 +27,7 @@
|
||||
<h4>{% trans "Connection Information" %}</h4>
|
||||
<p>{% trans "IP address to use:" %}</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>
|
||||
|
||||
<h4>{% trans "Status" %}</h4>
|
||||
|
||||
@ -47,7 +47,8 @@ class WireguardView(AppView):
|
||||
"""Return additional context for rendering the template."""
|
||||
context = super().get_context_data(**kwargs)
|
||||
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
|
||||
|
||||
|
||||
@ -79,12 +80,14 @@ class ShowClientView(SuccessMessageMixin, TemplateView):
|
||||
"""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']:
|
||||
context.update(info)
|
||||
for client in info['my_server']['clients']:
|
||||
if client['public_key'] == public_key:
|
||||
context['client'] = client
|
||||
|
||||
return context
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user