Development
This commit is contained in:
parent
d0cfffac52
commit
adcfe55c7c
24 changed files with 405 additions and 359 deletions
|
|
@ -2,7 +2,7 @@ import copy
|
|||
|
||||
from flask import Blueprint, request, redirect, flash
|
||||
from auth import require_level
|
||||
from config_utils import load_core, save_core_with_snapshot, verify_core_hash
|
||||
from config_utils import load_config, save_config_with_snapshot, verify_config_hash
|
||||
import sanitize
|
||||
import validation as validate
|
||||
|
||||
|
|
@ -21,7 +21,7 @@ def _row_index():
|
|||
|
||||
|
||||
def _hash_ok():
|
||||
if not verify_core_hash(request.form.get('config_hash', '')):
|
||||
if not verify_config_hash(request.form.get('config_hash', '')):
|
||||
flash('Configuration was modified by another session. Please refresh and try again.', 'error')
|
||||
return False
|
||||
return True
|
||||
|
|
@ -82,17 +82,17 @@ def add_port_forward():
|
|||
if not _hash_ok():
|
||||
return redirect(VIEW)
|
||||
|
||||
core = load_core()
|
||||
core.setdefault('port_forwarding', []).append(entry)
|
||||
errors = validate.validate_config(core)
|
||||
cfg = load_config()
|
||||
cfg.setdefault('port_forwarding', []).append(entry)
|
||||
errors = validate.validate_config(cfg)
|
||||
if errors:
|
||||
for msg in errors:
|
||||
flash(msg, 'error')
|
||||
return redirect(VIEW)
|
||||
|
||||
key = f'{entry["protocol"]}:{entry["dest_port"]}'
|
||||
flash(save_core_with_snapshot(
|
||||
core,
|
||||
flash(save_config_with_snapshot(
|
||||
cfg,
|
||||
path='port_forwarding', key=key, operation='add',
|
||||
before=None, after=entry,
|
||||
description=f'Added port forward: {key} → {entry["nat_ip"]}:{entry["nat_port"]}',
|
||||
|
|
@ -110,15 +110,15 @@ def toggle_port_forward():
|
|||
if not _hash_ok():
|
||||
return redirect(VIEW)
|
||||
|
||||
core = load_core()
|
||||
items = core.get('port_forwarding', [])
|
||||
cfg = load_config()
|
||||
items = cfg.get('port_forwarding', [])
|
||||
if idx < 0 or idx >= len(items):
|
||||
flash('Entry not found.', 'error')
|
||||
return redirect(VIEW)
|
||||
|
||||
old_enabled = items[idx].get('enabled', True)
|
||||
items[idx]['enabled'] = not old_enabled
|
||||
errors = validate.validate_config(core)
|
||||
errors = validate.validate_config(cfg)
|
||||
if errors:
|
||||
for msg in errors:
|
||||
flash(msg, 'error')
|
||||
|
|
@ -126,8 +126,8 @@ def toggle_port_forward():
|
|||
|
||||
key = f'{items[idx]["protocol"]}:{items[idx]["dest_port"]}'
|
||||
action = 'Enabled' if not old_enabled else 'Disabled'
|
||||
flash(save_core_with_snapshot(
|
||||
core,
|
||||
flash(save_config_with_snapshot(
|
||||
cfg,
|
||||
path='port_forwarding', key=key, operation='toggle',
|
||||
before={'enabled': old_enabled}, after={'enabled': not old_enabled},
|
||||
description=f'{action} port forward: {key}',
|
||||
|
|
@ -149,8 +149,8 @@ def edit_port_forward():
|
|||
if not _hash_ok():
|
||||
return redirect(VIEW)
|
||||
|
||||
core = load_core()
|
||||
items = core.get('port_forwarding', [])
|
||||
cfg = load_config()
|
||||
items = cfg.get('port_forwarding', [])
|
||||
if idx < 0 or idx >= len(items):
|
||||
flash('Entry not found.', 'error')
|
||||
return redirect(VIEW)
|
||||
|
|
@ -158,15 +158,15 @@ def edit_port_forward():
|
|||
before = copy.deepcopy(items[idx])
|
||||
items[idx] = entry
|
||||
items[idx]['enabled'] = request.form.get('enabled') == 'on'
|
||||
errors = validate.validate_config(core)
|
||||
errors = validate.validate_config(cfg)
|
||||
if errors:
|
||||
for msg in errors:
|
||||
flash(msg, 'error')
|
||||
return redirect(VIEW)
|
||||
|
||||
key = f'{entry["protocol"]}:{entry["dest_port"]}'
|
||||
flash(save_core_with_snapshot(
|
||||
core,
|
||||
flash(save_config_with_snapshot(
|
||||
cfg,
|
||||
path='port_forwarding', key=key, operation='edit',
|
||||
before=before, after=copy.deepcopy(items[idx]),
|
||||
description=f'Edited port forward: {key} → {entry["nat_ip"]}:{entry["nat_port"]}',
|
||||
|
|
@ -184,22 +184,22 @@ def delete_port_forward():
|
|||
if not _hash_ok():
|
||||
return redirect(VIEW)
|
||||
|
||||
core = load_core()
|
||||
items = core.get('port_forwarding', [])
|
||||
cfg = load_config()
|
||||
items = cfg.get('port_forwarding', [])
|
||||
if idx < 0 or idx >= len(items):
|
||||
flash('Entry not found.', 'error')
|
||||
return redirect(VIEW)
|
||||
|
||||
removed = items.pop(idx)
|
||||
errors = validate.validate_config(core)
|
||||
errors = validate.validate_config(cfg)
|
||||
if errors:
|
||||
for msg in errors:
|
||||
flash(msg, 'error')
|
||||
return redirect(VIEW)
|
||||
|
||||
key = f'{removed["protocol"]}:{removed["dest_port"]}'
|
||||
flash(save_core_with_snapshot(
|
||||
core,
|
||||
flash(save_config_with_snapshot(
|
||||
cfg,
|
||||
path='port_forwarding', key=key, operation='delete',
|
||||
before=removed, after=None,
|
||||
description=f'Deleted port forward: {key}',
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue