From 2dc7ca4134e823127600e9c72a9f28feb88cd9b5 Mon Sep 17 00:00:00 2001 From: gVisor bot Date: Thu, 18 Oct 2018 23:24:04 +0800 Subject: [PATCH] Fix: selector no longer randomly selects --- adapters/outbound/selector.go | 11 +++++----- config/config.go | 41 +++++++++++++++-------------------- config/utils.go | 14 ++++++++++++ 3 files changed, 37 insertions(+), 29 deletions(-) diff --git a/adapters/outbound/selector.go b/adapters/outbound/selector.go index ee547344..1f920ae1 100644 --- a/adapters/outbound/selector.go +++ b/adapters/outbound/selector.go @@ -52,21 +52,20 @@ func (s *Selector) Set(name string) error { return nil } -func NewSelector(name string, proxies map[string]C.Proxy) (*Selector, error) { +func NewSelector(name string, proxies []C.Proxy) (*Selector, error) { if len(proxies) == 0 { return nil, errors.New("Provide at least one proxy") } mapping := make(map[string]C.Proxy) - var init string - for k, v := range proxies { - mapping[k] = v - init = k + for _, proxy := range proxies { + mapping[proxy.Name()] = proxy } + s := &Selector{ name: name, proxies: mapping, - selected: proxies[init], + selected: proxies[0], } return s, nil } diff --git a/config/config.go b/config/config.go index 4d6b87fa..40e27c3c 100644 --- a/config/config.go +++ b/config/config.go @@ -312,13 +312,9 @@ func (c *Config) parseProxies(cfg *RawConfig) error { break } - var ps []C.Proxy - for _, name := range urlTestOption.Proxies { - p, ok := proxies[name] - if !ok { - return fmt.Errorf("ProxyGroup %s: proxy or proxy group '%s' not found", groupName, name) - } - ps = append(ps, p) + ps, err := getProxies(proxies, urlTestOption.Proxies) + if err != nil { + return fmt.Errorf("ProxyGroup %s: %s", groupName, err.Error()) } group, err = adapters.NewURLTest(*urlTestOption, ps) case "select": @@ -327,28 +323,22 @@ func (c *Config) parseProxies(cfg *RawConfig) error { if err != nil { break } - selectProxy := make(map[string]C.Proxy) - for _, name := range selectorOption.Proxies { - proxy, exist := proxies[name] - if !exist { - return fmt.Errorf("ProxyGroup %s: proxy or proxy group '%s' not found", groupName, name) - } - selectProxy[name] = proxy + + ps, err := getProxies(proxies, selectorOption.Proxies) + if err != nil { + return fmt.Errorf("ProxyGroup %s: %s", groupName, err.Error()) } - group, err = adapters.NewSelector(selectorOption.Name, selectProxy) + group, err = adapters.NewSelector(selectorOption.Name, ps) case "fallback": fallbackOption := &adapters.FallbackOption{} err = decoder.Decode(mapping, fallbackOption) if err != nil { break } - var ps []C.Proxy - for _, name := range fallbackOption.Proxies { - p, ok := proxies[name] - if !ok { - return fmt.Errorf("ProxyGroup %s: proxy or proxy group '%s' not found", groupName, name) - } - ps = append(ps, p) + + ps, err := getProxies(proxies, fallbackOption.Proxies) + if err != nil { + return fmt.Errorf("ProxyGroup %s: %s", groupName, err.Error()) } group, err = adapters.NewFallback(*fallbackOption, ps) } @@ -358,7 +348,12 @@ func (c *Config) parseProxies(cfg *RawConfig) error { proxies[groupName] = group } - proxies["GLOBAL"], _ = adapters.NewSelector("GLOBAL", proxies) + var ps []C.Proxy + for _, v := range proxies { + ps = append(ps, v) + } + + proxies["GLOBAL"], _ = adapters.NewSelector("GLOBAL", ps) // close old goroutine for _, proxy := range c.proxies { diff --git a/config/utils.go b/config/utils.go index e196394b..ca5a7723 100644 --- a/config/utils.go +++ b/config/utils.go @@ -3,6 +3,8 @@ package config import ( "fmt" "strings" + + C "github.com/Dreamacro/clash/constant" ) func trimArr(arr []string) (r []string) { @@ -19,6 +21,18 @@ func genAddr(port int, allowLan bool) string { return fmt.Sprintf("127.0.0.1:%d", port) } +func getProxies(mapping map[string]C.Proxy, list []string) ([]C.Proxy, error) { + var ps []C.Proxy + for _, name := range list { + p, ok := mapping[name] + if !ok { + return nil, fmt.Errorf("'%s' not found", name) + } + ps = append(ps, p) + } + return ps, nil +} + func or(pointers ...*int) *int { for _, p := range pointers { if p != nil {