feat: add include-all-providers to proxy-groups

This commit is contained in:
xishang0128 2023-11-11 22:15:57 +08:00
parent 832dae3421
commit 288c0c27d6
2 changed files with 30 additions and 15 deletions

View file

@ -22,18 +22,19 @@ var (
type GroupCommonOption struct { type GroupCommonOption struct {
outbound.BasicOption outbound.BasicOption
Name string `group:"name"` Name string `group:"name"`
Type string `group:"type"` Type string `group:"type"`
Proxies []string `group:"proxies,omitempty"` Proxies []string `group:"proxies,omitempty"`
Use []string `group:"use,omitempty"` Use []string `group:"use,omitempty"`
URL string `group:"url,omitempty"` URL string `group:"url,omitempty"`
Interval int `group:"interval,omitempty"` Interval int `group:"interval,omitempty"`
Lazy bool `group:"lazy,omitempty"` Lazy bool `group:"lazy,omitempty"`
DisableUDP bool `group:"disable-udp,omitempty"` DisableUDP bool `group:"disable-udp,omitempty"`
Filter string `group:"filter,omitempty"` Filter string `group:"filter,omitempty"`
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"`
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) (C.ProxyAdapter, error) {
@ -54,7 +55,18 @@ func ParseProxyGroup(config map[string]any, proxyMap map[string]C.Proxy, provide
providers := []types.ProxyProvider{} 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) 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 providersMap[groupName] = pd
} }
if len(groupOption.Use) != 0 { if len(GroupUse) != 0 {
list, err := getProviders(providersMap, groupOption.Use) list, err := getProviders(providersMap, GroupUse)
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,6 +24,8 @@ import (
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
var ProxyProviderName = make(map[string]struct{})
const ( const (
ReservedName = "default" 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)) fetcher := resource.NewFetcher[[]C.Proxy](name, interval, vehicle, proxiesParseAndFilter(filter, excludeFilter, excludeTypeArray, filterRegs, excludeFilterReg, dialerProxy), 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