From ee6c1871a9568dde9aa0a03c87fc7112a528c6da Mon Sep 17 00:00:00 2001 From: Skyxim Date: Tue, 11 Jan 2022 22:17:24 +0800 Subject: [PATCH] [Refactor] lazy loading geosite.bat --- config/config.go | 11 +++++++++++ config/initial.go | 36 +----------------------------------- 2 files changed, 12 insertions(+), 35 deletions(-) diff --git a/config/config.go b/config/config.go index 73389540b..740d0a4ce 100644 --- a/config/config.go +++ b/config/config.go @@ -565,6 +565,12 @@ func parseRules(cfg *RawConfig, proxies map[string]C.Proxy) ([]C.Rule, map[strin 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()) + } else { + if parsed.RuleType() == C.GEOSITE { + if err := initGeoSite(); err != nil { + return nil, nil, fmt.Errorf("can't initial GeoSite: %w", err) + } + } } if mode != T.Script { @@ -699,6 +705,11 @@ 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 { + return nil, fmt.Errorf("can't initial GeoSite: %w", err) + } + } for _, country := range countries { found := false diff --git a/config/initial.go b/config/initial.go index 9d8288ba8..e9869e7e8 100644 --- a/config/initial.go +++ b/config/initial.go @@ -50,23 +50,6 @@ func initMMDB() error { return nil } -//func downloadGeoIP(path string) (err error) { -// resp, err := http.Get("https://cdn.jsdelivr.net/gh/Loyalsoldier/v2ray-rules-dat@release/geoip.dat") -// if err != nil { -// return -// } -// defer resp.Body.Close() -// -// f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0644) -// if err != nil { -// return err -// } -// defer f.Close() -// _, err = io.Copy(f, resp.Body) -// -// return err -//} - 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 { @@ -84,22 +67,9 @@ func downloadGeoSite(path string) (err error) { return err } -// -//func initGeoIP() error { -// if _, err := os.Stat(C.Path.GeoIP()); os.IsNotExist(err) { -// log.Infoln("Can't find GeoIP.dat, start download") -// if err := downloadGeoIP(C.Path.GeoIP()); err != nil { -// return fmt.Errorf("can't download GeoIP.dat: %s", err.Error()) -// } -// log.Infoln("Download GeoIP.dat finish") -// } -// -// return nil -//} - func initGeoSite() error { if _, err := os.Stat(C.Path.GeoSite()); os.IsNotExist(err) { - log.Infoln("Can't find GeoSite.dat, start download") + 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()) } @@ -139,9 +109,5 @@ func Init(dir string) error { return fmt.Errorf("can't initial MMDB: %w", err) } - // initial GeoSite - if err := initGeoSite(); err != nil { - return fmt.Errorf("can't initial GeoSite: %w", err) - } return nil }