From f100a33d9849d6b39dbf0dd729d7656e00282374 Mon Sep 17 00:00:00 2001 From: ag2s20150909 Date: Tue, 3 Jan 2023 21:27:07 +0800 Subject: [PATCH] proxy-provider support exclude node by node type --- adapter/provider/parser.go | 5 ++++- adapter/provider/provider.go | 32 +++++++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/adapter/provider/parser.go b/adapter/provider/parser.go index e55ab914..3fdedce7 100644 --- a/adapter/provider/parser.go +++ b/adapter/provider/parser.go @@ -27,6 +27,7 @@ type proxyProviderSchema struct { Interval int `provider:"interval,omitempty"` Filter string `provider:"filter,omitempty"` ExcludeFilter string `provider:"exclude-filter,omitempty"` + ExcludeType string `provider:"exclude-type,omitempty"` HealthCheck healthCheckSchema `provider:"health-check,omitempty"` } @@ -63,5 +64,7 @@ func ParseProxyProvider(name string, mapping map[string]any) (types.ProxyProvide interval := time.Duration(uint(schema.Interval)) * time.Second filter := schema.Filter excludeFilter := schema.ExcludeFilter - return NewProxySetProvider(name, interval, filter, excludeFilter, vehicle, hc) + excludeType:=schema.ExcludeType + + return NewProxySetProvider(name, interval, filter, excludeFilter,excludeType,vehicle, hc) } diff --git a/adapter/provider/provider.go b/adapter/provider/provider.go index 51f536fe..e32c2bec 100644 --- a/adapter/provider/provider.go +++ b/adapter/provider/provider.go @@ -141,11 +141,16 @@ func stopProxyProvider(pd *ProxySetProvider) { _ = pd.Fetcher.Destroy() } -func NewProxySetProvider(name string, interval time.Duration, filter string, excludeFilter string, vehicle types.Vehicle, hc *HealthCheck) (*ProxySetProvider, error) { +func NewProxySetProvider(name string, interval time.Duration, filter string, excludeFilter string,excludeType string, vehicle types.Vehicle, hc *HealthCheck) (*ProxySetProvider, error) { excludeFilterReg, err := regexp2.Compile(excludeFilter, 0) if err != nil { return nil, fmt.Errorf("invalid excludeFilter regex: %w", err) } + var excludeTypeArray []string + if excludeType !=""{ + excludeTypeArray=strings.Split(excludeType,"|") + } + var filterRegs []*regexp2.Regexp for _, filter := range strings.Split(filter, "`") { filterReg, err := regexp2.Compile(filter, 0) @@ -164,7 +169,7 @@ func NewProxySetProvider(name string, interval time.Duration, filter string, exc healthCheck: hc, } - fetcher := resource.NewFetcher[[]C.Proxy](name, interval, vehicle, proxiesParseAndFilter(filter, excludeFilter, filterRegs, excludeFilterReg), proxiesOnUpdate(pd)) + fetcher := resource.NewFetcher[[]C.Proxy](name, interval, vehicle, proxiesParseAndFilter(filter, excludeFilter, excludeTypeArray,filterRegs, excludeFilterReg), proxiesOnUpdate(pd)) pd.Fetcher = fetcher pd.getSubscriptionInfo() @@ -262,7 +267,7 @@ func proxiesOnUpdate(pd *proxySetProvider) func([]C.Proxy) { } } -func proxiesParseAndFilter(filter string, excludeFilter string, filterRegs []*regexp2.Regexp, excludeFilterReg *regexp2.Regexp) resource.Parser[[]C.Proxy] { +func proxiesParseAndFilter(filter string, excludeFilter string,excludeTypeArray []string, filterRegs []*regexp2.Regexp, excludeFilterReg *regexp2.Regexp) resource.Parser[[]C.Proxy] { return func(buf []byte) ([]C.Proxy, error) { schema := &ProxySchema{} @@ -282,6 +287,27 @@ func proxiesParseAndFilter(filter string, excludeFilter string, filterRegs []*re proxiesSet := map[string]struct{}{} for _, filterReg := range filterRegs { for idx, mapping := range schema.Proxies { + if nil !=excludeTypeArray && len(excludeTypeArray)>0{ + mType,ok:=mapping["type"] + if !ok { + continue + } + pType,ok:=mType.(string) + if !ok { + continue + } + flag:=false + for i := range excludeTypeArray { + if(strings.EqualFold(pType,excludeTypeArray[i])){ + flag=true + } + + } + if(flag){ + continue + } + + } mName, ok := mapping["name"] if !ok { continue