Feature: add disable-udp option for all proxy group

This commit is contained in:
gVisor bot 2020-11-13 21:48:52 +08:00
parent beda9f2647
commit ec41d4f5f7
7 changed files with 59 additions and 33 deletions

View file

@ -12,6 +12,7 @@ import (
type Fallback struct {
*outbound.Base
disableUDP bool
single *singledo.Single
providers []provider.ProxyProvider
}
@ -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),
Base: outbound.NewBase(options.Name, "", C.Fallback, false),
single: singledo.NewSingle(defaultGetProxiesDuration),
providers: providers,
disableUDP: options.DisableUDP,
}
}

View file

@ -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
}

View file

@ -24,6 +24,7 @@ type GroupCommonOption struct {
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)
}

View file

@ -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,
}

View file

@ -13,6 +13,7 @@ import (
type Selector struct {
*outbound.Base
disableUDP bool
single *singledo.Single
selected string
providers []provider.ProxyProvider
@ -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),
Base: outbound.NewBase(options.Name, "", C.Selector, false),
single: singledo.NewSingle(defaultGetProxiesDuration),
providers: providers,
selected: selected,
disableUDP: options.DisableUDP,
}
}

View file

@ -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 {

View file

@ -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
}