Files
urutau/internal/server/api_egress.go
T
Lucas RayanandClaude Opus 5.5 c33cca22e3
CI / Docker image (build and smoke test) (push) Skipped
CI / Format and vet (push) Successful in 28s
CI / Dependency audit (push) Successful in 16s
CI / Tests (Linux, race detector) (push) Successful in 1m3s
Release / Plan (reuse the published image?) (push) Successful in 4s
Release / ci (push) Skipped
Release / Build image (amd64) (push) Skipped
Release / Build image (arm64) (push) Skipped
Release / Publish tags (push) Successful in 13s
Release / Gitea release (push) Successful in 3s
feat: 1.4.0 second firewall, egress analysis and HTML report
Optional second firewall (URUTAU_ENFORCE off/observe/on) through the host
fail2ban, outbound traffic analysis (beaconing, mining, spam, DNS bypass,
unauthorised scans) with authorised scanner workloads, host kit in
deploy/host and a printable HTML report.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 23:35:34 -03:00

102 lines
2.6 KiB
Go

package server
import (
"encoding/json"
"net/http"
"sort"
"sync/atomic"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/egress"
)
func init() {
registerUserRoute("GET /api/v1/egress", func(s *Server) http.HandlerFunc { return s.egressAPI })
}
var egressEngine atomic.Pointer[egress.Engine]
// SetEgress attaches the egress engine (nil disables the endpoints). It is
// called by the daemon wiring, before or after the server starts.
func SetEgress(e *egress.Engine) { egressEngine.Store(e) }
type egressOut struct {
Enabled bool `json:"enabled"`
Source egress.SourceInfo `json:"source"`
egress.Report
}
// egressAPI: GET /api/v1/egress?range=24h&host=all|<name>. On a hub the
// local engine and the peers' reports are merged, items carrying "host".
func (s *Server) egressAPI(w http.ResponseWriter, r *http.Request) {
rg, ok := s.rangeOf(w, r)
if !ok {
return
}
host := r.URL.Query().Get("host")
if len(host) > 253 {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "bad host"})
return
}
local, peers, only, ok := s.studyHosts(w, host)
if !ok {
return
}
from, to := rg.Bounds(s.opt.Store.Now().Unix())
eng := egressEngine.Load()
out := egressOut{Source: egress.SourceInfo{State: "disabled"}}
if eng != nil {
out.Source = eng.Info()
out.Enabled = true
}
rep := egress.EmptyReport()
if local && eng != nil {
rep = eng.Report(from, to)
}
if peers && s.opt.Hub != nil {
merged := egress.EmptyReport()
merged.Timeline.Start = rep.Timeline.Start
if local && eng != nil {
rep.Stamp(s.opt.Store.Name())
merged.Merge(rep, s.opt.Store.Name())
}
raws := s.opt.Hub.EgressFor(r.Context(), from, to, only)
names := make([]string, 0, len(raws))
for n := range raws {
names = append(names, n)
}
sort.Strings(names)
for _, n := range names {
var pr egress.Report
if json.Unmarshal(raws[n], &pr) != nil {
continue
}
pr.Sanitize(n)
merged.Merge(pr, n)
out.Enabled = true
}
rep = merged
}
out.Report = rep
writeJSON(w, 200, out)
}
// peerEgress: GET /api/v1/peer/egress, the local report a hub merges; opened
// only by the peer token (and --peer-allow). It must be registered next to
// the other peer routes.
func (s *Server) peerEgress(w http.ResponseWriter, r *http.Request) {
if !s.peerGate(w, r) {
return
}
eng := egressEngine.Load()
if eng == nil {
writeJSON(w, http.StatusNotFound, map[string]string{"error": "egress is not enabled"})
return
}
rg, ok := s.rangeOf(w, r)
if !ok {
return
}
from, to := rg.Bounds(s.opt.Store.Now().Unix())
writeJSON(w, 200, eng.Report(from, to))
}