From 90713510221422d117caf7ed1bf64fb521a3b62d Mon Sep 17 00:00:00 2001 From: Kr328 <39107975+Kr328@users.noreply.github.com> Date: Sat, 11 Jan 2020 21:07:01 +0800 Subject: [PATCH] Chore: aggregate mmdb (#474) --- component/mmdb/mmdb.go | 35 +++++++++++++++++++++++++++++++++++ dns/filters.go | 12 ++++++------ dns/resolver.go | 10 ---------- rules/geoip.go | 21 ++------------------- 4 files changed, 43 insertions(+), 35 deletions(-) create mode 100644 component/mmdb/mmdb.go diff --git a/component/mmdb/mmdb.go b/component/mmdb/mmdb.go new file mode 100644 index 00000000..6bc2ad73 --- /dev/null +++ b/component/mmdb/mmdb.go @@ -0,0 +1,35 @@ +package mmdb + +import ( + "sync" + + C "github.com/Dreamacro/clash/constant" + "github.com/Dreamacro/clash/log" + + "github.com/oschwald/geoip2-golang" +) + +var mmdb *geoip2.Reader +var once sync.Once + +func LoadFromBytes(buffer []byte) { + once.Do(func() { + var err error + mmdb, err = geoip2.FromBytes(buffer) + if err != nil { + log.Fatalln("Can't load mmdb: %s", err.Error()) + } + }) +} + +func Instance() *geoip2.Reader { + once.Do(func() { + var err error + mmdb, err = geoip2.Open(C.Path.MMDB()) + if err != nil { + log.Fatalln("Can't load mmdb: %s", err.Error()) + } + }) + + return mmdb +} diff --git a/dns/filters.go b/dns/filters.go index abb99f47..456280fa 100644 --- a/dns/filters.go +++ b/dns/filters.go @@ -1,6 +1,10 @@ package dns -import "net" +import ( + "net" + + "github.com/Dreamacro/clash/component/mmdb" +) type fallbackFilter interface { Match(net.IP) bool @@ -9,11 +13,7 @@ type fallbackFilter interface { type geoipFilter struct{} func (gf *geoipFilter) Match(ip net.IP) bool { - if mmdb == nil { - return false - } - - record, _ := mmdb.Country(ip) + record, _ := mmdb.Instance().Country(ip) return record.Country.IsoCode == "CN" || record.Country.IsoCode == "" } diff --git a/dns/resolver.go b/dns/resolver.go index 6a9eeeba..7e76e819 100644 --- a/dns/resolver.go +++ b/dns/resolver.go @@ -6,17 +6,14 @@ import ( "errors" "net" "strings" - "sync" "time" "github.com/Dreamacro/clash/common/cache" "github.com/Dreamacro/clash/common/picker" trie "github.com/Dreamacro/clash/component/domain-trie" "github.com/Dreamacro/clash/component/fakeip" - C "github.com/Dreamacro/clash/constant" D "github.com/miekg/dns" - geoip2 "github.com/oschwald/geoip2-golang" "golang.org/x/sync/singleflight" ) @@ -30,9 +27,6 @@ var ( var ( globalSessionCache = tls.NewLRUClientSessionCache(64) - - mmdb *geoip2.Reader - once sync.Once ) type resolver interface { @@ -311,10 +305,6 @@ func New(config Config) *Resolver { fallbackFilters := []fallbackFilter{} if config.FallbackFilter.GeoIP { - once.Do(func() { - mmdb, _ = geoip2.Open(C.Path.MMDB()) - }) - fallbackFilters = append(fallbackFilters, &geoipFilter{}) } for _, ipnet := range config.FallbackFilter.IPCIDR { diff --git a/rules/geoip.go b/rules/geoip.go index 4b9029cf..cfe8b52b 100644 --- a/rules/geoip.go +++ b/rules/geoip.go @@ -1,17 +1,8 @@ package rules import ( - "sync" - + "github.com/Dreamacro/clash/component/mmdb" C "github.com/Dreamacro/clash/constant" - "github.com/Dreamacro/clash/log" - - "github.com/oschwald/geoip2-golang" -) - -var ( - mmdb *geoip2.Reader - once sync.Once ) type GEOIP struct { @@ -29,7 +20,7 @@ func (g *GEOIP) Match(metadata *C.Metadata) bool { if ip == nil { return false } - record, _ := mmdb.Country(ip) + record, _ := mmdb.Instance().Country(ip) return record.Country.IsoCode == g.country } @@ -46,14 +37,6 @@ func (g *GEOIP) NoResolveIP() bool { } func NewGEOIP(country string, adapter string, noResolveIP bool) *GEOIP { - once.Do(func() { - var err error - mmdb, err = geoip2.Open(C.Path.MMDB()) - if err != nil { - log.Fatalln("Can't load mmdb: %s", err.Error()) - } - }) - geoip := &GEOIP{ country: country, adapter: adapter,