github.com/ipfans/trojan-go@v0.11.0/common/geodata/loader.go (about)

     1  package geodata
     2  
     3  import (
     4  	"runtime"
     5  
     6  	v2router "github.com/v2fly/v2ray-core/v4/app/router"
     7  )
     8  
     9  type geodataCache struct {
    10  	geoipCache
    11  	geositeCache
    12  }
    13  
    14  func NewGeodataLoader() GeodataLoader {
    15  	return &geodataCache{
    16  		make(map[string]*v2router.GeoIP),
    17  		make(map[string]*v2router.GeoSite),
    18  	}
    19  }
    20  
    21  func (g *geodataCache) LoadIP(filename, country string) ([]*v2router.CIDR, error) {
    22  	geoip, err := g.geoipCache.Unmarshal(filename, country)
    23  	if err != nil {
    24  		return nil, err
    25  	}
    26  	runtime.GC()
    27  	return geoip.Cidr, nil
    28  }
    29  
    30  func (g *geodataCache) LoadSite(filename, list string) ([]*v2router.Domain, error) {
    31  	geosite, err := g.geositeCache.Unmarshal(filename, list)
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  	runtime.GC()
    36  	return geosite.Domain, nil
    37  }
    38  
    39  func (g *geodataCache) LoadGeoIP(country string) ([]*v2router.CIDR, error) {
    40  	return g.LoadIP("geoip.dat", country)
    41  }
    42  
    43  func (g *geodataCache) LoadGeoSite(list string) ([]*v2router.Domain, error) {
    44  	return g.LoadSite("geosite.dat", list)
    45  }