feat!: Urutau 2.0.0, ultralight probes and a single analysis hub
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>
This commit is contained in:
1 parent
23a7a5963a
commit
8546d14cef
130 files changed
+9396
-3311
No files matched your search
+29
-110
@@ -1,43 +1,31 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/netip"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/core"
|
||||
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/enforce"
|
||||
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/fed"
|
||||
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store"
|
||||
)
|
||||
|
||||
// newEnforce builds the second firewall and hooks it into the store; nil when off.
|
||||
func newEnforce(c Config, st *store.Store, peers []fed.Peer, en enricher, ready func() bool) *enforce.Engine {
|
||||
// newEnforce builds the second firewall of one node and hooks it into the
|
||||
// node's store; nil when off. The decisions go to sink (the probe's queue).
|
||||
func newEnforce(c Config, st *store.Store, name, dir string, sink func(kind, line string) error, en enricher, ready func() bool) *enforce.Engine {
|
||||
mode := strings.ToLower(c.Enforce)
|
||||
if mode != enforce.ModeObserve && mode != enforce.ModeOn {
|
||||
return nil
|
||||
}
|
||||
trusted, _ := core.ParseCIDRs(c.Ignore + "," + c.Trusted)
|
||||
var hosts []string
|
||||
for _, p := range peers {
|
||||
if u, err := url.Parse(p.URL); err == nil && u.Hostname() != "" {
|
||||
hosts = append(hosts, u.Hostname())
|
||||
}
|
||||
}
|
||||
e := enforce.New(enforce.Config{
|
||||
Mode: mode, Path: c.EnforceLog, UnbanPath: unbanLogPath(c), StatePath: filepath.Join(c.DataDir, "enforce.json"), Host: c.Name,
|
||||
Mode: mode, Sink: sink, StatePath: filepath.Join(dir, "enforce.json"), Host: name,
|
||||
Threshold: c.EnforceThreshold, MaxPerMin: c.EnforceMaxPerMin, Share: strings.EqualFold(c.EnforceShare, "on"),
|
||||
Trusted: trusted, PeerHosts: hosts, InfraHosts: hosts, Resolve: resolveHost,
|
||||
Trusted: trusted, Resolve: resolveHost,
|
||||
Crawler: func(ip string, uas []string) bool {
|
||||
if v := st.FCrDNS(ip, uas); v != nil && *v {
|
||||
return true
|
||||
@@ -91,41 +79,33 @@ func resolveHost(h string) []netip.Addr {
|
||||
return ips
|
||||
}
|
||||
|
||||
// shareDecisions pulls the decisions of every peer and pushes the local ones, every 30 s.
|
||||
func shareDecisions(ctx context.Context, hub *fed.Hub, e *enforce.Engine, log *slog.Logger) {
|
||||
pulled := map[string]int64{}
|
||||
pushed := map[string]int64{}
|
||||
pushedUn := map[string]int64{}
|
||||
// shareDecisions relays, every 30 s, the decisions and unbans of each node to
|
||||
// the others (ENFORCE_SHARE=on); each receiver applies its own exclusions.
|
||||
func shareDecisions(ctx context.Context, nodes func() []*Node) {
|
||||
type mark struct{ dec, un int64 }
|
||||
last := map[[2]string]mark{} // [from, to]
|
||||
t := time.NewTicker(30 * time.Second)
|
||||
defer t.Stop()
|
||||
for {
|
||||
for _, p := range hub.Peers {
|
||||
start := time.Now().Unix()
|
||||
var in struct {
|
||||
Decisions []enforce.Wire `json:"decisions"`
|
||||
all := nodes()
|
||||
for _, from := range all {
|
||||
if from.Enf == nil {
|
||||
continue
|
||||
}
|
||||
if err := hub.Get(ctx, p, fmt.Sprintf("/api/v1/peer/decisions?since=%d", pulled[p.Name]), &in); err == nil {
|
||||
e.AcceptPeer(p.Name, in.Decisions)
|
||||
pulled[p.Name] = start - 5
|
||||
} else if ctx.Err() == nil {
|
||||
log.Debug("enforce: pull failed", "peer", p.Name, "err", err)
|
||||
}
|
||||
if mine := e.LocalSince(pushed[p.Name]); len(mine) > 0 {
|
||||
if err := hub.PostJSON(ctx, p, "/api/v1/peer/decisions?from="+url.QueryEscape(e.Host()), map[string]any{"decisions": mine}); err == nil {
|
||||
pushed[p.Name] = start
|
||||
} else if ctx.Err() == nil {
|
||||
log.Debug("enforce: push failed", "peer", p.Name, "err", err)
|
||||
for _, to := range all {
|
||||
if to == from || to.Enf == nil {
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, p := range hub.Peers {
|
||||
if mine := e.UnbansSince(pushedUn[p.Name]); len(mine) > 0 {
|
||||
k := [2]string{from.name, to.name}
|
||||
m := last[k]
|
||||
start := time.Now().Unix()
|
||||
if err := hub.PostJSON(ctx, p, "/api/v1/peer/unban?from="+url.QueryEscape(e.Host()), map[string]any{"unbans": mine}); err == nil {
|
||||
pushedUn[p.Name] = start
|
||||
} else if ctx.Err() == nil {
|
||||
log.Debug("enforce: unban push failed", "peer", p.Name, "err", err)
|
||||
if d := from.Enf.LocalSince(m.dec); len(d) > 0 {
|
||||
to.Enf.AcceptPeer(from.name, d)
|
||||
}
|
||||
if u := from.Enf.UnbansSince(m.un); len(u) > 0 {
|
||||
to.Enf.AcceptPeerUnban(from.name, u)
|
||||
}
|
||||
last[k] = mark{start - 5, start}
|
||||
}
|
||||
}
|
||||
select {
|
||||
@@ -136,77 +116,16 @@ func shareDecisions(ctx context.Context, hub *fed.Hub, e *enforce.Engine, log *s
|
||||
}
|
||||
}
|
||||
|
||||
// doctorEnforce reports the mode of the second firewall and its file.
|
||||
// doctorEnforce reports the mode of the second firewall. Decisions are queued
|
||||
// for the probes, which write them on their hosts (the probe reports a
|
||||
// command that cannot be written as cmd_errors).
|
||||
func doctorEnforce(c Config, add func(name, status, detail string)) {
|
||||
mode := strings.ToLower(c.Enforce)
|
||||
if mode == "" || mode == "off" {
|
||||
add("enforce", "off", "disabled (--enforce=off)")
|
||||
return
|
||||
}
|
||||
w := enforce.Writable(c.EnforceLog)
|
||||
detail := fmt.Sprintf("mode=%s share=%s threshold=%d file=%s writable=%v", mode, strings.ToLower(orDefault(c.EnforceShare, "off")), c.EnforceThreshold, c.EnforceLog, w)
|
||||
switch {
|
||||
case mode == "on" && !w:
|
||||
add("enforce", "fail", detail)
|
||||
case !w:
|
||||
add("enforce", "warn", detail)
|
||||
default:
|
||||
add("enforce", "ok", detail)
|
||||
}
|
||||
if mode == "on" {
|
||||
if n, tot := cloudflareClients(c.TraefikLog); n > 0 {
|
||||
add("enforce-cdn", "warn", fmt.Sprintf("%d of the last %d requests in %s come from Cloudflare addresses: Traefik is not "+
|
||||
"restoring the real client IP (set entryPoints.*.forwardedHeaders.trustedIPs to the Cloudflare ranges); "+
|
||||
"Cloudflare is never banned, but the real attackers stay invisible", n, tot, c.TraefikLog))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// cloudflareClients counts, in the tail of the Traefik JSON access log, the
|
||||
// requests whose ClientHost is a Cloudflare address.
|
||||
func cloudflareClients(path string) (cf, total int) {
|
||||
if path == "" {
|
||||
return 0, 0
|
||||
}
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return 0, 0
|
||||
}
|
||||
defer f.Close()
|
||||
const tail = 512 << 10
|
||||
if fi, err := f.Stat(); err == nil && fi.Size() > tail {
|
||||
f.Seek(fi.Size()-tail, io.SeekStart)
|
||||
}
|
||||
sc := bufio.NewScanner(f)
|
||||
sc.Buffer(make([]byte, 0, 64<<10), 1<<20)
|
||||
for sc.Scan() {
|
||||
var l struct {
|
||||
ClientHost string `json:"ClientHost"`
|
||||
}
|
||||
if json.Unmarshal(sc.Bytes(), &l) != nil || l.ClientHost == "" {
|
||||
continue
|
||||
}
|
||||
a, err := netip.ParseAddr(l.ClientHost)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
total++
|
||||
if enforce.IsCloudflare(a) {
|
||||
cf++
|
||||
}
|
||||
}
|
||||
return cf, total
|
||||
}
|
||||
|
||||
// unbanLogPath is --enforce-unban-log, or unban.log next to --enforce-log.
|
||||
func unbanLogPath(c Config) string {
|
||||
if c.EnforceUnbanLog != "" {
|
||||
return c.EnforceUnbanLog
|
||||
}
|
||||
if c.EnforceLog == "" {
|
||||
return ""
|
||||
}
|
||||
return filepath.Join(filepath.Dir(c.EnforceLog), "unban.log")
|
||||
add("enforce", "ok", fmt.Sprintf("mode=%s share=%s threshold=%d (decisions are queued for each probe)", mode, strings.ToLower(orDefault(c.EnforceShare, "off")), c.EnforceThreshold))
|
||||
}
|
||||
|
||||
func tallyOf(t store.Tally24) enforce.Tally {
|
||||
|
||||
Reference in new issue
Block a user