diff --git a/actions/wireguard b/actions/wireguard index 693477bcb..411db78c8 100755 --- a/actions/wireguard +++ b/actions/wireguard @@ -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): diff --git a/plinth/modules/wireguard/__init__.py b/plinth/modules/wireguard/__init__.py index e81fe5bd4..8e703fa3b 100644 --- a/plinth/modules/wireguard/__init__.py +++ b/plinth/modules/wireguard/__init__.py @@ -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, + }, + } diff --git a/plinth/modules/wireguard/templates/wireguard.html b/plinth/modules/wireguard/templates/wireguard.html index 584c7abd6..bbbcbd792 100644 --- a/plinth/modules/wireguard/templates/wireguard.html +++ b/plinth/modules/wireguard/templates/wireguard.html @@ -26,36 +26,36 @@
{% trans "Peers allowed to connect to this server" %}
| {% trans "Public Key" %} | {% trans "Last Connected Time" %} | {% trans "Delete" %} | |
|---|---|---|---|
| - - {{ client.public_key }} - - | -{{ client.latest_handshake }} | + + {{ peer.public_key }} + + +{{ peer.latest_handshake }} | + href="{% url 'wireguard:delete-client' peer.public_key %}"> | -
| + | {% blocktrans trimmed %} No peers configured to connect to this {{ box_name }} yet. {% endblocktrans %} - | +
{% trans "Peer servers that FreedomBox will connect to" %}
-{% trans "No connections to remove servers are configured yet." %}
+| {% trans "Endpoint" %} | +{% trans "Public Key" %} | +{% trans "Last Connected Time" %} | +{% trans "Edit" %} | +
|---|---|---|---|
| {{ peer.endpoint }} | +{{ peer.public_key }} | +{{ peer.latest_handshake }} | +Edit | +
| + {% blocktrans trimmed %} + No connections to remote servers are configured yet. + {% endblocktrans %} + | +
{% trans "IP address to use:" %}
{% trans "Server endpoints:" %}
-{% trans "Server's public key:" %} {{ server.public_key }}
+{% trans "Server's public key:" %} {{ my_server.public_key }}
{% trans "Pre-shared key:" %}