From 1034780e8eb32d6f550f956c7f89a8950596f97f Mon Sep 17 00:00:00 2001 From: MetaCubeX Date: Wed, 16 Mar 2022 00:43:08 +0800 Subject: [PATCH] [build test] --- Makefile | 10 ++-------- config/config.go | 12 +++++++----- config/initial.go | 7 +++---- dns/filters.go | 9 ++++++++- main.go | 8 ++++---- rule/common/domain_keyword.go | 2 +- rule/common/geoip.go | 11 ++++++++++- rule/common/geosite.go | 4 ++++ rule/common/network_type.go | 4 ++++ rule/common/port.go | 4 ++++ rule/common/process.go | 8 +++++++- rule/logic/and.go | 4 ++++ rule/logic/not.go | 4 ++++ rule/logic/or.go | 4 ++++ rule/provider/rule_set.go | 4 ++++ 15 files changed, 70 insertions(+), 25 deletions(-) diff --git a/Makefile b/Makefile index ebb7a9ae..d15dccb8 100644 --- a/Makefile +++ b/Makefile @@ -6,16 +6,10 @@ ifeq ($(BRANCH),Alpha) VERSION=alpha-$(shell git rev-parse --short HEAD) endif BUILDTIME=$(shell date -u) -AUTOIPTABLES=Enable GOBUILD=CGO_ENABLED=0 go build -trimpath -ldflags '-X "github.com/Dreamacro/clash/constant.Version=$(VERSION)" \ -X "github.com/Dreamacro/clash/constant.BuildTime=$(BUILDTIME)" \ -w -s -buildid=' -GOBUILDOP=CGO_ENABLED=0 go build -trimpath -ldflags '-X "github.com/Dreamacro/clash/constant.Version=$(VERSION)" \ - -X "github.com/Dreamacro/clash/constant.BuildTime=$(BUILDTIME)" \ - -X "github.com/Dreamacro/clash/constant.AutoIptables=$(AUTOIPTABLES)" \ - -w -s -buildid=' - PLATFORM_LIST = \ darwin-amd64 \ darwin-arm64 \ @@ -43,8 +37,8 @@ WINDOWS_ARCH_LIST = \ windows-arm32v7 -all:linux-amd64-AutoIptables linux-amd64\ - linux-arm64 linux-arm64-AutoIptables linux-armv7\ +all:linux-amd64\ + linux-armv7\ darwin-amd64 darwin-arm64\ windows-amd64 windows-386 \ linux-mips-hardfloat linux-mips-softfloat linux-mips64 linux-mips64le linux-mipsle-hardfloat linux-mipsle-softfloat# Most used diff --git a/config/config.go b/config/config.go index 947b8400..37698bdf 100644 --- a/config/config.go +++ b/config/config.go @@ -41,7 +41,8 @@ type General struct { LogLevel log.LogLevel `json:"log-level"` IPv6 bool `json:"ipv6"` Interface string `json:"-"` - GeodataMode string `json:"geodata-mode"` + RoutingMark int `json:"-"` + GeodataMode bool `json:"geodata-mode"` GeodataLoader string `json:"geodata-loader"` AutoIptables bool `json:"auto-iptables"` } @@ -173,10 +174,10 @@ type RawConfig struct { ExternalUI string `yaml:"external-ui"` Secret string `yaml:"secret"` Interface string `yaml:"interface-name"` - GeodataMode string `yaml:"geodata-mode"` + RoutingMark int `yaml:"routing-mark"` + GeodataMode bool `yaml:"geodata-mode"` GeodataLoader string `yaml:"geodata-loader"` AutoIptables bool `yaml:"auto-iptables"` - RoutingMark int `yaml:"routing-mark"` ProxyProvider map[string]map[string]interface{} `yaml:"proxy-providers"` RuleProvider map[string]map[string]interface{} `yaml:"rule-providers"` @@ -207,7 +208,7 @@ func UnmarshalRawConfig(buf []byte) (*RawConfig, error) { AllowLan: false, BindAddress: "*", Mode: T.Rule, - GeodataMode: "dat", + GeodataMode: GeodataMode, GeodataLoader: "memconservative", AutoIptables: false, UnifiedDelay: false, @@ -350,9 +351,10 @@ func parseGeneral(cfg *RawConfig) (*General, error) { LogLevel: cfg.LogLevel, IPv6: cfg.IPv6, Interface: cfg.Interface, + RoutingMark: cfg.RoutingMark, + GeodataMode: cfg.GeodataMode, GeodataLoader: cfg.GeodataLoader, AutoIptables: cfg.AutoIptables, - RoutingMark: cfg.RoutingMark, }, nil } diff --git a/config/initial.go b/config/initial.go index d3c2ce35..9b242afc 100644 --- a/config/initial.go +++ b/config/initial.go @@ -15,7 +15,7 @@ import ( var GeodataMode bool func downloadMMDB(path string) (err error) { - resp, err := http.Get("https://cdn.jsdelivr.net/gh/Loyalsoldier/geoip@release/Country.mmdb") + resp, err := http.Get("https://raw.githubusercontents.com/Loyalsoldier/geoip/release/Country.mmdb") if err != nil { return } @@ -32,7 +32,7 @@ func downloadMMDB(path string) (err error) { } func downloadGeoIP(path string) (err error) { - resp, err := http.Get("https://cdn.jsdelivr.net/gh/Loyalsoldier/v2ray-rules-dat@release/geoip.dat") + resp, err := http.Get("https://raw.githubusercontents.com/Loyalsoldier/v2ray-rules-dat/release/geoip.dat") if err != nil { return } @@ -49,7 +49,7 @@ func downloadGeoIP(path string) (err error) { } func downloadGeoSite(path string) (err error) { - resp, err := http.Get("https://cdn.jsdelivr.net/gh/Loyalsoldier/v2ray-rules-dat@release/geosite.dat") + resp, err := http.Get("https://raw.githubusercontents.com/Loyalsoldier/v2ray-rules-dat/release/geosite.dat") if err != nil { return } @@ -148,7 +148,6 @@ func Init(dir string) error { f.Write([]byte(`mixed-port: 7890`)) f.Close() } - // initial GeoIP if err := initGeoIP(); err != nil { return fmt.Errorf("can't initial GeoIP: %w", err) diff --git a/dns/filters.go b/dns/filters.go index 58b37bc4..9933a946 100644 --- a/dns/filters.go +++ b/dns/filters.go @@ -3,9 +3,12 @@ package dns import ( "github.com/Dreamacro/clash/component/geodata" "github.com/Dreamacro/clash/component/geodata/router" + "github.com/Dreamacro/clash/component/mmdb" "github.com/Dreamacro/clash/component/trie" + "github.com/Dreamacro/clash/config" "github.com/Dreamacro/clash/log" "net" + "strings" ) type fallbackIPFilter interface { @@ -19,6 +22,11 @@ type geoipFilter struct { var geoIPMatcher *router.GeoIPMatcher func (gf *geoipFilter) Match(ip net.IP) bool { + if !config.GeodataMode { + record, _ := mmdb.Instance().Country(ip) + return !strings.EqualFold(record.Country.IsoCode, gf.code) && !ip.IsPrivate() + } + if geoIPMatcher == nil { countryCode := "cn" geoLoader, err := geodata.GetGeoDataLoader(geodata.LoaderName()) @@ -46,7 +54,6 @@ func (gf *geoipFilter) Match(ip net.IP) bool { return false } } - return !geoIPMatcher.Match(ip) } diff --git a/main.go b/main.go index cd98a89b..4c465d2a 100644 --- a/main.go +++ b/main.go @@ -73,12 +73,12 @@ func main() { C.SetConfig(configFile) } - if err := config.Init(C.Path.HomeDir()); err != nil { - log.Fatalln("Initial configuration directory error: %s", err.Error()) + if geodataMode || executor.GetGeneral().GeodataMode { + config.GeodataMode = true } - if geodataMode { - config.GeodataMode = true + if err := config.Init(C.Path.HomeDir()); err != nil { + log.Fatalln("Initial configuration directory error: %s", err.Error()) } if testConfig { diff --git a/rule/common/domain_keyword.go b/rule/common/domain_keyword.go index 80f4313f..694cb5f4 100644 --- a/rule/common/domain_keyword.go +++ b/rule/common/domain_keyword.go @@ -44,7 +44,7 @@ func (dk *DomainKeyword) RuleExtra() *C.RuleExtra { return dk.ruleExtra } -func NewDomainKeyword(keyword string, adapter string) *DomainKeyword { +func NewDomainKeyword(keyword string, adapter string, ruleExtra *C.RuleExtra) *DomainKeyword { return &DomainKeyword{ keyword: strings.ToLower(keyword), adapter: adapter, diff --git a/rule/common/geoip.go b/rule/common/geoip.go index f0b7541d..29355ed9 100644 --- a/rule/common/geoip.go +++ b/rule/common/geoip.go @@ -4,6 +4,8 @@ import ( "fmt" "github.com/Dreamacro/clash/component/geodata" "github.com/Dreamacro/clash/component/geodata/router" + "github.com/Dreamacro/clash/component/mmdb" + "github.com/Dreamacro/clash/config" "strings" C "github.com/Dreamacro/clash/constant" @@ -18,6 +20,10 @@ type GEOIP struct { geoIPMatcher *router.GeoIPMatcher } +func (g *GEOIP) ShouldFindProcess() bool { + return false +} + func (g *GEOIP) RuleType() C.RuleType { return C.GEOIP } @@ -31,7 +37,10 @@ func (g *GEOIP) Match(metadata *C.Metadata) bool { if strings.EqualFold(g.country, "LAN") || C.TunBroadcastAddr.Equal(ip) { return ip.IsPrivate() } - + if !config.GeodataMode { + record, _ := mmdb.Instance().Country(ip) + return strings.EqualFold(record.Country.IsoCode, g.country) + } return g.geoIPMatcher.Match(ip) } diff --git a/rule/common/geosite.go b/rule/common/geosite.go index 95b6a2ff..bf81e9c7 100644 --- a/rule/common/geosite.go +++ b/rule/common/geosite.go @@ -44,6 +44,10 @@ func (gs *GEOSITE) ShouldResolveIP() bool { return false } +func (gs *GEOSITE) ShouldFindProcess() bool { + return false +} + func (gs *GEOSITE) RuleExtra() *C.RuleExtra { return gs.ruleExtra } diff --git a/rule/common/network_type.go b/rule/common/network_type.go index 965f9575..10729110 100644 --- a/rule/common/network_type.go +++ b/rule/common/network_type.go @@ -11,6 +11,10 @@ type NetworkType struct { adapter string } +func (n *NetworkType) ShouldFindProcess() bool { + return false +} + func NewNetworkType(network, adapter string) (*NetworkType, error) { ntType := new(NetworkType) ntType.adapter = adapter diff --git a/rule/common/port.go b/rule/common/port.go index 6990af50..a796d2b2 100644 --- a/rule/common/port.go +++ b/rule/common/port.go @@ -21,6 +21,10 @@ type Port struct { ruleExtra *C.RuleExtra } +func (p *Port) ShouldFindProcess() bool { + return false +} + func (p *Port) RuleType() C.RuleType { if p.isSource { return C.SrcPort diff --git a/rule/common/process.go b/rule/common/process.go index b7712d43..eeced31a 100644 --- a/rule/common/process.go +++ b/rule/common/process.go @@ -16,9 +16,14 @@ var processCache = cache.NewLRUCache(cache.WithAge(2), cache.WithSize(64)) type Process struct { adapter string process string + nameOnly bool ruleExtra *C.RuleExtra } +func (ps *Process) ShouldFindProcess() bool { + return false +} + func (ps *Process) RuleType() C.RuleType { return C.Process } @@ -70,10 +75,11 @@ func (ps *Process) RuleExtra() *C.RuleExtra { return ps.ruleExtra } -func NewProcess(process string, adapter string, ruleExtra *C.RuleExtra) (*Process, error) { +func NewProcess(process string, adapter string, nameOnly bool, ruleExtra *C.RuleExtra) (*Process, error) { return &Process{ adapter: adapter, process: process, + nameOnly: nameOnly, ruleExtra: ruleExtra, }, nil } diff --git a/rule/logic/and.go b/rule/logic/and.go index 01862e51..7c1d0b48 100644 --- a/rule/logic/and.go +++ b/rule/logic/and.go @@ -9,6 +9,10 @@ type AND struct { needIP bool } +func (A *AND) ShouldFindProcess() bool { + return false +} + func NewAND(payload string, adapter string) (*AND, error) { and := &AND{payload: payload, adapter: adapter} rules, err := parseRuleByPayload(payload) diff --git a/rule/logic/not.go b/rule/logic/not.go index 79102d4e..c42e6fab 100644 --- a/rule/logic/not.go +++ b/rule/logic/not.go @@ -11,6 +11,10 @@ type NOT struct { adapter string } +func (not *NOT) ShouldFindProcess() bool { + return false +} + func NewNOT(payload string, adapter string) (*NOT, error) { not := &NOT{payload: payload, adapter: adapter} rule, err := parseRuleByPayload(payload) diff --git a/rule/logic/or.go b/rule/logic/or.go index 3822e315..e3b0f48c 100644 --- a/rule/logic/or.go +++ b/rule/logic/or.go @@ -9,6 +9,10 @@ type OR struct { needIP bool } +func (or *OR) ShouldFindProcess() bool { + return false +} + func (or *OR) RuleType() C.RuleType { return C.OR } diff --git a/rule/provider/rule_set.go b/rule/provider/rule_set.go index cd06c821..92c5151a 100644 --- a/rule/provider/rule_set.go +++ b/rule/provider/rule_set.go @@ -13,6 +13,10 @@ type RuleSet struct { ruleExtra *C.RuleExtra } +func (rs *RuleSet) ShouldFindProcess() bool { + return false +} + func (rs *RuleSet) RuleType() C.RuleType { return C.RuleSet }