Skip to content

Commit c375728

Browse files
committed
Revert geofile change
This reverts commit 36425d2. Revert "DNS: Fix parse domain and geoip (#5499)" This reverts commit 961c352. Revert "Routing: Reduce peak memory usage (#5488)" This reverts commit c715154.
1 parent 09f619d commit c375728

File tree

11 files changed

+224
-450
lines changed

11 files changed

+224
-450
lines changed

app/dns/dns.go

Lines changed: 0 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,12 @@ import (
1212
"sync"
1313
"time"
1414

15-
router "github.com/xtls/xray-core/app/router"
1615
"github.com/xtls/xray-core/common"
1716
"github.com/xtls/xray-core/common/errors"
1817
"github.com/xtls/xray-core/common/net"
19-
"github.com/xtls/xray-core/common/platform/filesystem"
2018
"github.com/xtls/xray-core/common/session"
2119
"github.com/xtls/xray-core/common/strmatcher"
2220
"github.com/xtls/xray-core/features/dns"
23-
"google.golang.org/protobuf/proto"
2421
)
2522

2623
// DNS is a DNS rely server.
@@ -100,25 +97,6 @@ func New(ctx context.Context, config *Config) (*DNS, error) {
10097
}
10198

10299
for _, ns := range config.NameServer {
103-
if runtime.GOOS != "windows" && runtime.GOOS != "wasm" {
104-
err := parseDomains(ns)
105-
if err != nil {
106-
return nil, errors.New("failed to parse dns domain rules: ").Base(err)
107-
}
108-
109-
expectedGeoip, err := router.GetGeoIPList(ns.ExpectedGeoip)
110-
if err != nil {
111-
return nil, errors.New("failed to parse dns expectIPs rules: ").Base(err)
112-
}
113-
ns.ExpectedGeoip = expectedGeoip
114-
115-
unexpectedGeoip, err := router.GetGeoIPList(ns.UnexpectedGeoip)
116-
if err != nil {
117-
return nil, errors.New("failed to parse dns unexpectedGeoip rules: ").Base(err)
118-
}
119-
ns.UnexpectedGeoip = unexpectedGeoip
120-
121-
}
122100
domainRuleCount += len(ns.PrioritizedDomain)
123101
}
124102

@@ -602,76 +580,3 @@ func detectGUIPlatform() bool {
602580
}
603581
return false
604582
}
605-
606-
func parseDomains(ns *NameServer) error {
607-
pureDomains := []*router.Domain{}
608-
609-
// convert to pure domain
610-
for _, pd := range ns.PrioritizedDomain {
611-
pureDomains = append(pureDomains, &router.Domain{
612-
Type: router.Domain_Type(pd.Type),
613-
Value: pd.Domain,
614-
})
615-
}
616-
617-
domainList := []*router.Domain{}
618-
for _, domain := range pureDomains {
619-
val := strings.Split(domain.Value, "_")
620-
if len(val) >= 2 {
621-
622-
fileName := val[0]
623-
code := val[1]
624-
625-
bs, err := filesystem.ReadAsset(fileName)
626-
if err != nil {
627-
return errors.New("failed to load file: ", fileName).Base(err)
628-
}
629-
bs = filesystem.Find(bs, []byte(code))
630-
var geosite router.GeoSite
631-
632-
if err := proto.Unmarshal(bs, &geosite); err != nil {
633-
return errors.New("failed Unmarshal :").Base(err)
634-
}
635-
636-
// parse attr
637-
if len(val) == 3 {
638-
siteWithAttr := strings.Split(val[2], ",")
639-
attrs := router.ParseAttrs(siteWithAttr)
640-
if !attrs.IsEmpty() {
641-
filteredDomains := make([]*router.Domain, 0, len(pureDomains))
642-
for _, domain := range geosite.Domain {
643-
if attrs.Match(domain) {
644-
filteredDomains = append(filteredDomains, domain)
645-
}
646-
}
647-
geosite.Domain = filteredDomains
648-
}
649-
650-
}
651-
652-
domainList = append(domainList, geosite.Domain...)
653-
654-
// update ns.OriginalRules Size
655-
ruleTag := strings.Join(val, ":")
656-
for i, oRule := range ns.OriginalRules {
657-
if oRule.Rule == strings.ToLower(ruleTag) {
658-
ns.OriginalRules[i].Size = uint32(len(geosite.Domain))
659-
}
660-
}
661-
662-
} else {
663-
domainList = append(domainList, domain)
664-
}
665-
}
666-
667-
// convert back to NameServer_PriorityDomain
668-
ns.PrioritizedDomain = []*NameServer_PriorityDomain{}
669-
for _, pd := range domainList {
670-
ns.PrioritizedDomain = append(ns.PrioritizedDomain, &NameServer_PriorityDomain{
671-
Type: ToDomainMatchingType(pd.Type),
672-
Domain: pd.Value,
673-
})
674-
}
675-
676-
return nil
677-
}

