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 { return sanitizeLocation(s.cfg.Location) } // 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) if e.Status < 0 || e.Status > 999 { e.Status = 0 } e.Unban = 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) }