93 lines
3.4 KiB
Python
93 lines
3.4 KiB
Python
from pathlib import Path
|
|
import copy
|
|
from flask import Blueprint, request, redirect, flash
|
|
from auth import require_level
|
|
from config_utils import load_config, save_config_with_snapshot, verify_config_hash
|
|
import sanitize
|
|
import validation as validate
|
|
|
|
_PAGE = Path(__file__).parent.name
|
|
|
|
bp = Blueprint(_PAGE, __name__)
|
|
|
|
@bp.route('/action/dnsserver/upstreamdns_save', methods=['POST'])
|
|
@require_level('administrator')
|
|
def upstreamdns_save():
|
|
strict_order = 'strict_order' in request.form
|
|
submitted = request.form.getlist('upstream_servers')
|
|
|
|
for s in submitted:
|
|
if not s.strip():
|
|
flash('Remove blank server entries before saving.', 'error')
|
|
return redirect(f'/{_PAGE}')
|
|
|
|
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')
|
|
return redirect(f'/{_PAGE}')
|
|
upstream_servers.append(clean)
|
|
|
|
if not 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 = load_config()
|
|
before = copy.deepcopy(cfg.get('upstream_dns', {}))
|
|
current = cfg.get('upstream_dns', {})
|
|
if (strict_order == bool(current.get('strict_order', False)) and
|
|
upstream_servers == current.get('upstream_servers', [])):
|
|
flash('No changes detected.', 'info')
|
|
return redirect(f'/{_PAGE}')
|
|
|
|
cfg.setdefault('upstream_dns', {}).update({
|
|
'strict_order': strict_order,
|
|
'upstream_servers': upstream_servers,
|
|
})
|
|
errors = validate.validate_config(cfg)
|
|
if errors:
|
|
for msg in errors:
|
|
flash(msg, 'error')
|
|
return redirect(f'/{_PAGE}')
|
|
flash(save_config_with_snapshot(
|
|
cfg, path='upstream_dns', key='global', operation='edit',
|
|
before=before, after=copy.deepcopy(cfg['upstream_dns']),
|
|
description='Updated upstream DNS servers',
|
|
cmd='core apply',
|
|
), 'success')
|
|
return redirect(f'/{_PAGE}')
|
|
|
|
|
|
@bp.route('/action/dnsserver/dnsforwarding_save', methods=['POST'])
|
|
@require_level('administrator')
|
|
def dnsforwarding_save():
|
|
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')
|
|
return redirect(f'/{_PAGE}')
|
|
|
|
if not 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 = load_config()
|
|
before = copy.deepcopy(cfg.get('upstream_dns', {}))
|
|
current = cfg.get('upstream_dns', {})
|
|
if cache_size == int(current.get('cache_size', 0)):
|
|
flash('No changes detected.', 'info')
|
|
return redirect(f'/{_PAGE}')
|
|
|
|
cfg.setdefault('upstream_dns', {})['cache_size'] = cache_size
|
|
errors = validate.validate_config(cfg)
|
|
if errors:
|
|
for msg in errors:
|
|
flash(msg, 'error')
|
|
return redirect(f'/{_PAGE}')
|
|
flash(save_config_with_snapshot(
|
|
cfg, path='upstream_dns', key='global', operation='edit',
|
|
before=before, after=copy.deepcopy(cfg['upstream_dns']),
|
|
description='Updated DNS cache size',
|
|
cmd='core apply',
|
|
), 'success')
|
|
return redirect(f'/{_PAGE}')
|