From 8a4497ddf4a5151fd88c7276b712ab44622f9bc3 Mon Sep 17 00:00:00 2001 From: gVisor bot Date: Wed, 15 May 2019 14:40:14 +0800 Subject: [PATCH] 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 --- adapters/outbound/selector.go | 22 ++++++++++------------ config/config.go | 8 ++++++-- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/adapters/outbound/selector.go b/adapters/outbound/selector.go index 39b4ac0d..a14a7ce3 100644 --- a/adapters/outbound/selector.go +++ b/adapters/outbound/selector.go @@ -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 } diff --git a/config/config.go b/config/config.go index ddc0548a..9ecf6c39 100644 --- a/config/config.go +++ b/config/config.go @@ -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)