From c745ea63b2ab4383f2cef737ee60daf1e205f9db Mon Sep 17 00:00:00 2001 From: MetaCubeX Date: Sat, 4 Jun 2022 02:58:14 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E4=BC=98=E5=8C=96GeoSite=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.go | 7 ------ config/initial.go | 5 ++-- rule/common/geosite.go | 55 +++++------------------------------------- 3 files changed, 9 insertions(+), 58 deletions(-) diff --git a/config/config.go b/config/config.go index d170617e..153dbcf3 100644 --- a/config/config.go +++ b/config/config.go @@ -583,13 +583,6 @@ func parseRules(cfg *RawConfig, proxies map[string]C.Proxy) ([]C.Rule, map[strin } params = trimArr(params) - - if ruleName == "GEOSITE" { - if err := initGeoSite(); err != nil { - return nil, nil, fmt.Errorf("can't initial GeoSite: %s", err) - } - initMode = false - } parsed, parseErr := R.ParseRule(ruleName, payload, target, params) if parseErr != nil { return nil, nil, fmt.Errorf("rules[%d] [%s] error: %s", idx, line, parseErr.Error()) diff --git a/config/initial.go b/config/initial.go index ce47c4d6..b41fdd1a 100644 --- a/config/initial.go +++ b/config/initial.go @@ -12,7 +12,7 @@ import ( "github.com/Dreamacro/clash/log" ) -var initMode = true +var initFlag bool func downloadMMDB(path string) (err error) { resp, err := http.Get(C.MmdbUrl) @@ -73,7 +73,7 @@ func initGeoSite() error { } log.Infoln("Download GeoSite.dat finish") } - if initMode { + if !initFlag { if err := geodata.Verify(C.GeositeName); err != nil { log.Warnln("GeoSite.dat invalid, remove and download: %s", err) if err := os.Remove(C.Path.GeoSite()); err != nil { @@ -83,6 +83,7 @@ func initGeoSite() error { return fmt.Errorf("can't download GeoSite.dat: %s", err.Error()) } } + initFlag = true } return nil } diff --git a/rule/common/geosite.go b/rule/common/geosite.go index 211cb570..10fe3152 100644 --- a/rule/common/geosite.go +++ b/rule/common/geosite.go @@ -2,19 +2,18 @@ package common import ( "fmt" - "io" - "net/http" - "os" - "github.com/Dreamacro/clash/component/geodata" + _ "github.com/Dreamacro/clash/component/geodata/memconservative" "github.com/Dreamacro/clash/component/geodata/router" + _ "github.com/Dreamacro/clash/component/geodata/standard" C "github.com/Dreamacro/clash/constant" "github.com/Dreamacro/clash/log" - - _ "github.com/Dreamacro/clash/component/geodata/memconservative" - _ "github.com/Dreamacro/clash/component/geodata/standard" + _ "unsafe" ) +//go:linkname initGeoSite github.com/Dreamacro/clash/config.initGeoSite +func initGeoSite() error + type GEOSITE struct { *Base country string @@ -80,45 +79,3 @@ func NewGEOSITE(country string, adapter string) (*GEOSITE, error) { } var _ C.Rule = (*GEOSITE)(nil) - -func downloadGeoSite(path string) (err error) { - resp, err := http.Get(C.GeoSiteUrl) - if err != nil { - return - } - defer resp.Body.Close() - - f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0o644) - if err != nil { - return err - } - defer f.Close() - _, err = io.Copy(f, resp.Body) - - return err -} - -func initGeoSite() error { - if _, err := os.Stat(C.Path.GeoSite()); os.IsNotExist(err) { - log.Infoln("Can't find GeoSite.dat, start download") - if err := downloadGeoSite(C.Path.GeoSite()); err != nil { - return fmt.Errorf("can't download GeoSite.dat: %s", err.Error()) - } - log.Infoln("Download GeoSite.dat finish") - } - if !initFlag { - err := geodata.Verify(C.GeositeName) - if err != nil { - log.Warnln("GeoSite.dat invalid, remove and download: %s", err) - if err := os.Remove(C.Path.GeoSite()); err != nil { - return fmt.Errorf("can't remove invalid GeoSite.dat: %s", err.Error()) - } - if err := downloadGeoSite(C.Path.GeoSite()); err != nil { - return fmt.Errorf("can't download GeoSite.dat: %s", err.Error()) - } - } else { - initFlag = true - } - } - return nil -}