From 5b20d960f1622ed10f85bc6e6ad2cb63d7a7af5f Mon Sep 17 00:00:00 2001 From: Shelikhoo Date: Fri, 31 Dec 2021 13:16:11 +0000 Subject: [PATCH] socks5 UDP outbound reader writer --- proxy/socks/protocol.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/proxy/socks/protocol.go b/proxy/socks/protocol.go index 33eb89516..4bfdbef5d 100644 --- a/proxy/socks/protocol.go +++ b/proxy/socks/protocol.go @@ -3,6 +3,7 @@ package socks import ( "encoding/binary" "io" + gonet "net" "github.com/v2fly/v2ray-core/v5/common" "github.com/v2fly/v2ray-core/v5/common/buf" @@ -389,6 +390,23 @@ func (r *UDPReader) ReadMultiBuffer() (buf.MultiBuffer, error) { return buf.MultiBuffer{b}, nil } +func (v *UDPReader) ReadFrom(p []byte) (n int, addr gonet.Addr, err error) { + buffer := buf.New() + _, err = buffer.ReadFrom(v.reader) + if err != nil { + buffer.Release() + return 0, nil, err + } + req, err := DecodeUDPPacket(buffer) + if err != nil { + buffer.Release() + return 0, nil, err + } + n = copy(p, buffer.Bytes()) + buffer.Release() + return n, &gonet.UDPAddr{IP: req.Address.IP(), Port: int(req.Port)}, nil +} + type UDPWriter struct { request *protocol.RequestHeader writer io.Writer @@ -414,6 +432,21 @@ func (w *UDPWriter) Write(b []byte) (int, error) { return len(b), nil } +func (w *UDPWriter) WriteTo(payload []byte, addr gonet.Addr) (n int, err error) { + request := *w.request + udpAddr := addr.(*gonet.UDPAddr) + request.Command = protocol.RequestCommandUDP + request.Address = net.IPAddress(udpAddr.IP) + request.Port = net.Port(udpAddr.Port) + packet, err := EncodeUDPPacket(&request, payload) + if err != nil { + return 0, err + } + _, err = w.writer.Write(packet.Bytes()) + packet.Release() + return len(payload), err +} + func ClientHandshake(request *protocol.RequestHeader, reader io.Reader, writer io.Writer) (*protocol.RequestHeader, error) { authByte := byte(authNotRequired) if request.User != nil {