fix patchProviders works abnormally when path is empty

This commit is contained in:
wwqgtxx 2024-09-28 00:20:17 +08:00
parent e164e219bd
commit 36bc78070a
3 changed files with 19 additions and 7 deletions

View File

@ -112,7 +112,7 @@ func FetchAndValid(
return err return err
} }
forEachProviders(rawCfg, func(index int, total int, name string, provider map[string]any) { forEachProviders(rawCfg, func(index int, total int, name string, provider map[string]any, prefix string) {
bytes, _ := json.Marshal(&Status{ bytes, _ := json.Marshal(&Status{
Action: "FetchProviders", Action: "FetchProviders",
Args: []string{name}, Args: []string{name},

View File

@ -10,6 +10,7 @@ import (
"cfa/native/common" "cfa/native/common"
"github.com/metacubex/mihomo/common/utils"
"github.com/metacubex/mihomo/config" "github.com/metacubex/mihomo/config"
C "github.com/metacubex/mihomo/constant" C "github.com/metacubex/mihomo/constant"
"github.com/metacubex/mihomo/log" "github.com/metacubex/mihomo/log"
@ -109,10 +110,16 @@ func patchListeners(cfg *config.RawConfig, _ string) error {
} }
func patchProviders(cfg *config.RawConfig, profileDir string) error { func patchProviders(cfg *config.RawConfig, profileDir string) error {
forEachProviders(cfg, func(index int, total int, key string, provider map[string]any) { forEachProviders(cfg, func(index int, total int, key string, provider map[string]any, prefix string) {
if path, ok := provider["path"].(string); ok { path, _ := provider["path"].(string)
provider["path"] = profileDir + "/providers/" + common.ResolveAsRoot(path) if len(path) > 0 {
path = common.ResolveAsRoot(path)
} else if url, ok := provider["url"].(string); ok {
path = prefix + "/" + utils.MakeHash([]byte(url)).String() // same as C.GetPathByHash
} else {
return // both path and url is empty, WTF???
} }
provider["path"] = profileDir + "/providers/" + path
}) })
return nil return nil

View File

@ -6,18 +6,23 @@ import (
"github.com/metacubex/mihomo/config" "github.com/metacubex/mihomo/config"
) )
func forEachProviders(rawCfg *config.RawConfig, fun func(index int, total int, key string, provider map[string]any)) { const (
PROXIES = "proxies"
RULES = "rules"
)
func forEachProviders(rawCfg *config.RawConfig, fun func(index int, total int, key string, provider map[string]any, prefix string)) {
total := len(rawCfg.ProxyProvider) + len(rawCfg.RuleProvider) total := len(rawCfg.ProxyProvider) + len(rawCfg.RuleProvider)
index := 0 index := 0
for k, v := range rawCfg.ProxyProvider { for k, v := range rawCfg.ProxyProvider {
fun(index, total, k, v) fun(index, total, k, v, PROXIES)
index++ index++
} }
for k, v := range rawCfg.RuleProvider { for k, v := range rawCfg.RuleProvider {
fun(index, total, k, v) fun(index, total, k, v, RULES)
index++ index++
} }