Fix: selector no longer randomly selects

This commit is contained in:
gVisor bot 2018-10-18 23:24:04 +08:00
parent 6b2c92e797
commit 2dc7ca4134
3 changed files with 37 additions and 29 deletions

View file

@ -52,21 +52,20 @@ func (s *Selector) Set(name string) error {
return nil 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 { if len(proxies) == 0 {
return nil, errors.New("Provide at least one proxy") return nil, errors.New("Provide at least one proxy")
} }
mapping := make(map[string]C.Proxy) mapping := make(map[string]C.Proxy)
var init string for _, proxy := range proxies {
for k, v := range proxies { mapping[proxy.Name()] = proxy
mapping[k] = v
init = k
} }
s := &Selector{ s := &Selector{
name: name, name: name,
proxies: mapping, proxies: mapping,
selected: proxies[init], selected: proxies[0],
} }
return s, nil return s, nil
} }

View file

@ -312,13 +312,9 @@ func (c *Config) parseProxies(cfg *RawConfig) error {
break break
} }
var ps []C.Proxy ps, err := getProxies(proxies, urlTestOption.Proxies)
for _, name := range urlTestOption.Proxies { if err != nil {
p, ok := proxies[name] return fmt.Errorf("ProxyGroup %s: %s", groupName, err.Error())
if !ok {
return fmt.Errorf("ProxyGroup %s: proxy or proxy group '%s' not found", groupName, name)
}
ps = append(ps, p)
} }
group, err = adapters.NewURLTest(*urlTestOption, ps) group, err = adapters.NewURLTest(*urlTestOption, ps)
case "select": case "select":
@ -327,28 +323,22 @@ func (c *Config) parseProxies(cfg *RawConfig) error {
if err != nil { if err != nil {
break break
} }
selectProxy := make(map[string]C.Proxy)
for _, name := range selectorOption.Proxies { ps, err := getProxies(proxies, selectorOption.Proxies)
proxy, exist := proxies[name] if err != nil {
if !exist { return fmt.Errorf("ProxyGroup %s: %s", groupName, err.Error())
return fmt.Errorf("ProxyGroup %s: proxy or proxy group '%s' not found", groupName, name)
} }
selectProxy[name] = proxy group, err = adapters.NewSelector(selectorOption.Name, ps)
}
group, err = adapters.NewSelector(selectorOption.Name, selectProxy)
case "fallback": case "fallback":
fallbackOption := &adapters.FallbackOption{} fallbackOption := &adapters.FallbackOption{}
err = decoder.Decode(mapping, fallbackOption) err = decoder.Decode(mapping, fallbackOption)
if err != nil { if err != nil {
break break
} }
var ps []C.Proxy
for _, name := range fallbackOption.Proxies { ps, err := getProxies(proxies, fallbackOption.Proxies)
p, ok := proxies[name] if err != nil {
if !ok { return fmt.Errorf("ProxyGroup %s: %s", groupName, err.Error())
return fmt.Errorf("ProxyGroup %s: proxy or proxy group '%s' not found", groupName, name)
}
ps = append(ps, p)
} }
group, err = adapters.NewFallback(*fallbackOption, ps) group, err = adapters.NewFallback(*fallbackOption, ps)
} }
@ -358,7 +348,12 @@ func (c *Config) parseProxies(cfg *RawConfig) error {
proxies[groupName] = group 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 // close old goroutine
for _, proxy := range c.proxies { for _, proxy := range c.proxies {

View file

@ -3,6 +3,8 @@ package config
import ( import (
"fmt" "fmt"
"strings" "strings"
C "github.com/Dreamacro/clash/constant"
) )
func trimArr(arr []string) (r []string) { 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) 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 { func or(pointers ...*int) *int {
for _, p := range pointers { for _, p := range pointers {
if p != nil { if p != nil {