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

129 lines
4.4 KiB
Python
Raw Normal View History

2026-05-27 22:04:04 -04:00
from pathlib import Path
2026-05-25 20:46:17 -04:00
import copy
2026-05-20 04:06:50 -04:00
import os
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, queued_msg, queue_command
2026-05-20 04:06:50 -04:00
import sanitize
2026-06-05 01:48:27 -04:00
import mod_validation as validate
2026-05-20 04:06:50 -04:00
2026-05-27 22:04:04 -04:00
_PAGE = Path(__file__).parent.name
2026-05-20 04:06:50 -04:00
2026-05-27 22:04:04 -04:00
bp = Blueprint(_PAGE, __name__)
2026-05-20 04:06:50 -04:00
_EXCLUDE_PREFIXES = ('lo', 'wg', 'docker', 'br-', 'veth',
'tun', 'tap', 'ppp', 'virbr',
'podman', 'vnet', 'macvtap', 'fc-')
2026-05-23 02:01:37 -04:00
def _get_system_interfaces():
2026-05-20 04:06:50 -04:00
try:
2026-05-23 02:01:37 -04:00
return {
2026-05-20 04:06:50 -04:00
n for n in os.listdir('/sys/class/net')
if not n.startswith(_EXCLUDE_PREFIXES)
and os.path.exists(f'/sys/class/net/{n}/device')
}
except Exception:
2026-05-23 02:01:37 -04:00
return set()
def _valid_interface(name):
return name in _get_system_interfaces()
2026-05-27 22:04:04 -04:00
@bp.route('/action/physicalinterfaces/physicalinterface_save', methods=['POST'])
2026-05-23 02:01:37 -04:00
@require_level('administrator')
2026-05-27 22:04:04 -04:00
def physicalinterface_save():
2026-05-23 02:01:37 -04:00
wan = sanitize.interface_name(request.form.get('wan_interface', ''))
lan = sanitize.interface_name(request.form.get('lan_interface', ''))
if not wan or not lan:
flash('Both WAN and LAN interfaces are required.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-23 02:01:37 -04:00
2026-05-31 22:01:59 -04:00
# WAN and LAN must be distinct physical interfaces
err = validate.check_wan_lan_unique(wan, lan)
if err:
flash(err, 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-23 02:01:37 -04:00
2026-05-25 19:59:42 -04:00
if not verify_config_hash(request.form.get('config_hash', '')):
2026-05-23 02:01:37 -04:00
flash('Configuration was modified by another session. Please refresh and try again.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-23 02:01:37 -04:00
available = _get_system_interfaces()
2026-05-31 22:01:59 -04:00
# Interfaces must exist on this system (checked against physical-only interface list)
2026-05-23 02:01:37 -04:00
for iface in (wan, lan):
2026-05-31 22:01:59 -04:00
err = validate.check_interface_exists(iface, available)
if err:
flash(err, 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-23 02:01:37 -04:00
2026-05-25 20:46:17 -04:00
cfg = load_config()
before = copy.deepcopy(cfg.get('network_interfaces', {}))
gen = cfg.setdefault('network_interfaces', {})
2026-05-23 02:01:37 -04:00
gen['wan_interface'] = wan
gen['lan_interface'] = lan
2026-05-25 19:59:42 -04:00
errors = validate.validate_config(cfg)
2026-05-23 02:01:37 -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-30 14:57:33 -04:00
changes = diff_fields(before, cfg['network_interfaces'])
flash(record_group(cfg, 'network_interfaces', None, None, changes, 'core apply'), 'success')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-20 04:06:50 -04:00
2026-05-27 22:04:04 -04:00
@bp.route('/action/physicalinterfaces/ifaceconfig_apply', methods=['POST'])
2026-05-20 04:06:50 -04:00
@require_level('administrator')
2026-05-27 22:04:04 -04:00
def ifaceconfig_apply():
2026-05-25 19:59:42 -04:00
if not verify_config_hash(request.form.get('config_hash', '')):
2026-05-20 04:06:50 -04:00
flash('Configuration was modified by another session. Please refresh and try again.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-20 04:06:50 -04:00
iface = sanitize.interface_name(request.form.get('iface', ''))
mtu = request.form.get('mtu', '').strip()
mac = sanitize.mac(request.form.get('mac', ''))
original_mtu = request.form.get('original_mtu', '').strip()
original_mac = sanitize.mac(request.form.get('original_mac', ''))
if not iface:
flash('No interface specified.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-20 04:06:50 -04:00
if not _valid_interface(iface):
flash(f"Interface '{iface}' does not exist on this system.", 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-20 04:06:50 -04:00
mtu_int = None
if mtu:
2026-05-20 17:10:18 -04:00
mtu_int = validate.int_range(mtu, 68, 9000)
if mtu_int is None:
2026-05-20 04:06:50 -04:00
flash('MTU must be an integer between 68 and 9000.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-20 04:06:50 -04:00
mac_raw = request.form.get('mac', '').strip()
if mac_raw and not mac:
flash('MAC address must be in the format aa:bb:cc:dd:ee:ff.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-20 04:06:50 -04:00
if not mtu_int and not mac:
flash('No changes specified.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-20 04:06:50 -04:00
queued = False
if mtu_int and str(mtu_int) != original_mtu:
queue_command(f'mtu {iface} {mtu_int}')
queued = True
if mac and mac != original_mac:
queue_command(f'mac {iface} {mac}')
queued = True
if not queued:
flash('No changes detected.', 'info')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-20 04:06:50 -04:00
2026-05-23 04:14:58 -04:00
flash(queued_msg(action_label='Changes queued'), 'success')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')