Fixed wait for TUN to come up

This commit is contained in:
Revertron 2024-08-06 00:10:57 +02:00 committed by GitHub
parent edf179ed26
commit fd453d986c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -8,10 +8,12 @@ import (
"fmt" "fmt"
"log" "log"
"net/netip" "net/netip"
"time"
"github.com/yggdrasil-network/yggdrasil-go/src/config" "github.com/yggdrasil-network/yggdrasil-go/src/config"
"golang.org/x/sys/windows" "golang.org/x/sys/windows"
"golang.zx2c4.com/wintun"
wgtun "golang.zx2c4.com/wireguard/tun" wgtun "golang.zx2c4.com/wireguard/tun"
"golang.zx2c4.com/wireguard/windows/elevate" "golang.zx2c4.com/wireguard/windows/elevate"
"golang.zx2c4.com/wireguard/windows/tunnel/winipcfg" "golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
@ -31,14 +33,24 @@ func (tun *TunAdapter) setup(ifname string, addr string, mtu uint64) error {
if guid, err = windows.GUIDFromString("{8f59971a-7872-4aa6-b2eb-061fc4e9d0a7}"); err != nil { if guid, err = windows.GUIDFromString("{8f59971a-7872-4aa6-b2eb-061fc4e9d0a7}"); err != nil {
return err return err
} }
if iface, err = wgtun.CreateTUNWithRequestedGUID(ifname, &guid, int(mtu)); err != nil { tun.log.Printf("Creating TUN")
iface, err = wgtun.CreateTUNWithRequestedGUID(ifname, &guid, int(mtu))
if err != nil {
// Very rare condition, it will purge the old device and create new
tun.log.Printf("Error creatung TUN: '%s'", err)
wintun.Uninstall()
time.Sleep(3 * time.Second)
tun.log.Printf("Trying again")
iface, err = wgtun.CreateTUNWithRequestedGUID(ifname, &guid, int(mtu))
if err != nil {
return err return err
} }
if !waitForTUNUp(iface.Events()) {
return fmt.Errorf("TUN did not come up in time")
} }
tun.log.Printf("Waiting for TUN to come up")
time.Sleep(1 * time.Second)
tun.iface = iface tun.iface = iface
if addr != "" { if addr != "" {
tun.log.Printf("Setting up address")
if err = tun.setupAddress(addr); err != nil { if err = tun.setupAddress(addr); err != nil {
tun.log.Errorln("Failed to set up TUN address:", err) tun.log.Errorln("Failed to set up TUN address:", err)
return err return err
@ -51,6 +63,7 @@ func (tun *TunAdapter) setup(ifname string, addr string, mtu uint64) error {
if mtu, err := iface.MTU(); err == nil { if mtu, err := iface.MTU(); err == nil {
tun.mtu = uint64(mtu) tun.mtu = uint64(mtu)
} }
tun.log.Printf("TUN is set up successfully")
return nil return nil
}) })
} }