package server import ( "net/http" "strings" "github.com/Autumn-27/artex/traffic" ) // GET /api/traffic/flows func (s *Server) getTrafficFlows(w http.ResponseWriter, r *http.Request) { tr := s.m.Traffic() if tr == nil { writeJSON(w, 200, map[string]any{"enabled": false, "store_error_traffic": s.storeErrorTrafficEnabled(), "flows": []any{}, "total": 0, "count": 0}) return } q := r.URL.Query() page := atoiDefault(q.Get("page"), 0) size := atoiDefault(q.Get("size"), 100) rawTaskID := strings.TrimSpace(q.Get("task_id")) if rawTaskID != "" && s.m.ResolveTask(rawTaskID) == nil { writeErr(w, http.StatusNotFound, "task not found") return } rows, total, err := tr.PageNetFlows(traffic.NetFlowQuery{ Protocol: q.Get("protocol"), Action: q.Get("action"), OwnerTaskID: rawTaskID, SandboxScope: q.Get("scope"), SandboxOwnerID: q.Get("owner_id"), ContainerID: q.Get("container_id"), Query: q.Get("q"), }, page, size) if err != nil { writeErr(w, http.StatusInternalServerError, err.Error()) return } count, _ := tr.CountNetFlows() if rawTaskID != "" { count = total } writeJSON(w, 200, map[string]any{ "enabled": s.m.TrafficEnabled(), "store_error_traffic": s.storeErrorTrafficEnabled(), "count": count, "total": total, "page": page, "size": size, "flows": rows, }) } // GET /api/traffic/flows/{id} func (s *Server) getTrafficFlowDetail(w http.ResponseWriter, r *http.Request) { tr := s.m.Traffic() if tr == nil { writeErr(w, http.StatusNotFound, "traffic disabled") return } id := strings.TrimSpace(r.PathValue("id")) if id == "" { writeErr(w, http.StatusBadRequest, "missing id") return } flow, err := tr.GetNetFlow(id) if err != nil { writeErr(w, http.StatusNotFound, err.Error()) return } writeJSON(w, 200, flow) } // DELETE /api/traffic/flows func (s *Server) deleteTrafficFlows(w http.ResponseWriter, r *http.Request) { tr := s.m.Traffic() if tr == nil { writeErr(w, http.StatusNotFound, "traffic disabled") return } q := r.URL.Query() scope := strings.TrimSpace(q.Get("scope")) ownerID := strings.TrimSpace(q.Get("owner_id")) if tid := strings.TrimSpace(q.Get("task_id")); tid != "" && scope == "" { scope = "task" ownerID = tid } n, err := tr.DeleteNetFlows(scope, ownerID) if err != nil { writeErr(w, http.StatusInternalServerError, err.Error()) return } writeJSON(w, 200, map[string]any{"deleted": n}) } // GET /api/traffic/dns func (s *Server) getTrafficDNS(w http.ResponseWriter, r *http.Request) { tr := s.m.Traffic() if tr == nil { writeJSON(w, 200, map[string]any{"enabled": false, "store_error_traffic": s.storeErrorTrafficEnabled(), "queries": []any{}, "total": 0, "count": 0}) return } q := r.URL.Query() page := atoiDefault(q.Get("page"), 0) size := atoiDefault(q.Get("size"), 100) rawTaskID := strings.TrimSpace(q.Get("task_id")) if rawTaskID != "" && s.m.ResolveTask(rawTaskID) == nil { writeErr(w, http.StatusNotFound, "task not found") return } rows, total, err := tr.PageDNSQueries(traffic.DNSQueryFilter{ QType: q.Get("qtype"), RCode: q.Get("rcode"), Action: q.Get("action"), OwnerTaskID: rawTaskID, SandboxScope: q.Get("scope"), SandboxOwnerID: q.Get("owner_id"), ContainerID: q.Get("container_id"), Query: q.Get("q"), }, page, size) if err != nil { writeErr(w, http.StatusInternalServerError, err.Error()) return } count, _ := tr.CountDNSQueries() if rawTaskID != "" { count = total } writeJSON(w, 200, map[string]any{ "enabled": s.m.TrafficEnabled(), "store_error_traffic": s.storeErrorTrafficEnabled(), "count": count, "total": total, "page": page, "size": size, "queries": rows, }) } // DELETE /api/traffic/dns func (s *Server) deleteTrafficDNS(w http.ResponseWriter, r *http.Request) { tr := s.m.Traffic() if tr == nil { writeErr(w, http.StatusNotFound, "traffic disabled") return } q := r.URL.Query() scope := strings.TrimSpace(q.Get("scope")) ownerID := strings.TrimSpace(q.Get("owner_id")) if tid := strings.TrimSpace(q.Get("task_id")); tid != "" && scope == "" { scope = "task" ownerID = tid } n, err := tr.DeleteDNSQueries(scope, ownerID) if err != nil { writeErr(w, http.StatusInternalServerError, err.Error()) return } writeJSON(w, 200, map[string]any{"deleted": n}) }