Add network activity map overlay and configurable monitoring filters

This commit is contained in:
Maxim
2026-10-09 22:59:32 +03:00
parent f198cc47d9
commit 6a940772f0
11 changed files with 462 additions and 67 deletions

View File

@@ -12,18 +12,19 @@ import (
)
type TrafficEvent struct {
ID string `json:"id"`
Sequence int64 `json:"sequence,omitempty"`
Time int64 `json:"time"`
Kind string `json:"kind"`
IP string `json:"ip"`
Domain string `json:"domain,omitempty"`
Method string `json:"method,omitempty"`
Path string `json:"path,omitempty"`
Status int `json:"status,omitempty"`
Reason string `json:"reason,omitempty"`
Country string `json:"country,omitempty"`
Agent string `json:"agent,omitempty"`
Monitoring bool `json:"monitoring"`
ID string `json:"id"`
Sequence int64 `json:"sequence,omitempty"`
Time int64 `json:"time"`
Kind string `json:"kind"`
IP string `json:"ip"`
Domain string `json:"domain,omitempty"`
Method string `json:"method,omitempty"`
Path string `json:"path,omitempty"`
Status int `json:"status,omitempty"`
Reason string `json:"reason,omitempty"`
Country string `json:"country,omitempty"`
Agent string `json:"agent,omitempty"`
}
type TrafficBatch struct {
@@ -51,6 +52,7 @@ type TrafficCount struct {
}
type TrafficView struct {
Network *NetworkMapView `json:"network,omitempty"`
Events []TrafficEvent `json:"events"`
Sources []TrafficSource `json:"sources"`
Countries []TrafficCount `json:"countries"`
@@ -104,6 +106,7 @@ func (s *Store) SaveTraffic(agent string, batch TrafficBatch) error {
if e.Time < now-86400 || e.Time > now+300 {
continue
}
e.IP = net.ParseIP(e.IP).String()
country := lookupTrafficCountry(e.IP)
_, err = stmt.Exec(agent, e.ID, e.Time, e.Kind, e.IP, trafficText(e.Domain, 253), trafficText(e.Method, 16), trafficPath(e.Path), e.Status, trafficText(e.Reason, 256), country)
if err != nil {
@@ -144,12 +147,26 @@ func trafficPath(s string) string {
return trafficText(s, 256)
}
func (s *Store) TrafficView(domain, kind string, external bool) (TrafficView, error) {
func (s *Store) TrafficView(domain, kind string, external bool, hideMonitoring ...bool) (TrafficView, error) {
now := time.Now().Unix()
v := TrafficView{Events: []TrafficEvent{}, Sources: []TrafficSource{}, Countries: []TrafficCount{}, Domains: []TrafficCount{}, IPs: []TrafficCount{}, Series: []TrafficCount{}, Now: now, GeoReady: trafficGeoReady(), GeoDate: trafficGeoDate}
v.Settings = s.TrafficSettings()
hide := len(hideMonitoring) > 0 && hideMonitoring[0]
if v.Settings.NetworkEnabled {
snapshot, err := s.ReadNetworkSnapshot(v.Settings.AgentID, v.Settings.MonitoringRules, hide)
if err != nil {
snapshot.Error = "Не удалось прочитать сетевой снимок"
}
network := makeNetworkMap(snapshot, external)
v.Network = &network
}
where := ` WHERE ts>=? AND agent=?`
args := []any{now - 86400, v.Settings.AgentID}
if hide {
clause, params := monitoringSQL(v.Settings.MonitoringRules)
where += clause
args = append(args, params...)
}
if external {
where += ` AND country!='LAN'`
}
@@ -175,6 +192,7 @@ func (s *Store) TrafficView(domain, kind string, external bool) (TrafficView, er
rows.Close()
return v, err
}
e.Monitoring = isMonitoringRequest(e, v.Settings.MonitoringRules)
v.Events = append(v.Events, e)
}
err = rows.Err()
@@ -263,7 +281,7 @@ func trafficRoutes(mux *http.ServeMux, s *Store) {
http.Error(w, "Карта отключена", 404)
return
}
view, err := s.TrafficView(r.URL.Query().Get("domain"), r.URL.Query().Get("kind"), r.URL.Query().Get("external") == "1")
view, err := s.TrafficView(r.URL.Query().Get("domain"), r.URL.Query().Get("kind"), r.URL.Query().Get("external") == "1", r.URL.Query().Get("hideMonitoring") == "1")
writeJSON(w, view, err)
})
mux.HandleFunc("/api/traffic/world", func(w http.ResponseWriter, r *http.Request) {