github.com/metacubex/mihomo@v1.18.5/component/geodata/geodata.go (about)

     1  package geodata
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/metacubex/mihomo/component/geodata/router"
     7  	C "github.com/metacubex/mihomo/constant"
     8  )
     9  
    10  type loader struct {
    11  	LoaderImplementation
    12  }
    13  
    14  func (l *loader) LoadGeoSite(list string) ([]*router.Domain, error) {
    15  	return l.LoadSiteByPath(C.GeositeName, list)
    16  }
    17  
    18  func (l *loader) LoadGeoIP(country string) ([]*router.CIDR, error) {
    19  	return l.LoadIPByPath(C.GeoipName, country)
    20  }
    21  
    22  var loaders map[string]func() LoaderImplementation
    23  
    24  func RegisterGeoDataLoaderImplementationCreator(name string, loader func() LoaderImplementation) {
    25  	if loaders == nil {
    26  		loaders = map[string]func() LoaderImplementation{}
    27  	}
    28  	loaders[name] = loader
    29  }
    30  
    31  func getGeoDataLoaderImplementation(name string) (LoaderImplementation, error) {
    32  	if geoLoader, ok := loaders[name]; ok {
    33  		return geoLoader(), nil
    34  	}
    35  	return nil, fmt.Errorf("unable to locate GeoData loader %s", name)
    36  }
    37  
    38  func GetGeoDataLoader(name string) (Loader, error) {
    39  	loadImpl, err := getGeoDataLoaderImplementation(name)
    40  	if err == nil {
    41  		return &loader{loadImpl}, nil
    42  	}
    43  	return nil, err
    44  }