diff --git a/component/geodata/geodata.go b/component/geodata/geodata.go index cdd141fbb..c23d0b520 100644 --- a/component/geodata/geodata.go +++ b/component/geodata/geodata.go @@ -3,6 +3,7 @@ package geodata import ( "errors" "fmt" + C "github.com/Dreamacro/clash/constant" "strings" "github.com/Dreamacro/clash/component/geodata/router" @@ -14,7 +15,7 @@ type loader struct { } func (l *loader) LoadGeoSite(list string) ([]*router.Domain, error) { - return l.LoadGeoSiteWithAttr("geosite.dat", list) + return l.LoadGeoSiteWithAttr(C.GeositeName, list) } func (l *loader) LoadGeoSiteWithAttr(file string, siteWithAttr string) ([]*router.Domain, error) { @@ -58,7 +59,7 @@ func (l *loader) LoadGeoSiteWithAttr(file string, siteWithAttr string) ([]*route } func (l *loader) LoadGeoIP(country string) ([]*router.CIDR, error) { - return l.LoadIP("geoip.dat", country) + return l.LoadIP(C.GeoipName, country) } var loaders map[string]func() LoaderImplementation diff --git a/constant/path.go b/constant/path.go index f0f4e8c31..8dc8967c0 100644 --- a/constant/path.go +++ b/constant/path.go @@ -1,13 +1,20 @@ package constant import ( + "io/ioutil" "os" P "path" "path/filepath" + "strings" ) const Name = "clash" +var ( + GeositeName = "GeoSite.dat" + GeoipName = "GeoIP.dat" +) + // Path is used to get the configuration path var Path = func() *path { homeDir, err := os.UserHomeDir() @@ -48,7 +55,6 @@ func (p *path) Resolve(path string) string { if !filepath.IsAbs(path) { return filepath.Join(p.HomeDir(), path) } - return path } @@ -65,11 +71,41 @@ func (p *path) Cache() string { } func (p *path) GeoIP() string { - return P.Join(p.homeDir, "geoip.dat") + files, err := ioutil.ReadDir(p.homeDir) + if err != nil { + return "" + } + for _, fi := range files { + if fi.IsDir() { + // 目录则直接跳过 + continue + } else { + if strings.EqualFold(fi.Name(), "GeoIP.dat") { + GeoipName = fi.Name() + return P.Join(p.homeDir, fi.Name()) + } + } + } + return P.Join(p.homeDir, "GeoIP.dat") } func (p *path) GeoSite() string { - return P.Join(p.homeDir, "geosite.dat") + files, err := ioutil.ReadDir(p.homeDir) + if err != nil { + return "" + } + for _, fi := range files { + if fi.IsDir() { + // 目录则直接跳过 + continue + } else { + if strings.EqualFold(fi.Name(), "GeoSite.dat") { + GeositeName = fi.Name() + return P.Join(p.homeDir, fi.Name()) + } + } + } + return P.Join(p.homeDir, "GeoSite.dat") } func (p *path) ScriptDir() string {