1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-02 15:36:41 -05:00
This commit is contained in:
V2Ray 2015-10-08 23:28:51 +02:00
parent 746580d566
commit ebb4f5b7bd
2 changed files with 14 additions and 19 deletions

View File

@ -23,26 +23,18 @@ func (request *Socks5UDPRequest) Destination() v2net.Destination {
return v2net.NewUDPDestination(request.Address) return v2net.NewUDPDestination(request.Address)
} }
func (request *Socks5UDPRequest) Bytes(buffer []byte) []byte { func (request *Socks5UDPRequest) Write(buffer *alloc.Buffer) {
if buffer == nil { buffer.AppendBytes(0, 0, request.Fragment)
buffer = make([]byte, 0, 2*1024)
}
buffer = append(buffer, 0, 0, request.Fragment)
switch { switch {
case request.Address.IsIPv4(): case request.Address.IsIPv4():
buffer = append(buffer, AddrTypeIPv4) buffer.AppendBytes(AddrTypeIPv4).Append(request.Address.IP())
buffer = append(buffer, request.Address.IP()...)
case request.Address.IsIPv6(): case request.Address.IsIPv6():
buffer = append(buffer, AddrTypeIPv6) buffer.AppendBytes(AddrTypeIPv6).Append(request.Address.IP())
buffer = append(buffer, request.Address.IP()...)
case request.Address.IsDomain(): case request.Address.IsDomain():
buffer = append(buffer, AddrTypeDomain) buffer.AppendBytes(AddrTypeDomain, byte(len(request.Address.Domain()))).Append([]byte(request.Address.Domain()))
buffer = append(buffer, byte(len(request.Address.Domain())))
buffer = append(buffer, []byte(request.Address.Domain())...)
} }
buffer = append(buffer, request.Address.PortBytes()...) buffer.Append(request.Address.PortBytes())
buffer = append(buffer, request.Data.Value...) buffer.Append(request.Data.Value)
return buffer
} }
func ReadUDPRequest(packet []byte) (request Socks5UDPRequest, err error) { func ReadUDPRequest(packet []byte) (request Socks5UDPRequest, err error) {

View File

@ -46,9 +46,8 @@ func (server *SocksServer) AcceptPackets(conn *net.UDPConn) error {
buffer.Release() buffer.Release()
continue continue
} }
buffer.Slice(0, nBytes)
log.Info("Client UDP connection from %v", addr) log.Info("Client UDP connection from %v", addr)
request, err := protocol.ReadUDPRequest(buffer.Value) request, err := protocol.ReadUDPRequest(buffer.Value[:nBytes])
buffer.Release() buffer.Release()
if err != nil { if err != nil {
log.Error("Socks failed to parse UDP request: %v", err) 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, Data: data,
} }
log.Info("Writing back UDP response with %d bytes from %s to %s", data.Len(), targetAddr.String(), clientAddr.String()) 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() response.Data.Release()
if err != nil { if err != nil {
log.Error("Socks failed to write UDP message (%d bytes) to %s: %v", nBytes, clientAddr.String(), err) log.Error("Socks failed to write UDP message (%d bytes) to %s: %v", nBytes, clientAddr.String(), err)