This commit is contained in:
Clash-Mini 2022-02-06 01:59:35 +08:00
parent 28a1475f66
commit 4a446c4e31
6 changed files with 56 additions and 21 deletions

View File

@ -3,6 +3,7 @@ package geodata
import ( import (
"errors" "errors"
"fmt" "fmt"
C "github.com/Dreamacro/clash/constant"
"strings" "strings"
"github.com/Dreamacro/clash/component/geodata/router" "github.com/Dreamacro/clash/component/geodata/router"
@ -14,7 +15,7 @@ type loader struct {
} }
func (l *loader) LoadGeoSite(list string) ([]*router.Domain, error) { 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) { 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) { 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 var loaders map[string]func() LoaderImplementation

View File

@ -42,6 +42,7 @@ type General struct {
IPv6 bool `json:"ipv6"` IPv6 bool `json:"ipv6"`
Interface string `json:"-"` Interface string `json:"-"`
GeodataLoader string `json:"geodata-loader"` GeodataLoader string `json:"geodata-loader"`
AutoIptables bool `json:"auto-iptables"`
} }
// Inbound // Inbound
@ -172,6 +173,7 @@ type RawConfig struct {
Secret string `yaml:"secret"` Secret string `yaml:"secret"`
Interface string `yaml:"interface-name"` Interface string `yaml:"interface-name"`
GeodataLoader string `yaml:"geodata-loader"` GeodataLoader string `yaml:"geodata-loader"`
AutoIptables bool `yaml:"auto-iptables"`
ProxyProvider map[string]map[string]interface{} `yaml:"proxy-providers"` ProxyProvider map[string]map[string]interface{} `yaml:"proxy-providers"`
RuleProvider map[string]map[string]interface{} `yaml:"rule-providers"` RuleProvider map[string]map[string]interface{} `yaml:"rule-providers"`
@ -203,6 +205,7 @@ func UnmarshalRawConfig(buf []byte) (*RawConfig, error) {
BindAddress: "*", BindAddress: "*",
Mode: T.Rule, Mode: T.Rule,
GeodataLoader: "memconservative", GeodataLoader: "memconservative",
AutoIptables: false,
UnifiedDelay: false, UnifiedDelay: false,
Authentication: []string{}, Authentication: []string{},
LogLevel: log.INFO, LogLevel: log.INFO,
@ -260,7 +263,8 @@ func UnmarshalRawConfig(buf []byte) (*RawConfig, error) {
func ParseRawConfig(rawCfg *RawConfig) (*Config, error) { func ParseRawConfig(rawCfg *RawConfig) (*Config, error) {
config := &Config{} config := &Config{}
log.Infoln("Start initial configuration in progress") //Segment finished in xxm
startTime := time.Now()
config.Experimental = &rawCfg.Experimental config.Experimental = &rawCfg.Experimental
config.Profile = &rawCfg.Profile config.Profile = &rawCfg.Profile
@ -305,6 +309,8 @@ func ParseRawConfig(rawCfg *RawConfig) (*Config, error) {
config.Users = parseAuthentication(rawCfg.Authentication) 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 return config, nil
} }
@ -341,6 +347,7 @@ func parseGeneral(cfg *RawConfig) (*General, error) {
IPv6: cfg.IPv6, IPv6: cfg.IPv6,
Interface: cfg.Interface, Interface: cfg.Interface,
GeodataLoader: cfg.GeodataLoader, GeodataLoader: cfg.GeodataLoader,
AutoIptables: cfg.AutoIptables,
}, nil }, nil
} }
@ -487,8 +494,7 @@ time = ClashTime()
func parseRules(cfg *RawConfig, proxies map[string]C.Proxy) ([]C.Rule, map[string]*providerTypes.RuleProvider, error) { func parseRules(cfg *RawConfig, proxies map[string]C.Proxy) ([]C.Rule, map[string]*providerTypes.RuleProvider, error) {
ruleProviders := map[string]*providerTypes.RuleProvider{} ruleProviders := map[string]*providerTypes.RuleProvider{}
log.Infoln("Geodata Loader mode: %s", geodata.LoaderName())
startTime := time.Now()
// parse rule provider // parse rule provider
for name, mapping := range cfg.RuleProvider { for name, mapping := range cfg.RuleProvider {
rp, err := RP.ParseRuleProvider(name, mapping) 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) 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() runtime.GC()
return rules, ruleProviders, nil return rules, ruleProviders, nil

View File

@ -1,13 +1,20 @@
package constant package constant
import ( import (
"io/ioutil"
"os" "os"
P "path" P "path"
"path/filepath" "path/filepath"
"strings"
) )
const Name = "clash" const Name = "clash"
var (
GeositeName = "GeoSite.dat"
GeoipName = "GeoIP.dat"
)
// Path is used to get the configuration path // Path is used to get the configuration path
var Path = func() *path { var Path = func() *path {
homeDir, err := os.UserHomeDir() homeDir, err := os.UserHomeDir()
@ -65,10 +72,40 @@ func (p *path) Cache() string {
} }
func (p *path) GeoIP() 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") return P.Join(p.homeDir, "GeoIP.dat")
} }
func (p *path) GeoSite() string { 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") return P.Join(p.homeDir, "GeoSite.dat")
} }

View File

@ -1,9 +1,8 @@
package constant package constant
var ( var (
Meta = true Meta = true
Version = "1.9.0" Version = "1.9.1"
BuildTime = "unknown time" BuildTime = "unknown time"
AutoIptables string ClashName = "Clash.Meta"
ClashName = "Clash.Meta"
) )

View File

@ -314,8 +314,7 @@ func patchSelectGroup(proxies map[string]C.Proxy) {
} }
func updateIPTables(dns *config.DNS, general *config.General, tun *config.Tun) { 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 || !general.AutoIptables {
if runtime.GOOS != "linux" || dns.Listen == "" || general.TProxyPort == 0 || tun.Enable || AutoIptables != "Enable" {
return return
} }
@ -342,8 +341,7 @@ func updateIPTables(dns *config.DNS, general *config.General, tun *config.Tun) {
func CleanUp() { func CleanUp() {
P.CleanUp() P.CleanUp()
AutoIptables := C.AutoIptables if runtime.GOOS == "linux" {
if runtime.GOOS == "linux" && AutoIptables == "Enable" {
tproxy.CleanUpTProxyLinuxIPTables() tproxy.CleanUpTProxyLinuxIPTables()
} }
} }

View File

@ -28,10 +28,6 @@ func (ps *Process) Match(metadata *C.Metadata) bool {
return strings.EqualFold(metadata.Process, ps.process) 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) key := fmt.Sprintf("%s:%s:%s", metadata.NetWork.String(), metadata.SrcIP.String(), metadata.SrcPort)
if strings.TrimSpace(metadata.Process) == "" { if strings.TrimSpace(metadata.Process) == "" {
cached, hit := processCache.Get(key) cached, hit := processCache.Get(key)