wireguard: Simplify UI for manually adding a client

- Move server information (such as public key) to 'Add Client' page. It is only
needed there. 'Add Client Automatically' shows this information anyway and also
fills the configuration provided by QR code and file.

- Rename 'Allowed Client' to simply 'Client' and 'Peer' to 'Client' for
consistency and simplicity.

- Auto-generate a key pair for manual client addition also. The user may choose
to provide their own public like before.

- Provide description to what "As a Server" and "As a Client" mean.

- Provide server consistent information everywhere in a tabular format. This is
drawn from 'Add Client Automatically' page after improvements. The objective is
to make it easy for user to use WireGuard mobile app. So, use terminology from
there.

- Provide 'FreedomBox Server' IP addresses everywhere.

- Change how multiple endpoints are shown in the table.

Tests:

- WireGuard page has following changes:

  - Server information is not shown.

  - Description is shown in both the sections.

  - 'Add Allowed Client' button changed to 'Add Client'.

- Show client page has following changes:

  - Information is reorganized into two sections. All information present and
  correct.

  - Headers are styled differently.

  - 'FreedomBox Server' row is shown.

  - Table is divided into two separate sections.

- Add client automatically page has following changes:

  - FreedomBox Server IPs are shown.

  - Table is divided into two separate sections.

  - Headers are styled differently.

- Add Client page has following changes:

  - New description

  - Server information table is shown just like in 'Add Client Automatically'
  page. Extra information is shown for public/private keys and endpoints.

  - Public key and private keys are auto-generated.

  - Public key field is pre-populated.

  - Submitting the form creates the client with provided public key.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa authored and James Valleroy committed 2026-11-27 07:21:01 -05:00
1 parent ae945663d5
commit d4340dc903
7 files changed
+182 -108

No files matched your search

