78 lines
2.5 KiB
Python
78 lines
2.5 KiB
Python
import os
|
|
|
|
from flask import Blueprint, request, redirect, flash
|
|
from auth import require_level
|
|
from config_utils import verify_core_hash, queued_msg, queue_command
|
|
import sanitize
|
|
import validation as validate
|
|
|
|
bp = Blueprint('action_apply_iface_config', __name__)
|
|
|
|
_VIEW = '/view/view_general'
|
|
|
|
_EXCLUDE_PREFIXES = ('lo', 'wg', 'docker', 'br-', 'veth',
|
|
'tun', 'tap', 'ppp', 'virbr',
|
|
'podman', 'vnet', 'macvtap', 'fc-')
|
|
|
|
def _valid_interface(name):
|
|
try:
|
|
return name in {
|
|
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 False
|
|
|
|
|
|
@bp.route('/action/apply_iface_config', methods=['POST'])
|
|
@require_level('administrator')
|
|
def apply_iface_config():
|
|
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)
|
|
|
|
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')
|
|
return redirect(_VIEW)
|
|
|
|
if not _valid_interface(iface):
|
|
flash(f"Interface '{iface}' does not exist on this system.", 'error')
|
|
return redirect(_VIEW)
|
|
|
|
mtu_int = None
|
|
if mtu:
|
|
mtu_int = validate.int_range(mtu, 68, 9000)
|
|
if mtu_int is None:
|
|
flash('MTU must be an integer between 68 and 9000.', 'error')
|
|
return redirect(_VIEW)
|
|
|
|
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')
|
|
return redirect(_VIEW)
|
|
|
|
if not mtu_int and not mac:
|
|
flash('No changes specified.', 'error')
|
|
return redirect(_VIEW)
|
|
|
|
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')
|
|
return redirect(_VIEW)
|
|
|
|
flash(queued_msg(), 'success')
|
|
return redirect(_VIEW)
|