Files
ARTEX-chixuweihu/server/traffic_flows.go
T
ji ruo d786de90af feat(traffic,sandbox): add TUN/netstack transparent gateway, egress controls, CAP_NET_RAW hardening, and i18n fixes
- Implement full-traffic transparent gateway (cmd/artex-gateway, traffic/netstack.go, traffic/transparent.go) using Linux TUN + gVisor netstack deferred TCP handshakes (eliminating nmap/nc false-positive open ports), real ICMPv4 Echo proxying, UDP/DNS interception, and per-container upstream proxy overrides.
- Add egress blacklist + asset-intercept sync, non-HTTP flow/DNS SQLite persistence and APIs, and /system/proxy & /function/traffic TCP/UDP/DNS UI panels.
- Fix sandbox Playwright MCP browser loopback --proxy-server rewrite and NODE_EXTRA_CA_CERTS override in server/mcpstdio.go.
- Strip all SUID/SGID binaries in Dockerfile.sandbox, grant minimal cap_net_raw+ep to nmap/nping/ping with ARTEX_SANDBOX_ALLOW_NET_RAW toggle, and enforce IPv4/IPv6 anti-spoofing in iptables and netstack.
- Remove hardcoded Chinese "(全局)" from GET /api/proxy/routes, localize ContainerRoutesTab / NonHTTPTrafficPanel labels, and expose reactive locale/isEn from useLiveTranslation for React Compiler compatibility.
2026-12-02 02:51:36 +08:00

158 lines
4.1 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, "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(),
"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, "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(),
"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})
}