mirror of
https://github.com/eduardogsilva/wireguard_webadmin.git
synced 2025-08-05 18:55:05 +02:00
DNS apply configuration
This commit is contained in:
parent
414bc8076a
commit
da1513e560
4 changed files with 54 additions and 5 deletions
33
dns/functions.py
Normal file
33
dns/functions.py
Normal file
|
@ -0,0 +1,33 @@
|
|||
from .models import DNSSettings, StaticHost
|
||||
|
||||
|
||||
def generate_unbound_config():
|
||||
dns_settings = DNSSettings.objects.get(name='dns_settings')
|
||||
static_hosts = StaticHost.objects.all()
|
||||
if dns_settings.dns_primary:
|
||||
do_not_query_localhost = 'yes'
|
||||
forward_zone = f'\nforward-zone:\n name: "."\n forward-addr: {dns_settings.dns_primary}\n'
|
||||
if dns_settings.dns_secondary:
|
||||
forward_zone += f' forward-addr: {dns_settings.dns_secondary}\n'
|
||||
else:
|
||||
do_not_query_localhost = 'no'
|
||||
forward_zone = ''
|
||||
|
||||
|
||||
unbound_config = f'''
|
||||
server:
|
||||
interface: 0.0.0.0
|
||||
port: 53
|
||||
access-control: 0.0.0.0/0 allow
|
||||
do-ip4: yes
|
||||
do-ip6: no
|
||||
do-udp: yes
|
||||
local-zone: "local." static
|
||||
do-not-query-localhost: {do_not_query_localhost}
|
||||
verbosity: 1
|
||||
recursion: yes
|
||||
'''
|
||||
unbound_config += forward_zone
|
||||
for static_host in static_hosts:
|
||||
unbound_config += f'local-data: "{static_host.hostname}. IN A {static_host.ip_address}"\n'
|
||||
return unbound_config
|
19
dns/views.py
19
dns/views.py
|
@ -4,12 +4,28 @@ from django.contrib import messages
|
|||
from user_manager.models import UserAcl
|
||||
from .models import DNSSettings, StaticHost
|
||||
from .forms import StaticHostForm, DNSSettingsForm
|
||||
from .functions import generate_unbound_config
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
@login_required
|
||||
def view_apply_dns_config(request):
|
||||
dns_settings, _ = DNSSettings.objects.get_or_create(name='dns_settings')
|
||||
dns_settings.pending_changes = False
|
||||
dns_settings.save()
|
||||
unbound_config = generate_unbound_config()
|
||||
with open(settings.UNBOUND_CONFIG, 'w') as f:
|
||||
f.write(unbound_config)
|
||||
messages.success(request, 'DNS settings applied successfully')
|
||||
return redirect('/dns/')
|
||||
|
||||
|
||||
@login_required
|
||||
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')
|
||||
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,
|
||||
|
@ -25,8 +41,7 @@ def view_manage_dns_settings(request):
|
|||
form = DNSSettingsForm(request.POST or None, instance=dns_settings)
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
messages.success(request, 'DNS settings saved successfully')
|
||||
return redirect('/dns/')
|
||||
return redirect('/dns/apply_config/')
|
||||
|
||||
form_description_content = '''
|
||||
<strong>DNS Forwarders</strong>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue