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:
ezksd 2019-05-15 14:40:14 +08:00 committed by Dreamacro
parent 71a08ad8e2
commit 0eccbb023c
2 changed files with 16 additions and 14 deletions

View file

@ -4,15 +4,15 @@ import (
"encoding/json"
"errors"
"net"
"sort"
C "github.com/Dreamacro/clash/constant"
)
type Selector struct {
*Base
selected C.Proxy
proxies map[string]C.Proxy
selected C.Proxy
proxies map[string]C.Proxy
proxyList []string
}
type SelectorOption struct {
@ -33,15 +33,10 @@ func (s *Selector) SupportUDP() bool {
}
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{}{
"type": s.Type().String(),
"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)
for _, proxy := range proxies {
proxyList := make([]string, len(proxies))
for idx, proxy := range proxies {
mapping[proxy.Name()] = proxy
proxyList[idx] = proxy.Name()
}
s := &Selector{
@ -73,8 +70,9 @@ func NewSelector(name string, proxies []C.Proxy) (*Selector, error) {
name: name,
tp: C.Selector,
},
proxies: mapping,
selected: proxies[0],
proxies: mapping,
selected: proxies[0],
proxyList: proxyList,
}
return s, nil
}

View file

@ -194,6 +194,7 @@ func parseGeneral(cfg *rawConfig) (*General, error) {
func parseProxies(cfg *rawConfig) (map[string]C.Proxy, error) {
proxies := make(map[string]C.Proxy)
proxyList := []string{}
proxiesConfig := cfg.Proxy
groupsConfig := cfg.ProxyGroup
@ -201,6 +202,7 @@ func parseProxies(cfg *rawConfig) (map[string]C.Proxy, error) {
proxies["DIRECT"] = adapters.NewProxy(adapters.NewDirect())
proxies["REJECT"] = adapters.NewProxy(adapters.NewReject())
proxyList = append(proxyList, "DIRECT", "REJECT")
// parse proxy
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())
}
proxies[proxy.Name()] = adapters.NewProxy(proxy)
proxyList = append(proxyList, proxy.Name())
}
// 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())
}
proxies[groupName] = adapters.NewProxy(group)
proxyList = append(proxyList, groupName)
}
ps := []C.Proxy{}
for _, v := range proxies {
ps = append(ps, v)
for _, v := range proxyList {
ps = append(ps, proxies[v])
}
global, _ := adapters.NewSelector("GLOBAL", ps)