From d1fd57c432f05418b83cc48958c983fccd78db33 Mon Sep 17 00:00:00 2001 From: Dreamacro <305009791@qq.com> Date: Mon, 27 Apr 2020 21:23:03 +0800 Subject: [PATCH] Fix: select group can use provider real-time --- adapters/outboundgroup/selector.go | 31 ++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/adapters/outboundgroup/selector.go b/adapters/outboundgroup/selector.go index 6ad78f2..88ac815 100644 --- a/adapters/outboundgroup/selector.go +++ b/adapters/outboundgroup/selector.go @@ -14,12 +14,12 @@ import ( type Selector struct { *outbound.Base single *singledo.Single - selected C.Proxy + selected string providers []provider.ProxyProvider } func (s *Selector) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) { - c, err := s.selected.DialContext(ctx, metadata) + c, err := s.selectedProxy().DialContext(ctx, metadata) if err == nil { c.AppendToChains(s) } @@ -27,7 +27,7 @@ func (s *Selector) DialContext(ctx context.Context, metadata *C.Metadata) (C.Con } func (s *Selector) DialUDP(metadata *C.Metadata) (C.PacketConn, error) { - pc, err := s.selected.DialUDP(metadata) + pc, err := s.selectedProxy().DialUDP(metadata) if err == nil { pc.AppendToChains(s) } @@ -35,12 +35,12 @@ func (s *Selector) DialUDP(metadata *C.Metadata) (C.PacketConn, error) { } func (s *Selector) SupportUDP() bool { - return s.selected.SupportUDP() + return s.selectedProxy().SupportUDP() } func (s *Selector) MarshalJSON() ([]byte, error) { var all []string - for _, proxy := range s.proxies() { + for _, proxy := range getProvidersProxies(s.providers) { all = append(all, proxy.Name()) } @@ -52,13 +52,13 @@ func (s *Selector) MarshalJSON() ([]byte, error) { } func (s *Selector) Now() string { - return s.selected.Name() + return s.selectedProxy().Name() } func (s *Selector) Set(name string) error { - for _, proxy := range s.proxies() { + for _, proxy := range getProvidersProxies(s.providers) { if proxy.Name() == name { - s.selected = proxy + s.selected = name return nil } } @@ -66,16 +66,23 @@ func (s *Selector) Set(name string) error { return errors.New("Proxy does not exist") } -func (s *Selector) proxies() []C.Proxy { +func (s *Selector) selectedProxy() C.Proxy { elm, _, _ := s.single.Do(func() (interface{}, error) { - return getProvidersProxies(s.providers), nil + proxies := getProvidersProxies(s.providers) + for _, proxy := range proxies { + if proxy.Name() == s.selected { + return proxy, nil + } + } + + return proxies[0], nil }) - return elm.([]C.Proxy) + return elm.(C.Proxy) } func NewSelector(name string, providers []provider.ProxyProvider) *Selector { - selected := providers[0].Proxies()[0] + selected := providers[0].Proxies()[0].Name() return &Selector{ Base: outbound.NewBase(name, "", C.Selector, false), single: singledo.NewSingle(defaultGetProxiesDuration),