mirror of
https://github.com/eduardogsilva/wireguard_webadmin.git
synced 2026-11-17 02:23:15 +01:00
Add DNS search domains option to WireGuard instances
This commit is contained in:
+42
-1
@@ -1,4 +1,5 @@
|
|||||||
import ipaddress
|
import ipaddress
|
||||||
|
import re
|
||||||
|
|
||||||
from crispy_forms.helper import FormHelper
|
from crispy_forms.helper import FormHelper
|
||||||
from crispy_forms.layout import Column, HTML, Layout, Row, Submit
|
from crispy_forms.layout import Column, HTML, Layout, Row, Submit
|
||||||
@@ -23,13 +24,17 @@ class WireGuardInstanceForm(forms.ModelForm):
|
|||||||
peer_list_refresh_interval = forms.IntegerField(label=_('Web Refresh Interval'), initial=10)
|
peer_list_refresh_interval = forms.IntegerField(label=_('Web Refresh Interval'), initial=10)
|
||||||
dns_primary = forms.GenericIPAddressField(label=_('Primary DNS'), initial='1.1.1.1', required=False)
|
dns_primary = forms.GenericIPAddressField(label=_('Primary DNS'), initial='1.1.1.1', required=False)
|
||||||
dns_secondary = forms.GenericIPAddressField(label=_('Secondary DNS'), initial='', required=False)
|
dns_secondary = forms.GenericIPAddressField(label=_('Secondary DNS'), initial='', required=False)
|
||||||
|
dns_search_domains = forms.CharField(
|
||||||
|
label=_('DNS Search Domains'), required=False, max_length=255,
|
||||||
|
help_text=_('Optional. One or more domains separated by commas (e.g. vpn, example.com).')
|
||||||
|
)
|
||||||
enforce_route_policy = forms.BooleanField(label=_('Enforce Route Policy'), required=False)
|
enforce_route_policy = forms.BooleanField(label=_('Enforce Route Policy'), required=False)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = WireGuardInstance
|
model = WireGuardInstance
|
||||||
fields = [
|
fields = [
|
||||||
'name', 'instance_id', 'private_key', 'public_key','hostname', 'listen_port', 'address',
|
'name', 'instance_id', 'private_key', 'public_key','hostname', 'listen_port', 'address',
|
||||||
'netmask', 'peer_list_refresh_interval', 'dns_primary', 'dns_secondary', 'enforce_route_policy'
|
'netmask', 'peer_list_refresh_interval', 'dns_primary', 'dns_secondary', 'dns_search_domains', 'enforce_route_policy'
|
||||||
]
|
]
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
@@ -87,6 +92,10 @@ class WireGuardInstanceForm(forms.ModelForm):
|
|||||||
Column('dns_secondary', css_class='form-group col-md-6 mb-0'),
|
Column('dns_secondary', css_class='form-group col-md-6 mb-0'),
|
||||||
css_class='form-row'
|
css_class='form-row'
|
||||||
),
|
),
|
||||||
|
Row(
|
||||||
|
Column('dns_search_domains', css_class='form-group col-md-12 mb-0'),
|
||||||
|
css_class='form-row'
|
||||||
|
),
|
||||||
Row(
|
Row(
|
||||||
Column('enforce_route_policy', css_class='form-group col-md-12 mb-0'),
|
Column('enforce_route_policy', css_class='form-group col-md-12 mb-0'),
|
||||||
css_class='form-row'
|
css_class='form-row'
|
||||||
@@ -106,11 +115,43 @@ class WireGuardInstanceForm(forms.ModelForm):
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def clean_dns_search_domains(self):
|
||||||
|
dns_search_domains = self.cleaned_data.get('dns_search_domains')
|
||||||
|
if not dns_search_domains:
|
||||||
|
return ''
|
||||||
|
|
||||||
|
domain_list = []
|
||||||
|
for domain in dns_search_domains.split(','):
|
||||||
|
domain = domain.strip().lower().rstrip('.')
|
||||||
|
if not domain:
|
||||||
|
continue
|
||||||
|
|
||||||
|
try:
|
||||||
|
ipaddress.ip_address(domain)
|
||||||
|
is_ip_address = True
|
||||||
|
except ValueError:
|
||||||
|
is_ip_address = False
|
||||||
|
|
||||||
|
valid_labels = all(re.match(r'^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$', label) for label in domain.split('.'))
|
||||||
|
if is_ip_address or not valid_labels:
|
||||||
|
raise forms.ValidationError(_('Invalid search domain: %(domain)s') % {'domain': domain})
|
||||||
|
|
||||||
|
if domain not in domain_list:
|
||||||
|
domain_list.append(domain)
|
||||||
|
|
||||||
|
return ', '.join(domain_list)
|
||||||
|
|
||||||
def clean(self):
|
def clean(self):
|
||||||
cleaned_data = super().clean()
|
cleaned_data = super().clean()
|
||||||
hostname = cleaned_data.get('hostname')
|
hostname = cleaned_data.get('hostname')
|
||||||
address = cleaned_data.get('address')
|
address = cleaned_data.get('address')
|
||||||
netmask = cleaned_data.get('netmask')
|
netmask = cleaned_data.get('netmask')
|
||||||
|
dns_primary = cleaned_data.get('dns_primary')
|
||||||
|
dns_secondary = cleaned_data.get('dns_secondary')
|
||||||
|
dns_search_domains = cleaned_data.get('dns_search_domains')
|
||||||
|
|
||||||
|
if dns_search_domains and not dns_primary and not dns_secondary:
|
||||||
|
raise forms.ValidationError(_('DNS Search Domains requires at least one DNS server.'))
|
||||||
|
|
||||||
peer_list_refresh_interval = cleaned_data.get('peer_list_refresh_interval')
|
peer_list_refresh_interval = cleaned_data.get('peer_list_refresh_interval')
|
||||||
if peer_list_refresh_interval and peer_list_refresh_interval < 5:
|
if peer_list_refresh_interval and peer_list_refresh_interval < 5:
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
# Generated by Django 5.2.19 on 2026-11-16 19:28
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('wireguard', '0032_remove_peer_enabled_by_schedule'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='wireguardinstance',
|
||||||
|
name='dns_search_domains',
|
||||||
|
field=models.CharField(blank=True, max_length=255, null=True),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -64,6 +64,7 @@ class WireGuardInstance(models.Model):
|
|||||||
peer_list_refresh_interval = models.IntegerField(default=10)
|
peer_list_refresh_interval = models.IntegerField(default=10)
|
||||||
dns_primary = models.GenericIPAddressField(unique=False, protocol='IPv4', default='1.1.1.1', blank=True, null=True)
|
dns_primary = models.GenericIPAddressField(unique=False, protocol='IPv4', default='1.1.1.1', blank=True, null=True)
|
||||||
dns_secondary = models.GenericIPAddressField(unique=False, protocol='IPv4', default='1.0.0.1', blank=True, null=True)
|
dns_secondary = models.GenericIPAddressField(unique=False, protocol='IPv4', default='1.0.0.1', blank=True, null=True)
|
||||||
|
dns_search_domains = models.CharField(max_length=255, blank=True, null=True)
|
||||||
pending_changes = models.BooleanField(default=True)
|
pending_changes = models.BooleanField(default=True)
|
||||||
legacy_firewall = models.BooleanField(default=False)
|
legacy_firewall = models.BooleanField(default=False)
|
||||||
enforce_route_policy = models.BooleanField(default=False)
|
enforce_route_policy = models.BooleanField(default=False)
|
||||||
|
|||||||
@@ -43,6 +43,8 @@ def generate_peer_config(peer_uuid, server_address=None):
|
|||||||
|
|
||||||
dns_entries = [wg_instance.dns_primary, wg_instance.dns_secondary]
|
dns_entries = [wg_instance.dns_primary, wg_instance.dns_secondary]
|
||||||
dns_line = ", ".join(filter(None, dns_entries))
|
dns_line = ", ".join(filter(None, dns_entries))
|
||||||
|
if dns_line and wg_instance.dns_search_domains:
|
||||||
|
dns_line += f", {wg_instance.dns_search_domains}"
|
||||||
|
|
||||||
if server_address:
|
if server_address:
|
||||||
endpoint = server_address
|
endpoint = server_address
|
||||||
|
|||||||
Reference in New Issue
Block a user