Skip to content

Commit

Permalink
fix patchProviders works abnormally when path is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
wwqgtxx committed Sep 27, 2024
1 parent e164e21 commit 36bc780
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion core/src/main/golang/native/config/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
13 changes: 10 additions & 3 deletions core/src/main/golang/native/config/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions core/src/main/golang/native/config/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++
}
Expand Down

0 comments on commit 36bc780

Please sign in to comment.