2021-11-17 08:03:47 +00:00
|
|
|
package geodata
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/component/geodata/router"
|
2023-03-23 10:58:24 +00:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2021-11-17 08:03:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type loader struct {
|
|
|
|
LoaderImplementation
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *loader) LoadGeoSite(list string) ([]*router.Domain, error) {
|
2023-03-23 10:58:24 +00:00
|
|
|
return l.LoadSiteByPath(C.GeositeName, list)
|
2021-11-17 08:03:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *loader) LoadGeoIP(country string) ([]*router.CIDR, error) {
|
2022-06-03 08:50:05 +00:00
|
|
|
return l.LoadIPByPath(C.GeoipName, country)
|
2021-11-17 08:03:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var loaders map[string]func() LoaderImplementation
|
|
|
|
|
|
|
|
func RegisterGeoDataLoaderImplementationCreator(name string, loader func() LoaderImplementation) {
|
|
|
|
if loaders == nil {
|
|
|
|
loaders = map[string]func() LoaderImplementation{}
|
|
|
|
}
|
|
|
|
loaders[name] = loader
|
|
|
|
}
|
|
|
|
|
|
|
|
func getGeoDataLoaderImplementation(name string) (LoaderImplementation, error) {
|
|
|
|
if geoLoader, ok := loaders[name]; ok {
|
|
|
|
return geoLoader(), nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("unable to locate GeoData loader %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetGeoDataLoader(name string) (Loader, error) {
|
|
|
|
loadImpl, err := getGeoDataLoaderImplementation(name)
|
|
|
|
if err == nil {
|
|
|
|
return &loader{loadImpl}, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|