fix: darwin calculate correct tunIndex

https://github.com/MetaCubeX/mihomo/pull/1285
This commit is contained in:
wwqgtxx 2024-05-21 19:13:44 +08:00
parent 3195c678c7
commit 43bdc76f87

View File

@ -59,15 +59,23 @@ func CalculateInterfaceName(name string) (tunName string) {
if err != nil { if err != nil {
return return
} }
var tunIndex int tunIndex := 0
indexSet := make(map[int]struct{})
for _, netInterface := range interfaces { for _, netInterface := range interfaces {
if strings.HasPrefix(netInterface.Name, tunName) { if strings.HasPrefix(netInterface.Name, tunName) {
index, parseErr := strconv.ParseInt(netInterface.Name[len(tunName):], 10, 16) index, parseErr := strconv.ParseInt(netInterface.Name[len(tunName):], 10, 16)
if parseErr == nil { if parseErr == nil {
tunIndex = int(index) + 1 indexSet[int(index)] = struct{}{}
} }
} }
} }
for index := range indexSet {
if index == tunIndex {
tunIndex += 1
} else { // indexSet already sorted and distinct, so this tunIndex nobody used
break
}
}
tunName = F.ToString(tunName, tunIndex) tunName = F.ToString(tunName, tunIndex)
return return
} }