Chore: aggregate mmdb (#474)

This commit is contained in:
Kr328 2020-01-11 21:07:01 +08:00 committed by Dreamacro
parent f688eda2c2
commit 9071351022
4 changed files with 43 additions and 35 deletions

35
component/mmdb/mmdb.go Normal file
View file

@ -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
}

View file

@ -1,6 +1,10 @@
package dns package dns
import "net" import (
"net"
"github.com/Dreamacro/clash/component/mmdb"
)
type fallbackFilter interface { type fallbackFilter interface {
Match(net.IP) bool Match(net.IP) bool
@ -9,11 +13,7 @@ type fallbackFilter interface {
type geoipFilter struct{} type geoipFilter struct{}
func (gf *geoipFilter) Match(ip net.IP) bool { func (gf *geoipFilter) Match(ip net.IP) bool {
if mmdb == nil { record, _ := mmdb.Instance().Country(ip)
return false
}
record, _ := mmdb.Country(ip)
return record.Country.IsoCode == "CN" || record.Country.IsoCode == "" return record.Country.IsoCode == "CN" || record.Country.IsoCode == ""
} }

View file

@ -6,17 +6,14 @@ import (
"errors" "errors"
"net" "net"
"strings" "strings"
"sync"
"time" "time"
"github.com/Dreamacro/clash/common/cache" "github.com/Dreamacro/clash/common/cache"
"github.com/Dreamacro/clash/common/picker" "github.com/Dreamacro/clash/common/picker"
trie "github.com/Dreamacro/clash/component/domain-trie" trie "github.com/Dreamacro/clash/component/domain-trie"
"github.com/Dreamacro/clash/component/fakeip" "github.com/Dreamacro/clash/component/fakeip"
C "github.com/Dreamacro/clash/constant"
D "github.com/miekg/dns" D "github.com/miekg/dns"
geoip2 "github.com/oschwald/geoip2-golang"
"golang.org/x/sync/singleflight" "golang.org/x/sync/singleflight"
) )
@ -30,9 +27,6 @@ var (
var ( var (
globalSessionCache = tls.NewLRUClientSessionCache(64) globalSessionCache = tls.NewLRUClientSessionCache(64)
mmdb *geoip2.Reader
once sync.Once
) )
type resolver interface { type resolver interface {
@ -311,10 +305,6 @@ func New(config Config) *Resolver {
fallbackFilters := []fallbackFilter{} fallbackFilters := []fallbackFilter{}
if config.FallbackFilter.GeoIP { if config.FallbackFilter.GeoIP {
once.Do(func() {
mmdb, _ = geoip2.Open(C.Path.MMDB())
})
fallbackFilters = append(fallbackFilters, &geoipFilter{}) fallbackFilters = append(fallbackFilters, &geoipFilter{})
} }
for _, ipnet := range config.FallbackFilter.IPCIDR { for _, ipnet := range config.FallbackFilter.IPCIDR {

View file

@ -1,17 +1,8 @@
package rules package rules
import ( import (
"sync" "github.com/Dreamacro/clash/component/mmdb"
C "github.com/Dreamacro/clash/constant" 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 { type GEOIP struct {
@ -29,7 +20,7 @@ func (g *GEOIP) Match(metadata *C.Metadata) bool {
if ip == nil { if ip == nil {
return false return false
} }
record, _ := mmdb.Country(ip) record, _ := mmdb.Instance().Country(ip)
return record.Country.IsoCode == g.country return record.Country.IsoCode == g.country
} }
@ -46,14 +37,6 @@ func (g *GEOIP) NoResolveIP() bool {
} }
func NewGEOIP(country string, adapter string, noResolveIP bool) *GEOIP { 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{ geoip := &GEOIP{
country: country, country: country,
adapter: adapter, adapter: adapter,