Release / Publish tags (push) Successful in 18s
Release / Plan (reuse the published image?) (push) Successful in 5s
Release / ci (push) Successful in 1m47s
Release / Build image (amd64) (push) Successful in 21s
Release / Build image (arm64) (push) Successful in 28s
CI / Format and vet (push) Successful in 23s
CI / Tests (Linux, race detector) (push) Successful in 1m24s
CI / Dependency audit (push) Successful in 24s
CI / Docker image (build and smoke test) (push) Skipped
Release / Gitea release (push) Successful in 5s
Backend pagination with one pager for sessions, IPs, campaigns and events; network probe source (port-scan, T1046, second firewall reason port-scan); host kit logs inbound probes; urutau call on the report cover. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
101 lines
2.6 KiB
Go
101 lines
2.6 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
|
|
}
|
|
out := store.CampaignsOf(p)
|
|
page, per := pageParams(r.URL.Query())
|
|
from, to := window(len(out.Campaigns), page, per)
|
|
out.Campaigns = out.Campaigns[from:to]
|
|
writeJSON(w, 200, struct {
|
|
store.CampaignsOut
|
|
pageMeta
|
|
}{out, metaOf(out.Total, page, per)})
|
|
}
|
|
})
|
|
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
|
|
}
|