feat: Urutau 1.0.0, lightweight threat intelligence and web analytics
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
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
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>
This commit is contained in:
commit
b4a2c2c9ec
99 files changed
+15403
No files matched your search
@@ -0,0 +1,161 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/core"
|
||||
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/fed"
|
||||
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/geo"
|
||||
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/rdns"
|
||||
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/server"
|
||||
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store"
|
||||
)
|
||||
|
||||
// Serve runs the daemon until SIGTERM/SIGINT; it returns nil on a clean stop.
|
||||
func Serve(c Config) error {
|
||||
log := slog.New(slog.NewTextHandler(os.Stderr, nil))
|
||||
token, terr := Secret("URUTAU_TOKEN")
|
||||
if terr != nil {
|
||||
return terr
|
||||
}
|
||||
peers, err := c.Validate(token)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if os.Getenv("GOMEMLIMIT") == "" {
|
||||
debug.SetMemoryLimit(160 << 20)
|
||||
}
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT)
|
||||
defer stop()
|
||||
|
||||
st := store.New(store.Config{Name: c.Name, Version: Version, Retention: c.Retention, AdminUsers: c.AdminUserList()})
|
||||
st.SetDomains(splitList(c.Domains))
|
||||
rd := rdns.New()
|
||||
st.SetRDNS(rd)
|
||||
offsets := LoadState(st, c.DataDir, log)
|
||||
runner, err := NewRunner(c, st, log)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
gr, gerr := geo.Open(c.GeoDir)
|
||||
if gerr != nil {
|
||||
log.Warn("geo databases unavailable, locations disabled until they are downloaded", "dir", c.GeoDir, "err", gerr)
|
||||
gr = nil
|
||||
}
|
||||
en := enricher{g: gr}
|
||||
runner.Enr = en
|
||||
st.SetEnricher(en)
|
||||
st.SetAttribution(Attribution)
|
||||
runner.Extra = func() []store.SourceStatus { return []store.SourceStatus{geoStatus(gr, c.GeoDir)} }
|
||||
var hub *fed.Hub
|
||||
if len(peers) > 0 {
|
||||
hub = fed.NewHub(peers, c.PeerToken)
|
||||
}
|
||||
allow, _ := core.ParseCIDRs(c.PeerAllow)
|
||||
tproxy, _ := core.ParseCIDRs(c.TrustedProxy)
|
||||
ign, _ := core.ParseCIDRs(c.Ignore + "," + c.Trusted)
|
||||
bindHost, _, _ := net.SplitHostPort(c.Bind)
|
||||
h := server.New(server.Options{Token: token, PeerTokens: c.PeerTokens, PeerAllow: allow, TrustedProxy: tproxy, Ignore: ign,
|
||||
AllowHosts: []string{bindHost}, NoAuth: c.NoAuth, Version: Version, Store: st, Hub: hub})
|
||||
ln, err := net.Listen("tcp", c.Bind)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
srv := server.HTTPServer(h, ctx)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
runner.Start(ctx, offsets, &wg)
|
||||
wg.Add(2)
|
||||
go func() { defer wg.Done(); rd.Run(ctx) }()
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
save := time.NewTicker(60 * time.Second)
|
||||
prune := time.NewTicker(10 * time.Minute)
|
||||
defer save.Stop()
|
||||
defer prune.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-save.C:
|
||||
if err := runner.Save(); err != nil {
|
||||
log.Warn("snapshot failed", "err", err)
|
||||
}
|
||||
case <-prune.C:
|
||||
if n := st.Prune(); n > 0 {
|
||||
log.Info("pruned old IPs", "n", n)
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
if gr != nil && c.GeoUpdate > 0 {
|
||||
gr.StartUpdater(ctx, c.GeoUpdate, &http.Client{Timeout: 10 * time.Minute})
|
||||
}
|
||||
if hub != nil {
|
||||
wg.Add(1)
|
||||
go func() { defer wg.Done(); hub.Run(ctx) }()
|
||||
}
|
||||
log.Info("urutau started", "version", Version, "name", c.Name, "bind", ln.Addr().String(), "peers", len(peers), "auth", !c.NoAuth)
|
||||
errc := make(chan error, 1)
|
||||
go func() { errc <- srv.Serve(ln) }()
|
||||
select {
|
||||
case err := <-errc:
|
||||
stop()
|
||||
wg.Wait()
|
||||
return err
|
||||
case <-ctx.Done():
|
||||
}
|
||||
log.Info("shutting down")
|
||||
sctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
_ = srv.Shutdown(sctx)
|
||||
wg.Wait()
|
||||
if err := runner.Save(); err != nil {
|
||||
log.Warn("final snapshot failed", "err", err)
|
||||
} else {
|
||||
log.Info("snapshot saved")
|
||||
}
|
||||
if err := <-errc; err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
return fmt.Errorf("server: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadState restores the snapshot in dir. A missing file is a fresh start; a
|
||||
// corrupt or truncated one is set aside (.corrupt), logged, and the daemon
|
||||
// starts empty (the sources then backfill from the logs).
|
||||
func LoadState(st *store.Store, dir string, log *slog.Logger) map[string]store.Offset {
|
||||
path := SnapshotPath(dir)
|
||||
offsets, err := st.Load(path)
|
||||
if err != nil {
|
||||
log.Error("snapshot unreadable, starting fresh", "err", err)
|
||||
_ = os.Rename(path, path+".corrupt")
|
||||
return map[string]store.Offset{}
|
||||
}
|
||||
if len(offsets) > 0 {
|
||||
log.Info("snapshot restored", "ips", st.IPCount())
|
||||
}
|
||||
return offsets
|
||||
}
|
||||
|
||||
func splitList(s string) []string {
|
||||
var out []string
|
||||
for _, p := range strings.Split(s, ",") {
|
||||
if p = strings.TrimSpace(p); p != "" {
|
||||
out = append(out, p)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
Reference in new issue
Block a user