From 426291030ac2be2c744aa2c2fb97226975ea7b4d Mon Sep 17 00:00:00 2001 From: mzz2017 Date: Wed, 16 Dec 2020 15:46:54 +0800 Subject: [PATCH] fix(socks5): should not dial returned bind addr directly When server returns an any ip (0.0.0.0 or [::0]), we should use conventional ip to replace the any ip given (0.0.0.0 or [::0]). This behaviour adapts to most situations. See v2fly/v2ray-core#523 --- proxy/socks5/client.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/proxy/socks5/client.go b/proxy/socks5/client.go index e79a07d..9df8c74 100644 --- a/proxy/socks5/client.go +++ b/proxy/socks5/client.go @@ -86,7 +86,17 @@ func (s *Socks5) DialUDP(network, addr string) (pc net.PacketConn, writeTo net.A return nil, nil, err } - pc, nextHop, err := s.dialer.DialUDP(network, uAddr.String()) + var uAddress string + h, p, _ := net.SplitHostPort(uAddr.String()) + // if returned bind ip is unspecified + if ip := net.ParseIP(h); ip != nil && ip.IsUnspecified() { + // indicate using conventional addr + uAddress = net.JoinHostPort(s.addr, p) + } else { + uAddress = uAddr.String() + } + + pc, nextHop, err := s.dialer.DialUDP(network, uAddress) if err != nil { log.F("[socks5] dialudp to %s error: %s", uAddr.String(), err) return nil, nil, err