package server import ( "encoding/json" "errors" "net/http" "net/netip" "strings" "sync/atomic" "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) } 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, "allows": []enforce.Allow{}, "unban_log_writable": false, "coverage": enforce.Coverage{}, "counts": enforce.Counts{Skipped: map[string]int{}}, "recent": []enforce.Recent{}} 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() 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 } writeJSON(w, http.StatusOK, out) } }) 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 }) } const maxUnbanBody = 1 << 10 func (s *Server) enforceUnban(w http.ResponseWriter, r *http.Request) { if !sameOrigin(r) { writeJSON(w, http.StatusForbidden, map[string]string{"error": "cross-site request refused"}) 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() if err := dec.Decode(&in); err != nil || dec.More() { writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid JSON body"}) return } a, err := netip.ParseAddr(strings.TrimSpace(in.IP)) 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 } 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) { if !sameOrigin(r) { writeJSON(w, http.StatusForbidden, map[string]string{"error": "cross-site request refused"}) return } 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"}) return } if !e.EndAllow(a) { writeJSON(w, http.StatusNotFound, map[string]string{"error": "no allowance for this address"}) return } writeJSON(w, http.StatusOK, map[string]bool{"ok": true}) }