From f5933b91f143b35aabca936181d2887c136771bb Mon Sep 17 00:00:00 2001 From: gVisor bot Date: Sun, 6 Feb 2022 01:59:35 +0800 Subject: [PATCH] [build] --- component/geodata/geodata.go | 5 +++-- config/config.go | 16 ++++++++++------ constant/path.go | 37 ++++++++++++++++++++++++++++++++++++ constant/version.go | 9 ++++----- hub/executor/executor.go | 6 ++---- rule/common/process.go | 4 ---- 6 files changed, 56 insertions(+), 21 deletions(-) diff --git a/component/geodata/geodata.go b/component/geodata/geodata.go index 2a5b02f4..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/config/config.go b/config/config.go index 55b382b7..8d9465ec 100644 --- a/config/config.go +++ b/config/config.go @@ -42,6 +42,7 @@ type General struct { IPv6 bool `json:"ipv6"` Interface string `json:"-"` GeodataLoader string `json:"geodata-loader"` + AutoIptables bool `json:"auto-iptables"` } // Inbound @@ -172,6 +173,7 @@ type RawConfig struct { Secret string `yaml:"secret"` Interface string `yaml:"interface-name"` GeodataLoader string `yaml:"geodata-loader"` + AutoIptables bool `yaml:"auto-iptables"` ProxyProvider map[string]map[string]interface{} `yaml:"proxy-providers"` RuleProvider map[string]map[string]interface{} `yaml:"rule-providers"` @@ -203,6 +205,7 @@ func UnmarshalRawConfig(buf []byte) (*RawConfig, error) { BindAddress: "*", Mode: T.Rule, GeodataLoader: "memconservative", + AutoIptables: false, UnifiedDelay: false, Authentication: []string{}, LogLevel: log.INFO, @@ -260,7 +263,8 @@ func UnmarshalRawConfig(buf []byte) (*RawConfig, error) { func ParseRawConfig(rawCfg *RawConfig) (*Config, error) { config := &Config{} - + log.Infoln("Start initial configuration in progress") //Segment finished in xxm + startTime := time.Now() config.Experimental = &rawCfg.Experimental config.Profile = &rawCfg.Profile @@ -305,6 +309,8 @@ func ParseRawConfig(rawCfg *RawConfig) (*Config, error) { config.Users = parseAuthentication(rawCfg.Authentication) + elapsedTime := time.Since(startTime) / time.Millisecond // duration in ms + log.Infoln("Initial configuration complete, total time: %dms", elapsedTime) //Segment finished in xxm return config, nil } @@ -341,6 +347,7 @@ func parseGeneral(cfg *RawConfig) (*General, error) { IPv6: cfg.IPv6, Interface: cfg.Interface, GeodataLoader: cfg.GeodataLoader, + AutoIptables: cfg.AutoIptables, }, nil } @@ -487,8 +494,7 @@ time = ClashTime() func parseRules(cfg *RawConfig, proxies map[string]C.Proxy) ([]C.Rule, map[string]*providerTypes.RuleProvider, error) { ruleProviders := map[string]*providerTypes.RuleProvider{} - - startTime := time.Now() + log.Infoln("Geodata Loader mode: %s", geodata.LoaderName()) // parse rule provider for name, mapping := range cfg.RuleProvider { rp, err := RP.ParseRuleProvider(name, mapping) @@ -563,9 +569,7 @@ func parseRules(cfg *RawConfig, proxies map[string]C.Proxy) ([]C.Rule, map[strin rules = append(rules, parsed) } } - elapsedTime := time.Since(startTime) / time.Millisecond // duration in ms - log.Infoln("Initialization time consuming %dms", elapsedTime) //Segment finished in xxm - log.Infoln("Geodata Loader mode: %s", geodata.LoaderName()) + runtime.GC() return rules, ruleProviders, nil diff --git a/constant/path.go b/constant/path.go index 1461e85b..4ace7ab0 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() @@ -65,10 +72,40 @@ func (p *path) Cache() string { } func (p *path) GeoIP() string { + 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 { + 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") } diff --git a/constant/version.go b/constant/version.go index 1d4e6e7b..9bee7777 100644 --- a/constant/version.go +++ b/constant/version.go @@ -1,9 +1,8 @@ package constant var ( - Meta = true - Version = "1.9.0" - BuildTime = "unknown time" - AutoIptables string - ClashName = "Clash.Meta" + Meta = true + Version = "1.9.1" + BuildTime = "unknown time" + ClashName = "Clash.Meta" ) diff --git a/hub/executor/executor.go b/hub/executor/executor.go index 3dc5d14c..27eef5e0 100644 --- a/hub/executor/executor.go +++ b/hub/executor/executor.go @@ -314,8 +314,7 @@ func patchSelectGroup(proxies map[string]C.Proxy) { } func updateIPTables(dns *config.DNS, general *config.General, tun *config.Tun) { - AutoIptables := C.AutoIptables - if runtime.GOOS != "linux" || dns.Listen == "" || general.TProxyPort == 0 || tun.Enable || AutoIptables != "Enable" { + if runtime.GOOS != "linux" || dns.Listen == "" || general.TProxyPort == 0 || tun.Enable || !general.AutoIptables { return } @@ -342,8 +341,7 @@ func updateIPTables(dns *config.DNS, general *config.General, tun *config.Tun) { func CleanUp() { P.CleanUp() - AutoIptables := C.AutoIptables - if runtime.GOOS == "linux" && AutoIptables == "Enable" { + if runtime.GOOS == "linux" { tproxy.CleanUpTProxyLinuxIPTables() } } diff --git a/rule/common/process.go b/rule/common/process.go index 6398a1a7..b7712d43 100644 --- a/rule/common/process.go +++ b/rule/common/process.go @@ -28,10 +28,6 @@ func (ps *Process) Match(metadata *C.Metadata) bool { return strings.EqualFold(metadata.Process, ps.process) } - if C.AutoIptables == "Enable" { - return false - } - key := fmt.Sprintf("%s:%s:%s", metadata.NetWork.String(), metadata.SrcIP.String(), metadata.SrcPort) if strings.TrimSpace(metadata.Process) == "" { cached, hit := processCache.Get(key)