mirror of https://github.com/v2ray/v2ray-core
fix buffer usage in VMess
parent
5df40c31f0
commit
27b521d2aa
|
@ -123,6 +123,7 @@ func (this *VMessInboundHandler) HandleConnection(connection *net.TCPConn) error
|
||||||
|
|
||||||
// Optimize for small response packet
|
// Optimize for small response packet
|
||||||
buffer := alloc.NewLargeBuffer().Clear()
|
buffer := alloc.NewLargeBuffer().Clear()
|
||||||
|
defer buffer.Release()
|
||||||
buffer.AppendBytes(request.ResponseHeader[0] ^ request.ResponseHeader[1])
|
buffer.AppendBytes(request.ResponseHeader[0] ^ request.ResponseHeader[1])
|
||||||
buffer.AppendBytes(request.ResponseHeader[2] ^ request.ResponseHeader[3])
|
buffer.AppendBytes(request.ResponseHeader[2] ^ request.ResponseHeader[3])
|
||||||
buffer.AppendBytes(byte(0), byte(0))
|
buffer.AppendBytes(byte(0), byte(0))
|
||||||
|
@ -131,7 +132,6 @@ func (this *VMessInboundHandler) HandleConnection(connection *net.TCPConn) error
|
||||||
buffer.Append(data.Value)
|
buffer.Append(data.Value)
|
||||||
data.Release()
|
data.Release()
|
||||||
responseWriter.Write(buffer.Value)
|
responseWriter.Write(buffer.Value)
|
||||||
buffer.Release()
|
|
||||||
go handleOutput(request, responseWriter, output, &writeFinish)
|
go handleOutput(request, responseWriter, output, &writeFinish)
|
||||||
writeFinish.Lock()
|
writeFinish.Lock()
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,7 +39,7 @@ func (this *VMessOutboundHandler) Dispatch(firstPacket v2net.Packet, ray ray.Out
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer := alloc.NewSmallBuffer()
|
buffer := alloc.NewSmallBuffer()
|
||||||
defer buffer.Release()
|
defer buffer.Release() // Buffer is released after communication finishes.
|
||||||
v2net.ReadAllBytes(rand.Reader, buffer.Value[:36]) // 16 + 16 + 4
|
v2net.ReadAllBytes(rand.Reader, buffer.Value[:36]) // 16 + 16 + 4
|
||||||
request.RequestIV = buffer.Value[:16]
|
request.RequestIV = buffer.Value[:16]
|
||||||
request.RequestKey = buffer.Value[16:32]
|
request.RequestKey = buffer.Value[16:32]
|
||||||
|
@ -185,17 +185,6 @@ func handleResponse(conn net.Conn, request *protocol.VMessRequest, output chan<-
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
type VMessOutboundHandlerFactory struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *VMessOutboundHandlerFactory) Create(space app.Space, rawConfig interface{}) (proxy.OutboundConnectionHandler, error) {
|
|
||||||
vOutConfig := rawConfig.(Config)
|
|
||||||
return &VMessOutboundHandler{
|
|
||||||
space: space,
|
|
||||||
receiverManager: NewReceiverManager(vOutConfig.Receivers()),
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
if err := internal.RegisterOutboundConnectionHandlerFactory("vmess", func(space app.Space, rawConfig interface{}) (proxy.OutboundConnectionHandler, error) {
|
if err := internal.RegisterOutboundConnectionHandlerFactory("vmess", func(space app.Space, rawConfig interface{}) (proxy.OutboundConnectionHandler, error) {
|
||||||
vOutConfig := rawConfig.(Config)
|
vOutConfig := rawConfig.(Config)
|
||||||
|
|
|
@ -68,6 +68,7 @@ func NewVMessRequestReader(vUserSet user.UserSet) *VMessRequestReader {
|
||||||
// Read reads a VMessRequest from a byte stream.
|
// Read reads a VMessRequest from a byte stream.
|
||||||
func (this *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
|
func (this *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
|
||||||
buffer := alloc.NewSmallBuffer()
|
buffer := alloc.NewSmallBuffer()
|
||||||
|
defer buffer.Release()
|
||||||
|
|
||||||
nBytes, err := v2net.ReadAllBytes(reader, buffer.Value[:vmess.IDBytesLen])
|
nBytes, err := v2net.ReadAllBytes(reader, buffer.Value[:vmess.IDBytesLen])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -102,9 +103,9 @@ func (this *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
|
||||||
return nil, proxy.InvalidProtocolVersion
|
return nil, proxy.InvalidProtocolVersion
|
||||||
}
|
}
|
||||||
|
|
||||||
request.RequestIV = buffer.Value[1:17] // 16 bytes
|
request.RequestIV = append([]byte(nil), buffer.Value[1:17]...) // 16 bytes
|
||||||
request.RequestKey = buffer.Value[17:33] // 16 bytes
|
request.RequestKey = append([]byte(nil), buffer.Value[17:33]...) // 16 bytes
|
||||||
request.ResponseHeader = buffer.Value[33:37] // 4 bytes
|
request.ResponseHeader = append([]byte(nil), buffer.Value[33:37]...) // 4 bytes
|
||||||
request.Command = buffer.Value[37]
|
request.Command = buffer.Value[37]
|
||||||
|
|
||||||
request.Port = v2net.PortFromBytes(buffer.Value[38:40])
|
request.Port = v2net.PortFromBytes(buffer.Value[38:40])
|
||||||
|
@ -135,7 +136,8 @@ func (this *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
bufferLen += 1 + domainLength
|
bufferLen += 1 + domainLength
|
||||||
request.Address = v2net.DomainAddress(string(buffer.Value[42 : 42+domainLength]))
|
domainBytes := append([]byte(nil), buffer.Value[42 : 42+domainLength]...)
|
||||||
|
request.Address = v2net.DomainAddress(string(domainBytes))
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = v2net.ReadAllBytes(decryptor, buffer.Value[bufferLen:bufferLen+4])
|
_, err = v2net.ReadAllBytes(decryptor, buffer.Value[bufferLen:bufferLen+4])
|
||||||
|
|
Loading…
Reference in New Issue