From 68bf6f16ac7d6acb98975b939f13cf33f32d960a Mon Sep 17 00:00:00 2001 From: H1JK Date: Sun, 6 Aug 2023 23:34:10 +0800 Subject: [PATCH] refactor: Geodata initialization --- config/config.go | 13 +++++++++---- config/initial.go | 18 ------------------ 2 files changed, 9 insertions(+), 22 deletions(-) diff --git a/config/config.go b/config/config.go index ba61070c..401786c8 100644 --- a/config/config.go +++ b/config/config.go @@ -51,6 +51,7 @@ type General struct { IPv6 bool `json:"ipv6"` Interface string `json:"interface-name"` RoutingMark int `json:"-"` + GeoXUrl GeoXUrl `json:"geox-url"` GeodataMode bool `json:"geodata-mode"` GeodataLoader string `json:"geodata-loader"` TCPConcurrent bool `json:"tcp-concurrent"` @@ -273,7 +274,7 @@ type RawConfig struct { IPTables IPTables `yaml:"iptables"` Experimental Experimental `yaml:"experimental"` Profile Profile `yaml:"profile"` - GeoXUrl RawGeoXUrl `yaml:"geox-url"` + GeoXUrl GeoXUrl `yaml:"geox-url"` Proxy []map[string]any `yaml:"proxies"` ProxyGroup []map[string]any `yaml:"proxy-groups"` Rule []string `yaml:"rules"` @@ -282,7 +283,7 @@ type RawConfig struct { Listeners []map[string]any `yaml:"listeners"` } -type RawGeoXUrl struct { +type GeoXUrl struct { GeoIp string `yaml:"geoip" json:"geoip"` Mmdb string `yaml:"mmdb" json:"mmdb"` GeoSite string `yaml:"geosite" json:"geosite"` @@ -418,7 +419,7 @@ func UnmarshalRawConfig(buf []byte) (*RawConfig, error) { Profile: Profile{ StoreSelected: true, }, - GeoXUrl: RawGeoXUrl{ + GeoXUrl: GeoXUrl{ Mmdb: "https://fastly.jsdelivr.net/gh/MetaCubeX/meta-rules-dat@release/geoip.metadb", GeoIp: "https://fastly.jsdelivr.net/gh/MetaCubeX/meta-rules-dat@release/geoip.dat", GeoSite: "https://fastly.jsdelivr.net/gh/MetaCubeX/meta-rules-dat@release/geosite.dat", @@ -448,7 +449,7 @@ func ParseRawConfig(rawCfg *RawConfig) (*Config, error) { config.General = general if len(config.General.GlobalClientFingerprint) != 0 { - log.Debugln("GlobalClientFingerprint:%s", config.General.GlobalClientFingerprint) + log.Debugln("GlobalClientFingerprint: %s", config.General.GlobalClientFingerprint) tlsC.SetGlobalUtlsClient(config.General.GlobalClientFingerprint) } @@ -532,6 +533,9 @@ func ParseRawConfig(rawCfg *RawConfig) (*Config, error) { func parseGeneral(cfg *RawConfig) (*General, error) { externalUI := cfg.ExternalUI geodata.SetLoader(cfg.GeodataLoader) + C.GeoIpUrl = cfg.GeoXUrl.GeoIp + C.GeoSiteUrl = cfg.GeoXUrl.GeoSite + C.MmdbUrl = cfg.GeoXUrl.Mmdb // checkout externalUI exist if externalUI != "" { externalUI = C.Path.Resolve(externalUI) @@ -565,6 +569,7 @@ func parseGeneral(cfg *RawConfig) (*General, error) { IPv6: cfg.IPv6, Interface: cfg.Interface, RoutingMark: cfg.RoutingMark, + GeoXUrl: cfg.GeoXUrl, GeodataMode: cfg.GeodataMode, GeodataLoader: cfg.GeodataLoader, TCPConcurrent: cfg.TCPConcurrent, diff --git a/config/initial.go b/config/initial.go index 0921040d..6d6429ab 100644 --- a/config/initial.go +++ b/config/initial.go @@ -2,7 +2,6 @@ package config import ( "fmt" - "github.com/Dreamacro/clash/component/geodata" "os" C "github.com/Dreamacro/clash/constant" @@ -28,23 +27,6 @@ func Init(dir string) error { f.Write([]byte(`mixed-port: 7890`)) f.Close() } - buf, _ := os.ReadFile(C.Path.Config()) - rawCfg, err := UnmarshalRawConfig(buf) - if err != nil { - log.Errorln(err.Error()) - fmt.Printf("configuration file %s test failed\n", C.Path.Config()) - os.Exit(1) - } - if !C.GeodataMode { - C.GeodataMode = rawCfg.GeodataMode - } - C.GeoIpUrl = rawCfg.GeoXUrl.GeoIp - C.GeoSiteUrl = rawCfg.GeoXUrl.GeoSite - C.MmdbUrl = rawCfg.GeoXUrl.Mmdb - // initial GeoIP - if err := geodata.InitGeoIP(); err != nil { - return fmt.Errorf("can't initial GeoIP: %w", err) - } return nil }