mirror of
https://github.com/eduardogsilva/wireguard_webadmin.git
synced 2026-02-19 19:26:17 +00:00
Enhance hostname validation and update help text in forms.py
This commit is contained in:
32
dns/forms.py
32
dns/forms.py
@@ -63,7 +63,7 @@ class StaticHostForm(forms.ModelForm):
|
|||||||
self.helper = FormHelper()
|
self.helper = FormHelper()
|
||||||
self.helper.form_method = 'post'
|
self.helper.form_method = 'post'
|
||||||
self.fields['hostname'].label = _('Hostname')
|
self.fields['hostname'].label = _('Hostname')
|
||||||
self.fields['hostname'].help_text = _('Hostname or wildcard domain (e.g. *.example.com)')
|
self.fields['hostname'].help_text = _('Exact hostname or domain rule (e.g. *.example.com matches example.com and all subdomains)')
|
||||||
self.fields['ip_address'].label = _('IP Address')
|
self.fields['ip_address'].label = _('IP Address')
|
||||||
back_label = _('Back')
|
back_label = _('Back')
|
||||||
delete_label = _('Delete')
|
delete_label = _('Delete')
|
||||||
@@ -77,7 +77,7 @@ class StaticHostForm(forms.ModelForm):
|
|||||||
Div(
|
Div(
|
||||||
Field('hostname', css_class='form-control'),
|
Field('hostname', css_class='form-control'),
|
||||||
Field('ip_address', css_class='form-control'),
|
Field('ip_address', css_class='form-control'),
|
||||||
css_class='col-md-6'
|
css_class='col-md-12'
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
FormActions(
|
FormActions(
|
||||||
@@ -90,12 +90,28 @@ class StaticHostForm(forms.ModelForm):
|
|||||||
def clean(self):
|
def clean(self):
|
||||||
cleaned_data = super().clean()
|
cleaned_data = super().clean()
|
||||||
hostname = cleaned_data.get('hostname')
|
hostname = cleaned_data.get('hostname')
|
||||||
if hostname:
|
if not hostname:
|
||||||
# Allow plain hostname (e.g. example.com) or wildcard (e.g. *.example.com)
|
raise ValidationError(_('Invalid hostname.'))
|
||||||
plain_regex = r'^[a-zA-Z0-9]([a-zA-Z0-9-\.]*[a-zA-Z0-9])?$'
|
|
||||||
wildcard_regex = r'^\*\.([a-zA-Z0-9]([a-zA-Z0-9-\.]*[a-zA-Z0-9])?)$'
|
hostname = hostname.strip().lower()
|
||||||
if not (re.match(plain_regex, hostname) or re.match(wildcard_regex, hostname)):
|
if '://' in hostname or '/' in hostname or ':' in hostname:
|
||||||
raise ValidationError(_('Invalid hostname. Use a hostname (e.g. example.com) or wildcard (e.g. *.example.com).'))
|
raise ValidationError(_('Invalid hostname.'))
|
||||||
|
|
||||||
|
domain = hostname[2:] if hostname.startswith('*.') else hostname
|
||||||
|
labels = domain.split('.')
|
||||||
|
|
||||||
|
if len(labels) < 2:
|
||||||
|
raise ValidationError(_('Invalid hostname.'))
|
||||||
|
|
||||||
|
for label in labels:
|
||||||
|
if not label:
|
||||||
|
raise ValidationError(_('Invalid hostname.'))
|
||||||
|
if not re.match(r'^[a-z0-9-]+$', label):
|
||||||
|
raise ValidationError(_('Invalid hostname.'))
|
||||||
|
if label.startswith('-') or label.endswith('-'):
|
||||||
|
raise ValidationError(_('Invalid hostname.'))
|
||||||
|
|
||||||
|
cleaned_data['hostname'] = hostname
|
||||||
return cleaned_data
|
return cleaned_data
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -124,12 +124,11 @@ bind-interfaces
|
|||||||
if static_hosts:
|
if static_hosts:
|
||||||
dnsmasq_config += '\n'
|
dnsmasq_config += '\n'
|
||||||
for static_host in static_hosts:
|
for static_host in static_hosts:
|
||||||
# dnsmasq uses /.example.com/ for wildcards (matches *.example.com and example.com)
|
hostname = static_host.hostname.strip().lower()
|
||||||
if static_host.hostname.startswith('*.'):
|
if static_host.hostname.startswith('*.'):
|
||||||
dnsmasq_domain = '.' + static_host.hostname[2:]
|
dnsmasq_config += f'address=/{hostname[1:]}/{static_host.ip_address}\n'
|
||||||
else:
|
else:
|
||||||
dnsmasq_domain = static_host.hostname
|
dnsmasq_config += f'host-record={hostname},{static_host.ip_address}\n'
|
||||||
dnsmasq_config += f'address=/{dnsmasq_domain}/{static_host.ip_address}\n'
|
|
||||||
|
|
||||||
if dns_lists:
|
if dns_lists:
|
||||||
dnsmasq_config += '\n'
|
dnsmasq_config += '\n'
|
||||||
|
|||||||
Reference in New Issue
Block a user