diff --git a/freedombox/modules/wireguard/forms.py b/freedombox/modules/wireguard/forms.py index 19282be13..fc4e5c82a 100644 --- a/freedombox/modules/wireguard/forms.py +++ b/freedombox/modules/wireguard/forms.py @@ -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): diff --git a/freedombox/modules/wireguard/templates/wireguard.html b/freedombox/modules/wireguard/templates/wireguard.html index f6544262a..6d4b35163 100644 --- a/freedombox/modules/wireguard/templates/wireguard.html +++ b/freedombox/modules/wireguard/templates/wireguard.html @@ -10,60 +10,21 @@ {% block configuration %}

{% trans "As a Server" %}

+

+ {% 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 %} +

+ {% if server.public_key %} -

{% trans "Server" %}

- -

- {% blocktrans trimmed %} - Information for this {{ box_name }}: - {% endblocktrans %} -

- -
- - - - - - - - - - - - - - - - - - - - - -
{% trans "Property" %}{% trans "Value" %}
{% trans "Public Key" %}{{ server.public_key }}
{% trans "Endpoint(s)" %} -
{% for endpoint in server_endpoints %}{{ endpoint }}
-{% endfor %}
-
- {% blocktrans trimmed %} - {{ box_name }} VPN IP for services - {% endblocktrans %} - {{ server.ipv4_address }}, {{ server.ipv6_address }}
- {% blocktrans trimmed %} - {{ box_name }} DNS server - {% endblocktrans %} - {{ server.ipv4_address }}
-
- -

{% trans "Peers" %}

- -

{% trans "Peers allowed to connect to this server:" %}

+

{% trans "Clients allowed to connect to this server:" %}

- + {% 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" %} {% else %} @@ -125,7 +86,12 @@

{% trans "As a Client" %}

{% 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 %} +

+ {% blocktrans trimmed %} + Servers that {{ box_name }} will connect to: {% endblocktrans %}

diff --git a/freedombox/modules/wireguard/templates/wireguard_add_client.html b/freedombox/modules/wireguard/templates/wireguard_add_client.html index 994423228..fa53d1889 100644 --- a/freedombox/modules/wireguard/templates/wireguard_add_client.html +++ b/freedombox/modules/wireguard/templates/wireguard_add_client.html @@ -10,24 +10,75 @@

{{ title }}

+

+ {% blocktrans trimmed %} + Newly added client can be configured using the following information. + {% endblocktrans %} +

+ +
+
{% trans "Public Key" %}{% trans "Allowed IPs" %}{% trans "Client IP addresses" %} {% trans "Last Connected Time" %}
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
{% trans "Client" %}
{% trans "Public Key" %}{{ client_pubkey }}
+ {% trans "(Use this or generate one yourself)" %} +
{% trans "Private Key" %}{{ client_privkey }}
+ {% trans "(Use this or generate one yourself)" %} +
{% trans "IP Addresses" %}{{ next_ipv4_address }}, {{ next_ipv6_address }}
{% trans "DNS Server" %}{{ server.ipv4_address }}
+ {% blocktrans trimmed %} + {{ box_name }} Server + {% endblocktrans %} + {{ server.ipv4_address }}, {{ server.ipv6_address }}
{% trans "Server/Peer" %}
{% trans "Public Key" %}{{ server.public_key }}
{% trans "Endpoint(s)" %} + {% for endpoint in server_endpoints %} +
{{ endpoint }}
+ {% endfor %} +
{% trans "(Use one)" %}
+
{% trans "Allowed IPs" %}0.0.0.0/0, ::0/0
+
+
{% csrf_token %} {{ form|bootstrap }} - {% if next_ipv4_address and next_ipv6_address %} -
- -
- -
-
- {% endif %} -
diff --git a/freedombox/modules/wireguard/templates/wireguard_auto_add_client.html b/freedombox/modules/wireguard/templates/wireguard_auto_add_client.html index ecb61d46d..8001c59d2 100644 --- a/freedombox/modules/wireguard/templates/wireguard_auto_add_client.html +++ b/freedombox/modules/wireguard/templates/wireguard_auto_add_client.html @@ -52,32 +52,46 @@ - + + + + - + - + - + - + + + + + + + + - + - - + +
{% trans "Client Public Key" %}{% trans "Client" %}
{% trans "Public Key" %} {{ client_pubkey }}
{% trans "Client Private Key" %}{% trans "Private Key" %} {{ client_privkey }}
{% trans "Client IP Addresses" %}{% trans "IP Addresses" %} {{ ipv4_address }}, {{ ipv6_address }}
{% trans "Client DNS Server" %}{% trans "DNS Server" %} {{ server_ipv4_address }}
{% trans "Server/Peer Public Key" %} + {% blocktrans trimmed %} + {{ box_name }} Server + {% endblocktrans %} + {{ server_ipv4_address }}, {{ server_ipv6_address }}
{% trans "Server/Peer" %}
{% trans "Public Key" %} {{ server_pubkey }}
{% trans "Server/Peer Endpoint" %}{% trans "Endpoint" %} {{ domain_info.endpoint }}
{% trans "Server/Peer Allowed IPs" %}0.0.0.0/0,::0/0{% trans "Allowed IPs" %}0.0.0.0/0, ::0/0
diff --git a/freedombox/modules/wireguard/templates/wireguard_show_client.html b/freedombox/modules/wireguard/templates/wireguard_show_client.html index f0d494b61..251275fac 100644 --- a/freedombox/modules/wireguard/templates/wireguard_show_client.html +++ b/freedombox/modules/wireguard/templates/wireguard_show_client.html @@ -18,37 +18,55 @@
+ + + - + + + + + - + - + + + + + + + + + + + + + + + + - + - - - - - - - - - - - + + +
{% trans "Client" %}
{% trans "Client public key:" %}{% trans "Public Key" %} {{ client.public_key }}
{% trans "Private Key" %}{% trans "(Not stored on server)" %}
{% trans "IP addresses to use for client:" %}{% trans "IP Addresses" %} {{ client.allowed_ips|join:", " }}
{% trans "Pre-shared key:" %}{% trans "Pre-shared Key" %} {{ client.preshared_key }}
{% trans "DNS Server" %}{{ server.ipv4_address }}
+ {% blocktrans trimmed %} + {{ box_name }} Server + {% endblocktrans %} + {{ server.ipv4_address }}, {{ server.ipv6_address }}
{% trans "Server/Peer" %}
{% trans "Public Key" %}{{ server.public_key }}
{% trans "Server endpoints:" %}{% trans "Endpoint(s)" %} {% for endpoint in endpoints %}
{{ endpoint }}
{% endfor %}
{% trans "Server public key:" %}{{ server.public_key }}
{% trans "Server VPN IP address for services:" %}{{ server.ipv4_address }}, {{ server.ipv6_address }}
{% trans "DNS Server:" %}{{ server.ipv4_address }}
{% trans "Allowed IPs" %}0.0.0.0/0, ::0/0
@@ -59,15 +77,15 @@ - + - + - + diff --git a/freedombox/modules/wireguard/utils.py b/freedombox/modules/wireguard/utils.py index 881a7813f..6608a6b64 100644 --- a/freedombox/modules/wireguard/utils.py +++ b/freedombox/modules/wireguard/utils.py @@ -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 diff --git a/freedombox/modules/wireguard/views.py b/freedombox/modules/wireguard/views.py index 57900ac25..21bb0b2ba 100644 --- a/freedombox/modules/wireguard/views.py +++ b/freedombox/modules/wireguard/views.py @@ -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
{% trans "Data transmitted:" %}{% trans "Data transmitted" %} {{ client.status.transfer_tx|filesizeformat }}
{% trans "Data received:" %}{% trans "Data received" %} {{ client.status.transfer_rx|filesizeformat }}
{% trans "Latest handshake:" %}{% trans "Latest handshake" %} {{ client.status.latest_handshake|default:'' }}