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>
135 lines
3.5 KiB
Go
135 lines
3.5 KiB
Go
package egress
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log/slog"
|
|
"os"
|
|
"sync"
|
|
"sync/atomic"
|
|
|
|
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store"
|
|
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/tail"
|
|
)
|
|
|
|
const maxLine = 16 * 1024
|
|
|
|
// Source follows the egress log file and feeds the engine. State is in memory
|
|
// only: on start the rotated backups and the live file are replayed from the
|
|
// beginning, which rebuilds the beacon history deterministically.
|
|
type Source struct {
|
|
Path string
|
|
E *Engine
|
|
Log *slog.Logger
|
|
backfilling atomic.Bool
|
|
fol atomic.Pointer[tail.Follower]
|
|
remote bool
|
|
rstate atomic.Pointer[string]
|
|
}
|
|
|
|
// NewRemoteSource creates a source fed by a probe through Feed: there is no
|
|
// file and nothing to start.
|
|
func NewRemoteSource(e *Engine, log *slog.Logger) *Source {
|
|
s := NewSource("", e, log)
|
|
s.remote = true
|
|
return s
|
|
}
|
|
|
|
// Feed analyses one line received from a probe.
|
|
func (s *Source) Feed(line []byte) { s.E.Ingest(line) }
|
|
|
|
// SetState sets the state shown for a remote source (following, waiting, error).
|
|
func (s *Source) SetState(st string) { s.rstate.Store(&st) }
|
|
|
|
// NewSource creates the source of e reading path.
|
|
func NewSource(path string, e *Engine, log *slog.Logger) *Source {
|
|
if log == nil {
|
|
log = slog.Default()
|
|
}
|
|
s := &Source{Path: path, E: e, Log: log}
|
|
f := s.state
|
|
e.stateFn.Store(&f)
|
|
return s
|
|
}
|
|
|
|
func (s *Source) state() string {
|
|
if s.remote {
|
|
if p := s.rstate.Load(); p != nil {
|
|
return *p
|
|
}
|
|
return "waiting"
|
|
}
|
|
switch f := s.fol.Load(); {
|
|
case s.backfilling.Load():
|
|
return "backfill"
|
|
case f == nil:
|
|
return "waiting"
|
|
default:
|
|
if err := f.Err(); err != nil {
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
return "waiting"
|
|
}
|
|
return "error"
|
|
}
|
|
}
|
|
return "following"
|
|
}
|
|
|
|
// Status is the source line for the sources panel.
|
|
func (s *Source) Status() store.SourceStatus {
|
|
l, m, last := s.E.counters()
|
|
st := store.SourceStatus{Name: "egress", Path: s.Path, State: s.state(), Lines: l, Malformed: m, LastEvent: last}
|
|
if st.State == "error" {
|
|
st.Error = "read error"
|
|
}
|
|
return st
|
|
}
|
|
|
|
// Start launches the single goroutine of the source; it stops with ctx.
|
|
func (s *Source) Start(ctx context.Context, wg *sync.WaitGroup) {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
defer func() {
|
|
if p := recover(); p != nil {
|
|
s.Log.Error("egress source stopped by a panic", "panic", p)
|
|
}
|
|
}()
|
|
s.backfilling.Store(true)
|
|
for _, b := range tail.Backups(s.Path) {
|
|
if ctx.Err() != nil {
|
|
break
|
|
}
|
|
if err := tail.ReadAll(b, maxLine, s.E.Ingest, func() { s.E.malformed.Add(1) }); err != nil {
|
|
s.Log.Warn("egress backfill failed", "file", b, "err", err)
|
|
}
|
|
}
|
|
s.backfilling.Store(false)
|
|
f := tail.NewFollower(s.Path, tail.Pos{})
|
|
f.MaxLine, f.Handle = maxLine, s.E.Ingest
|
|
f.Oversize = func() { s.E.malformed.Add(1) }
|
|
s.fol.Store(f)
|
|
f.Run(ctx)
|
|
}()
|
|
}
|
|
|
|
// SourceInfo is the "source" object of the API.
|
|
type SourceInfo struct {
|
|
State string `json:"state"`
|
|
Lines uint64 `json:"lines"`
|
|
Malformed uint64 `json:"malformed"`
|
|
// NonSYNDropped counts TCP lines without a pure SYN (old kit); not malformed.
|
|
NonSYNDropped uint64 `json:"non_syn_dropped"`
|
|
Last int64 `json:"last"`
|
|
}
|
|
|
|
// Info reports the source state (state "disabled" when no source is attached).
|
|
func (e *Engine) Info() SourceInfo {
|
|
l, m, last := e.counters()
|
|
st := "disabled"
|
|
if f := e.stateFn.Load(); f != nil {
|
|
st = (*f)()
|
|
}
|
|
return SourceInfo{State: st, Lines: l, Malformed: m, NonSYNDropped: e.nonSYN.Load(), Last: last}
|
|
}
|