Files
urutau/internal/egress/source.go
T
Lucas RayanandClaude Opus 5.5 3ae72e27d6
Release / Publish tags (push) Successful in 13s
Release / Plan (reuse the published image?) (push) Successful in 5s
Release / ci (push) Successful in 1m20s
Release / Build image (amd64) (push) Successful in 24s
Release / Build image (arm64) (push) Successful in 1m11s
Release / Gitea release (push) Skipped
CI / Format and vet (push) Successful in 51s
CI / Tests (Linux, race detector) (push) Successful in 50s
CI / Dependency audit (push) Successful in 29s
CI / Docker image (build and smoke test) (push) Skipped
fix: 1.4.8 egress false positives (non-SYN replies, IMDS on host, Akamai/Telegram)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 08:51:42 -03:00

113 lines
2.9 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]
}
// 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 {
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}
}