Development

This commit is contained in:
Matthew Grotke 2026-06-09 11:00:37 -04:00
parent 909030bace
commit 721670469e
3 changed files with 43 additions and 17 deletions

View file

@ -36,8 +36,20 @@ def _dnsblocking_log_tail(cfg):
return '(error reading log)', '' return '(error reading log)', ''
WARN_ICON = '<img src="/www/icons/warning.svg" data-tooltip="Cached blocklist used. Verify URL is correct." style="width:1em;height:1em;vertical-align:middle;margin-left:0.35em;">'
def _last_dl_time():
path = f'{config_utils.BLOCKLISTS_DIR}/.last-dl'
try:
return int(open(path).read().strip())
except Exception:
return None
def blocklist_stats_html(cfg): def blocklist_stats_html(cfg):
db_rows = config_utils._bl_db_rows() db_rows = config_utils._bl_db_rows()
last_dl = _last_dl_time()
rows = '' rows = ''
for bl in cfg.get('dns_blocking', {}).get('blocklists', []): for bl in cfg.get('dns_blocking', {}).get('blocklists', []):
name = bl.get('name', '') name = bl.get('name', '')
@ -53,6 +65,7 @@ def blocklist_stats_html(cfg):
except Exception: except Exception:
size_str = '-' size_str = '-'
last_refreshed = 'Local' last_refreshed = 'Local'
warn = ''
else: else:
fetched_at = db.get('fetched_at') fetched_at = db.get('fetched_at')
if fetched_at: if fetched_at:
@ -66,14 +79,19 @@ def blocklist_stats_html(cfg):
bl_path = f'{config_utils.BLOCKLISTS_DIR}/{save_as}' if save_as else '' bl_path = f'{config_utils.BLOCKLISTS_DIR}/{save_as}' if save_as else ''
try: try:
size_str = config_utils.fmt_bytes(os.path.getsize(bl_path)) size_str = config_utils.fmt_bytes(os.path.getsize(bl_path))
file_mtime = int(os.path.getmtime(bl_path))
except Exception: except Exception:
size_str = '-' size_str = '-'
file_mtime = None
warn = ''
if last_dl and file_mtime is not None and file_mtime < last_dl:
warn = WARN_ICON
rows += ( rows += (
'<tr>' '<tr>'
f'<td class="table-cell">{factory.e(name)}</td>' f'<td class="table-cell">{factory.e(name)}</td>'
f'<td class="table-cell">{entries}</td>' f'<td class="table-cell">{entries}</td>'
f'<td class="table-cell">{size_str}</td>' f'<td class="table-cell">{size_str}</td>'
f'<td class="table-cell">{factory.e(last_refreshed)}</td>' f'<td class="table-cell">{factory.e(last_refreshed)}{warn}</td>'
'</tr>' '</tr>'
) )
if not rows: if not rows:

View file

@ -64,11 +64,16 @@ def download_blocklists(data):
return not any_fail return not any_fail
LAST_DL_FILE = BLOCKLIST_DIR / ".last-dl"
def main(): def main():
check_root() check_root()
data = load_config() data = load_config()
print("Downloading blocklists ==============================================") print("Downloading blocklists ==============================================")
success = download_blocklists(data) success = download_blocklists(data)
BLOCKLIST_DIR.mkdir(exist_ok=True)
LAST_DL_FILE.write_text(str(int(__import__('time').time())))
if not success: if not success:
print("WARNING: One or more downloads failed.") print("WARNING: One or more downloads failed.")
sys.exit(1) sys.exit(1)

View file

@ -515,27 +515,30 @@ def check_configurations(data):
pass pass
# --- Blocklist file freshness --- # --- Blocklist file freshness ---
now = datetime.now(timezone.utc).timestamp() now = datetime.now(timezone.utc).timestamp()
bl_library = {bl["name"]: bl for bl in data.get("dns_blocking", {}).get("blocklists", [])}
needed = set()
for vlan in vlans: for vlan in vlans:
names = vlan.get("use_blocklists", []) needed.update(vlan.get("use_blocklists", []))
if not names: for name in sorted(needed):
bl = bl_library.get(name)
if not bl or bl.get("bl_type") == "local":
continue continue
vlan_name = vlan["name"] save_as = bl.get("save_as", "")
path = _vlan_hosts_file(vlan) path = BLOCKLIST_DIR / save_as if save_as else None
label = ", ".join(sorted(names)) if not path or not path.exists():
if not path.exists():
results.append(problem( results.append(problem(
f"blocklist_{vlan_name}", f"blocklist ({vlan_name})", "warning", f"blocklist_{name}", f"blocklist ({name})", "warning",
f"Blocklist hosts file for '{vlan_name}' does not exist.", f"Blocklist file for '{name}' has not been downloaded.",
"Run `sudo python3 dl_blocklists.py && sudo python3 core.py --merge-blocklists`.")) "Run `sudo python3 dl_blocklists.py`."))
elif now - path.stat().st_mtime > BLOCKLIST_STALE_SECS: elif now - path.stat().st_mtime > BLOCKLIST_STALE_SECS:
age_h = int((now - path.stat().st_mtime) / 3600) age_h = int((now - path.stat().st_mtime) / 3600)
results.append(problem( results.append(problem(
f"blocklist_{vlan_name}", f"blocklist ({vlan_name})", "warning", f"blocklist_{name}", f"blocklist ({name})", "warning",
f"Blocklist hosts file for '{vlan_name}' is {age_h}h old (threshold 36h).", f"Blocklist '{name}' is {age_h}h old (threshold 36h).",
"Run `sudo python3 dl_blocklists.py && sudo python3 core.py --merge-blocklists`.")) "Run `sudo python3 dl_blocklists.py`."))
else: else:
results.append(ok(f"blocklist_{vlan_name}", f"blocklist ({vlan_name})")) results.append(ok(f"blocklist_{name}", f"blocklist ({name})"))
# --- Disk space --- # --- Disk space ---
try: try: