Files
urutau/internal/server/api_campaigns.go
T
Lucas RayanandClaude Opus 5.5 2ebda1e4d7
Release / Plan (reuse the published image?) (push) Successful in 3s
CI / Docker image (build and smoke test) (push) Skipped
CI / Format and vet (push) Successful in 28s
CI / Dependency audit (push) Successful in 20s
CI / Tests (Linux, race detector) (push) Successful in 1m10s
Release / ci (push) Successful in 1m10s
Release / Build image (arm64) (push) Successful in 55s
Release / Build image (amd64) (push) Successful in 1m16s
Release / Publish tags (push) Successful in 14s
Release / Gitea release (push) Skipped
feat: 1.3.0 thirteen new pages, charts, tooltips and error analysis
New pages grouped in the rail (Monitor, Investigate, Analyze, Operate,
Reports): Services (SLO, Apdex, latency app vs proxy), Sessions (stage
funnel), Campaigns, Event search, MITRE ATT&CK matrix, Evolution,
Concentration (Pareto, Gini), Survival (Kaplan-Meier per attack
episode, recidivists), Where errors come from (application vs failed
attack vs proxy vs client), Protocols and TLS, Defense (fail2ban, SSH,
blocklist), Integrity, and a printable executive report.

Chart library (donut, radar, stacked area, bars, heatmap, gauge),
glossary with accessible "?" tooltips, self-explanatory category names
(nonexistent page 404), page loader through pages.json.

Unique visitors fixed: real browsers opening a page, one per IP per day,
deduplicated across domains, with non-human clients counted apart.
Anonymous legitimate visits on the Live page (?visits=1, no IP).
Traefik origin, service, protocol and TLS fields parsed. Paths and
Referer paths drop e-mail and token segments.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 22:39:33 -03:00

94 lines
2.4 KiB
Go

package server
import (
"net/http"
"regexp"
"strconv"
"time"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store"
)
var campaignIDRe = regexp.MustCompile(`^[A-Za-z0-9_-]{1,64}$`)
func init() {
registerUserRoute("GET /api/v1/campaigns", func(s *Server) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
p, ok := s.periodStudy(w, r)
if !ok {
return
}
writeJSON(w, 200, store.CampaignsOf(p))
}
})
registerUserRoute("GET /api/v1/campaigns/{id}", func(s *Server) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
if !campaignIDRe.MatchString(id) {
badRequest(w, "invalid campaign id")
return
}
p, ok := s.periodStudy(w, r)
if !ok {
return
}
d, found := store.CampaignOf(p, id)
if !found {
writeJSON(w, http.StatusNotFound, map[string]string{"error": "unknown campaign"})
return
}
writeJSON(w, 200, d)
}
})
}
// periodStudy runs the same study as /api/v1/period (same filters, user
// semaphore, peers merged) and answers the errors itself.
func (s *Server) periodStudy(w http.ResponseWriter, r *http.Request) (store.PeriodOut, bool) {
eq, ok := s.eventFilter(w, r, 24*time.Hour)
if !ok {
return store.PeriodOut{}, false
}
if !s.userSlot(w) {
return store.PeriodOut{}, false
}
defer func() { <-s.user }()
pq := store.PeriodQuery{From: eq.From, To: eq.To, IP: eq.IP, Cat: eq.Cat, Fam: eq.Fam}
if v := r.URL.Query().Get("gap"); v != "" {
n, err := strconv.ParseInt(v, 10, 64)
if err != nil || n < 60 || n > 86400 {
badRequest(w, "gap must be 60..86400 seconds")
return store.PeriodOut{}, false
}
pq.Gap = n
}
local, peers, only, ok := s.studyHosts(w, eq.Host)
if !ok {
return store.PeriodOut{}, false
}
var parts []store.PeriodOut
var studies map[string]store.PeerStudy
if peers {
studies = s.opt.Hub.StudiesFor(r.Context(), pq, only)
}
if local || len(studies) == 0 {
lq := pq
switch {
case !local:
lq.Host = only
case s.opt.Hub != nil:
lq.Host = s.opt.Store.Name()
}
parts = append(parts, s.opt.Store.Period(lq))
}
for _, p := range s.opt.Hub.PeerNames() {
if st, ok := studies[p]; ok {
parts = append(parts, st.Period)
}
}
out := store.MergePeriods(parts)
rg, _ := s.opt.Store.ResolveRange(r.URL.Query().Get("range"), r.URL.Query().Get("from"), r.URL.Query().Get("to"))
out.Range = rg.Info(s.opt.Now().Unix())
return out, true
}