Skip to content

Commit

Permalink
Feature: add geoip-code option
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamacro committed Aug 25, 2021
1 parent 121bc91 commit e0d3f92
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
20 changes: 12 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ type DNS struct {

// FallbackFilter config
type FallbackFilter struct {
GeoIP bool `yaml:"geoip"`
IPCIDR []*net.IPNet `yaml:"ipcidr"`
Domain []string `yaml:"domain"`
GeoIP bool `yaml:"geoip"`
GeoIPCode string `yaml:"geoip-code"`
IPCIDR []*net.IPNet `yaml:"ipcidr"`
Domain []string `yaml:"domain"`
}

// Profile config
Expand Down Expand Up @@ -113,9 +114,10 @@ type RawDNS struct {
}

type RawFallbackFilter struct {
GeoIP bool `yaml:"geoip"`
IPCIDR []string `yaml:"ipcidr"`
Domain []string `yaml:"domain"`
GeoIP bool `yaml:"geoip"`
GeoIPCode string `yaml:"geoip-code"`
IPCIDR []string `yaml:"ipcidr"`
Domain []string `yaml:"domain"`
}

type RawConfig struct {
Expand Down Expand Up @@ -172,8 +174,9 @@ func UnmarshalRawConfig(buf []byte) (*RawConfig, error) {
UseHosts: true,
FakeIPRange: "198.18.0.1/16",
FallbackFilter: RawFallbackFilter{
GeoIP: true,
IPCIDR: []string{},
GeoIP: true,
GeoIPCode: "CN",
IPCIDR: []string{},
},
DefaultNameserver: []string{
"114.114.114.114",
Expand Down Expand Up @@ -600,6 +603,7 @@ func parseDNS(cfg RawDNS, hosts *trie.DomainTrie) (*DNS, error) {
}

dnsCfg.FallbackFilter.GeoIP = cfg.FallbackFilter.GeoIP
dnsCfg.FallbackFilter.GeoIPCode = cfg.FallbackFilter.GeoIPCode
if fallbackip, err := parseFallbackIPCIDR(cfg.FallbackFilter.IPCIDR); err == nil {
dnsCfg.FallbackFilter.IPCIDR = fallbackip
}
Expand Down
6 changes: 4 additions & 2 deletions dns/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ type fallbackIPFilter interface {
Match(net.IP) bool
}

type geoipFilter struct{}
type geoipFilter struct {
code string
}

func (gf *geoipFilter) Match(ip net.IP) bool {
record, _ := mmdb.Instance().Country(ip)
return record.Country.IsoCode != "CN" && !ip.IsPrivate()
return record.Country.IsoCode != gf.code && !ip.IsPrivate()
}

type ipnetFilter struct {
Expand Down
11 changes: 7 additions & 4 deletions dns/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,10 @@ type NameServer struct {
}

type FallbackFilter struct {
GeoIP bool
IPCIDR []*net.IPNet
Domain []string
GeoIP bool
GeoIPCode string
IPCIDR []*net.IPNet
Domain []string
}

type Config struct {
Expand Down Expand Up @@ -344,7 +345,9 @@ func NewResolver(config Config) *Resolver {

fallbackIPFilters := []fallbackIPFilter{}
if config.FallbackFilter.GeoIP {
fallbackIPFilters = append(fallbackIPFilters, &geoipFilter{})
fallbackIPFilters = append(fallbackIPFilters, &geoipFilter{
code: config.FallbackFilter.GeoIPCode,
})
}
for _, ipnet := range config.FallbackFilter.IPCIDR {
fallbackIPFilters = append(fallbackIPFilters, &ipnetFilter{ipnet: ipnet})
Expand Down
7 changes: 4 additions & 3 deletions hub/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,10 @@ func updateDNS(c *config.DNS) {
Pool: c.FakeIPRange,
Hosts: c.Hosts,
FallbackFilter: dns.FallbackFilter{
GeoIP: c.FallbackFilter.GeoIP,
IPCIDR: c.FallbackFilter.IPCIDR,
Domain: c.FallbackFilter.Domain,
GeoIP: c.FallbackFilter.GeoIP,
GeoIPCode: c.FallbackFilter.GeoIPCode,
IPCIDR: c.FallbackFilter.IPCIDR,
Domain: c.FallbackFilter.Domain,
},
Default: c.DefaultNameserver,
Policy: c.NameServerPolicy,
Expand Down

0 comments on commit e0d3f92

Please sign in to comment.