Files
urutau/internal/egress/source.go
T
Lucas RayanandClaude Opus 5.5 c33cca22e3
CI / Docker image (build and smoke test) (push) Skipped
CI / Format and vet (push) Successful in 28s
CI / Dependency audit (push) Successful in 16s
CI / Tests (Linux, race detector) (push) Successful in 1m3s
Release / Plan (reuse the published image?) (push) Successful in 4s
Release / ci (push) Skipped
Release / Build image (amd64) (push) Skipped
Release / Build image (arm64) (push) Skipped
Release / Publish tags (push) Successful in 13s
Release / Gitea release (push) Successful in 3s
feat: 1.4.0 second firewall, egress analysis and HTML report
Optional second firewall (URUTAU_ENFORCE off/observe/on) through the host
fail2ban, outbound traffic analysis (beaconing, mining, spam, DNS bypass,
unauthorised scans) with authorised scanner workloads, host kit in
deploy/host and a printable HTML report.

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

111 lines
2.7 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"`
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, Last: last}
}