Development
This commit is contained in:
parent
d8d1d46fd2
commit
eed1d295dc
69 changed files with 3355 additions and 3230 deletions
360
docker/routlin-dash/app/pages/networklayout/action.py
Normal file
360
docker/routlin-dash/app/pages/networklayout/action.py
Normal file
|
|
@ -0,0 +1,360 @@
|
|||
import copy
|
||||
import ipaddress
|
||||
|
||||
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
|
||||
|
||||
bp = Blueprint('networklayout', __name__)
|
||||
|
||||
VIEW = '/view/view_networklayout'
|
||||
|
||||
_VLAN_FIELDS = ['name', 'vlan_id', 'is_vpn', 'subnet', 'subnet_mask', 'dnsmasq_log_queries',
|
||||
'radius_default', 'mdns_reflection', 'use_blocklists']
|
||||
|
||||
|
||||
def _row_index():
|
||||
try:
|
||||
return int(request.form.get('row_index', ''))
|
||||
except (ValueError, TypeError):
|
||||
return None
|
||||
|
||||
|
||||
def _hash_ok():
|
||||
if not verify_config_hash(request.form.get('config_hash', '')):
|
||||
flash('Configuration was modified by another session. Please refresh and try again.', 'error')
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
@bp.route('/action/networklayout_cardaddvlan_addvlan', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
def networklayout_cardaddvlan_addvlan():
|
||||
name = sanitize.name(request.form.get('name', ''))
|
||||
vlan_id = sanitize.vlan_id(request.form.get('vlan_id', ''))
|
||||
is_vpn = 'is_vpn' in request.form
|
||||
subnet = sanitize.ip(request.form.get('subnet', ''))
|
||||
subnet_mask = sanitize.subnet_mask(request.form.get('subnet_mask', ''))
|
||||
radius_default = 'radius_default' in request.form
|
||||
mdns_reflection = 'mdns_reflection' in request.form
|
||||
dnsmasq_log_queries = 'dnsmasq_log_queries' in request.form
|
||||
use_blocklists = sanitize.filterlist(
|
||||
request.form.getlist('use_blocklists'),
|
||||
{b.get('name') for b in load_config().get('dns_blocking', {}).get('blocklists', [])},
|
||||
)
|
||||
|
||||
if not name:
|
||||
flash('Name is required.', 'error')
|
||||
return redirect(VIEW)
|
||||
if vlan_id is None:
|
||||
flash('VLAN ID must be an integer between 1 and 4094.', 'error')
|
||||
return redirect(VIEW)
|
||||
if not subnet:
|
||||
flash('Subnet IP is required.', 'error')
|
||||
return redirect(VIEW)
|
||||
if subnet_mask is None:
|
||||
flash('Invalid subnet prefix (must be 1-30).', 'error')
|
||||
return redirect(VIEW)
|
||||
|
||||
if not _hash_ok():
|
||||
return redirect(VIEW)
|
||||
|
||||
cfg = load_config()
|
||||
vlans = cfg.setdefault('vlans', [])
|
||||
|
||||
if any(v.get('vlan_id') == vlan_id for v in vlans):
|
||||
flash(f'VLAN ID {vlan_id} is already in use.', 'error')
|
||||
return redirect(VIEW)
|
||||
if radius_default and any(v.get('radius_default') for v in vlans):
|
||||
flash('Only one VLAN can be the RADIUS default.', 'error')
|
||||
return redirect(VIEW)
|
||||
|
||||
entry = {
|
||||
'name': name,
|
||||
'vlan_id': vlan_id,
|
||||
'is_vpn': is_vpn,
|
||||
'subnet': subnet,
|
||||
'subnet_mask': subnet_mask,
|
||||
'dnsmasq_log_queries': dnsmasq_log_queries,
|
||||
'use_blocklists': use_blocklists,
|
||||
'radius_default': radius_default,
|
||||
'mdns_reflection': mdns_reflection,
|
||||
}
|
||||
if is_vpn:
|
||||
entry['peers'] = []
|
||||
else:
|
||||
entry['reservations'] = []
|
||||
vlans.append(entry)
|
||||
errors = validate.validate_config(cfg)
|
||||
if errors:
|
||||
for msg in errors:
|
||||
flash(msg, 'error')
|
||||
return redirect(VIEW)
|
||||
|
||||
flash(save_config_with_snapshot(
|
||||
cfg,
|
||||
path='vlans', key=name, operation='add',
|
||||
before=None, after={k: entry[k] for k in _VLAN_FIELDS if k in entry},
|
||||
description=f'Added VLAN: {name} ({subnet}/{subnet_mask})',
|
||||
), 'success')
|
||||
return redirect(VIEW)
|
||||
|
||||
|
||||
@bp.route('/action/networklayout_tablevlans_edit', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
def networklayout_tablevlans_edit():
|
||||
idx = _row_index()
|
||||
if idx is None:
|
||||
flash('Invalid request.', 'error')
|
||||
return redirect(VIEW)
|
||||
|
||||
name = sanitize.name(request.form.get('name', ''))
|
||||
vlan_id = sanitize.vlan_id(request.form.get('vlan_id', ''))
|
||||
subnet = sanitize.ip(request.form.get('subnet', ''))
|
||||
radius_default = 'radius_default' in request.form
|
||||
mdns_reflection = 'mdns_reflection' in request.form
|
||||
dnsmasq_log_queries = 'dnsmasq_log_queries' in request.form
|
||||
use_blocklists = sanitize.filterlist(
|
||||
request.form.getlist('use_blocklists'),
|
||||
{b.get('name') for b in load_config().get('dns_blocking', {}).get('blocklists', [])},
|
||||
)
|
||||
identity_ips_raw = [line.strip() for line in request.form.get('server_identity_ips', '').splitlines() if line.strip()]
|
||||
identity_ips = []
|
||||
for raw_ip in identity_ips_raw:
|
||||
clean = sanitize.ip(raw_ip)
|
||||
if not clean:
|
||||
flash(f"'{raw_ip}' is not a valid IP address.", 'error')
|
||||
return redirect(VIEW)
|
||||
identity_ips.append(clean)
|
||||
identity_descs = request.form.get('server_identity_descriptions', '').splitlines()
|
||||
identity_hostnames = request.form.get('server_identity_hostnames', '').splitlines()
|
||||
|
||||
subnet_mask_raw = request.form.get('subnet_mask')
|
||||
if subnet_mask_raw is not None:
|
||||
subnet_mask = sanitize.subnet_mask(subnet_mask_raw)
|
||||
if subnet_mask is None:
|
||||
flash('Invalid subnet prefix (must be 1-30).', 'error')
|
||||
return redirect(VIEW)
|
||||
else:
|
||||
subnet_mask = None
|
||||
|
||||
if not name:
|
||||
flash('Name is required.', 'error')
|
||||
return redirect(VIEW)
|
||||
if vlan_id is None:
|
||||
flash('VLAN ID must be an integer between 1 and 4094.', 'error')
|
||||
return redirect(VIEW)
|
||||
if not subnet:
|
||||
flash('Subnet IP is required.', 'error')
|
||||
return redirect(VIEW)
|
||||
if not _hash_ok():
|
||||
return redirect(VIEW)
|
||||
|
||||
cfg = load_config()
|
||||
vlans = cfg.get('vlans', [])
|
||||
if idx < 0 or idx >= len(vlans):
|
||||
flash('VLAN not found.', 'error')
|
||||
return redirect(VIEW)
|
||||
|
||||
existing = vlans[idx]
|
||||
is_vpn = existing.get('is_vpn', False)
|
||||
final_mask = subnet_mask if subnet_mask is not None else existing.get('subnet_mask', 24)
|
||||
|
||||
if identity_ips:
|
||||
_vlan_net = ipaddress.IPv4Network(f'{subnet}/{final_mask}', strict=False)
|
||||
for _ip in identity_ips:
|
||||
if ipaddress.IPv4Address(_ip) not in _vlan_net:
|
||||
flash(f"Server identity IP '{_ip}' is not in the VLAN subnet ({subnet}/{final_mask}).", 'error')
|
||||
return redirect(VIEW)
|
||||
|
||||
current_id = existing.get('vlan_id')
|
||||
if current_id == 1 and vlan_id != 1:
|
||||
flash('VLAN 1 is the physical interface and cannot change its ID.', 'error')
|
||||
return redirect(VIEW)
|
||||
|
||||
if vlan_id != current_id and any(v.get('vlan_id') == vlan_id for i, v in enumerate(vlans) if i != idx):
|
||||
flash(f'VLAN ID {vlan_id} is already in use.', 'error')
|
||||
return redirect(VIEW)
|
||||
|
||||
if radius_default and any(i != idx and v.get('radius_default') for i, v in enumerate(vlans)):
|
||||
flash('Only one VLAN can be the RADIUS default.', 'error')
|
||||
return redirect(VIEW)
|
||||
|
||||
old_identities = existing.get('server_identities', [])
|
||||
new_identities = []
|
||||
for i, ip in enumerate(identity_ips):
|
||||
entry = dict(old_identities[i]) if i < len(old_identities) else {}
|
||||
entry['ip'] = ip
|
||||
desc = identity_descs[i].strip() if i < len(identity_descs) else ''
|
||||
if desc:
|
||||
entry['description'] = desc
|
||||
else:
|
||||
entry.pop('description', None)
|
||||
hostname_raw = identity_hostnames[i].strip() if i < len(identity_hostnames) else ''
|
||||
if hostname_raw:
|
||||
clean_hostname = sanitize.hostname(hostname_raw)
|
||||
if clean_hostname is None:
|
||||
flash(f"'{hostname_raw}' is not a valid hostname.", 'error')
|
||||
return redirect(VIEW)
|
||||
entry['hostname'] = clean_hostname
|
||||
else:
|
||||
entry.pop('hostname', None)
|
||||
new_identities.append(entry)
|
||||
|
||||
gateway_raw = sanitize.ip(request.form.get('gateway', ''))
|
||||
if gateway_raw and gateway_raw not in identity_ips:
|
||||
flash(f"Gateway '{gateway_raw}' must match one of the server identity IPs.", 'error')
|
||||
return redirect(VIEW)
|
||||
inferred_gw = (min(identity_ips, key=lambda ip: int(ip.split('.')[-1]))
|
||||
if identity_ips else '')
|
||||
new_stored_gw = gateway_raw if (gateway_raw and gateway_raw != inferred_gw) else ''
|
||||
existing_gw = existing.get('dhcp_information', {}).get('explicit_overrides', {}).get('gateway', '')
|
||||
|
||||
dns_override = 'dns_server_override' in request.form
|
||||
dns_ips = []
|
||||
for _line in request.form.get('dns_server', '').splitlines():
|
||||
_line = _line.strip()
|
||||
if not _line:
|
||||
continue
|
||||
_clean = sanitize.ip(_line)
|
||||
if not _clean:
|
||||
flash(f"'{_line}' is not a valid DNS server IP.", 'error')
|
||||
return redirect(VIEW)
|
||||
dns_ips.append(_clean)
|
||||
if dns_override and not dns_ips:
|
||||
flash('At least one DNS server IP is required when override is enabled.', 'error')
|
||||
return redirect(VIEW)
|
||||
if dns_override and dns_ips:
|
||||
_vlan_net = ipaddress.IPv4Network(f'{subnet}/{final_mask}', strict=False)
|
||||
for _ip in dns_ips:
|
||||
if ipaddress.IPv4Address(_ip) not in _vlan_net:
|
||||
flash(f"DNS server '{_ip}' is not in the VLAN subnet ({subnet}/{final_mask}).", 'error')
|
||||
return redirect(VIEW)
|
||||
new_stored_dns = dns_ips if dns_override else []
|
||||
_existing_dns = existing.get('dhcp_information', {}).get('explicit_overrides', {}).get('dns_server', [])
|
||||
existing_dns = _existing_dns if isinstance(_existing_dns, list) else ([_existing_dns] if _existing_dns else [])
|
||||
|
||||
ntp_override = 'ntp_server_override' in request.form
|
||||
ntp_ips = []
|
||||
for _line in request.form.get('ntp_server', '').splitlines():
|
||||
_line = _line.strip()
|
||||
if not _line:
|
||||
continue
|
||||
_clean = sanitize.ip(_line)
|
||||
if not _clean:
|
||||
flash(f"'{_line}' is not a valid NTP server IP.", 'error')
|
||||
return redirect(VIEW)
|
||||
ntp_ips.append(_clean)
|
||||
if ntp_override and not ntp_ips:
|
||||
flash('At least one NTP server IP is required when override is enabled.', 'error')
|
||||
return redirect(VIEW)
|
||||
if ntp_override and ntp_ips:
|
||||
_vlan_net = ipaddress.IPv4Network(f'{subnet}/{final_mask}', strict=False)
|
||||
for _ip in ntp_ips:
|
||||
if ipaddress.IPv4Address(_ip) not in _vlan_net:
|
||||
flash(f"NTP server '{_ip}' is not in the VLAN subnet ({subnet}/{final_mask}).", 'error')
|
||||
return redirect(VIEW)
|
||||
new_stored_ntp = ntp_ips if ntp_override else []
|
||||
_existing_ntp = existing.get('dhcp_information', {}).get('explicit_overrides', {}).get('ntp_server', [])
|
||||
existing_ntp = _existing_ntp if isinstance(_existing_ntp, list) else ([_existing_ntp] if _existing_ntp else [])
|
||||
|
||||
_ids_unchanged = (
|
||||
len(new_identities) == len(old_identities) and
|
||||
all(
|
||||
n.get('ip') == o.get('ip') and
|
||||
n.get('description', '') == o.get('description', '') and
|
||||
n.get('hostname', '') == o.get('hostname', '')
|
||||
for n, o in zip(new_identities, old_identities)
|
||||
)
|
||||
)
|
||||
if (name == existing.get('name', '')
|
||||
and vlan_id == existing.get('vlan_id')
|
||||
and subnet == existing.get('subnet', '')
|
||||
and final_mask == existing.get('subnet_mask', 24)
|
||||
and dnsmasq_log_queries == bool(existing.get('dnsmasq_log_queries', False))
|
||||
and radius_default == bool(existing.get('radius_default', False))
|
||||
and mdns_reflection == bool(existing.get('mdns_reflection', False))
|
||||
and sorted(use_blocklists) == sorted(existing.get('use_blocklists', []))
|
||||
and _ids_unchanged
|
||||
and new_stored_gw == existing_gw
|
||||
and new_stored_dns == existing_dns
|
||||
and new_stored_ntp == existing_ntp):
|
||||
flash('No changes were made.', 'info')
|
||||
return redirect(VIEW)
|
||||
|
||||
before = {k: existing.get(k) for k in _VLAN_FIELDS}
|
||||
existing.update({
|
||||
'name': name,
|
||||
'vlan_id': vlan_id,
|
||||
'is_vpn': is_vpn,
|
||||
'subnet': subnet,
|
||||
'subnet_mask': final_mask,
|
||||
'dnsmasq_log_queries': dnsmasq_log_queries,
|
||||
'radius_default': radius_default,
|
||||
'mdns_reflection': mdns_reflection,
|
||||
'use_blocklists': use_blocklists,
|
||||
'server_identities': new_identities,
|
||||
})
|
||||
dhcp_overrides = existing.setdefault('dhcp_information', {}).setdefault('explicit_overrides', {})
|
||||
if new_stored_gw:
|
||||
dhcp_overrides['gateway'] = new_stored_gw
|
||||
else:
|
||||
dhcp_overrides.pop('gateway', None)
|
||||
if new_stored_dns:
|
||||
dhcp_overrides['dns_server'] = new_stored_dns
|
||||
else:
|
||||
dhcp_overrides.pop('dns_server', None)
|
||||
if new_stored_ntp:
|
||||
dhcp_overrides['ntp_server'] = new_stored_ntp
|
||||
else:
|
||||
dhcp_overrides.pop('ntp_server', None)
|
||||
if not dhcp_overrides:
|
||||
existing.get('dhcp_information', {}).pop('explicit_overrides', None)
|
||||
errors = validate.validate_config(cfg)
|
||||
if errors:
|
||||
for msg in errors:
|
||||
flash(msg, 'error')
|
||||
return redirect(VIEW)
|
||||
|
||||
flash(save_config_with_snapshot(
|
||||
cfg,
|
||||
path='vlans', key=name, operation='edit',
|
||||
before=before, after={k: existing.get(k) for k in _VLAN_FIELDS},
|
||||
description=f'Edited VLAN: {name}',
|
||||
), 'success')
|
||||
return redirect(VIEW)
|
||||
|
||||
|
||||
@bp.route('/action/networklayout_tablevlans_delete', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
def networklayout_tablevlans_delete():
|
||||
idx = _row_index()
|
||||
if idx is None:
|
||||
flash('Invalid request.', 'error')
|
||||
return redirect(VIEW)
|
||||
if not _hash_ok():
|
||||
return redirect(VIEW)
|
||||
|
||||
cfg = load_config()
|
||||
vlans = cfg.get('vlans', [])
|
||||
if idx < 0 or idx >= len(vlans):
|
||||
flash('VLAN not found.', 'error')
|
||||
return redirect(VIEW)
|
||||
|
||||
removed = vlans.pop(idx)
|
||||
errors = validate.validate_config(cfg)
|
||||
if errors:
|
||||
for msg in errors:
|
||||
flash(msg, 'error')
|
||||
return redirect(VIEW)
|
||||
|
||||
flash(save_config_with_snapshot(
|
||||
cfg,
|
||||
path='vlans', key=removed['name'], operation='delete',
|
||||
before={k: removed.get(k) for k in _VLAN_FIELDS},
|
||||
after=None,
|
||||
description=f'Deleted VLAN: {removed["name"]}',
|
||||
), 'success')
|
||||
return redirect(VIEW)
|
||||
Loading…
Add table
Add a link
Reference in a new issue