Files
ARTEX-chixuweihu/server/traffic_flows.go
T
ji ruo d5926d6420 fix(traffic,server,sandbox): fix TCP/53 false positives, add error-traffic storage toggle, and fix spawn_task/sandbox polling
- Route external TCP/53 through netstack deferred handshake and real-target DNS inspection (RFC 7766 single-write framing) instead of hijacking to host DNS.
- Preserve explicit targetServer in ProcessDNSQueryToTarget under upstream proxy so external UDP/53 probes never fall back to host DNSUpstream.
- Default error traffic storage (net_flows/dns_queries action=error) to off with hot-toggle controls synced across gateway, Proxy & Egress, and Traffic pages.
- Restore json.Unmarshal in spawn_task orchestration tool and poll live sandbox container state in chat/task controls.
2026-12-02 15:17:42 +08:00

160 lines
4.3 KiB
Go

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})
}