github.com/chwjbn/xclash@v0.2.0/component/mmdb/mmdb.go (about)

     1  package mmdb
     2  
     3  import (
     4  	"sync"
     5  
     6  	C "github.com/chwjbn/xclash/constant"
     7  	"github.com/chwjbn/xclash/log"
     8  
     9  	"github.com/oschwald/geoip2-golang"
    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.Fatalln("Can't load mmdb: %s", err.Error())
    23  		}
    24  	})
    25  }
    26  
    27  func Verify() bool {
    28  	instance, err := geoip2.Open(C.Path.MMDB())
    29  	if err == nil {
    30  		instance.Close()
    31  	}
    32  	return err == nil
    33  }
    34  
    35  func Instance() *geoip2.Reader {
    36  	once.Do(func() {
    37  		var err error
    38  		mmdb, err = geoip2.Open(C.Path.MMDB())
    39  		if err != nil {
    40  			log.Fatalln("Can't load mmdb: %s", err.Error())
    41  		}
    42  	})
    43  
    44  	return mmdb
    45  }