Files
urutau/internal/store/live.go
T
Lucas RayanandClaude Opus 5.5 8546d14cef
CI / Format and vet (pull_request) Failing after 53s
CI / Dependency audit (pull_request) Successful in 33s
CI / Tests (Linux, race detector) (pull_request) Successful in 2m1s
CI / Docker images (build and smoke test) (pull_request) Failing after 47s
feat!: Urutau 2.0.0, ultralight probes and a single analysis hub
Each server runs an ultralight probe (scratch image, ~7 MiB RSS) that
tails the logs, ships raw lines to the hub over HTTPS with an on-disk
spool, and applies enforce decisions locally. The hub keeps one node per
probe (primary node = the 1.x data dir, unchanged), does all analysis and
serves the UI. Peer federation is removed. 'urutau import' brings 1.x
data dirs in with hash and count verification. UI: probes page, host
qualified incidents, IPs never wrap, egress heatmap labels, live and
replay globes no longer auto-rotate, replay repeat, live traffic metrics.

BREAKING CHANGE: URUTAU_PEERS, URUTAU_PEER_* and /api/v1/peer/* are gone;
log path variables move from the hub to the probe. See docs/migration-2.0.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 22:14:50 -03:00

190 lines
5.1 KiB
Go

package store
import (
"errors"
"fmt"
"math"
"net/netip"
"strconv"
"strings"
"unicode/utf8"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/core"
)
// Location is where an instance lives (the destination of its live arcs).
type Location struct {
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
Label string `json:"label,omitempty"`
}
// ParseLocation parses "lat,lon" or "lat,lon,Label" (strict, for configuration).
func ParseLocation(s string) (*Location, error) {
s = strings.TrimSpace(s)
if s == "" {
return nil, nil
}
p := strings.SplitN(s, ",", 3)
if len(p) < 2 {
return nil, errors.New("location: expected lat,lon or lat,lon,label")
}
lat, e1 := strconv.ParseFloat(strings.TrimSpace(p[0]), 64)
lon, e2 := strconv.ParseFloat(strings.TrimSpace(p[1]), 64)
if e1 != nil || e2 != nil || !validLatLon(lat, lon) {
return nil, errors.New("location: lat must be within [-90,90] and lon within [-180,180]")
}
l := &Location{Lat: lat, Lon: lon}
if len(p) == 3 {
l.Label = strings.TrimSpace(p[2])
if utf8.RuneCountInString(l.Label) > 64 || !utf8.ValidString(l.Label) || clipS(l.Label, 1<<20) != l.Label {
return nil, errors.New("location: label must have at most 64 characters and no control characters")
}
}
return l, nil
}
func validLatLon(lat, lon float64) bool {
return !math.IsNaN(lat) && !math.IsNaN(lon) && lat >= -90 && lat <= 90 && lon >= -180 && lon <= 180
}
// sanitizeLocation returns a safe copy or nil (the value may come from a peer).
func sanitizeLocation(l *Location) *Location {
if l == nil || !validLatLon(l.Lat, l.Lon) {
return nil
}
return &Location{Lat: l.Lat, Lon: l.Lon, Label: clipS(l.Label, 64)}
}
// GeoPoint is where an event came from.
type GeoPoint struct {
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
CC string `json:"cc,omitempty"`
City string `json:"city,omitempty"`
}
// LiveEvent is a feed event with the geography of its two ends.
type LiveEvent struct {
FeedEvent
Src *GeoPoint `json:"src,omitempty"`
Dst *Location `json:"dst,omitempty"`
}
// Location is the configured location of this instance (nil when unset).
func (s *Store) Location() *Location {
if l := s.locOverride.Load(); l != nil {
return sanitizeLocation(l)
}
return sanitizeLocation(s.cfg.Location)
}
// SetLocation replaces the configured location (the hub sets it per probe).
func (s *Store) SetLocation(l *Location) {
if l == nil {
s.locOverride.Store(nil)
return
}
c := *l
s.locOverride.Store(&c)
}
// GeoOf resolves the geography of an address or IPv6 /64 key (zero without an enricher).
func (s *Store) GeoOf(ip string) core.GeoInfo {
if s.enr == nil {
return core.GeoInfo{}
}
if p, err := netip.ParsePrefix(ip); err == nil {
ip = p.Addr().String()
}
if a, err := netip.ParseAddr(ip); err != nil || a.Zone() != "" {
return core.GeoInfo{}
}
return s.enr.Geo(ip)
}
// Live decorates a local feed event with its source geography and this
// instance's location.
func (s *Store) Live(fe FeedEvent) LiveEvent {
le := LiveEvent{FeedEvent: fe, Dst: s.Location()}
g := s.GeoOf(fe.IP)
if le.CC == "" {
le.CC = g.CC
}
if (g.Lat != 0 || g.Lon != 0) && validLatLon(g.Lat, g.Lon) {
le.Src = &GeoPoint{Lat: g.Lat, Lon: g.Lon, CC: g.CC, City: g.City}
}
return le
}
// LiveRecent returns the newest malicious local events, decorated, newest first.
func (s *Store) LiveRecent(n int) []LiveEvent {
out := make([]LiveEvent, 0, n)
for _, fe := range s.Recent(FeedSize) {
if len(out) >= n {
break
}
if core.Category(fe.Cat).IsMalicious() {
out = append(out, s.Live(fe))
}
}
return out
}
// Sanitize validates an event received from a peer; it returns false when the
// event must be dropped. Everything is bounded and stripped of control characters.
func (e *LiveEvent) Sanitize(now int64) bool {
a, err := netip.ParseAddr(e.IP)
if err != nil || a.Zone() != "" {
return false
}
e.IP = a.Unmap().String()
if !core.Category(e.Cat).IsMalicious() {
return false
}
if e.T <= 0 || e.T > now+300 || e.T < now-7*24*3600 {
e.T = now
}
e.Source = clipS(e.Source, 20)
e.Domain, e.Method, e.Path = clipS(e.Domain, 100), clipS(e.Method, 12), clipS(e.Path, 240)
e.User, e.Jail = clipS(e.User, 64), clipS(e.Jail, 40)
sanitizeFeedDetail(&e.FeedEvent)
if e.Status < 0 || e.Status > 999 {
e.Status = 0
}
e.Unban, e.Forged = false, false
e.CC = validCC2(clipS(e.CC, 2))
if e.Src != nil {
if !validLatLon(e.Src.Lat, e.Src.Lon) {
e.Src = nil
} else {
e.Src = &GeoPoint{Lat: e.Src.Lat, Lon: e.Src.Lon, CC: validCC2(clipS(e.Src.CC, 2)), City: clipS(e.Src.City, 80)}
}
}
e.Dst = sanitizeLocation(e.Dst)
return true
}
func validCC2(cc string) string {
if len(cc) != 2 || cc[0] < 'A' || cc[0] > 'Z' || cc[1] < 'A' || cc[1] > 'Z' {
return ""
}
return cc
}
// String is for debugging.
func (l Location) String() string { return fmt.Sprintf("%.4f,%.4f %s", l.Lat, l.Lon, l.Label) }
// SanitizeFeed validates a feed row that came from a peer's summary; false drops it.
func SanitizeFeed(r *FeedEvent) bool {
a, err := netip.ParseAddr(r.IP)
if err != nil || a.Zone() != "" {
return false
}
r.IP = a.Unmap().String()
r.CC = validCC2(clipS(r.CC, 2))
r.Unban, r.Forged = false, false
sanitizeFeedDetail(r)
return true
}