2022-11-22 09:05:54 +08:00
|
|
|
package wireguard
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2023-11-12 15:10:01 -05:00
|
|
|
"runtime"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
2022-11-22 09:05:54 +08:00
|
|
|
|
2025-12-10 15:17:29 +08:00
|
|
|
"github.com/xtls/xray-core/common/net"
|
2023-11-12 15:10:01 -05:00
|
|
|
)
|
2022-11-22 09:05:54 +08:00
|
|
|
|
2023-11-12 15:10:01 -05:00
|
|
|
func CalculateInterfaceName(name string) (tunName string) {
|
|
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
|
tunName = "utun"
|
|
|
|
|
} else if name != "" {
|
|
|
|
|
tunName = name
|
2022-11-22 09:05:54 +08:00
|
|
|
} else {
|
2023-11-12 15:10:01 -05:00
|
|
|
tunName = "tun"
|
2022-11-22 09:05:54 +08:00
|
|
|
}
|
2023-11-12 15:10:01 -05:00
|
|
|
interfaces, err := net.Interfaces()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
2022-11-22 09:05:54 +08:00
|
|
|
}
|
2023-11-12 15:10:01 -05:00
|
|
|
var tunIndex int
|
|
|
|
|
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
|
2022-11-22 09:05:54 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-12 15:10:01 -05:00
|
|
|
tunName = fmt.Sprintf("%s%d", tunName, tunIndex)
|
|
|
|
|
return
|
2022-11-22 09:05:54 +08:00
|
|
|
}
|