From 43bdc76f87165bba04d0435e61b943ced945a872 Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Tue, 21 May 2024 19:13:44 +0800 Subject: [PATCH] fix: darwin calculate correct tunIndex https://github.com/MetaCubeX/mihomo/pull/1285 --- listener/sing_tun/server.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/listener/sing_tun/server.go b/listener/sing_tun/server.go index 2dc6524ee..09bf308c7 100644 --- a/listener/sing_tun/server.go +++ b/listener/sing_tun/server.go @@ -59,15 +59,23 @@ func CalculateInterfaceName(name string) (tunName string) { if err != nil { return } - var tunIndex int + tunIndex := 0 + indexSet := make(map[int]struct{}) for _, netInterface := range interfaces { if strings.HasPrefix(netInterface.Name, tunName) { index, parseErr := strconv.ParseInt(netInterface.Name[len(tunName):], 10, 16) 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) return }