Development
This commit is contained in:
parent
916d238602
commit
6c3abca58c
4 changed files with 208 additions and 303 deletions
|
|
@ -248,6 +248,25 @@ def passes(req, level):
|
||||||
|
|
||||||
# Snapshot helpers ====================================================
|
# Snapshot helpers ====================================================
|
||||||
|
|
||||||
|
def _flatten_json(val, prefix):
|
||||||
|
"""Recursively flatten a parsed JSON value into [(path, leaf_str)] pairs."""
|
||||||
|
if isinstance(val, dict):
|
||||||
|
out = []
|
||||||
|
for k, v in val.items():
|
||||||
|
out.extend(_flatten_json(v, f'{prefix}.{k}'))
|
||||||
|
return out
|
||||||
|
if isinstance(val, list):
|
||||||
|
out = []
|
||||||
|
for i, v in enumerate(val):
|
||||||
|
out.extend(_flatten_json(v, f'{prefix}[{i}]'))
|
||||||
|
return out
|
||||||
|
if val is None:
|
||||||
|
return [(prefix, None)]
|
||||||
|
if isinstance(val, bool):
|
||||||
|
return [(prefix, 'true' if val else 'false')]
|
||||||
|
return [(prefix, str(val))]
|
||||||
|
|
||||||
|
|
||||||
def build_snap_val(changes):
|
def build_snap_val(changes):
|
||||||
"""Return a brief summary of changed field names for the history table cell."""
|
"""Return a brief summary of changed field names for the history table cell."""
|
||||||
if not changes:
|
if not changes:
|
||||||
|
|
@ -264,11 +283,42 @@ def snap_expand_row(changes, colspan):
|
||||||
return ''
|
return ''
|
||||||
rows = ''
|
rows = ''
|
||||||
for c in changes:
|
for c in changes:
|
||||||
bval = c['before'] if c['before'] is not None else ''
|
field = c['field']
|
||||||
aval = c['after'] if c['after'] is not None else ''
|
before_text = c['before']
|
||||||
|
after_text = c['after']
|
||||||
|
vtype = c.get('value_type', 'str')
|
||||||
|
|
||||||
|
if vtype == 'json':
|
||||||
|
try:
|
||||||
|
bval = json.loads(before_text) if before_text is not None else None
|
||||||
|
aval = json.loads(after_text) if after_text is not None else None
|
||||||
|
if isinstance(bval, (dict, list)) or isinstance(aval, (dict, list)):
|
||||||
|
bflat = dict(_flatten_json(bval, field)) if isinstance(bval, (dict, list)) else {}
|
||||||
|
aflat = dict(_flatten_json(aval, field)) if isinstance(aval, (dict, list)) else {}
|
||||||
|
if bflat or aflat:
|
||||||
|
seen = set()
|
||||||
|
for k in list(aflat) + list(bflat):
|
||||||
|
if k in seen:
|
||||||
|
continue
|
||||||
|
seen.add(k)
|
||||||
|
bv = bflat.get(k)
|
||||||
|
av = aflat.get(k)
|
||||||
|
rows += (
|
||||||
|
'<tr>'
|
||||||
|
f'<td class="snap-expand-field">{e(k)}</td>'
|
||||||
|
f'<td class="snap-expand-val">{e(bv) if bv is not None else "<em>(none)</em>"}</td>'
|
||||||
|
f'<td class="snap-expand-val">{e(av) if av is not None else "<em>(none)</em>"}</td>'
|
||||||
|
'</tr>'
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
bval = before_text if before_text is not None else ''
|
||||||
|
aval = after_text if after_text is not None else ''
|
||||||
rows += (
|
rows += (
|
||||||
'<tr>'
|
'<tr>'
|
||||||
f'<td class="snap-expand-field">{e(c["field"])}</td>'
|
f'<td class="snap-expand-field">{e(field)}</td>'
|
||||||
f'<td class="snap-expand-val">{e(bval) if bval else "<em>(none)</em>"}</td>'
|
f'<td class="snap-expand-val">{e(bval) if bval else "<em>(none)</em>"}</td>'
|
||||||
f'<td class="snap-expand-val">{e(aval) if aval else "<em>(none)</em>"}</td>'
|
f'<td class="snap-expand-val">{e(aval) if aval else "<em>(none)</em>"}</td>'
|
||||||
'</tr>'
|
'</tr>'
|
||||||
|
|
|
||||||
|
|
@ -28,18 +28,25 @@ def _hash_ok():
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
@bp.route('/action/networklayout/addvlan_add', methods=['POST'])
|
@bp.route('/action/networklayout/vlans_addedit', methods=['POST'])
|
||||||
@require_level('administrator')
|
@require_level('administrator')
|
||||||
def addvlan_add():
|
def vlans_addedit():
|
||||||
name = sanitize.name(request.form.get('name', ''))
|
_ri = request.form.get('row_index', '').strip()
|
||||||
vlan_id = sanitize.vlan_id(request.form.get('vlan_id', ''))
|
try:
|
||||||
is_vpn = 'is_vpn' in request.form
|
edit_idx = int(_ri)
|
||||||
subnet = sanitize.ip(request.form.get('subnet', ''))
|
is_edit = True
|
||||||
subnet_mask = sanitize.subnet_mask(request.form.get('subnet_mask', ''))
|
except (ValueError, TypeError):
|
||||||
radius_default = 'radius_default' in request.form
|
edit_idx = None
|
||||||
mdns_reflection = 'mdns_reflection' in request.form
|
is_edit = False
|
||||||
|
|
||||||
|
name = sanitize.name(request.form.get('name', ''))
|
||||||
|
vlan_id = sanitize.vlan_id(request.form.get('vlan_id', ''))
|
||||||
|
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
|
dnsmasq_log_queries = 'dnsmasq_log_queries' in request.form
|
||||||
use_blocklists = sanitize.filterlist(
|
use_blocklists = sanitize.filterlist(
|
||||||
request.form.getlist('use_blocklists'),
|
request.form.getlist('use_blocklists'),
|
||||||
{b.get('name') for b in load_config().get('dns_blocking', {}).get('blocklists', [])},
|
{b.get('name') for b in load_config().get('dns_blocking', {}).get('blocklists', [])},
|
||||||
)
|
)
|
||||||
|
|
@ -56,9 +63,6 @@ def addvlan_add():
|
||||||
if subnet_mask is None:
|
if subnet_mask is None:
|
||||||
flash('Invalid subnet prefix (must be 1-30).', 'error')
|
flash('Invalid subnet prefix (must be 1-30).', 'error')
|
||||||
return redirect(f'/{_PAGE}')
|
return redirect(f'/{_PAGE}')
|
||||||
if is_vpn and mdns_reflection:
|
|
||||||
flash('mDNS reflection is not supported on VPN VLANs.', 'error')
|
|
||||||
return redirect(f'/{_PAGE}')
|
|
||||||
|
|
||||||
_vlan_net = ipaddress.IPv4Network(f'{subnet}/{subnet_mask}', strict=False)
|
_vlan_net = ipaddress.IPv4Network(f'{subnet}/{subnet_mask}', strict=False)
|
||||||
if ipaddress.IPv4Address(subnet) != _vlan_net.network_address:
|
if ipaddress.IPv4Address(subnet) != _vlan_net.network_address:
|
||||||
|
|
@ -79,29 +83,29 @@ def addvlan_add():
|
||||||
|
|
||||||
new_identities = []
|
new_identities = []
|
||||||
for raw in raw_identities:
|
for raw in raw_identities:
|
||||||
ip_clean = sanitize.ip(str(raw.get('ip', '')))
|
ip_clean = sanitize.ip(str(raw.get('ip', '')))
|
||||||
if not ip_clean:
|
if not ip_clean:
|
||||||
flash('Invalid IP address in identity.', 'error')
|
flash('Invalid IP address in identity.', 'error')
|
||||||
|
return redirect(f'/{_PAGE}')
|
||||||
|
_addr = ipaddress.IPv4Address(ip_clean)
|
||||||
|
if _addr not in _vlan_net:
|
||||||
|
flash(f"Identity IP '{ip_clean}' is not in the VLAN subnet ({subnet}/{subnet_mask}).", 'error')
|
||||||
|
return redirect(f'/{_PAGE}')
|
||||||
|
if _addr == _vlan_net.network_address or _addr == _vlan_net.broadcast_address:
|
||||||
|
flash(f"Identity IP '{ip_clean}' cannot be the network or broadcast address.", 'error')
|
||||||
|
return redirect(f'/{_PAGE}')
|
||||||
|
ident = {'ip': ip_clean}
|
||||||
|
desc = str(raw.get('description', '')).strip()
|
||||||
|
if desc:
|
||||||
|
ident['description'] = desc
|
||||||
|
hostname_raw = str(raw.get('hostname', '')).strip()
|
||||||
|
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(f'/{_PAGE}')
|
return redirect(f'/{_PAGE}')
|
||||||
_addr = ipaddress.IPv4Address(ip_clean)
|
ident['hostname'] = clean_hostname
|
||||||
if _addr not in _vlan_net:
|
new_identities.append(ident)
|
||||||
flash(f"Identity IP '{ip_clean}' is not in the VLAN subnet ({subnet}/{subnet_mask}).", 'error')
|
|
||||||
return redirect(f'/{_PAGE}')
|
|
||||||
if _addr == _vlan_net.network_address or _addr == _vlan_net.broadcast_address:
|
|
||||||
flash(f"Identity IP '{ip_clean}' cannot be the network or broadcast address.", 'error')
|
|
||||||
return redirect(f'/{_PAGE}')
|
|
||||||
ident = {'ip': ip_clean}
|
|
||||||
desc = str(raw.get('description', '')).strip()
|
|
||||||
if desc:
|
|
||||||
ident['description'] = desc
|
|
||||||
hostname_raw = str(raw.get('hostname', '')).strip()
|
|
||||||
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(f'/{_PAGE}')
|
|
||||||
ident['hostname'] = clean_hostname
|
|
||||||
new_identities.append(ident)
|
|
||||||
|
|
||||||
identity_ips = [ident['ip'] for ident in new_identities]
|
identity_ips = [ident['ip'] for ident in new_identities]
|
||||||
|
|
||||||
|
|
@ -109,7 +113,7 @@ def addvlan_add():
|
||||||
if gateway_raw and gateway_raw not in identity_ips:
|
if gateway_raw and gateway_raw not in identity_ips:
|
||||||
flash(f"Gateway '{gateway_raw}' must match one of the server identity IPs.", 'error')
|
flash(f"Gateway '{gateway_raw}' must match one of the server identity IPs.", 'error')
|
||||||
return redirect(f'/{_PAGE}')
|
return redirect(f'/{_PAGE}')
|
||||||
inferred_gw = (min(identity_ips, key=lambda ip: int(ip.split('.')[-1])) if identity_ips else '')
|
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 ''
|
new_stored_gw = gateway_raw if (gateway_raw and gateway_raw != inferred_gw) else ''
|
||||||
|
|
||||||
dns_override = 'dns_servers_override' in request.form
|
dns_override = 'dns_servers_override' in request.form
|
||||||
|
|
@ -149,7 +153,7 @@ def addvlan_add():
|
||||||
new_stored_ntp = ntp_ips if ntp_override else []
|
new_stored_ntp = ntp_ips if ntp_override else []
|
||||||
|
|
||||||
dhcp_domain_raw = request.form.get('dhcp_domain', '').strip()
|
dhcp_domain_raw = request.form.get('dhcp_domain', '').strip()
|
||||||
dhcp_domain = sanitize.hostname(dhcp_domain_raw) if dhcp_domain_raw else 'local'
|
dhcp_domain = sanitize.hostname(dhcp_domain_raw) if dhcp_domain_raw else 'local'
|
||||||
if dhcp_domain_raw and dhcp_domain is None:
|
if dhcp_domain_raw and dhcp_domain is None:
|
||||||
flash(f"'{dhcp_domain_raw}' is not a valid domain name.", 'error')
|
flash(f"'{dhcp_domain_raw}' is not a valid domain name.", 'error')
|
||||||
return redirect(f'/{_PAGE}')
|
return redirect(f'/{_PAGE}')
|
||||||
|
|
@ -184,16 +188,6 @@ def addvlan_add():
|
||||||
return redirect(f'/{_PAGE}')
|
return redirect(f'/{_PAGE}')
|
||||||
dhcp_lease_time = f'{_lt}{_unit_suffix}'
|
dhcp_lease_time = f'{_lt}{_unit_suffix}'
|
||||||
|
|
||||||
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(f'/{_PAGE}')
|
|
||||||
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(f'/{_PAGE}')
|
|
||||||
|
|
||||||
dhcp_info = {}
|
dhcp_info = {}
|
||||||
if dhcp_domain and dhcp_domain != 'local':
|
if dhcp_domain and dhcp_domain != 'local':
|
||||||
dhcp_info['domain'] = dhcp_domain
|
dhcp_info['domain'] = dhcp_domain
|
||||||
|
|
@ -213,262 +207,109 @@ def addvlan_add():
|
||||||
if dhcp_overrides:
|
if dhcp_overrides:
|
||||||
dhcp_info['explicit_overrides'] = dhcp_overrides
|
dhcp_info['explicit_overrides'] = dhcp_overrides
|
||||||
|
|
||||||
entry = {
|
cfg = load_config()
|
||||||
'name': name,
|
vlans = cfg.setdefault('vlans', [])
|
||||||
'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,
|
|
||||||
'server_identities': new_identities,
|
|
||||||
}
|
|
||||||
if dhcp_info:
|
|
||||||
entry['dhcp_information'] = dhcp_info
|
|
||||||
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(f'/{_PAGE}')
|
|
||||||
|
|
||||||
changes = diff_fields(None, entry)
|
if is_edit:
|
||||||
flash(record_group(cfg, 'vlans', 'name', name, changes, 'core apply'), 'success')
|
if edit_idx < 0 or edit_idx >= len(vlans):
|
||||||
return redirect(f'/{_PAGE}')
|
flash('VLAN not found.', 'error')
|
||||||
|
|
||||||
|
|
||||||
@bp.route('/action/networklayout/vlans_edit', methods=['POST'])
|
|
||||||
@require_level('administrator')
|
|
||||||
def vlans_edit():
|
|
||||||
idx = _row_index()
|
|
||||||
if idx is None:
|
|
||||||
flash('Invalid request.', 'error')
|
|
||||||
return redirect(f'/{_PAGE}')
|
|
||||||
|
|
||||||
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(f'/{_PAGE}')
|
return redirect(f'/{_PAGE}')
|
||||||
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')
|
existing = vlans[edit_idx]
|
||||||
if subnet_mask_raw is not None:
|
is_vpn = existing.get('is_vpn', False)
|
||||||
subnet_mask = sanitize.subnet_mask(subnet_mask_raw)
|
|
||||||
if subnet_mask is None:
|
if is_vpn and mdns_reflection:
|
||||||
flash('Invalid subnet prefix (must be 1-30).', 'error')
|
flash('mDNS reflection is not supported on VPN VLANs.', 'error')
|
||||||
return redirect(f'/{_PAGE}')
|
return redirect(f'/{_PAGE}')
|
||||||
else:
|
|
||||||
subnet_mask = None
|
|
||||||
|
|
||||||
if not name:
|
current_id = existing.get('vlan_id')
|
||||||
flash('Name is required.', 'error')
|
if current_id == 1 and vlan_id != 1:
|
||||||
return redirect(f'/{_PAGE}')
|
flash('VLAN 1 is the physical interface and cannot change its ID.', 'error')
|
||||||
if vlan_id is None:
|
return redirect(f'/{_PAGE}')
|
||||||
flash('VLAN ID must be an integer between 1 and 4094.', 'error')
|
|
||||||
return redirect(f'/{_PAGE}')
|
|
||||||
if not subnet:
|
|
||||||
flash('Subnet IP is required.', 'error')
|
|
||||||
return redirect(f'/{_PAGE}')
|
|
||||||
if not _hash_ok():
|
|
||||||
return redirect(f'/{_PAGE}')
|
|
||||||
|
|
||||||
cfg = load_config()
|
if vlan_id != current_id and any(
|
||||||
vlans = cfg.get('vlans', [])
|
v.get('vlan_id') == vlan_id for i, v in enumerate(vlans) if i != edit_idx
|
||||||
if idx < 0 or idx >= len(vlans):
|
):
|
||||||
flash('VLAN not found.', 'error')
|
flash(f'VLAN ID {vlan_id} is already in use.', 'error')
|
||||||
return redirect(f'/{_PAGE}')
|
return redirect(f'/{_PAGE}')
|
||||||
|
|
||||||
existing = vlans[idx]
|
if radius_default and any(i != edit_idx and v.get('radius_default') for i, v in enumerate(vlans)):
|
||||||
is_vpn = existing.get('is_vpn', False)
|
flash('Only one VLAN can be the RADIUS default.', 'error')
|
||||||
final_mask = subnet_mask if subnet_mask is not None else existing.get('subnet_mask', 24)
|
return redirect(f'/{_PAGE}')
|
||||||
|
|
||||||
if is_vpn and mdns_reflection:
|
before = copy.deepcopy(existing)
|
||||||
flash('mDNS reflection is not supported on VPN VLANs.', 'error')
|
existing.update({
|
||||||
return redirect(f'/{_PAGE}')
|
'name': name,
|
||||||
|
'vlan_id': vlan_id,
|
||||||
if identity_ips:
|
'subnet': subnet,
|
||||||
_vlan_net = ipaddress.IPv4Network(f'{subnet}/{final_mask}', strict=False)
|
'subnet_mask': subnet_mask,
|
||||||
for _ip in identity_ips:
|
'dnsmasq_log_queries': dnsmasq_log_queries,
|
||||||
_addr = ipaddress.IPv4Address(_ip)
|
'radius_default': radius_default,
|
||||||
if _addr not in _vlan_net:
|
'mdns_reflection': mdns_reflection,
|
||||||
flash(f"Server identity IP '{_ip}' is not in the VLAN subnet ({subnet}/{final_mask}).", 'error')
|
'use_blocklists': use_blocklists,
|
||||||
return redirect(f'/{_PAGE}')
|
'server_identities': new_identities,
|
||||||
if _addr == _vlan_net.network_address or _addr == _vlan_net.broadcast_address:
|
})
|
||||||
flash(f"Server identity IP '{_ip}' cannot be the network or broadcast address.", 'error')
|
if dhcp_info:
|
||||||
return redirect(f'/{_PAGE}')
|
existing['dhcp_information'] = dhcp_info
|
||||||
|
|
||||||
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(f'/{_PAGE}')
|
|
||||||
|
|
||||||
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(f'/{_PAGE}')
|
|
||||||
|
|
||||||
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(f'/{_PAGE}')
|
|
||||||
|
|
||||||
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:
|
else:
|
||||||
entry.pop('description', None)
|
existing.pop('dhcp_information', None)
|
||||||
hostname_raw = identity_hostnames[i].strip() if i < len(identity_hostnames) else ''
|
|
||||||
if hostname_raw:
|
errors = validate.validate_config(cfg)
|
||||||
clean_hostname = sanitize.hostname(hostname_raw)
|
if errors:
|
||||||
if clean_hostname is None:
|
for msg in errors:
|
||||||
flash(f"'{hostname_raw}' is not a valid hostname.", 'error')
|
flash(msg, 'error')
|
||||||
return redirect(f'/{_PAGE}')
|
return redirect(f'/{_PAGE}')
|
||||||
entry['hostname'] = clean_hostname
|
|
||||||
|
changes = diff_fields(before, existing)
|
||||||
|
if not changes:
|
||||||
|
flash('No changes were made.', 'info')
|
||||||
|
return redirect(f'/{_PAGE}')
|
||||||
|
flash(record_group(cfg, 'vlans', 'name', name, changes, 'core apply'), 'success')
|
||||||
|
|
||||||
|
else:
|
||||||
|
is_vpn = 'is_vpn' in request.form
|
||||||
|
|
||||||
|
if is_vpn and mdns_reflection:
|
||||||
|
flash('mDNS reflection is not supported on VPN VLANs.', 'error')
|
||||||
|
return redirect(f'/{_PAGE}')
|
||||||
|
|
||||||
|
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(f'/{_PAGE}')
|
||||||
|
|
||||||
|
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(f'/{_PAGE}')
|
||||||
|
|
||||||
|
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,
|
||||||
|
'server_identities': new_identities,
|
||||||
|
}
|
||||||
|
if dhcp_info:
|
||||||
|
entry['dhcp_information'] = dhcp_info
|
||||||
|
if is_vpn:
|
||||||
|
entry['peers'] = []
|
||||||
else:
|
else:
|
||||||
entry.pop('hostname', None)
|
entry['reservations'] = []
|
||||||
new_identities.append(entry)
|
vlans.append(entry)
|
||||||
|
|
||||||
gateway_raw = sanitize.ip(request.form.get('gateway', ''))
|
errors = validate.validate_config(cfg)
|
||||||
if gateway_raw and gateway_raw not in identity_ips:
|
if errors:
|
||||||
flash(f"Gateway '{gateway_raw}' must match one of the server identity IPs.", 'error')
|
for msg in errors:
|
||||||
return redirect(f'/{_PAGE}')
|
flash(msg, 'error')
|
||||||
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_servers_override' in request.form
|
|
||||||
dns_ips = []
|
|
||||||
for _line in request.form.get('dns_servers', '').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(f'/{_PAGE}')
|
return redirect(f'/{_PAGE}')
|
||||||
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(f'/{_PAGE}')
|
|
||||||
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(f'/{_PAGE}')
|
|
||||||
new_stored_dns = dns_ips if dns_override else []
|
|
||||||
_existing_dns = existing.get('dhcp_information', {}).get('explicit_overrides', {}).get('dns_servers', [])
|
|
||||||
existing_dns = _existing_dns if isinstance(_existing_dns, list) else ([_existing_dns] if _existing_dns else [])
|
|
||||||
|
|
||||||
ntp_override = 'ntp_server_override' in request.form
|
changes = diff_fields(None, entry)
|
||||||
ntp_ips = []
|
flash(record_group(cfg, 'vlans', 'name', name, changes, 'core apply'), 'success')
|
||||||
for _line in request.form.get('ntp_servers', '').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(f'/{_PAGE}')
|
|
||||||
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(f'/{_PAGE}')
|
|
||||||
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(f'/{_PAGE}')
|
|
||||||
new_stored_ntp = ntp_ips if ntp_override else []
|
|
||||||
_existing_ntp = existing.get('dhcp_information', {}).get('explicit_overrides', {}).get('ntp_servers', [])
|
|
||||||
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(f'/{_PAGE}')
|
|
||||||
|
|
||||||
before = copy.deepcopy(existing)
|
|
||||||
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_servers'] = new_stored_dns
|
|
||||||
else:
|
|
||||||
dhcp_overrides.pop('dns_servers', None)
|
|
||||||
if new_stored_ntp:
|
|
||||||
dhcp_overrides['ntp_servers'] = new_stored_ntp
|
|
||||||
else:
|
|
||||||
dhcp_overrides.pop('ntp_servers', 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(f'/{_PAGE}')
|
|
||||||
|
|
||||||
changes = diff_fields(before, existing)
|
|
||||||
flash(record_group(cfg, 'vlans', 'name', name, changes, 'core apply'), 'success')
|
|
||||||
return redirect(f'/{_PAGE}')
|
return redirect(f'/{_PAGE}')
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -95,7 +95,7 @@
|
||||||
{
|
{
|
||||||
"client_requirement": "client_is_administrator+",
|
"client_requirement": "client_is_administrator+",
|
||||||
"method": "js_edit",
|
"method": "js_edit",
|
||||||
"target": "edit-vlan-form",
|
"target": "add-form",
|
||||||
"text": "Edit",
|
"text": "Edit",
|
||||||
"class": "btn-ghost btn-sm"
|
"class": "btn-ghost btn-sm"
|
||||||
},
|
},
|
||||||
|
|
@ -112,14 +112,6 @@
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"type": "card",
|
|
||||||
"id": "edit-vlan-form",
|
|
||||||
"label": "Edit VLAN",
|
|
||||||
"hidden": true,
|
|
||||||
"client_requirement": "client_is_administrator+",
|
|
||||||
"items": []
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"type": "card",
|
"type": "card",
|
||||||
"id": "add-form",
|
"id": "add-form",
|
||||||
|
|
@ -128,9 +120,14 @@
|
||||||
"items": [
|
"items": [
|
||||||
{
|
{
|
||||||
"type": "form",
|
"type": "form",
|
||||||
"action": "/action/networklayout/addvlan_add",
|
"action": "/action/networklayout/vlans_addedit",
|
||||||
"method": "post",
|
"method": "post",
|
||||||
"items": [
|
"items": [
|
||||||
|
{
|
||||||
|
"type": "hidden",
|
||||||
|
"name": "row_index",
|
||||||
|
"value": ""
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"type": "field_row",
|
"type": "field_row",
|
||||||
"cols": 4,
|
"cols": 4,
|
||||||
|
|
@ -342,7 +339,7 @@
|
||||||
"items": [
|
"items": [
|
||||||
{
|
{
|
||||||
"type": "button_primary",
|
"type": "button_primary",
|
||||||
"action": "/action/networklayout/addvlan_add",
|
"action": "/action/networklayout/vlans_addedit",
|
||||||
"method": "post",
|
"method": "post",
|
||||||
"text": "Add VLAN",
|
"text": "Add VLAN",
|
||||||
"class": "add-vlan-btn",
|
"class": "add-vlan-btn",
|
||||||
|
|
|
||||||
|
|
@ -306,6 +306,23 @@ def config_datasource(name):
|
||||||
row['server_identity_dns_servers'] = '\n'.join(_dns) if isinstance(_dns, list) else str(_dns or '')
|
row['server_identity_dns_servers'] = '\n'.join(_dns) if isinstance(_dns, list) else str(_dns or '')
|
||||||
_ntp = v.get('dhcp_information', {}).get('explicit_overrides', {}).get('ntp_servers', [])
|
_ntp = v.get('dhcp_information', {}).get('explicit_overrides', {}).get('ntp_servers', [])
|
||||||
row['server_identity_ntp_servers'] = '\n'.join(_ntp) if isinstance(_ntp, list) else str(_ntp or '')
|
row['server_identity_ntp_servers'] = '\n'.join(_ntp) if isinstance(_ntp, list) else str(_ntp or '')
|
||||||
|
row['gateway'] = row['server_identity_gateway']
|
||||||
|
row['dns_servers'] = row['server_identity_dns_servers']
|
||||||
|
row['ntp_servers'] = row['server_identity_ntp_servers']
|
||||||
|
row['dns_servers_override'] = 1 if row['server_identity_dns_servers'] else 0
|
||||||
|
row['ntp_servers_override'] = 1 if row['server_identity_ntp_servers'] else 0
|
||||||
|
_dhi = v.get('dhcp_information', {})
|
||||||
|
row['dhcp_pool_start'] = _dhi.get('dynamic_pool_start', '')
|
||||||
|
row['dhcp_pool_end'] = _dhi.get('dynamic_pool_end', '')
|
||||||
|
_lt = _dhi.get('lease_time', '')
|
||||||
|
if _lt and len(_lt) > 1 and _lt[:-1].isdigit() and _lt[-1] in 'mhd':
|
||||||
|
row['dhcp_lease_time'] = _lt[:-1]
|
||||||
|
row['dhcp_lease_unit'] = {'m': 'minutes', 'h': 'hours', 'd': 'days'}[_lt[-1]]
|
||||||
|
else:
|
||||||
|
row['dhcp_lease_time'] = ''
|
||||||
|
row['dhcp_lease_unit'] = ''
|
||||||
|
row['dhcp_domain'] = _dhi.get('domain', '')
|
||||||
|
row['server_identities_json'] = json.dumps(v.get('server_identities', []))
|
||||||
rows.append(row)
|
rows.append(row)
|
||||||
return rows
|
return rows
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue