mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-21 09:36:34 -05:00
udp
This commit is contained in:
parent
746580d566
commit
ebb4f5b7bd
@ -23,26 +23,18 @@ func (request *Socks5UDPRequest) Destination() v2net.Destination {
|
||||
return v2net.NewUDPDestination(request.Address)
|
||||
}
|
||||
|
||||
func (request *Socks5UDPRequest) Bytes(buffer []byte) []byte {
|
||||
if buffer == nil {
|
||||
buffer = make([]byte, 0, 2*1024)
|
||||
}
|
||||
buffer = append(buffer, 0, 0, request.Fragment)
|
||||
func (request *Socks5UDPRequest) Write(buffer *alloc.Buffer) {
|
||||
buffer.AppendBytes(0, 0, request.Fragment)
|
||||
switch {
|
||||
case request.Address.IsIPv4():
|
||||
buffer = append(buffer, AddrTypeIPv4)
|
||||
buffer = append(buffer, request.Address.IP()...)
|
||||
buffer.AppendBytes(AddrTypeIPv4).Append(request.Address.IP())
|
||||
case request.Address.IsIPv6():
|
||||
buffer = append(buffer, AddrTypeIPv6)
|
||||
buffer = append(buffer, request.Address.IP()...)
|
||||
buffer.AppendBytes(AddrTypeIPv6).Append(request.Address.IP())
|
||||
case request.Address.IsDomain():
|
||||
buffer = append(buffer, AddrTypeDomain)
|
||||
buffer = append(buffer, byte(len(request.Address.Domain())))
|
||||
buffer = append(buffer, []byte(request.Address.Domain())...)
|
||||
buffer.AppendBytes(AddrTypeDomain, byte(len(request.Address.Domain()))).Append([]byte(request.Address.Domain()))
|
||||
}
|
||||
buffer = append(buffer, request.Address.PortBytes()...)
|
||||
buffer = append(buffer, request.Data.Value...)
|
||||
return buffer
|
||||
buffer.Append(request.Address.PortBytes())
|
||||
buffer.Append(request.Data.Value)
|
||||
}
|
||||
|
||||
func ReadUDPRequest(packet []byte) (request Socks5UDPRequest, err error) {
|
||||
|
@ -46,9 +46,8 @@ func (server *SocksServer) AcceptPackets(conn *net.UDPConn) error {
|
||||
buffer.Release()
|
||||
continue
|
||||
}
|
||||
buffer.Slice(0, nBytes)
|
||||
log.Info("Client UDP connection from %v", addr)
|
||||
request, err := protocol.ReadUDPRequest(buffer.Value)
|
||||
request, err := protocol.ReadUDPRequest(buffer.Value[:nBytes])
|
||||
buffer.Release()
|
||||
if err != nil {
|
||||
log.Error("Socks failed to parse UDP request: %v", err)
|
||||
@ -79,8 +78,12 @@ func (server *SocksServer) handlePacket(conn *net.UDPConn, packet v2net.Packet,
|
||||
Data: data,
|
||||
}
|
||||
log.Info("Writing back UDP response with %d bytes from %s to %s", data.Len(), targetAddr.String(), clientAddr.String())
|
||||
udpMessage := response.Bytes(nil)
|
||||
nBytes, err := conn.WriteToUDP(udpMessage, clientAddr)
|
||||
|
||||
udpMessage := alloc.NewSmallBuffer().Clear()
|
||||
response.Write(udpMessage)
|
||||
|
||||
nBytes, err := conn.WriteToUDP(udpMessage.Value, clientAddr)
|
||||
udpMessage.Release()
|
||||
response.Data.Release()
|
||||
if err != nil {
|
||||
log.Error("Socks failed to write UDP message (%d bytes) to %s: %v", nBytes, clientAddr.String(), err)
|
||||
|
Loading…
Reference in New Issue
Block a user