fix: better wireguard error handle

This commit is contained in:
gVisor bot 2022-11-18 19:40:39 +08:00
parent e9333f4586
commit 538743555e

View file

@ -36,6 +36,7 @@ type WireGuard struct {
tunDevice wireguard.Device tunDevice wireguard.Device
dialer *wgDialer dialer *wgDialer
startOnce sync.Once startOnce sync.Once
startErr error
} }
type WireGuardOption struct { type WireGuardOption struct {
@ -198,10 +199,10 @@ func (w *WireGuard) DialContext(ctx context.Context, metadata *C.Metadata, opts
w.dialer.options = opts w.dialer.options = opts
var conn net.Conn var conn net.Conn
w.startOnce.Do(func() { w.startOnce.Do(func() {
err = w.tunDevice.Start() w.startErr = w.tunDevice.Start()
}) })
if err != nil { if w.startErr != nil {
return nil, err return nil, w.startErr
} }
if !metadata.Resolved() { if !metadata.Resolved() {
var addrs []netip.Addr var addrs []netip.Addr
@ -226,8 +227,11 @@ func (w *WireGuard) ListenPacketContext(ctx context.Context, metadata *C.Metadat
w.dialer.options = opts w.dialer.options = opts
var pc net.PacketConn var pc net.PacketConn
w.startOnce.Do(func() { w.startOnce.Do(func() {
err = w.tunDevice.Start() w.startErr = w.tunDevice.Start()
}) })
if w.startErr != nil {
return nil, w.startErr
}
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -242,6 +246,9 @@ func (w *WireGuard) ListenPacketContext(ctx context.Context, metadata *C.Metadat
if err != nil { if err != nil {
return nil, err return nil, err
} }
if pc == nil {
return nil, E.New("packetConn is nil")
}
return newPacketConn(&wgPacketConn{pc, w}, w), nil return newPacketConn(&wgPacketConn{pc, w}, w), nil
} }