2021-11-17 16:03:47 +08:00
|
|
|
package memconservative
|
|
|
|
|
|
|
|
import (
|
2022-06-03 16:50:05 +08:00
|
|
|
"errors"
|
2021-11-17 16:03:47 +08:00
|
|
|
"fmt"
|
|
|
|
"runtime"
|
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/component/geodata"
|
|
|
|
"github.com/metacubex/mihomo/component/geodata/router"
|
2021-11-17 16:03:47 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type memConservativeLoader struct {
|
|
|
|
geoipcache GeoIPCache
|
|
|
|
geositecache GeoSiteCache
|
|
|
|
}
|
|
|
|
|
2022-06-03 16:50:05 +08:00
|
|
|
func (m *memConservativeLoader) LoadIPByPath(filename, country string) ([]*router.CIDR, error) {
|
2021-11-17 16:03:47 +08:00
|
|
|
defer runtime.GC()
|
|
|
|
geoip, err := m.geoipcache.Unmarshal(filename, country)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to decode geodata file: %s, base error: %s", filename, err.Error())
|
|
|
|
}
|
|
|
|
return geoip.Cidr, nil
|
|
|
|
}
|
|
|
|
|
2022-06-03 16:50:05 +08:00
|
|
|
func (m *memConservativeLoader) LoadIPByBytes(geoipBytes []byte, country string) ([]*router.CIDR, error) {
|
|
|
|
return nil, errors.New("memConservative do not support LoadIPByBytes")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memConservativeLoader) LoadSiteByPath(filename, list string) ([]*router.Domain, error) {
|
2021-11-17 16:03:47 +08:00
|
|
|
defer runtime.GC()
|
|
|
|
geosite, err := m.geositecache.Unmarshal(filename, list)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to decode geodata file: %s, base error: %s", filename, err.Error())
|
|
|
|
}
|
|
|
|
return geosite.Domain, nil
|
|
|
|
}
|
|
|
|
|
2022-06-03 16:50:05 +08:00
|
|
|
func (m *memConservativeLoader) LoadSiteByBytes(geositeBytes []byte, list string) ([]*router.Domain, error) {
|
|
|
|
return nil, errors.New("memConservative do not support LoadSiteByBytes")
|
|
|
|
}
|
|
|
|
|
2021-11-17 16:03:47 +08:00
|
|
|
func newMemConservativeLoader() geodata.LoaderImplementation {
|
|
|
|
return &memConservativeLoader{make(map[string]*router.GeoIP), make(map[string]*router.GeoSite)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
geodata.RegisterGeoDataLoaderImplementationCreator("memconservative", newMemConservativeLoader)
|
|
|
|
}
|