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 (
"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

View file

@ -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

View file

@ -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")
}

View file

@ -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"
)

View file

@ -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()
}
}

View file

@ -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)