Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion app/dns/nameserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,22 @@ func (c *Client) MatchExpectedIPs(domain string, ips []net.IP) ([]net.IP, error)
}
newIps := []net.IP{}
for _, ip := range ips {
result := false
for _, matcher := range c.expectIPs {
if matcher.Match(ip) {
newIps = append(newIps, ip)
result = true
if !matcher.IsReverseMatch() {
break
}
} else if matcher.IsReverseMatch() {
result = false
break
}
}

if result {
newIps = append(newIps, ip)
}
}
if len(newIps) == 0 {
return nil, errExpectedIPNonMatch
Expand Down
13 changes: 12 additions & 1 deletion app/router/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,22 @@ func (m *MultiGeoIPMatcher) Apply(ctx routing.Context) bool {
ips = ctx.GetTargetIPs()
}
for _, ip := range ips {
result := false
for _, matcher := range m.matchers {
if matcher.Match(ip) {
return true
result = true
if !matcher.IsReverseMatch() {
break
}
} else if matcher.IsReverseMatch() {
result = false
break
}
}

if result {
return true
}
}
return false
}
Expand Down
4 changes: 4 additions & 0 deletions app/router/condition_geoip.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ func (m *GeoIPMatcher) SetReverseMatch(isReverseMatch bool) {
m.reverseMatch = isReverseMatch
}

func (m *GeoIPMatcher) IsReverseMatch() bool {
return m.reverseMatch
}

func (m *GeoIPMatcher) match4(ip uint32) bool {
if len(m.ip4) == 0 {
return false
Expand Down
41 changes: 30 additions & 11 deletions infra/conf/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,17 +416,35 @@ func parseDomainRule(domain string) ([]*router.Domain, error) {
return []*router.Domain{domainRule}, nil
}

func appendCidrsToList(list *[]*router.GeoIP, cidrs *[]*router.CIDR, isReverseMatch bool) {
if len(*cidrs) > 0 {
(*list) = append(*list, &router.GeoIP{
Cidr: *cidrs,
ReverseMatch: isReverseMatch,
})
(*cidrs) = []*router.CIDR{}
}
}

func ToCidrList(ips StringList) ([]*router.GeoIP, error) {
var geoipList []*router.GeoIP
var customCidrs []*router.CIDR

lastIpReverseMatch := false
for _, ip := range ips {
isReverseMatch := false
if strings.HasPrefix(ip, "!") {
isReverseMatch = true
ip = ip[1:]
}

if strings.HasPrefix(ip, "geoip:") {
appendCidrsToList(&geoipList, &customCidrs, lastIpReverseMatch)

country := ip[6:]
isReverseMatch := false
if strings.HasPrefix(ip, "geoip:!") {
country = ip[7:]
isReverseMatch = true
isReverseMatch = !isReverseMatch
}
if len(country) == 0 {
return nil, newError("empty country name in rule")
Expand Down Expand Up @@ -455,6 +473,8 @@ func ToCidrList(ips StringList) ([]*router.GeoIP, error) {
}
}
if isExtDatFile != 0 {
appendCidrsToList(&geoipList, &customCidrs, lastIpReverseMatch)

kv := strings.Split(ip[isExtDatFile:], ":")
if len(kv) != 2 {
return nil, newError("invalid external resource: ", ip)
Expand All @@ -466,10 +486,9 @@ func ToCidrList(ips StringList) ([]*router.GeoIP, error) {
return nil, newError("empty filename or empty country in rule")
}

isReverseMatch := false
if strings.HasPrefix(country, "!") {
country = country[1:]
isReverseMatch = true
isReverseMatch = !isReverseMatch
}
geoip, err := loadIP(filename, strings.ToUpper(country))
if err != nil {
Expand All @@ -485,19 +504,19 @@ func ToCidrList(ips StringList) ([]*router.GeoIP, error) {
continue
}

// last ip reverse match state changed, appends customCidrs to geoipList
if lastIpReverseMatch != isReverseMatch {
appendCidrsToList(&geoipList, &customCidrs, lastIpReverseMatch)
}
lastIpReverseMatch = isReverseMatch

ipRule, err := ParseIP(ip)
if err != nil {
return nil, newError("invalid IP: ", ip).Base(err)
}
customCidrs = append(customCidrs, ipRule)
}

if len(customCidrs) > 0 {
geoipList = append(geoipList, &router.GeoIP{
Cidr: customCidrs,
})
}

appendCidrsToList(&geoipList, &customCidrs, lastIpReverseMatch)
return geoipList, nil
}

Expand Down