mirror of
https://github.com/eduardogsilva/wireguard_webadmin.git
synced 2025-08-26 21:31:14 +00:00
add DNS Filter List management functionality
This commit is contained in:
25
dns/admin.py
25
dns/admin.py
@@ -1,3 +1,26 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
from .models import DNSFilterList, DNSSettings, StaticHost
|
||||
|
||||
|
||||
class DNSFilterListAdmin(admin.ModelAdmin):
|
||||
list_display = ('name', 'description', 'enabled', 'list_url', 'host_count', 'created', 'updated')
|
||||
list_filter = ('enabled', 'created', 'updated')
|
||||
search_fields = ('name', 'description', 'list_url')
|
||||
ordering = ('name', 'created')
|
||||
admin.site.register(DNSFilterList, DNSFilterListAdmin)
|
||||
|
||||
|
||||
class DNSSettingsAdmin(admin.ModelAdmin):
|
||||
list_display = ('dns_primary', 'dns_secondary', 'pending_changes', 'created', 'updated')
|
||||
list_filter = ('pending_changes', 'created', 'updated')
|
||||
search_fields = ('dns_primary', 'dns_secondary')
|
||||
ordering = ('created', 'updated')
|
||||
admin.site.register(DNSSettings, DNSSettingsAdmin)
|
||||
|
||||
|
||||
class StaticHostAdmin(admin.ModelAdmin):
|
||||
list_display = ('hostname', 'ip_address', 'created', 'updated')
|
||||
search_fields = ('hostname', 'ip_address')
|
||||
ordering = ('hostname', 'created')
|
||||
admin.site.register(StaticHost, StaticHostAdmin)
|
55
dns/forms.py
55
dns/forms.py
@@ -1,15 +1,14 @@
|
||||
import requests
|
||||
from .models import DNSSettings, StaticHost
|
||||
|
||||
from django import forms
|
||||
from crispy_forms.helper import FormHelper
|
||||
from crispy_forms.layout import Layout, Fieldset, ButtonHolder, Submit, Div, Field, HTML
|
||||
from crispy_forms.bootstrap import FormActions, StrictButton
|
||||
from django.core.exceptions import ValidationError
|
||||
from datetime import datetime
|
||||
from django.core.exceptions import ValidationError
|
||||
import re
|
||||
|
||||
from crispy_forms.bootstrap import FormActions
|
||||
from crispy_forms.helper import FormHelper
|
||||
from crispy_forms.layout import Layout, Fieldset, Div, Field, Submit, HTML
|
||||
from django import forms
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
from .models import DNSFilterList
|
||||
from .models import DNSSettings, StaticHost
|
||||
|
||||
|
||||
class DNSSettingsForm(forms.ModelForm):
|
||||
class Meta:
|
||||
@@ -89,3 +88,39 @@ class StaticHostForm(forms.ModelForm):
|
||||
if not re.match(regex, hostname):
|
||||
raise ValidationError('Invalid hostname')
|
||||
return
|
||||
|
||||
|
||||
class DNSFilterListForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = DNSFilterList
|
||||
# Only allow editable fields
|
||||
fields = ['name', 'description', 'list_url']
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(DNSFilterListForm, self).__init__(*args, **kwargs)
|
||||
self.helper = FormHelper()
|
||||
self.helper.form_method = 'post'
|
||||
# Add a delete button if editing an existing instance
|
||||
if self.instance.pk:
|
||||
delete_html = (
|
||||
"<a href='javascript:void(0)' class='btn btn-outline-danger' "
|
||||
"data-command='delete' onclick='openCommandDialog(this)'>Delete</a>"
|
||||
)
|
||||
else:
|
||||
delete_html = ''
|
||||
self.helper.layout = Layout(
|
||||
Fieldset(
|
||||
'DNS Filter List Details',
|
||||
Div(
|
||||
Div(Field('name', css_class='form-control'), css_class='col-md-12'),
|
||||
Div(Field('description', css_class='form-control'), css_class='col-md-12'),
|
||||
Div(Field('list_url', css_class='form-control'), css_class='col-md-12'),
|
||||
css_class='row'
|
||||
),
|
||||
),
|
||||
FormActions(
|
||||
Submit('save', 'Save', css_class='btn btn-primary'),
|
||||
HTML('<a class="btn btn-outline-primary" href="/dns/">Back</a>'),
|
||||
HTML(delete_html),
|
||||
)
|
||||
)
|
||||
|
29
dns/migrations/0002_dnsfilterlist.py
Normal file
29
dns/migrations/0002_dnsfilterlist.py
Normal file
@@ -0,0 +1,29 @@
|
||||
# Generated by Django 5.1.5 on 2025-03-01 21:05
|
||||
|
||||
import uuid
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('dns', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='DNSFilterList',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('name', models.SlugField(max_length=100, unique=True)),
|
||||
('description', models.CharField(max_length=100)),
|
||||
('enabled', models.BooleanField(default=False)),
|
||||
('list_url', models.URLField()),
|
||||
('last_updated', models.DateTimeField(blank=True, null=True)),
|
||||
('created', models.DateTimeField(auto_now_add=True)),
|
||||
('updated', models.DateTimeField(auto_now=True)),
|
||||
('uuid', models.UUIDField(default=uuid.uuid4, editable=False, unique=True)),
|
||||
],
|
||||
),
|
||||
]
|
18
dns/migrations/0003_dnsfilterlist_host_count.py
Normal file
18
dns/migrations/0003_dnsfilterlist_host_count.py
Normal file
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 5.1.5 on 2025-03-01 21:37
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('dns', '0002_dnsfilterlist'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='dnsfilterlist',
|
||||
name='host_count',
|
||||
field=models.IntegerField(default=0),
|
||||
),
|
||||
]
|
@@ -1,6 +1,7 @@
|
||||
from django.db import models
|
||||
import uuid
|
||||
|
||||
from django.db import models
|
||||
|
||||
|
||||
class DNSSettings(models.Model):
|
||||
name = models.CharField(default='dns_settings', max_length=100)
|
||||
@@ -23,3 +24,19 @@ class StaticHost(models.Model):
|
||||
|
||||
def __str__(self):
|
||||
return self.hostname
|
||||
|
||||
|
||||
class DNSFilterList(models.Model):
|
||||
name = models.SlugField(max_length=100, unique=True)
|
||||
description = models.CharField(max_length=100)
|
||||
enabled = models.BooleanField(default=False)
|
||||
list_url = models.URLField()
|
||||
last_updated = models.DateTimeField(blank=True, null=True)
|
||||
host_count = models.IntegerField(default=0)
|
||||
|
||||
created = models.DateTimeField(auto_now_add=True)
|
||||
updated = models.DateTimeField(auto_now=True)
|
||||
uuid = models.UUIDField(unique=True, default=uuid.uuid4, editable=False)
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
58
dns/views.py
58
dns/views.py
@@ -1,11 +1,14 @@
|
||||
from django.conf import settings
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.shortcuts import render, get_object_or_404, redirect
|
||||
from django.contrib import messages
|
||||
|
||||
from user_manager.models import UserAcl
|
||||
from .models import DNSSettings, StaticHost
|
||||
from .forms import DNSFilterListForm
|
||||
from .forms import StaticHostForm, DNSSettingsForm
|
||||
from .functions import generate_dnsmasq_config
|
||||
from django.conf import settings
|
||||
from .models import DNSSettings, DNSFilterList
|
||||
from .models import StaticHost
|
||||
|
||||
|
||||
def export_dns_configuration():
|
||||
@@ -29,11 +32,21 @@ def view_apply_dns_config(request):
|
||||
def view_static_host_list(request):
|
||||
dns_settings, _ = DNSSettings.objects.get_or_create(name='dns_settings')
|
||||
static_host_list = StaticHost.objects.all().order_by('hostname')
|
||||
filter_lists = DNSFilterList.objects.all().order_by('name')
|
||||
if not filter_lists:
|
||||
DNSFilterList.objects.create(
|
||||
name='stevenblack-hosts', list_url='https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts',
|
||||
description='adware and malware domains', enabled=False
|
||||
)
|
||||
filter_lists = DNSFilterList.objects.all().order_by('name')
|
||||
messages.success(request, 'Default DNS Filter List created successfully')
|
||||
|
||||
if dns_settings.pending_changes:
|
||||
messages.warning(request, 'Pending Changes|There are pending DNS changes that have not been applied')
|
||||
context = {
|
||||
'dns_settings': dns_settings,
|
||||
'static_host_list': static_host_list,
|
||||
'filter_lists': filter_lists
|
||||
}
|
||||
return render(request, 'dns/static_host_list.html', context=context)
|
||||
|
||||
@@ -101,3 +114,42 @@ def view_manage_static_host(request):
|
||||
'instance': static_dns,
|
||||
}
|
||||
return render(request, 'generic_form.html', context=context)
|
||||
|
||||
|
||||
@login_required
|
||||
def view_manage_filter_list(request):
|
||||
if not UserAcl.objects.filter(user=request.user, user_level__gte=50).exists():
|
||||
return render(request, 'access_denied.html', {'page_title': 'Access Denied'})
|
||||
|
||||
dns_settings, _ = DNSSettings.objects.get_or_create(name='dns_settings')
|
||||
|
||||
if request.GET.get('uuid'):
|
||||
filter_list = get_object_or_404(DNSFilterList, uuid=request.GET.get('uuid'))
|
||||
if request.GET.get('action') == 'delete':
|
||||
if request.GET.get('confirmation') == 'delete':
|
||||
if filter_list.enabled:
|
||||
messages.warning(request, 'DNS Filter List not deleted | Filter List is enabled')
|
||||
return redirect('/dns/')
|
||||
filter_list.delete()
|
||||
messages.success(request, 'DNS Filter List deleted successfully')
|
||||
return redirect('/dns/')
|
||||
else:
|
||||
messages.warning(request, 'DNS Filter List not deleted | Invalid confirmation')
|
||||
return redirect('/dns/')
|
||||
else:
|
||||
filter_list = None
|
||||
|
||||
form = DNSFilterListForm(request.POST or None, instance=filter_list)
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
dns_settings.pending_changes = True
|
||||
dns_settings.save()
|
||||
messages.success(request, 'DNS Filter List saved successfully')
|
||||
return redirect('/dns/')
|
||||
|
||||
context = {
|
||||
'dns_settings': dns_settings,
|
||||
'form': form,
|
||||
'instance': filter_list,
|
||||
}
|
||||
return render(request, 'generic_form.html', context=context)
|
Reference in New Issue
Block a user