57 lines
2.2 KiB
Python
57 lines
2.2 KiB
Python
import json
|
|
import os
|
|
from datetime import datetime, timezone
|
|
from view_common import fmt_bytes, relative_time, BLOCKLISTS_DIR
|
|
from factory import e
|
|
|
|
|
|
def _blocklist_stats_html(cfg):
|
|
rows = ''
|
|
for bl in cfg.get('dns_blocking', {}).get('blocklists', []):
|
|
name = e(bl.get('name', ''))
|
|
save_as = bl.get('save_as', '')
|
|
bl_path = f'{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))
|
|
last_refreshed = (
|
|
f'{datetime.fromtimestamp(mtime).strftime("%Y-%m-%d %H:%M")}'
|
|
f' ({relative_time(mtime, datetime.now(tz=timezone.utc).timestamp())} ago)'
|
|
)
|
|
except Exception:
|
|
entries, size_str, last_refreshed = '-', '-', 'Never'
|
|
rows += (
|
|
'<tr>'
|
|
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>'
|
|
'</tr>'
|
|
)
|
|
if not rows:
|
|
return ''
|
|
return (
|
|
'<table class="data-table"><thead><tr>'
|
|
'<th class="table-header">Blocklist</th>'
|
|
'<th class="table-header">Entries</th>'
|
|
'<th class="table-header">Size</th>'
|
|
'<th class="table-header">Last Refreshed</th>'
|
|
'</tr></thead>'
|
|
f'<tbody>{rows}</tbody></table>'
|
|
)
|
|
|
|
|
|
def collect_tokens(cfg):
|
|
dns_blk_gen = cfg.get('dns_blocking', {}).get('general', {})
|
|
return {
|
|
'GENERAL_LOG_MAX_KB': str(dns_blk_gen.get('log_max_kb', '-')),
|
|
'GENERAL_LOG_ERRORS_ONLY': 'true' if dns_blk_gen.get('log_errors_only') else 'false',
|
|
'GENERAL_DAILY_EXECUTE_TIME': str(dns_blk_gen.get('daily_execute_time_24hr_local', '-')),
|
|
'BLOCKLIST_STATS_HTML': _blocklist_stats_html(cfg),
|
|
'BLOCKLIST_FORMAT_OPTIONS': json.dumps([
|
|
{'value': 'hosts', 'label': 'hosts (hosts file format)'},
|
|
{'value': 'dnsmasq', 'label': 'dnsmasq (local=/ syntax)'},
|
|
]),
|
|
}
|