mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-11-29 08:25:10 +00:00
- 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>
181 lines
6.2 KiB
Python
181 lines
6.2 KiB
Python
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
"""
|
|
Forms for wireguard module.
|
|
"""
|
|
|
|
import base64
|
|
import binascii
|
|
from ipaddress import IPv4Interface, IPv6Interface, ip_interface
|
|
|
|
from django import forms
|
|
from django.core.exceptions import ValidationError
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
KEY_LENGTH = 32
|
|
|
|
|
|
def validate_key(key):
|
|
"""Validate a WireGuard public/private/pre-shared key."""
|
|
valid = False
|
|
if isinstance(key, str):
|
|
key = key.encode()
|
|
|
|
try:
|
|
decoded_key = base64.b64decode(key)
|
|
if len(decoded_key) == KEY_LENGTH and base64.b64encode(
|
|
decoded_key) == key:
|
|
valid = True
|
|
except binascii.Error:
|
|
pass
|
|
|
|
if not valid:
|
|
raise ValidationError(_('Invalid key.'))
|
|
|
|
|
|
def validate_endpoint(endpoint):
|
|
"""Validate an endpoint of the form: demo.wireguard.com:12912.
|
|
|
|
Implemented similar to nm-utils.c::_parse_endpoint().
|
|
|
|
"""
|
|
valid = False
|
|
try:
|
|
destination, port = endpoint.rsplit(':', maxsplit=1)
|
|
port = int(port)
|
|
if 1 <= port < ((1 << 16) - 1) and destination:
|
|
valid = True
|
|
|
|
if destination[0] == '[' and (destination[-1] != ']'
|
|
or len(destination) < 3):
|
|
valid = False
|
|
except ValueError:
|
|
pass
|
|
|
|
if not valid:
|
|
raise ValidationError('Invalid endpoint.')
|
|
|
|
|
|
def validate_ip_addresses(value: str):
|
|
"""Check that value is a valid IP address with an optional network."""
|
|
try:
|
|
for ip_address in value.split(','):
|
|
ip_interface(ip_address.strip())
|
|
except ValueError:
|
|
raise ValidationError(_('Not a valid list of IP addresses.'))
|
|
|
|
|
|
class AddClientForm(forms.Form):
|
|
"""Form to add client."""
|
|
public_key = forms.CharField(
|
|
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):
|
|
"""Form to add server."""
|
|
peer_endpoint = forms.CharField(
|
|
label=_('Endpoint of the server'), strip=True,
|
|
help_text=_('Domain name and port in the form "ip:port". Example: '
|
|
'demo.wireguard.com:12912 .'),
|
|
validators=[validate_endpoint])
|
|
|
|
peer_public_key = forms.CharField(
|
|
label=_('Public key of the server'), strip=True, help_text=_(
|
|
'Provided by the server operator, a long string of characters. '
|
|
'Example: MConEJFIg6+DFHg2J1nn9SNLOSE9KR0ysdPgmPjibEs= .'),
|
|
validators=[validate_key])
|
|
|
|
ip_addresses = forms.CharField(
|
|
label=_('Client IP addresses provided by server'), strip=True,
|
|
help_text=_(
|
|
'Comma separated list of IP addresses assigned to this machine on '
|
|
'the VPN after connecting to the endpoint. This value is usually '
|
|
'provided by server operator. Example: 192.168.0.10, '
|
|
'2a03:7c80:4b2c:91a2:5d41:ffee:9b82:7c17. You can also specify '
|
|
'the network. This will allow reaching machines in the network. '
|
|
'Examples: 10.68.12.43/24, 10.68.12.43/255.255.255.0.'),
|
|
validators=[validate_ip_addresses])
|
|
|
|
private_key = forms.CharField(
|
|
label=_('Private key of this machine'), strip=True, help_text=_(
|
|
'Optional. New public/private keys are generated if left blank. '
|
|
'Public key can then be provided to the server. This is the '
|
|
'recommended way. However, some server operators insist on '
|
|
'providing this. Example: '
|
|
'MConEJFIg6+DFHg2J1nn9SNLOSE9KR0ysdPgmPjibEs= .'), required=False,
|
|
validators=[validate_key])
|
|
|
|
preshared_key = forms.CharField(
|
|
label=_('Pre-shared key'), strip=True, required=False, help_text=_(
|
|
'Optional. A shared secret key provided by the server to add an '
|
|
'additional layer of security. Fill in only if provided. Example: '
|
|
'MConEJFIg6+DFHg2J1nn9SNLOSE9KR0ysdPgmPjibEs=.'),
|
|
validators=[validate_key])
|
|
|
|
default_route = forms.BooleanField(
|
|
label=_('Use this connection to send all outgoing traffic'),
|
|
required=False, help_text=_(
|
|
'Typically checked for a VPN service through which all traffic '
|
|
'is sent.'))
|
|
|
|
def _build_ipv4_settings(self, iface) -> dict:
|
|
"""Build IPv4 NM settings from interfaces."""
|
|
return {
|
|
'method': 'manual',
|
|
'address': str(iface.ip),
|
|
'netmask': str(iface.netmask),
|
|
'gateway': '',
|
|
'dns': '',
|
|
'second_dns': '',
|
|
}
|
|
|
|
def _build_ipv6_settings(self, iface) -> dict:
|
|
"""Build IPv6 NM settings from interfaces."""
|
|
return {
|
|
'method': 'manual',
|
|
'address': str(iface.ip),
|
|
'prefix': iface.network.prefixlen,
|
|
'gateway': '',
|
|
'dns': '',
|
|
'second_dns': '',
|
|
}
|
|
|
|
def get_settings(self) -> dict[str, dict]:
|
|
"""Return NM settings dict from cleaned data."""
|
|
allowed_ips: list[str] = []
|
|
ipv4_interface: IPv4Interface | None = None
|
|
ipv6_interface: IPv6Interface | None = None
|
|
for ip in self.cleaned_data['ip_addresses'].split(','):
|
|
ip = ip_interface(ip.strip())
|
|
allowed_ips.append(str(ip))
|
|
if ip.ip.version == 4:
|
|
ipv4_interface = ip
|
|
else:
|
|
ipv6_interface = ip
|
|
|
|
if self.cleaned_data['default_route']:
|
|
allowed_ips = ['0.0.0.0/0', '::/0']
|
|
|
|
settings = {
|
|
'common': {
|
|
'type': 'wireguard',
|
|
'zone': 'external',
|
|
},
|
|
'wireguard': {
|
|
'peer_endpoint': self.cleaned_data['peer_endpoint'],
|
|
'peer_public_key': self.cleaned_data['peer_public_key'],
|
|
'private_key': self.cleaned_data['private_key'],
|
|
'preshared_key': self.cleaned_data['preshared_key'],
|
|
'allowed_ips': allowed_ips,
|
|
}
|
|
}
|
|
|
|
if ipv4_interface:
|
|
settings['ipv4'] = self._build_ipv4_settings(ipv4_interface)
|
|
|
|
if ipv6_interface:
|
|
settings['ipv6'] = self._build_ipv6_settings(ipv6_interface)
|
|
|
|
return settings
|