Files
wireguard_webadmin/dns/functions.py

127 lines
4.0 KiB
Python
Raw Normal View History

2025-03-01 21:42:03 -03:00
import os
import tarfile
2025-03-01 21:42:03 -03:00
from cluster.models import ClusterSettings
2025-03-01 21:42:03 -03:00
from .models import DNSSettings, StaticHost, DNSFilterList
2024-04-26 16:58:01 -03:00
def compress_dnsmasq_config():
output_file = "/etc/dnsmasq/dnsmasq_config.tar.gz"
base_dir = "/etc/dnsmasq"
if not ClusterSettings.objects.filter(enabled=True, name='cluster_settings').exists():
if os.path.exists(output_file):
os.remove(output_file)
return None
if not os.path.isdir(base_dir):
if os.path.exists(output_file):
os.remove(output_file)
return None
conf_files = [
fn for fn in os.listdir(base_dir)
if fn.endswith(".conf") and os.path.isfile(os.path.join(base_dir, fn))
]
# If tar exists and is newer (or equal) than all .conf, do not recompile
if os.path.exists(output_file):
tar_mtime = os.path.getmtime(output_file)
newest_conf_mtime = max(
os.path.getmtime(os.path.join(base_dir, fn)) for fn in conf_files
)
if newest_conf_mtime <= tar_mtime:
return output_file
# Create tar.gz
tmp_output = output_file + ".tmp"
with tarfile.open(tmp_output, "w:gz") as tar:
for fn in conf_files:
fullpath = os.path.join(base_dir, fn)
tar.add(fullpath, arcname=fn)
os.replace(tmp_output, output_file)
return output_file
2024-04-26 16:58:01 -03:00
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
'''
unbound_config += forward_zone
2024-04-29 15:26:30 -03:00
if static_hosts:
unbound_config += '\nlocal-zone: "." transparent\n'
for static_host in static_hosts:
unbound_config += f' local-data: "{static_host.hostname}. IN A {static_host.ip_address}"\n'
2024-04-26 16:58:01 -03:00
return unbound_config
2024-04-29 15:26:30 -03:00
def generate_dnsdist_config():
dns_settings = DNSSettings.objects.get(name='dns_settings')
static_hosts = StaticHost.objects.all()
dnsdist_config = "setLocal('0.0.0.0:53')\n"
dnsdist_config += "setACL('0.0.0.0/0')\n"
if dns_settings.dns_primary:
dnsdist_config += f"newServer({{address='{dns_settings.dns_primary}', pool='upstreams'}})\n"
if dns_settings.dns_secondary:
dnsdist_config += f"newServer({{address='{dns_settings.dns_secondary}', pool='upstreams'}})\n"
if static_hosts:
dnsdist_config += "addAction(makeRule(''), PoolAction('staticHosts'))\n"
for static_host in static_hosts:
dnsdist_config += f"addLocal('{static_host.hostname}', '{static_host.ip_address}')\n"
return dnsdist_config
def generate_dnsmasq_config():
dns_settings = DNSSettings.objects.get(name='dns_settings')
static_hosts = StaticHost.objects.all()
2025-03-01 21:42:03 -03:00
dns_lists = DNSFilterList.objects.filter(enabled=True)
2024-04-29 15:26:30 -03:00
dnsmasq_config = f'''
no-dhcp-interface=
listen-address=0.0.0.0
bind-interfaces
'''
if dns_settings.dns_primary:
dnsmasq_config += f'server={dns_settings.dns_primary}\n'
if dns_settings.dns_secondary:
dnsmasq_config += f'server={dns_settings.dns_secondary}\n'
if static_hosts:
dnsmasq_config += '\n'
for static_host in static_hosts:
dnsmasq_config += f'address=/{static_host.hostname}/{static_host.ip_address}\n'
2025-03-01 21:42:03 -03:00
if dns_lists:
dnsmasq_config += '\n'
for dns_list in dns_lists:
file_path = os.path.join("/etc/dnsmasq/", f"{dns_list.uuid}.conf")
dnsmasq_config += f'addn-hosts={file_path}\n'
2024-04-29 15:26:30 -03:00
return dnsmasq_config