Files
3x-ui/internal/util/maskcompat/udphop_test.go
T
MHSanaei ec0a57fc4f fix(finalmask): hop client UDP through the udphop mask
xray-core 26.9.9 dropped finalmask.quicParams.udpHop for the client-only
"udphop" UDP mask {mode, interval, remoteIPs, remotePorts}. The panel kept
writing the old key, so hysteria/xhttp outbounds and JSON-subscription
clients silently stopped hopping ports.

An inbound keeps quicParams.udpHop: it is panel metadata advertised as the
link's mport, and a udphop mask on a listener fails with "udphop: client
only". The conversion (maskcompat.UpgradeLegacyUDPHop, intervalRemote mode)
runs only where a client config is produced:

- UDPHopClientMaskFix seeder over hosts, sub-JSON finalmask, cached
  outbound-subscription outbounds and template outbounds (never inbounds);
- GetXrayConfig heals template outbounds at build time;
- the JSON subscription upgrades each generated client stream;
- the Go and frontend link importers upgrade a legacy fm= param.

The finalmask editor gains a side prop: the client side lifts the dead key
on mount and offers a udphop mask editor (also for xhttp's HTTP/3 dial);
the inbound side keeps the UDP Hop switch, labelled as advertised.
2026-10-11 11:20:34 +02:00

107 lines
3.5 KiB
Go

package maskcompat
import (
"encoding/json"
"testing"
"github.com/xtls/xray-core/infra/conf"
"github.com/xtls/xray-core/transport/internet/finalmask/udphop"
)
// coreUDPHop builds a finalmask's UDP masks through conf.Mask, the loader the core
// calls at startup, and returns the udphop config the client would hop with, if any.
func coreUDPHop(t *testing.T, finalmask map[string]any) *udphop.Config {
t.Helper()
raw, err := json.Marshal(finalmask)
if err != nil {
t.Fatalf("marshal finalmask: %v", err)
}
var fm conf.FinalMask
if err := json.Unmarshal(raw, &fm); err != nil {
t.Fatalf("decode finalmask: %v", err)
}
for _, mask := range fm.Udp {
built, err := mask.Build(false)
if err != nil {
t.Fatalf("the core refuses UDP mask %q: %v", mask.Type, err)
}
if hop, ok := built.(*udphop.Config); ok {
return hop
}
}
return nil
}
func decodeFinalmask(t *testing.T, s string) map[string]any {
t.Helper()
var fm map[string]any
if err := json.Unmarshal([]byte(s), &fm); err != nil {
t.Fatalf("decode finalmask: %v", err)
}
return fm
}
func TestUpgradeLegacyUDPHop(t *testing.T) {
fm := decodeFinalmask(t, `{"udp":[{"type":"salamander","settings":{"password":"p"}}],
"quicParams":{"congestion":"bbr","udpHop":{"ports":"20000-20003","interval":"5-10"}}}`)
if hop := coreUDPHop(t, fm); hop != nil {
t.Fatal("the core hops on quicParams.udpHop again; the upgrade is no longer needed")
}
if !UpgradeLegacyUDPHop(fm) {
t.Fatal("UpgradeLegacyUDPHop reported no change for a legacy hop")
}
got, _ := json.Marshal(fm)
const want = `{"quicParams":{"congestion":"bbr"},"udp":[{"settings":{"password":"p"},"type":"salamander"},{"settings":{"interval":"5-10","mode":"intervalRemote","remotePorts":"20000-20003"},"type":"udphop"}]}`
if string(got) != want {
t.Fatalf("upgraded finalmask\n got: %s\nwant: %s", got, want)
}
hop := coreUDPHop(t, fm)
if hop == nil || !hop.Remote || hop.Local || hop.IntervalMin != 5 || hop.IntervalMax != 10 ||
len(hop.RemotePorts) != 4 || hop.RemotePorts[0] != 20000 || hop.RemotePorts[3] != 20003 {
t.Fatalf("the core hops with %+v, want remote ports 20000-20003 every 5-10s", hop)
}
}
func TestUpgradeLegacyUDPHopEdgeCases(t *testing.T) {
tests := []struct {
name string
input string
want string
}{
{
name: "an explicit udphop mask wins over the dead key",
input: `{"udp":[{"type":"udphop","settings":{"mode":"perConnRemote","remotePorts":"443"}}],"quicParams":{"udpHop":{"ports":"20000-30000"}}}`,
want: `{"udp":[{"settings":{"mode":"perConnRemote","remotePorts":"443"},"type":"udphop"}]}`,
},
{
name: "a hop without ports only loses the dead key",
input: `{"quicParams":{"udpHop":{"interval":"5-10"}}}`,
want: `{}`,
},
{
name: "no interval keeps the core's 30s default",
input: `{"quicParams":{"udpHop":{"ports":"443,8443"}}}`,
want: `{"udp":[{"settings":{"mode":"intervalRemote","remotePorts":"443,8443"},"type":"udphop"}]}`,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
fm := decodeFinalmask(t, tc.input)
if !UpgradeLegacyUDPHop(fm) {
t.Fatal("UpgradeLegacyUDPHop reported no change")
}
got, _ := json.Marshal(fm)
if string(got) != tc.want {
t.Fatalf("upgraded finalmask\n got: %s\nwant: %s", got, tc.want)
}
coreUDPHop(t, fm)
})
}
current := decodeFinalmask(t, `{"udp":[{"type":"udphop","settings":{"mode":"intervalRemote","remotePorts":"443"}}],"quicParams":{"congestion":"bbr"}}`)
if UpgradeLegacyUDPHop(current) {
t.Fatal("UpgradeLegacyUDPHop rewrote a finalmask without the legacy key")
}
}