Skip to content

Commit

Permalink
fix: darwin calculate correct tunIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
wwqgtxx committed Jun 1, 2024
1 parent 59fd3cf commit be3d121
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions listener/sing_tun/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
E "github.com/sagernet/sing/common/exceptions"
F "github.com/sagernet/sing/common/format"
"github.com/sagernet/sing/common/ranges"
"golang.org/x/exp/slices"
)

var InterfaceName = "Meta"
Expand Down Expand Up @@ -60,19 +61,21 @@ func CalculateInterfaceName(name string) (tunName string) {
return
}
tunIndex := 0
indexSet := make(map[int]struct{})
indexArr := make([]int, 0, len(interfaces))
for _, netInterface := range interfaces {
if strings.HasPrefix(netInterface.Name, tunName) {
index, parseErr := strconv.ParseInt(netInterface.Name[len(tunName):], 10, 16)
if parseErr == nil {
indexSet[int(index)] = struct{}{}
indexArr = append(indexArr, int(index))
}
}
}
for index := range indexSet {
slices.Sort(indexArr)
indexArr = slices.Compact(indexArr)
for _, index := range indexArr {
if index == tunIndex {
tunIndex += 1
} else { // indexSet already sorted and distinct, so this tunIndex nobody used
} else { // indexArr already sorted and distinct, so this tunIndex nobody used
break
}
}
Expand Down

0 comments on commit be3d121

Please sign in to comment.