From ec41d4f5f7aa8332cfb7ea20288347de6428125d Mon Sep 17 00:00:00 2001 From: gVisor bot Date: Fri, 13 Nov 2020 21:48:52 +0800 Subject: [PATCH] Feature: add `disable-udp` option for all proxy group --- adapters/outboundgroup/fallback.go | 18 ++++++++++++------ adapters/outboundgroup/loadbalance.go | 8 +++++--- adapters/outboundgroup/parser.go | 23 ++++++++++++----------- adapters/outboundgroup/relay.go | 4 ++-- adapters/outboundgroup/selector.go | 22 ++++++++++++++-------- adapters/outboundgroup/urltest.go | 10 ++++++++-- config/config.go | 7 ++++++- 7 files changed, 59 insertions(+), 33 deletions(-) diff --git a/adapters/outboundgroup/fallback.go b/adapters/outboundgroup/fallback.go index acf577e1..8fb31c8e 100644 --- a/adapters/outboundgroup/fallback.go +++ b/adapters/outboundgroup/fallback.go @@ -12,8 +12,9 @@ import ( type Fallback struct { *outbound.Base - single *singledo.Single - providers []provider.ProxyProvider + disableUDP bool + single *singledo.Single + providers []provider.ProxyProvider } func (f *Fallback) Now() string { @@ -40,6 +41,10 @@ func (f *Fallback) DialUDP(metadata *C.Metadata) (C.PacketConn, error) { } func (f *Fallback) SupportUDP() bool { + if f.disableUDP { + return false + } + proxy := f.findAliveProxy() return proxy.SupportUDP() } @@ -80,10 +85,11 @@ func (f *Fallback) findAliveProxy() C.Proxy { return f.proxies()[0] } -func NewFallback(name string, providers []provider.ProxyProvider) *Fallback { +func NewFallback(options *GroupCommonOption, providers []provider.ProxyProvider) *Fallback { return &Fallback{ - Base: outbound.NewBase(name, "", C.Fallback, false), - single: singledo.NewSingle(defaultGetProxiesDuration), - providers: providers, + Base: outbound.NewBase(options.Name, "", C.Fallback, false), + single: singledo.NewSingle(defaultGetProxiesDuration), + providers: providers, + disableUDP: options.DisableUDP, } } diff --git a/adapters/outboundgroup/loadbalance.go b/adapters/outboundgroup/loadbalance.go index de634967..992b2c43 100644 --- a/adapters/outboundgroup/loadbalance.go +++ b/adapters/outboundgroup/loadbalance.go @@ -20,6 +20,7 @@ type strategyFn = func(proxies []C.Proxy, metadata *C.Metadata) C.Proxy type LoadBalance struct { *outbound.Base + disableUDP bool single *singledo.Single providers []provider.ProxyProvider strategyFn strategyFn @@ -93,7 +94,7 @@ func (lb *LoadBalance) DialUDP(metadata *C.Metadata) (pc C.PacketConn, err error } func (lb *LoadBalance) SupportUDP() bool { - return true + return !lb.disableUDP } func strategyRoundRobin() strategyFn { @@ -153,7 +154,7 @@ func (lb *LoadBalance) MarshalJSON() ([]byte, error) { }) } -func NewLoadBalance(name string, providers []provider.ProxyProvider, strategy string) (lb *LoadBalance, err error) { +func NewLoadBalance(options *GroupCommonOption, providers []provider.ProxyProvider, strategy string) (lb *LoadBalance, err error) { var strategyFn strategyFn switch strategy { case "consistent-hashing": @@ -164,9 +165,10 @@ func NewLoadBalance(name string, providers []provider.ProxyProvider, strategy st return nil, fmt.Errorf("%w: %s", errStrategy, strategy) } return &LoadBalance{ - Base: outbound.NewBase(name, "", C.LoadBalance, false), + Base: outbound.NewBase(options.Name, "", C.LoadBalance, false), single: singledo.NewSingle(defaultGetProxiesDuration), providers: providers, strategyFn: strategyFn, + disableUDP: options.DisableUDP, }, nil } diff --git a/adapters/outboundgroup/parser.go b/adapters/outboundgroup/parser.go index 9a4a1681..a6c2c848 100644 --- a/adapters/outboundgroup/parser.go +++ b/adapters/outboundgroup/parser.go @@ -18,12 +18,13 @@ var ( ) type GroupCommonOption struct { - 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"` + 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"` + DisableUDP bool `group:"disable-udp,omitempty"` } func ParseProxyGroup(config map[string]interface{}, proxyMap map[string]C.Proxy, providersMap map[string]provider.ProxyProvider) (C.ProxyAdapter, error) { @@ -105,16 +106,16 @@ func ParseProxyGroup(config map[string]interface{}, proxyMap map[string]C.Proxy, switch groupOption.Type { case "url-test": opts := parseURLTestOption(config) - group = NewURLTest(groupName, providers, opts...) + group = NewURLTest(groupOption, providers, opts...) case "select": - group = NewSelector(groupName, providers) + group = NewSelector(groupOption, providers) case "fallback": - group = NewFallback(groupName, providers) + group = NewFallback(groupOption, providers) case "load-balance": strategy := parseStrategy(config) - return NewLoadBalance(groupName, providers, strategy) + return NewLoadBalance(groupOption, providers, strategy) case "relay": - group = NewRelay(groupName, providers) + group = NewRelay(groupOption, providers) default: return nil, fmt.Errorf("%w: %s", errType, groupOption.Type) } diff --git a/adapters/outboundgroup/relay.go b/adapters/outboundgroup/relay.go index e27df09b..adc28881 100644 --- a/adapters/outboundgroup/relay.go +++ b/adapters/outboundgroup/relay.go @@ -89,9 +89,9 @@ func (r *Relay) proxies(metadata *C.Metadata) []C.Proxy { return proxies } -func NewRelay(name string, providers []provider.ProxyProvider) *Relay { +func NewRelay(options *GroupCommonOption, providers []provider.ProxyProvider) *Relay { return &Relay{ - Base: outbound.NewBase(name, "", C.Relay, false), + Base: outbound.NewBase(options.Name, "", C.Relay, false), single: singledo.NewSingle(defaultGetProxiesDuration), providers: providers, } diff --git a/adapters/outboundgroup/selector.go b/adapters/outboundgroup/selector.go index e253cb3e..e4f11b2f 100644 --- a/adapters/outboundgroup/selector.go +++ b/adapters/outboundgroup/selector.go @@ -13,9 +13,10 @@ import ( type Selector struct { *outbound.Base - single *singledo.Single - selected string - providers []provider.ProxyProvider + disableUDP bool + single *singledo.Single + selected string + providers []provider.ProxyProvider } func (s *Selector) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) { @@ -35,6 +36,10 @@ func (s *Selector) DialUDP(metadata *C.Metadata) (C.PacketConn, error) { } func (s *Selector) SupportUDP() bool { + if s.disableUDP { + return false + } + return s.selectedProxy().SupportUDP() } @@ -86,12 +91,13 @@ func (s *Selector) selectedProxy() C.Proxy { return elm.(C.Proxy) } -func NewSelector(name string, providers []provider.ProxyProvider) *Selector { +func NewSelector(options *GroupCommonOption, providers []provider.ProxyProvider) *Selector { selected := providers[0].Proxies()[0].Name() return &Selector{ - Base: outbound.NewBase(name, "", C.Selector, false), - single: singledo.NewSingle(defaultGetProxiesDuration), - providers: providers, - selected: selected, + Base: outbound.NewBase(options.Name, "", C.Selector, false), + single: singledo.NewSingle(defaultGetProxiesDuration), + providers: providers, + selected: selected, + disableUDP: options.DisableUDP, } } diff --git a/adapters/outboundgroup/urltest.go b/adapters/outboundgroup/urltest.go index c20340ec..1074a42a 100644 --- a/adapters/outboundgroup/urltest.go +++ b/adapters/outboundgroup/urltest.go @@ -22,6 +22,7 @@ func urlTestWithTolerance(tolerance uint16) urlTestOption { type URLTest struct { *outbound.Base tolerance uint16 + disableUDP bool fastNode C.Proxy single *singledo.Single fastSingle *singledo.Single @@ -89,6 +90,10 @@ func (u *URLTest) fast() C.Proxy { } func (u *URLTest) SupportUDP() bool { + if u.disableUDP { + return false + } + return u.fast().SupportUDP() } @@ -117,12 +122,13 @@ func parseURLTestOption(config map[string]interface{}) []urlTestOption { return opts } -func NewURLTest(name string, providers []provider.ProxyProvider, options ...urlTestOption) *URLTest { +func NewURLTest(commonOptions *GroupCommonOption, providers []provider.ProxyProvider, options ...urlTestOption) *URLTest { urlTest := &URLTest{ - Base: outbound.NewBase(name, "", C.URLTest, false), + Base: outbound.NewBase(commonOptions.Name, "", C.URLTest, false), single: singledo.NewSingle(defaultGetProxiesDuration), fastSingle: singledo.NewSingle(time.Second * 10), providers: providers, + disableUDP: commonOptions.DisableUDP, } for _, option := range options { diff --git a/config/config.go b/config/config.go index 68401346..99d48b92 100644 --- a/config/config.go +++ b/config/config.go @@ -349,7 +349,12 @@ func parseProxies(cfg *RawConfig) (proxies map[string]C.Proxy, providersMap map[ pd, _ := provider.NewCompatibleProvider(provider.ReservedName, ps, hc) providersMap[provider.ReservedName] = pd - global := outboundgroup.NewSelector("GLOBAL", []provider.ProxyProvider{pd}) + global := outboundgroup.NewSelector( + &outboundgroup.GroupCommonOption{ + Name: "GLOBAL", + }, + []provider.ProxyProvider{pd}, + ) proxies["GLOBAL"] = outbound.NewProxy(global) return proxies, providersMap, nil }