From 68201a88983099fee500e1d4acc7cff4bde36136 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E3=81=AE=E3=81=8B=E3=81=88=E3=81=A7?= Date: Thu, 8 Apr 2021 13:07:53 +0800 Subject: [PATCH 1/6] Feat: add reverse match for GeoIP --- common/matcher/geoip/conf.go | 20 +++++++++--- common/matcher/geoip/geoip.go | 29 +++++++++++++---- common/matcher/geoip/geoip.pb.go | 52 ++++++++++++++++++------------ common/matcher/geoip/geoip.proto | 1 + common/matcher/geoip/geoip_test.go | 36 +++++++++++++++++++++ 5 files changed, 106 insertions(+), 32 deletions(-) diff --git a/common/matcher/geoip/conf.go b/common/matcher/geoip/conf.go index 9c5fa42b..e1595340 100644 --- a/common/matcher/geoip/conf.go +++ b/common/matcher/geoip/conf.go @@ -99,14 +99,16 @@ func ParaseIPList(ips []string) ([]*GeoIP, error) { for _, ip := range ips { if strings.HasPrefix(ip, "geoip:") { country := ip[6:] + isReverseMatch := false geoipc, err := LoadGeoIP(strings.ToUpper(country)) if err != nil { return nil, newError("failed to load GeoIP: ", country).Base(err) } geoipList = append(geoipList, &GeoIP{ - CountryCode: strings.ToUpper(country), - Cidr: geoipc, + CountryCode: strings.ToUpper(country), + Cidr: geoipc, + ReverseMatch: isReverseMatch, }) continue } @@ -129,14 +131,24 @@ func ParaseIPList(ips []string) ([]*GeoIP, error) { filename := kv[0] country := kv[1] + if len(filename) == 0 || len(country) == 0 { + return nil, newError("empty filename or empty country in rule") + } + + isReverseMatch := false + if strings.HasPrefix(country, "!") { + country = country[1:] + isReverseMatch = true + } geoipc, err := LoadIPFile(filename, strings.ToUpper(country)) if err != nil { return nil, newError("failed to load IPs: ", country, " from ", filename).Base(err) } geoipList = append(geoipList, &GeoIP{ - CountryCode: strings.ToUpper(filename + "_" + country), - Cidr: geoipc, + CountryCode: strings.ToUpper(filename + "_" + country), + Cidr: geoipc, + ReverseMatch: isReverseMatch, }) continue diff --git a/common/matcher/geoip/geoip.go b/common/matcher/geoip/geoip.go index 08eaf26a..f702c682 100644 --- a/common/matcher/geoip/geoip.go +++ b/common/matcher/geoip/geoip.go @@ -15,11 +15,16 @@ type ipv6 struct { } type GeoIPMatcher struct { - countryCode string - ip4 []uint32 - prefix4 []uint8 - ip6 []ipv6 - prefix6 []uint8 + countryCode string + reverseMatch bool + ip4 []uint32 + prefix4 []uint8 + ip6 []ipv6 + prefix6 []uint8 +} + +func (m *GeoIPMatcher) SetReverseMatch(isReverseMatch bool) { + m.reverseMatch = isReverseMatch } func normalize4(ip uint32, prefix uint8) uint32 { @@ -149,8 +154,17 @@ func (m *GeoIPMatcher) match6(ip ipv6) bool { func (m *GeoIPMatcher) Match(ip net.IP) bool { switch len(ip) { case 4: + if m.reverseMatch { + return !m.match4(binary.BigEndian.Uint32(ip)) + } return m.match4(binary.BigEndian.Uint32(ip)) case 16: + if m.reverseMatch { + return !m.match6(ipv6{ + a: binary.BigEndian.Uint64(ip[0:8]), + b: binary.BigEndian.Uint64(ip[8:16]), + }) + } return m.match6(ipv6{ a: binary.BigEndian.Uint64(ip[0:8]), b: binary.BigEndian.Uint64(ip[8:16]), @@ -170,14 +184,15 @@ type GeoIPMatcherContainer struct { func (c *GeoIPMatcherContainer) Add(geoip *GeoIP) (*GeoIPMatcher, error) { if len(geoip.CountryCode) > 0 { for _, m := range c.matchers { - if m.countryCode == geoip.CountryCode { + if m.countryCode == geoip.CountryCode && m.reverseMatch == geoip.ReverseMatch { return m, nil } } } m := &GeoIPMatcher{ - countryCode: geoip.CountryCode, + countryCode: geoip.CountryCode, + reverseMatch: geoip.ReverseMatch, } if err := m.Init(geoip.Cidr); err != nil { return nil, err diff --git a/common/matcher/geoip/geoip.pb.go b/common/matcher/geoip/geoip.pb.go index 4176127f..589ccd49 100644 --- a/common/matcher/geoip/geoip.pb.go +++ b/common/matcher/geoip/geoip.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.25.0 -// protoc v3.15.6 +// protoc v3.15.7 // source: common/matcher/geoip/geoip.proto package geoip @@ -88,8 +88,9 @@ type GeoIP struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - CountryCode string `protobuf:"bytes,1,opt,name=country_code,json=countryCode,proto3" json:"country_code,omitempty"` - Cidr []*CIDR `protobuf:"bytes,2,rep,name=cidr,proto3" json:"cidr,omitempty"` + CountryCode string `protobuf:"bytes,1,opt,name=country_code,json=countryCode,proto3" json:"country_code,omitempty"` + Cidr []*CIDR `protobuf:"bytes,2,rep,name=cidr,proto3" json:"cidr,omitempty"` + ReverseMatch bool `protobuf:"varint,3,opt,name=reverse_match,json=reverseMatch,proto3" json:"reverse_match,omitempty"` } func (x *GeoIP) Reset() { @@ -138,6 +139,13 @@ func (x *GeoIP) GetCidr() []*CIDR { return nil } +func (x *GeoIP) GetReverseMatch() bool { + if x != nil { + return x.ReverseMatch + } + return false +} + type GeoIPList struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -194,25 +202,27 @@ var file_common_matcher_geoip_geoip_proto_rawDesc = []byte{ 0x6d, 0x61, 0x74, 0x63, 0x68, 0x65, 0x72, 0x2e, 0x67, 0x65, 0x6f, 0x69, 0x70, 0x22, 0x2e, 0x0a, 0x04, 0x43, 0x49, 0x44, 0x52, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x70, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x02, 0x69, 0x70, 0x12, 0x16, 0x0a, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, 0x18, - 0x02, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, 0x22, 0x5f, 0x0a, - 0x05, 0x47, 0x65, 0x6f, 0x49, 0x50, 0x12, 0x21, 0x0a, 0x0c, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x72, - 0x79, 0x5f, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x63, 0x6f, - 0x75, 0x6e, 0x74, 0x72, 0x79, 0x43, 0x6f, 0x64, 0x65, 0x12, 0x33, 0x0a, 0x04, 0x63, 0x69, 0x64, - 0x72, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x1f, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x63, - 0x6f, 0x6d, 0x6d, 0x6f, 0x6e, 0x2e, 0x6d, 0x61, 0x74, 0x63, 0x68, 0x65, 0x72, 0x2e, 0x67, 0x65, - 0x6f, 0x69, 0x70, 0x2e, 0x43, 0x49, 0x44, 0x52, 0x52, 0x04, 0x63, 0x69, 0x64, 0x72, 0x22, 0x43, - 0x0a, 0x09, 0x47, 0x65, 0x6f, 0x49, 0x50, 0x4c, 0x69, 0x73, 0x74, 0x12, 0x36, 0x0a, 0x05, 0x65, - 0x6e, 0x74, 0x72, 0x79, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x20, 0x2e, 0x78, 0x72, 0x61, - 0x79, 0x2e, 0x63, 0x6f, 0x6d, 0x6d, 0x6f, 0x6e, 0x2e, 0x6d, 0x61, 0x74, 0x63, 0x68, 0x65, 0x72, - 0x2e, 0x67, 0x65, 0x6f, 0x69, 0x70, 0x2e, 0x47, 0x65, 0x6f, 0x49, 0x50, 0x52, 0x05, 0x65, 0x6e, - 0x74, 0x72, 0x79, 0x42, 0x6d, 0x0a, 0x1d, 0x63, 0x6f, 0x6d, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, + 0x02, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, 0x22, 0x84, 0x01, + 0x0a, 0x05, 0x47, 0x65, 0x6f, 0x49, 0x50, 0x12, 0x21, 0x0a, 0x0c, 0x63, 0x6f, 0x75, 0x6e, 0x74, + 0x72, 0x79, 0x5f, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x63, + 0x6f, 0x75, 0x6e, 0x74, 0x72, 0x79, 0x43, 0x6f, 0x64, 0x65, 0x12, 0x33, 0x0a, 0x04, 0x63, 0x69, + 0x64, 0x72, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x1f, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x63, 0x6f, 0x6d, 0x6d, 0x6f, 0x6e, 0x2e, 0x6d, 0x61, 0x74, 0x63, 0x68, 0x65, 0x72, 0x2e, 0x67, - 0x65, 0x6f, 0x69, 0x70, 0x50, 0x01, 0x5a, 0x2e, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, - 0x6f, 0x6d, 0x2f, 0x78, 0x74, 0x6c, 0x73, 0x2f, 0x78, 0x72, 0x61, 0x79, 0x2d, 0x63, 0x6f, 0x72, - 0x65, 0x2f, 0x63, 0x6f, 0x6d, 0x6d, 0x6f, 0x6e, 0x2f, 0x6d, 0x61, 0x74, 0x63, 0x68, 0x65, 0x72, - 0x2f, 0x67, 0x65, 0x6f, 0x69, 0x70, 0xaa, 0x02, 0x19, 0x58, 0x72, 0x61, 0x79, 0x2e, 0x43, 0x6f, - 0x6d, 0x6d, 0x6f, 0x6e, 0x2e, 0x4d, 0x61, 0x74, 0x63, 0x68, 0x65, 0x72, 0x2e, 0x47, 0x65, 0x6f, - 0x49, 0x50, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x65, 0x6f, 0x69, 0x70, 0x2e, 0x43, 0x49, 0x44, 0x52, 0x52, 0x04, 0x63, 0x69, 0x64, 0x72, 0x12, + 0x23, 0x0a, 0x0d, 0x72, 0x65, 0x76, 0x65, 0x72, 0x73, 0x65, 0x5f, 0x6d, 0x61, 0x74, 0x63, 0x68, + 0x18, 0x03, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0c, 0x72, 0x65, 0x76, 0x65, 0x72, 0x73, 0x65, 0x4d, + 0x61, 0x74, 0x63, 0x68, 0x22, 0x43, 0x0a, 0x09, 0x47, 0x65, 0x6f, 0x49, 0x50, 0x4c, 0x69, 0x73, + 0x74, 0x12, 0x36, 0x0a, 0x05, 0x65, 0x6e, 0x74, 0x72, 0x79, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, + 0x32, 0x20, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x63, 0x6f, 0x6d, 0x6d, 0x6f, 0x6e, 0x2e, 0x6d, + 0x61, 0x74, 0x63, 0x68, 0x65, 0x72, 0x2e, 0x67, 0x65, 0x6f, 0x69, 0x70, 0x2e, 0x47, 0x65, 0x6f, + 0x49, 0x50, 0x52, 0x05, 0x65, 0x6e, 0x74, 0x72, 0x79, 0x42, 0x6d, 0x0a, 0x1d, 0x63, 0x6f, 0x6d, + 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x63, 0x6f, 0x6d, 0x6d, 0x6f, 0x6e, 0x2e, 0x6d, 0x61, 0x74, + 0x63, 0x68, 0x65, 0x72, 0x2e, 0x67, 0x65, 0x6f, 0x69, 0x70, 0x50, 0x01, 0x5a, 0x2e, 0x67, 0x69, + 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x78, 0x74, 0x6c, 0x73, 0x2f, 0x78, 0x72, + 0x61, 0x79, 0x2d, 0x63, 0x6f, 0x72, 0x65, 0x2f, 0x63, 0x6f, 0x6d, 0x6d, 0x6f, 0x6e, 0x2f, 0x6d, + 0x61, 0x74, 0x63, 0x68, 0x65, 0x72, 0x2f, 0x67, 0x65, 0x6f, 0x69, 0x70, 0xaa, 0x02, 0x19, 0x58, + 0x72, 0x61, 0x79, 0x2e, 0x43, 0x6f, 0x6d, 0x6d, 0x6f, 0x6e, 0x2e, 0x4d, 0x61, 0x74, 0x63, 0x68, + 0x65, 0x72, 0x2e, 0x47, 0x65, 0x6f, 0x49, 0x50, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/common/matcher/geoip/geoip.proto b/common/matcher/geoip/geoip.proto index a781ff09..d8a28398 100644 --- a/common/matcher/geoip/geoip.proto +++ b/common/matcher/geoip/geoip.proto @@ -18,6 +18,7 @@ message CIDR { message GeoIP { string country_code = 1; repeated CIDR cidr = 2; + bool reverse_match =3; } message GeoIPList { diff --git a/common/matcher/geoip/geoip_test.go b/common/matcher/geoip/geoip_test.go index f959b944..0e583ff3 100644 --- a/common/matcher/geoip/geoip_test.go +++ b/common/matcher/geoip/geoip_test.go @@ -135,6 +135,42 @@ func TestGeoIPMatcher4CN(t *testing.T) { } } +func TestGeoIPReverseMatcher(t *testing.T) { + cidrList := CIDRList{ + {Ip: []byte{8, 8, 8, 8}, Prefix: 32}, + {Ip: []byte{91, 108, 4, 0}, Prefix: 16}, + } + matcher := &GeoIPMatcher{} + matcher.SetReverseMatch(true) // Reverse match + common.Must(matcher.Init(cidrList)) + + testCases := []struct { + Input string + Output bool + }{ + { + Input: "8.8.8.8", + Output: false, + }, + { + Input: "2001:cdba::3257:9652", + Output: true, + }, + { + Input: "91.108.255.254", + Output: false, + }, + } + + for _, testCase := range testCases { + ip := net.ParseAddress(testCase.Input).IP() + actual := matcher.Match(ip) + if actual != testCase.Output { + t.Error("expect input", testCase.Input, "to be", testCase.Output, ", but actually", actual) + } + } +} + func TestGeoIPMatcher6US(t *testing.T) { ips, err := loadGeoIP("US") common.Must(err) From 44317bd657eb1d4beab5fbc9f0f64fd842cce6af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E3=81=AE=E3=81=8B=E3=81=88=E3=81=A7?= Date: Thu, 8 Apr 2021 13:19:25 +0800 Subject: [PATCH 2/6] Refine router config parse --- common/matcher/domain/conf/domain.go | 24 ++++++++++++++++++++---- common/matcher/geosite/attribute.go | 4 +++- infra/conf/router.go | 6 +++--- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/common/matcher/domain/conf/domain.go b/common/matcher/domain/conf/domain.go index 10a8cf57..524b39bd 100644 --- a/common/matcher/domain/conf/domain.go +++ b/common/matcher/domain/conf/domain.go @@ -44,20 +44,36 @@ func ParseDomainRule(domain string) ([]*dm.Domain, error) { domainRule := new(dm.Domain) switch { case strings.HasPrefix(domain, "regexp:"): + regexpVal := domain[7:] + if len(regexpVal) == 0 { + return nil, newError("empty regexp type of rule: ", domain) + } domainRule.Type = dm.MatchingType_Regex - domainRule.Value = domain[7:] + domainRule.Value = regexpVal case strings.HasPrefix(domain, "domain:"): + domainName := domain[7:] + if len(domainName) == 0 { + return nil, newError("empty domain type of rule: ", domain) + } domainRule.Type = dm.MatchingType_Subdomain - domainRule.Value = domain[7:] + domainRule.Value = domainName case strings.HasPrefix(domain, "full:"): + fullVal := domain[5:] + if len(fullVal) == 0 { + return nil, newError("empty full domain type of rule: ", domain) + } domainRule.Type = dm.MatchingType_Full - domainRule.Value = domain[5:] + domainRule.Value = fullVal case strings.HasPrefix(domain, "keyword:"): + keywordVal := domain[8:] + if len(keywordVal) == 0 { + return nil, newError("empty keyword type of rule: ", domain) + } domainRule.Type = dm.MatchingType_Keyword - domainRule.Value = domain[8:] + domainRule.Value = keywordVal case strings.HasPrefix(domain, "dotless:"): domainRule.Type = dm.MatchingType_Regex diff --git a/common/matcher/geosite/attribute.go b/common/matcher/geosite/attribute.go index a16361c1..c2fc0b30 100644 --- a/common/matcher/geosite/attribute.go +++ b/common/matcher/geosite/attribute.go @@ -1,5 +1,7 @@ package geosite +import "strings" + type AttributeList struct { matcher []AttributeMatcher } @@ -25,7 +27,7 @@ type BooleanMatcher string func (m BooleanMatcher) Match(domain *Domain) bool { for _, attr := range domain.Attribute { - if attr.Key == string(m) { + if strings.EqualFold(attr.GetKey(), string(m)) { return true } } diff --git a/infra/conf/router.go b/infra/conf/router.go index 1e005bf2..e905b672 100644 --- a/infra/conf/router.go +++ b/infra/conf/router.go @@ -261,21 +261,21 @@ func ParseRule(msg json.RawMessage) (*router.RoutingRule, error) { if err != nil { return nil, newError("invalid router rule").Base(err) } - if rawRule.Type == "field" { + if strings.EqualFold(rawRule.Type, "field") { fieldrule, err := parseFieldRule(msg) if err != nil { return nil, newError("invalid field rule").Base(err) } return fieldrule, nil } - if rawRule.Type == "chinaip" { + if strings.EqualFold(rawRule.Type, "chinaip") { chinaiprule, err := parseChinaIPRule(msg) if err != nil { return nil, newError("invalid chinaip rule").Base(err) } return chinaiprule, nil } - if rawRule.Type == "chinasites" { + if strings.EqualFold(rawRule.Type, "chinasites") { chinasitesrule, err := parseChinaSitesRule(msg) if err != nil { return nil, newError("invalid chinasites rule").Base(err) From 5d5beb2028b9848c3c1d208b1a058d6c6db6d866 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E3=81=AE=E3=81=8B=E3=81=88=E3=81=A7?= Date: Thu, 8 Apr 2021 13:25:22 +0800 Subject: [PATCH 3/6] Add test to ParaseIPList --- common/matcher/geoip/geoip_test.go | 45 ++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/common/matcher/geoip/geoip_test.go b/common/matcher/geoip/geoip_test.go index 0e583ff3..60295782 100644 --- a/common/matcher/geoip/geoip_test.go +++ b/common/matcher/geoip/geoip_test.go @@ -25,6 +25,27 @@ func init() { } } +func TestParaseIPList(t *testing.T) { + t.Log(os.Getenv("xray.location.asset")) + + common.Must(filesystem.CopyFile(platform.GetAssetLocation("geoiptestrouter.dat"), platform.GetAssetLocation("geoip.dat"))) + + ips := []string{ + "geoip:us", + "geoip:cn", + "geoip:!cn", + "ext:geoiptestrouter.dat:!cn", + "ext:geoiptestrouter.dat:ca", + "ext-ip:geoiptestrouter.dat:!cn", + "ext-ip:geoiptestrouter.dat:!ca", + } + + _, err := ParaseIPList(ips) + if err != nil { + t.Fatalf("Failed to parse geoip list, got %s", err) + } +} + func TestGeoIPMatcherContainer(t *testing.T) { container := &GeoIPMatcherContainer{} @@ -123,18 +144,6 @@ func TestGeoIPMatcher(t *testing.T) { } } -func TestGeoIPMatcher4CN(t *testing.T) { - ips, err := loadGeoIP("CN") - common.Must(err) - - matcher := &GeoIPMatcher{} - common.Must(matcher.Init(ips)) - - if matcher.Match([]byte{8, 8, 8, 8}) { - t.Error("expect CN geoip doesn't contain 8.8.8.8, but actually does") - } -} - func TestGeoIPReverseMatcher(t *testing.T) { cidrList := CIDRList{ {Ip: []byte{8, 8, 8, 8}, Prefix: 32}, @@ -171,6 +180,18 @@ func TestGeoIPReverseMatcher(t *testing.T) { } } +func TestGeoIPMatcher4CN(t *testing.T) { + ips, err := loadGeoIP("CN") + common.Must(err) + + matcher := &GeoIPMatcher{} + common.Must(matcher.Init(ips)) + + if matcher.Match([]byte{8, 8, 8, 8}) { + t.Error("expect CN geoip doesn't contain 8.8.8.8, but actually does") + } +} + func TestGeoIPMatcher6US(t *testing.T) { ips, err := loadGeoIP("US") common.Must(err) From 1c3abb2ec3ad72495d61763cbcad87fa6c99e1d7 Mon Sep 17 00:00:00 2001 From: JimhHan <50871214+JimhHan@users.noreply.github.com> Date: Thu, 8 Apr 2021 19:18:23 +0800 Subject: [PATCH 4/6] Fix: config loader --- common/matcher/geoip/conf.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/common/matcher/geoip/conf.go b/common/matcher/geoip/conf.go index e1595340..5760409a 100644 --- a/common/matcher/geoip/conf.go +++ b/common/matcher/geoip/conf.go @@ -100,6 +100,11 @@ func ParaseIPList(ips []string) ([]*GeoIP, error) { if strings.HasPrefix(ip, "geoip:") { country := ip[6:] isReverseMatch := false + if strings.HasPrefix(country, "!") { + country = country[1:] + isReverseMatch = true + } + geoipc, err := LoadGeoIP(strings.ToUpper(country)) if err != nil { return nil, newError("failed to load GeoIP: ", country).Base(err) From 095b17da6208793903d0e51409569b3013804623 Mon Sep 17 00:00:00 2001 From: JimhHan <50871214+JimhHan@users.noreply.github.com> Date: Thu, 8 Apr 2021 19:19:53 +0800 Subject: [PATCH 5/6] Fix typo --- common/matcher/geoip/conf.go | 2 +- common/matcher/geoip/geoip_test.go | 4 ++-- infra/conf/dns.go | 2 +- infra/conf/router.go | 4 ++-- infra/conf/xray.go | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/common/matcher/geoip/conf.go b/common/matcher/geoip/conf.go index 5760409a..4a997148 100644 --- a/common/matcher/geoip/conf.go +++ b/common/matcher/geoip/conf.go @@ -92,7 +92,7 @@ func find(data, code []byte) []byte { } } -func ParaseIPList(ips []string) ([]*GeoIP, error) { +func ParseIPList(ips []string) ([]*GeoIP, error) { var geoipList []*GeoIP var customCidrs []*CIDR diff --git a/common/matcher/geoip/geoip_test.go b/common/matcher/geoip/geoip_test.go index 60295782..b2bee0c4 100644 --- a/common/matcher/geoip/geoip_test.go +++ b/common/matcher/geoip/geoip_test.go @@ -25,7 +25,7 @@ func init() { } } -func TestParaseIPList(t *testing.T) { +func TestParseIPList(t *testing.T) { t.Log(os.Getenv("xray.location.asset")) common.Must(filesystem.CopyFile(platform.GetAssetLocation("geoiptestrouter.dat"), platform.GetAssetLocation("geoip.dat"))) @@ -40,7 +40,7 @@ func TestParaseIPList(t *testing.T) { "ext-ip:geoiptestrouter.dat:!ca", } - _, err := ParaseIPList(ips) + _, err := ParseIPList(ips) if err != nil { t.Fatalf("Failed to parse geoip list, got %s", err) } diff --git a/infra/conf/dns.go b/infra/conf/dns.go index 0cb3b766..0e685dc5 100644 --- a/infra/conf/dns.go +++ b/infra/conf/dns.go @@ -74,7 +74,7 @@ func (c *NameServerConfig) Build() (*dns.NameServer, error) { }) } - geoipList, err := geoip.ParaseIPList(c.ExpectIPs) + geoipList, err := geoip.ParseIPList(c.ExpectIPs) if err != nil { return nil, newError("invalid IP rule: ", c.ExpectIPs).Base(err) } diff --git a/infra/conf/router.go b/infra/conf/router.go index e905b672..8a59bddf 100644 --- a/infra/conf/router.go +++ b/infra/conf/router.go @@ -203,7 +203,7 @@ func parseFieldRule(msg json.RawMessage) (*router.RoutingRule, error) { } if rawFieldRule.IP != nil { - geoipList, err := geoip.ParaseIPList(*rawFieldRule.IP) + geoipList, err := geoip.ParseIPList(*rawFieldRule.IP) if err != nil { return nil, err } @@ -219,7 +219,7 @@ func parseFieldRule(msg json.RawMessage) (*router.RoutingRule, error) { } if rawFieldRule.SourceIP != nil { - geoipList, err := geoip.ParaseIPList(*rawFieldRule.SourceIP) + geoipList, err := geoip.ParseIPList(*rawFieldRule.SourceIP) if err != nil { return nil, err } diff --git a/infra/conf/xray.go b/infra/conf/xray.go index 1f89ebad..b8b1ba8a 100644 --- a/infra/conf/xray.go +++ b/infra/conf/xray.go @@ -101,7 +101,7 @@ func (c *SniffingConfig) Build() (*proxyman.SniffingConfig, error) { var exIP []*geoip.GeoIP if c.IPsExcluded != nil { - exip, err := geoip.ParaseIPList(*c.IPsExcluded) + exip, err := geoip.ParseIPList(*c.IPsExcluded) if err != nil { return nil, newError("failed to parse excluded ip").Base(err) } From eeb40c9ce2df5390eef75c67cb08507dc1cdb1cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E3=81=AE=E3=81=8B=E3=81=88=E3=81=A7?= Date: Thu, 8 Apr 2021 22:51:55 +0800 Subject: [PATCH 6/6] Refine tests --- common/matcher/geoip/geoip_test.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/common/matcher/geoip/geoip_test.go b/common/matcher/geoip/geoip_test.go index b2bee0c4..48bc5286 100644 --- a/common/matcher/geoip/geoip_test.go +++ b/common/matcher/geoip/geoip_test.go @@ -5,12 +5,12 @@ import ( "path/filepath" "testing" - "github.com/golang/protobuf/proto" "github.com/xtls/xray-core/common" . "github.com/xtls/xray-core/common/matcher/geoip" "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" ) func init() { @@ -20,16 +20,15 @@ func init() { if _, err := os.Stat(platform.GetAssetLocation("geoip.dat")); err != nil && os.IsNotExist(err) { common.Must(filesystem.CopyFile(platform.GetAssetLocation("geoip.dat"), filepath.Join(wd, "..", "..", "..", "resources", "geoip.dat"))) } + if _, err := os.Stat(platform.GetAssetLocation("geoiptestrouter.dat")); err != nil && os.IsNotExist(err) { + common.Must(filesystem.CopyFile(platform.GetAssetLocation("geoiptestrouter.dat"), filepath.Join(wd, "..", "..", "..", "resources", "geoip.dat"))) + } if _, err := os.Stat(platform.GetAssetLocation("geosite.dat")); err != nil && os.IsNotExist(err) { common.Must(filesystem.CopyFile(platform.GetAssetLocation("geosite.dat"), filepath.Join(wd, "..", "..", "..", "resources", "geosite.dat"))) } } func TestParseIPList(t *testing.T) { - t.Log(os.Getenv("xray.location.asset")) - - common.Must(filesystem.CopyFile(platform.GetAssetLocation("geoiptestrouter.dat"), platform.GetAssetLocation("geoip.dat"))) - ips := []string{ "geoip:us", "geoip:cn",