fix: better wireguard error handle
This commit is contained in:
parent
e9333f4586
commit
538743555e
1 changed files with 11 additions and 4 deletions
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue