Release / Plan (reuse the published image?) (push) Successful in 3s
CI / Docker image (build and smoke test) (push) Skipped
CI / Format and vet (push) Successful in 28s
CI / Dependency audit (push) Successful in 20s
CI / Tests (Linux, race detector) (push) Successful in 1m10s
Release / ci (push) Successful in 1m10s
Release / Build image (arm64) (push) Successful in 55s
Release / Build image (amd64) (push) Successful in 1m16s
Release / Publish tags (push) Successful in 14s
Release / Gitea release (push) Skipped
New pages grouped in the rail (Monitor, Investigate, Analyze, Operate, Reports): Services (SLO, Apdex, latency app vs proxy), Sessions (stage funnel), Campaigns, Event search, MITRE ATT&CK matrix, Evolution, Concentration (Pareto, Gini), Survival (Kaplan-Meier per attack episode, recidivists), Where errors come from (application vs failed attack vs proxy vs client), Protocols and TLS, Defense (fail2ban, SSH, blocklist), Integrity, and a printable executive report. Chart library (donut, radar, stacked area, bars, heatmap, gauge), glossary with accessible "?" tooltips, self-explanatory category names (nonexistent page 404), page loader through pages.json. Unique visitors fixed: real browsers opening a page, one per IP per day, deduplicated across domains, with non-human clients counted apart. Anonymous legitimate visits on the Live page (?visits=1, no IP). Traefik origin, service, protocol and TLS fields parsed. Paths and Referer paths drop e-mail and token segments. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
146 lines
4.0 KiB
Go
146 lines
4.0 KiB
Go
package store
|
|
|
|
import (
|
|
"math"
|
|
"strings"
|
|
"sync"
|
|
)
|
|
|
|
// Live legitimate visits. Privacy: a visit event carries NO address, User-Agent
|
|
// or referrer; the source is the aggregated geography of the city (coordinates
|
|
// rounded to one decimal, about 11 km). It is only broadcast to subscribers
|
|
// (SSE ?visits=1), never written to the event log, a snapshot or live/recent.
|
|
|
|
// visitRate is the ceiling of visit events per second and instance; the excess
|
|
// is dropped and summarised by one visit_batch per second.
|
|
const visitRate = 20
|
|
|
|
// VisitMsg is a "visit" (one request) or a "visit_batch" (n dropped).
|
|
type VisitMsg struct {
|
|
Kind string `json:"kind"`
|
|
T int64 `json:"t"`
|
|
N int `json:"n,omitempty"`
|
|
Host string `json:"host,omitempty"`
|
|
Domain string `json:"domain,omitempty"`
|
|
Path string `json:"path,omitempty"`
|
|
Status int `json:"status,omitempty"`
|
|
Device string `json:"device,omitempty"`
|
|
Src *GeoPoint `json:"src,omitempty"`
|
|
Dst *Location `json:"dst,omitempty"`
|
|
}
|
|
|
|
type visitState struct {
|
|
mu sync.Mutex
|
|
subs map[chan VisitMsg]struct{}
|
|
sec int64
|
|
sent int
|
|
dropped int
|
|
}
|
|
|
|
// SubscribeVisits returns the live visit events of this instance.
|
|
func (s *Store) SubscribeVisits() (<-chan VisitMsg, func()) {
|
|
ch := make(chan VisitMsg, 64)
|
|
s.vis.mu.Lock()
|
|
if s.vis.subs == nil {
|
|
s.vis.subs = map[chan VisitMsg]struct{}{}
|
|
}
|
|
s.vis.subs[ch] = struct{}{}
|
|
s.vis.mu.Unlock()
|
|
return ch, func() {
|
|
s.vis.mu.Lock()
|
|
delete(s.vis.subs, ch)
|
|
s.vis.mu.Unlock()
|
|
}
|
|
}
|
|
|
|
func roundCoord(v float64) float64 { return math.Round(v*10) / 10 }
|
|
|
|
var visitDevices = map[string]bool{"desktop": true, "mobile": true, "tablet": true, "bot": true, "tool": true}
|
|
|
|
func visitPath(p string) string {
|
|
if i := strings.IndexAny(p, "?#"); i >= 0 {
|
|
p = p[:i]
|
|
}
|
|
return clipS(normalizePath(p), 120)
|
|
}
|
|
|
|
// emitVisit broadcasts one legitimate request to the visit subscribers.
|
|
// Called with s.mu held; it never blocks and costs nothing without subscribers.
|
|
func (s *Store) emitVisit(ts int64, dom, path, ip string, status int, device string, bot, tool bool) {
|
|
v := &s.vis
|
|
v.mu.Lock()
|
|
defer v.mu.Unlock()
|
|
if len(v.subs) == 0 {
|
|
return
|
|
}
|
|
if ts != v.sec {
|
|
if v.dropped > 0 {
|
|
s.fanVisit(VisitMsg{Kind: "visit_batch", T: v.sec, N: v.dropped, Host: s.cfg.Name})
|
|
}
|
|
v.sec, v.sent, v.dropped = ts, 0, 0
|
|
}
|
|
if v.sent >= visitRate {
|
|
v.dropped++
|
|
return
|
|
}
|
|
v.sent++
|
|
switch {
|
|
case bot:
|
|
device = "bot"
|
|
case tool:
|
|
device = "tool"
|
|
case !visitDevices[device]:
|
|
device = "unknown"
|
|
}
|
|
m := VisitMsg{Kind: "visit", T: ts, Host: s.cfg.Name, Domain: clipS(dom, 100), Path: visitPath(path), Status: status, Device: device, Dst: s.Location()}
|
|
if g := s.GeoOf(ip); (g.Lat != 0 || g.Lon != 0) && validLatLon(g.Lat, g.Lon) {
|
|
m.Src = &GeoPoint{Lat: roundCoord(g.Lat), Lon: roundCoord(g.Lon), CC: validCC2(g.CC), City: clipS(g.City, 80)}
|
|
}
|
|
s.fanVisit(m)
|
|
}
|
|
|
|
func (s *Store) fanVisit(m VisitMsg) {
|
|
for ch := range s.vis.subs {
|
|
select {
|
|
case ch <- m:
|
|
default: // slow client: drop
|
|
}
|
|
}
|
|
}
|
|
|
|
// Sanitize validates a visit event received from a peer (it has no address
|
|
// field, and anything unknown is dropped); false means: discard it.
|
|
func (m *VisitMsg) Sanitize(now int64) bool {
|
|
if m.Kind != "visit" && m.Kind != "visit_batch" {
|
|
return false
|
|
}
|
|
if m.T <= 0 || m.T > now+300 || m.T < now-3600 {
|
|
m.T = now
|
|
}
|
|
if m.Kind == "visit_batch" {
|
|
if m.N < 0 || m.N > 1<<20 {
|
|
return false
|
|
}
|
|
*m = VisitMsg{Kind: m.Kind, T: m.T, N: m.N}
|
|
return true
|
|
}
|
|
m.N = 0
|
|
// a peer's visit path is normalized again here: never trust it to have done so
|
|
m.Domain, m.Path = clipS(m.Domain, 100), clipS(normalizePath(m.Path), 120)
|
|
if m.Status < 0 || m.Status > 999 {
|
|
m.Status = 0
|
|
}
|
|
if !visitDevices[m.Device] {
|
|
m.Device = "unknown"
|
|
}
|
|
if m.Src != nil {
|
|
if !validLatLon(m.Src.Lat, m.Src.Lon) {
|
|
m.Src = nil
|
|
} else {
|
|
m.Src = &GeoPoint{Lat: roundCoord(m.Src.Lat), Lon: roundCoord(m.Src.Lon), CC: validCC2(clipS(m.Src.CC, 2)), City: clipS(m.Src.City, 80)}
|
|
}
|
|
}
|
|
m.Dst = sanitizeLocation(m.Dst)
|
|
return true
|
|
}
|