1
0
mirror of https://github.com/nitezs/sub2clash.git synced 2024-12-24 12:04:42 -05:00
This commit is contained in:
Nite07 2023-09-23 01:31:04 +08:00
parent c1e9099156
commit 67e4121fa6
4 changed files with 24 additions and 14 deletions

View File

@ -93,7 +93,7 @@ func BuildSub(clashType model.ClashType, query validator.SubValidator, template
if len(query.Proxies) != 0 { if len(query.Proxies) != 0 {
proxyList = append(proxyList, utils.ParseProxy(query.Proxies...)...) proxyList = append(proxyList, utils.ParseProxy(query.Proxies...)...)
} }
// 去 // 去掉配置相同的节点
proxies := make(map[string]*model.Proxy) proxies := make(map[string]*model.Proxy)
newProxies := make([]model.Proxy, 0, len(proxyList)) newProxies := make([]model.Proxy, 0, len(proxyList))
for i := range proxyList { for i := range proxyList {
@ -104,14 +104,6 @@ func BuildSub(clashType model.ClashType, query validator.SubValidator, template
} }
} }
proxyList = newProxies 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) != "" { if strings.TrimSpace(query.Remove) != "" {
newProxyList := make([]model.Proxy, 0, len(proxyList)) 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 // trim
for i := range proxyList { for i := range proxyList {
proxyList[i].Name = strings.TrimSpace(proxyList[i].Name) proxyList[i].Name = strings.TrimSpace(proxyList[i].Name)

View File

@ -35,7 +35,9 @@ func LoadSubscription(url string, refresh bool) ([]byte, error) {
return nil, err return nil, err
} }
defer func(file *os.File) { defer func(file *os.File) {
if file != nil {
_ = file.Close() _ = file.Close()
}
}(file) }(file)
fileLock.RLock() fileLock.RLock()
defer fileLock.RUnlock() defer fileLock.RUnlock()
@ -56,7 +58,9 @@ func FetchSubscriptionFromAPI(url string) ([]byte, error) {
return nil, err return nil, err
} }
defer func(Body io.ReadCloser) { defer func(Body io.ReadCloser) {
if Body != nil {
_ = Body.Close() _ = Body.Close()
}
}(resp.Body) }(resp.Body)
data, err := io.ReadAll(resp.Body) data, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
@ -67,7 +71,9 @@ func FetchSubscriptionFromAPI(url string) ([]byte, error) {
return nil, err return nil, err
} }
defer func(file *os.File) { defer func(file *os.File) {
if file != nil {
_ = file.Close() _ = file.Close()
}
}(file) }(file)
fileLock.Lock() fileLock.Lock()
defer fileLock.Unlock() defer fileLock.Unlock()

View File

@ -17,7 +17,9 @@ func LoadTemplate(template string) ([]byte, error) {
return nil, err return nil, err
} }
defer func(file *os.File) { defer func(file *os.File) {
if file != nil {
_ = file.Close() _ = file.Close()
}
}(file) }(file)
result, err := io.ReadAll(file) result, err := io.ReadAll(file)
if err != nil { if err != nil {

View File

@ -16,7 +16,9 @@ func writeTemplate(path string, template string) error {
return err return err
} }
defer func(file *os.File) { defer func(file *os.File) {
if file != nil {
_ = file.Close() _ = file.Close()
}
}(file) }(file)
_, err = file.WriteString(template) _, err = file.WriteString(template)
if err != nil { if err != nil {