From 269a8560ce49ba514e2c48b44e68ca394a398a32 Mon Sep 17 00:00:00 2001 From: gVisor bot Date: Wed, 5 Jan 2022 12:19:49 +0800 Subject: [PATCH] [test]Add name filter to proxy group --- adapter/outboundgroup/common.go | 17 ++++++++++++++++- adapter/outboundgroup/fallback.go | 4 +++- adapter/outboundgroup/loadbalance.go | 4 +++- adapter/outboundgroup/parser.go | 3 +++ adapter/outboundgroup/relay.go | 4 +++- adapter/outboundgroup/selector.go | 8 +++++--- adapter/outboundgroup/urltest.go | 4 +++- 7 files changed, 36 insertions(+), 8 deletions(-) diff --git a/adapter/outboundgroup/common.go b/adapter/outboundgroup/common.go index 758c7801..1c9333b9 100644 --- a/adapter/outboundgroup/common.go +++ b/adapter/outboundgroup/common.go @@ -1,6 +1,7 @@ package outboundgroup import ( + "regexp" "time" C "github.com/Dreamacro/clash/constant" @@ -11,7 +12,7 @@ const ( defaultGetProxiesDuration = time.Second * 5 ) -func getProvidersProxies(providers []provider.ProxyProvider, touch bool) []C.Proxy { +func getProvidersProxies(providers []provider.ProxyProvider, touch bool, filter string) []C.Proxy { proxies := []C.Proxy{} for _, provider := range providers { if touch { @@ -20,5 +21,19 @@ func getProvidersProxies(providers []provider.ProxyProvider, touch bool) []C.Pro proxies = append(proxies, provider.Proxies()...) } } + var filterReg *regexp.Regexp = nil + matchedProxies := []C.Proxy{} + if len(filter) > 0 { + filterReg = regexp.MustCompile(filter) + for _, p := range proxies { + if filterReg.MatchString(p.Name()) { + matchedProxies = append(matchedProxies, p) + } + } + //if no proxy matched, means bad filter, return all proxies + if len(matchedProxies) > 0 { + proxies = matchedProxies + } + } return proxies } diff --git a/adapter/outboundgroup/fallback.go b/adapter/outboundgroup/fallback.go index 4784cd3c..4ecc8d39 100644 --- a/adapter/outboundgroup/fallback.go +++ b/adapter/outboundgroup/fallback.go @@ -17,6 +17,7 @@ import ( type Fallback struct { *outbound.Base disableUDP bool + filter string single *singledo.Single providers []provider.ProxyProvider failedTimes *atomic.Int32 @@ -113,7 +114,7 @@ func (f *Fallback) Unwrap(metadata *C.Metadata) C.Proxy { func (f *Fallback) proxies(touch bool) []C.Proxy { elm, _, _ := f.single.Do(func() (interface{}, error) { - return getProvidersProxies(f.providers, touch), nil + return getProvidersProxies(f.providers, touch, f.filter), nil }) return elm.([]C.Proxy) @@ -141,6 +142,7 @@ func NewFallback(option *GroupCommonOption, providers []provider.ProxyProvider) single: singledo.NewSingle(defaultGetProxiesDuration), providers: providers, disableUDP: option.DisableUDP, + filter: option.Filter, failedTimes: atomic.NewInt32(-1), failedTime: atomic.NewInt64(-1), } diff --git a/adapter/outboundgroup/loadbalance.go b/adapter/outboundgroup/loadbalance.go index 26c8052a..2a6e3b30 100644 --- a/adapter/outboundgroup/loadbalance.go +++ b/adapter/outboundgroup/loadbalance.go @@ -23,6 +23,7 @@ type LoadBalance struct { *outbound.Base disableUDP bool single *singledo.Single + filter string providers []provider.ProxyProvider strategyFn strategyFn } @@ -141,7 +142,7 @@ func (lb *LoadBalance) Unwrap(metadata *C.Metadata) C.Proxy { func (lb *LoadBalance) proxies(touch bool) []C.Proxy { elm, _, _ := lb.single.Do(func() (interface{}, error) { - return getProvidersProxies(lb.providers, touch), nil + return getProvidersProxies(lb.providers, touch, lb.filter), nil }) return elm.([]C.Proxy) @@ -180,5 +181,6 @@ func NewLoadBalance(option *GroupCommonOption, providers []provider.ProxyProvide providers: providers, strategyFn: strategyFn, disableUDP: option.DisableUDP, + filter: option.Filter, }, nil } diff --git a/adapter/outboundgroup/parser.go b/adapter/outboundgroup/parser.go index 4b789a41..575d8b93 100644 --- a/adapter/outboundgroup/parser.go +++ b/adapter/outboundgroup/parser.go @@ -29,6 +29,7 @@ type GroupCommonOption struct { Interval int `group:"interval,omitempty"` Lazy bool `group:"lazy,omitempty"` DisableUDP bool `group:"disable-udp,omitempty"` + Filter string `group:"filter,omitempty"` } func ParseProxyGroup(config map[string]interface{}, proxyMap map[string]C.Proxy, providersMap map[string]types.ProxyProvider) (C.ProxyAdapter, error) { @@ -95,6 +96,8 @@ func ParseProxyGroup(config map[string]interface{}, proxyMap map[string]C.Proxy, return nil, err } providers = append(providers, list...) + } else { + groupOption.Filter = "" } var group C.ProxyAdapter diff --git a/adapter/outboundgroup/relay.go b/adapter/outboundgroup/relay.go index 393a69bb..fab24ed4 100644 --- a/adapter/outboundgroup/relay.go +++ b/adapter/outboundgroup/relay.go @@ -16,6 +16,7 @@ type Relay struct { *outbound.Base single *singledo.Single providers []provider.ProxyProvider + filter string } // DialContext implements C.ProxyAdapter @@ -80,7 +81,7 @@ func (r *Relay) MarshalJSON() ([]byte, error) { func (r *Relay) rawProxies(touch bool) []C.Proxy { elm, _, _ := r.single.Do(func() (interface{}, error) { - return getProvidersProxies(r.providers, touch), nil + return getProvidersProxies(r.providers, touch, r.filter), nil }) return elm.([]C.Proxy) @@ -110,5 +111,6 @@ func NewRelay(option *GroupCommonOption, providers []provider.ProxyProvider) *Re }), single: singledo.NewSingle(defaultGetProxiesDuration), providers: providers, + filter: option.Filter, } } diff --git a/adapter/outboundgroup/selector.go b/adapter/outboundgroup/selector.go index 47e4b87d..d79e4a92 100644 --- a/adapter/outboundgroup/selector.go +++ b/adapter/outboundgroup/selector.go @@ -17,6 +17,7 @@ type Selector struct { disableUDP bool single *singledo.Single selected string + filter string providers []provider.ProxyProvider } @@ -50,7 +51,7 @@ func (s *Selector) SupportUDP() bool { // MarshalJSON implements C.ProxyAdapter func (s *Selector) MarshalJSON() ([]byte, error) { var all []string - for _, proxy := range getProvidersProxies(s.providers, false) { + for _, proxy := range getProvidersProxies(s.providers, false, s.filter) { all = append(all, proxy.Name()) } @@ -66,7 +67,7 @@ func (s *Selector) Now() string { } func (s *Selector) Set(name string) error { - for _, proxy := range getProvidersProxies(s.providers, false) { + for _, proxy := range getProvidersProxies(s.providers, false, s.filter) { if proxy.Name() == name { s.selected = name s.single.Reset() @@ -84,7 +85,7 @@ func (s *Selector) Unwrap(metadata *C.Metadata) C.Proxy { func (s *Selector) selectedProxy(touch bool) C.Proxy { elm, _, _ := s.single.Do(func() (interface{}, error) { - proxies := getProvidersProxies(s.providers, touch) + proxies := getProvidersProxies(s.providers, touch, s.filter) for _, proxy := range proxies { if proxy.Name() == s.selected { return proxy, nil @@ -110,5 +111,6 @@ func NewSelector(option *GroupCommonOption, providers []provider.ProxyProvider) providers: providers, selected: selected, disableUDP: option.DisableUDP, + filter: option.Filter, } } diff --git a/adapter/outboundgroup/urltest.go b/adapter/outboundgroup/urltest.go index 40d9f734..ee6fe13a 100644 --- a/adapter/outboundgroup/urltest.go +++ b/adapter/outboundgroup/urltest.go @@ -27,6 +27,7 @@ type URLTest struct { tolerance uint16 disableUDP bool fastNode C.Proxy + filter string single *singledo.Single fastSingle *singledo.Single providers []provider.ProxyProvider @@ -73,7 +74,7 @@ func (u *URLTest) Unwrap(metadata *C.Metadata) C.Proxy { func (u *URLTest) proxies(touch bool) []C.Proxy { elm, _, _ := u.single.Do(func() (interface{}, error) { - return getProvidersProxies(u.providers, touch), nil + return getProvidersProxies(u.providers, touch, u.filter), nil }) return elm.([]C.Proxy) @@ -184,6 +185,7 @@ func NewURLTest(option *GroupCommonOption, providers []provider.ProxyProvider, o fastSingle: singledo.NewSingle(time.Second * 10), providers: providers, disableUDP: option.DisableUDP, + filter: option.Filter, failedTimes: atomic.NewInt32(-1), failedTime: atomic.NewInt64(-1), }