fix(finalmask): keep a single udphop mask last in every client finalmask

xray-core reverses finalmask.udp and refuses, at dial time, a mask that
dials its own socket (udphop) anywhere but the socket end: "incorrect
index". xray run -test still says Configuration OK, so the failure only
shows as a client that never connects.

UDPHopClientMaskFix turned a global subJsonFinalMask hop and a host hop
into udphop masks; the JSON subscription then appended both, leaving two
hoppers with the first one misplaced, and a host mask merged after a
global hop pushed it off the end too. A hand-ordered template outbound
or an imported fm= could carry the same order.

maskcompat.NormalizeUDPHop keeps the last-listed udphop alone at the end
of udp, and runs wherever a client finalmask is produced: each JSON
subscription endpoint, share-link fm=, the Go and frontend link
importers and the template outbound heal in GetXrayConfig. The JSON
subscription resolves competing hop ranges most specific first: a host
hop wins over the inbound's own, which wins over the global default.
This commit is contained in:
MHSanaei
2026-10-11 18:02:12 +02:00
parent a8255944bf
commit 6321297f68
12 changed files with 281 additions and 43 deletions
@@ -114,37 +114,46 @@ func TestGetXrayConfig_UpgradesTemplateOutboundXdnsMask(t *testing.T) {
t.Fatal("outbound dns-tunnel not found in the generated config")
}
// A hysteria outbound pasted from a pre-26.9.9 template still carries quicParams.udpHop,
// which the core now ignores; the build heal hands it the udphop mask instead.
func TestGetXrayConfig_UpgradesTemplateOutboundUDPHop(t *testing.T) {
setupConflictDB(t)
template := `{"outbounds":[{"protocol":"freedom","tag":"direct"},{"protocol":"hysteria","tag":"hy-out",
"settings":{"version":2,"address":"hy.example.com","port":443},
"streamSettings":{"network":"hysteria","security":"tls","hysteriaSettings":{"version":2,"auth":"a"},
"finalmask":{"quicParams":{"udpHop":{"ports":"20000-30000","interval":"5-10"}}}}}]}`
if err := (&SettingService{}).saveSetting("xrayTemplateConfig", template); err != nil {
t.Fatalf("seed template: %v", err)
// A template outbound pasted from before 26.9.9 carries quicParams.udpHop, which the core ignores,
// and a hand-ordered udphop not last makes it refuse every dial; the build heals both.
func TestGetXrayConfig_HealsTemplateOutboundUDPHop(t *testing.T) {
const hop = `{"settings":{"interval":"5-10","mode":"intervalRemote","remotePorts":"20000-30000"},"type":"udphop"}`
const salamander = `{"settings":{"password":"s3cr3t"},"type":"salamander"}`
tests := []struct{ name, finalmask, want string }{
{"legacy quicParams.udpHop", `{"quicParams":{"udpHop":{"ports":"20000-30000","interval":"5-10"}}}`, `{"udp":[` + hop + `]}`},
{"udphop listed first", `{"udp":[` + hop + `,` + salamander + `]}`, `{"udp":[` + salamander + `,` + hop + `]}`},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
setupConflictDB(t)
template := `{"outbounds":[{"protocol":"freedom","tag":"direct"},{"protocol":"hysteria","tag":"hy-out",
"settings":{"version":2,"address":"hy.example.com","port":443},
"streamSettings":{"network":"hysteria","security":"tls","hysteriaSettings":{"version":2,"auth":"a"},
"finalmask":` + tc.finalmask + `}}]}`
if err := (&SettingService{}).saveSetting("xrayTemplateConfig", template); err != nil {
t.Fatalf("seed template: %v", err)
}
cfg, err := (&XrayService{}).GetXrayConfig()
if err != nil {
t.Fatalf("GetXrayConfig: %v", err)
cfg, err := (&XrayService{}).GetXrayConfig()
if err != nil {
t.Fatalf("GetXrayConfig: %v", err)
}
var outbounds []map[string]any
if err := json.Unmarshal(cfg.OutboundConfigs, &outbounds); err != nil {
t.Fatalf("decode emitted outbounds: %v", err)
}
for _, outbound := range outbounds {
if outbound["tag"] != "hy-out" {
continue
}
stream, _ := outbound["streamSettings"].(map[string]any)
got, _ := json.Marshal(stream["finalmask"])
if string(got) != tc.want {
t.Fatalf("emitted finalmask\n got: %s\nwant: %s", got, tc.want)
}
return
}
t.Fatal("outbound hy-out not found in the generated config")
})
}
var outbounds []map[string]any
if err := json.Unmarshal(cfg.OutboundConfigs, &outbounds); err != nil {
t.Fatalf("decode emitted outbounds: %v", err)
}
for _, outbound := range outbounds {
if outbound["tag"] != "hy-out" {
continue
}
stream, _ := outbound["streamSettings"].(map[string]any)
got, _ := json.Marshal(stream["finalmask"])
const want = `{"udp":[{"settings":{"interval":"5-10","mode":"intervalRemote","remotePorts":"20000-30000"},"type":"udphop"}]}`
if string(got) != want {
t.Fatalf("emitted finalmask\n got: %s\nwant: %s", got, want)
}
return
}
t.Fatal("outbound hy-out not found in the generated config")
}
+7 -3
View File
@@ -180,10 +180,10 @@ func (s *XrayService) GetXrayConfig() (*xray.Config, error) {
xrayConfig.API = ensureAPIServices(xrayConfig.API)
xrayConfig.Policy = ensureStatsPolicy(xrayConfig.Policy)
xrayConfig.RouterConfig = stripDisabledRules(xrayConfig.RouterConfig)
// A pasted or restored template can still carry pre-#6258 XHTTP session keys, a pre-26.10.10
// xdns mask or a pre-26.9.9 quicParams.udpHop, all of which the core silently ignores.
// A pasted or restored template can carry pre-#6258 XHTTP keys, a pre-26.10.10 xdns mask or a
// pre-26.9.9 quicParams.udpHop, which the core ignores, or a udphop not last, which it cannot dial.
xrayConfig.OutboundConfigs = healOutboundStreams(xrayConfig.OutboundConfigs,
liftXhttpSessionIDKeys, upgradeStreamLegacyXdns, upgradeStreamLegacyUDPHop)
liftXhttpSessionIDKeys, upgradeStreamLegacyXdns, upgradeStreamLegacyUDPHop, normalizeStreamUDPHop)
// Bridge amneziawg outbounds before anything else reads OutboundConfigs;
// the core has no amneziawg proxy and would reject the raw entry.
if err := transformAmneziaWGOutbounds(xrayConfig); err != nil {
@@ -1752,3 +1752,7 @@ func upgradeStreamLegacyXdns(stream map[string]any) bool {
func upgradeStreamLegacyUDPHop(stream map[string]any) bool {
return maskcompat.UpgradeLegacyUDPHop(stream["finalmask"])
}
func normalizeStreamUDPHop(stream map[string]any) bool {
return maskcompat.NormalizeUDPHop(stream["finalmask"])
}