clash/component/mmdb/mmdb.go

46 lines
711 B
Go
Raw Normal View History

2020-01-11 21:07:01 +08:00
package mmdb
import (
"sync"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/log"
"github.com/oschwald/geoip2-golang"
)
2021-10-10 23:44:09 +08:00
var (
mmdb *geoip2.Reader
once sync.Once
)
2020-01-11 21:07:01 +08:00
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())
}
})
}
2020-04-16 19:12:25 +08:00
func Verify() bool {
instance, err := geoip2.Open(C.Path.MMDB())
if err == nil {
instance.Close()
}
return err == nil
}
2020-01-11 21:07:01 +08:00
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
}