Release / Publish tags (push) Successful in 16s
Release / Plan (reuse the published image?) (push) Successful in 4s
Release / ci (push) Successful in 1m47s
Release / Build image (amd64) (push) Successful in 21s
Release / Build image (arm64) (push) Successful in 23s
CI / Format and vet (push) Successful in 27s
CI / Tests (Linux, race detector) (push) Successful in 1m20s
CI / Dependency audit (push) Successful in 31s
CI / Docker image (build and smoke test) (push) Skipped
Release / Gitea release (push) Successful in 5s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
194 lines
6.6 KiB
Go
194 lines
6.6 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"net/netip"
|
|
"strconv"
|
|
"strings"
|
|
"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,
|
|
"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 {
|
|
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())
|
|
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 })
|
|
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
|
|
|
|
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
|
|
}
|
|
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"`
|
|
}
|
|
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 {
|
|
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid ip 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})
|
|
}
|
|
}
|
|
|
|
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 := enforceEngine.Load()
|
|
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})
|
|
}
|