Development
This commit is contained in:
parent
89306b132d
commit
6ad78e9ed7
4 changed files with 187 additions and 104 deletions
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -33,8 +33,8 @@
|
|||
"class": "col-narrow"
|
||||
},
|
||||
{
|
||||
"label": "Source URL",
|
||||
"field": "url",
|
||||
"label": "Source",
|
||||
"field": "source_display",
|
||||
"class": "col-mono"
|
||||
}
|
||||
],
|
||||
|
|
|
|||
|
|
@ -37,35 +37,40 @@ def _dnsblocking_log_tail(cfg):
|
|||
|
||||
|
||||
def blocklist_stats_html(cfg):
|
||||
db_rows = config_utils._bl_db_rows()
|
||||
rows = ''
|
||||
for bl in cfg.get('dns_blocking', {}).get('blocklists', []):
|
||||
name = factory.e(bl.get('name', ''))
|
||||
name = bl.get('name', '')
|
||||
is_local = bl.get('bl_type') == 'local'
|
||||
save_as = bl.get('save_as', '')
|
||||
bl_path = f'{config_utils.BLOCKLISTS_DIR}/{save_as}' if save_as else ''
|
||||
db = db_rows.get(name, {})
|
||||
count = db.get('domain_count')
|
||||
entries = f'{count:,}' if count is not None else '-'
|
||||
if is_local:
|
||||
save_as = bl.get('save_as', '')
|
||||
bl_path = f'{config_utils.BLOCKLISTS_DIR}/{save_as}' if save_as else ''
|
||||
try:
|
||||
with open(bl_path) as f:
|
||||
entries = sum(1 for ln in f if ln.strip() and not ln.startswith('#'))
|
||||
size_str = config_utils.fmt_bytes(os.path.getsize(bl_path))
|
||||
last_refreshed = 'Local'
|
||||
except Exception:
|
||||
entries, size_str, last_refreshed = '-', '-', 'Local'
|
||||
size_str = '-'
|
||||
last_refreshed = 'Local'
|
||||
else:
|
||||
try:
|
||||
with open(bl_path) as f:
|
||||
entries = sum(1 for _ in f)
|
||||
mtime = int(os.path.getmtime(bl_path))
|
||||
size_str = config_utils.fmt_bytes(os.path.getsize(bl_path))
|
||||
fetched_at = db.get('fetched_at')
|
||||
if fetched_at:
|
||||
last_refreshed = (
|
||||
f'{datetime.fromtimestamp(mtime).strftime("%Y-%m-%d %H:%M")}'
|
||||
f' ({config_utils.relative_time(mtime, datetime.now(tz=timezone.utc).timestamp())} ago)'
|
||||
f'{datetime.fromtimestamp(fetched_at).strftime("%Y-%m-%d %H:%M")}'
|
||||
f' ({config_utils.relative_time(fetched_at, datetime.now(tz=timezone.utc).timestamp())} ago)'
|
||||
)
|
||||
else:
|
||||
last_refreshed = 'Never'
|
||||
save_as = bl.get('save_as', '')
|
||||
bl_path = f'{config_utils.BLOCKLISTS_DIR}/{save_as}' if save_as else ''
|
||||
try:
|
||||
size_str = config_utils.fmt_bytes(os.path.getsize(bl_path))
|
||||
except Exception:
|
||||
entries, size_str, last_refreshed = '-', '-', 'Never'
|
||||
size_str = '-'
|
||||
rows += (
|
||||
'<tr>'
|
||||
f'<td class="table-cell">{name}</td>'
|
||||
f'<td class="table-cell">{factory.e(name)}</td>'
|
||||
f'<td class="table-cell">{entries}</td>'
|
||||
f'<td class="table-cell">{size_str}</td>'
|
||||
f'<td class="table-cell">{factory.e(last_refreshed)}</td>'
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue