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
+7
-157
@@ -15,7 +15,6 @@ import (
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
@@ -31,9 +30,6 @@ var webFS embed.FS
|
||||
// maxSSE bounds the simultaneous live streams.
|
||||
const maxSSE = 20
|
||||
|
||||
// maxPeerSSE bounds the simultaneous hub subscriptions to /api/v1/peer/stream.
|
||||
const maxPeerSSE = 10
|
||||
|
||||
// sseHeartbeat is how often a live stream pings and revalidates its session.
|
||||
var sseHeartbeat = 20 * time.Second
|
||||
|
||||
@@ -51,16 +47,12 @@ func HTTPServer(h http.Handler, base context.Context) *http.Server {
|
||||
// Options configure the handler.
|
||||
type Options struct {
|
||||
Token string
|
||||
// PeerTokens are the federation tokens accepted by /api/v1/peer/export.
|
||||
PeerTokens []string
|
||||
// Ignore lists networks that must never appear in the blocklist (ignore + trusted).
|
||||
Ignore []netip.Prefix
|
||||
// AllowHosts: with NoAuth, only requests whose Host is one of these are served
|
||||
// (anti DNS rebinding).
|
||||
AllowHosts []string
|
||||
// PeerAllow, when not empty, restricts /api/v1/peer/export to these source
|
||||
// networks. TrustedProxy lists the reverse proxies whose X-Forwarded-For is believed.
|
||||
PeerAllow []netip.Prefix
|
||||
// TrustedProxy lists the reverse proxies whose X-Forwarded-For is believed.
|
||||
TrustedProxy []netip.Prefix
|
||||
NoAuth bool
|
||||
// LocalBlocklist makes the blocklist use only this instance's IPs (--peer-blocklist=off).
|
||||
@@ -68,6 +60,10 @@ type Options struct {
|
||||
Version string
|
||||
Store *store.Store
|
||||
Hub *fed.Hub
|
||||
// Probes serves the probe ingest and the admin routes (nil: not a hub).
|
||||
Probes ProbeAPI
|
||||
// Nodes gives per-host access to the engines and tickets of every probe.
|
||||
Nodes NodeSet
|
||||
// Incidents is the ticket store (nil: incident management off).
|
||||
Incidents *incidents.Store
|
||||
Now func() time.Time
|
||||
@@ -76,14 +72,12 @@ type Options struct {
|
||||
// Server is the HTTP handler.
|
||||
type Server struct {
|
||||
sse atomic.Int32
|
||||
exp exportCache
|
||||
opt Options
|
||||
ses *sessions
|
||||
lim *limiter
|
||||
mux *http.ServeMux
|
||||
assets fs.FS
|
||||
geo geoCache
|
||||
psse atomic.Int32
|
||||
study chan struct{}
|
||||
user chan struct{} // user-side studies (period, metrics) running at once
|
||||
}
|
||||
@@ -102,10 +96,6 @@ func New(opt Options) http.Handler {
|
||||
m.HandleFunc("POST /logout", s.logout)
|
||||
m.HandleFunc("GET /{$}", s.index)
|
||||
m.HandleFunc("GET /{file}", s.asset)
|
||||
m.HandleFunc("GET /api/v1/peer/export", s.peerExport)
|
||||
m.HandleFunc("GET /api/v1/peer/stream", s.peerStream)
|
||||
m.HandleFunc("GET /api/v1/peer/period", s.peerPeriod)
|
||||
m.HandleFunc("GET /api/v1/peer/egress", s.peerEgress)
|
||||
m.HandleFunc("GET /api/v1/geo", s.api(s.geoAPI))
|
||||
m.HandleFunc("GET /api/v1/live/recent", s.api(s.liveRecent))
|
||||
m.HandleFunc("GET /api/v1/summary", s.api(s.summary))
|
||||
@@ -123,9 +113,7 @@ func New(opt Options) http.Handler {
|
||||
for _, r := range userRoutes {
|
||||
m.HandleFunc(r.pattern, s.api(r.handler(s)))
|
||||
}
|
||||
for _, r := range peerRoutes { // peer-token routes of api_<name>.go files
|
||||
m.HandleFunc(r.pattern, r.handler(s))
|
||||
}
|
||||
s.probeRoutes(m)
|
||||
m.HandleFunc("GET /api/v1/events/stream", s.api(s.stream))
|
||||
m.HandleFunc("GET /api/v1/blocklist.txt", s.api(s.blocklist(false)))
|
||||
m.HandleFunc("GET /api/v1/blocklist.json", s.api(s.blocklist(true)))
|
||||
@@ -168,7 +156,7 @@ func (s *Server) health(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// authKind returns "session" or "bearer" for a valid user credential (cookie or
|
||||
// URUTAU_TOKEN), "peer" for the federation token, "" otherwise.
|
||||
// URUTAU_TOKEN), "" otherwise.
|
||||
func (s *Server) authKind(r *http.Request) string {
|
||||
if s.opt.NoAuth {
|
||||
return "session"
|
||||
@@ -178,9 +166,6 @@ func (s *Server) authKind(r *http.Request) string {
|
||||
if tokenEqual(tok, s.opt.Token) {
|
||||
return "bearer"
|
||||
}
|
||||
if s.peerTokenOK(tok) {
|
||||
return "peer"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
if c, err := r.Cookie(cookieName); err == nil && s.ses.valid(c.Value, s.opt.Now()) {
|
||||
@@ -189,25 +174,12 @@ func (s *Server) authKind(r *http.Request) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (s *Server) peerTokenOK(tok string) bool {
|
||||
ok := false
|
||||
for _, t := range s.opt.PeerTokens { // no early exit: constant work per token
|
||||
if tokenEqual(tok, t) {
|
||||
ok = true
|
||||
}
|
||||
}
|
||||
return ok
|
||||
}
|
||||
|
||||
// api wraps a user endpoint: only a session or URUTAU_TOKEN opens it. The peer
|
||||
// token is refused with 403 (it is a valid credential, for another route).
|
||||
func (s *Server) api(h http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
switch s.authKind(r) {
|
||||
case "session", "bearer":
|
||||
case "peer":
|
||||
writeJSON(w, http.StatusForbidden, map[string]string{"error": "the peer token only opens /api/v1/peer/export, /api/v1/peer/stream, /api/v1/peer/period and /api/v1/peer/egress"})
|
||||
return
|
||||
default:
|
||||
ip := clientIP(r, s.opt.TrustedProxy)
|
||||
wait := s.lim.blocked(ip)
|
||||
@@ -246,37 +218,6 @@ func (s *Server) slowFail(ip string, d time.Duration) {
|
||||
}
|
||||
}
|
||||
|
||||
type exportCache struct {
|
||||
mu sync.Mutex
|
||||
at map[string]time.Time
|
||||
v map[string][]byte
|
||||
}
|
||||
|
||||
// peerGate admits only the federation token (and --peer-allow sources); it
|
||||
// answers the request itself and returns false when it refuses.
|
||||
func (s *Server) peerGate(w http.ResponseWriter, r *http.Request) bool {
|
||||
if len(s.opt.PeerTokens) == 0 && !s.opt.NoAuth {
|
||||
writeJSON(w, http.StatusNotFound, map[string]string{"error": "federation is not enabled"})
|
||||
return false
|
||||
}
|
||||
if len(s.opt.PeerAllow) > 0 {
|
||||
a, err := netip.ParseAddr(clientIP(r, s.opt.TrustedProxy))
|
||||
if err != nil || !prefixesContain(s.opt.PeerAllow, a) {
|
||||
writeJSON(w, http.StatusForbidden, map[string]string{"error": "source not allowed"})
|
||||
return false
|
||||
}
|
||||
}
|
||||
if !s.opt.NoAuth && s.authKind(r) != "peer" {
|
||||
if strings.HasPrefix(r.Header.Get("Authorization"), "Bearer ") {
|
||||
s.lim.failure(clientIP(r, s.opt.TrustedProxy))
|
||||
}
|
||||
w.Header().Set("WWW-Authenticate", `Bearer realm="urutau-peer"`)
|
||||
writeJSON(w, http.StatusUnauthorized, map[string]string{"error": "unauthorized"})
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// rangeOf resolves the range parameters of a request (range, from, to); it
|
||||
// answers 400 itself and returns false on garbage.
|
||||
func (s *Server) rangeOf(w http.ResponseWriter, r *http.Request) (store.Range, bool) {
|
||||
@@ -289,31 +230,6 @@ func (s *Server) rangeOf(w http.ResponseWriter, r *http.Request) (store.Range, b
|
||||
return rg, true
|
||||
}
|
||||
|
||||
// peerExport: GET /api/v1/peer/export, opened by the peer token.
|
||||
func (s *Server) peerExport(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.peerGate(w, r) {
|
||||
return
|
||||
}
|
||||
rg, ok := s.rangeOf(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
key := rg.Key()
|
||||
s.exp.mu.Lock()
|
||||
if s.exp.v == nil || len(s.exp.v) >= 24 {
|
||||
s.exp.v, s.exp.at = map[string][]byte{}, map[string]time.Time{}
|
||||
}
|
||||
b, ok := s.exp.v[key]
|
||||
if !ok || time.Since(s.exp.at[key]) > 5*time.Second {
|
||||
b, _ = json.Marshal(s.opt.Store.ExportFor(rg))
|
||||
s.exp.v[key], s.exp.at[key] = b, time.Now()
|
||||
}
|
||||
s.exp.mu.Unlock()
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
w.Write(b)
|
||||
}
|
||||
|
||||
func prefixesContain(ps []netip.Prefix, a netip.Addr) bool {
|
||||
a = a.Unmap()
|
||||
for _, p := range ps {
|
||||
@@ -691,72 +607,6 @@ func (s *Server) stream(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// peerStream: GET /api/v1/peer/stream, SSE of this instance's own malicious
|
||||
// events for a hub. Opened only by the peer token, like the export.
|
||||
func (s *Server) peerStream(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.peerGate(w, r) {
|
||||
return
|
||||
}
|
||||
fl, ok := w.(http.Flusher)
|
||||
if !ok {
|
||||
http.Error(w, "streaming unsupported", 500)
|
||||
return
|
||||
}
|
||||
if s.psse.Add(1) > maxPeerSSE {
|
||||
s.psse.Add(-1)
|
||||
w.Header().Set("Retry-After", "10")
|
||||
http.Error(w, "too many peer streams", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
defer s.psse.Add(-1)
|
||||
_ = http.NewResponseController(w).SetWriteDeadline(time.Time{})
|
||||
h := w.Header()
|
||||
h.Set("Content-Type", "text/event-stream")
|
||||
h.Set("Cache-Control", "no-store")
|
||||
h.Set("X-Accel-Buffering", "no")
|
||||
ch, stop := s.opt.Store.Subscribe()
|
||||
defer stop()
|
||||
var visCh <-chan store.VisitMsg // only with ?visits=1; carries no address
|
||||
if r.URL.Query().Get("visits") == "1" {
|
||||
c, stopV := s.opt.Store.SubscribeVisits()
|
||||
defer stopV()
|
||||
visCh = c
|
||||
}
|
||||
fmt.Fprint(w, "retry: 3000\n: connected\n\n")
|
||||
fl.Flush()
|
||||
tick := time.NewTicker(sseHeartbeat)
|
||||
defer tick.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-r.Context().Done():
|
||||
return
|
||||
case v := <-visCh:
|
||||
b, _ := json.Marshal(v)
|
||||
if _, err := fmt.Fprintf(w, "event: visit\ndata: %s\n\n", b); err != nil {
|
||||
return
|
||||
}
|
||||
fl.Flush()
|
||||
case <-tick.C:
|
||||
if !s.opt.NoAuth && s.authKind(r) != "peer" {
|
||||
return
|
||||
}
|
||||
if _, err := fmt.Fprint(w, ": ping\n\n"); err != nil {
|
||||
return
|
||||
}
|
||||
fl.Flush()
|
||||
case e := <-ch:
|
||||
if !core.Category(e.Cat).IsMalicious() {
|
||||
continue
|
||||
}
|
||||
b, _ := json.Marshal(s.opt.Store.Live(e))
|
||||
if _, err := fmt.Fprintf(w, "event: ev\ndata: %s\n\n", b); err != nil {
|
||||
return
|
||||
}
|
||||
fl.Flush()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// liveRecent: GET /api/v1/live/recent?limit=200 (max 500), newest first, local and peers.
|
||||
func (s *Server) liveRecent(w http.ResponseWriter, r *http.Request) {
|
||||
n, _ := strconv.Atoi(r.URL.Query().Get("limit"))
|
||||
|
||||
Reference in new issue
Block a user