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

View file

@ -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)
ps, err := getProxies(proxies, selectorOption.Proxies)
if err != nil {
return fmt.Errorf("ProxyGroup %s: %s", groupName, err.Error())
}
selectProxy[name] = proxy
}
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 {

View file

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