Development
This commit is contained in:
parent
563d82daf3
commit
70ccfe2c29
48 changed files with 549 additions and 578 deletions
|
|
@ -2,12 +2,12 @@ from pathlib import Path
|
|||
import copy
|
||||
import re
|
||||
from flask import Blueprint, request, redirect, flash, send_file
|
||||
from auth import require_level
|
||||
from config_utils import load_config, record_group, diff_fields, verify_config_hash, queued_msg, CONFIGS_DIR
|
||||
import auth
|
||||
import config_utils
|
||||
import sanitize
|
||||
import mod_validation as validate
|
||||
|
||||
DNS_LOG_FILE = Path(CONFIGS_DIR) / 'dns-blocklists.log'
|
||||
DNS_LOG_FILE = Path(config_utils.CONFIGS_DIR) / 'dns-blocklists.log'
|
||||
|
||||
_PAGE = Path(__file__).parent.name
|
||||
|
||||
|
|
@ -24,7 +24,7 @@ def _row_index():
|
|||
|
||||
|
||||
def _hash_ok():
|
||||
if not verify_config_hash(request.form.get('config_hash', '')):
|
||||
if not config_utils.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
|
||||
|
|
@ -56,7 +56,7 @@ def _parse_fields():
|
|||
|
||||
|
||||
@bp.route('/action/dnsblocking/blocklists_delete', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
@auth.require_level('administrator')
|
||||
def blocklists_delete():
|
||||
idx = _row_index()
|
||||
if idx is None:
|
||||
|
|
@ -66,7 +66,7 @@ def blocklists_delete():
|
|||
if not _hash_ok():
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
cfg = load_config()
|
||||
cfg = config_utils.load_config()
|
||||
items = cfg.get('dns_blocking', {}).get('blocklists', [])
|
||||
if idx < 0 or idx >= len(items):
|
||||
flash('Entry not found.', 'error')
|
||||
|
|
@ -80,13 +80,13 @@ def blocklists_delete():
|
|||
for msg in errors:
|
||||
flash(msg, 'error')
|
||||
return redirect(f'/{_PAGE}')
|
||||
changes = diff_fields(before, None)
|
||||
flash(record_group(cfg, 'dns_blocking.blocklists', 'name', name, changes, 'core apply', queue=False), 'success')
|
||||
changes = config_utils.diff_fields(before, None)
|
||||
flash(config_utils.record_group(cfg, 'dns_blocking.blocklists', 'name', name, changes, 'core apply', queue=False), 'success')
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
|
||||
@bp.route('/action/dnsblocking/blocklists_edit', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
@auth.require_level('administrator')
|
||||
def blocklists_edit():
|
||||
idx = _row_index()
|
||||
if idx is None:
|
||||
|
|
@ -100,7 +100,7 @@ def blocklists_edit():
|
|||
if not _hash_ok():
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
cfg = load_config()
|
||||
cfg = config_utils.load_config()
|
||||
items = cfg.get('dns_blocking', {}).get('blocklists', [])
|
||||
if idx < 0 or idx >= len(items):
|
||||
flash('Entry not found.', 'error')
|
||||
|
|
@ -125,13 +125,13 @@ def blocklists_edit():
|
|||
for msg in errors:
|
||||
flash(msg, 'error')
|
||||
return redirect(f'/{_PAGE}')
|
||||
changes = diff_fields(before, items[idx])
|
||||
flash(record_group(cfg, 'dns_blocking.blocklists', 'name', fields['name'], changes, 'core apply', queue=False), 'success')
|
||||
changes = config_utils.diff_fields(before, items[idx])
|
||||
flash(config_utils.record_group(cfg, 'dns_blocking.blocklists', 'name', fields['name'], changes, 'core apply', queue=False), 'success')
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
|
||||
@bp.route('/action/dnsblocking/addblocklist_add', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
@auth.require_level('administrator')
|
||||
def addblocklist_add():
|
||||
fields, err = _parse_fields()
|
||||
if err:
|
||||
|
|
@ -140,7 +140,7 @@ def addblocklist_add():
|
|||
if not _hash_ok():
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
cfg = load_config()
|
||||
cfg = config_utils.load_config()
|
||||
blocklists = cfg.setdefault('dns_blocking', {}).setdefault('blocklists', [])
|
||||
|
||||
# Blocklist name must be unique - it is the lookup key for VLAN use_blocklists references
|
||||
|
|
@ -162,13 +162,13 @@ def addblocklist_add():
|
|||
for msg in errors:
|
||||
flash(msg, 'error')
|
||||
return redirect(f'/{_PAGE}')
|
||||
changes = diff_fields(None, entry)
|
||||
flash(record_group(cfg, 'dns_blocking.blocklists', 'name', fields['name'], changes, 'core apply', queue=False), 'success')
|
||||
changes = config_utils.diff_fields(None, entry)
|
||||
flash(config_utils.record_group(cfg, 'dns_blocking.blocklists', 'name', fields['name'], changes, 'core apply', queue=False), 'success')
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
|
||||
@bp.route('/action/dnsblocking/blocklistrefresh_save', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
@auth.require_level('administrator')
|
||||
def blocklistrefresh_save():
|
||||
daily_execute_time = validate.time_24h(sanitize.time_24h(request.form.get('daily_execute_time_24hr_local', '')))
|
||||
|
||||
|
|
@ -176,27 +176,27 @@ def blocklistrefresh_save():
|
|||
flash('Daily Refresh Time must be a valid 24-hour time (e.g. 02:30).', 'error')
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
if not verify_config_hash(request.form.get('config_hash', '')):
|
||||
if not config_utils.verify_config_hash(request.form.get('config_hash', '')):
|
||||
flash('Configuration was modified by another session. Please refresh and try again.', 'error')
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
cfg = load_config()
|
||||
cfg = config_utils.load_config()
|
||||
before = copy.deepcopy(cfg.get('dns_blocking', {}).get('general', {}))
|
||||
cfg.setdefault('dns_blocking', {}).setdefault('general', {})['daily_execute_time_24hr_local'] = daily_execute_time
|
||||
changes = diff_fields(before, cfg['dns_blocking']['general'])
|
||||
flash(record_group(cfg, 'dns_blocking.general', None, None, changes, 'core apply'), 'success')
|
||||
changes = config_utils.diff_fields(before, cfg['dns_blocking']['general'])
|
||||
flash(config_utils.record_group(cfg, 'dns_blocking.general', None, None, changes, 'core apply'), 'success')
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
|
||||
@bp.route('/action/dnsblocking/blocklistrefresh_refresh', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
@auth.require_level('administrator')
|
||||
def blocklistrefresh_refresh():
|
||||
flash(queued_msg('core update-blocklists', action_label='Blocklist refresh queued'), 'success')
|
||||
flash(config_utils.queued_msg('core update-blocklists', action_label='Blocklist refresh queued'), 'success')
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
|
||||
@bp.route('/action/dnsblocking/logging_save', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
@auth.require_level('administrator')
|
||||
def logging_save():
|
||||
log_max_kb_raw = request.form.get('log_max_kb', '').strip()
|
||||
log_errors_only = 'log_errors_only' in request.form
|
||||
|
|
@ -206,11 +206,11 @@ def logging_save():
|
|||
flash('Max Log Size must be a number >= 64.', 'error')
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
if not verify_config_hash(request.form.get('config_hash', '')):
|
||||
if not config_utils.verify_config_hash(request.form.get('config_hash', '')):
|
||||
flash('Configuration was modified by another session. Please refresh and try again.', 'error')
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
cfg = load_config()
|
||||
cfg = config_utils.load_config()
|
||||
before = copy.deepcopy(cfg.get('dns_blocking', {}).get('general', {}))
|
||||
cfg.setdefault('dns_blocking', {}).setdefault('general', {}).update({
|
||||
'log_max_kb': log_max_kb,
|
||||
|
|
@ -221,13 +221,13 @@ def logging_save():
|
|||
for msg in errors:
|
||||
flash(msg, 'error')
|
||||
return redirect(f'/{_PAGE}')
|
||||
changes = diff_fields(before, cfg['dns_blocking']['general'])
|
||||
flash(record_group(cfg, 'dns_blocking.general', None, None, changes, 'core apply', queue=False), 'success')
|
||||
changes = config_utils.diff_fields(before, cfg['dns_blocking']['general'])
|
||||
flash(config_utils.record_group(cfg, 'dns_blocking.general', None, None, changes, 'core apply', queue=False), 'success')
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
|
||||
@bp.route('/action/dnsblocking/logging_clear', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
@auth.require_level('administrator')
|
||||
def logging_clear():
|
||||
try:
|
||||
DNS_LOG_FILE.write_text('')
|
||||
|
|
@ -238,7 +238,7 @@ def logging_clear():
|
|||
|
||||
|
||||
@bp.route('/action/dnsblocking/logging_download', methods=['GET'])
|
||||
@require_level('administrator')
|
||||
@auth.require_level('administrator')
|
||||
def logging_download():
|
||||
if not DNS_LOG_FILE.is_file():
|
||||
flash('Log file not found.', 'error')
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
import json
|
||||
import os
|
||||
from datetime import datetime, timezone
|
||||
from config_utils import collect_layout_tokens, load_datasource, fmt_bytes, relative_time, BLOCKLISTS_DIR, CONFIGS_DIR
|
||||
from factory import e, load_json, build_table, table_token_key, iter_table_items, PAGES_DIR
|
||||
import config_utils
|
||||
import factory
|
||||
|
||||
DNS_LOG_FILE = f'{CONFIGS_DIR}/dns-blocklists.log'
|
||||
DNS_LOG_FILE = f'{config_utils.CONFIGS_DIR}/dns-blocklists.log'
|
||||
DNS_LOG_MAX = 50
|
||||
|
||||
|
||||
|
|
@ -37,17 +37,17 @@ def _dnsblocking_log_tail(cfg):
|
|||
def blocklist_stats_html(cfg):
|
||||
rows = ''
|
||||
for bl in cfg.get('dns_blocking', {}).get('blocklists', []):
|
||||
name = e(bl.get('name', ''))
|
||||
name = factory.e(bl.get('name', ''))
|
||||
save_as = bl.get('save_as', '')
|
||||
bl_path = f'{BLOCKLISTS_DIR}/{save_as}' if save_as else ''
|
||||
bl_path = f'{config_utils.BLOCKLISTS_DIR}/{save_as}' if save_as else ''
|
||||
try:
|
||||
with open(bl_path) as f:
|
||||
entries = sum(1 for _ in f)
|
||||
mtime = int(os.path.getmtime(bl_path))
|
||||
size_str = fmt_bytes(os.path.getsize(bl_path))
|
||||
size_str = config_utils.fmt_bytes(os.path.getsize(bl_path))
|
||||
last_refreshed = (
|
||||
f'{datetime.fromtimestamp(mtime).strftime("%Y-%m-%d %H:%M")}'
|
||||
f' ({relative_time(mtime, datetime.now(tz=timezone.utc).timestamp())} ago)'
|
||||
f' ({config_utils.relative_time(mtime, datetime.now(tz=timezone.utc).timestamp())} ago)'
|
||||
)
|
||||
except Exception:
|
||||
entries, size_str, last_refreshed = '-', '-', 'Never'
|
||||
|
|
@ -56,7 +56,7 @@ def blocklist_stats_html(cfg):
|
|||
f'<td class="table-cell">{name}</td>'
|
||||
f'<td class="table-cell">{entries}</td>'
|
||||
f'<td class="table-cell">{size_str}</td>'
|
||||
f'<td class="table-cell">{e(last_refreshed)}</td>'
|
||||
f'<td class="table-cell">{factory.e(last_refreshed)}</td>'
|
||||
'</tr>'
|
||||
)
|
||||
if not rows:
|
||||
|
|
@ -73,7 +73,7 @@ def blocklist_stats_html(cfg):
|
|||
|
||||
|
||||
def collect_tokens(cfg):
|
||||
tokens = collect_layout_tokens(cfg)
|
||||
tokens = config_utils.collect_layout_tokens(cfg)
|
||||
dns_blk_gen = cfg.get('dns_blocking', {}).get('general', {})
|
||||
tokens['GENERAL_LOG_MAX_KB'] = str(dns_blk_gen.get('log_max_kb', '-'))
|
||||
tokens['GENERAL_LOG_ERRORS_ONLY'] = 'true' if dns_blk_gen.get('log_errors_only') else 'false'
|
||||
|
|
@ -84,8 +84,8 @@ def collect_tokens(cfg):
|
|||
{'value': 'hosts', 'label': 'hosts (hosts file format)'},
|
||||
{'value': 'dnsmasq', 'label': 'dnsmasq (local=/ syntax)'},
|
||||
])
|
||||
content = load_json(f'{PAGES_DIR}/dnsblocking/content.json')
|
||||
for table_item in iter_table_items(content.get('items', [])):
|
||||
content = factory.load_json(f'{factory.PAGES_DIR}/dnsblocking/content.json')
|
||||
for table_item in factory.iter_table_items(content.get('items', [])):
|
||||
ds = table_item.get('datasource', '')
|
||||
tokens[table_token_key(ds)] = build_table(table_item, tokens, load_datasource(ds))
|
||||
tokens[factory.table_token_key(ds)] = factory.build_table(table_item, tokens, config_utils.load_datasource(ds))
|
||||
return tokens
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue