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

196 lines
6 KiB
Python
Raw Normal View History

2026-05-27 22:04:04 -04:00
from pathlib import Path
2026-05-25 16:07:21 -04:00
import copy
2026-05-18 14:38:23 -04:00
import ipaddress
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
2026-05-27 22:04:04 -04:00
_PAGE = Path(__file__).parent.name
2026-05-17 03:26:01 -04:00
2026-05-27 22:04:04 -04:00
bp = Blueprint(_PAGE, __name__)
2026-05-17 03:26:01 -04:00
2026-05-25 19:59:42 -04:00
def _vlan_networks(cfg):
2026-05-18 14:38:23 -04:00
nets = []
2026-05-25 19:59:42 -04:00
for v in cfg.get('vlans', []):
2026-05-18 14:38:23 -04:00
subnet = v.get('subnet', '')
mask = v.get('subnet_mask', '')
if subnet and mask:
try:
2026-05-25 16:07:21 -04:00
nets.append(ipaddress.IPv4Network(f'{subnet}/{mask}', strict=False))
2026-05-18 14:38:23 -04:00
except ValueError:
pass
return nets
2026-05-25 19:59:42 -04:00
def _ip_in_vlan(ip_str, cfg):
2026-05-18 14:38:23 -04:00
try:
addr = ipaddress.IPv4Address(ip_str)
except ValueError:
return False
2026-05-25 19:59:42 -04:00
nets = _vlan_networks(cfg)
2026-05-18 14:38:23 -04:00
return not nets or any(addr in net for net in nets)
2026-05-17 03:26:01 -04:00
def _row_index():
try:
return int(request.form.get('row_index', ''))
except (ValueError, TypeError):
return None
def _hash_ok():
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 False
return True
2026-05-27 22:04:04 -04:00
@bp.route('/action/hostoverrides/addoverride_add', methods=['POST'])
2026-05-17 03:26:01 -04:00
@require_level('administrator')
2026-05-27 22:04:04 -04:00
def addoverride_add():
2026-05-17 03:26:01 -04:00
description = sanitize.text(request.form.get('description', ''))
2026-05-20 17:10:18 -04:00
host = validate.domainname(request.form.get('host', ''))
2026-05-17 03:26:01 -04:00
ip = sanitize.ip(request.form.get('ip', ''))
if not host or not ip:
flash('Hostname and IP address are required.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
if not _hash_ok():
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
2026-05-25 19:59:42 -04:00
cfg = load_config()
if not _ip_in_vlan(ip, cfg):
2026-05-18 14:38:23 -04:00
flash('IP address does not fall within any configured VLAN subnet.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-18 14:38:23 -04:00
2026-05-25 16:07:21 -04:00
entry = {'description': description, 'host': host, 'ip': ip, 'enabled': True}
2026-05-25 19:59:42 -04:00
cfg.setdefault('host_overrides', []).append(entry)
errors = validate.validate_config(cfg)
2026-05-20 17:10:18 -04:00
if errors:
for msg in errors:
flash(msg, 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
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='host_overrides', key=host, operation='add',
before=None, after=entry,
description=f'Added host override: {host}{ip}',
), 'success')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
2026-05-27 22:04:04 -04:00
@bp.route('/action/hostoverrides/table_toggle', methods=['POST'])
2026-05-17 03:26:01 -04:00
@require_level('administrator')
2026-05-27 22:04:04 -04:00
def table_toggle():
2026-05-17 03:26:01 -04:00
idx = _row_index()
if idx is None:
flash('Invalid request.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
if not _hash_ok():
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
2026-05-25 19:59:42 -04:00
cfg = load_config()
items = cfg.get('host_overrides', [])
2026-05-17 03:26:01 -04:00
if idx < 0 or idx >= len(items):
flash('Entry not found.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
2026-05-25 16:07:21 -04:00
old_enabled = items[idx].get('enabled', True)
items[idx]['enabled'] = not old_enabled
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')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
2026-05-25 16:07:21 -04:00
action = 'Enabled' if not old_enabled else 'Disabled'
2026-05-25 19:59:42 -04:00
flash(save_config_with_snapshot(
cfg,
2026-05-25 16:07:21 -04:00
path='host_overrides', key=items[idx]['host'], operation='toggle',
before={'enabled': old_enabled}, after={'enabled': not old_enabled},
description=f'{action} host override: {items[idx]["host"]}',
), 'success')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
2026-05-27 22:04:04 -04:00
@bp.route('/action/hostoverrides/table_edit', methods=['POST'])
2026-05-17 03:26:01 -04:00
@require_level('administrator')
2026-05-27 22:04:04 -04:00
def table_edit():
2026-05-17 03:26:01 -04:00
idx = _row_index()
if idx is None:
flash('Invalid request.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
description = sanitize.text(request.form.get('description', ''))
2026-05-20 17:10:18 -04:00
host = validate.domainname(request.form.get('host', ''))
2026-05-17 03:26:01 -04:00
ip = sanitize.ip(request.form.get('ip', ''))
2026-05-17 03:37:26 -04:00
enabled = request.form.get('enabled') == 'on'
2026-05-17 03:26:01 -04:00
if not host or not ip:
flash('Hostname and IP address are required.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
if not _hash_ok():
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
2026-05-25 19:59:42 -04:00
cfg = load_config()
if not _ip_in_vlan(ip, cfg):
2026-05-18 14:38:23 -04:00
flash('IP address does not fall within any configured VLAN subnet.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-18 14:38:23 -04:00
2026-05-25 19:59:42 -04:00
items = cfg.get('host_overrides', [])
2026-05-17 03:26:01 -04:00
if idx < 0 or idx >= len(items):
flash('Entry not found.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
2026-05-25 16:07:21 -04:00
before = copy.deepcopy(items[idx])
2026-05-17 03:37:26 -04:00
items[idx].update({'description': description, 'host': host, 'ip': ip, 'enabled': enabled})
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')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
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='host_overrides', key=host, operation='edit',
before=before, after=copy.deepcopy(items[idx]),
description=f'Edited host override: {host}{ip}',
), 'success')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
2026-05-27 22:04:04 -04:00
@bp.route('/action/hostoverrides/table_delete', methods=['POST'])
2026-05-17 03:26:01 -04:00
@require_level('administrator')
2026-05-27 22:04:04 -04:00
def table_delete():
2026-05-17 03:26:01 -04:00
idx = _row_index()
if idx is None:
flash('Invalid request.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
if not _hash_ok():
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
2026-05-25 19:59:42 -04:00
cfg = load_config()
items = cfg.get('host_overrides', [])
2026-05-17 03:26:01 -04:00
if idx < 0 or idx >= len(items):
flash('Entry not found.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
removed = items.pop(idx)
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')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
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='host_overrides', key=removed['host'], operation='delete',
before=removed, after=None,
description=f'Deleted host override: {removed["host"]}',
), 'success')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')