Feature: support store group selected node to cache (enable by default)

This commit is contained in:
Dreamacro
2021-02-18 23:41:50 +08:00
parent aa81193d5b
commit 14bbf6eedc
6 changed files with 183 additions and 1 deletions

View File

@ -6,9 +6,13 @@ import (
"os"
"sync"
"github.com/Dreamacro/clash/adapters/outbound"
"github.com/Dreamacro/clash/adapters/outboundgroup"
"github.com/Dreamacro/clash/adapters/provider"
"github.com/Dreamacro/clash/component/auth"
"github.com/Dreamacro/clash/component/dialer"
"github.com/Dreamacro/clash/component/profile"
"github.com/Dreamacro/clash/component/profile/cachefile"
"github.com/Dreamacro/clash/component/resolver"
"github.com/Dreamacro/clash/component/trie"
"github.com/Dreamacro/clash/config"
@ -72,6 +76,7 @@ func ApplyConfig(cfg *config.Config, force bool) {
updateDNS(cfg.DNS)
updateHosts(cfg.Hosts)
updateExperimental(cfg)
updateProfile(cfg)
}
func GetGeneral() *config.General {
@ -209,3 +214,38 @@ func updateUsers(users []auth.AuthUser) {
log.Infoln("Authentication of local server updated")
}
}
func updateProfile(cfg *config.Config) {
profileCfg := cfg.Profile
profile.StoreSelected.Store(profileCfg.StoreSelected)
if profileCfg.StoreSelected {
patchSelectGroup(cfg.Proxies)
}
}
func patchSelectGroup(proxies map[string]C.Proxy) {
mapping := cachefile.Cache().SelectedMap()
if mapping == nil {
return
}
for name, proxy := range proxies {
outbound, ok := proxy.(*outbound.Proxy)
if !ok {
continue
}
selector, ok := outbound.ProxyAdapter.(*outboundgroup.Selector)
if !ok {
continue
}
selected, exist := mapping[name]
if !exist {
continue
}
selector.Set(selected)
}
}

View File

@ -9,6 +9,7 @@ import (
"github.com/Dreamacro/clash/adapters/outbound"
"github.com/Dreamacro/clash/adapters/outboundgroup"
"github.com/Dreamacro/clash/component/profile/cachefile"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/tunnel"
@ -91,6 +92,7 @@ func updateProxy(w http.ResponseWriter, r *http.Request) {
return
}
cachefile.Cache().SetSelected(proxy.Name(), req.Name)
render.NoContent(w, r)
}