From 0ad629ca31c3cb9a783d7611bf799534e6af32d5 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Thu, 8 Dec 2016 11:21:24 +0100 Subject: [PATCH] remove use of prepend --- app/dispatcher/testing/dispatcher.go | 8 ++++++-- proxy/shadowsocks/ota.go | 10 +++++++--- proxy/shadowsocks/ota_test.go | 3 --- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/app/dispatcher/testing/dispatcher.go b/app/dispatcher/testing/dispatcher.go index 4f853c3d3..92fd3ef5c 100644 --- a/app/dispatcher/testing/dispatcher.go +++ b/app/dispatcher/testing/dispatcher.go @@ -1,6 +1,7 @@ package testing import ( + "v2ray.com/core/common/alloc" v2net "v2ray.com/core/common/net" "v2ray.com/core/proxy" "v2ray.com/core/transport/ray" @@ -19,8 +20,11 @@ func NewTestPacketDispatcher(handler func(destination v2net.Destination, traffic if err != nil { break } - payload.Prepend([]byte("Processed: ")) - traffic.OutboundOutput().Write(payload) + output := alloc.NewBuffer() + output.Append([]byte("Processed: ")) + output.Append(payload.Bytes()) + payload.Release() + traffic.OutboundOutput().Write(output) } traffic.OutboundOutput().Close() } diff --git a/proxy/shadowsocks/ota.go b/proxy/shadowsocks/ota.go index e3b2e6cbe..b8e799cbc 100644 --- a/proxy/shadowsocks/ota.go +++ b/proxy/shadowsocks/ota.go @@ -5,6 +5,7 @@ import ( "crypto/hmac" "crypto/sha1" "io" + "v2ray.com/core/common/alloc" "v2ray.com/core/common/errors" "v2ray.com/core/common/serial" @@ -111,12 +112,14 @@ func (v *ChunkReader) Read() (*alloc.Buffer, error) { type ChunkWriter struct { writer io.Writer auth *Authenticator + buffer []byte } func NewChunkWriter(writer io.Writer, auth *Authenticator) *ChunkWriter { return &ChunkWriter{ writer: writer, auth: auth, + buffer: make([]byte, 32*1024), } } @@ -127,8 +130,9 @@ func (v *ChunkWriter) Release() { func (v *ChunkWriter) Write(payload *alloc.Buffer) error { totalLength := payload.Len() - payload.PrependFunc(AuthSize, v.auth.Authenticate(payload.Bytes())) - payload.PrependFunc(2, serial.WriteUint16(uint16(totalLength))) - _, err := v.writer.Write(payload.Bytes()) + serial.Uint16ToBytes(uint16(totalLength), v.buffer[:0]) + v.auth.Authenticate(payload.Bytes())(v.buffer[2:]) + copy(v.buffer[2+AuthSize:], payload.Bytes()) + _, err := v.writer.Write(v.buffer[:2+AuthSize+payload.Len()]) return err } diff --git a/proxy/shadowsocks/ota_test.go b/proxy/shadowsocks/ota_test.go index e5d001574..d436082d2 100644 --- a/proxy/shadowsocks/ota_test.go +++ b/proxy/shadowsocks/ota_test.go @@ -19,9 +19,6 @@ func TestNormalChunkReading(t *testing.T) { payload, err := reader.Read() assert.Error(err).IsNil() assert.Bytes(payload.Bytes()).Equals([]byte{11, 12, 13, 14, 15, 16, 17, 18}) - - payload.PrependBytes(3, 4) - assert.Bytes(payload.Bytes()).Equals([]byte{3, 4, 11, 12, 13, 14, 15, 16, 17, 18}) } func TestNormalChunkWriting(t *testing.T) {