mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-10-17 19:35:02 +03:00
feat(xray): update xray-core to v26.9.30 and adapt panel
Bump xtls/xray-core to b26a91de4f (v26.9.30) and the three binary pins (DockerInit.sh, release.yml Linux + Windows) in lockstep. No deleted symbols; the sing and sing-shadowsocks indirect deps drop out with the SS2022 rewrite. XDNS finalmask (#6718) replaced its string lists with objects: domains are {name, types, edns0, lenLimit, labelLimit} and client resolvers {type, settings.addr}. The loader no longer parses the old lists, so a single stored xdns mask keeps the whole core from starting. The new leaf package internal/util/maskcompat converts them: "name[:type]" becomes a domain and "name[:type]+udp://addr" a domain plus a udp resolver. A bare name maps to TXT, the type legacy clients queried by default, and each converted domain keeps the 1232-byte EDNS0 the old code always used (without it the server caps answers at 512). It runs from: - the XdnsFinalmaskObjectsFix seeder, over inbound streams, hosts, the xray template, the global sub-JSON mask and cached subscription outbounds; - inbound save (normalizeStreamSettings) and GetXrayConfig, for rows that never went through the seeder; - both link importers, since fm= from an older panel carries the lists. The finalmask form edits the object shape (every key needs a registered field, or the finalmask watch drops it on save) and lifts legacy masks on open. The udp-mask golden fixture moves to the object shape, which TestGoldenStreamFixturesBuildInXray now builds through the core. The wire format changed as well, so pre-upgrade clients need the new core. WireGuard outbound (#6771) dropped settings.domainStrategy and the remoteDNS "local" mode. The endpoint lookup now follows sockopt.domainStrategy and in-tunnel targets the outbound's targetStrategy. The old key is silently ignored, which undid the IPv4-first endpoint lookup the WARP outbound depends on (#5205), and "local" now panics the core at startup because remoteDNS goes through netip.MustParseAddr. The WireguardDomainStrategyFix seeder moves a stored family preference to both keys (a value already set wins) and turns "local" into targetStrategy; the outbound form lifts legacy rows the same way and drops its select, the WARP modal writes the new placement, and the inbound form loses a field the server never read. ValidateOutboundConfig now refuses a non-IP remoteDNS entry, which conf.Build() lets through, on template save and for outbound subscriptions. Noise finalmask items accept type "exp" (#6862), a tag expression. The form offers it for noise items only: header-custom items go through the core's PraseByteSlice, which refuses it. TUN gained autoSystemDnsToGateway (Linux) and autoSystemWfpBlockLeak (Windows). Both pass through the settings schema so a value set in JSON survives the next form save. MASQUE (inbound, outbound, transport) and the XDRIVE transport are new protocols the panel does not offer yet; their new loader refusals only cover configs the panel never generates. The FakeDNS IPv6 pool default, the SS2022 rewrite (same gRPC account; emails are now deduped case-insensitively, as the panel already does), the restored udphop interval default and the rest change no panel-facing config.
This commit is contained in:
+234
-1
@@ -23,6 +23,7 @@ import (
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/config"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/util/crypto"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/util/maskcompat"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/util/random"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/xray"
|
||||
|
||||
@@ -1280,7 +1281,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", "WireguardPeersToClients", "MtprotoSecretsToClients", "NodeInboundsAdopted", "ResetIpLimitNoFail2ban"}
|
||||
seeders := []string{"UserPasswordHash", "ClientsTable", "InboundClientsArrayFix", "InboundClientTgIdFix2", "InboundClientSubIdFix", "FreedomFinalRulesReverseFix", "FreedomFinalRulesPrivateEgressBlock", "UppercaseFreedomFinalRulesFix", "InboundRealityFinalmaskTcpStrip", "ApiTokensHash", "LegacyProxySettingsCleanup", "OutboundRemovedKeysFix", "FreedomDomainStrategyFix", "DNSOutboundLegacyKeysFix", "DNSOutboundQTypeZeroFix", "WireguardDomainStrategyFix", "XdnsFinalmaskObjectsFix", "WireguardPeersToClients", "MtprotoSecretsToClients", "NodeInboundsAdopted", "ResetIpLimitNoFail2ban"}
|
||||
for _, name := range seeders {
|
||||
if err := db.Create(&model.HistoryOfSeeders{SeederName: name}).Error; err != nil {
|
||||
return err
|
||||
@@ -1415,6 +1416,18 @@ func runSeeders(isUsersEmpty bool) error {
|
||||
}
|
||||
}
|
||||
|
||||
if !slices.Contains(seedersHistory, "WireguardDomainStrategyFix") {
|
||||
if err := migrateWireguardDomainStrategy(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if !slices.Contains(seedersHistory, "XdnsFinalmaskObjectsFix") {
|
||||
if err := migrateXdnsFinalmaskObjects(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if !slices.Contains(seedersHistory, "NodeInboundsAdopted") {
|
||||
if err := seedNodeInboundsAdopted(); err != nil {
|
||||
return err
|
||||
@@ -1815,6 +1828,226 @@ var freedomDomainStrategies = map[string]bool{
|
||||
"forceipv6": true, "forceipv4v6": true, "forceipv6v4": true,
|
||||
}
|
||||
|
||||
func migrateWireguardDomainStrategy() error {
|
||||
var setting model.Setting
|
||||
err := db.Model(model.Setting{}).Where("key = ?", "xrayTemplateConfig").First(&setting).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return db.Create(&model.HistoryOfSeeders{SeederName: "WireguardDomainStrategyFix"}).Error
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
updated, changed, rErr := rewriteWireguardDomainStrategy(setting.Value)
|
||||
if rErr != nil {
|
||||
log.Printf("WireguardDomainStrategyFix: skip (invalid xrayTemplateConfig json): %v", rErr)
|
||||
return db.Create(&model.HistoryOfSeeders{SeederName: "WireguardDomainStrategyFix"}).Error
|
||||
}
|
||||
|
||||
return db.Transaction(func(tx *gorm.DB) error {
|
||||
if changed {
|
||||
if err := tx.Model(&model.Setting{}).Where("key = ?", "xrayTemplateConfig").
|
||||
Update("value", updated).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return tx.Create(&model.HistoryOfSeeders{SeederName: "WireguardDomainStrategyFix"}).Error
|
||||
})
|
||||
}
|
||||
|
||||
// rewriteWireguardDomainStrategy splits the settings.domainStrategy xray-core 26.9.30 (#6771)
|
||||
// ignores: sockopt.domainStrategy now picks the endpoint's family, targetStrategy the targets'.
|
||||
func rewriteWireguardDomainStrategy(raw string) (string, bool, error) {
|
||||
if strings.TrimSpace(raw) == "" {
|
||||
return raw, false, nil
|
||||
}
|
||||
var cfg map[string]any
|
||||
if err := json.Unmarshal([]byte(raw), &cfg); err != nil {
|
||||
return raw, false, err
|
||||
}
|
||||
outbounds, ok := cfg["outbounds"].([]any)
|
||||
if !ok {
|
||||
return raw, false, nil
|
||||
}
|
||||
changed := false
|
||||
for _, ob := range outbounds {
|
||||
obj, ok := ob.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if proto, _ := obj["protocol"].(string); !strings.EqualFold(proto, "wireguard") {
|
||||
continue
|
||||
}
|
||||
settings, _ := obj["settings"].(map[string]any)
|
||||
legacy, hasLegacy := settings["domainStrategy"]
|
||||
remoteDNS, _ := settings["remoteDNS"].([]any)
|
||||
localDNS := len(remoteDNS) == 1 && remoteDNS[0] == "local"
|
||||
if !hasLegacy && !localDNS {
|
||||
continue
|
||||
}
|
||||
delete(settings, "domainStrategy")
|
||||
strategy, _ := legacy.(string)
|
||||
if !wireguardFamilyStrategies[strings.ToLower(strategy)] {
|
||||
strategy = ""
|
||||
}
|
||||
if strategy != "" {
|
||||
if sockopt := outboundSockopt(obj, true); !strategyIsSet(sockopt["domainStrategy"]) {
|
||||
sockopt["domainStrategy"] = strategy
|
||||
}
|
||||
}
|
||||
if localDNS {
|
||||
delete(settings, "remoteDNS")
|
||||
if strategy == "" {
|
||||
strategy = "ForceIP"
|
||||
}
|
||||
}
|
||||
if strategy != "" && !strategyIsSet(obj["targetStrategy"]) {
|
||||
obj["targetStrategy"] = strategy
|
||||
}
|
||||
changed = true
|
||||
}
|
||||
if !changed {
|
||||
return raw, false, nil
|
||||
}
|
||||
out, err := json.MarshalIndent(cfg, "", " ")
|
||||
if err != nil {
|
||||
return raw, false, err
|
||||
}
|
||||
return string(out), true, nil
|
||||
}
|
||||
|
||||
// wireguardFamilyStrategies are the old wireguard values that pinned an address family;
|
||||
// plain ForceIP pinned none, and any other value already failed the old core's load.
|
||||
var wireguardFamilyStrategies = map[string]bool{
|
||||
"forceipv4": true, "forceipv6": true, "forceipv4v6": true, "forceipv6v4": true,
|
||||
}
|
||||
|
||||
func strategyIsSet(value any) bool {
|
||||
s, _ := value.(string)
|
||||
return s != "" && !strings.EqualFold(s, "asis")
|
||||
}
|
||||
|
||||
// migrateXdnsFinalmaskObjects upgrades every stored xdns mask to the object shape
|
||||
// xray-core 26.9.30 requires, wherever the panel keeps a finalmask.
|
||||
func migrateXdnsFinalmaskObjects() error {
|
||||
return db.Transaction(func(tx *gorm.DB) error {
|
||||
var inbounds []model.Inbound
|
||||
if err := tx.Select("id", "stream_settings").Find(&inbounds).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
for _, inbound := range inbounds {
|
||||
if updated, changed := upgradeLegacyXdnsJSON(inbound.StreamSettings, streamFinalmask, false); changed {
|
||||
if err := tx.Model(&model.Inbound{}).Where("id = ?", inbound.Id).
|
||||
Update("stream_settings", updated).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
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 := upgradeLegacyXdnsJSON(host.FinalMask, wholeFinalmask, false); 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 := upgradeLegacyXdnsJSON(sub.LastFetchedOutbounds, outboundListFinalmasks, false); 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", templateFinalmasks, 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 := upgradeLegacyXdnsJSON(setting.Value, stored.locate, stored.indent); 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: "XdnsFinalmaskObjectsFix"}).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) {
|
||||
if strings.TrimSpace(raw) == "" {
|
||||
return raw, false
|
||||
}
|
||||
var doc any
|
||||
if err := json.Unmarshal([]byte(raw), &doc); err != nil {
|
||||
return raw, false
|
||||
}
|
||||
changed := false
|
||||
for _, mask := range locate(doc) {
|
||||
if maskcompat.UpgradeLegacyXdns(mask) {
|
||||
changed = true
|
||||
}
|
||||
}
|
||||
if !changed {
|
||||
return raw, false
|
||||
}
|
||||
var out []byte
|
||||
var err error
|
||||
if indent {
|
||||
out, err = json.MarshalIndent(doc, "", " ")
|
||||
} else {
|
||||
out, err = json.Marshal(doc)
|
||||
}
|
||||
if err != nil {
|
||||
return raw, false
|
||||
}
|
||||
return string(out), true
|
||||
}
|
||||
|
||||
func wholeFinalmask(doc any) []any { return []any{doc} }
|
||||
|
||||
func streamFinalmask(doc any) []any {
|
||||
stream, _ := doc.(map[string]any)
|
||||
return []any{stream["finalmask"]}
|
||||
}
|
||||
|
||||
func outboundListFinalmasks(doc any) []any {
|
||||
list, _ := doc.([]any)
|
||||
finalmasks := make([]any, 0, len(list))
|
||||
for _, entry := range list {
|
||||
obj, _ := entry.(map[string]any)
|
||||
finalmasks = append(finalmasks, streamFinalmask(obj["streamSettings"])...)
|
||||
}
|
||||
return finalmasks
|
||||
}
|
||||
|
||||
func templateFinalmasks(doc any) []any {
|
||||
cfg, _ := doc.(map[string]any)
|
||||
return append(outboundListFinalmasks(cfg["inbounds"]), outboundListFinalmasks(cfg["outbounds"])...)
|
||||
}
|
||||
|
||||
// migrateDNSOutboundLegacyKeys rewrites stored dns outbounds once, because the
|
||||
// core logs nonIPQuery/blockTypes as deprecated on every config load.
|
||||
func migrateDNSOutboundLegacyKeys() error {
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/config"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/xray"
|
||||
)
|
||||
|
||||
const wgTestKeys = `"secretKey":"yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk=","peers":[{"publicKey":"xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg=","endpoint":"engage.cloudflareclient.com:2408"}]`
|
||||
|
||||
func TestRewriteWireguardDomainStrategy(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
outbound string
|
||||
wantChanged bool
|
||||
wantRoot string
|
||||
wantSockopt string
|
||||
wantDNS bool
|
||||
}{
|
||||
{
|
||||
name: "the WARP default moves to both places the core now reads",
|
||||
outbound: `{"protocol":"wireguard","tag":"warp","settings":{"domainStrategy":"ForceIPv4v6",` + wgTestKeys + `}}`,
|
||||
wantChanged: true, wantRoot: "ForceIPv4v6", wantSockopt: "ForceIPv4v6",
|
||||
},
|
||||
{
|
||||
name: "values the admin already set win over the legacy key",
|
||||
outbound: `{"protocol":"wireguard","tag":"wg","targetStrategy":"UseIPv6","streamSettings":{"sockopt":{"domainStrategy":"UseIPv4"}},"settings":{"domainStrategy":"forceipv6",` + wgTestKeys + `}}`,
|
||||
wantChanged: true, wantRoot: "UseIPv6", wantSockopt: "UseIPv4",
|
||||
},
|
||||
{
|
||||
name: "plain ForceIP had no family preference, so only the key goes",
|
||||
outbound: `{"protocol":"wireguard","tag":"wg","settings":{"domainStrategy":"ForceIP",` + wgTestKeys + `}}`,
|
||||
wantChanged: true,
|
||||
},
|
||||
{
|
||||
name: "remoteDNS local becomes targetStrategy, which resolves with the built-in DNS",
|
||||
outbound: `{"protocol":"wireguard","tag":"wg","settings":{"domainStrategy":"ForceIPv4","remoteDNS":["local"],` + wgTestKeys + `}}`,
|
||||
wantChanged: true, wantRoot: "ForceIPv4", wantSockopt: "ForceIPv4",
|
||||
},
|
||||
{
|
||||
name: "remoteDNS local without a strategy resolves any family",
|
||||
outbound: `{"protocol":"wireguard","tag":"wg","settings":{"remoteDNS":["local"],` + wgTestKeys + `}}`,
|
||||
wantChanged: true, wantRoot: "ForceIP",
|
||||
},
|
||||
{
|
||||
name: "a strategy the old core refused is dropped rather than moved",
|
||||
outbound: `{"protocol":"wireguard","tag":"wg","settings":{"domainStrategy":"UseIPv4",` + wgTestKeys + `}}`,
|
||||
wantChanged: true,
|
||||
},
|
||||
{
|
||||
name: "IP remoteDNS entries stay",
|
||||
outbound: `{"protocol":"wireguard","tag":"wg","settings":{"remoteDNS":["1.1.1.1"],` + wgTestKeys + `}}`,
|
||||
wantChanged: false, wantDNS: true,
|
||||
},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
updated, changed, err := rewriteWireguardDomainStrategy(`{"outbounds":[` + tc.outbound + `]}`)
|
||||
if err != nil {
|
||||
t.Fatalf("rewrite: %v", err)
|
||||
}
|
||||
if changed != tc.wantChanged {
|
||||
t.Fatalf("changed = %v, want %v", changed, tc.wantChanged)
|
||||
}
|
||||
var cfg struct {
|
||||
Outbounds []json.RawMessage `json:"outbounds"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(updated), &cfg); err != nil || len(cfg.Outbounds) != 1 {
|
||||
t.Fatalf("rewritten template unreadable (%v): %s", err, updated)
|
||||
}
|
||||
var got struct {
|
||||
TargetStrategy string `json:"targetStrategy"`
|
||||
StreamSettings struct {
|
||||
Sockopt struct {
|
||||
DomainStrategy string `json:"domainStrategy"`
|
||||
} `json:"sockopt"`
|
||||
} `json:"streamSettings"`
|
||||
Settings map[string]any `json:"settings"`
|
||||
}
|
||||
if err := json.Unmarshal(cfg.Outbounds[0], &got); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.TargetStrategy != tc.wantRoot {
|
||||
t.Errorf("targetStrategy = %q, want %q", got.TargetStrategy, tc.wantRoot)
|
||||
}
|
||||
if got.StreamSettings.Sockopt.DomainStrategy != tc.wantSockopt {
|
||||
t.Errorf("sockopt.domainStrategy = %q, want %q", got.StreamSettings.Sockopt.DomainStrategy, tc.wantSockopt)
|
||||
}
|
||||
if _, kept := got.Settings["domainStrategy"]; kept {
|
||||
t.Errorf("settings.domainStrategy survived the rewrite: %s", cfg.Outbounds[0])
|
||||
}
|
||||
if _, kept := got.Settings["remoteDNS"]; kept != tc.wantDNS {
|
||||
t.Errorf("remoteDNS kept = %v, want %v", kept, tc.wantDNS)
|
||||
}
|
||||
if err := xray.ValidateOutboundConfig(cfg.Outbounds[0]); err != nil {
|
||||
t.Fatalf("xray-core refuses the rewritten outbound: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestWireguardDomainStrategySeederRewritesStoredTemplateOnce(t *testing.T) {
|
||||
t.Setenv("XUI_DB_FOLDER", t.TempDir())
|
||||
if err := InitDB(config.GetDBPath()); err != nil {
|
||||
if strings.Contains(err.Error(), "CGO_ENABLED=0") {
|
||||
t.Skipf("sqlite needs cgo: %v", err)
|
||||
}
|
||||
t.Fatalf("init db: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = CloseDB() })
|
||||
|
||||
legacy := `{"outbounds":[{"protocol":"wireguard","tag":"warp","settings":{"domainStrategy":"ForceIPv4v6",` + wgTestKeys + `}}]}`
|
||||
seedTemplate(t, legacy)
|
||||
if err := db.Where("seeder_name = ?", "WireguardDomainStrategyFix").
|
||||
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 cfg struct {
|
||||
Outbounds []struct {
|
||||
TargetStrategy string `json:"targetStrategy"`
|
||||
Settings map[string]any `json:"settings"`
|
||||
} `json:"outbounds"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(storedTemplate(t)), &cfg); err != nil || len(cfg.Outbounds) != 1 {
|
||||
t.Fatalf("stored template unreadable (%v)", err)
|
||||
}
|
||||
if _, kept := cfg.Outbounds[0].Settings["domainStrategy"]; kept || cfg.Outbounds[0].TargetStrategy != "ForceIPv4v6" {
|
||||
t.Fatalf("stored outbound was not rewritten: %+v", cfg.Outbounds[0])
|
||||
}
|
||||
|
||||
// The history gate keeps a hand-edited template from being rewritten on every restart.
|
||||
seedTemplate(t, legacy)
|
||||
if err := runSeeders(false); err != nil {
|
||||
t.Fatalf("runSeeders: %v", err)
|
||||
}
|
||||
if got := storedTemplate(t); got != legacy {
|
||||
t.Errorf("a completed seeder rewrote the template again: %s", got)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
||||
)
|
||||
|
||||
const legacyXdnsFinalmask = `{"udp":[{"type":"xdns","settings":{"domains":["t.example.com"]}}]}`
|
||||
|
||||
// assertXdnsUpgraded fails unless the finalmask's xdns domains are objects, the only
|
||||
// shape xray-core 26.9.30 parses.
|
||||
func assertXdnsUpgraded(t *testing.T, where string, finalmask any) {
|
||||
t.Helper()
|
||||
fm, _ := finalmask.(map[string]any)
|
||||
udp, _ := fm["udp"].([]any)
|
||||
if len(udp) != 1 {
|
||||
t.Fatalf("%s: udp masks = %v, want one", where, fm["udp"])
|
||||
}
|
||||
mask, _ := udp[0].(map[string]any)
|
||||
settings, _ := mask["settings"].(map[string]any)
|
||||
domains, _ := settings["domains"].([]any)
|
||||
if len(domains) != 1 {
|
||||
t.Fatalf("%s: domains = %v, want one entry", where, settings["domains"])
|
||||
}
|
||||
domain, ok := domains[0].(map[string]any)
|
||||
if !ok || domain["name"] != "t.example.com" {
|
||||
t.Fatalf("%s: domain = %#v, want an object named t.example.com", where, domains[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestXdnsFinalmaskSeederUpgradesEveryStoredMask(t *testing.T) {
|
||||
initMigrateDB(t)
|
||||
ib := seedInboundWithStream(t, "xdns-in", 5353,
|
||||
`{"network":"kcp","security":"none","finalmask":`+legacyXdnsFinalmask+`}`)
|
||||
host := &model.Host{InboundId: ib.Id, Remark: "h", Address: "cdn.example.com", Port: 53, FinalMask: legacyXdnsFinalmask}
|
||||
if err := GetDB().Create(host).Error; err != nil {
|
||||
t.Fatalf("create host: %v", err)
|
||||
}
|
||||
seedTemplate(t, `{"outbounds":[{"protocol":"vless","tag":"dns-tunnel","settings":{},"streamSettings":{"network":"kcp","finalmask":`+legacyXdnsFinalmask+`}}]}`)
|
||||
if err := GetDB().Create(&model.Setting{Key: "subJsonFinalMask", Value: legacyXdnsFinalmask}).Error; err != nil {
|
||||
t.Fatalf("seed subJsonFinalMask: %v", err)
|
||||
}
|
||||
sub := &model.OutboundSubscription{
|
||||
Remark: "donor", Url: "https://donor.example.com/sub",
|
||||
LastFetchedOutbounds: `[{"protocol":"vless","tag":"sub-1","settings":{},"streamSettings":{"network":"kcp","finalmask":` + legacyXdnsFinalmask + `}}]`,
|
||||
}
|
||||
if err := GetDB().Create(sub).Error; err != nil {
|
||||
t.Fatalf("create outbound subscription: %v", err)
|
||||
}
|
||||
if err := GetDB().Where("seeder_name = ?", "XdnsFinalmaskObjectsFix").
|
||||
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 stored model.Inbound
|
||||
if err := GetDB().First(&stored, ib.Id).Error; err != nil {
|
||||
t.Fatalf("reload inbound: %v", err)
|
||||
}
|
||||
var stream map[string]any
|
||||
if err := json.Unmarshal([]byte(stored.StreamSettings), &stream); err != nil {
|
||||
t.Fatalf("inbound stream is not JSON: %v", err)
|
||||
}
|
||||
assertXdnsUpgraded(t, "inbound stream", stream["finalmask"])
|
||||
|
||||
var storedHost model.Host
|
||||
if err := GetDB().First(&storedHost, host.Id).Error; err != nil {
|
||||
t.Fatalf("reload host: %v", err)
|
||||
}
|
||||
var hostMask any
|
||||
if err := json.Unmarshal([]byte(storedHost.FinalMask), &hostMask); err != nil {
|
||||
t.Fatalf("host finalMask is not JSON: %v", err)
|
||||
}
|
||||
assertXdnsUpgraded(t, "host finalMask", hostMask)
|
||||
|
||||
var template struct {
|
||||
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 {
|
||||
t.Fatalf("stored template unreadable (%v)", err)
|
||||
}
|
||||
assertXdnsUpgraded(t, "template outbound", template.Outbounds[0].StreamSettings.Finalmask)
|
||||
|
||||
var subMask model.Setting
|
||||
if err := GetDB().Where("key = ?", "subJsonFinalMask").First(&subMask).Error; err != nil {
|
||||
t.Fatalf("reload subJsonFinalMask: %v", err)
|
||||
}
|
||||
var subFinalmask any
|
||||
if err := json.Unmarshal([]byte(subMask.Value), &subFinalmask); err != nil {
|
||||
t.Fatalf("subJsonFinalMask is not JSON: %v", err)
|
||||
}
|
||||
assertXdnsUpgraded(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)
|
||||
}
|
||||
assertXdnsUpgraded(t, "cached subscription outbound", cached[0].StreamSettings.Finalmask)
|
||||
}
|
||||
Reference in New Issue
Block a user