From 1d3e9f4889a7f5e6a62888452835c7a9cfe5b327 Mon Sep 17 00:00:00 2001 From: xishang0128 Date: Sun, 31 Dec 2023 09:43:52 +0800 Subject: [PATCH] feat: add `include-all` to proxy-groups --- adapter/outboundgroup/parser.go | 26 +++++++++++++++++--------- adapter/provider/provider.go | 3 --- config/config.go | 6 +++++- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/adapter/outboundgroup/parser.go b/adapter/outboundgroup/parser.go index c23147d7..3f7f9770 100644 --- a/adapter/outboundgroup/parser.go +++ b/adapter/outboundgroup/parser.go @@ -34,10 +34,12 @@ type GroupCommonOption struct { ExcludeFilter string `group:"exclude-filter,omitempty"` ExcludeType string `group:"exclude-type,omitempty"` ExpectedStatus string `group:"expected-status,omitempty"` + IncludeAll bool `group:"include-all,omitempty"` + IncludeAllProxies bool `group:"include-all-proxies,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) { +func ParseProxyGroup(config map[string]any, proxyMap map[string]C.Proxy, providersMap map[string]types.ProxyProvider, AllProxies []string, AllProviders []string) (C.ProxyAdapter, error) { decoder := structure.NewDecoder(structure.Option{TagName: "group", WeaklyTypedInput: true}) groupOption := &GroupCommonOption{ @@ -55,18 +57,24 @@ func ParseProxyGroup(config map[string]any, proxyMap map[string]C.Proxy, provide providers := []types.ProxyProvider{} + if groupOption.IncludeAll { + groupOption.IncludeAllProviders = true + groupOption.IncludeAllProxies = true + } var GroupUse []string - visited := make(map[string]bool) + var GroupProxies []string if groupOption.IncludeAllProviders { - for name := range provider.ProxyProviderName { - GroupUse = append(GroupUse, name) - visited[name] = true - } + GroupUse = append(GroupUse, AllProviders...) } else { GroupUse = groupOption.Use } + if groupOption.IncludeAllProxies { + GroupProxies = append(groupOption.Proxies, AllProxies...) + } else { + GroupProxies = groupOption.Proxies + } - if len(groupOption.Proxies) == 0 && len(GroupUse) == 0 { + if len(GroupProxies) == 0 && len(GroupUse) == 0 { return nil, fmt.Errorf("%s: %w", groupName, errMissProxy) } @@ -87,8 +95,8 @@ func ParseProxyGroup(config map[string]any, proxyMap map[string]C.Proxy, provide testUrl = groupOption.URL } - if len(groupOption.Proxies) != 0 { - ps, err := getProxies(proxyMap, groupOption.Proxies) + if len(GroupProxies) != 0 { + ps, err := getProxies(proxyMap, GroupProxies) if err != nil { return nil, fmt.Errorf("%s: %w", groupName, err) } diff --git a/adapter/provider/provider.go b/adapter/provider/provider.go index a2db06fc..d710d3a4 100644 --- a/adapter/provider/provider.go +++ b/adapter/provider/provider.go @@ -24,8 +24,6 @@ import ( "gopkg.in/yaml.v3" ) -var ProxyProviderName = make(map[string]struct{}) - const ( ReservedName = "default" ) @@ -201,7 +199,6 @@ 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, override), proxiesOnUpdate(pd)) pd.Fetcher = fetcher - ProxyProviderName[name] = struct{}{} wrapper := &ProxySetProvider{pd} runtime.SetFinalizer(wrapper, stopProxyProvider) return wrapper, nil diff --git a/config/config.go b/config/config.go index 9eaf5a49..bb503b49 100644 --- a/config/config.go +++ b/config/config.go @@ -695,6 +695,7 @@ func parseProxies(cfg *RawConfig) (proxies map[string]C.Proxy, providersMap map[ providersConfig := cfg.ProxyProvider var proxyList []string + var AllProxies []string proxiesList := list.New() groupsList := list.New() @@ -717,6 +718,7 @@ func parseProxies(cfg *RawConfig) (proxies map[string]C.Proxy, providersMap map[ } proxies[proxy.Name()] = proxy proxyList = append(proxyList, proxy.Name()) + AllProxies = append(AllProxies, proxy.Name()) proxiesList.PushBack(mapping) } @@ -735,6 +737,7 @@ func parseProxies(cfg *RawConfig) (proxies map[string]C.Proxy, providersMap map[ return nil, nil, err } + var AllProviders []string // parse and initial providers for name, mapping := range providersConfig { if name == provider.ReservedName { @@ -747,11 +750,12 @@ func parseProxies(cfg *RawConfig) (proxies map[string]C.Proxy, providersMap map[ } providersMap[name] = pd + AllProviders = append(AllProviders, name) } // parse proxy group for idx, mapping := range groupsConfig { - group, err := outboundgroup.ParseProxyGroup(mapping, proxies, providersMap) + group, err := outboundgroup.ParseProxyGroup(mapping, proxies, providersMap, AllProxies, AllProviders) if err != nil { return nil, nil, fmt.Errorf("proxy group[%d]: %w", idx, err) }