From 5e553d1f2864ebcd84791d471f0e0e7dbfca0006 Mon Sep 17 00:00:00 2001 From: hossinasaadi Date: Tue, 6 Jan 2026 17:41:04 +0330 Subject: [PATCH 1/8] fix routing log --- app/router/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/router/config.go b/app/router/config.go index 0b2ebd9404c0..bb0776a6c19e 100644 --- a/app/router/config.go +++ b/app/router/config.go @@ -122,7 +122,7 @@ func (rr *RoutingRule) BuildCondition() (Condition, error) { if err != nil { return nil, errors.New("failed to build domain condition with MphDomainMatcher").Base(err) } - errors.LogDebug(context.Background(), "MphDomainMatcher is enabled for ", len(rr.Domain), " domain rule(s)") + errors.LogDebug(context.Background(), "MphDomainMatcher is enabled for ", len(domains), " domain rule(s)") conds.Add(matcher) } From a0439290d2c687418e7016b3b34ae0dfbf5b6cf5 Mon Sep 17 00:00:00 2001 From: hossinasaadi Date: Tue, 6 Jan 2026 17:41:41 +0330 Subject: [PATCH 2/8] write test for Geosite with Attr --- app/router/condition_test.go | 80 ++++++++++++++++++++++++++++++++---- 1 file changed, 73 insertions(+), 7 deletions(-) diff --git a/app/router/condition_test.go b/app/router/condition_test.go index 1272aef6e9f4..f28c49350304 100644 --- a/app/router/condition_test.go +++ b/app/router/condition_test.go @@ -2,8 +2,10 @@ package router_test import ( "strconv" + "strings" "testing" + "github.com/xtls/xray-core/app/router" . "github.com/xtls/xray-core/app/router" "github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common/errors" @@ -300,8 +302,16 @@ func TestRoutingRule(t *testing.T) { } } -func loadGeoSite(country string) ([]*Domain, error) { - path, err := getAssetPath("geosite.dat") +func loadGeositeWithAttr(file string, siteWithAttr string) ([]*Domain, error) { + + parts := strings.Split(siteWithAttr, "@") + if len(parts) == 0 { + return nil, errors.New("empty site") + } + country := strings.ToUpper(parts[0]) + attrs := router.ParseAttrs(parts[1:]) + + path, err := getAssetPath(file) if err != nil { return nil, err } @@ -314,18 +324,32 @@ func loadGeoSite(country string) ([]*Domain, error) { if err := proto.Unmarshal(geositeBytes, &geositeList); err != nil { return nil, err } - + var domainList []*Domain for _, site := range geositeList.Entry { if site.CountryCode == country { - return site.Domain, nil + domainList = site.Domain + } + } + if domainList == nil { + return nil, errors.New("country not found: " + country) + } + + if attrs.IsEmpty() { + return domainList, nil + } + + filteredDomains := make([]*router.Domain, 0, len(domainList)) + for _, domain := range domainList { + if attrs.Match(domain) { + filteredDomains = append(filteredDomains, domain) } } - return nil, errors.New("country not found: " + country) + return filteredDomains, nil } func TestChinaSites(t *testing.T) { - domains, err := loadGeoSite("CN") + domains, err := loadGeositeWithAttr("geosite.dat", "CN") common.Must(err) acMatcher, err := NewMphMatcherGroup(domains) @@ -366,8 +390,50 @@ func TestChinaSites(t *testing.T) { } } +func TestChinaSitesWithAttrs(t *testing.T) { + domains, err := loadGeositeWithAttr("geosite.dat", "google@CN") + common.Must(err) + + acMatcher, err := NewMphMatcherGroup(domains) + common.Must(err) + + type TestCase struct { + Domain string + Output bool + } + testCases := []TestCase{ + { + Domain: "google.cn", + Output: true, + }, + { + Domain: "recaptcha.net", + Output: true, + }, + { + Domain: "164.com", + Output: false, + }, + { + Domain: "164.com", + Output: false, + }, + } + + for i := 0; i < 1024; i++ { + testCases = append(testCases, TestCase{Domain: strconv.Itoa(i) + ".not-exists.com", Output: false}) + } + + for _, testCase := range testCases { + r := acMatcher.ApplyDomain(testCase.Domain) + if r != testCase.Output { + t.Error("ACDomainMatcher expected output ", testCase.Output, " for domain ", testCase.Domain, " but got ", r) + } + } +} + func BenchmarkMphDomainMatcher(b *testing.B) { - domains, err := loadGeoSite("CN") + domains, err := loadGeositeWithAttr("geosite.dat", "CN") common.Must(err) matcher, err := NewMphMatcherGroup(domains) From 0779a6b48dfe2afe0a16440672dc10f2d024df95 Mon Sep 17 00:00:00 2001 From: hossinasaadi Date: Tue, 6 Jan 2026 18:11:29 +0330 Subject: [PATCH 3/8] improve test for geosite --- app/router/condition_test.go | 64 ++++++++---------------------------- app/router/config.go | 4 +-- common/platform/others.go | 17 ++++++++++ infra/conf/dns.go | 2 +- infra/conf/router.go | 6 ++-- 5 files changed, 36 insertions(+), 57 deletions(-) diff --git a/app/router/condition_test.go b/app/router/condition_test.go index f28c49350304..2296fb2e532d 100644 --- a/app/router/condition_test.go +++ b/app/router/condition_test.go @@ -1,22 +1,20 @@ package router_test import ( + "runtime" "strconv" - "strings" "testing" "github.com/xtls/xray-core/app/router" . "github.com/xtls/xray-core/app/router" "github.com/xtls/xray-core/common" - "github.com/xtls/xray-core/common/errors" "github.com/xtls/xray-core/common/net" - "github.com/xtls/xray-core/common/platform/filesystem" "github.com/xtls/xray-core/common/protocol" "github.com/xtls/xray-core/common/protocol/http" "github.com/xtls/xray-core/common/session" "github.com/xtls/xray-core/features/routing" routing_session "github.com/xtls/xray-core/features/routing/session" - "google.golang.org/protobuf/proto" + "github.com/xtls/xray-core/infra/conf" ) func withBackground() routing.Context { @@ -302,54 +300,13 @@ func TestRoutingRule(t *testing.T) { } } -func loadGeositeWithAttr(file string, siteWithAttr string) ([]*Domain, error) { - - parts := strings.Split(siteWithAttr, "@") - if len(parts) == 0 { - return nil, errors.New("empty site") - } - country := strings.ToUpper(parts[0]) - attrs := router.ParseAttrs(parts[1:]) - - path, err := getAssetPath(file) - if err != nil { - return nil, err - } - geositeBytes, err := filesystem.ReadFile(path) - if err != nil { - return nil, err - } - - var geositeList GeoSiteList - if err := proto.Unmarshal(geositeBytes, &geositeList); err != nil { - return nil, err - } - var domainList []*Domain - for _, site := range geositeList.Entry { - if site.CountryCode == country { - domainList = site.Domain - } - } - if domainList == nil { - return nil, errors.New("country not found: " + country) - } +func TestChinaSites(t *testing.T) { + domains, err := conf.ParseDomainRule("geosite:cn") - if attrs.IsEmpty() { - return domainList, nil + if runtime.GOOS != "windows" && runtime.GOOS != "wasm" { + domains, err = router.GetDomainList(domains) } - filteredDomains := make([]*router.Domain, 0, len(domainList)) - for _, domain := range domainList { - if attrs.Match(domain) { - filteredDomains = append(filteredDomains, domain) - } - } - - return filteredDomains, nil -} - -func TestChinaSites(t *testing.T) { - domains, err := loadGeositeWithAttr("geosite.dat", "CN") common.Must(err) acMatcher, err := NewMphMatcherGroup(domains) @@ -391,7 +348,12 @@ func TestChinaSites(t *testing.T) { } func TestChinaSitesWithAttrs(t *testing.T) { - domains, err := loadGeositeWithAttr("geosite.dat", "google@CN") + domains, err := conf.ParseDomainRule("geosite:google@cn") + + if runtime.GOOS != "windows" && runtime.GOOS != "wasm" { + domains, err = router.GetDomainList(domains) + } + common.Must(err) acMatcher, err := NewMphMatcherGroup(domains) @@ -433,7 +395,7 @@ func TestChinaSitesWithAttrs(t *testing.T) { } func BenchmarkMphDomainMatcher(b *testing.B) { - domains, err := loadGeositeWithAttr("geosite.dat", "CN") + domains, err := conf.ParseDomainRule("geosite:cn") common.Must(err) matcher, err := NewMphMatcherGroup(domains) diff --git a/app/router/config.go b/app/router/config.go index bb0776a6c19e..5a39c13b9a00 100644 --- a/app/router/config.go +++ b/app/router/config.go @@ -112,7 +112,7 @@ func (rr *RoutingRule) BuildCondition() (Condition, error) { domains := rr.Domain if runtime.GOOS != "windows" && runtime.GOOS != "wasm" { var err error - domains, err = getDomainList(rr.Domain) + domains, err = GetDomainList(rr.Domain) if err != nil { return nil, errors.New("failed to build domains from mmap").Base(err) } @@ -218,7 +218,7 @@ func GetGeoIPList(ips []*GeoIP) ([]*GeoIP, error) { } -func getDomainList(domains []*Domain) ([]*Domain, error) { +func GetDomainList(domains []*Domain) ([]*Domain, error) { domainList := []*Domain{} for _, domain := range domains { val := strings.Split(domain.Value, "_") diff --git a/common/platform/others.go b/common/platform/others.go index be86b6faaac2..6c89994cb6c5 100644 --- a/common/platform/others.go +++ b/common/platform/others.go @@ -4,6 +4,7 @@ package platform import ( + "flag" "os" "path/filepath" ) @@ -12,6 +13,10 @@ func LineSeparator() string { return "\n" } +func IsTest() bool { + return flag.Lookup("test.v") != nil +} + // GetAssetLocation searches for `file` in the env dir, the executable dir, and certain locations func GetAssetLocation(file string) string { assetPath := NewEnvFlag(AssetLocation).GetValue(getExecutableDir) @@ -30,6 +35,18 @@ func GetAssetLocation(file string) string { return p } + if IsTest() { + path := filepath.Join("..", "..", "resources", file) + _, err := os.Stat(path) + if os.IsNotExist(err) { + return defPath + } + if err != nil { + return defPath + } + return path + } + // asset not found, let the caller throw out the error return defPath } diff --git a/infra/conf/dns.go b/infra/conf/dns.go index 6ec307c66d02..f6d56913e313 100644 --- a/infra/conf/dns.go +++ b/infra/conf/dns.go @@ -89,7 +89,7 @@ func (c *NameServerConfig) Build() (*dns.NameServer, error) { var originalRules []*dns.NameServer_OriginalRule for _, rule := range c.Domains { - parsedDomain, err := parseDomainRule(rule) + parsedDomain, err := ParseDomainRule(rule) if err != nil { return nil, errors.New("invalid domain rule: ", rule).Base(err) } diff --git a/infra/conf/router.go b/infra/conf/router.go index 8aa15065872d..c5813b4ab0d8 100644 --- a/infra/conf/router.go +++ b/infra/conf/router.go @@ -291,7 +291,7 @@ func loadGeositeWithAttr(file string, siteWithAttr string) ([]*router.Domain, er return filteredDomains, nil } -func parseDomainRule(domain string) ([]*router.Domain, error) { +func ParseDomainRule(domain string) ([]*router.Domain, error) { if strings.HasPrefix(domain, "geosite:") { country := strings.ToUpper(domain[8:]) domains, err := loadGeositeWithAttr("geosite.dat", country) @@ -489,7 +489,7 @@ func parseFieldRule(msg json.RawMessage) (*router.RoutingRule, error) { if rawFieldRule.Domain != nil { for _, domain := range *rawFieldRule.Domain { - rules, err := parseDomainRule(domain) + rules, err := ParseDomainRule(domain) if err != nil { return nil, errors.New("failed to parse domain rule: ", domain).Base(err) } @@ -499,7 +499,7 @@ func parseFieldRule(msg json.RawMessage) (*router.RoutingRule, error) { if rawFieldRule.Domains != nil { for _, domain := range *rawFieldRule.Domains { - rules, err := parseDomainRule(domain) + rules, err := ParseDomainRule(domain) if err != nil { return nil, errors.New("failed to parse domain rule: ", domain).Base(err) } From 0dd86d0b9bcd6132719b353a8860d41680042dfb Mon Sep 17 00:00:00 2001 From: hossinasaadi Date: Tue, 6 Jan 2026 18:36:51 +0330 Subject: [PATCH 4/8] improve geoip test --- app/router/condition_geoip_test.go | 81 +++++++++--------------------- app/router/condition_test.go | 14 +++--- 2 files changed, 31 insertions(+), 64 deletions(-) diff --git a/app/router/condition_geoip_test.go b/app/router/condition_geoip_test.go index b712db9e1f68..0810f35a3916 100644 --- a/app/router/condition_geoip_test.go +++ b/app/router/condition_geoip_test.go @@ -1,40 +1,15 @@ package router_test import ( - "fmt" - "os" - "path/filepath" + "runtime" "testing" "github.com/xtls/xray-core/app/router" "github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common/net" - "github.com/xtls/xray-core/common/platform" - "github.com/xtls/xray-core/common/platform/filesystem" - "google.golang.org/protobuf/proto" + "github.com/xtls/xray-core/infra/conf" ) -func getAssetPath(file string) (string, error) { - path := platform.GetAssetLocation(file) - _, err := os.Stat(path) - if os.IsNotExist(err) { - path := filepath.Join("..", "..", "resources", file) - _, err := os.Stat(path) - if os.IsNotExist(err) { - return "", fmt.Errorf("can't find %s in standard asset locations or {project_root}/resources", file) - } - if err != nil { - return "", fmt.Errorf("can't stat %s: %v", path, err) - } - return path, nil - } - if err != nil { - return "", fmt.Errorf("can't stat %s: %v", path, err) - } - - return path, nil -} - func TestGeoIPMatcher(t *testing.T) { cidrList := []*router.CIDR{ {Ip: []byte{0, 0, 0, 0}, Prefix: 8}, @@ -182,12 +157,11 @@ func TestGeoIPReverseMatcher(t *testing.T) { } func TestGeoIPMatcher4CN(t *testing.T) { - ips, err := loadGeoIP("CN") + geo := "geoip:cn" + geoip, err := loadGeoIP(geo) common.Must(err) - matcher, err := router.BuildOptimizedGeoIPMatcher(&router.GeoIP{ - Cidr: ips, - }) + matcher, err := router.BuildOptimizedGeoIPMatcher(geoip) common.Must(err) if matcher.Match([]byte{8, 8, 8, 8}) { @@ -196,12 +170,11 @@ func TestGeoIPMatcher4CN(t *testing.T) { } func TestGeoIPMatcher6US(t *testing.T) { - ips, err := loadGeoIP("US") + geo := "geoip:us" + geoip, err := loadGeoIP(geo) common.Must(err) - matcher, err := router.BuildOptimizedGeoIPMatcher(&router.GeoIP{ - Cidr: ips, - }) + matcher, err := router.BuildOptimizedGeoIPMatcher(geoip) common.Must(err) if !matcher.Match(net.ParseAddress("2001:4860:4860::8888").IP()) { @@ -209,37 +182,32 @@ func TestGeoIPMatcher6US(t *testing.T) { } } -func loadGeoIP(country string) ([]*router.CIDR, error) { - path, err := getAssetPath("geoip.dat") - if err != nil { - return nil, err - } - geoipBytes, err := filesystem.ReadFile(path) +func loadGeoIP(geo string) (*router.GeoIP, error) { + geoip, err := conf.ToCidrList([]string{geo}) if err != nil { return nil, err } - var geoipList router.GeoIPList - if err := proto.Unmarshal(geoipBytes, &geoipList); err != nil { - return nil, err + if runtime.GOOS != "windows" && runtime.GOOS != "wasm" { + geoip, err = router.GetGeoIPList(geoip) + if err != nil { + return nil, err + } } - for _, geoip := range geoipList.Entry { - if geoip.CountryCode == country { - return geoip.Cidr, nil - } + if len(geoip) == 0 { + panic("country not found: " + geo) } - panic("country not found: " + country) + return geoip[0], nil } func BenchmarkGeoIPMatcher4CN(b *testing.B) { - ips, err := loadGeoIP("CN") + geo := "geoip:cn" + geoip, err := loadGeoIP(geo) common.Must(err) - matcher, err := router.BuildOptimizedGeoIPMatcher(&router.GeoIP{ - Cidr: ips, - }) + matcher, err := router.BuildOptimizedGeoIPMatcher(geoip) common.Must(err) b.ResetTimer() @@ -250,12 +218,11 @@ func BenchmarkGeoIPMatcher4CN(b *testing.B) { } func BenchmarkGeoIPMatcher6US(b *testing.B) { - ips, err := loadGeoIP("US") + geo := "geoip:us" + geoip, err := loadGeoIP(geo) common.Must(err) - matcher, err := router.BuildOptimizedGeoIPMatcher(&router.GeoIP{ - Cidr: ips, - }) + matcher, err := router.BuildOptimizedGeoIPMatcher(geoip) common.Must(err) b.ResetTimer() diff --git a/app/router/condition_test.go b/app/router/condition_test.go index 2296fb2e532d..fa669a0da10e 100644 --- a/app/router/condition_test.go +++ b/app/router/condition_test.go @@ -440,11 +440,11 @@ func BenchmarkMultiGeoIPMatcher(b *testing.B) { var geoips []*GeoIP { - ips, err := loadGeoIP("CN") + ips, err := loadGeoIP("geoip:cn") common.Must(err) geoips = append(geoips, &GeoIP{ CountryCode: "CN", - Cidr: ips, + Cidr: ips.Cidr, }) } @@ -453,25 +453,25 @@ func BenchmarkMultiGeoIPMatcher(b *testing.B) { common.Must(err) geoips = append(geoips, &GeoIP{ CountryCode: "JP", - Cidr: ips, + Cidr: ips.Cidr, }) } { - ips, err := loadGeoIP("CA") + ips, err := loadGeoIP("geoip:ca") common.Must(err) geoips = append(geoips, &GeoIP{ CountryCode: "CA", - Cidr: ips, + Cidr: ips.Cidr, }) } { - ips, err := loadGeoIP("US") + ips, err := loadGeoIP("geoip:us") common.Must(err) geoips = append(geoips, &GeoIP{ CountryCode: "US", - Cidr: ips, + Cidr: ips.Cidr, }) } From 08bdc8f7ee7a395cc29909880392514b836706e6 Mon Sep 17 00:00:00 2001 From: hossinasaadi Date: Tue, 6 Jan 2026 18:56:53 +0330 Subject: [PATCH 5/8] improve asset path for test --- common/platform/others.go | 27 +++++++++++++++------------ common/platform/windows.go | 26 +++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/common/platform/others.go b/common/platform/others.go index 6c89994cb6c5..2ce6bb33fe69 100644 --- a/common/platform/others.go +++ b/common/platform/others.go @@ -13,8 +13,19 @@ func LineSeparator() string { return "\n" } -func IsTest() bool { - return flag.Lookup("test.v") != nil +func TestAssetPath(file string) string { + if flag.Lookup("test.v") != nil { + path := filepath.Join("..", "..", "resources", file) + _, err := os.Stat(path) + if os.IsNotExist(err) { + return "" + } + if err != nil { + return "" + } + return path + } + return "" } // GetAssetLocation searches for `file` in the env dir, the executable dir, and certain locations @@ -35,16 +46,8 @@ func GetAssetLocation(file string) string { return p } - if IsTest() { - path := filepath.Join("..", "..", "resources", file) - _, err := os.Stat(path) - if os.IsNotExist(err) { - return defPath - } - if err != nil { - return defPath - } - return path + if testPath := TestAssetPath(file); testPath != "" { + return testPath } // asset not found, let the caller throw out the error diff --git a/common/platform/windows.go b/common/platform/windows.go index 684ddc9c30ce..ed6d72bf2ad5 100644 --- a/common/platform/windows.go +++ b/common/platform/windows.go @@ -3,15 +3,39 @@ package platform -import "path/filepath" +import ( + "flag" + "os" + "path/filepath" +) func LineSeparator() string { return "\r\n" } +func TestAssetPath(file string) string { + if flag.Lookup("test.v") != nil { + path := filepath.Join("..", "..", "resources", file) + _, err := os.Stat(path) + if os.IsNotExist(err) { + return "" + } + if err != nil { + return "" + } + return path + } + return "" +} + // GetAssetLocation searches for `file` in the env dir and the executable dir func GetAssetLocation(file string) string { assetPath := NewEnvFlag(AssetLocation).GetValue(getExecutableDir) + + if testPath := TestAssetPath(file); testPath != "" { + return testPath + } + return filepath.Join(assetPath, file) } From 68cdc1fb23ea2cbff0d8ff31e3693974af24b01f Mon Sep 17 00:00:00 2001 From: hossinasaadi Date: Tue, 6 Jan 2026 19:13:34 +0330 Subject: [PATCH 6/8] cleanup geosite --- app/router/condition_test.go | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/app/router/condition_test.go b/app/router/condition_test.go index fa669a0da10e..a5e5daf01048 100644 --- a/app/router/condition_test.go +++ b/app/router/condition_test.go @@ -300,13 +300,23 @@ func TestRoutingRule(t *testing.T) { } } -func TestChinaSites(t *testing.T) { - domains, err := conf.ParseDomainRule("geosite:cn") +func loadGeoSiteDomains(geo string) ([]*Domain, error) { + domains, err := conf.ParseDomainRule(geo) + if err != nil { + return nil, err + } if runtime.GOOS != "windows" && runtime.GOOS != "wasm" { domains, err = router.GetDomainList(domains) + if err != nil { + return nil, err + } } + return domains, nil +} +func TestChinaSites(t *testing.T) { + domains, err := loadGeoSiteDomains("geosite:cn") common.Must(err) acMatcher, err := NewMphMatcherGroup(domains) @@ -348,12 +358,7 @@ func TestChinaSites(t *testing.T) { } func TestChinaSitesWithAttrs(t *testing.T) { - domains, err := conf.ParseDomainRule("geosite:google@cn") - - if runtime.GOOS != "windows" && runtime.GOOS != "wasm" { - domains, err = router.GetDomainList(domains) - } - + domains, err := loadGeoSiteDomains("geosite:google@cn") common.Must(err) acMatcher, err := NewMphMatcherGroup(domains) @@ -395,7 +400,7 @@ func TestChinaSitesWithAttrs(t *testing.T) { } func BenchmarkMphDomainMatcher(b *testing.B) { - domains, err := conf.ParseDomainRule("geosite:cn") + domains, err := loadGeoSiteDomains("geosite:cn") common.Must(err) matcher, err := NewMphMatcherGroup(domains) From d417bbfe9dfedeceb7a64fb50d1cebf580f8c414 Mon Sep 17 00:00:00 2001 From: hossinasaadi Date: Wed, 7 Jan 2026 02:31:12 +0330 Subject: [PATCH 7/8] fix test flag --- common/platform/others.go | 21 +-------------------- common/platform/windows.go | 31 ++++++++++++------------------- 2 files changed, 13 insertions(+), 39 deletions(-) diff --git a/common/platform/others.go b/common/platform/others.go index 2ce6bb33fe69..41c51ddbc328 100644 --- a/common/platform/others.go +++ b/common/platform/others.go @@ -4,7 +4,6 @@ package platform import ( - "flag" "os" "path/filepath" ) @@ -13,21 +12,6 @@ func LineSeparator() string { return "\n" } -func TestAssetPath(file string) string { - if flag.Lookup("test.v") != nil { - path := filepath.Join("..", "..", "resources", file) - _, err := os.Stat(path) - if os.IsNotExist(err) { - return "" - } - if err != nil { - return "" - } - return path - } - return "" -} - // GetAssetLocation searches for `file` in the env dir, the executable dir, and certain locations func GetAssetLocation(file string) string { assetPath := NewEnvFlag(AssetLocation).GetValue(getExecutableDir) @@ -37,6 +21,7 @@ func GetAssetLocation(file string) string { filepath.Join("/usr/local/share/xray/", file), filepath.Join("/usr/share/xray/", file), filepath.Join("/opt/share/xray/", file), + filepath.Join("..", "..", "resources", file), } { if _, err := os.Stat(p); os.IsNotExist(err) { continue @@ -46,10 +31,6 @@ func GetAssetLocation(file string) string { return p } - if testPath := TestAssetPath(file); testPath != "" { - return testPath - } - // asset not found, let the caller throw out the error return defPath } diff --git a/common/platform/windows.go b/common/platform/windows.go index ed6d72bf2ad5..bebdeb053b7a 100644 --- a/common/platform/windows.go +++ b/common/platform/windows.go @@ -4,7 +4,6 @@ package platform import ( - "flag" "os" "path/filepath" ) @@ -13,30 +12,24 @@ func LineSeparator() string { return "\r\n" } -func TestAssetPath(file string) string { - if flag.Lookup("test.v") != nil { - path := filepath.Join("..", "..", "resources", file) - _, err := os.Stat(path) - if os.IsNotExist(err) { - return "" - } - if err != nil { - return "" - } - return path - } - return "" -} - // GetAssetLocation searches for `file` in the env dir and the executable dir func GetAssetLocation(file string) string { assetPath := NewEnvFlag(AssetLocation).GetValue(getExecutableDir) + defPath := filepath.Join(assetPath, file) + + for _, p := range []string{ + defPath, + filepath.Join("..", "..", "resources", file), + } { + if _, err := os.Stat(p); os.IsNotExist(err) { + continue + } - if testPath := TestAssetPath(file); testPath != "" { - return testPath + // asset found + return p } - return filepath.Join(assetPath, file) + return defPath } // GetCertLocation searches for `file` in the env dir and the executable dir From 7b6ac923256ea5a937f7d5d13bd23b9d4796c21a Mon Sep 17 00:00:00 2001 From: hossinasaadi Date: Wed, 7 Jan 2026 13:05:42 +0330 Subject: [PATCH 8/8] revert GetAssetLocation --- app/router/condition_geoip_test.go | 4 ++++ app/router/condition_test.go | 4 ++++ common/platform/others.go | 1 - common/platform/windows.go | 16 +--------------- 4 files changed, 9 insertions(+), 16 deletions(-) diff --git a/app/router/condition_geoip_test.go b/app/router/condition_geoip_test.go index 0810f35a3916..de289a712ad6 100644 --- a/app/router/condition_geoip_test.go +++ b/app/router/condition_geoip_test.go @@ -1,6 +1,8 @@ package router_test import ( + "os" + "path/filepath" "runtime" "testing" @@ -183,6 +185,8 @@ func TestGeoIPMatcher6US(t *testing.T) { } func loadGeoIP(geo string) (*router.GeoIP, error) { + os.Setenv("XRAY_LOCATION_ASSET", filepath.Join("..", "..", "resources")) + geoip, err := conf.ToCidrList([]string{geo}) if err != nil { return nil, err diff --git a/app/router/condition_test.go b/app/router/condition_test.go index a5e5daf01048..283e672518c3 100644 --- a/app/router/condition_test.go +++ b/app/router/condition_test.go @@ -1,6 +1,8 @@ package router_test import ( + "os" + "path/filepath" "runtime" "strconv" "testing" @@ -301,6 +303,8 @@ func TestRoutingRule(t *testing.T) { } func loadGeoSiteDomains(geo string) ([]*Domain, error) { + os.Setenv("XRAY_LOCATION_ASSET", filepath.Join("..", "..", "resources")) + domains, err := conf.ParseDomainRule(geo) if err != nil { return nil, err diff --git a/common/platform/others.go b/common/platform/others.go index 41c51ddbc328..be86b6faaac2 100644 --- a/common/platform/others.go +++ b/common/platform/others.go @@ -21,7 +21,6 @@ func GetAssetLocation(file string) string { filepath.Join("/usr/local/share/xray/", file), filepath.Join("/usr/share/xray/", file), filepath.Join("/opt/share/xray/", file), - filepath.Join("..", "..", "resources", file), } { if _, err := os.Stat(p); os.IsNotExist(err) { continue diff --git a/common/platform/windows.go b/common/platform/windows.go index bebdeb053b7a..5bd15520fa20 100644 --- a/common/platform/windows.go +++ b/common/platform/windows.go @@ -4,7 +4,6 @@ package platform import ( - "os" "path/filepath" ) @@ -15,21 +14,8 @@ func LineSeparator() string { // GetAssetLocation searches for `file` in the env dir and the executable dir func GetAssetLocation(file string) string { assetPath := NewEnvFlag(AssetLocation).GetValue(getExecutableDir) - defPath := filepath.Join(assetPath, file) - for _, p := range []string{ - defPath, - filepath.Join("..", "..", "resources", file), - } { - if _, err := os.Stat(p); os.IsNotExist(err) { - continue - } - - // asset found - return p - } - - return defPath + return filepath.Join(assetPath, file) } // GetCertLocation searches for `file` in the env dir and the executable dir