mirror of
https://github.com/v2fly/v2ray-core.git
synced 2026-01-06 09:15:37 -05:00
Maintain an internal buffer pool to accelerate allocation
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"sync"
|
||||
|
||||
"github.com/v2ray/v2ray-core"
|
||||
"github.com/v2ray/v2ray-core/common/alloc"
|
||||
"github.com/v2ray/v2ray-core/common/log"
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
)
|
||||
@@ -33,7 +34,9 @@ func (vconn *FreedomConnection) Dispatch(firstPacket v2net.Packet, ray core.Outb
|
||||
writeMutex.Lock()
|
||||
|
||||
if chunk := firstPacket.Chunk(); chunk != nil {
|
||||
conn.Write(chunk)
|
||||
conn.Write(chunk.Value)
|
||||
chunk.Release()
|
||||
chunk = nil
|
||||
}
|
||||
|
||||
if !firstPacket.MoreChunks() {
|
||||
@@ -56,23 +59,22 @@ func (vconn *FreedomConnection) Dispatch(firstPacket v2net.Packet, ray core.Outb
|
||||
return nil
|
||||
}
|
||||
|
||||
func dumpInput(conn net.Conn, input <-chan []byte, finish *sync.Mutex) {
|
||||
func dumpInput(conn net.Conn, input <-chan *alloc.Buffer, finish *sync.Mutex) {
|
||||
v2net.ChanToWriter(conn, input)
|
||||
finish.Unlock()
|
||||
}
|
||||
|
||||
func dumpOutput(conn net.Conn, output chan<- []byte, finish *sync.Mutex, udp bool) {
|
||||
func dumpOutput(conn net.Conn, output chan<- *alloc.Buffer, finish *sync.Mutex, udp bool) {
|
||||
defer finish.Unlock()
|
||||
defer close(output)
|
||||
|
||||
bufferSize := 4 /* KB */
|
||||
if udp {
|
||||
bufferSize = 2
|
||||
}
|
||||
response, err := v2net.ReadFrom(conn, bufferSize)
|
||||
log.Info("Freedom receives %d bytes from %s", len(response), conn.RemoteAddr().String())
|
||||
if len(response) > 0 {
|
||||
response, err := v2net.ReadFrom(conn, nil)
|
||||
log.Info("Freedom receives %d bytes from %s", response.Len(), conn.RemoteAddr().String())
|
||||
if response.Len() > 0 {
|
||||
output <- response
|
||||
} else {
|
||||
response.Release()
|
||||
response = nil
|
||||
}
|
||||
if err != nil {
|
||||
return
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"golang.org/x/net/proxy"
|
||||
|
||||
"github.com/v2ray/v2ray-core"
|
||||
"github.com/v2ray/v2ray-core/common/alloc"
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
_ "github.com/v2ray/v2ray-core/proxy/socks"
|
||||
"github.com/v2ray/v2ray-core/proxy/socks/config/json"
|
||||
@@ -62,8 +63,11 @@ func TestUDPSend(t *testing.T) {
|
||||
err = point.Start()
|
||||
assert.Error(err).IsNil()
|
||||
|
||||
data2SendBuffer := alloc.NewBuffer()
|
||||
data2SendBuffer.Clear()
|
||||
data2SendBuffer.Append([]byte(data2Send))
|
||||
dest := v2net.NewUDPDestination(udpServerAddr)
|
||||
ich.Communicate(v2net.NewPacket(dest, []byte(data2Send), false))
|
||||
ich.Communicate(v2net.NewPacket(dest, data2SendBuffer, false))
|
||||
assert.Bytes(ich.DataReturned.Bytes()).Equals([]byte("Processed: Data to be sent to remote"))
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
|
||||
"github.com/v2ray/v2ray-core/common/alloc"
|
||||
"github.com/v2ray/v2ray-core/common/log"
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
)
|
||||
@@ -15,7 +16,7 @@ var (
|
||||
type Socks5UDPRequest struct {
|
||||
Fragment byte
|
||||
Address v2net.Address
|
||||
Data []byte
|
||||
Data *alloc.Buffer
|
||||
}
|
||||
|
||||
func (request *Socks5UDPRequest) Destination() v2net.Destination {
|
||||
@@ -40,7 +41,7 @@ func (request *Socks5UDPRequest) Bytes(buffer []byte) []byte {
|
||||
buffer = append(buffer, []byte(request.Address.Domain())...)
|
||||
}
|
||||
buffer = append(buffer, request.Address.PortBytes()...)
|
||||
buffer = append(buffer, request.Data...)
|
||||
buffer = append(buffer, request.Data.Value...)
|
||||
return buffer
|
||||
}
|
||||
|
||||
@@ -74,8 +75,9 @@ func ReadUDPRequest(packet []byte) (request Socks5UDPRequest, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
request.Data = make([]byte, len(packet)-dataBegin)
|
||||
copy(request.Data, packet[dataBegin:])
|
||||
request.Data = alloc.NewBuffer()
|
||||
request.Data.Clear()
|
||||
request.Data.Append(packet[dataBegin:])
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/v2ray/v2ray-core"
|
||||
"github.com/v2ray/v2ray-core/common/alloc"
|
||||
"github.com/v2ray/v2ray-core/common/errors"
|
||||
"github.com/v2ray/v2ray-core/common/log"
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
@@ -158,7 +159,7 @@ func (server *SocksServer) handleSocks5(reader *v2net.TimeOutReader, writer io.W
|
||||
}
|
||||
|
||||
dest := request.Destination()
|
||||
data, err := v2net.ReadFrom(reader, 4)
|
||||
data, err := v2net.ReadFrom(reader, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -192,8 +193,8 @@ func (server *SocksServer) handleUDP(reader *v2net.TimeOutReader, writer io.Writ
|
||||
return err
|
||||
}
|
||||
|
||||
reader.SetTimeOut(300) /* 5 minutes */
|
||||
v2net.ReadFrom(reader, 1) // Just in case of anything left in the socket
|
||||
reader.SetTimeOut(300) /* 5 minutes */
|
||||
v2net.ReadFrom(reader, nil) // Just in case of anything left in the socket
|
||||
// The TCP connection closes after this method returns. We need to wait until
|
||||
// the client closes it.
|
||||
// TODO: get notified from UDP part
|
||||
@@ -215,7 +216,7 @@ func (server *SocksServer) handleSocks4(reader io.Reader, writer io.Writer, auth
|
||||
}
|
||||
|
||||
dest := v2net.NewTCPDestination(v2net.IPAddress(auth.IP[:], auth.Port))
|
||||
data, err := v2net.ReadFrom(reader, 4)
|
||||
data, err := v2net.ReadFrom(reader, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -239,13 +240,13 @@ func (server *SocksServer) transport(reader io.Reader, writer io.Writer, firstPa
|
||||
outputFinish.Lock()
|
||||
}
|
||||
|
||||
func dumpInput(reader io.Reader, input chan<- []byte, finish *sync.Mutex) {
|
||||
func dumpInput(reader io.Reader, input chan<- *alloc.Buffer, finish *sync.Mutex) {
|
||||
v2net.ReaderToChan(input, reader)
|
||||
finish.Unlock()
|
||||
close(input)
|
||||
}
|
||||
|
||||
func dumpOutput(writer io.Writer, output <-chan []byte, finish *sync.Mutex) {
|
||||
func dumpOutput(writer io.Writer, output <-chan *alloc.Buffer, finish *sync.Mutex) {
|
||||
v2net.ChanToWriter(writer, output)
|
||||
finish.Unlock()
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package socks
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/v2ray/v2ray-core/common/alloc"
|
||||
"github.com/v2ray/v2ray-core/common/log"
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
"github.com/v2ray/v2ray-core/proxy/socks/protocol"
|
||||
@@ -38,14 +39,16 @@ func (server *SocksServer) getUDPAddr() v2net.Address {
|
||||
|
||||
func (server *SocksServer) AcceptPackets(conn *net.UDPConn) error {
|
||||
for {
|
||||
buffer := make([]byte, bufferSize)
|
||||
nBytes, addr, err := conn.ReadFromUDP(buffer)
|
||||
buffer := alloc.NewBuffer()
|
||||
defer buffer.Release()
|
||||
nBytes, addr, err := conn.ReadFromUDP(buffer.Value)
|
||||
if err != nil {
|
||||
log.Error("Socks failed to read UDP packets: %v", err)
|
||||
continue
|
||||
}
|
||||
buffer.Slice(0, nBytes)
|
||||
log.Info("Client UDP connection from %v", addr)
|
||||
request, err := protocol.ReadUDPRequest(buffer[:nBytes])
|
||||
request, err := protocol.ReadUDPRequest(buffer.Value)
|
||||
if err != nil {
|
||||
log.Error("Socks failed to parse UDP request: %v", err)
|
||||
continue
|
||||
@@ -57,7 +60,7 @@ func (server *SocksServer) AcceptPackets(conn *net.UDPConn) error {
|
||||
}
|
||||
|
||||
udpPacket := v2net.NewPacket(request.Destination(), request.Data, false)
|
||||
log.Info("Send packet to %s with %d bytes", udpPacket.Destination().String(), len(request.Data))
|
||||
log.Info("Send packet to %s with %d bytes", udpPacket.Destination().String(), request.Data.Len())
|
||||
go server.handlePacket(conn, udpPacket, addr, request.Address)
|
||||
}
|
||||
}
|
||||
@@ -72,9 +75,11 @@ func (server *SocksServer) handlePacket(conn *net.UDPConn, packet v2net.Packet,
|
||||
Address: targetAddr,
|
||||
Data: data,
|
||||
}
|
||||
log.Info("Writing back UDP response with %d bytes from %s to %s", len(data), 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)
|
||||
response.Data.Release()
|
||||
response.Data = nil
|
||||
if err != nil {
|
||||
log.Error("Socks failed to write UDP message (%d bytes) to %s: %v", nBytes, clientAddr.String(), err)
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/v2ray/v2ray-core"
|
||||
"github.com/v2ray/v2ray-core/common/alloc"
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
"github.com/v2ray/v2ray-core/testing/mocks"
|
||||
"github.com/v2ray/v2ray-core/testing/unit"
|
||||
@@ -163,8 +164,11 @@ func TestVMessInAndOutUDP(t *testing.T) {
|
||||
err = pointB.Start()
|
||||
assert.Error(err).IsNil()
|
||||
|
||||
data2SendBuffer := alloc.NewBuffer()
|
||||
data2SendBuffer.Clear()
|
||||
data2SendBuffer.Append([]byte(data2Send))
|
||||
dest := v2net.NewUDPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}, 80))
|
||||
ich.Communicate(v2net.NewPacket(dest, []byte(data2Send), false))
|
||||
ich.Communicate(v2net.NewPacket(dest, data2SendBuffer, false))
|
||||
assert.Bytes([]byte(data2Send)).Equals(och.Data2Send.Bytes())
|
||||
assert.Bytes(ich.DataReturned.Bytes()).Equals(och.Data2Return)
|
||||
}
|
||||
|
||||
@@ -5,9 +5,9 @@ import (
|
||||
"io"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/v2ray/v2ray-core"
|
||||
"github.com/v2ray/v2ray-core/common/alloc"
|
||||
v2io "github.com/v2ray/v2ray-core/common/io"
|
||||
"github.com/v2ray/v2ray-core/common/log"
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
@@ -15,14 +15,6 @@ import (
|
||||
"github.com/v2ray/v2ray-core/proxy/vmess/protocol/user"
|
||||
)
|
||||
|
||||
const (
|
||||
requestReadTimeOut = 4 * time.Second
|
||||
)
|
||||
|
||||
var (
|
||||
zeroTime time.Time
|
||||
)
|
||||
|
||||
type VMessInboundHandler struct {
|
||||
vPoint *core.Point
|
||||
clients user.UserSet
|
||||
@@ -103,7 +95,7 @@ func (handler *VMessInboundHandler) HandleConnection(connection *net.TCPConn) er
|
||||
buffer = append(buffer, request.ResponseHeader...)
|
||||
|
||||
if data, open := <-output; open {
|
||||
buffer = append(buffer, data...)
|
||||
buffer = append(buffer, data.Value...)
|
||||
data = nil
|
||||
responseWriter.Write(buffer)
|
||||
buffer = nil
|
||||
@@ -117,7 +109,7 @@ func (handler *VMessInboundHandler) HandleConnection(connection *net.TCPConn) er
|
||||
return nil
|
||||
}
|
||||
|
||||
func handleInput(request *protocol.VMessRequest, reader io.Reader, input chan<- []byte, finish *sync.Mutex) {
|
||||
func handleInput(request *protocol.VMessRequest, reader io.Reader, input chan<- *alloc.Buffer, finish *sync.Mutex) {
|
||||
defer close(input)
|
||||
defer finish.Unlock()
|
||||
|
||||
@@ -130,7 +122,7 @@ func handleInput(request *protocol.VMessRequest, reader io.Reader, input chan<-
|
||||
v2net.ReaderToChan(input, requestReader)
|
||||
}
|
||||
|
||||
func handleOutput(request *protocol.VMessRequest, writer io.Writer, output <-chan []byte, finish *sync.Mutex) {
|
||||
func handleOutput(request *protocol.VMessRequest, writer io.Writer, output <-chan *alloc.Buffer, finish *sync.Mutex) {
|
||||
v2net.ChanToWriter(writer, output)
|
||||
finish.Unlock()
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"crypto/md5"
|
||||
"net"
|
||||
|
||||
"github.com/v2ray/v2ray-core/common/alloc"
|
||||
v2io "github.com/v2ray/v2ray-core/common/io"
|
||||
"github.com/v2ray/v2ray-core/common/log"
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
@@ -55,14 +56,15 @@ func (handler *VMessInboundHandler) AcceptPackets(conn *net.UDPConn) {
|
||||
continue
|
||||
}
|
||||
|
||||
data := make([]byte, bufferSize)
|
||||
nBytes, err = cryptReader.Read(data)
|
||||
data := alloc.NewBuffer()
|
||||
nBytes, err = cryptReader.Read(data.Value)
|
||||
if err != nil {
|
||||
log.Warning("VMessIn: Unable to decrypt data: %v", err)
|
||||
continue
|
||||
}
|
||||
data.Slice(0, nBytes)
|
||||
|
||||
packet := v2net.NewPacket(request.Destination(), data[:nBytes], false)
|
||||
packet := v2net.NewPacket(request.Destination(), data, false)
|
||||
go handler.handlePacket(conn, request, packet, addr)
|
||||
}
|
||||
}
|
||||
@@ -87,7 +89,9 @@ func (handler *VMessInboundHandler) handlePacket(conn *net.UDPConn, request *pro
|
||||
|
||||
if data, ok := <-ray.InboundOutput(); ok {
|
||||
hasData = true
|
||||
responseWriter.Write(data)
|
||||
responseWriter.Write(data.Value)
|
||||
data.Release()
|
||||
data = nil
|
||||
}
|
||||
|
||||
if hasData {
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"sync"
|
||||
|
||||
"github.com/v2ray/v2ray-core"
|
||||
"github.com/v2ray/v2ray-core/common/alloc"
|
||||
v2io "github.com/v2ray/v2ray-core/common/io"
|
||||
"github.com/v2ray/v2ray-core/common/log"
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
@@ -121,7 +122,7 @@ func startCommunicate(request *protocol.VMessRequest, dest v2net.Destination, ra
|
||||
return nil
|
||||
}
|
||||
|
||||
func handleRequest(conn net.Conn, request *protocol.VMessRequest, firstPacket v2net.Packet, input <-chan []byte, finish *sync.Mutex) {
|
||||
func handleRequest(conn net.Conn, request *protocol.VMessRequest, firstPacket v2net.Packet, input <-chan *alloc.Buffer, finish *sync.Mutex) {
|
||||
defer finish.Unlock()
|
||||
encryptRequestWriter, err := v2io.NewAesEncryptWriter(request.RequestKey[:], request.RequestIV[:], conn)
|
||||
if err != nil {
|
||||
@@ -129,8 +130,9 @@ func handleRequest(conn net.Conn, request *protocol.VMessRequest, firstPacket v2
|
||||
return
|
||||
}
|
||||
|
||||
buffer := make([]byte, 0, 2*1024)
|
||||
buffer, err = request.ToBytes(user.NewTimeHash(user.HMACHash{}), user.GenerateRandomInt64InRange, buffer)
|
||||
buffer := alloc.NewBuffer()
|
||||
buffer.Clear()
|
||||
requestBytes, err := request.ToBytes(user.NewTimeHash(user.HMACHash{}), user.GenerateRandomInt64InRange, buffer.Value)
|
||||
if err != nil {
|
||||
log.Error("VMessOut: Failed to serialize VMess request: %v", err)
|
||||
return
|
||||
@@ -145,10 +147,14 @@ func handleRequest(conn net.Conn, request *protocol.VMessRequest, firstPacket v2
|
||||
}
|
||||
|
||||
if firstChunk != nil {
|
||||
encryptRequestWriter.Crypt(firstChunk)
|
||||
buffer = append(buffer, firstChunk...)
|
||||
encryptRequestWriter.Crypt(firstChunk.Value)
|
||||
requestBytes = append(requestBytes, firstChunk.Value...)
|
||||
firstChunk.Release()
|
||||
firstChunk = nil
|
||||
|
||||
_, err = conn.Write(buffer)
|
||||
_, err = conn.Write(requestBytes)
|
||||
buffer.Release()
|
||||
buffer = nil
|
||||
if err != nil {
|
||||
log.Error("VMessOut: Failed to write VMess request: %v", err)
|
||||
return
|
||||
@@ -161,7 +167,7 @@ func handleRequest(conn net.Conn, request *protocol.VMessRequest, firstPacket v2
|
||||
return
|
||||
}
|
||||
|
||||
func handleResponse(conn net.Conn, request *protocol.VMessRequest, output chan<- []byte, finish *sync.Mutex, isUDP bool) {
|
||||
func handleResponse(conn net.Conn, request *protocol.VMessRequest, output chan<- *alloc.Buffer, finish *sync.Mutex, isUDP bool) {
|
||||
defer finish.Unlock()
|
||||
defer close(output)
|
||||
responseKey := md5.Sum(request.RequestKey[:])
|
||||
@@ -173,18 +179,19 @@ func handleResponse(conn net.Conn, request *protocol.VMessRequest, output chan<-
|
||||
return
|
||||
}
|
||||
|
||||
buffer, err := v2net.ReadFrom(decryptResponseReader, 4)
|
||||
buffer, err := v2net.ReadFrom(decryptResponseReader, nil)
|
||||
if err != nil {
|
||||
log.Error("VMessOut: Failed to read VMess response (%d bytes): %v", len(buffer), err)
|
||||
log.Error("VMessOut: Failed to read VMess response (%d bytes): %v", buffer.Len(), err)
|
||||
return
|
||||
}
|
||||
if len(buffer) < 4 || !bytes.Equal(buffer[:4], request.ResponseHeader[:]) {
|
||||
if buffer.Len() < 4 || !bytes.Equal(buffer.Value[:4], request.ResponseHeader[:]) {
|
||||
log.Warning("VMessOut: unexepcted response header. The connection is probably hijacked.")
|
||||
return
|
||||
}
|
||||
log.Info("VMessOut received %d bytes from %s", len(buffer)-4, conn.RemoteAddr().String())
|
||||
log.Info("VMessOut received %d bytes from %s", buffer.Len()-4, conn.RemoteAddr().String())
|
||||
|
||||
output <- buffer[4:]
|
||||
buffer.SliceFrom(4)
|
||||
output <- buffer
|
||||
|
||||
if !isUDP {
|
||||
v2net.ReaderToChan(output, decryptResponseReader)
|
||||
|
||||
Reference in New Issue
Block a user