From 350a65a36b479457622cc3608a76481191102bae Mon Sep 17 00:00:00 2001 From: gVisor bot Date: Thu, 31 Dec 2020 18:58:03 +0800 Subject: [PATCH] Fix: support unspecified UDP bind address (#1159) --- adapters/outbound/socks5.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/adapters/outbound/socks5.go b/adapters/outbound/socks5.go index 78a59468..eb65d513 100644 --- a/adapters/outbound/socks5.go +++ b/adapters/outbound/socks5.go @@ -122,7 +122,21 @@ func (ss *Socks5) DialUDP(metadata *C.Metadata) (_ C.PacketConn, err error) { pc.Close() }() - return newPacketConn(&socksPacketConn{PacketConn: pc, rAddr: bindAddr.UDPAddr(), tcpConn: c}, ss), nil + // Support unspecified UDP bind address. + bindUDPAddr := bindAddr.UDPAddr() + if bindUDPAddr == nil { + err = errors.New("invalid UDP bind address") + return + } else if bindUDPAddr.IP.IsUnspecified() { + serverAddr, err := resolveUDPAddr("udp", ss.Addr()) + if err != nil { + return nil, err + } + + bindUDPAddr.IP = serverAddr.IP + } + + return newPacketConn(&socksPacketConn{PacketConn: pc, rAddr: bindUDPAddr, tcpConn: c}, ss), nil } func NewSocks5(option Socks5Option) *Socks5 {