From 3867329ef3d25b25fbb63e92da01db7332d27a1f Mon Sep 17 00:00:00 2001 From: Hellojack Date: Sun, 7 Aug 2022 20:43:11 +0800 Subject: [PATCH 1/3] fix: Converter error when VMess `aid` field not exists --- common/convert/converter.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/common/convert/converter.go b/common/convert/converter.go index 4c1aceb59..811acecba 100644 --- a/common/convert/converter.go +++ b/common/convert/converter.go @@ -10,8 +10,10 @@ import ( "strings" ) -var encRaw = base64.RawStdEncoding -var enc = base64.StdEncoding +var ( + encRaw = base64.RawStdEncoding + enc = base64.StdEncoding +) func DecodeBase64(buf []byte) []byte { dBuf := make([]byte, encRaw.DecodedLen(len(buf))) @@ -149,7 +151,7 @@ func ConvertsV2Ray(buf []byte) ([]map[string]any, error) { vless["skip-cert-verify"] = false vless["tls"] = false tls := strings.ToLower(query.Get("security")) - if strings.Contains(tls, "tls") { + if strings.HasSuffix(tls, "tls") { vless["tls"] = true } sni := query.Get("sni") @@ -244,7 +246,11 @@ func ConvertsV2Ray(buf []byte) ([]map[string]any, error) { vmess["server"] = values["add"] vmess["port"] = values["port"] vmess["uuid"] = values["id"] - vmess["alterId"] = values["aid"] + if alterId, ok := values["aid"]; ok { + vmess["alterId"] = alterId + } else { + vmess["alterId"] = 0 + } vmess["cipher"] = "auto" vmess["udp"] = true vmess["tls"] = false From 97270dcbe0e28a94d0bec57e2ac77aa2ed0d01d0 Mon Sep 17 00:00:00 2001 From: zhudan Date: Mon, 8 Aug 2022 10:21:16 +0800 Subject: [PATCH 2/3] rm EBpf tun && disable android ebpf --- component/ebpf/ebpf_linux.go | 2 ++ component/ebpf/ebpf_others.go | 2 +- hub/executor/executor.go | 1 - listener/listener.go | 44 +++++------------------------------ 4 files changed, 9 insertions(+), 40 deletions(-) diff --git a/component/ebpf/ebpf_linux.go b/component/ebpf/ebpf_linux.go index 6893ae48e..0fa2e275e 100644 --- a/component/ebpf/ebpf_linux.go +++ b/component/ebpf/ebpf_linux.go @@ -1,3 +1,5 @@ +//go:build !android && linux + package ebpf import ( diff --git a/component/ebpf/ebpf_others.go b/component/ebpf/ebpf_others.go index 53658ffef..0ecf5caaf 100644 --- a/component/ebpf/ebpf_others.go +++ b/component/ebpf/ebpf_others.go @@ -1,4 +1,4 @@ -//go:build !linux +//go:build !linux || android package ebpf diff --git a/hub/executor/executor.go b/hub/executor/executor.go index a155bda72..d35a91498 100644 --- a/hub/executor/executor.go +++ b/hub/executor/executor.go @@ -335,7 +335,6 @@ func updateGeneral(general *config.General, force bool) { P.ReCreateTProxy(general.TProxyPort, tcpIn, udpIn) P.ReCreateMixed(general.MixedPort, tcpIn, udpIn) P.ReCreateAutoRedir(general.EBpf.AutoRedir, tcpIn, udpIn) - P.ReCreateRedirToTun(general.EBpf.RedirectToTun) } func updateUsers(users []auth.AuthUser) { diff --git a/listener/listener.go b/listener/listener.go index 79b60e5e4..8184b9cfc 100644 --- a/listener/listener.go +++ b/listener/listener.go @@ -41,7 +41,6 @@ var ( mixedListener *mixed.Listener mixedUDPLister *socks.UDPListener tunStackListener ipstack.Stack - tcProgram *ebpf.TcEBpfProgram autoRedirListener *autoredir.Listener autoRedirProgram *ebpf.TcEBpfProgram @@ -371,9 +370,9 @@ func ReCreateAutoRedir(ifaceNames []string, tcpIn chan<- C.ConnContext, _ chan<- var err error defer func() { if err != nil { - if redirListener != nil { - _ = redirListener.Close() - redirListener = nil + if autoRedirListener != nil { + _ = autoRedirListener.Close() + autoRedirListener = nil } if autoRedirProgram != nil { autoRedirProgram.Close() @@ -387,10 +386,10 @@ func ReCreateAutoRedir(ifaceNames []string, tcpIn chan<- C.ConnContext, _ chan<- slices.Sort(nicArr) nicArr = slices.Compact(nicArr) - if redirListener != nil && autoRedirProgram != nil { - _ = redirListener.Close() + if autoRedirListener != nil && autoRedirProgram != nil { + _ = autoRedirListener.Close() autoRedirProgram.Close() - redirListener = nil + autoRedirListener = nil autoRedirProgram = nil } @@ -420,37 +419,6 @@ func ReCreateAutoRedir(ifaceNames []string, tcpIn chan<- C.ConnContext, _ chan<- log.Infoln("Auto redirect proxy listening at: %s, attached tc ebpf program to interfaces %v", autoRedirListener.Address(), autoRedirProgram.RawNICs()) } -func ReCreateRedirToTun(ifaceNames []string) { - tcMux.Lock() - defer tcMux.Unlock() - - nicArr := ifaceNames - slices.Sort(nicArr) - nicArr = slices.Compact(nicArr) - - if tcProgram != nil { - tcProgram.Close() - tcProgram = nil - } - - if len(nicArr) == 0 { - return - } - - if lastTunConf == nil || !lastTunConf.Enable { - return - } - - program, err := ebpf.NewTcEBpfProgram(nicArr, lastTunConf.Device) - if err != nil { - log.Errorln("Attached tc ebpf program error: %v", err) - return - } - tcProgram = program - - log.Infoln("Attached tc ebpf program to interfaces %v", tcProgram.RawNICs()) -} - // GetPorts return the ports of proxy servers func GetPorts() *Ports { ports := &Ports{} From 473d0f74bd56801f414280ddd5dd9d9bb6dde6ce Mon Sep 17 00:00:00 2001 From: Skyxim Date: Thu, 11 Aug 2022 21:50:16 +0800 Subject: [PATCH 3/3] fix: remove extra and the actual original IDNA domain name is no longer stored, for reduce memory --- constant/rule.go | 2 -- constant/rule_extra.go | 39 ----------------------- rules/common/base.go | 57 ---------------------------------- rules/common/domain.go | 20 +++++++----- rules/common/domain_keyword.go | 20 +++++++----- rules/common/domain_suffix.go | 22 +++++++------ rules/parser.go | 8 ----- 7 files changed, 37 insertions(+), 131 deletions(-) diff --git a/constant/rule.go b/constant/rule.go index 2cc60cdd0..a403ac63c 100644 --- a/constant/rule.go +++ b/constant/rule.go @@ -83,6 +83,4 @@ type Rule interface { Payload() string ShouldResolveIP() bool ShouldFindProcess() bool - RuleExtra() *RuleExtra - SetRuleExtra(re *RuleExtra) } diff --git a/constant/rule_extra.go b/constant/rule_extra.go index f13ec0bb8..3c5de5d53 100644 --- a/constant/rule_extra.go +++ b/constant/rule_extra.go @@ -1,48 +1,9 @@ package constant import ( - "net/netip" - "strings" - "github.com/Dreamacro/clash/component/geodata/router" ) -type RuleExtra struct { - Network NetWork - SourceIPs []*netip.Prefix - ProcessNames []string -} - -func (re *RuleExtra) NotMatchNetwork(network NetWork) bool { - return re.Network != ALLNet && re.Network != network -} - -func (re *RuleExtra) NotMatchSourceIP(srcIP netip.Addr) bool { - if re.SourceIPs == nil { - return false - } - - for _, ips := range re.SourceIPs { - if ips.Contains(srcIP) { - return false - } - } - return true -} - -func (re *RuleExtra) NotMatchProcessName(processName string) bool { - if re.ProcessNames == nil { - return false - } - - for _, pn := range re.ProcessNames { - if strings.EqualFold(pn, processName) { - return false - } - } - return true -} - type RuleGeoSite interface { GetDomainMatcher() *router.DomainMatcher } diff --git a/rules/common/base.go b/rules/common/base.go index 381eab820..42b4d7700 100644 --- a/rules/common/base.go +++ b/rules/common/base.go @@ -2,10 +2,6 @@ package common import ( "errors" - "net/netip" - "strings" - - C "github.com/Dreamacro/clash/constant" ) var ( @@ -15,15 +11,6 @@ var ( ) type Base struct { - ruleExtra *C.RuleExtra -} - -func (b *Base) RuleExtra() *C.RuleExtra { - return b.ruleExtra -} - -func (b *Base) SetRuleExtra(re *C.RuleExtra) { - b.ruleExtra = re } func (b *Base) ShouldFindProcess() bool { @@ -42,47 +29,3 @@ func HasNoResolve(params []string) bool { } return false } - -func FindNetwork(params []string) C.NetWork { - for _, p := range params { - if strings.EqualFold(p, "tcp") { - return C.TCP - } else if strings.EqualFold(p, "udp") { - return C.UDP - } - } - return C.ALLNet -} - -func FindSourceIPs(params []string) []*netip.Prefix { - var ips []*netip.Prefix - for _, p := range params { - if p == noResolve || len(p) < 7 { - continue - } - ipnet, err := netip.ParsePrefix(p) - if err != nil { - continue - } - ips = append(ips, &ipnet) - } - - if len(ips) > 0 { - return ips - } - return nil -} - -func FindProcessName(params []string) []string { - var processNames []string - for _, p := range params { - if strings.HasPrefix(p, "P:") { - processNames = append(processNames, strings.TrimPrefix(p, "P:")) - } - } - - if len(processNames) > 0 { - return processNames - } - return nil -} diff --git a/rules/common/domain.go b/rules/common/domain.go index 5638afe16..4a8a7d278 100644 --- a/rules/common/domain.go +++ b/rules/common/domain.go @@ -9,9 +9,9 @@ import ( type Domain struct { *Base - domain string - rawDomain string - adapter string + domain string + adapter string + isIDNA bool } func (d *Domain) RuleType() C.RuleType { @@ -30,16 +30,20 @@ func (d *Domain) Adapter() string { } func (d *Domain) Payload() string { - return d.rawDomain + domain := d.domain + if d.isIDNA { + domain, _ = idna.ToUnicode(domain) + } + return domain } func NewDomain(domain string, adapter string) *Domain { actualDomain, _ := idna.ToASCII(domain) return &Domain{ - Base: &Base{}, - domain: strings.ToLower(actualDomain), - adapter: adapter, - rawDomain: domain, + Base: &Base{}, + domain: strings.ToLower(actualDomain), + adapter: adapter, + isIDNA: actualDomain != domain, } } diff --git a/rules/common/domain_keyword.go b/rules/common/domain_keyword.go index c13dcdbf0..667b28610 100644 --- a/rules/common/domain_keyword.go +++ b/rules/common/domain_keyword.go @@ -9,9 +9,9 @@ import ( type DomainKeyword struct { *Base - keyword string - adapter string - rawKeyword string + keyword string + adapter string + isIDNA bool } func (dk *DomainKeyword) RuleType() C.RuleType { @@ -31,16 +31,20 @@ func (dk *DomainKeyword) Adapter() string { } func (dk *DomainKeyword) Payload() string { - return dk.rawKeyword + keyword := dk.keyword + if dk.isIDNA { + keyword, _ = idna.ToUnicode(keyword) + } + return keyword } func NewDomainKeyword(keyword string, adapter string) *DomainKeyword { actualDomainKeyword, _ := idna.ToASCII(keyword) return &DomainKeyword{ - Base: &Base{}, - keyword: strings.ToLower(actualDomainKeyword), - adapter: adapter, - rawKeyword: keyword, + Base: &Base{}, + keyword: strings.ToLower(actualDomainKeyword), + adapter: adapter, + isIDNA: keyword != actualDomainKeyword, } } diff --git a/rules/common/domain_suffix.go b/rules/common/domain_suffix.go index 278052f2e..c2edcd160 100644 --- a/rules/common/domain_suffix.go +++ b/rules/common/domain_suffix.go @@ -9,9 +9,9 @@ import ( type DomainSuffix struct { *Base - suffix string - adapter string - rawSuffix string + suffix string + adapter string + isIDNA bool } func (ds *DomainSuffix) RuleType() C.RuleType { @@ -31,16 +31,20 @@ func (ds *DomainSuffix) Adapter() string { } func (ds *DomainSuffix) Payload() string { - return ds.rawSuffix + suffix := ds.suffix + if ds.isIDNA { + suffix, _ = idna.ToUnicode(suffix) + } + return suffix } func NewDomainSuffix(suffix string, adapter string) *DomainSuffix { - actualDomainKeyword, _ := idna.ToASCII(suffix) + actualDomainSuffix, _ := idna.ToASCII(suffix) return &DomainSuffix{ - Base: &Base{}, - suffix: strings.ToLower(actualDomainKeyword), - adapter: adapter, - rawSuffix: suffix, + Base: &Base{}, + suffix: strings.ToLower(actualDomainSuffix), + adapter: adapter, + isIDNA: suffix != actualDomainSuffix, } } diff --git a/rules/parser.go b/rules/parser.go index 6ddd63a83..c6ca88477 100644 --- a/rules/parser.go +++ b/rules/parser.go @@ -65,13 +65,5 @@ func ParseRule(tp, payload, target string, params []string) (parsed C.Rule, pars return nil, parseErr } - ruleExtra := &C.RuleExtra{ - Network: RC.FindNetwork(params), - SourceIPs: RC.FindSourceIPs(params), - ProcessNames: RC.FindProcessName(params), - } - - parsed.SetRuleExtra(ruleExtra) - return }