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.
This commit is contained in:
ji ruo committed 2026-12-02 02:51:36 +08:00
1 parent 9c43e694a3
commit d786de90af
69 files changed
+13082 -1433

No files matched your search

+168
View File
@@ -0,0 +1,168 @@
package server
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/Autumn-27/artex/traffic"
)
func TestGatewaySyncAndNonHTTPTrafficAPIs(t *testing.T) {
t.Setenv("ARTEX_GATEWAY_TOKEN", "test-gw-token-218")
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()
// 1. Unauthorized gateway policy request must be rejected with 401.
reqUnauth := httptest.NewRequest(http.MethodGet, "/api/internal/gateway/policy", nil)
recUnauth := httptest.NewRecorder()
s.getGatewayPolicy(recUnauth, reqUnauth)
if recUnauth.Code != http.StatusUnauthorized {
t.Fatalf("expected 401 for missing token, got %d", recUnauth.Code)
}
// 2. Configure per-container proxy route + gateway settings.
putRouteBody := `{"mode":"proxy","proxy_url":"socks5://127.0.0.1:1080","note":"task 5 socks5"}`
reqRoute := httptest.NewRequest(http.MethodPut, "/api/proxy/routes/task/5", bytes.NewBufferString(putRouteBody))
reqRoute.SetPathValue("scope", "task")
reqRoute.SetPathValue("owner_id", "5")
recRoute := httptest.NewRecorder()
s.putProxyRoute(recRoute, reqRoute)
if recRoute.Code != http.StatusOK {
t.Fatalf("putProxyRoute failed: %d %s", recRoute.Code, recRoute.Body.String())
}
reqGW := httptest.NewRequest(http.MethodPut, "/api/proxy/gateway/settings", bytes.NewBufferString(`{"udp_policy":"block","dns_upstream":"1.1.1.1:53"}`))
recGW := httptest.NewRecorder()
s.updateGatewaySettings(recGW, reqGW)
if recGW.Code != http.StatusOK {
t.Fatalf("updateGatewaySettings failed: %d %s", recGW.Code, recGW.Body.String())
}
// 3. Authorized gateway policy fetch returns route & settings.
reqPol := httptest.NewRequest(http.MethodGet, "/api/internal/gateway/policy?gateway_id=gw-test", nil)
reqPol.Header.Set("X-Artex-Gateway-Token", "test-gw-token-218")
recPol := httptest.NewRecorder()
s.getGatewayPolicy(recPol, reqPol)
if recPol.Code != http.StatusOK {
t.Fatalf("getGatewayPolicy failed: %d %s", recPol.Code, recPol.Body.String())
}
var snap traffic.GatewayPolicySnapshot
if err := json.Unmarshal(recPol.Body.Bytes(), &snap); err != nil {
t.Fatalf("decode policy snapshot: %v", err)
}
if snap.UDPPolicy != traffic.UDPPolicyBlock || snap.DNSUpstream != "1.1.1.1:53" {
t.Fatalf("unexpected gateway policy: %+v", snap)
}
if r, ok := snap.ContainerRoutes["task:5"]; !ok || r.EffectiveURL != "socks5://127.0.0.1:1080" {
t.Fatalf("expected task:5 route in snapshot, got %+v", snap.ContainerRoutes)
}
// 4. Ingest gateway events (net_flows + dns_queries + block_events).
batch := traffic.GatewayEventBatch{
GatewayID: "gw-test",
NetFlows: []traffic.NetFlowRecord{{
ID: "nf-test-1",
TS: time.Now().Unix(),
Protocol: "tcp",
DstIP: "10.0.0.8",
DstPort: 3306,
DstHost: "db.target.local",
Action: "allow",
BytesTX: 64,
BytesRX: 128,
TXPreview: []byte("SELECT 1"),
RXPreview: []byte("5.7.40-mysql"),
SandboxScope: "task",
SandboxOwnerID: "5",
SrcIP: "172.30.0.5",
}},
DNSQueries: []traffic.DNSQueryRecord{{
ID: "dns-test-1",
TS: time.Now().Unix(),
QName: "db.target.local",
QType: "A",
RCode: "NOERROR",
Answers: []string{"10.0.0.8"},
Action: "allow",
Upstream: "direct:1.1.1.1:53",
SandboxScope: "task",
SandboxOwnerID: "5",
SrcIP: "172.30.0.5",
}},
}
rawBatch, _ := json.Marshal(batch)
reqEv := httptest.NewRequest(http.MethodPost, "/api/internal/gateway/events", bytes.NewReader(rawBatch))
reqEv.Header.Set("X-Artex-Gateway-Token", "test-gw-token-218")
recEv := httptest.NewRecorder()
s.postGatewayEvents(recEv, reqEv)
if recEv.Code != http.StatusOK {
t.Fatalf("postGatewayEvents failed: %d %s", recEv.Code, recEv.Body.String())
}
// 5. Query flows + flow detail + DNS via public API handlers.
recFlows := httptest.NewRecorder()
s.getTrafficFlows(recFlows, httptest.NewRequest(http.MethodGet, "/api/traffic/flows?protocol=tcp", nil))
if recFlows.Code != http.StatusOK {
t.Fatalf("getTrafficFlows: %d %s", recFlows.Code, recFlows.Body.String())
}
var flowsResp struct {
Total int `json:"total"`
Flows []traffic.NetFlowRecord `json:"flows"`
}
_ = json.Unmarshal(recFlows.Body.Bytes(), &flowsResp)
if flowsResp.Total != 1 || len(flowsResp.Flows) != 1 || flowsResp.Flows[0].DstPort != 3306 {
t.Fatalf("unexpected flowsResp: %+v", flowsResp)
}
reqDet := httptest.NewRequest(http.MethodGet, "/api/traffic/flows/nf-test-1", nil)
reqDet.SetPathValue("id", "nf-test-1")
recDet := httptest.NewRecorder()
s.getTrafficFlowDetail(recDet, reqDet)
if recDet.Code != http.StatusOK || !bytes.Contains(recDet.Body.Bytes(), []byte("SELECT 1")) {
t.Fatalf("unexpected flow detail: %d %s", recDet.Code, recDet.Body.String())
}
recDNS := httptest.NewRecorder()
s.getTrafficDNS(recDNS, httptest.NewRequest(http.MethodGet, "/api/traffic/dns?q=db.target", nil))
if recDNS.Code != http.StatusOK {
t.Fatalf("getTrafficDNS: %d %s", recDNS.Code, recDNS.Body.String())
}
var dnsResp struct {
Total int `json:"total"`
Queries []traffic.DNSQueryRecord `json:"queries"`
}
_ = json.Unmarshal(recDNS.Body.Bytes(), &dnsResp)
if dnsResp.Total != 1 || len(dnsResp.Queries) != 1 || dnsResp.Queries[0].QName != "db.target.local" {
t.Fatalf("unexpected dnsResp: %+v", dnsResp)
}
// 6. GET /api/proxy/routes reflects gateway connected=true and task:5 override.
recRoutes := httptest.NewRecorder()
s.getProxyRoutes(recRoutes, httptest.NewRequest(http.MethodGet, "/api/proxy/routes", nil))
if recRoutes.Code != http.StatusOK {
t.Fatalf("getProxyRoutes: %d %s", recRoutes.Code, recRoutes.Body.String())
}
body := recRoutes.Body.String()
if !bytes.Contains([]byte(body), []byte(`"connected":true`)) || !bytes.Contains([]byte(body), []byte(`"socks5://127.0.0.1:1080"`)) {
t.Fatalf("unexpected getProxyRoutes body: %s", body)
}
if bytes.Contains([]byte(body), []byte("全局")) {
t.Fatalf("getProxyRoutes must not hardcode Chinese labels in API response: %s", body)
}
}