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} }