From ca8ed0a01b78c86452b57f5b45c60be55bb1009c Mon Sep 17 00:00:00 2001 From: Clash-Mini Date: Sun, 6 Feb 2022 04:41:34 +0800 Subject: [PATCH] [Fix]GeoSite.dat initial in logic rule --- config/config.go | 4 ++-- config/initial.go | 2 +- rule/logic/common.go | 35 +++++++++++++++++++++++++++++++++-- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/config/config.go b/config/config.go index 2ff509d4..8d9465ec 100644 --- a/config/config.go +++ b/config/config.go @@ -556,7 +556,7 @@ func parseRules(cfg *RawConfig, proxies map[string]C.Proxy) ([]C.Rule, map[strin params = trimArr(params) if ruleName == "GEOSITE" { - if err := InitGeoSite(); err != nil { + if err := initGeoSite(); err != nil { return nil, nil, fmt.Errorf("can't initial GeoSite: %w", err) } } @@ -701,7 +701,7 @@ func parseFallbackIPCIDR(ips []string) ([]*net.IPNet, error) { func parseFallbackGeoSite(countries []string, rules []C.Rule) ([]*router.DomainMatcher, error) { var sites []*router.DomainMatcher if len(countries) > 0 { - if err := InitGeoSite(); err != nil { + if err := initGeoSite(); err != nil { return nil, fmt.Errorf("can't initial GeoSite: %w", err) } } diff --git a/config/initial.go b/config/initial.go index 44225761..5363d3ba 100644 --- a/config/initial.go +++ b/config/initial.go @@ -44,7 +44,7 @@ func downloadGeoSite(path string) (err error) { return err } -func InitGeoSite() error { +func initGeoSite() error { if _, err := os.Stat(C.Path.GeoSite()); os.IsNotExist(err) { log.Infoln("Need GeoSite but can't find GeoSite.dat, start download") if err := downloadGeoSite(C.Path.GeoSite()); err != nil { diff --git a/rule/logic/common.go b/rule/logic/common.go index 524a7eb1..f599df8d 100644 --- a/rule/logic/common.go +++ b/rule/logic/common.go @@ -3,11 +3,13 @@ package logic import ( "fmt" "github.com/Dreamacro/clash/common/collections" - "github.com/Dreamacro/clash/config" C "github.com/Dreamacro/clash/constant" "github.com/Dreamacro/clash/log" RC "github.com/Dreamacro/clash/rule/common" "github.com/Dreamacro/clash/rule/provider" + "io" + "net/http" + "os" "regexp" "strings" ) @@ -55,7 +57,7 @@ func payloadToRule(subPayload string) (C.Rule, error) { return parseRule(tp, payload, nil) } if tp == "GEOSITE" { - if err := config.InitGeoSite(); err != nil { + if err := initGeoSite(); err != nil { log.Errorln("can't initial GeoSite: %w", err) } } @@ -173,3 +175,32 @@ func findSubRuleRange(payload string, ruleRanges []Range) []Range { return subRuleRange } + +func downloadGeoSite(path string) (err error) { + resp, err := http.Get("https://cdn.jsdelivr.net/gh/Loyalsoldier/v2ray-rules-dat@release/geosite.dat") + 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("Need GeoSite but 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") + } + + return nil +}