import os from flask import Blueprint, request, redirect, flash from auth import require_level from config_utils import load_core, save_core, verify_core_hash, queued_msg import sanitize import validation as validate bp = Blueprint('action_apply_interface', __name__) _VIEW = '/view/view_general' _EXCLUDE_PREFIXES = ('lo', 'wg', 'docker', 'br-', 'veth', 'tun', 'tap', 'ppp', 'virbr', 'podman', 'vnet', 'macvtap', 'fc-') def _get_system_interfaces(): try: return { 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: return set() @bp.route('/action/apply_interface', methods=['POST']) @require_level('administrator') def apply_interface(): 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') return redirect(_VIEW) if wan == lan: flash('WAN and LAN interfaces must be different.', 'error') return redirect(_VIEW) if not verify_core_hash(request.form.get('config_hash', '')): flash('Configuration was modified by another session. Please refresh and try again.', 'error') return redirect(_VIEW) available = _get_system_interfaces() for iface in (wan, lan): if available and iface not in available: flash(f"Interface '{iface}' does not exist on this system.", 'error') return redirect(_VIEW) core = load_core() gen = core.setdefault('general', {}) gen['wan_interface'] = wan gen['lan_interface'] = lan errors = validate.validate_config(core) if errors: for msg in errors: flash(msg, 'error') return redirect(_VIEW) save_core(core) flash(queued_msg('core apply'), 'success') return redirect(_VIEW)