From 288c0c27d65b8d30c525c56cac4e507beee7b090 Mon Sep 17 00:00:00 2001 From: xishang0128 Date: Sat, 11 Nov 2023 22:15:57 +0800 Subject: [PATCH] feat: add `include-all-providers` to proxy-groups --- adapter/outboundgroup/parser.go | 42 +++++++++++++++++++++------------ adapter/provider/provider.go | 3 +++ 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/adapter/outboundgroup/parser.go b/adapter/outboundgroup/parser.go index 8f3335d8..806732cf 100644 --- a/adapter/outboundgroup/parser.go +++ b/adapter/outboundgroup/parser.go @@ -22,18 +22,19 @@ var ( type GroupCommonOption struct { outbound.BasicOption - Name string `group:"name"` - Type string `group:"type"` - Proxies []string `group:"proxies,omitempty"` - Use []string `group:"use,omitempty"` - URL string `group:"url,omitempty"` - Interval int `group:"interval,omitempty"` - Lazy bool `group:"lazy,omitempty"` - DisableUDP bool `group:"disable-udp,omitempty"` - Filter string `group:"filter,omitempty"` - ExcludeFilter string `group:"exclude-filter,omitempty"` - ExcludeType string `group:"exclude-type,omitempty"` - ExpectedStatus string `group:"expected-status,omitempty"` + Name string `group:"name"` + Type string `group:"type"` + Proxies []string `group:"proxies,omitempty"` + Use []string `group:"use,omitempty"` + URL string `group:"url,omitempty"` + Interval int `group:"interval,omitempty"` + Lazy bool `group:"lazy,omitempty"` + DisableUDP bool `group:"disable-udp,omitempty"` + Filter string `group:"filter,omitempty"` + ExcludeFilter string `group:"exclude-filter,omitempty"` + ExcludeType string `group:"exclude-type,omitempty"` + ExpectedStatus string `group:"expected-status,omitempty"` + IncludeAllProviders bool `group:"include-all-providers,omitempty"` } func ParseProxyGroup(config map[string]any, proxyMap map[string]C.Proxy, providersMap map[string]types.ProxyProvider) (C.ProxyAdapter, error) { @@ -54,7 +55,18 @@ func ParseProxyGroup(config map[string]any, proxyMap map[string]C.Proxy, provide providers := []types.ProxyProvider{} - if len(groupOption.Proxies) == 0 && len(groupOption.Use) == 0 { + var GroupUse []string + visited := make(map[string]bool) + if groupOption.IncludeAllProviders { + for name := range provider.ProxyProviderName { + GroupUse = append(GroupUse, name) + visited[name] = true + } + } else { + GroupUse = groupOption.Use + } + + if len(groupOption.Proxies) == 0 && len(GroupUse) == 0 { return nil, fmt.Errorf("%s: %w", groupName, errMissProxy) } @@ -107,8 +119,8 @@ func ParseProxyGroup(config map[string]any, proxyMap map[string]C.Proxy, provide providersMap[groupName] = pd } - if len(groupOption.Use) != 0 { - list, err := getProviders(providersMap, groupOption.Use) + if len(GroupUse) != 0 { + list, err := getProviders(providersMap, GroupUse) if err != nil { return nil, fmt.Errorf("%s: %w", groupName, err) } diff --git a/adapter/provider/provider.go b/adapter/provider/provider.go index ffaec91b..aa434a11 100644 --- a/adapter/provider/provider.go +++ b/adapter/provider/provider.go @@ -24,6 +24,8 @@ import ( "gopkg.in/yaml.v3" ) +var ProxyProviderName = make(map[string]struct{}) + const ( ReservedName = "default" ) @@ -193,6 +195,7 @@ func NewProxySetProvider(name string, interval time.Duration, filter string, exc fetcher := resource.NewFetcher[[]C.Proxy](name, interval, vehicle, proxiesParseAndFilter(filter, excludeFilter, excludeTypeArray, filterRegs, excludeFilterReg, dialerProxy), proxiesOnUpdate(pd)) pd.Fetcher = fetcher + ProxyProviderName[name] = struct{}{} wrapper := &ProxySetProvider{pd} runtime.SetFinalizer(wrapper, stopProxyProvider) return wrapper, nil