Files
urutau/internal/store/snapshot.go
T
Lucas RayanandClaude Opus 5.5 b4a2c2c9ec
Release / Plan (reuse the published image?) (push) Successful in 4s
CI / Docker image (build and smoke test) (push) Skipped
CI / Format and vet (push) Successful in 1m29s
CI / Tests (Linux, race detector) (push) Failing after 1m48s
CI / Dependency audit (push) Successful in 21s
Release / ci (push) Failing after 1m50s
Release / Build image (amd64) (push) Skipped
Release / Build image (arm64) (push) Skipped
Release / Publish tags (push) Skipped
Release / Gitea release (push) Skipped
feat: Urutau 1.0.0, lightweight threat intelligence and web analytics
Always-on Go service (stdlib only) that follows the Traefik JSON access
log, the sshd log and the fail2ban log of a host, classifies attacks,
scores sources, keeps bounded aggregates with snapshots, and serves an
embedded dashboard with live updates, offline geolocation (DB-IP Lite),
privacy-friendly traffic analytics, an exportable blocklist and
token-authenticated federation between instances.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 16:13:34 -03:00

126 lines
3.0 KiB
Go

package store
import (
"bufio"
"encoding/gob"
"errors"
"os"
"path/filepath"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/core"
)
const snapshotVersion = 1
// Offset is a saved read position of a log file.
type Offset struct {
Inode uint64
Offset int64
FP uint64
FPLen int
}
type snapshot struct {
V int
Saved int64
Stamps [slots]int64
Counts [slots][core.NumCats]uint32
Hours [hourSlots]hourSlot
MonSt [slots]int64
MonN [slots]uint32
Dom map[string]*domData
IPs map[string]*IPRec
Recent []FeedEvent
Rhead int
Offsets map[string]Offset
}
// Save writes the state atomically (tmp + rename).
func (s *Store) Save(path string, offsets map[string]Offset) error {
tmp := path + ".tmp"
f, err := os.OpenFile(tmp, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o600)
if err != nil {
return err
}
bw := bufio.NewWriterSize(f, 256*1024)
s.mu.RLock()
snap := snapshot{V: snapshotVersion, Saved: s.now().Unix(), Stamps: s.stamps, Counts: s.counts, Hours: s.hours, MonSt: s.monSt, MonN: s.monN, Dom: s.tr.dom,
IPs: s.ips, Recent: s.recent, Rhead: s.rhead, Offsets: offsets}
err = gob.NewEncoder(bw).Encode(&snap)
s.mu.RUnlock()
if err == nil {
err = bw.Flush()
}
if err == nil {
err = f.Sync()
}
if cerr := f.Close(); err == nil {
err = cerr
}
if err != nil {
os.Remove(tmp)
return err
}
return os.Rename(tmp, path)
}
// Load restores a snapshot and returns the saved file offsets. A missing file
// is not an error (fresh start).
func (s *Store) Load(path string) (map[string]Offset, error) {
f, err := os.Open(path)
if errors.Is(err, os.ErrNotExist) {
return map[string]Offset{}, nil
}
if err != nil {
return nil, err
}
defer f.Close()
var snap snapshot
if err := gob.NewDecoder(bufio.NewReaderSize(f, 256*1024)).Decode(&snap); err != nil {
return nil, err
}
if snap.V != snapshotVersion {
return nil, errors.New("snapshot version mismatch")
}
s.mu.Lock()
s.stamps, s.counts, s.hours = snap.Stamps, snap.Counts, snap.Hours
s.monSt, s.monN = snap.MonSt, snap.MonN
s.tr.dom = snap.Dom
for _, d := range s.tr.dom {
if d != nil && d.Last == 0 {
// snapshot written before Last existed: never prune on the first pass
d.Last = snap.Saved
for _, h := range d.Hours {
if h.Req > 0 && h.Hour*3600 > d.Last {
d.Last = h.Hour * 3600
}
}
}
}
s.ips = snap.IPs
if s.ips == nil {
s.ips = map[string]*IPRec{}
}
// a tampered snapshot must not be able to crash the ring or exceed the caps
if len(snap.Recent) > FeedSize {
snap.Recent = snap.Recent[len(snap.Recent)-FeedSize:]
snap.Rhead = 0
}
if len(snap.Recent) < FeedSize || snap.Rhead < 0 || snap.Rhead >= len(snap.Recent) {
snap.Rhead = 0
}
s.recent, s.rhead = snap.Recent, snap.Rhead
for len(s.ips) > s.cfg.MaxIPs {
s.evictLocked()
}
s.mu.Unlock()
s.invalidate()
if snap.Offsets == nil {
snap.Offsets = map[string]Offset{}
}
return snap.Offsets, nil
}
// EnsureDir creates the directory of a snapshot path.
func EnsureDir(path string) error { return os.MkdirAll(filepath.Dir(path), 0o700) }