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.
This commit is contained in:
ji ruo committed 2026-12-02 15:17:42 +08:00
1 parent 07309e4530
commit d5926d6420
22 files changed
+1189 -101

No files matched your search

+111
View File
@@ -166,3 +166,114 @@ func TestGatewaySyncAndNonHTTPTrafficAPIs(t *testing.T) {
t.Fatalf("getProxyRoutes must not hardcode Chinese labels in API response: %s", body)
}
}
func TestStoreErrorTrafficPolicyPartialUpdateAndIngestion(t *testing.T) {
t.Setenv("ARTEX_GATEWAY_TOKEN", "test-gw-token-err")
tr, err := traffic.Open(t.TempDir(), "127.0.0.1:0")
if err != nil {
t.Fatalf("open traffic: %v", err)
}
defer tr.Close()
s := &Server{
m: &Manager{
traffic: tr,
tasks: map[string]*Task{},
},
}
s.initProxyRoutesAndGateway()
// Set UDP & DNS first; store_error_traffic remains default false.
recSet := httptest.NewRecorder()
s.updateGatewaySettings(recSet, httptest.NewRequest(http.MethodPut, "/api/proxy/gateway/settings", bytes.NewBufferString(`{"udp_policy":"direct","dns_upstream":"9.9.9.9:53"}`)))
if recSet.Code != http.StatusOK {
t.Fatalf("updateGatewaySettings initial failed: %d %s", recSet.Code, recSet.Body.String())
}
// Ingest an error flow & an allow flow while store_error_traffic=false.
rawBatch, _ := json.Marshal(traffic.GatewayEventBatch{
GatewayID: "gw-err",
NetFlows: []traffic.NetFlowRecord{
{ID: "nf-ok", TS: time.Now().Unix(), Protocol: "tcp", DstIP: "1.1.1.1", DstPort: 80, Action: "allow"},
{ID: "nf-err-1", TS: time.Now().Unix(), Protocol: "tcp", DstIP: "1.1.1.2", DstPort: 81, Action: "error", Error: "refused"},
},
DNSQueries: []traffic.DNSQueryRecord{
{ID: "dns-ok", TS: time.Now().Unix(), QName: "ok.test", QType: "A", Action: "allow"},
{ID: "dns-err-1", TS: time.Now().Unix(), QName: "err.test", QType: "A", Action: "error", RCode: "SERVFAIL"},
},
})
reqEv := httptest.NewRequest(http.MethodPost, "/api/internal/gateway/events", bytes.NewReader(rawBatch))
reqEv.Header.Set("X-Artex-Gateway-Token", "test-gw-token-err")
recEv := httptest.NewRecorder()
s.postGatewayEvents(recEv, reqEv)
if recEv.Code != http.StatusOK {
t.Fatalf("postGatewayEvents failed: %d %s", recEv.Code, recEv.Body.String())
}
// Verify only non-error rows were persisted and GET /api/traffic/flows reports store_error_traffic=false.
recFlows := httptest.NewRecorder()
s.getTrafficFlows(recFlows, httptest.NewRequest(http.MethodGet, "/api/traffic/flows", nil))
var flowsResp struct {
StoreErrorTraffic bool `json:"store_error_traffic"`
Total int `json:"total"`
Flows []traffic.NetFlowRecord `json:"flows"`
}
_ = json.Unmarshal(recFlows.Body.Bytes(), &flowsResp)
if flowsResp.StoreErrorTraffic || flowsResp.Total != 1 || flowsResp.Flows[0].ID != "nf-ok" {
t.Fatalf("expected store_error_traffic=false and 1 allow flow, got %+v", flowsResp)
}
// Partial update from Traffic UI toggling only store_error_traffic=true must preserve udp_policy & dns_upstream.
recToggle := httptest.NewRecorder()
s.updateGatewaySettings(recToggle, httptest.NewRequest(http.MethodPut, "/api/proxy/gateway/settings", bytes.NewBufferString(`{"store_error_traffic":true}`)))
if recToggle.Code != http.StatusOK {
t.Fatalf("partial toggle failed: %d %s", recToggle.Code, recToggle.Body.String())
}
var updated GatewaySettings
_ = json.Unmarshal(recToggle.Body.Bytes(), &updated)
if !updated.StoreErrorTraffic || updated.UDPPolicy != traffic.UDPPolicyDirect || updated.DNSUpstream != "9.9.9.9:53" {
t.Fatalf("partial update clobbered fields: %+v", updated)
}
// Gateway policy snapshot reflects store_error_traffic=true.
reqPol := httptest.NewRequest(http.MethodGet, "/api/internal/gateway/policy", nil)
reqPol.Header.Set("X-Artex-Gateway-Token", "test-gw-token-err")
recPol := httptest.NewRecorder()
s.getGatewayPolicy(recPol, reqPol)
var snap traffic.GatewayPolicySnapshot
_ = json.Unmarshal(recPol.Body.Bytes(), &snap)
if !snap.StoreErrorTraffic {
t.Fatalf("expected gateway policy snapshot store_error_traffic=true, got %+v", snap)
}
// Subsequent error flow & DNS query are persisted.
rawBatch2, _ := json.Marshal(traffic.GatewayEventBatch{
GatewayID: "gw-err",
NetFlows: []traffic.NetFlowRecord{
{ID: "nf-err-2", TS: time.Now().Unix(), Protocol: "tcp", DstIP: "1.1.1.2", DstPort: 81, Action: "error", Error: "refused"},
},
DNSQueries: []traffic.DNSQueryRecord{
{ID: "dns-err-2", TS: time.Now().Unix(), QName: "err.test", QType: "A", Action: "error", RCode: "SERVFAIL"},
},
})
reqEv2 := httptest.NewRequest(http.MethodPost, "/api/internal/gateway/events", bytes.NewReader(rawBatch2))
reqEv2.Header.Set("X-Artex-Gateway-Token", "test-gw-token-err")
recEv2 := httptest.NewRecorder()
s.postGatewayEvents(recEv2, reqEv2)
if recEv2.Code != http.StatusOK {
t.Fatalf("postGatewayEvents 2 failed: %d %s", recEv2.Code, recEv2.Body.String())
}
recFlows2 := httptest.NewRecorder()
s.getTrafficFlows(recFlows2, httptest.NewRequest(http.MethodGet, "/api/traffic/flows?action=error", nil))
var flowsResp2 struct {
StoreErrorTraffic bool `json:"store_error_traffic"`
Total int `json:"total"`
Flows []traffic.NetFlowRecord `json:"flows"`
}
_ = json.Unmarshal(recFlows2.Body.Bytes(), &flowsResp2)
if !flowsResp2.StoreErrorTraffic || flowsResp2.Total != 1 || flowsResp2.Flows[0].ID != "nf-err-2" {
t.Fatalf("expected error flow stored after toggle, got %+v", flowsResp2)
}
}