linuxrouter/docker/routlin-dash/app/pages/dnsserver/action.py

107 lines
4 KiB
Python
Raw Normal View History

2026-05-27 22:04:04 -04:00
from pathlib import Path
2026-05-25 20:46:17 -04:00
import copy
2026-05-23 02:01:37 -04:00
from flask import Blueprint, request, redirect, flash
2026-06-07 00:21:08 -04:00
import auth
import config_utils
2026-05-23 02:01:37 -04:00
import sanitize
2026-06-05 01:48:27 -04:00
import mod_validation as validate
2026-05-23 02:01:37 -04:00
2026-05-27 22:04:04 -04:00
_PAGE = Path(__file__).parent.name
2026-05-23 02:01:37 -04:00
2026-05-27 22:04:04 -04:00
bp = Blueprint(_PAGE, __name__)
2026-05-23 02:01:37 -04:00
2026-05-27 22:04:04 -04:00
@bp.route('/action/dnsserver/upstreamdns_save', methods=['POST'])
2026-06-07 00:21:08 -04:00
@auth.require_level('administrator')
2026-05-27 22:04:04 -04:00
def upstreamdns_save():
2026-05-25 02:22:21 -04:00
strict_order = 'strict_order' in request.form
submitted = request.form.getlist('upstream_servers')
2026-05-23 02:01:37 -04:00
for s in submitted:
if not s.strip():
flash('Remove blank server entries before saving.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-23 02:01:37 -04:00
upstream_servers = []
for s in submitted:
clean = sanitize.ip(s.strip())
if not clean:
flash(f"'{s.strip()}' is not a valid IP address.", 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-23 02:01:37 -04:00
upstream_servers.append(clean)
2026-06-07 00:21:08 -04:00
if not config_utils.verify_config_hash(request.form.get('config_hash', '')):
2026-05-23 02:01:37 -04:00
flash('Configuration was modified by another session. Please refresh and try again.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-23 02:01:37 -04:00
2026-06-07 00:21:08 -04:00
cfg = config_utils.load_config()
2026-05-25 20:46:17 -04:00
before = copy.deepcopy(cfg.get('upstream_dns', {}))
2026-05-25 19:59:42 -04:00
current = cfg.get('upstream_dns', {})
2026-05-25 02:22:21 -04:00
if (strict_order == bool(current.get('strict_order', False)) and
2026-05-23 02:01:37 -04:00
upstream_servers == current.get('upstream_servers', [])):
flash('No changes detected.', 'info')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-23 02:01:37 -04:00
2026-05-25 19:59:42 -04:00
cfg.setdefault('upstream_dns', {}).update({
2026-05-23 02:01:37 -04:00
'strict_order': strict_order,
'upstream_servers': upstream_servers,
})
2026-05-25 19:59:42 -04:00
errors = validate.validate_config(cfg)
2026-05-23 02:01:37 -04:00
if errors:
for msg in errors:
flash(msg, 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-06-07 00:21:08 -04:00
changes = config_utils.diff_fields(before, cfg['upstream_dns'])
flash(config_utils.record_group(cfg, 'upstream_dns', None, None, changes, 'core apply'), 'success')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-25 02:22:21 -04:00
2026-06-09 21:40:14 -04:00
VALID_PERIODS = {0, 1, 7, 30, 60, 90, 365}
@bp.route('/action/dnsserver/metrics_period_save', methods=['POST'])
@auth.require_level('administrator')
def metrics_period_save():
try:
period = int(request.form.get('metrics_period', '0'))
except ValueError:
period = 0
if period not in VALID_PERIODS:
flash('Invalid period value.', 'error')
return redirect(f'/{_PAGE}')
if not config_utils.verify_config_hash(request.form.get('config_hash', '')):
flash('Configuration was modified by another session. Please refresh and try again.', 'error')
return redirect(f'/{_PAGE}')
cfg = config_utils.load_config()
cfg.setdefault('upstream_dns', {})['metrics_period'] = period
config_utils.save_config(cfg)
return redirect(f'/{_PAGE}')
2026-05-27 22:04:04 -04:00
@bp.route('/action/dnsserver/dnsforwarding_save', methods=['POST'])
2026-06-07 00:21:08 -04:00
@auth.require_level('administrator')
2026-05-27 22:04:04 -04:00
def dnsforwarding_save():
2026-05-25 02:22:21 -04:00
cache_size = validate.int_range(request.form.get('cache_size', '').strip(), 0, None)
if cache_size is None:
flash('Cache Size must be a non-negative integer.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-25 02:22:21 -04:00
2026-06-07 00:21:08 -04:00
if not config_utils.verify_config_hash(request.form.get('config_hash', '')):
2026-05-25 02:22:21 -04:00
flash('Configuration was modified by another session. Please refresh and try again.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-25 02:22:21 -04:00
2026-06-07 00:21:08 -04:00
cfg = config_utils.load_config()
2026-05-25 20:46:17 -04:00
before = copy.deepcopy(cfg.get('upstream_dns', {}))
2026-05-25 19:59:42 -04:00
current = cfg.get('upstream_dns', {})
2026-05-25 02:22:21 -04:00
if cache_size == int(current.get('cache_size', 0)):
flash('No changes detected.', 'info')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-25 02:22:21 -04:00
2026-05-25 19:59:42 -04:00
cfg.setdefault('upstream_dns', {})['cache_size'] = cache_size
errors = validate.validate_config(cfg)
2026-05-25 02:22:21 -04:00
if errors:
for msg in errors:
flash(msg, 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-06-07 00:21:08 -04:00
changes = config_utils.diff_fields(before, cfg['upstream_dns'])
flash(config_utils.record_group(cfg, 'upstream_dns', None, None, changes, 'core apply'), 'success')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')