From d5b5d609562b428c07f868c1ff5c6ce49453c8ed Mon Sep 17 00:00:00 2001 From: _ksco Date: Sun, 9 Oct 2022 10:15:16 +0800 Subject: [PATCH] Fix(freebsd): ReadUDPMsg nil pointer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: ksco.he Co-authored-by: 秋のかえで --- transport/internet/udp/hub_freebsd.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/transport/internet/udp/hub_freebsd.go b/transport/internet/udp/hub_freebsd.go index f5f853c34..87924bf3f 100644 --- a/transport/internet/udp/hub_freebsd.go +++ b/transport/internet/udp/hub_freebsd.go @@ -8,6 +8,7 @@ import ( "encoding/gob" "io" + "github.com/v2fly/v2ray-core/v5/common/errors" "github.com/v2fly/v2ray-core/v5/common/net" "github.com/v2fly/v2ray-core/v5/transport/internet" ) @@ -28,11 +29,24 @@ func RetrieveOriginalDest(oob []byte) net.Destination { // ReadUDPMsg stores laddr, caddr for later use func ReadUDPMsg(conn *net.UDPConn, payload []byte, oob []byte) (int, int, int, *net.UDPAddr, error) { nBytes, addr, err := conn.ReadFromUDP(payload) + if err != nil { + return nBytes, 0, 0, addr, err + } + var buf bytes.Buffer enc := gob.NewEncoder(&buf) - enc.Encode(conn.LocalAddr().(*net.UDPAddr)) + localAddr, ok := conn.LocalAddr().(*net.UDPAddr) + if !ok { + return 0, 0, 0, nil, errors.New("invalid local address") + } + if addr == nil { + return 0, 0, 0, nil, errors.New("invalid remote address") + } + enc.Encode(localAddr) enc.Encode(addr) + var reader io.Reader = &buf noob, _ := reader.Read(oob) - return nBytes, noob, 0, addr, err + + return nBytes, noob, 0, addr, nil }