Development

This commit is contained in:
Matthew Grotke 2026-06-09 01:25:02 -04:00
parent 89306b132d
commit 6ad78e9ed7
4 changed files with 187 additions and 104 deletions

View file

@ -678,6 +678,23 @@ def resolve_iface(vlan, cfg):
# Config datasources ================================================
def _bl_db_rows():
"""Return {blocklist_name: {domain_count, fetched_at}} from domains.db, or {} if unavailable."""
db_path = os.path.join(BLOCKLISTS_DIR, 'domains.db')
try:
db = _sqlite3.connect(f'file:{db_path}?mode=ro', uri=True)
rows = db.execute('SELECT name, domain_count, fetched_at FROM blocklists').fetchall()
db.close()
return {name: {'domain_count': count, 'fetched_at': fetched_at}
for name, count, fetched_at in rows}
except Exception:
return {}
def _bl_db_counts():
return {name: v['domain_count'] for name, v in _bl_db_rows().items()}
def config_datasource(name):
cfg = load_config()
vlans = cfg.get('vlans', [])
@ -689,30 +706,25 @@ def config_datasource(name):
return cfg.get('host_overrides', [])
if name == 'blocklists':
db_counts = _bl_db_counts()
rows = []
for bl in cfg.get('dns_blocking', {}).get('blocklists', []):
row = dict(bl)
bl_type = bl.get('bl_type', 'community')
row['bl_type_label'] = 'Local' if bl_type == 'local' else 'Community'
bl_path = os.path.join(BLOCKLISTS_DIR, bl.get('save_as', ''))
count = db_counts.get(bl.get('name', ''))
row['domain_count'] = f'{count:,}' if count is not None else '-'
if bl_type == 'local':
bl_path = os.path.join(BLOCKLISTS_DIR, bl.get('save_as', ''))
try:
with open(bl_path) as f:
content = f.read()
row['local_entries'] = content.strip()
row['domain_count'] = str(sum(1 for ln in content.splitlines() if ln.strip() and not ln.startswith('#')))
row['local_entries'] = f.read().strip()
except Exception:
row['local_entries'] = ''
row['domain_count'] = '-'
row['last_updated'] = '-'
row['source_display'] = bl.get('save_as', '')
else:
try:
with open(bl_path) as f:
row['domain_count'] = str(sum(1 for _ in f))
row['last_updated'] = fmt_timestamp(int(os.path.getmtime(bl_path)))
except Exception:
row['domain_count'] = '-'
row['last_updated'] = '-'
row['local_entries'] = ''
row['source_display'] = row.get('url', '')
rows.append(row)
return rows