chore: support multi filter in GroupBase too

This commit is contained in:
wwqgtxx 2022-10-30 22:30:54 +08:00
parent 0e5bf0c27e
commit dedb9122df

View file

@ -18,7 +18,7 @@ import (
type GroupBase struct { type GroupBase struct {
*outbound.Base *outbound.Base
filter *regexp2.Regexp filterRegs []*regexp2.Regexp
providers []provider.ProxyProvider providers []provider.ProxyProvider
failedTestMux sync.Mutex failedTestMux sync.Mutex
failedTimes int failedTimes int
@ -35,14 +35,17 @@ type GroupBaseOption struct {
} }
func NewGroupBase(opt GroupBaseOption) *GroupBase { func NewGroupBase(opt GroupBaseOption) *GroupBase {
var filter *regexp2.Regexp = nil var filterRegs []*regexp2.Regexp
if opt.filter != "" { if opt.filter != "" {
filter = regexp2.MustCompile(opt.filter, 0) for _, filter := range strings.Split(opt.filter, "`") {
filterReg := regexp2.MustCompile(filter, 0)
filterRegs = append(filterRegs, filterReg)
}
} }
gb := &GroupBase{ gb := &GroupBase{
Base: outbound.NewBase(opt.BaseOption), Base: outbound.NewBase(opt.BaseOption),
filter: filter, filterRegs: filterRegs,
providers: opt.providers, providers: opt.providers,
failedTesting: atomic.NewBool(false), failedTesting: atomic.NewBool(false),
} }
@ -54,7 +57,7 @@ func NewGroupBase(opt GroupBaseOption) *GroupBase {
} }
func (gb *GroupBase) GetProxies(touch bool) []C.Proxy { func (gb *GroupBase) GetProxies(touch bool) []C.Proxy {
if gb.filter == nil { if len(gb.filterRegs) == 0 {
var proxies []C.Proxy var proxies []C.Proxy
for _, pd := range gb.providers { for _, pd := range gb.providers {
if touch { if touch {
@ -80,16 +83,23 @@ func (gb *GroupBase) GetProxies(touch bool) []C.Proxy {
} }
version := gb.versions[i].Load() version := gb.versions[i].Load()
if version != pd.Version() && gb.versions[i].CAS(version, pd.Version()) { if version != pd.Version() && gb.versions[i].CompareAndSwap(version, pd.Version()) {
var ( var (
proxies []C.Proxy proxies []C.Proxy
newProxies []C.Proxy newProxies []C.Proxy
) )
proxies = pd.Proxies() proxies = pd.Proxies()
for _, p := range proxies { proxiesSet := map[string]struct{}{}
if mat, _ := gb.filter.FindStringMatch(p.Name()); mat != nil { for _, filterReg := range gb.filterRegs {
newProxies = append(newProxies, p) for _, p := range proxies {
name := p.Name()
if mat, _ := filterReg.FindStringMatch(name); mat != nil {
if _, ok := proxiesSet[name]; !ok {
proxiesSet[name] = struct{}{}
newProxies = append(newProxies, p)
}
}
} }
} }
@ -106,6 +116,23 @@ func (gb *GroupBase) GetProxies(touch bool) []C.Proxy {
return append(proxies, tunnel.Proxies()["COMPATIBLE"]) return append(proxies, tunnel.Proxies()["COMPATIBLE"])
} }
if len(gb.providers) > 1 && len(gb.filterRegs) > 1 {
var newProxies []C.Proxy
proxiesSet := map[string]struct{}{}
for _, filterReg := range gb.filterRegs {
for _, p := range proxies {
name := p.Name()
if mat, _ := filterReg.FindStringMatch(name); mat != nil {
if _, ok := proxiesSet[name]; !ok {
proxiesSet[name] = struct{}{}
newProxies = append(newProxies, p)
}
}
}
}
proxies = newProxies
}
return proxies return proxies
} }