package server import ( "encoding/json" "errors" "io" "net/http" "strconv" "sync/atomic" "time" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/enforce" ) // enforceEngine is the optional second firewall (nil when --enforce is off). 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) { rg, ok := s.rangeOf(w, r) if !ok { return } now := s.opt.Now() from := now.Add(-rg.Dur).Unix() if rg.From > 0 { from = rg.From } out := map[string]any{"mode": enforce.ModeOff, "share": "off", "threshold": 0, "log_writable": false, "coverage": enforce.Coverage{}, "counts": enforce.Counts{Skipped: map[string]int{}}, "recent": []enforce.Recent{}} if e := enforceEngine.Load(); 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["coverage"], out["counts"], out["recent"] = rep.Coverage, rep.Counts, rep.Recent } writeJSON(w, http.StatusOK, out) } }) peerRoutes = append(peerRoutes, 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}) } }}, ) }