Add export_configs management command for WireGuard and Caddy configurations

This commit is contained in:
Eduardo Silva
2026-03-24 16:00:58 -03:00
parent d14615a73e
commit 09bb86d31a
5 changed files with 82 additions and 8 deletions

View File

@@ -134,6 +134,9 @@ bind-interfaces
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'
if dns_list.list_format == 'hosts':
dnsmasq_config += f'addn-hosts={file_path}\n'
elif dns_list.list_format == 'dnsmasq':
dnsmasq_config += f'conf-file={file_path}\n'
return dnsmasq_config

View File

@@ -0,0 +1,20 @@
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('dns', '0004_dnsfilterlist_recommended'),
]
operations = [
migrations.AddField(
model_name='dnsfilterlist',
name='list_format',
field=models.CharField(
choices=[('', 'Unknown'), ('hosts', 'Hosts'), ('dnsmasq', 'Dnsmasq'), ('unsupported', 'Unsupported')],
default='',
max_length=15,
),
),
]

View File

@@ -1,6 +1,7 @@
import uuid
from django.db import models
from django.utils.translation import gettext_lazy as _
class DNSSettings(models.Model):
@@ -27,6 +28,13 @@ class StaticHost(models.Model):
class DNSFilterList(models.Model):
LIST_FORMAT_CHOICES = [
('', _('Unknown')),
('hosts', 'Hosts'),
('dnsmasq', 'Dnsmasq'),
('unsupported', _('Unsupported')),
]
name = models.SlugField(max_length=100, unique=True)
description = models.CharField(max_length=100)
enabled = models.BooleanField(default=False)
@@ -34,6 +42,7 @@ class DNSFilterList(models.Model):
last_updated = models.DateTimeField(blank=True, null=True)
host_count = models.IntegerField(default=0)
recommended = models.BooleanField(default=False)
list_format = models.CharField(max_length=15, choices=LIST_FORMAT_CHOICES, default='')
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)

View File

@@ -1,5 +1,6 @@
import hashlib
import os
import re
import requests
from django.conf import settings
@@ -17,6 +18,20 @@ from .models import DNSFilterList, DNSSettings
from .models import StaticHost
def detect_list_format(content):
for line in content.splitlines():
line = line.strip()
if not line or line.startswith('#'):
continue
if line.startswith(('local=/', 'address=/', 'server=/', 'conf-file=')):
return 'dnsmasq'
parts = line.split()
if len(parts) >= 2 and re.match(r'^[\d\.]+$|^[0-9a-fA-F:]+$', parts[0]):
return 'hosts'
return 'unsupported'
return ''
def export_dns_configuration():
dnsmasq_config = generate_dnsmasq_config()
with open(settings.DNS_CONFIG_FILE, 'w') as f:
@@ -172,7 +187,9 @@ def view_manage_filter_list(request):
form = DNSFilterListForm(request.POST or None, instance=filter_list)
if form.is_valid():
form.save()
saved = form.save(commit=False)
saved.list_format = ''
saved.save()
dns_settings.pending_changes = True
dns_settings.save()
messages.success(request, _('DNS Filter List saved successfully'))
@@ -244,6 +261,17 @@ def view_update_dns_list(request):
dns_settings.pending_changes = True
dns_settings.save()
# Detect list format from content.
detected_format = detect_list_format(content)
dns_list.list_format = detected_format
# If unsupported format, disable the list.
if detected_format == 'unsupported' and dns_list.enabled:
dns_list.enabled = False
dns_settings.pending_changes = True
dns_settings.save()
messages.warning(request, _('DNS Filter List disabled | Unsupported format detected'))
# Count the number of valid host entries (ignoring empty lines and lines starting with '#').
host_count = sum(1 for line in content.splitlines() if line.strip() and not line.strip().startswith('#'))
dns_list.host_count = host_count
@@ -268,7 +296,11 @@ def view_toggle_dns_list(request):
file_path = os.path.join("/etc/dnsmasq/", f"{dns_list.uuid}.conf")
if request.GET.get('action') == 'enable':
if dns_list.host_count > 0 and os.path.exists(file_path):
if dns_list.list_format == 'unsupported':
messages.error(request, _('DNS Filter List not enabled | Unsupported format'))
elif dns_list.list_format == '':
messages.error(request, _('DNS Filter List not enabled | List has not been downloaded yet'))
elif dns_list.host_count > 0 and os.path.exists(file_path):
dns_list.enabled = True
dns_list.save()
export_dns_configuration()