geodata.go 1.0 KB

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