mirror of
https://github.com/eduardogsilva/wireguard_webadmin.git
synced 2026-04-04 06:26:20 +00:00
Add export_configs management command for WireGuard and Caddy configurations
This commit is contained in:
@@ -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")
|
||||
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
|
||||
|
||||
|
||||
20
dns/migrations/0005_dnsfilterlist_list_format.py
Normal file
20
dns/migrations/0005_dnsfilterlist_list_format.py
Normal 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,
|
||||
),
|
||||
),
|
||||
]
|
||||
@@ -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)
|
||||
|
||||
36
dns/views.py
36
dns/views.py
@@ -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()
|
||||
|
||||
@@ -67,11 +67,12 @@
|
||||
<th class="min-width"></th>
|
||||
<th>{% trans 'Name' %}</th>
|
||||
<th>{% trans 'Description' %}</th>
|
||||
<th>{% trans 'Format' %}</th>
|
||||
<th>{% trans 'Hosts' %}</th>
|
||||
<th>{% trans 'Last Update' %}</th>
|
||||
<th class="min-width">{% trans 'Status' %}</th>
|
||||
<th class="min-width">{% trans 'Update' %}</th>
|
||||
<th class="min-width">{% trans 'Edit' %}</th>
|
||||
<th class="min-width"></th>
|
||||
<th class="min-width"></th>
|
||||
<th class="min-width"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@@ -83,8 +84,17 @@
|
||||
{% endif %}</td>
|
||||
<td>{{ filter_list.name }}</td>
|
||||
<td>{{ filter_list.description }}</td>
|
||||
<td>
|
||||
{% if filter_list.list_format == 'unsupported' %}
|
||||
<span class="text-danger">{% trans 'Unsupported' %}</span>
|
||||
{% elif filter_list.list_format == '' %}
|
||||
<span class="text-muted">{% trans 'Unknown' %}</span>
|
||||
{% else %}
|
||||
{{ filter_list.list_format }}
|
||||
{% endif %}
|
||||
</td>
|
||||
<td>{{ filter_list.host_count }}</td>
|
||||
<td>{{ filter_list.last_updated|default_if_none:"" }}</td>
|
||||
<td>{{ filter_list.last_updated|date:"d/m/y H:i"|default_if_none:"" }}</td>
|
||||
<td>
|
||||
{% if filter_list.enabled %}
|
||||
<a class="text-green" href="/dns/toggle_dns_list/?uuid={{ filter_list.uuid }}&action=disable"><i class="fas fa-toggle-on"></i></a>
|
||||
@@ -103,7 +113,7 @@
|
||||
{% endfor %}
|
||||
{% else %}
|
||||
<tr>
|
||||
<td colspan="7"></td>
|
||||
<td colspan="8"></td>
|
||||
</tr>
|
||||
{% endif %}
|
||||
</tbody>
|
||||
|
||||
Reference in New Issue
Block a user