+3 -4
View File
@@ -67,10 +67,9 @@ def validate_ip_addresses(value: str):
class AddClientForm(forms.Form):
"""Form to add client."""
public_key = forms.CharField(
label=_('Public Key'), strip=True,
help_text=_('Public key of the peer. Example: '
'MConEJFIg6+DFHg2J1nn9SNLOSE9KR0ysdPgmPjibEs= .'),
validators=[validate_key])
label=_('Public Key'), strip=True, help_text=_(
'Public key of the client to add. Use the provided value or '
'generate one yourself.'), validators=[validate_key])
class AddServerForm(forms.Form):
@@ -10,60 +10,21 @@
{% block configuration %}
<h3>{% trans "As a Server" %}</h3>
<p>
{% blocktrans trimmed %}
{{ box_name }} acts a VPN server. Devices can connect to it and route traffic
safely using {{ box_name }}'s internet connection no matter where they are.
{% endblocktrans %}
</p>
{% if server.public_key %}
<h4>{% trans "Server" %}</h4>
<p>
{% blocktrans trimmed %}
Information for this {{ box_name }}:
{% endblocktrans %}
</p>
<div class="table-responsive">
<table class="table" id="server-info-table">
<tr>
<th>{% trans "Property" %}</th>
<th>{% trans "Value" %}</th>
</tr>
<tr>
<td>{% trans "Public Key" %}</td>
<td>{{ server.public_key }}</td>
</tr>
<tr>
<td>{% trans "Endpoint(s)" %}</td>
<td>
<pre>{% for endpoint in server_endpoints %}{{ endpoint }}
{% endfor %}</pre>
</td>
</tr>
<tr>
<td>
{% blocktrans trimmed %}
{{ box_name }} VPN IP for services
{% endblocktrans %}
</td>
<td>{{ server.ipv4_address }}, {{ server.ipv6_address }}</td>
</tr>
<tr>
<td>
{% blocktrans trimmed %}
{{ box_name }} DNS server
{% endblocktrans %}
</td>
<td>{{ server.ipv4_address }}</td>
</tr>
</table>
</div>
<h4>{% trans "Peers" %}</h4>
<p>{% trans "Peers allowed to connect to this server:" %}</p>
<p>{% trans "Clients allowed to connect to this server:" %}</p>
<div class="table-responsive">
<table class="table" id="server-peers-list">
<tr>
<th>{% trans "Public Key" %}</th>
<th>{% trans "Allowed IPs" %}</th>
<th>{% trans "Client IP addresses" %}</th>
<th>{% trans "Last Connected Time" %}</th>
</tr>
{% if server.peers %}
@@ -105,7 +66,7 @@
role="button" class="btn btn-default btn-add-client"
href="{% url 'wireguard:add-client' %}">
{% icon 'plus' %}
{% trans "Add Allowed Client" %}
{% trans "Add Client" %}
</a>
</div>
{% else %}
@@ -125,7 +86,12 @@
<h3>{% trans "As a Client" %}</h3>
<p>
{% blocktrans trimmed %}
Servers that {{ box_name }} will connect to:
{{ box_name }} can connect to a VPN server and route it's traffic
through the internet connection of a VPN provider.
{% endblocktrans %}
<p>
{% blocktrans trimmed %}
Servers that {{ box_name }} will connect to:
{% endblocktrans %}
</p>
<div class="table-responsive">
@@ -10,24 +10,75 @@
<h3>{{ title }}</h3>
<p>
{% blocktrans trimmed %}
Newly added client can be configured using the following information.
{% endblocktrans %}
</p>
<div class="table-responsive">
<table class="table table-sm" id="server-info-table">
<tbody>
<tr>
<td colspan="2" class="fs-4">{% trans "Client" %}</td>
</tr>
<tr>
<th>{% trans "Public Key" %}</th>
<td class="client-public-key">{{ client_pubkey }}<br>
{% trans "(Use this or generate one yourself)" %}
</td>
</tr>
<tr>
<th>{% trans "Private Key" %}</th>
<td class="client-private-key">{{ client_privkey }}<br>
{% trans "(Use this or generate one yourself)" %}
</td>
</tr>
<tr>
<th>{% trans "IP Addresses" %}</th>
<td>{{ next_ipv4_address }}, {{ next_ipv6_address }}</td>
</tr>
<tr>
<th>{% trans "DNS Server" %}</th>
<td>{{ server.ipv4_address }}</td>
</tr>
<tr>
<th>
{% blocktrans trimmed %}
{{ box_name }} Server
{% endblocktrans %}
</th>
<td>{{ server.ipv4_address }}, {{ server.ipv6_address }}</td>
</tr>
<tr>
<td colspan="2" class="fs-4">{% trans "Server/Peer" %}</td>
</tr>
<tr>
<th>{% trans "Public Key" %}</th>
<td>{{ server.public_key }}</td>
</tr>
<tr>
<th>{% trans "Endpoint(s)" %}</th>
<td>
{% for endpoint in server_endpoints %}
<div>{{ endpoint }}</div>
{% endfor %}
<div>{% trans "(Use one)" %}</div>
</td>
</tr>
<tr>
<th>{% trans "Allowed IPs" %}</th>
<td>0.0.0.0/0, ::0/0</td>
</tr>
</tbody>
</table>
</div>
<form class="form form-add-client" method="post">
{% csrf_token %}
{{ form|bootstrap }}
{% if next_ipv4_address and next_ipv6_address %}
<div class="form-group">
<label for="id_next_ip_adresses" class="control-label">
{% trans "IP addresses that will be assigned to this client" %}
</label>
<div>
<input type="text" id="id_next_ip_adresses" class="form-control"
value="{{ next_ipv4_address }}, {{ next_ipv6_address }}"
disabled="disabled">
</div>
</div>
{% endif %}
<input type="submit" class="btn btn-primary"
value="{% trans "Add Client" %}"/>
</form>
@@ -52,32 +52,46 @@
<table class="table table-sm">
<tbody>
<tr>
<td>{% trans "Client Public Key" %}</td>
<td colspan="2" class="fs-4">{% trans "Client" %}</td>
</tr>
<tr>
<th>{% trans "Public Key" %}</th>
<td class="client-public-key">{{ client_pubkey }}</td>
</tr>
<tr>
<td>{% trans "Client Private Key" %}</td>
<th>{% trans "Private Key" %}</th>
<td class="client-private-key">{{ client_privkey }}</td>
</tr>
<tr>
<td>{% trans "Client IP Addresses" %}</td>
<th>{% trans "IP Addresses" %}</th>
<td>{{ ipv4_address }}, {{ ipv6_address }}</td>
</tr>
<tr>
<td>{% trans "Client DNS Server" %}</td>
<th>{% trans "DNS Server" %}</th>
<td>{{ server_ipv4_address }}</td>
</tr>
<tr>
<td>{% trans "Server/Peer Public Key" %}</td>
<th>
{% blocktrans trimmed %}
{{ box_name }} Server
{% endblocktrans %}
</th>
<td>{{ server_ipv4_address }}, {{ server_ipv6_address }}</td>
</tr>
<tr>
<td colspan="2" class="fs-4">{% trans "Server/Peer" %}</td>
</tr>
<tr>
<th>{% trans "Public Key" %}</th>
<td>{{ server_pubkey }}</td>
</tr>
<tr>
<td>{% trans "Server/Peer Endpoint" %}</td>
<th>{% trans "Endpoint" %}</th>
<td>{{ domain_info.endpoint }}</td>
</tr>
<tr>
<td>{% trans "Server/Peer Allowed IPs" %}</td>
<td>0.0.0.0/0,::0/0</td>
<th>{% trans "Allowed IPs" %}</th>
<td>0.0.0.0/0, ::0/0</td>
</tr>
</tbody>
</table>
@@ -18,37 +18,55 @@
<div class="table-responsive">
<table class="table">
<tbody>
<tr>
<td colspan="2" class="fs-4">{% trans "Client" %}</td>
</tr>
<tr class="client-public-key">
<th>{% trans "Client public key:" %}</th>
<th>{% trans "Public Key" %}</th>
<td>{{ client.public_key }}</td>
</tr>
<tr class="client-public-key">
<th>{% trans "Private Key" %}</th>
<td>{% trans "(Not stored on server)" %}</td>
</tr>
<tr class="client-allowed-ips">
<th>{% trans "IP addresses to use for client:" %}</th>
<th>{% trans "IP Addresses" %}</th>
<td>{{ client.allowed_ips|join:", " }}</td>
</tr>
<tr class="client-preshared-key">
<th>{% trans "Pre-shared key:" %}</th>
<th>{% trans "Pre-shared Key" %}</th>
<td>{{ client.preshared_key }}</td>
</tr>
<tr class="client-dns">
<th>{% trans "DNS Server" %}</th>
<td>{{ server.ipv4_address }}</td>
</tr>
<tr class="client-services-ips">
<th>
{% blocktrans trimmed %}
{{ box_name }} Server
{% endblocktrans %}
</th>
<td>{{ server.ipv4_address }}, {{ server.ipv6_address }}</td>
</tr>
<tr>
<td colspan="2" class="fs-4">{% trans "Server/Peer" %}</td>
</tr>
<tr class="server-public-key">
<th>{% trans "Public Key" %}</th>
<td>{{ server.public_key }}</td>
</tr>
<tr class="server-endpoints">
<th>{% trans "Server endpoints:" %}</th>
<th>{% trans "Endpoint(s)" %}</th>
<td>
{% for endpoint in endpoints %}
<div>{{ endpoint }}</div>
{% endfor %}
</td>
</tr>
<tr class="server-public-key">
<th>{% trans "Server public key:" %}</th>
<td>{{ server.public_key }}</td>
</tr>
<tr class="server-ip">
<th>{% trans "Server VPN IP address for services:" %}</th>
<td>{{ server.ipv4_address }}, {{ server.ipv6_address }}</td>
</tr>
<tr class="server-dns">
<th>{% trans "DNS Server:" %}</th>
<td>{{ server.ipv4_address }}</td>
<tr>
<th>{% trans "Allowed IPs" %}</th>
<td>0.0.0.0/0, ::0/0</td>
</tr>
</tbody>
</table>
@@ -59,15 +77,15 @@
<table class="table">
<tbody>
<tr class="client-transfer-tx">
<th>{% trans "Data transmitted:" %}</th>
<th>{% trans "Data transmitted" %}</th>
<td>{{ client.status.transfer_tx|filesizeformat }}</td>
</tr>
<tr class="client-transfer-rx">
<th>{% trans "Data received:" %}</th>
<th>{% trans "Data received" %}</th>
<td>{{ client.status.transfer_rx|filesizeformat }}</td>
</tr>
<tr class="client-latest-handshake">
<th>{% trans "Latest handshake:" %}</th>
<th>{% trans "Latest handshake" %}</th>
<td>{{ client.status.latest_handshake|default:'' }}</td>
</tr>
</tbody>
+7 -2
View File
@@ -327,9 +327,14 @@ def setup_server():
logging.warning('Could not add server')
def _get_next_available_ip_addresses(
def get_next_available_ip_addresses(
settings=None) -> Tuple[IPv4Address, IPv6Address]:
"""Get the next available IPv4/IPv6 address to allocate to a client."""
if not settings:
setting_name = nm.SETTING_WIREGUARD_SETTING_NAME
connection = _server_connection()
settings = connection.get_setting_by_name(setting_name)
allocated_ips = set()
for peer_index in range(settings.get_peers_len()):
peer = settings.get_peer(peer_index)
@@ -389,7 +394,7 @@ def add_client(public_key: str) -> Tuple[IPv4Address, IPv6Address]:
if peer:
raise ValueError('Peer with public key already exists')
ipv4_address, ipv6_address = _get_next_available_ip_addresses(settings)
ipv4_address, ipv6_address = get_next_available_ip_addresses(settings)
peer = nm.WireGuardPeer.new()
peer.set_public_key(public_key, False)
peer.set_persistent_keepalive(25) # To keep NAT 'connections' alive
+34 -13
View File
@@ -6,6 +6,7 @@ Views for WireGuard application.
import base64
import urllib.parse
from io import BytesIO
from typing import Tuple
from django.contrib import messages
from django.contrib.messages.views import SuccessMessageMixin
@@ -55,25 +56,45 @@ class AddClientView(SuccessMessageMixin, FormView):
template_name = 'wireguard_add_client.html'
success_url = reverse_lazy('wireguard:index')
success_message = _('Added new client.')
_public_key: str | None = None
_private_key: str | None = None
def _get_client_key_pair(self) -> Tuple[str, str]:
"""Return a newly generated public/private key pair."""
if not self._private_key or not self._public_key:
(self._private_key,
self._public_key) = utils.generate_client_keypair()
return self._private_key, self._public_key
def get_context_data(self, **kwargs):
"""Return additional context for rendering the template."""
context = super().get_context_data(**kwargs)
context['title'] = _('Add Allowed Client')
context['title'] = _('Add Client')
# Show next available IP.
try:
connection = utils._server_connection()
setting_name = utils.nm.SETTING_WIREGUARD_SETTING_NAME
settings = connection.get_setting_by_name(setting_name)
(context['next_ipv4_address'], context['next_ipv6_address']
) = utils._get_next_available_ip_address(settings)
except Exception:
context['next_ipv4_address'] = None
context['next_ipv6_address'] = None
# Server information
context['server'] = utils.get_info()['my_server']
# Generate key pair
(context['client_privkey'],
context['client_pubkey']) = self._get_client_key_pair()
# Get next available IP.
context['next_ipv4_address'], context[
'next_ipv6_address'] = utils.get_next_available_ip_addresses()
# Get domains
context['server_endpoints'] = DomainName.list_names(
filter_for_service='wireguard')
return context
def get_initial(self):
"""Return initial form data."""
initial = super().get_initial()
initial['public_key'] = self._get_client_key_pair()[1]
return initial
def form_valid(self, form):
"""Add the client."""
public_key = form.cleaned_data.get('public_key')
@@ -152,7 +173,7 @@ class ShowClientView(SuccessMessageMixin, TemplateView):
def get_context_data(self, **kwargs) -> dict[str, object]:
"""Return additional context data for rendering the template."""
context = super().get_context_data(**kwargs)
context['title'] = _('Allowed Client')
context['title'] = _('Client')
public_key = urllib.parse.unquote(self.kwargs['public_key'])
server_info = utils.get_info()['my_server']
@@ -212,7 +233,7 @@ class DeleteClientView(SuccessMessageMixin, TemplateView):
def get_context_data(self, **kwargs):
"""Return additional context data for rendering the template."""
context = super().get_context_data(**kwargs)
context['title'] = _('Delete Allowed Client')
context['title'] = _('Delete Client')
context['public_key'] = urllib.parse.unquote(self.kwargs['public_key'])
return context