package main import "testing" // outboundByTagContains returns the first outbound whose tag contains sub. func outboundByTagContains(cfg map[string]any, sub string) map[string]any { for _, o := range cfg["outbounds"].([]any) { ob := o.(map[string]any) if tag, _ := ob["tag"].(string); tag != "" && contains(tag, sub) { return ob } } return nil } func contains(s, sub string) bool { for i := 0; i+len(sub) <= len(s); i++ { if s[i:i+len(sub)] == sub { return true } } return false } func muxOf(ob map[string]any) map[string]any { if ob == nil { return nil } m, _ := ob["mux"].(map[string]any) return m } func sockoptOf(ob map[string]any) map[string]any { if ob == nil { return nil } ss, _ := ob["streamSettings"].(map[string]any) if ss == nil { return nil } so, _ := ss["sockopt"].(map[string]any) return so } func buildOneNode(t *testing.T, nd Node) map[string]any { t.Helper() m := &Model{Globals: defaultGlobals()} nd.Enabled = true m.Nodes = []Node{nd} m.Rules = []Rule{{Name: "r", Enabled: true, Order: 10, Target: "node:" + nd.Name}} cfg, err := BuildConfig(m) if err != nil { t.Fatalf("BuildConfig: %v", err) } return cfg } func TestNodeMuxEmitted(t *testing.T) { cfg := buildOneNode(t, Node{Name: "n1", URI: vlessReality, Mux: true, MuxConcurrency: 4}) ob := outboundByTagContains(cfg, "node_n1") mux := muxOf(ob) if mux == nil || mux["enabled"] != true { t.Fatalf("mux not enabled: %v", ob) } if mux["concurrency"] != 4 { t.Fatalf("concurrency = %v, want 4", mux["concurrency"]) } } func TestNodeMuxDefaultOff(t *testing.T) { cfg := buildOneNode(t, Node{Name: "n1", URI: vlessReality}) ob := outboundByTagContains(cfg, "node_n1") if _, ok := ob["mux"]; ok { t.Fatalf("mux key present on a mux-off node: %v", ob["mux"]) } } func TestNodeXUDPOnlyVlessVmess(t *testing.T) { // vless: XUDP keys present. cfg := buildOneNode(t, Node{Name: "nv", URI: "vless://11111111-1111-1111-1111-111111111111@a.example.com:443?type=tcp&security=reality&pbk=PBK&sid=aa&sni=www.microsoft.com#nv", Mux: true, XUDPConcurrency: 16, XUDPProxyUDP443: "reject"}) mux := muxOf(outboundByTagContains(cfg, "node_nv")) if mux["xudpConcurrency"] != 16 || mux["xudpProxyUDP443"] != "reject" { t.Fatalf("vless XUDP keys missing: %v", mux) } // trojan: mux present but no XUDP keys. cfg = buildOneNode(t, Node{Name: "nt", URI: "trojan://pw@b.example.com:443?security=tls&sni=b.example.com#nt", Mux: true, XUDPConcurrency: 16, XUDPProxyUDP443: "reject"}) mux = muxOf(outboundByTagContains(cfg, "node_nt")) if mux == nil || mux["enabled"] != true { t.Fatalf("trojan mux not enabled: %v", mux) } if _, ok := mux["xudpConcurrency"]; ok { t.Fatalf("trojan should not carry XUDP keys: %v", mux) } } func TestNodeMuxSkippedForVision(t *testing.T) { uri := "vless://11111111-1111-1111-1111-111111111111@a.example.com:443?type=tcp&security=reality&pbk=PBK&sid=aa&sni=www.microsoft.com&flow=xtls-rprx-vision#nvis" cfg := buildOneNode(t, Node{Name: "nvis", URI: uri, Mux: true}) ob := outboundByTagContains(cfg, "node_nvis") if _, ok := ob["mux"]; ok { t.Fatalf("mux must be skipped under XTLS Vision: %v", ob["mux"]) } } func TestNodeSockoptMarkLoopGuardWins(t *testing.T) { cfg := buildOneNode(t, Node{Name: "n1", URI: vlessReality, Mark: 1234}) so := sockoptOf(outboundByTagContains(cfg, "node_n1")) if so == nil || so["mark"] != loopMark { t.Fatalf("loop-guard mark not enforced, got %v", so) } } func TestNodeSockoptTCPKnobs(t *testing.T) { cfg := buildOneNode(t, Node{Name: "n1", URI: vlessReality, TCPFastOpen: "1", TCPKeepAliveIdle: 30}) so := sockoptOf(outboundByTagContains(cfg, "node_n1")) if so["tcpFastOpen"] != true || so["tcpKeepAliveIdle"] != 30 { t.Fatalf("tcp knobs missing: %v", so) } if so["mark"] != loopMark { t.Fatalf("loop mark lost alongside tcp knobs: %v", so) } }