Feature: make the selector proxies order as same as the order in the config file (#180)

* make the proxies order the same as the order in config file

* formatting & rename variable
This commit is contained in:
gVisor bot 2019-05-15 14:40:14 +08:00
parent 31b496a5d1
commit 8a4497ddf4
2 changed files with 16 additions and 14 deletions

View file

@ -4,15 +4,15 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"net" "net"
"sort"
C "github.com/Dreamacro/clash/constant" C "github.com/Dreamacro/clash/constant"
) )
type Selector struct { type Selector struct {
*Base *Base
selected C.Proxy selected C.Proxy
proxies map[string]C.Proxy proxies map[string]C.Proxy
proxyList []string
} }
type SelectorOption struct { type SelectorOption struct {
@ -33,15 +33,10 @@ func (s *Selector) SupportUDP() bool {
} }
func (s *Selector) MarshalJSON() ([]byte, error) { func (s *Selector) MarshalJSON() ([]byte, error) {
var all []string
for k := range s.proxies {
all = append(all, k)
}
sort.Strings(all)
return json.Marshal(map[string]interface{}{ return json.Marshal(map[string]interface{}{
"type": s.Type().String(), "type": s.Type().String(),
"now": s.Now(), "now": s.Now(),
"all": all, "all": s.proxyList,
}) })
} }
@ -64,8 +59,10 @@ func NewSelector(name string, proxies []C.Proxy) (*Selector, error) {
} }
mapping := make(map[string]C.Proxy) mapping := make(map[string]C.Proxy)
for _, proxy := range proxies { proxyList := make([]string, len(proxies))
for idx, proxy := range proxies {
mapping[proxy.Name()] = proxy mapping[proxy.Name()] = proxy
proxyList[idx] = proxy.Name()
} }
s := &Selector{ s := &Selector{
@ -73,8 +70,9 @@ func NewSelector(name string, proxies []C.Proxy) (*Selector, error) {
name: name, name: name,
tp: C.Selector, tp: C.Selector,
}, },
proxies: mapping, proxies: mapping,
selected: proxies[0], selected: proxies[0],
proxyList: proxyList,
} }
return s, nil return s, nil
} }

View file

@ -194,6 +194,7 @@ func parseGeneral(cfg *rawConfig) (*General, error) {
func parseProxies(cfg *rawConfig) (map[string]C.Proxy, error) { func parseProxies(cfg *rawConfig) (map[string]C.Proxy, error) {
proxies := make(map[string]C.Proxy) proxies := make(map[string]C.Proxy)
proxyList := []string{}
proxiesConfig := cfg.Proxy proxiesConfig := cfg.Proxy
groupsConfig := cfg.ProxyGroup groupsConfig := cfg.ProxyGroup
@ -201,6 +202,7 @@ func parseProxies(cfg *rawConfig) (map[string]C.Proxy, error) {
proxies["DIRECT"] = adapters.NewProxy(adapters.NewDirect()) proxies["DIRECT"] = adapters.NewProxy(adapters.NewDirect())
proxies["REJECT"] = adapters.NewProxy(adapters.NewReject()) proxies["REJECT"] = adapters.NewProxy(adapters.NewReject())
proxyList = append(proxyList, "DIRECT", "REJECT")
// parse proxy // parse proxy
for idx, mapping := range proxiesConfig { for idx, mapping := range proxiesConfig {
@ -252,6 +254,7 @@ func parseProxies(cfg *rawConfig) (map[string]C.Proxy, error) {
return nil, fmt.Errorf("Proxy %s is the duplicate name", proxy.Name()) return nil, fmt.Errorf("Proxy %s is the duplicate name", proxy.Name())
} }
proxies[proxy.Name()] = adapters.NewProxy(proxy) proxies[proxy.Name()] = adapters.NewProxy(proxy)
proxyList = append(proxyList, proxy.Name())
} }
// parse proxy group // parse proxy group
@ -323,11 +326,12 @@ func parseProxies(cfg *rawConfig) (map[string]C.Proxy, error) {
return nil, fmt.Errorf("Proxy %s: %s", groupName, err.Error()) return nil, fmt.Errorf("Proxy %s: %s", groupName, err.Error())
} }
proxies[groupName] = adapters.NewProxy(group) proxies[groupName] = adapters.NewProxy(group)
proxyList = append(proxyList, groupName)
} }
ps := []C.Proxy{} ps := []C.Proxy{}
for _, v := range proxies { for _, v := range proxyList {
ps = append(ps, v) ps = append(ps, proxies[v])
} }
global, _ := adapters.NewSelector("GLOBAL", ps) global, _ := adapters.NewSelector("GLOBAL", ps)