feat: add include-all to proxy-groups

This commit is contained in:
xishang0128 2023-12-31 09:43:52 +08:00
parent 3d643cb95a
commit 1d3e9f4889
No known key found for this signature in database
GPG Key ID: 44A1E10B5ADF68CB
3 changed files with 22 additions and 13 deletions

View File

@ -34,10 +34,12 @@ type GroupCommonOption struct {
ExcludeFilter string `group:"exclude-filter,omitempty"` ExcludeFilter string `group:"exclude-filter,omitempty"`
ExcludeType string `group:"exclude-type,omitempty"` ExcludeType string `group:"exclude-type,omitempty"`
ExpectedStatus string `group:"expected-status,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"` 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}) decoder := structure.NewDecoder(structure.Option{TagName: "group", WeaklyTypedInput: true})
groupOption := &GroupCommonOption{ groupOption := &GroupCommonOption{
@ -55,18 +57,24 @@ func ParseProxyGroup(config map[string]any, proxyMap map[string]C.Proxy, provide
providers := []types.ProxyProvider{} providers := []types.ProxyProvider{}
if groupOption.IncludeAll {
groupOption.IncludeAllProviders = true
groupOption.IncludeAllProxies = true
}
var GroupUse []string var GroupUse []string
visited := make(map[string]bool) var GroupProxies []string
if groupOption.IncludeAllProviders { if groupOption.IncludeAllProviders {
for name := range provider.ProxyProviderName { GroupUse = append(GroupUse, AllProviders...)
GroupUse = append(GroupUse, name)
visited[name] = true
}
} else { } else {
GroupUse = groupOption.Use 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) 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 testUrl = groupOption.URL
} }
if len(groupOption.Proxies) != 0 { if len(GroupProxies) != 0 {
ps, err := getProxies(proxyMap, groupOption.Proxies) ps, err := getProxies(proxyMap, GroupProxies)
if err != nil { if err != nil {
return nil, fmt.Errorf("%s: %w", groupName, err) return nil, fmt.Errorf("%s: %w", groupName, err)
} }

View File

@ -24,8 +24,6 @@ import (
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
var ProxyProviderName = make(map[string]struct{})
const ( const (
ReservedName = "default" 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)) fetcher := resource.NewFetcher[[]C.Proxy](name, interval, vehicle, proxiesParseAndFilter(filter, excludeFilter, excludeTypeArray, filterRegs, excludeFilterReg, dialerProxy, override), proxiesOnUpdate(pd))
pd.Fetcher = fetcher pd.Fetcher = fetcher
ProxyProviderName[name] = struct{}{}
wrapper := &ProxySetProvider{pd} wrapper := &ProxySetProvider{pd}
runtime.SetFinalizer(wrapper, stopProxyProvider) runtime.SetFinalizer(wrapper, stopProxyProvider)
return wrapper, nil return wrapper, nil

View File

@ -695,6 +695,7 @@ func parseProxies(cfg *RawConfig) (proxies map[string]C.Proxy, providersMap map[
providersConfig := cfg.ProxyProvider providersConfig := cfg.ProxyProvider
var proxyList []string var proxyList []string
var AllProxies []string
proxiesList := list.New() proxiesList := list.New()
groupsList := list.New() groupsList := list.New()
@ -717,6 +718,7 @@ func parseProxies(cfg *RawConfig) (proxies map[string]C.Proxy, providersMap map[
} }
proxies[proxy.Name()] = proxy proxies[proxy.Name()] = proxy
proxyList = append(proxyList, proxy.Name()) proxyList = append(proxyList, proxy.Name())
AllProxies = append(AllProxies, proxy.Name())
proxiesList.PushBack(mapping) proxiesList.PushBack(mapping)
} }
@ -735,6 +737,7 @@ func parseProxies(cfg *RawConfig) (proxies map[string]C.Proxy, providersMap map[
return nil, nil, err return nil, nil, err
} }
var AllProviders []string
// parse and initial providers // parse and initial providers
for name, mapping := range providersConfig { for name, mapping := range providersConfig {
if name == provider.ReservedName { if name == provider.ReservedName {
@ -747,11 +750,12 @@ func parseProxies(cfg *RawConfig) (proxies map[string]C.Proxy, providersMap map[
} }
providersMap[name] = pd providersMap[name] = pd
AllProviders = append(AllProviders, name)
} }
// parse proxy group // parse proxy group
for idx, mapping := range groupsConfig { for idx, mapping := range groupsConfig {
group, err := outboundgroup.ParseProxyGroup(mapping, proxies, providersMap) group, err := outboundgroup.ParseProxyGroup(mapping, proxies, providersMap, AllProxies, AllProviders)
if err != nil { if err != nil {
return nil, nil, fmt.Errorf("proxy group[%d]: %w", idx, err) return nil, nil, fmt.Errorf("proxy group[%d]: %w", idx, err)
} }