From 67e4121fa6ffe9e442f4fdc9969f7250c6e75abb Mon Sep 17 00:00:00 2001 From: nitezs Date: Sat, 23 Sep 2023 01:31:04 +0800 Subject: [PATCH] fix --- api/controller/default.go | 18 +++++++++--------- utils/sub.go | 12 +++++++++--- utils/template.go | 4 +++- utils/write_default_template.go | 4 +++- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/api/controller/default.go b/api/controller/default.go index 8631326..174dccf 100644 --- a/api/controller/default.go +++ b/api/controller/default.go @@ -93,7 +93,7 @@ func BuildSub(clashType model.ClashType, query validator.SubValidator, template if len(query.Proxies) != 0 { proxyList = append(proxyList, utils.ParseProxy(query.Proxies...)...) } - // 去重 + // 去掉配置相同的节点 proxies := make(map[string]*model.Proxy) newProxies := make([]model.Proxy, 0, len(proxyList)) for i := range proxyList { @@ -104,14 +104,6 @@ func BuildSub(clashType model.ClashType, query validator.SubValidator, template } } proxyList = newProxies - // 重名检测 - names := make(map[string]bool) - for i := range proxyList { - if _, exist := names[proxyList[i].Name]; exist { - proxyList[i].Name = proxyList[i].Name + "@" + proxyList[i].Server + ":" + strconv.Itoa(proxyList[i].Port) - } - names[proxyList[i].Name] = true - } // 删除节点 if strings.TrimSpace(query.Remove) != "" { newProxyList := make([]model.Proxy, 0, len(proxyList)) @@ -156,6 +148,14 @@ func BuildSub(clashType model.ClashType, query validator.SubValidator, template } } } + // 重名检测 + names := make(map[string]int) + for i := range proxyList { + if _, exist := names[proxyList[i].Name]; exist { + proxyList[i].Name = proxyList[i].Name + " " + strconv.Itoa(names[proxyList[i].Name]) + } + names[proxyList[i].Name] = names[proxyList[i].Name] + 1 + } // trim for i := range proxyList { proxyList[i].Name = strings.TrimSpace(proxyList[i].Name) diff --git a/utils/sub.go b/utils/sub.go index 00bf437..d7442b1 100644 --- a/utils/sub.go +++ b/utils/sub.go @@ -35,7 +35,9 @@ func LoadSubscription(url string, refresh bool) ([]byte, error) { return nil, err } defer func(file *os.File) { - _ = file.Close() + if file != nil { + _ = file.Close() + } }(file) fileLock.RLock() defer fileLock.RUnlock() @@ -56,7 +58,9 @@ func FetchSubscriptionFromAPI(url string) ([]byte, error) { return nil, err } defer func(Body io.ReadCloser) { - _ = Body.Close() + if Body != nil { + _ = Body.Close() + } }(resp.Body) data, err := io.ReadAll(resp.Body) if err != nil { @@ -67,7 +71,9 @@ func FetchSubscriptionFromAPI(url string) ([]byte, error) { return nil, err } defer func(file *os.File) { - _ = file.Close() + if file != nil { + _ = file.Close() + } }(file) fileLock.Lock() defer fileLock.Unlock() diff --git a/utils/template.go b/utils/template.go index 781a099..45bf68a 100644 --- a/utils/template.go +++ b/utils/template.go @@ -17,7 +17,9 @@ func LoadTemplate(template string) ([]byte, error) { return nil, err } defer func(file *os.File) { - _ = file.Close() + if file != nil { + _ = file.Close() + } }(file) result, err := io.ReadAll(file) if err != nil { diff --git a/utils/write_default_template.go b/utils/write_default_template.go index 07a60ae..9fbb192 100644 --- a/utils/write_default_template.go +++ b/utils/write_default_template.go @@ -16,7 +16,9 @@ func writeTemplate(path string, template string) error { return err } defer func(file *os.File) { - _ = file.Close() + if file != nil { + _ = file.Close() + } }(file) _, err = file.WriteString(template) if err != nil {