github.com/xraypb/Xray-core@v1.8.1/transport/internet/udp/hub_darwin.go (about) 1 //go:build darwin 2 // +build darwin 3 4 package udp 5 6 import ( 7 "bytes" 8 "encoding/gob" 9 "io" 10 11 "github.com/xraypb/Xray-core/common/errors" 12 "github.com/xraypb/Xray-core/common/net" 13 "github.com/xraypb/Xray-core/transport/internet" 14 ) 15 16 // RetrieveOriginalDest from stored laddr, caddr 17 func RetrieveOriginalDest(oob []byte) net.Destination { 18 dec := gob.NewDecoder(bytes.NewBuffer(oob)) 19 var la, ra net.UDPAddr 20 dec.Decode(&la) 21 dec.Decode(&ra) 22 ip, port, err := internet.OriginalDst(&la, &ra) 23 if err != nil { 24 return net.Destination{} 25 } 26 return net.UDPDestination(net.IPAddress(ip), net.Port(port)) 27 } 28 29 // ReadUDPMsg stores laddr, caddr for later use 30 func ReadUDPMsg(conn *net.UDPConn, payload []byte, oob []byte) (int, int, int, *net.UDPAddr, error) { 31 nBytes, addr, err := conn.ReadFromUDP(payload) 32 var buf bytes.Buffer 33 enc := gob.NewEncoder(&buf) 34 udpAddr, ok := conn.LocalAddr().(*net.UDPAddr) 35 if !ok { 36 return 0, 0, 0, nil, errors.New("invalid local address") 37 } 38 if addr == nil { 39 return 0, 0, 0, nil, errors.New("invalid remote address") 40 } 41 enc.Encode(udpAddr) 42 enc.Encode(addr) 43 var reader io.Reader = &buf 44 noob, _ := reader.Read(oob) 45 return nBytes, noob, 0, addr, err 46 }