linuxrouter/docker/routlin-dash/app/action_apply_mdns.py

45 lines
1.5 KiB
Python
Raw Normal View History

2026-05-25 16:07:21 -04:00
import copy
2026-05-17 03:26:01 -04:00
from flask import Blueprint, request, redirect, flash
from auth import require_level
2026-05-25 19:59:42 -04:00
from config_utils import load_config, save_config_with_snapshot, verify_config_hash
2026-05-17 03:26:01 -04:00
import sanitize
2026-05-20 17:10:18 -04:00
import validation as validate
2026-05-17 03:26:01 -04:00
bp = Blueprint('action_apply_mdns', __name__)
@bp.route('/action/apply_mdns', methods=['POST'])
@require_level('administrator')
def apply_mdns():
2026-05-25 16:07:21 -04:00
mdns_enabled = 'mdns_enabled' in request.form
2026-05-17 03:26:01 -04:00
2026-05-25 19:59:42 -04:00
if not verify_config_hash(request.form.get('config_hash', '')):
2026-05-17 03:26:01 -04:00
flash('Configuration was modified by another session. Please refresh and try again.', 'error')
return redirect('/view/view_mdns')
2026-05-25 19:59:42 -04:00
cfg = load_config()
2026-05-25 16:07:21 -04:00
mdns_reflect_vlans = sanitize.filterlist(
request.form.getlist('mdns_reflect_vlans'),
2026-05-25 19:59:42 -04:00
{v.get('name') for v in cfg.get('vlans', [])},
2026-05-25 16:07:21 -04:00
)
2026-05-25 19:59:42 -04:00
before = copy.deepcopy(cfg.get('mdns_reflection', {}))
cfg.setdefault('mdns_reflection', {}).update({
2026-05-17 03:26:01 -04:00
'enabled': mdns_enabled,
'reflect_vlans': mdns_reflect_vlans,
})
2026-05-25 19:59:42 -04:00
errors = validate.validate_config(cfg)
2026-05-20 17:10:18 -04:00
if errors:
for msg in errors:
flash(msg, 'error')
return redirect('/view/view_mdns')
2026-05-17 03:26:01 -04:00
2026-05-25 19:59:42 -04:00
flash(save_config_with_snapshot(
cfg,
2026-05-25 16:07:21 -04:00
path='mdns_reflection', key='global', operation='edit',
2026-05-25 19:59:42 -04:00
before=before or None, after=copy.deepcopy(cfg['mdns_reflection']),
2026-05-25 16:07:21 -04:00
description='Updated mDNS reflection settings',
), 'success')
2026-05-17 03:26:01 -04:00
return redirect('/view/view_mdns')