From 0a180eeb40ba6281f07c68a6a039f3469abd45dd Mon Sep 17 00:00:00 2001 From: Clash-Mini Date: Sun, 6 Feb 2022 00:51:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BF=BD=E7=95=A5geosite=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=A4=A7=E5=B0=8F=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- component/geodata/geodata.go | 5 +++-- constant/path.go | 42 +++++++++++++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/component/geodata/geodata.go b/component/geodata/geodata.go index cdd141fb..c23d0b52 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 f0f4e8c3..8dc8967c 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 {