package server import ( "net/http" "strings" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/ingress" ) func init() { registerUserRoute("GET /api/v1/ingress", func(s *Server) http.HandlerFunc { return s.ingressAPI }) } type ingressOut struct { Hosts []string `json:"hosts"` ingress.Report } // ingressAPI: GET /api/v1/ingress?range=24h&host=all|[,]. The // firewall telemetry of the hosts that send it: exact totals from the host kit // counters, the details (ports, origins, banned share) from its sampled log. // Only the probes of this hub are covered (the telemetry is not relayed to // peers). func (s *Server) ingressAPI(w http.ResponseWriter, r *http.Request) { rg, ok := s.rangeOf(w, r) if !ok { return } host := strings.TrimSpace(r.URL.Query().Get("host")) if len(host) > 64*maxHostSel { writeJSON(w, http.StatusBadRequest, map[string]string{"error": "bad host"}) return } var all []string if s.opt.Nodes != nil { all = s.opt.Nodes.Hosts() } var names []string switch host { case "", "all": names = all default: for _, n := range strings.Split(host, ",") { n = strings.TrimSpace(n) if n == "" { continue } if !contains(all, n) { writeJSON(w, http.StatusNotFound, map[string]string{"error": "unknown host"}) return } names = append(names, n) } } aggs := make([]*ingress.Agg, 0, len(names)) for _, n := range names { if a := s.opt.Nodes.Ingress(n); a != nil { aggs = append(aggs, a) } } from, to := rg.Bounds(s.opt.Store.Now().Unix()) if names == nil { names = []string{} } writeJSON(w, 200, ingressOut{Hosts: names, Report: ingress.ReportOf(aggs, from, to)}) } func contains(l []string, s string) bool { for _, x := range l { if x == s { return true } } return false }