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

239 lines
7.7 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-20 17:10:18 -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-30 14:57:33 -04:00
from config_utils import load_config, record_group, diff_fields, 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
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
def _flat_index_to_vlan_res(vlans, flat_idx):
pos = 0
for vi, vlan in enumerate(vlans):
for ri in range(len(vlan.get('reservations', []))):
if pos == flat_idx:
return vi, ri
pos += 1
return None, None
def _parse_ip():
raw = request.form.get('ip', '').strip()
if not raw:
2026-05-25 22:12:12 -04:00
return 'dynamic'
2026-05-17 03:26:01 -04:00
ip = validate.ip(raw)
if not ip:
flash(f'The configuration has not been saved because "{raw}" is not a valid IP address.', 'error')
return None
return ip
2026-05-20 17:10:18 -04:00
def _check_ip_conflicts(ip, vlan):
2026-05-25 22:12:12 -04:00
if ip == 'dynamic':
return None
2026-05-25 16:07:21 -04:00
dhcp = vlan.get('dhcp_information', {})
2026-05-20 17:10:18 -04:00
pool_start = dhcp.get('dynamic_pool_start')
pool_end = dhcp.get('dynamic_pool_end')
if pool_start and pool_end:
try:
if (ipaddress.IPv4Address(pool_start) <= ipaddress.IPv4Address(ip)
<= ipaddress.IPv4Address(pool_end)):
2026-05-21 03:45:14 -04:00
return f'{ip} falls within the dynamic pool range ({pool_start}-{pool_end}).'
2026-05-20 17:10:18 -04:00
except Exception:
pass
identity_ips = {s['ip'] for s in vlan.get('server_identities', []) if s.get('ip')}
if ip in identity_ips:
return f'{ip} is already assigned as a server identity IP.'
return None
2026-05-27 22:04:04 -04:00
@bp.route('/action/dhcp/addreservation_add', methods=['POST'])
2026-05-17 03:26:01 -04:00
@require_level('administrator')
2026-05-27 22:04:04 -04:00
def addreservation_add():
2026-05-17 03:26:01 -04:00
vlan_name = sanitize.name(request.form.get('vlan_name', ''))
description = sanitize.text(request.form.get('description', ''))
2026-05-20 17:10:18 -04:00
hostname = validate.domainname(request.form.get('hostname', ''))
2026-05-17 03:26:01 -04:00
mac = sanitize.mac(request.form.get('mac', ''))
ip = _parse_ip()
radius_client = 'radius_client' in request.form
if ip is None:
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
if not vlan_name:
flash('The configuration has not been saved because a VLAN is required.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
if not mac:
flash('The configuration has not been saved because a MAC address is 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()
vlans = cfg.get('vlans', [])
2026-05-17 03:26:01 -04:00
vlan = next((v for v in vlans if v.get('name') == vlan_name), None)
if vlan is None:
flash(f'The configuration has not been saved because VLAN "{vlan_name}" was 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-20 17:10:18 -04:00
conflict = _check_ip_conflicts(ip, vlan)
if conflict:
flash(f'The configuration has not been saved because {conflict}', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-20 17:10:18 -04:00
2026-05-25 16:07:21 -04:00
entry = {
2026-05-17 03:26:01 -04:00
'description': description,
'hostname': hostname,
'mac': mac,
'ip': ip,
'radius_client': radius_client,
'enabled': True,
2026-05-25 16:07:21 -04:00
}
vlan.setdefault('reservations', []).append(entry)
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-30 14:57:33 -04:00
changes = diff_fields(None, entry)
flash(record_group(cfg, f'vlans[name={vlan_name}].reservations', 'mac', mac, changes, 'core apply'), '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/dhcp/reservations_toggle', methods=['POST'])
2026-05-17 03:26:01 -04:00
@require_level('administrator')
2026-05-27 22:04:04 -04:00
def reservations_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()
vlans = cfg.get('vlans', [])
2026-05-17 03:26:01 -04:00
vi, ri = _flat_index_to_vlan_res(vlans, idx)
if vi is None:
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
res = vlans[vi]['reservations'][ri]
old_enabled = res.get('enabled', True)
2026-05-30 14:57:33 -04:00
before = copy.deepcopy(res)
2026-05-25 16:07:21 -04:00
res['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
vlan_name = vlans[vi]['name']
2026-05-30 14:57:33 -04:00
changes = diff_fields(before, res)
flash(record_group(cfg, f'vlans[name={vlan_name}].reservations', 'mac', res['mac'], changes, 'core apply'), '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/dhcp/reservations_edit', methods=['POST'])
2026-05-17 03:26:01 -04:00
@require_level('administrator')
2026-05-27 22:04:04 -04:00
def reservations_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
hostname = validate.domainname(request.form.get('hostname', ''))
2026-05-17 03:26:01 -04:00
mac = sanitize.mac(request.form.get('mac', ''))
ip = _parse_ip()
radius_client = 'radius_client' in request.form
if ip is None:
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
if not mac:
flash('The configuration has not been saved because a MAC address is 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()
vlans = cfg.get('vlans', [])
2026-05-17 03:26:01 -04:00
vi, ri = _flat_index_to_vlan_res(vlans, idx)
if vi is None:
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-20 17:10:18 -04:00
conflict = _check_ip_conflicts(ip, vlans[vi])
if conflict:
flash(f'The configuration has not been saved because {conflict}', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-20 17:10:18 -04:00
2026-05-25 16:07:21 -04:00
res = vlans[vi]['reservations'][ri]
before = copy.deepcopy(res)
2026-05-17 03:26:01 -04:00
res.update({
'description': description,
'hostname': hostname,
'mac': mac,
'ip': ip,
'radius_client': radius_client,
2026-05-17 03:37:26 -04:00
'enabled': 'enabled' in request.form,
2026-05-17 03:26:01 -04:00
})
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
vlan_name = vlans[vi]['name']
2026-05-30 14:57:33 -04:00
changes = diff_fields(before, res)
flash(record_group(cfg, f'vlans[name={vlan_name}].reservations', 'mac', mac, changes, 'core apply'), '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/dhcp/reservations_delete', methods=['POST'])
2026-05-17 03:26:01 -04:00
@require_level('administrator')
2026-05-27 22:04:04 -04:00
def reservations_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()
vlans = cfg.get('vlans', [])
2026-05-17 03:26:01 -04:00
vi, ri = _flat_index_to_vlan_res(vlans, idx)
if vi is None:
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
vlan_name = vlans[vi]['name']
removed = vlans[vi]['reservations'].pop(ri)
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-30 14:57:33 -04:00
changes = diff_fields(removed, None)
flash(record_group(cfg, f'vlans[name={vlan_name}].reservations', 'mac', removed['mac'], changes, 'core apply'), 'success')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')