mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2024-11-15 21:51:23 +08:00
46 lines
711 B
Go
46 lines
711 B
Go
package mmdb
|
|
|
|
import (
|
|
"sync"
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
"github.com/Dreamacro/clash/log"
|
|
|
|
"github.com/oschwald/geoip2-golang"
|
|
)
|
|
|
|
var (
|
|
mmdb *geoip2.Reader
|
|
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 Verify() bool {
|
|
instance, err := geoip2.Open(C.Path.MMDB())
|
|
if err == nil {
|
|
instance.Close()
|
|
}
|
|
return err == nil
|
|
}
|
|
|
|
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
|
|
}
|