Fix: selector no longer randomly selects
This commit is contained in:
parent
ce07eda428
commit
94d1972782
3 changed files with 37 additions and 29 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue