From adad81e7a453ee3e3584b48d4d1362e68fd48e98 Mon Sep 17 00:00:00 2001 From: naison <895703375@qq.com> Date: Tue, 30 Sep 2025 16:41:51 +0800 Subject: [PATCH] fix: add index number to tun device name on windows (#726) --- pkg/tun/tun_windows.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/pkg/tun/tun_windows.go b/pkg/tun/tun_windows.go index 0118bed2..9d7c522b 100644 --- a/pkg/tun/tun_windows.go +++ b/pkg/tun/tun_windows.go @@ -31,13 +31,26 @@ func createTun(cfg Config) (conn net.Conn, itf *net.Interface, err error) { if len(cfg.Name) != 0 { tunName = cfg.Name } + var interfaces []net.Interface + interfaces, err = net.Interfaces() + if err != nil { + return + } + maxIndex := -1 + for _, i := range interfaces { + ifIndex := -1 + _, err := fmt.Sscanf(i.Name, "KubeVPN%d", &ifIndex) + if err == nil && ifIndex >= 0 && maxIndex < ifIndex { + maxIndex = ifIndex + } + } mtu := cfg.MTU if mtu <= 0 { mtu = config.DefaultMTU } wireguardtun.WintunTunnelType = "KubeVPN" - tunDevice, err := wireguardtun.CreateTUN(tunName, mtu) + tunDevice, err := wireguardtun.CreateTUN(fmt.Sprintf("KubeVPN%d", maxIndex+1), mtu) if err != nil { err = fmt.Errorf("failed to create TUN device: %w", err) return