app/dns/dns_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ func TestIPMatch(t *testing.T) {
541541
},
542542
ExpectedGeoip: []*router.GeoIP{
543543
{
544-
// local
544+
CountryCode: "local",
545545
Cidr: []*router.CIDR{
546546
{
547547
// inner ip, will not match
@@ -565,7 +565,7 @@ func TestIPMatch(t *testing.T) {
565565
},
566566
ExpectedGeoip: []*router.GeoIP{
567567
{
568-
// test
568+
CountryCode: "test",
569569
Cidr: []*router.CIDR{
570570
{
571571
Ip: []byte{8, 8, 8, 8},
@@ -574,7 +574,7 @@ func TestIPMatch(t *testing.T) {
574574
},
575575
},
576576
{
577-
// test
577+
CountryCode: "test",
578578
Cidr: []*router.CIDR{
579579
{
580580
Ip: []byte{8, 8, 8, 4},
@@ -669,7 +669,7 @@ func TestLocalDomain(t *testing.T) {
669669
},
670670
ExpectedGeoip: []*router.GeoIP{
671671
{ // Will match localhost, localhost-a and localhost-b,
672-
// local
672+
CountryCode: "local",
673673
Cidr: []*router.CIDR{
674674
{Ip: []byte{127, 0, 0, 2}, Prefix: 32},
675675
{Ip: []byte{127, 0, 0, 3}, Prefix: 32},

app/dns/nameserver.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -297,18 +297,3 @@ func ResolveIpOptionOverride(queryStrategy QueryStrategy, ipOption dns.IPOption)
297297
return ipOption
298298
}
299299
}
300-
301-
func ToDomainMatchingType(t router.Domain_Type) DomainMatchingType {
302-
switch t {
303-
case router.Domain_Domain:
304-
return DomainMatchingType_Subdomain
305-
case router.Domain_Full:
306-
return DomainMatchingType_Full
307-
case router.Domain_Plain:
308-
return DomainMatchingType_Keyword
309-
case router.Domain_Regex:
310-
return DomainMatchingType_Regex
311-
default:
312-
panic("unknown domain type")
313-
}
314-
}

app/router/condition.go

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -309,48 +309,6 @@ func (m *AttributeMatcher) Apply(ctx routing.Context) bool {
309309
return m.Match(attributes)
310310
}
311311

312-
// Geo attribute
313-
type GeoAttributeMatcher interface {
314-
Match(*Domain) bool
315-
}
316-
317-
type GeoBooleanMatcher string
318-
319-
func (m GeoBooleanMatcher) Match(domain *Domain) bool {
320-
for _, attr := range domain.Attribute {
321-
if attr.Key == string(m) {
322-
return true
323-
}
324-
}
325-
return false
326-
}
327-
328-
type GeoAttributeList struct {
329-
Matcher []GeoAttributeMatcher
330-
}
331-
332-
func (al *GeoAttributeList) Match(domain *Domain) bool {
333-
for _, matcher := range al.Matcher {
334-
if !matcher.Match(domain) {
335-
return false
336-
}
337-
}
338-
return true
339-
}
340-
341-
func (al *GeoAttributeList) IsEmpty() bool {
342-
return len(al.Matcher) == 0
343-
}
344-
345-
func ParseAttrs(attrs []string) *GeoAttributeList {
346-
al := new(GeoAttributeList)
347-
for _, attr := range attrs {
348-
lc := strings.ToLower(attr)
349-
al.Matcher = append(al.Matcher, GeoBooleanMatcher(lc))
350-
}
351-
return al
352-
}
353-
354312
type ProcessNameMatcher struct {
355313
ProcessNames []string
356314
AbsPaths []string
@@ -439,4 +397,4 @@ func (m *ProcessNameMatcher) Apply(ctx routing.Context) bool {
439397
}
440398
}
441399
return false
442-
}
400+
}

app/router/condition_geoip_test.go

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,40 @@
11
package router_test
22

33
import (
4+
"fmt"
45
"os"
56
"path/filepath"
6-
"runtime"
77
"testing"
88

99
"github.com/xtls/xray-core/app/router"
1010
"github.com/xtls/xray-core/common"
1111
"github.com/xtls/xray-core/common/net"
12-
"github.com/xtls/xray-core/infra/conf"
12+
"github.com/xtls/xray-core/common/platform"
13+
"github.com/xtls/xray-core/common/platform/filesystem"
14+
"google.golang.org/protobuf/proto"
1315
)
1416

17+
func getAssetPath(file string) (string, error) {
18+
path := platform.GetAssetLocation(file)
19+
_, err := os.Stat(path)
20+
if os.IsNotExist(err) {
21+
path := filepath.Join("..", "..", "resources", file)
22+
_, err := os.Stat(path)
23+
if os.IsNotExist(err) {
24+
return "", fmt.Errorf("can't find %s in standard asset locations or {project_root}/resources", file)
25+
}
26+
if err != nil {
27+
return "", fmt.Errorf("can't stat %s: %v", path, err)
28+
}
29+
return path, nil
30+
}
31+
if err != nil {
32+
return "", fmt.Errorf("can't stat %s: %v", path, err)
33+
}
34+
35+
return path, nil
36+
}
37+
1538
func TestGeoIPMatcher(t *testing.T) {
1639
cidrList := []*router.CIDR{
1740
{Ip: []byte{0, 0, 0, 0}, Prefix: 8},
@@ -159,11 +182,12 @@ func TestGeoIPReverseMatcher(t *testing.T) {
159182
}
160183

161184
func TestGeoIPMatcher4CN(t *testing.T) {
162-
geo := "geoip:cn"
163-
geoip, err := loadGeoIP(geo)
185+
ips, err := loadGeoIP("CN")
164186
common.Must(err)
165187

166-
matcher, err := router.BuildOptimizedGeoIPMatcher(geoip)
188+
matcher, err := router.BuildOptimizedGeoIPMatcher(&router.GeoIP{
189+
Cidr: ips,
190+
})
167191
common.Must(err)
168192

169193
if matcher.Match([]byte{8, 8, 8, 8}) {
@@ -172,46 +196,50 @@ func TestGeoIPMatcher4CN(t *testing.T) {
172196
}
173197

174198
func TestGeoIPMatcher6US(t *testing.T) {
175-
geo := "geoip:us"
176-
geoip, err := loadGeoIP(geo)
199+
ips, err := loadGeoIP("US")
177200
common.Must(err)
178201

179-
matcher, err := router.BuildOptimizedGeoIPMatcher(geoip)
202+
matcher, err := router.BuildOptimizedGeoIPMatcher(&router.GeoIP{
203+
Cidr: ips,
204+
})
180205
common.Must(err)
181206

182207
if !matcher.Match(net.ParseAddress("2001:4860:4860::8888").IP()) {
183208
t.Error("expect US geoip contain 2001:4860:4860::8888, but actually not")
184209
}
185210
}
186211

187-
func loadGeoIP(geo string) (*router.GeoIP, error) {
188-
os.Setenv("XRAY_LOCATION_ASSET", filepath.Join("..", "..", "resources"))
189-
190-
geoip, err := conf.ToCidrList([]string{geo})
212+
func loadGeoIP(country string) ([]*router.CIDR, error) {
213+
path, err := getAssetPath("geoip.dat")
214+
if err != nil {
215+
return nil, err
216+
}
217+
geoipBytes, err := filesystem.ReadFile(path)
191218
if err != nil {
192219
return nil, err
193220
}
194221

195-
if runtime.GOOS != "windows" && runtime.GOOS != "wasm" {
196-
geoip, err = router.GetGeoIPList(geoip)
197-
if err != nil {
198-
return nil, err
199-
}
222+
var geoipList router.GeoIPList
223+
if err := proto.Unmarshal(geoipBytes, &geoipList); err != nil {
224+
return nil, err
200225
}
201226

202-
if len(geoip) == 0 {
203-
panic("country not found: " + geo)
227+
for _, geoip := range geoipList.Entry {
228+
if geoip.CountryCode == country {
229+
return geoip.Cidr, nil
230+
}
204231
}
205232

206-
return geoip[0], nil
233+
panic("country not found: " + country)
207234
}
208235

209236
func BenchmarkGeoIPMatcher4CN(b *testing.B) {
210-
geo := "geoip:cn"
211-
geoip, err := loadGeoIP(geo)
237+
ips, err := loadGeoIP("CN")
212238
common.Must(err)
213239

214-
matcher, err := router.BuildOptimizedGeoIPMatcher(geoip)
240+
matcher, err := router.BuildOptimizedGeoIPMatcher(&router.GeoIP{
241+
Cidr: ips,
242+
})
215243
common.Must(err)
216244

217245
b.ResetTimer()
@@ -222,11 +250,12 @@ func BenchmarkGeoIPMatcher4CN(b *testing.B) {
222250
}
223251

224252
func BenchmarkGeoIPMatcher6US(b *testing.B) {
225-
geo := "geoip:us"
226-
geoip, err := loadGeoIP(geo)
253+
ips, err := loadGeoIP("US")
227254
common.Must(err)
228255

229-
matcher, err := router.BuildOptimizedGeoIPMatcher(geoip)
256+
matcher, err := router.BuildOptimizedGeoIPMatcher(&router.GeoIP{
257+
Cidr: ips,
258+
})
230259
common.Must(err)
231260

232261
b.ResetTimer()

0 commit comments

Comments
 (0)