mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-10-13 01:14:59 +03:00
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.
This commit is contained in:
+75
-9
@@ -1282,7 +1282,7 @@ func runSeeders(isUsersEmpty bool) error {
|
||||
}
|
||||
|
||||
if empty && isUsersEmpty {
|
||||
seeders := []string{"UserPasswordHash", "ClientsTable", "InboundClientsArrayFix", "InboundClientTgIdFix2", "InboundClientSubIdFix", "FreedomFinalRulesReverseFix", "FreedomFinalRulesPrivateEgressBlock", "UppercaseFreedomFinalRulesFix", "InboundRealityFinalmaskTcpStrip", "ApiTokensHash", "LegacyProxySettingsCleanup", "OutboundRemovedKeysFix", "FreedomDomainStrategyFix", "DNSOutboundLegacyKeysFix", "DNSOutboundQTypeZeroFix", "WireguardDomainStrategyFix", "XdnsFinalmaskNamesAddrsFix", "WireguardPeersToClients", "MtprotoSecretsToClients", "NodeInboundsAdopted", "ResetIpLimitNoFail2ban"}
|
||||
seeders := []string{"UserPasswordHash", "ClientsTable", "InboundClientsArrayFix", "InboundClientTgIdFix2", "InboundClientSubIdFix", "FreedomFinalRulesReverseFix", "FreedomFinalRulesPrivateEgressBlock", "UppercaseFreedomFinalRulesFix", "InboundRealityFinalmaskTcpStrip", "ApiTokensHash", "LegacyProxySettingsCleanup", "OutboundRemovedKeysFix", "FreedomDomainStrategyFix", "DNSOutboundLegacyKeysFix", "DNSOutboundQTypeZeroFix", "WireguardDomainStrategyFix", "XdnsFinalmaskNamesAddrsFix", "UDPHopClientMaskFix", "WireguardPeersToClients", "MtprotoSecretsToClients", "NodeInboundsAdopted", "ResetIpLimitNoFail2ban"}
|
||||
for _, name := range seeders {
|
||||
if err := db.Create(&model.HistoryOfSeeders{SeederName: name}).Error; err != nil {
|
||||
return err
|
||||
@@ -1429,6 +1429,12 @@ func runSeeders(isUsersEmpty bool) error {
|
||||
}
|
||||
}
|
||||
|
||||
if !slices.Contains(seedersHistory, "UDPHopClientMaskFix") {
|
||||
if err := migrateUDPHopClientMasks(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if !slices.Contains(seedersHistory, "NodeInboundsAdopted") {
|
||||
if err := seedNodeInboundsAdopted(); err != nil {
|
||||
return err
|
||||
@@ -1937,7 +1943,7 @@ func migrateXdnsFinalmaskShape() error {
|
||||
return err
|
||||
}
|
||||
for _, inbound := range inbounds {
|
||||
if updated, changed := upgradeLegacyXdnsJSON(inbound.StreamSettings, streamFinalmask, false); changed {
|
||||
if updated, changed := upgradeFinalmasksJSON(inbound.StreamSettings, streamFinalmask, false, maskcompat.UpgradeLegacyXdns); changed {
|
||||
if err := tx.Model(&model.Inbound{}).Where("id = ?", inbound.Id).
|
||||
Update("stream_settings", updated).Error; err != nil {
|
||||
return err
|
||||
@@ -1949,7 +1955,7 @@ func migrateXdnsFinalmaskShape() error {
|
||||
return err
|
||||
}
|
||||
for _, host := range hosts {
|
||||
if updated, changed := upgradeLegacyXdnsJSON(host.FinalMask, wholeFinalmask, false); changed {
|
||||
if updated, changed := upgradeFinalmasksJSON(host.FinalMask, wholeFinalmask, false, maskcompat.UpgradeLegacyXdns); changed {
|
||||
if err := tx.Model(&model.Host{}).Where("id = ?", host.Id).
|
||||
Update("final_mask", updated).Error; err != nil {
|
||||
return err
|
||||
@@ -1961,7 +1967,7 @@ func migrateXdnsFinalmaskShape() error {
|
||||
return err
|
||||
}
|
||||
for _, sub := range subs {
|
||||
if updated, changed := upgradeLegacyXdnsJSON(sub.LastFetchedOutbounds, outboundListFinalmasks, false); changed {
|
||||
if updated, changed := upgradeFinalmasksJSON(sub.LastFetchedOutbounds, outboundListFinalmasks, false, maskcompat.UpgradeLegacyXdns); changed {
|
||||
if err := tx.Model(&model.OutboundSubscription{}).Where("id = ?", sub.Id).
|
||||
Update("last_fetched_outbounds", updated).Error; err != nil {
|
||||
return err
|
||||
@@ -1984,7 +1990,7 @@ func migrateXdnsFinalmaskShape() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if updated, changed := upgradeLegacyXdnsJSON(setting.Value, stored.locate, stored.indent); changed {
|
||||
if updated, changed := upgradeFinalmasksJSON(setting.Value, stored.locate, stored.indent, maskcompat.UpgradeLegacyXdns); changed {
|
||||
if err := tx.Model(&model.Setting{}).Where("key = ?", stored.key).
|
||||
Update("value", updated).Error; err != nil {
|
||||
return err
|
||||
@@ -1995,9 +2001,64 @@ func migrateXdnsFinalmaskShape() error {
|
||||
})
|
||||
}
|
||||
|
||||
// upgradeLegacyXdnsJSON rewrites the finalmasks locate finds in one stored JSON document,
|
||||
// leaving the document byte-for-byte alone when nothing in it is legacy.
|
||||
func upgradeLegacyXdnsJSON(raw string, locate func(any) []any, indent bool) (string, bool) {
|
||||
// migrateUDPHopClientMasks moves the quicParams.udpHop xray-core 26.9.9 ignores into a
|
||||
// udphop mask in every client-side finalmask; inbounds keep it, udphop refuses to run there.
|
||||
func migrateUDPHopClientMasks() error {
|
||||
return db.Transaction(func(tx *gorm.DB) error {
|
||||
var hosts []model.Host
|
||||
if err := tx.Select("id", "final_mask").Find(&hosts).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
for _, host := range hosts {
|
||||
if updated, changed := upgradeFinalmasksJSON(host.FinalMask, wholeFinalmask, false, maskcompat.UpgradeLegacyUDPHop); changed {
|
||||
if err := tx.Model(&model.Host{}).Where("id = ?", host.Id).
|
||||
Update("final_mask", updated).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
var subs []model.OutboundSubscription
|
||||
if err := tx.Select("id", "last_fetched_outbounds").Find(&subs).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
for _, sub := range subs {
|
||||
if updated, changed := upgradeFinalmasksJSON(sub.LastFetchedOutbounds, outboundListFinalmasks, false, maskcompat.UpgradeLegacyUDPHop); changed {
|
||||
if err := tx.Model(&model.OutboundSubscription{}).Where("id = ?", sub.Id).
|
||||
Update("last_fetched_outbounds", updated).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, stored := range []struct {
|
||||
key string
|
||||
locate func(any) []any
|
||||
indent bool
|
||||
}{
|
||||
{"xrayTemplateConfig", templateOutboundFinalmasks, true},
|
||||
{"subJsonFinalMask", wholeFinalmask, false},
|
||||
} {
|
||||
var setting model.Setting
|
||||
err := tx.Where("key = ?", stored.key).First(&setting).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if updated, changed := upgradeFinalmasksJSON(setting.Value, stored.locate, stored.indent, maskcompat.UpgradeLegacyUDPHop); changed {
|
||||
if err := tx.Model(&model.Setting{}).Where("key = ?", stored.key).
|
||||
Update("value", updated).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return tx.Create(&model.HistoryOfSeeders{SeederName: "UDPHopClientMaskFix"}).Error
|
||||
})
|
||||
}
|
||||
|
||||
// upgradeFinalmasksJSON runs upgrade on the finalmasks locate finds in one stored JSON
|
||||
// document, leaving the document byte-for-byte alone when nothing in it changed.
|
||||
func upgradeFinalmasksJSON(raw string, locate func(any) []any, indent bool, upgrade func(any) bool) (string, bool) {
|
||||
if strings.TrimSpace(raw) == "" {
|
||||
return raw, false
|
||||
}
|
||||
@@ -2007,7 +2068,7 @@ func upgradeLegacyXdnsJSON(raw string, locate func(any) []any, indent bool) (str
|
||||
}
|
||||
changed := false
|
||||
for _, mask := range locate(doc) {
|
||||
if maskcompat.UpgradeLegacyXdns(mask) {
|
||||
if upgrade(mask) {
|
||||
changed = true
|
||||
}
|
||||
}
|
||||
@@ -2044,6 +2105,11 @@ func outboundListFinalmasks(doc any) []any {
|
||||
return finalmasks
|
||||
}
|
||||
|
||||
func templateOutboundFinalmasks(doc any) []any {
|
||||
cfg, _ := doc.(map[string]any)
|
||||
return outboundListFinalmasks(cfg["outbounds"])
|
||||
}
|
||||
|
||||
func templateFinalmasks(doc any) []any {
|
||||
cfg, _ := doc.(map[string]any)
|
||||
return append(outboundListFinalmasks(cfg["inbounds"]), outboundListFinalmasks(cfg["outbounds"])...)
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
||||
)
|
||||
|
||||
const legacyHopFinalmask = `{"quicParams":{"udpHop":{"ports":"20000-30000","interval":"5-10"}}}`
|
||||
|
||||
const upgradedHopFinalmask = `{"udp":[{"settings":{"interval":"5-10","mode":"intervalRemote","remotePorts":"20000-30000"},"type":"udphop"}]}`
|
||||
|
||||
func assertHopUpgraded(t *testing.T, where string, finalmask any) {
|
||||
t.Helper()
|
||||
got, err := json.Marshal(finalmask)
|
||||
if err != nil {
|
||||
t.Fatalf("%s: marshal finalmask: %v", where, err)
|
||||
}
|
||||
if string(got) != upgradedHopFinalmask {
|
||||
t.Fatalf("%s: finalmask\n got: %s\nwant: %s", where, got, upgradedHopFinalmask)
|
||||
}
|
||||
}
|
||||
|
||||
// xray-core 26.9.9 ignores quicParams.udpHop, so every client-side finalmask the panel
|
||||
// stored before then hops nowhere; inbounds keep it, since udphop refuses to run there.
|
||||
func TestUDPHopSeederMovesClientHopsToTheMask(t *testing.T) {
|
||||
initMigrateDB(t)
|
||||
inboundStream := `{"network":"hysteria","security":"tls","finalmask":` + legacyHopFinalmask + `}`
|
||||
ib := seedInboundWithStream(t, "hy-in", 8443, inboundStream)
|
||||
host := &model.Host{InboundId: ib.Id, Remark: "h", Address: "cdn.example.com", Port: 8443, FinalMask: legacyHopFinalmask}
|
||||
if err := GetDB().Create(host).Error; err != nil {
|
||||
t.Fatalf("create host: %v", err)
|
||||
}
|
||||
seedTemplate(t, `{"inbounds":[{"tag":"tpl-in","streamSettings":{"finalmask":`+legacyHopFinalmask+`}}],
|
||||
"outbounds":[{"protocol":"hysteria","tag":"hy-out","settings":{},"streamSettings":{"network":"hysteria","finalmask":`+legacyHopFinalmask+`}}]}`)
|
||||
if err := GetDB().Create(&model.Setting{Key: "subJsonFinalMask", Value: legacyHopFinalmask}).Error; err != nil {
|
||||
t.Fatalf("seed subJsonFinalMask: %v", err)
|
||||
}
|
||||
sub := &model.OutboundSubscription{
|
||||
Remark: "donor", Url: "https://donor.example.com/sub",
|
||||
LastFetchedOutbounds: `[{"protocol":"hysteria","tag":"sub-1","settings":{},"streamSettings":{"network":"hysteria","finalmask":` + legacyHopFinalmask + `}}]`,
|
||||
}
|
||||
if err := GetDB().Create(sub).Error; err != nil {
|
||||
t.Fatalf("create outbound subscription: %v", err)
|
||||
}
|
||||
if err := GetDB().Where("seeder_name = ?", "UDPHopClientMaskFix").Delete(&model.HistoryOfSeeders{}).Error; err != nil {
|
||||
t.Fatalf("clear seeder history: %v", err)
|
||||
}
|
||||
|
||||
if err := runSeeders(false); err != nil {
|
||||
t.Fatalf("runSeeders: %v", err)
|
||||
}
|
||||
|
||||
var storedHost model.Host
|
||||
if err := GetDB().First(&storedHost, host.Id).Error; err != nil {
|
||||
t.Fatalf("reload host: %v", err)
|
||||
}
|
||||
var hostMask any
|
||||
_ = json.Unmarshal([]byte(storedHost.FinalMask), &hostMask)
|
||||
assertHopUpgraded(t, "host finalMask", hostMask)
|
||||
|
||||
var template struct {
|
||||
Inbounds []struct {
|
||||
StreamSettings struct {
|
||||
Finalmask json.RawMessage `json:"finalmask"`
|
||||
} `json:"streamSettings"`
|
||||
} `json:"inbounds"`
|
||||
Outbounds []struct {
|
||||
StreamSettings struct {
|
||||
Finalmask any `json:"finalmask"`
|
||||
} `json:"streamSettings"`
|
||||
} `json:"outbounds"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(storedTemplate(t)), &template); err != nil || len(template.Outbounds) != 1 || len(template.Inbounds) != 1 {
|
||||
t.Fatalf("stored template unreadable (%v)", err)
|
||||
}
|
||||
assertHopUpgraded(t, "template outbound", template.Outbounds[0].StreamSettings.Finalmask)
|
||||
var inboundMask any
|
||||
_ = json.Unmarshal(template.Inbounds[0].StreamSettings.Finalmask, &inboundMask)
|
||||
var legacy any
|
||||
_ = json.Unmarshal([]byte(legacyHopFinalmask), &legacy)
|
||||
want, _ := json.Marshal(legacy)
|
||||
if got, _ := json.Marshal(inboundMask); string(got) != string(want) {
|
||||
t.Fatalf("template inbound finalmask = %s, want it untouched", got)
|
||||
}
|
||||
|
||||
var subMask model.Setting
|
||||
if err := GetDB().Where("key = ?", "subJsonFinalMask").First(&subMask).Error; err != nil {
|
||||
t.Fatalf("reload subJsonFinalMask: %v", err)
|
||||
}
|
||||
var subFinalmask any
|
||||
_ = json.Unmarshal([]byte(subMask.Value), &subFinalmask)
|
||||
assertHopUpgraded(t, "subJsonFinalMask", subFinalmask)
|
||||
|
||||
var storedSub model.OutboundSubscription
|
||||
if err := GetDB().First(&storedSub, sub.Id).Error; err != nil {
|
||||
t.Fatalf("reload outbound subscription: %v", err)
|
||||
}
|
||||
var cached []struct {
|
||||
StreamSettings struct {
|
||||
Finalmask any `json:"finalmask"`
|
||||
} `json:"streamSettings"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(storedSub.LastFetchedOutbounds), &cached); err != nil || len(cached) != 1 {
|
||||
t.Fatalf("cached subscription outbounds unreadable (%v)", err)
|
||||
}
|
||||
assertHopUpgraded(t, "cached subscription outbound", cached[0].StreamSettings.Finalmask)
|
||||
|
||||
var stored model.Inbound
|
||||
if err := GetDB().First(&stored, ib.Id).Error; err != nil {
|
||||
t.Fatalf("reload inbound: %v", err)
|
||||
}
|
||||
if stored.StreamSettings != inboundStream {
|
||||
t.Fatalf("inbound stream = %s, want it untouched", stored.StreamSettings)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user