From bf35298289be3129266ee00e1ff6294c386bf755 Mon Sep 17 00:00:00 2001 From: xjasonlyu Date: Mon, 7 Feb 2022 17:31:28 +0800 Subject: [PATCH] Fix: suppress panic when wintun.dll not found --- core/device/tun/tun_wireguard.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/core/device/tun/tun_wireguard.go b/core/device/tun/tun_wireguard.go index 762fff5..33691e2 100644 --- a/core/device/tun/tun_wireguard.go +++ b/core/device/tun/tun_wireguard.go @@ -21,7 +21,13 @@ type TUN struct { offset int } -func Open(name string, mtu uint32) (device.Device, error) { +func Open(name string, mtu uint32) (_ device.Device, err error) { + defer func() { + if r := recover(); r != nil { + err = fmt.Errorf("open tun: %v", r) + } + }() + var ( offset = 4 /* 4 bytes TUN_PI */ defaultMTU = 1500 @@ -44,11 +50,11 @@ func Open(name string, mtu uint32) (device.Device, error) { } t.nt = nt.(*tun.NativeTun) - _mtu, err := nt.MTU() + tunMTU, err := nt.MTU() if err != nil { return nil, fmt.Errorf("get mtu: %w", err) } - t.mtu = uint32(_mtu) + t.mtu = uint32(tunMTU) ep, err := iobased.New(t, t.mtu, offset) if err != nil {