From 36bc78070a2f4f43479334496851b9aafafd64b0 Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Sat, 28 Sep 2024 00:20:17 +0800 Subject: [PATCH] fix patchProviders works abnormally when path is empty --- core/src/main/golang/native/config/fetch.go | 2 +- core/src/main/golang/native/config/process.go | 13 ++++++++++--- core/src/main/golang/native/config/provider.go | 11 ++++++++--- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/core/src/main/golang/native/config/fetch.go b/core/src/main/golang/native/config/fetch.go index c1b25105..ba08ebcc 100644 --- a/core/src/main/golang/native/config/fetch.go +++ b/core/src/main/golang/native/config/fetch.go @@ -112,7 +112,7 @@ func FetchAndValid( 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{ Action: "FetchProviders", Args: []string{name}, diff --git a/core/src/main/golang/native/config/process.go b/core/src/main/golang/native/config/process.go index 09a4f8d1..9c986961 100644 --- a/core/src/main/golang/native/config/process.go +++ b/core/src/main/golang/native/config/process.go @@ -10,6 +10,7 @@ import ( "cfa/native/common" + "github.com/metacubex/mihomo/common/utils" "github.com/metacubex/mihomo/config" C "github.com/metacubex/mihomo/constant" "github.com/metacubex/mihomo/log" @@ -109,10 +110,16 @@ func patchListeners(cfg *config.RawConfig, _ string) error { } func patchProviders(cfg *config.RawConfig, profileDir string) error { - forEachProviders(cfg, func(index int, total int, key string, provider map[string]any) { - if path, ok := provider["path"].(string); ok { - provider["path"] = profileDir + "/providers/" + common.ResolveAsRoot(path) + forEachProviders(cfg, func(index int, total int, key string, provider map[string]any, prefix string) { + path, _ := provider["path"].(string) + 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 diff --git a/core/src/main/golang/native/config/provider.go b/core/src/main/golang/native/config/provider.go index 28506a5e..ebf4f34a 100644 --- a/core/src/main/golang/native/config/provider.go +++ b/core/src/main/golang/native/config/provider.go @@ -6,18 +6,23 @@ import ( "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) index := 0 for k, v := range rawCfg.ProxyProvider { - fun(index, total, k, v) + fun(index, total, k, v, PROXIES) index++ } for k, v := range rawCfg.RuleProvider { - fun(index, total, k, v) + fun(index, total, k, v, RULES) index++ }