github.com/yaling888/clash@v1.53.0/component/mmdb/mmdb.go (about)

     1  package mmdb
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/oschwald/geoip2-golang"
     7  	"github.com/phuslu/log"
     8  
     9  	C "github.com/yaling888/clash/constant"
    10  )
    11  
    12  var (
    13  	mmdb *geoip2.Reader
    14  	once sync.Once
    15  )
    16  
    17  func LoadFromBytes(buffer []byte) {
    18  	once.Do(func() {
    19  		var err error
    20  		mmdb, err = geoip2.FromBytes(buffer)
    21  		if err != nil {
    22  			log.Fatal().
    23  				Err(err).
    24  				Msg("Can't load mmdb")
    25  		}
    26  	})
    27  }
    28  
    29  func Verify() bool {
    30  	instance, err := geoip2.Open(C.Path.MMDB())
    31  	if err == nil {
    32  		_ = instance.Close()
    33  	}
    34  	return err == nil
    35  }
    36  
    37  func Instance() *geoip2.Reader {
    38  	once.Do(func() {
    39  		var err error
    40  		mmdb, err = geoip2.Open(C.Path.MMDB())
    41  		if err != nil {
    42  			log.Fatal().
    43  				Err(err).
    44  				Msg("Can't load mmdb")
    45  		}
    46  	})
    47  
    48  	return mmdb
    49  }