TTOPM commited on
Commit
0ac7ee4
·
verified ·
1 Parent(s): 5c3b5e4

Create dashboard/app.py

Browse files
Files changed (1) hide show
  1. belel-shield/dashboard/app.py +145 -0
belel-shield/dashboard/app.py ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os, json, time
2
+ import pandas as pd
3
+ import streamlit as st
4
+ import pydeck as pdk
5
+ import plotly.express as px
6
+ from pathlib import Path
7
+
8
+ from utils import (
9
+ load_alerts, normalize_alerts, load_blocklist_status,
10
+ firewall_present, process_running, geoip_lookup, GEOLITE_DB
11
+ )
12
+
13
+ st.set_page_config(page_title="Belel Shield Dashboard", page_icon="🛡️", layout="wide")
14
+ with open(Path(__file__).with_name("styles.css")) as f:
15
+ st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
16
+
17
+ st.title("🛡️ Belel Shield — Live Defense Dashboard")
18
+
19
+ # Sidebar controls
20
+ st.sidebar.header("Controls")
21
+ auto_refresh = st.sidebar.checkbox("Auto-refresh", value=True)
22
+ refresh_sec = st.sidebar.slider("Refresh interval (sec)", 2, 30, 5)
23
+ use_geoip = st.sidebar.checkbox("Use local GeoIP (GeoLite2-City.mmdb in ~/.belel)", value=True)
24
+ external_ip_lookup = st.sidebar.checkbox("Allow external IP lookups (NOT recommended)", value=False, help="Disabled by default for privacy.")
25
+ min_conf = st.sidebar.slider("Min points for heatmap", 1, 100, 5)
26
+
27
+ # System status
28
+ colA, colB, colC, colD = st.columns(4)
29
+ with colA:
30
+ running = process_running()
31
+ st.metric("Scanner Running", "Yes" if running else "No")
32
+ with colB:
33
+ fw = firewall_present()
34
+ st.metric("Firewall Detected", "Yes" if fw else "No")
35
+ with colC:
36
+ bl = load_blocklist_status()
37
+ ok = bl["checksum_ok"]
38
+ if bl["present"] is False:
39
+ st.metric("Blocklist Cache", "Missing")
40
+ elif ok is None:
41
+ st.metric("Blocklist Cache", "Unverified")
42
+ else:
43
+ st.metric("Blocklist Checksum", "OK" if ok else "MISMATCH")
44
+ with colD:
45
+ st.metric("Auto-refresh", f"{refresh_sec}s" if auto_refresh else "Off")
46
+
47
+ st.markdown("---")
48
+
49
+ # Load alerts
50
+ rows = normalize_alerts(load_alerts())
51
+ df = pd.DataFrame(rows)
52
+ if not df.empty:
53
+ df = df.sort_values("dt")
54
+ st.subheader("Recent Alerts")
55
+ st.caption("Local events only; never transmitted.")
56
+
57
+ if df.empty:
58
+ st.info("No alerts yet. Keep the scanner running to populate events.")
59
+ else:
60
+ # Basic stats
61
+ c1, c2, c3 = st.columns(3)
62
+ with c1:
63
+ st.metric("Total Alerts", len(df))
64
+ with c2:
65
+ st.metric("Unique IPs", df["ip"].nunique())
66
+ with c3:
67
+ st.metric("Unique Hosts", df["host"].fillna("unknown").nunique())
68
+
69
+ # Timeline
70
+ timeline = df.groupby(pd.Grouper(key="dt", freq="5min"))["ip"].count().reset_index()
71
+ timeline.columns = ["time","alerts"]
72
+ fig = px.area(timeline, x="time", y="alerts", title="Alert Volume (5-min bins)")
73
+ st.plotly_chart(fig, use_container_width=True)
74
+
75
+ # Optional GeoIP
76
+ geos = []
77
+ reader = None
78
+ if use_geoip and GEOLITE_DB.exists():
79
+ try:
80
+ import geoip2.database
81
+ reader = geoip2.database.Reader(str(GEOLITE_DB))
82
+ except Exception as e:
83
+ st.warning(f"GeoIP reader unavailable: {e}")
84
+
85
+ for _, r in df.iterrows():
86
+ g = geoip_lookup(r["ip"], reader) if reader else None
87
+ if g and g.get("lat") and g.get("lon"):
88
+ geos.append({
89
+ "lat": g["lat"], "lon": g["lon"], "ip": r["ip"],
90
+ "host": r["host"] or "unknown",
91
+ "city": g.get("city") or "",
92
+ "country": g.get("country") or "",
93
+ "reason": r["reason"] or ""
94
+ })
95
+
96
+ # Map
97
+ st.subheader("Global Threat Map")
98
+ if geos:
99
+ gdf = pd.DataFrame(geos)
100
+
101
+ heat = pdk.Layer(
102
+ "HeatmapLayer",
103
+ data=gdf,
104
+ get_position=["lon", "lat"],
105
+ aggregation="MEAN",
106
+ get_weight=1,
107
+ radiusPixels=60,
108
+ )
109
+ scatter = pdk.Layer(
110
+ "ScatterplotLayer",
111
+ data=gdf,
112
+ get_position=["lon", "lat"],
113
+ get_radius=40000,
114
+ pickable=True,
115
+ auto_highlight=True,
116
+ )
117
+ tooltip = {"html": "<b>{ip}</b><br/>{host}<br/>{city}, {country}<br/>{reason}", "style": {"color": "white"}}
118
+ view = pdk.ViewState(latitude=20, longitude=0, zoom=1.2)
119
+
120
+ st.pydeck_chart(pdk.Deck(map_style="mapbox://styles/mapbox/dark-v10",
121
+ initial_view_state=view,
122
+ tooltip=tooltip,
123
+ layers=[heat, scatter] if len(gdf)>=min_conf else [scatter]))
124
+ else:
125
+ st.info("No geo points (provide ~/.belel/GeoLite2-City.mmdb to enable local GeoIP).")
126
+
127
+ st.markdown("---")
128
+ st.subheader("Table & Export")
129
+ st.dataframe(df[["timestamp","ip","port","host","reason"]], use_container_width=True)
130
+ cexp1, cexp2 = st.columns(2)
131
+ csv = df.to_csv(index=False).encode()
132
+ json_bytes = df.to_json(orient="records").encode()
133
+ with cexp1:
134
+ st.download_button("⬇️ Download CSV", csv, file_name="belel_alerts.csv", mime="text/csv")
135
+ with cexp2:
136
+ st.download_button("⬇️ Download JSON", json_bytes, file_name="belel_alerts.json", mime="application/json")
137
+
138
+ # Footer
139
+ st.markdown("---")
140
+ st.caption("Belel Shield • local-first • no telemetry")
141
+
142
+ # Auto refresh
143
+ if auto_refresh:
144
+ st.experimental_rerun() if st.session_state.get("_tick") else st.session_state.update({"_tick": True})
145
+ time.sleep(refresh_sec)