wireguard: Show public key even when connection is not active

When wireguard interface is not active 'wg show' does not provide any
information. In such case, get the public key by computing it from private key
by calling 'wg pubkey'.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2020-01-17 18:52:33 -08:00 committed by James Valleroy
parent f43d9a5469
commit 419559a86f
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
2 changed files with 21 additions and 2 deletions

View File

@ -62,7 +62,11 @@
{% blocktrans %}
Public key for this {{ box_name }}:
{% endblocktrans %}
<pre>{{ server.public_key }}</pre>
{% if server.public_key %}
<pre>{{ server.public_key }}</pre>
{% else %}
<p>{% trans "Not configured yet." %}</p>
{% endif %}
</p>
<a title="{% trans 'Add a new peer' %}"

View File

@ -20,6 +20,7 @@ Utilities for managing WireGuard.
import datetime
import json
import logging
import subprocess
import time
@ -30,6 +31,8 @@ nm = import_from_gi('NM', '1.0')
IP_TEMPLATE = '10.84.0.{}'
logger = logging.getLogger(__name__)
def get_nm_info():
"""Get information from network manager."""
@ -93,7 +96,12 @@ def get_info():
else:
my_client_servers[interface] = info
if interface not in status:
# If the NM connection is not active but the device link is up, 'wg
# show' will not show any public key configured on the interface.
if interface not in status or (interface in status and
not status[interface]['public_key']):
info['public_key'] = _get_public_key_from_private_key(
info['private_key'])
continue
info['public_key'] = status[interface]['public_key']
@ -135,6 +143,12 @@ def enable_connections(enable):
pass # Connection is already inactive
def _get_public_key_from_private_key(private_key):
process = subprocess.run(['wg', 'pubkey'], check=True, capture_output=True,
input=private_key.encode())
return process.stdout.decode()
def _generate_private_key():
"""Return a private key generated by 'wg' command."""
process = subprocess.run(['wg', 'genkey'], check=True, capture_output=True)
@ -209,6 +223,7 @@ def setup_server():
}
}
network.add_connection(settings)
logger.info('Created new WireGuard server connection')
def _get_next_available_ip_address(settings):