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

157 lines
4.8 KiB
Python
Raw Permalink Normal View History

2026-05-27 22:04:04 -04:00
from pathlib import Path
2026-05-25 16:07:21 -04:00
import copy
2026-05-17 03:26:01 -04:00
from flask import Blueprint, request, redirect, flash
2026-06-07 00:21:08 -04:00
import auth
import config_utils
2026-05-17 03:26:01 -04:00
import sanitize
2026-06-05 01:48:27 -04:00
import mod_validation as validate
2026-05-17 03:26:01 -04:00
2026-05-27 22:04:04 -04:00
_PAGE = Path(__file__).parent.name
2026-05-17 03:26:01 -04:00
2026-05-27 22:04:04 -04:00
bp = Blueprint(_PAGE, __name__)
2026-05-17 03:26:01 -04:00
def _row_index():
try:
return int(request.form.get('row_index', ''))
except (ValueError, TypeError):
return None
def _hash_ok():
2026-06-07 00:21:08 -04:00
if not config_utils.verify_config_hash(request.form.get('config_hash', '')):
2026-05-17 03:26:01 -04:00
flash('Configuration was modified by another session. Please refresh and try again.', 'error')
return False
return True
def _parse_ip():
raw = request.form.get('ip', '').strip()
if not raw:
flash('The configuration has not been saved because an IP address, CIDR, or wildcard pattern is required.', 'error')
return None
ip = validate.banned_ip(raw)
if not ip:
flash(f'The configuration has not been saved because "{raw}" is not a valid IP address, CIDR, or wildcard pattern.', 'error')
return None
return ip
2026-05-27 22:04:04 -04:00
@bp.route('/action/bannedips/addip_add', methods=['POST'])
2026-06-07 00:21:08 -04:00
@auth.require_level('administrator')
2026-05-27 22:04:04 -04:00
def addip_add():
2026-05-17 03:26:01 -04:00
description = sanitize.text(request.form.get('description', ''))
ip = _parse_ip()
if ip is None:
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
if not _hash_ok():
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
2026-06-07 00:21:08 -04:00
cfg = config_utils.load_config()
2026-05-25 16:07:21 -04:00
entry = {'description': description, 'ip': ip, 'enabled': True}
2026-05-25 19:59:42 -04:00
cfg.setdefault('banned_ips', []).append(entry)
errors = validate.validate_config(cfg)
2026-05-20 17:10:18 -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-17 03:26:01 -04:00
2026-06-07 00:21:08 -04:00
changes = config_utils.diff_fields(None, entry)
flash(config_utils.record_group(cfg, 'banned_ips', 'ip', ip, changes, 'core apply'), 'success')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
2026-05-27 22:04:04 -04:00
@bp.route('/action/bannedips/table_toggle', methods=['POST'])
2026-06-07 00:21:08 -04:00
@auth.require_level('administrator')
2026-05-27 22:04:04 -04:00
def table_toggle():
2026-05-17 03:26:01 -04:00
idx = _row_index()
if idx is None:
flash('Invalid request.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
if not _hash_ok():
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
2026-06-07 00:21:08 -04:00
cfg = config_utils.load_config()
2026-05-25 19:59:42 -04:00
items = cfg.get('banned_ips', [])
2026-05-17 03:26:01 -04:00
if idx < 0 or idx >= len(items):
flash('Entry not found.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
2026-05-25 16:07:21 -04:00
old_enabled = items[idx].get('enabled', True)
2026-05-30 14:57:33 -04:00
before = copy.deepcopy(items[idx])
2026-05-25 16:07:21 -04:00
items[idx]['enabled'] = not old_enabled
2026-05-25 19:59:42 -04:00
errors = validate.validate_config(cfg)
2026-05-20 17:10:18 -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-17 03:26:01 -04:00
2026-05-30 14:57:33 -04:00
ip = items[idx]['ip']
2026-06-07 00:21:08 -04:00
changes = config_utils.diff_fields(before, items[idx])
flash(config_utils.record_group(cfg, 'banned_ips', 'ip', ip, changes, 'core apply'), 'success')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
2026-05-27 22:04:04 -04:00
@bp.route('/action/bannedips/table_edit', methods=['POST'])
2026-06-07 00:21:08 -04:00
@auth.require_level('administrator')
2026-05-27 22:04:04 -04:00
def table_edit():
2026-05-17 03:26:01 -04:00
idx = _row_index()
if idx is None:
flash('Invalid request.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
description = sanitize.text(request.form.get('description', ''))
ip = _parse_ip()
if ip is None:
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
enabled = request.form.get('enabled') == 'on'
if not _hash_ok():
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
2026-06-07 00:21:08 -04:00
cfg = config_utils.load_config()
2026-05-25 19:59:42 -04:00
items = cfg.get('banned_ips', [])
2026-05-17 03:26:01 -04:00
if idx < 0 or idx >= len(items):
flash('Entry not found.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
2026-05-25 16:07:21 -04:00
before = copy.deepcopy(items[idx])
2026-05-17 03:26:01 -04:00
items[idx].update({'description': description, 'ip': ip, 'enabled': enabled})
2026-05-25 19:59:42 -04:00
errors = validate.validate_config(cfg)
2026-05-20 17:10:18 -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-17 03:26:01 -04:00
2026-06-07 00:21:08 -04:00
changes = config_utils.diff_fields(before, items[idx])
flash(config_utils.record_group(cfg, 'banned_ips', 'ip', ip, changes, 'core apply'), 'success')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
2026-05-27 22:04:04 -04:00
@bp.route('/action/bannedips/table_delete', methods=['POST'])
2026-06-07 00:21:08 -04:00
@auth.require_level('administrator')
2026-05-27 22:04:04 -04:00
def table_delete():
2026-05-17 03:26:01 -04:00
idx = _row_index()
if idx is None:
flash('Invalid request.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
if not _hash_ok():
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
2026-06-07 00:21:08 -04:00
cfg = config_utils.load_config()
2026-05-25 19:59:42 -04:00
items = cfg.get('banned_ips', [])
2026-05-17 03:26:01 -04:00
if idx < 0 or idx >= len(items):
flash('Entry not found.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
removed = items.pop(idx)
2026-05-25 19:59:42 -04:00
errors = validate.validate_config(cfg)
2026-05-20 17:10:18 -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-17 03:26:01 -04:00
2026-06-07 00:21:08 -04:00
changes = config_utils.diff_fields(removed, None)
flash(config_utils.record_group(cfg, 'banned_ips', 'ip', removed['ip'], changes, 'core apply'), 'success')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')