mihomo/component/geodata/geodata.go

45 lines
1 KiB
Go
Raw Permalink Normal View History

2021-11-17 16:03:47 +08:00
package geodata
import (
"fmt"
2023-11-03 21:01:45 +08:00
"github.com/metacubex/mihomo/component/geodata/router"
C "github.com/metacubex/mihomo/constant"
2021-11-17 16:03:47 +08:00
)
type loader struct {
LoaderImplementation
}
func (l *loader) LoadGeoSite(list string) ([]*router.Domain, error) {
2023-03-23 18:58:24 +08:00
return l.LoadSiteByPath(C.GeositeName, list)
2021-11-17 16:03:47 +08:00
}
func (l *loader) LoadGeoIP(country string) ([]*router.CIDR, error) {
2022-06-03 16:50:05 +08:00
return l.LoadIPByPath(C.GeoipName, country)
2021-11-17 16:03:47 +08: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
}