Adds optional per-node multiplexing and socket options, applied after
ParseShareLink at all three outbound build sites (group members, standalone
node targets, chain hops) and BEFORE withMark so the loop-guard escape mark
(255) is always re-asserted last and wins — a per-node mark that is neither 0
nor 255 is refused (would break the nft escape rule → routing loop).
- model.go: Node.{Mux,MuxConcurrency,XUDPConcurrency,XUDPProxyUDP443,Mark,
TCPFastOpen,TCPKeepAliveIdle}; uci.go parses them. All zero-value = off, so
existing nodes emit byte-identical outbounds.
- generate.go: applyNodeOpts() emits outbound-level `mux` (XUDP keys only for
vless/vmess) + streamSettings.sockopt tcp knobs; skips mux under XTLS Vision.
- nodes.js: modalonly mux/XUDP/sockopt fields (depends on mux).
- tests: node_opts_test.go (mux emitted/off, XUDP gating, vision skip,
loop-guard mark wins, tcp knobs).
Verified live on the 512M VM: mux node emits {enabled,concurrency:8,
xudpConcurrency:16,xudpProxyUDP443:reject}; sockopt.mark==255 preserved;
xray -test OK. r9.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
123 lines
3.8 KiB
Go
123 lines
3.8 KiB
Go
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)
|
|
}
|
|
}
|