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
+41
-105
@@ -3,13 +3,10 @@ package server
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/netip"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/enforce"
|
||||
)
|
||||
@@ -20,16 +17,6 @@ var enforceEngine atomic.Pointer[enforce.Engine]
|
||||
// SetEnforce installs the engine the enforce endpoints talk to.
|
||||
func SetEnforce(e *enforce.Engine) { enforceEngine.Store(e) }
|
||||
|
||||
// peerRoute is a route opened by the federation token, contributed by its own file.
|
||||
type peerRoute struct {
|
||||
pattern string
|
||||
handler func(*Server) http.HandlerFunc
|
||||
}
|
||||
|
||||
var peerRoutes []peerRoute
|
||||
|
||||
const maxDecisionBody = 64 << 10
|
||||
|
||||
func init() {
|
||||
registerUserRoute("GET /api/v1/enforce", func(s *Server) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -44,15 +31,19 @@ func init() {
|
||||
}
|
||||
out := map[string]any{"mode": enforce.ModeOff, "share": "off", "threshold": 0, "log_writable": false,
|
||||
"allows": []enforce.Allow{}, "unban_log_writable": false, "coverage": enforce.Coverage{}, "counts": enforce.Counts{Skipped: map[string]int{}}, "recent": []enforce.Recent{}}
|
||||
if e := enforceEngine.Load(); e != nil {
|
||||
if e := s.enforceFor(r.URL.Query().Get("host")); e != nil {
|
||||
rep := e.Report(from)
|
||||
share := "off"
|
||||
if e.Share() {
|
||||
share = "on"
|
||||
}
|
||||
out["mode"], out["share"], out["threshold"] = e.Mode(), share, e.Threshold()
|
||||
out["log_writable"] = enforce.Writable(e.LogPath())
|
||||
out["unban_log_writable"] = enforce.Writable(e.UnbanLogPath())
|
||||
if e.HasSink() { // the probe writes the files: queueing is always possible
|
||||
out["log_writable"], out["unban_log_writable"] = true, true
|
||||
} else {
|
||||
out["log_writable"] = enforce.Writable(e.LogPath())
|
||||
out["unban_log_writable"] = enforce.Writable(e.UnbanLogPath())
|
||||
}
|
||||
out["allows"] = e.Allows()
|
||||
out["coverage"], out["counts"], out["recent"] = rep.Coverage, rep.Counts, rep.Recent
|
||||
}
|
||||
@@ -61,80 +52,6 @@ func init() {
|
||||
})
|
||||
registerUserRoute("POST /api/v1/enforce/unban", func(s *Server) http.HandlerFunc { return s.enforceUnban })
|
||||
registerUserRoute("DELETE /api/v1/enforce/allow", func(s *Server) http.HandlerFunc { return s.enforceEndAllow })
|
||||
peerRoutes = append(peerRoutes,
|
||||
peerRoute{"POST /api/v1/peer/unban", func(s *Server) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
e := enforceEngine.Load()
|
||||
if !s.peerGate(w, r) {
|
||||
return
|
||||
}
|
||||
if e == nil || !e.Share() {
|
||||
writeJSON(w, http.StatusNotFound, map[string]string{"error": "decision sharing is not enabled"})
|
||||
return
|
||||
}
|
||||
var in struct {
|
||||
Unbans []enforce.UnbanWire `json:"unbans"`
|
||||
}
|
||||
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, maxDecisionBody)).Decode(&in); err != nil {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid JSON"})
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]int{"accepted": e.AcceptPeerUnban(r.URL.Query().Get("from"), in.Unbans)})
|
||||
}
|
||||
}},
|
||||
peerRoute{"GET /api/v1/peer/decisions", func(s *Server) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
e := enforceEngine.Load()
|
||||
if !s.peerGate(w, r) {
|
||||
return
|
||||
}
|
||||
if e == nil || !e.Share() || e.Mode() != enforce.ModeOn {
|
||||
writeJSON(w, http.StatusNotFound, map[string]string{"error": "decision sharing is not enabled"})
|
||||
return
|
||||
}
|
||||
since, _ := strconv.ParseInt(r.URL.Query().Get("since"), 10, 64)
|
||||
if min := s.opt.Now().Add(-24 * time.Hour).Unix(); since < min {
|
||||
since = min
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{"decisions": e.LocalSince(since)})
|
||||
}
|
||||
}},
|
||||
peerRoute{"POST /api/v1/peer/decisions", func(s *Server) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
e := enforceEngine.Load()
|
||||
if !s.peerGate(w, r) {
|
||||
return
|
||||
}
|
||||
if e == nil || !e.Share() {
|
||||
writeJSON(w, http.StatusNotFound, map[string]string{"error": "decision sharing is not enabled"})
|
||||
return
|
||||
}
|
||||
body, err := io.ReadAll(http.MaxBytesReader(w, r.Body, maxDecisionBody))
|
||||
if err != nil {
|
||||
var mb *http.MaxBytesError
|
||||
if errors.As(err, &mb) {
|
||||
writeJSON(w, http.StatusRequestEntityTooLarge, map[string]string{"error": "body too large"})
|
||||
} else {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "unreadable body"})
|
||||
}
|
||||
return
|
||||
}
|
||||
var in struct {
|
||||
Decisions []enforce.Wire `json:"decisions"`
|
||||
}
|
||||
if json.Unmarshal(body, &in) != nil {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid JSON"})
|
||||
return
|
||||
}
|
||||
if len(in.Decisions) > enforce.MaxWire {
|
||||
writeJSON(w, http.StatusRequestEntityTooLarge, map[string]string{"error": "too many decisions"})
|
||||
return
|
||||
}
|
||||
acc, bad := e.AcceptPeer(r.URL.Query().Get("from"), in.Decisions)
|
||||
writeJSON(w, http.StatusOK, map[string]int{"accepted": acc, "invalid": bad})
|
||||
}
|
||||
}},
|
||||
)
|
||||
}
|
||||
|
||||
const maxUnbanBody = 1 << 10
|
||||
@@ -144,14 +61,10 @@ func (s *Server) enforceUnban(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusForbidden, map[string]string{"error": "cross-site request refused"})
|
||||
return
|
||||
}
|
||||
e := enforceEngine.Load()
|
||||
if e == nil {
|
||||
writeJSON(w, http.StatusConflict, map[string]string{"error": "the second firewall is off"})
|
||||
return
|
||||
}
|
||||
var in struct {
|
||||
IP string `json:"ip"`
|
||||
AllowS int `json:"allow_s"`
|
||||
Host string `json:"host"`
|
||||
}
|
||||
dec := json.NewDecoder(http.MaxBytesReader(w, r.Body, maxUnbanBody))
|
||||
dec.DisallowUnknownFields()
|
||||
@@ -160,18 +73,41 @@ func (s *Server) enforceUnban(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
a, err := netip.ParseAddr(strings.TrimSpace(in.IP))
|
||||
if err != nil || a.Zone() != "" || in.AllowS < 0 || in.AllowS > enforce.MaxAllow {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid ip or allow_s"})
|
||||
if err != nil || a.Zone() != "" || in.AllowS < 0 || in.AllowS > enforce.MaxAllow || len(in.Host) > 64 {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid ip, host or allow_s"})
|
||||
return
|
||||
}
|
||||
switch err := e.Unban(a, in.AllowS, "local"); {
|
||||
case errors.Is(err, enforce.ErrNoUnbanLog):
|
||||
writeJSON(w, http.StatusConflict, map[string]string{"error": "unban file not configured"})
|
||||
case err != nil:
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "could not write the unban request"})
|
||||
default:
|
||||
writeJSON(w, http.StatusOK, map[string]any{"ok": true, "ip": a.Unmap().String(), "allow_s": in.AllowS})
|
||||
var engines []*enforce.Engine
|
||||
if in.Host == "all" {
|
||||
hosts := []string{s.primaryName()}
|
||||
if s.opt.Nodes != nil {
|
||||
hosts = s.opt.Nodes.Hosts()
|
||||
}
|
||||
for _, h := range hosts {
|
||||
if e := s.enforceFor(h); e != nil {
|
||||
engines = append(engines, e)
|
||||
}
|
||||
}
|
||||
} else if e := s.enforceFor(in.Host); e != nil {
|
||||
engines = append(engines, e)
|
||||
}
|
||||
if len(engines) == 0 {
|
||||
writeJSON(w, http.StatusConflict, map[string]string{"error": "the second firewall is off"})
|
||||
return
|
||||
}
|
||||
done := 0
|
||||
for _, e := range engines {
|
||||
switch err := e.Unban(a, in.AllowS, "local"); {
|
||||
case errors.Is(err, enforce.ErrNoUnbanLog):
|
||||
writeJSON(w, http.StatusConflict, map[string]string{"error": "unban file not configured"})
|
||||
return
|
||||
case err != nil:
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "could not write the unban request"})
|
||||
return
|
||||
}
|
||||
done++
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{"ok": true, "ip": a.Unmap().String(), "allow_s": in.AllowS, "hosts": done})
|
||||
}
|
||||
|
||||
func (s *Server) enforceEndAllow(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -179,7 +115,7 @@ func (s *Server) enforceEndAllow(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusForbidden, map[string]string{"error": "cross-site request refused"})
|
||||
return
|
||||
}
|
||||
e := enforceEngine.Load()
|
||||
e := s.enforceFor(r.URL.Query().Get("host"))
|
||||
a, err := netip.ParseAddr(r.URL.Query().Get("ip"))
|
||||
if e == nil || err != nil {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid ip"})
|
||||
|
||||
Reference in new issue
Block a user