diff --git a/app/dispatcher/impl/default.go b/app/dispatcher/impl/default.go index ddf70e375..4f6c93136 100644 --- a/app/dispatcher/impl/default.go +++ b/app/dispatcher/impl/default.go @@ -4,7 +4,7 @@ import ( "v2ray.com/core/app" "v2ray.com/core/app/proxyman" "v2ray.com/core/app/router" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" "v2ray.com/core/common/errors" "v2ray.com/core/common/log" v2net "v2ray.com/core/common/net" @@ -62,7 +62,7 @@ func (v *DefaultDispatcher) DispatchToOutbound(session *proxy.SessionInfo) ray.I } if session.Inbound != nil && session.Inbound.AllowPassiveConnection { - go dispatcher.Dispatch(destination, alloc.NewLocalBuffer(32), direct) + go dispatcher.Dispatch(destination, buf.NewLocalBuffer(32), direct) } else { go v.FilterPacketAndDispatch(destination, direct, dispatcher) } diff --git a/app/dispatcher/testing/dispatcher.go b/app/dispatcher/testing/dispatcher.go index 92fd3ef5c..9ff73e6aa 100644 --- a/app/dispatcher/testing/dispatcher.go +++ b/app/dispatcher/testing/dispatcher.go @@ -1,7 +1,7 @@ package testing import ( - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" v2net "v2ray.com/core/common/net" "v2ray.com/core/proxy" "v2ray.com/core/transport/ray" @@ -20,7 +20,7 @@ func NewTestPacketDispatcher(handler func(destination v2net.Destination, traffic if err != nil { break } - output := alloc.NewBuffer() + output := buf.NewBuffer() output.Append([]byte("Processed: ")) output.Append(payload.Bytes()) payload.Release() diff --git a/app/dns/nameserver.go b/app/dns/nameserver.go index e2ecf1ee1..27be872b0 100644 --- a/app/dns/nameserver.go +++ b/app/dns/nameserver.go @@ -6,7 +6,7 @@ import ( "time" "v2ray.com/core/app/dispatcher" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" "v2ray.com/core/common/dice" "v2ray.com/core/common/log" v2net "v2ray.com/core/common/net" @@ -101,7 +101,7 @@ func (v *UDPNameServer) AssignUnusedID(response chan<- *ARecord) uint16 { } // Private: Visible for testing. -func (v *UDPNameServer) HandleResponse(dest v2net.Destination, payload *alloc.Buffer) { +func (v *UDPNameServer) HandleResponse(dest v2net.Destination, payload *buf.Buffer) { msg := new(dns.Msg) err := msg.Unpack(payload.Bytes()) if err != nil { @@ -144,8 +144,8 @@ func (v *UDPNameServer) HandleResponse(dest v2net.Destination, payload *alloc.Bu close(request.response) } -func (v *UDPNameServer) BuildQueryA(domain string, id uint16) *alloc.Buffer { - buffer := alloc.NewBuffer() +func (v *UDPNameServer) BuildQueryA(domain string, id uint16) *buf.Buffer { + buffer := buf.NewBuffer() msg := new(dns.Msg) msg.Id = id msg.RecursionDesired = true @@ -162,7 +162,7 @@ func (v *UDPNameServer) BuildQueryA(domain string, id uint16) *alloc.Buffer { return buffer } -func (v *UDPNameServer) DispatchQuery(payload *alloc.Buffer) { +func (v *UDPNameServer) DispatchQuery(payload *buf.Buffer) { v.udpServer.Dispatch(&proxy.SessionInfo{Source: pseudoDestination, Destination: v.address}, payload, v.HandleResponse) } diff --git a/common/alloc/buffer.go b/common/buf/buffer.go similarity index 99% rename from common/alloc/buffer.go rename to common/buf/buffer.go index c860340f7..dc53d25e8 100644 --- a/common/alloc/buffer.go +++ b/common/buf/buffer.go @@ -1,5 +1,5 @@ // Package alloc provides a light-weight memory allocation mechanism. -package alloc +package buf import ( "io" diff --git a/common/alloc/buffer_pool.go b/common/buf/buffer_pool.go similarity index 99% rename from common/alloc/buffer_pool.go rename to common/buf/buffer_pool.go index e305d72b1..ec13cf8e9 100644 --- a/common/alloc/buffer_pool.go +++ b/common/buf/buffer_pool.go @@ -1,4 +1,4 @@ -package alloc +package buf import ( "os" diff --git a/common/alloc/buffer_test.go b/common/buf/buffer_test.go similarity index 97% rename from common/alloc/buffer_test.go rename to common/buf/buffer_test.go index 13a1ad04c..61bd01803 100644 --- a/common/alloc/buffer_test.go +++ b/common/buf/buffer_test.go @@ -1,9 +1,9 @@ -package alloc_test +package buf_test import ( "testing" - . "v2ray.com/core/common/alloc" + . "v2ray.com/core/common/buf" "v2ray.com/core/common/serial" "v2ray.com/core/testing/assert" ) diff --git a/common/crypto/auth.go b/common/crypto/auth.go index b0a735168..f142a298c 100644 --- a/common/crypto/auth.go +++ b/common/crypto/auth.go @@ -5,7 +5,7 @@ import ( "errors" "io" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" "v2ray.com/core/common/serial" ) @@ -71,7 +71,7 @@ func (v *AEADAuthenticator) Seal(dst, plainText []byte) ([]byte, error) { type AuthenticationReader struct { auth Authenticator - buffer *alloc.Buffer + buffer *buf.Buffer reader io.Reader chunk []byte @@ -81,7 +81,7 @@ type AuthenticationReader struct { func NewAuthenticationReader(auth Authenticator, reader io.Reader, aggressive bool) *AuthenticationReader { return &AuthenticationReader{ auth: auth, - buffer: alloc.NewLocalBuffer(32 * 1024), + buffer: buf.NewLocalBuffer(32 * 1024), reader: reader, aggressive: aggressive, } diff --git a/common/io/buffered_reader.go b/common/io/buffered_reader.go index 1568283ac..bf9c93f92 100644 --- a/common/io/buffered_reader.go +++ b/common/io/buffered_reader.go @@ -4,20 +4,20 @@ import ( "io" "sync" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" ) type BufferedReader struct { sync.Mutex reader io.Reader - buffer *alloc.Buffer + buffer *buf.Buffer cached bool } func NewBufferedReader(rawReader io.Reader) *BufferedReader { return &BufferedReader{ reader: rawReader, - buffer: alloc.NewBuffer(), + buffer: buf.NewBuffer(), cached: true, } } diff --git a/common/io/buffered_reader_test.go b/common/io/buffered_reader_test.go index 78e4e69f6..49d9edb39 100644 --- a/common/io/buffered_reader_test.go +++ b/common/io/buffered_reader_test.go @@ -4,7 +4,7 @@ import ( "testing" "crypto/rand" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" . "v2ray.com/core/common/io" "v2ray.com/core/testing/assert" ) @@ -12,7 +12,7 @@ import ( func TestBufferedReader(t *testing.T) { assert := assert.On(t) - content := alloc.NewBuffer() + content := buf.NewBuffer() content.FillFrom(rand.Reader) len := content.Len() diff --git a/common/io/buffered_writer.go b/common/io/buffered_writer.go index 3dc6e9744..67df6be80 100644 --- a/common/io/buffered_writer.go +++ b/common/io/buffered_writer.go @@ -3,21 +3,21 @@ package io import ( "io" "sync" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" "v2ray.com/core/common/errors" ) type BufferedWriter struct { sync.Mutex writer io.Writer - buffer *alloc.Buffer + buffer *buf.Buffer cached bool } func NewBufferedWriter(rawWriter io.Writer) *BufferedWriter { return &BufferedWriter{ writer: rawWriter, - buffer: alloc.NewSmallBuffer(), + buffer: buf.NewSmallBuffer(), cached: true, } } diff --git a/common/io/buffered_writer_test.go b/common/io/buffered_writer_test.go index 0b8dac9c0..215b918c0 100644 --- a/common/io/buffered_writer_test.go +++ b/common/io/buffered_writer_test.go @@ -4,7 +4,7 @@ import ( "crypto/rand" "testing" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" . "v2ray.com/core/common/io" "v2ray.com/core/testing/assert" ) @@ -12,7 +12,7 @@ import ( func TestBufferedWriter(t *testing.T) { assert := assert.On(t) - content := alloc.NewBuffer() + content := buf.NewBuffer() writer := NewBufferedWriter(content) assert.Bool(writer.Cached()).IsTrue() @@ -32,7 +32,7 @@ func TestBufferedWriter(t *testing.T) { func TestBufferedWriterLargePayload(t *testing.T) { assert := assert.On(t) - content := alloc.NewLocalBuffer(128 * 1024) + content := buf.NewLocalBuffer(128 * 1024) writer := NewBufferedWriter(content) assert.Bool(writer.Cached()).IsTrue() diff --git a/common/io/chain_writer.go b/common/io/chain_writer.go index 70b751c56..6cf0d4705 100644 --- a/common/io/chain_writer.go +++ b/common/io/chain_writer.go @@ -4,7 +4,7 @@ import ( "io" "sync" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" ) type ChainWriter struct { @@ -28,7 +28,7 @@ func (v *ChainWriter) Write(payload []byte) (int, error) { bytesWritten := 0 size := len(payload) for size > 0 { - buffer := alloc.NewBuffer() + buffer := buf.NewBuffer() nBytes, _ := buffer.Write(payload) size -= nBytes payload = payload[nBytes:] diff --git a/common/io/chan_reader.go b/common/io/chan_reader.go index e9a8d853d..23d0215a6 100644 --- a/common/io/chan_reader.go +++ b/common/io/chan_reader.go @@ -4,13 +4,13 @@ import ( "io" "sync" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" ) type ChanReader struct { sync.Mutex stream Reader - current *alloc.Buffer + current *buf.Buffer eof bool } diff --git a/common/io/reader.go b/common/io/reader.go index 58a9edcf2..5614a4a18 100644 --- a/common/io/reader.go +++ b/common/io/reader.go @@ -4,20 +4,20 @@ import ( "io" "v2ray.com/core/common" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" ) // Reader extends io.Reader with alloc.Buffer. type Reader interface { common.Releasable // Read reads content from underlying reader, and put it into an alloc.Buffer. - Read() (*alloc.Buffer, error) + Read() (*buf.Buffer, error) } // AdaptiveReader is a Reader that adjusts its reading speed automatically. type AdaptiveReader struct { reader io.Reader - largeBuffer *alloc.Buffer + largeBuffer *buf.Buffer highVolumn bool } @@ -30,21 +30,21 @@ func NewAdaptiveReader(reader io.Reader) *AdaptiveReader { } // Read implements Reader.Read(). -func (v *AdaptiveReader) Read() (*alloc.Buffer, error) { +func (v *AdaptiveReader) Read() (*buf.Buffer, error) { if v.highVolumn && v.largeBuffer.IsEmpty() { if v.largeBuffer == nil { - v.largeBuffer = alloc.NewLocalBuffer(32 * 1024) + v.largeBuffer = buf.NewLocalBuffer(32 * 1024) } nBytes, err := v.largeBuffer.FillFrom(v.reader) if err != nil { return nil, err } - if nBytes < alloc.BufferSize { + if nBytes < buf.BufferSize { v.highVolumn = false } } - buffer := alloc.NewBuffer() + buffer := buf.NewBuffer() if !v.largeBuffer.IsEmpty() { buffer.FillFrom(v.largeBuffer) return buffer, nil diff --git a/common/io/reader_test.go b/common/io/reader_test.go index 7150178b3..67ab1600d 100644 --- a/common/io/reader_test.go +++ b/common/io/reader_test.go @@ -4,7 +4,7 @@ import ( "bytes" "testing" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" . "v2ray.com/core/common/io" "v2ray.com/core/testing/assert" ) @@ -19,8 +19,8 @@ func TestAdaptiveReader(t *testing.T) { b1, err := reader.Read() assert.Error(err).IsNil() assert.Bool(b1.IsFull()).IsTrue() - assert.Int(b1.Len()).Equals(alloc.BufferSize) - assert.Int(buffer.Len()).Equals(cap(rawContent) - alloc.BufferSize) + assert.Int(b1.Len()).Equals(buf.BufferSize) + assert.Int(buffer.Len()).Equals(cap(rawContent) - buf.BufferSize) b2, err := reader.Read() assert.Error(err).IsNil() diff --git a/common/io/writer.go b/common/io/writer.go index f6bb86c65..8c7ba3f6e 100644 --- a/common/io/writer.go +++ b/common/io/writer.go @@ -4,14 +4,14 @@ import ( "io" "v2ray.com/core/common" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" ) // Writer extends io.Writer with alloc.Buffer. type Writer interface { common.Releasable // Write writes an alloc.Buffer into underlying writer. - Write(*alloc.Buffer) error + Write(*buf.Buffer) error } // AdaptiveWriter is a Writer that writes alloc.Buffer into underlying writer. @@ -27,7 +27,7 @@ func NewAdaptiveWriter(writer io.Writer) *AdaptiveWriter { } // Write implements Writer.Write(). Write() takes ownership of the given buffer. -func (v *AdaptiveWriter) Write(buffer *alloc.Buffer) error { +func (v *AdaptiveWriter) Write(buffer *buf.Buffer) error { defer buffer.Release() for { nBytes, err := v.writer.Write(buffer.Bytes()) diff --git a/common/io/writer_test.go b/common/io/writer_test.go index ca07978ae..90ce69cb2 100644 --- a/common/io/writer_test.go +++ b/common/io/writer_test.go @@ -5,7 +5,7 @@ import ( "crypto/rand" "testing" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" . "v2ray.com/core/common/io" "v2ray.com/core/testing/assert" ) @@ -13,7 +13,7 @@ import ( func TestAdaptiveWriter(t *testing.T) { assert := assert.On(t) - lb := alloc.NewBuffer() + lb := buf.NewBuffer() lb.FillFrom(rand.Reader) expectedBytes := append([]byte(nil), lb.Bytes()...) diff --git a/common/serial/hash.go b/common/serial/hash.go index 933cb9241..84683d84b 100644 --- a/common/serial/hash.go +++ b/common/serial/hash.go @@ -2,10 +2,11 @@ package serial import ( "hash" - "v2ray.com/core/common/alloc" + + "v2ray.com/core/common/buf" ) -func WriteHash(h hash.Hash) alloc.BytesWriter { +func WriteHash(h hash.Hash) buf.BytesWriter { return func(b []byte) int { h.Sum(b[:0]) return h.Size() diff --git a/common/serial/numbers.go b/common/serial/numbers.go index 2246076af..bd670b606 100644 --- a/common/serial/numbers.go +++ b/common/serial/numbers.go @@ -2,7 +2,8 @@ package serial import ( "strconv" - "v2ray.com/core/common/alloc" + + "v2ray.com/core/common/buf" ) func Uint16ToBytes(value uint16, b []byte) []byte { @@ -13,7 +14,7 @@ func Uint16ToString(value uint16) string { return strconv.Itoa(int(value)) } -func WriteUint16(value uint16) alloc.BytesWriter { +func WriteUint16(value uint16) buf.BytesWriter { return func(b []byte) int { b = Uint16ToBytes(value, b[:0]) return 2 @@ -28,7 +29,7 @@ func Uint32ToString(value uint32) string { return strconv.FormatUint(uint64(value), 10) } -func WriteUint32(value uint32) alloc.BytesWriter { +func WriteUint32(value uint32) buf.BytesWriter { return func(b []byte) int { b = Uint32ToBytes(value, b[:0]) return 4 diff --git a/common/serial/string.go b/common/serial/string.go index 4ca320685..e21eb126b 100644 --- a/common/serial/string.go +++ b/common/serial/string.go @@ -3,7 +3,8 @@ package serial import ( "fmt" "strings" - "v2ray.com/core/common/alloc" + + "v2ray.com/core/common/buf" ) func ToString(v interface{}) string { @@ -35,7 +36,7 @@ func Concat(v ...interface{}) string { return strings.Join(values, "") } -func WriteString(s string) alloc.BytesWriter { +func WriteString(s string) buf.BytesWriter { return func(b []byte) int { copy(b, []byte(s)) return len(s) diff --git a/proxy/blackhole/blackhole.go b/proxy/blackhole/blackhole.go index 5e9985e82..cb23e3fdc 100644 --- a/proxy/blackhole/blackhole.go +++ b/proxy/blackhole/blackhole.go @@ -2,7 +2,7 @@ package blackhole import ( "v2ray.com/core/app" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" v2net "v2ray.com/core/common/net" "v2ray.com/core/proxy" "v2ray.com/core/transport/ray" @@ -25,7 +25,7 @@ func NewBlackHole(space app.Space, config *Config, meta *proxy.OutboundHandlerMe }, nil } -func (v *BlackHole) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) { +func (v *BlackHole) Dispatch(destination v2net.Destination, payload *buf.Buffer, ray ray.OutboundRay) { payload.Release() v.response.WriteTo(ray.OutboundOutput()) diff --git a/proxy/blackhole/config.go b/proxy/blackhole/config.go index df1cf0fa3..05e5ab432 100644 --- a/proxy/blackhole/config.go +++ b/proxy/blackhole/config.go @@ -1,7 +1,7 @@ package blackhole import ( - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" v2io "v2ray.com/core/common/io" "github.com/golang/protobuf/ptypes" @@ -32,7 +32,7 @@ func (v *NoneResponse) AsAny() *any.Any { } func (v *HTTPResponse) WriteTo(writer v2io.Writer) { - b := alloc.NewLocalBuffer(512) + b := buf.NewLocalBuffer(512) b.AppendFunc(serial.WriteString(http403response)) writer.Write(b) } diff --git a/proxy/blackhole/config_test.go b/proxy/blackhole/config_test.go index 2884a5687..3d34ab92e 100644 --- a/proxy/blackhole/config_test.go +++ b/proxy/blackhole/config_test.go @@ -5,7 +5,7 @@ import ( "net/http" "testing" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" v2io "v2ray.com/core/common/io" . "v2ray.com/core/proxy/blackhole" "v2ray.com/core/testing/assert" @@ -14,7 +14,7 @@ import ( func TestHTTPResponse(t *testing.T) { assert := assert.On(t) - buffer := alloc.NewBuffer() + buffer := buf.NewBuffer() httpResponse := new(HTTPResponse) httpResponse.WriteTo(v2io.NewAdaptiveWriter(buffer)) diff --git a/proxy/dokodemo/dokodemo.go b/proxy/dokodemo/dokodemo.go index e1635a780..8fbd5467b 100644 --- a/proxy/dokodemo/dokodemo.go +++ b/proxy/dokodemo/dokodemo.go @@ -5,7 +5,7 @@ import ( "v2ray.com/core/app" "v2ray.com/core/app/dispatcher" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" "v2ray.com/core/common/errors" v2io "v2ray.com/core/common/io" "v2ray.com/core/common/loader" @@ -107,7 +107,7 @@ func (v *DokodemoDoor) ListenUDP() error { return nil } -func (v *DokodemoDoor) handleUDPPackets(payload *alloc.Buffer, session *proxy.SessionInfo) { +func (v *DokodemoDoor) handleUDPPackets(payload *buf.Buffer, session *proxy.SessionInfo) { if session.Destination.Network == v2net.Network_Unknown && v.address != nil && v.port > 0 { session.Destination = v2net.UDPDestination(v.address, v.port) } @@ -119,7 +119,7 @@ func (v *DokodemoDoor) handleUDPPackets(payload *alloc.Buffer, session *proxy.Se v.udpServer.Dispatch(session, payload, v.handleUDPResponse) } -func (v *DokodemoDoor) handleUDPResponse(dest v2net.Destination, payload *alloc.Buffer) { +func (v *DokodemoDoor) handleUDPResponse(dest v2net.Destination, payload *buf.Buffer) { defer payload.Release() v.udpMutex.RLock() defer v.udpMutex.RUnlock() diff --git a/proxy/freedom/freedom.go b/proxy/freedom/freedom.go index bc94966d3..e14f760ca 100644 --- a/proxy/freedom/freedom.go +++ b/proxy/freedom/freedom.go @@ -4,7 +4,7 @@ import ( "io" "v2ray.com/core/app" "v2ray.com/core/app/dns" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" "v2ray.com/core/common/dice" "v2ray.com/core/common/errors" v2io "v2ray.com/core/common/io" @@ -67,7 +67,7 @@ func (v *FreedomConnection) ResolveIP(destination v2net.Destination) v2net.Desti return newDest } -func (v *FreedomConnection) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) { +func (v *FreedomConnection) Dispatch(destination v2net.Destination, payload *buf.Buffer, ray ray.OutboundRay) { log.Info("Freedom: Opening connection to ", destination) defer payload.Release() diff --git a/proxy/freedom/freedom_test.go b/proxy/freedom/freedom_test.go index fde289487..f0584e5f1 100644 --- a/proxy/freedom/freedom_test.go +++ b/proxy/freedom/freedom_test.go @@ -9,7 +9,7 @@ import ( "v2ray.com/core/app/dns" "v2ray.com/core/app/proxyman" "v2ray.com/core/app/router" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" v2net "v2ray.com/core/common/net" "v2ray.com/core/proxy" . "v2ray.com/core/proxy/freedom" @@ -47,7 +47,7 @@ func TestSinglePacket(t *testing.T) { traffic := ray.NewRay() data2Send := "Data to be sent to remote" - payload := alloc.NewLocalBuffer(2048) + payload := buf.NewLocalBuffer(2048) payload.Append([]byte(data2Send)) go freedom.Dispatch(v2net.TCPDestination(v2net.LocalHostIP, tcpServer.Port), payload, traffic) diff --git a/proxy/proxy.go b/proxy/proxy.go index 0b3d1974e..b84b066f5 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -2,7 +2,7 @@ package proxy import ( - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" v2net "v2ray.com/core/common/net" "v2ray.com/core/common/protocol" "v2ray.com/core/transport/internet" @@ -58,5 +58,5 @@ type InboundHandler interface { // An OutboundHandler handles outbound network connection for V2Ray. type OutboundHandler interface { // Dispatch sends one or more Packets to its destination. - Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) + Dispatch(destination v2net.Destination, payload *buf.Buffer, ray ray.OutboundRay) } diff --git a/proxy/shadowsocks/client.go b/proxy/shadowsocks/client.go index 39f917ed0..d7192ba79 100644 --- a/proxy/shadowsocks/client.go +++ b/proxy/shadowsocks/client.go @@ -3,7 +3,7 @@ package shadowsocks import ( "sync" "v2ray.com/core/app" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" v2io "v2ray.com/core/common/io" "v2ray.com/core/common/log" v2net "v2ray.com/core/common/net" @@ -32,7 +32,7 @@ func NewClient(config *ClientConfig, space app.Space, meta *proxy.OutboundHandle return client, nil } -func (v *Client) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) { +func (v *Client) Dispatch(destination v2net.Destination, payload *buf.Buffer, ray ray.OutboundRay) { defer payload.Release() defer ray.OutboundInput().Release() defer ray.OutboundOutput().Close() diff --git a/proxy/shadowsocks/ota.go b/proxy/shadowsocks/ota.go index b8e799cbc..174d2ae62 100644 --- a/proxy/shadowsocks/ota.go +++ b/proxy/shadowsocks/ota.go @@ -6,7 +6,7 @@ import ( "crypto/sha1" "io" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" "v2ray.com/core/common/errors" "v2ray.com/core/common/serial" ) @@ -27,7 +27,7 @@ func NewAuthenticator(keygen KeyGenerator) *Authenticator { } } -func (v *Authenticator) Authenticate(data []byte) alloc.BytesWriter { +func (v *Authenticator) Authenticate(data []byte) buf.BytesWriter { hasher := hmac.New(sha1.New, v.key()) hasher.Write(data) res := hasher.Sum(nil) @@ -74,8 +74,8 @@ func (v *ChunkReader) Release() { v.auth = nil } -func (v *ChunkReader) Read() (*alloc.Buffer, error) { - buffer := alloc.NewBuffer() +func (v *ChunkReader) Read() (*buf.Buffer, error) { + buffer := buf.NewBuffer() if _, err := buffer.FillFullFrom(v.reader, 2); err != nil { buffer.Release() return nil, err @@ -83,10 +83,10 @@ func (v *ChunkReader) Read() (*alloc.Buffer, error) { // There is a potential buffer overflow here. Large buffer is 64K bytes, // while uin16 + 10 will be more than that length := serial.BytesToUint16(buffer.BytesTo(2)) + AuthSize - if length > alloc.BufferSize { + if length > buf.BufferSize { // Theoretically the size of a chunk is 64K, but most Shadowsocks implementations used <4K buffer. buffer.Release() - buffer = alloc.NewLocalBuffer(int(length) + 128) + buffer = buf.NewLocalBuffer(int(length) + 128) } buffer.Clear() @@ -128,7 +128,7 @@ func (v *ChunkWriter) Release() { v.auth = nil } -func (v *ChunkWriter) Write(payload *alloc.Buffer) error { +func (v *ChunkWriter) Write(payload *buf.Buffer) error { totalLength := payload.Len() serial.Uint16ToBytes(uint16(totalLength), v.buffer[:0]) v.auth.Authenticate(payload.Bytes())(v.buffer[2:]) diff --git a/proxy/shadowsocks/ota_test.go b/proxy/shadowsocks/ota_test.go index d436082d2..2bad69762 100644 --- a/proxy/shadowsocks/ota_test.go +++ b/proxy/shadowsocks/ota_test.go @@ -3,7 +3,7 @@ package shadowsocks_test import ( "testing" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" . "v2ray.com/core/proxy/shadowsocks" "v2ray.com/core/testing/assert" ) @@ -11,7 +11,7 @@ import ( func TestNormalChunkReading(t *testing.T) { assert := assert.On(t) - buffer := alloc.NewBuffer() + buffer := buf.NewBuffer() buffer.AppendBytes( 0, 8, 39, 228, 69, 96, 133, 39, 254, 26, 201, 70, 11, 12, 13, 14, 15, 16, 17, 18) reader := NewChunkReader(buffer, NewAuthenticator(ChunkKeyGenerator( @@ -24,11 +24,11 @@ func TestNormalChunkReading(t *testing.T) { func TestNormalChunkWriting(t *testing.T) { assert := assert.On(t) - buffer := alloc.NewLocalBuffer(512) + buffer := buf.NewLocalBuffer(512) writer := NewChunkWriter(buffer, NewAuthenticator(ChunkKeyGenerator( []byte{21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36}))) - b := alloc.NewLocalBuffer(256) + b := buf.NewLocalBuffer(256) b.Append([]byte{11, 12, 13, 14, 15, 16, 17, 18}) err := writer.Write(b) assert.Error(err).IsNil() diff --git a/proxy/shadowsocks/protocol.go b/proxy/shadowsocks/protocol.go index 3d8075076..75c8ebd37 100644 --- a/proxy/shadowsocks/protocol.go +++ b/proxy/shadowsocks/protocol.go @@ -4,7 +4,7 @@ import ( "bytes" "crypto/rand" "io" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" "v2ray.com/core/common/crypto" "v2ray.com/core/common/errors" v2io "v2ray.com/core/common/io" @@ -29,7 +29,7 @@ func ReadTCPSession(user *protocol.User, reader io.Reader) (*protocol.RequestHea } account := rawAccount.(*ShadowsocksAccount) - buffer := alloc.NewLocalBuffer(512) + buffer := buf.NewLocalBuffer(512) defer buffer.Release() ivLen := account.Cipher.IVSize() @@ -152,7 +152,7 @@ func WriteTCPRequest(request *protocol.RequestHeader, writer io.Writer) (v2io.Wr writer = crypto.NewCryptionWriter(stream, writer) - header := alloc.NewLocalBuffer(512) + header := buf.NewLocalBuffer(512) switch request.Address.Family() { case v2net.AddressFamilyIPv4: @@ -235,7 +235,7 @@ func WriteTCPResponse(request *protocol.RequestHeader, writer io.Writer) (v2io.W return v2io.NewAdaptiveWriter(crypto.NewCryptionWriter(stream, writer)), nil } -func EncodeUDPPacket(request *protocol.RequestHeader, payload *alloc.Buffer) (*alloc.Buffer, error) { +func EncodeUDPPacket(request *protocol.RequestHeader, payload *buf.Buffer) (*buf.Buffer, error) { user := request.User rawAccount, err := user.GetTypedAccount() if err != nil { @@ -243,7 +243,7 @@ func EncodeUDPPacket(request *protocol.RequestHeader, payload *alloc.Buffer) (*a } account := rawAccount.(*ShadowsocksAccount) - buffer := alloc.NewSmallBuffer() + buffer := buf.NewSmallBuffer() ivLen := account.Cipher.IVSize() buffer.FillFullFrom(rand.Reader, ivLen) iv := buffer.Bytes() @@ -281,7 +281,7 @@ func EncodeUDPPacket(request *protocol.RequestHeader, payload *alloc.Buffer) (*a return buffer, nil } -func DecodeUDPPacket(user *protocol.User, payload *alloc.Buffer) (*protocol.RequestHeader, *alloc.Buffer, error) { +func DecodeUDPPacket(user *protocol.User, payload *buf.Buffer) (*protocol.RequestHeader, *buf.Buffer, error) { rawAccount, err := user.GetTypedAccount() if err != nil { return nil, nil, errors.Base(err).Message("Shadowsocks|UDP: Failed to parse account.") @@ -359,8 +359,8 @@ type UDPReader struct { User *protocol.User } -func (v *UDPReader) Read() (*alloc.Buffer, error) { - buffer := alloc.NewSmallBuffer() +func (v *UDPReader) Read() (*buf.Buffer, error) { + buffer := buf.NewSmallBuffer() _, err := buffer.FillFrom(v.Reader) if err != nil { buffer.Release() @@ -382,7 +382,7 @@ type UDPWriter struct { Request *protocol.RequestHeader } -func (v *UDPWriter) Write(buffer *alloc.Buffer) error { +func (v *UDPWriter) Write(buffer *buf.Buffer) error { payload, err := EncodeUDPPacket(v.Request, buffer) if err != nil { return err diff --git a/proxy/shadowsocks/protocol_test.go b/proxy/shadowsocks/protocol_test.go index ede36ceed..d27d78b9d 100644 --- a/proxy/shadowsocks/protocol_test.go +++ b/proxy/shadowsocks/protocol_test.go @@ -3,7 +3,7 @@ package shadowsocks_test import ( "testing" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" "v2ray.com/core/common/loader" v2net "v2ray.com/core/common/net" "v2ray.com/core/common/protocol" @@ -30,7 +30,7 @@ func TestUDPEncoding(t *testing.T) { }, } - data := alloc.NewLocalBuffer(256) + data := buf.NewLocalBuffer(256) data.AppendFunc(serial.WriteString("test string")) encodedData, err := EncodeUDPPacket(request, data) assert.Error(err).IsNil() @@ -60,9 +60,9 @@ func TestTCPRequest(t *testing.T) { }, } - data := alloc.NewLocalBuffer(256) + data := buf.NewLocalBuffer(256) data.AppendFunc(serial.WriteString("test string")) - cache := alloc.NewBuffer() + cache := buf.NewBuffer() writer, err := WriteTCPRequest(request, cache) assert.Error(err).IsNil() @@ -88,7 +88,7 @@ func TestUDPReaderWriter(t *testing.T) { CipherType: CipherType_CHACHA20_IEFT, }), } - cache := alloc.NewBuffer() + cache := buf.NewBuffer() writer := &UDPWriter{ Writer: cache, Request: &protocol.RequestHeader{ @@ -105,7 +105,7 @@ func TestUDPReaderWriter(t *testing.T) { User: user, } - b := alloc.NewBuffer() + b := buf.NewBuffer() b.AppendFunc(serial.WriteString("test payload")) err := writer.Write(b) assert.Error(err).IsNil() @@ -114,7 +114,7 @@ func TestUDPReaderWriter(t *testing.T) { assert.Error(err).IsNil() assert.String(payload.String()).Equals("test payload") - b = alloc.NewBuffer() + b = buf.NewBuffer() b.AppendFunc(serial.WriteString("test payload 2")) err = writer.Write(b) assert.Error(err).IsNil() diff --git a/proxy/shadowsocks/server.go b/proxy/shadowsocks/server.go index 44d811f6e..473df9f37 100644 --- a/proxy/shadowsocks/server.go +++ b/proxy/shadowsocks/server.go @@ -6,7 +6,7 @@ import ( "v2ray.com/core/app" "v2ray.com/core/app/dispatcher" "v2ray.com/core/common" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" "v2ray.com/core/common/errors" v2io "v2ray.com/core/common/io" "v2ray.com/core/common/log" @@ -103,7 +103,7 @@ func (v *Server) Start() error { return nil } -func (v *Server) handlerUDPPayload(payload *alloc.Buffer, session *proxy.SessionInfo) { +func (v *Server) handlerUDPPayload(payload *buf.Buffer, session *proxy.SessionInfo) { source := session.Source request, data, err := DecodeUDPPacket(v.user, payload) if err != nil { @@ -129,7 +129,7 @@ func (v *Server) handlerUDPPayload(payload *alloc.Buffer, session *proxy.Session log.Access(source, dest, log.AccessAccepted, "") log.Info("Shadowsocks|Server: Tunnelling request to ", dest) - v.udpServer.Dispatch(&proxy.SessionInfo{Source: source, Destination: dest, User: request.User, Inbound: v.meta}, data, func(destination v2net.Destination, payload *alloc.Buffer) { + v.udpServer.Dispatch(&proxy.SessionInfo{Source: source, Destination: dest, User: request.User, Inbound: v.meta}, data, func(destination v2net.Destination, payload *buf.Buffer) { defer payload.Release() data, err := EncodeUDPPacket(request, payload) diff --git a/proxy/socks/protocol/socks.go b/proxy/socks/protocol/socks.go index 5f4d869cd..9408bb22c 100644 --- a/proxy/socks/protocol/socks.go +++ b/proxy/socks/protocol/socks.go @@ -3,7 +3,7 @@ package protocol import ( "fmt" "io" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" "v2ray.com/core/common/errors" "v2ray.com/core/common/log" v2net "v2ray.com/core/common/net" @@ -120,7 +120,7 @@ func (request Socks5UserPassRequest) AuthDetail() string { } func ReadUserPassRequest(reader io.Reader) (request Socks5UserPassRequest, err error) { - buffer := alloc.NewLocalBuffer(512) + buffer := buf.NewLocalBuffer(512) defer buffer.Release() _, err = buffer.FillFullFrom(reader, 2) @@ -188,7 +188,7 @@ type Socks5Request struct { } func ReadRequest(reader io.Reader) (request *Socks5Request, err error) { - buffer := alloc.NewLocalBuffer(512) + buffer := buf.NewLocalBuffer(512) defer buffer.Release() _, err = buffer.FillFullFrom(reader, 4) diff --git a/proxy/socks/protocol/socks4_test.go b/proxy/socks/protocol/socks4_test.go index 4a2852528..6050aa3ac 100644 --- a/proxy/socks/protocol/socks4_test.go +++ b/proxy/socks/protocol/socks4_test.go @@ -4,7 +4,7 @@ import ( "bytes" "testing" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" v2net "v2ray.com/core/common/net" "v2ray.com/core/testing/assert" ) @@ -31,7 +31,7 @@ func TestSocks4AuthenticationResponseToBytes(t *testing.T) { response := NewSocks4AuthenticationResponse(byte(0x10), 443, []byte{1, 2, 3, 4}) - buffer := alloc.NewLocalBuffer(2048) + buffer := buf.NewLocalBuffer(2048) defer buffer.Release() response.Write(buffer) diff --git a/proxy/socks/protocol/socks_test.go b/proxy/socks/protocol/socks_test.go index d47a4c3d6..f25164c2b 100644 --- a/proxy/socks/protocol/socks_test.go +++ b/proxy/socks/protocol/socks_test.go @@ -5,7 +5,7 @@ import ( "io" "testing" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" v2net "v2ray.com/core/common/net" "v2ray.com/core/proxy" "v2ray.com/core/testing/assert" @@ -30,7 +30,7 @@ func TestHasAuthenticationMethod(t *testing.T) { func TestAuthenticationRequestRead(t *testing.T) { assert := assert.On(t) - buffer := alloc.NewBuffer() + buffer := buf.NewBuffer() buffer.AppendBytes( 0x05, // version 0x01, // nMethods @@ -85,7 +85,7 @@ func TestResponseWrite(t *testing.T) { [16]byte{}, v2net.Port(53), } - buffer := alloc.NewLocalBuffer(2048) + buffer := buf.NewLocalBuffer(2048) defer buffer.Release() response.Write(buffer) @@ -106,7 +106,7 @@ func TestSetIPv6(t *testing.T) { response := NewSocks5Response() response.SetIPv6([]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}) - buffer := alloc.NewLocalBuffer(2048) + buffer := buf.NewLocalBuffer(2048) defer buffer.Release() response.Write(buffer) assert.Bytes(buffer.Bytes()).Equals([]byte{ @@ -119,7 +119,7 @@ func TestSetDomain(t *testing.T) { response := NewSocks5Response() response.SetDomain("v2ray.com") - buffer := alloc.NewLocalBuffer(2048) + buffer := buf.NewLocalBuffer(2048) defer buffer.Release() response.Write(buffer) assert.Bytes(buffer.Bytes()).Equals([]byte{ @@ -129,7 +129,7 @@ func TestSetDomain(t *testing.T) { func TestEmptyAuthRequest(t *testing.T) { assert := assert.On(t) - _, _, err := ReadAuthentication(alloc.NewBuffer()) + _, _, err := ReadAuthentication(buf.NewBuffer()) assert.Error(err).Equals(io.EOF) } @@ -143,7 +143,7 @@ func TestSingleByteAuthRequest(t *testing.T) { func TestZeroAuthenticationMethod(t *testing.T) { assert := assert.On(t) - buffer := alloc.NewBuffer() + buffer := buf.NewBuffer() buffer.AppendBytes(5, 0) _, _, err := ReadAuthentication(buffer) assert.Error(err).Equals(crypto.ErrAuthenticationFailed) @@ -151,7 +151,7 @@ func TestZeroAuthenticationMethod(t *testing.T) { func TestWrongProtocolVersion(t *testing.T) { assert := assert.On(t) - buffer := alloc.NewBuffer() + buffer := buf.NewBuffer() buffer.AppendBytes(6, 1, 0) _, _, err := ReadAuthentication(buffer) assert.Error(err).Equals(proxy.ErrInvalidProtocolVersion) @@ -160,14 +160,14 @@ func TestWrongProtocolVersion(t *testing.T) { func TestEmptyRequest(t *testing.T) { assert := assert.On(t) - _, err := ReadRequest(alloc.NewBuffer()) + _, err := ReadRequest(buf.NewBuffer()) assert.Error(err).Equals(io.EOF) } func TestIPv6Request(t *testing.T) { assert := assert.On(t) - b := alloc.NewBuffer() + b := buf.NewBuffer() b.AppendBytes(5, 1, 0, 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 0, 8) request, err := ReadRequest(b) assert.Error(err).IsNil() diff --git a/proxy/socks/protocol/udp.go b/proxy/socks/protocol/udp.go index 84f6a2b7d..f85d7001e 100644 --- a/proxy/socks/protocol/udp.go +++ b/proxy/socks/protocol/udp.go @@ -2,7 +2,7 @@ package protocol import ( "fmt" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" "v2ray.com/core/common/errors" v2net "v2ray.com/core/common/net" "v2ray.com/core/common/serial" @@ -16,14 +16,14 @@ type Socks5UDPRequest struct { Fragment byte Address v2net.Address Port v2net.Port - Data *alloc.Buffer + Data *buf.Buffer } func (request *Socks5UDPRequest) Destination() v2net.Destination { return v2net.UDPDestination(request.Address, request.Port) } -func (request *Socks5UDPRequest) Write(buffer *alloc.Buffer) { +func (request *Socks5UDPRequest) Write(buffer *buf.Buffer) { buffer.AppendBytes(0, 0, request.Fragment) switch request.Address.Family() { case v2net.AddressFamilyIPv4: @@ -83,7 +83,7 @@ func ReadUDPRequest(packet []byte) (*Socks5UDPRequest, error) { } if len(packet) > dataBegin { - b := alloc.NewSmallBuffer() + b := buf.NewSmallBuffer() b.Append(packet[dataBegin:]) request.Data = b } diff --git a/proxy/socks/server_udp.go b/proxy/socks/server_udp.go index 56efd7c15..05c31063d 100644 --- a/proxy/socks/server_udp.go +++ b/proxy/socks/server_udp.go @@ -1,7 +1,7 @@ package socks import ( - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" "v2ray.com/core/common/log" v2net "v2ray.com/core/common/net" "v2ray.com/core/proxy" @@ -23,7 +23,7 @@ func (v *Server) listenUDP() error { return nil } -func (v *Server) handleUDPPayload(payload *alloc.Buffer, session *proxy.SessionInfo) { +func (v *Server) handleUDPPayload(payload *buf.Buffer, session *proxy.SessionInfo) { source := session.Source log.Info("Socks: Client UDP connection from ", source) request, err := protocol.ReadUDPRequest(payload.Bytes()) @@ -46,7 +46,7 @@ func (v *Server) handleUDPPayload(payload *alloc.Buffer, session *proxy.SessionI log.Info("Socks: Send packet to ", request.Destination(), " with ", request.Data.Len(), " bytes") log.Access(source, request.Destination, log.AccessAccepted, "") - v.udpServer.Dispatch(&proxy.SessionInfo{Source: source, Destination: request.Destination(), Inbound: v.meta}, request.Data, func(destination v2net.Destination, payload *alloc.Buffer) { + v.udpServer.Dispatch(&proxy.SessionInfo{Source: source, Destination: request.Destination(), Inbound: v.meta}, request.Data, func(destination v2net.Destination, payload *buf.Buffer) { response := &protocol.Socks5UDPRequest{ Fragment: 0, Address: request.Destination().Address, @@ -55,7 +55,7 @@ func (v *Server) handleUDPPayload(payload *alloc.Buffer, session *proxy.SessionI } log.Info("Socks: Writing back UDP response with ", payload.Len(), " bytes to ", destination) - udpMessage := alloc.NewLocalBuffer(2048) + udpMessage := buf.NewLocalBuffer(2048) response.Write(udpMessage) v.udpMutex.RLock() diff --git a/proxy/testing/mocks/outboundhandler.go b/proxy/testing/mocks/outboundhandler.go index 668268707..1b21c5dcf 100644 --- a/proxy/testing/mocks/outboundhandler.go +++ b/proxy/testing/mocks/outboundhandler.go @@ -5,7 +5,7 @@ import ( "sync" "v2ray.com/core/app" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" v2io "v2ray.com/core/common/io" v2net "v2ray.com/core/common/net" "v2ray.com/core/proxy" @@ -18,7 +18,7 @@ type OutboundConnectionHandler struct { ConnOutput io.Writer } -func (v *OutboundConnectionHandler) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) { +func (v *OutboundConnectionHandler) Dispatch(destination v2net.Destination, payload *buf.Buffer, ray ray.OutboundRay) { input := ray.OutboundInput() output := ray.OutboundOutput() diff --git a/proxy/vmess/encoding/commands.go b/proxy/vmess/encoding/commands.go index 4898dd262..654bb3cc7 100644 --- a/proxy/vmess/encoding/commands.go +++ b/proxy/vmess/encoding/commands.go @@ -3,7 +3,7 @@ package encoding import ( "io" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" "v2ray.com/core/common/errors" v2net "v2ray.com/core/common/net" "v2ray.com/core/common/protocol" @@ -32,7 +32,7 @@ func MarshalCommand(command interface{}, writer io.Writer) error { return ErrUnknownCommand } - buffer := alloc.NewLocalBuffer(512) + buffer := buf.NewLocalBuffer(512) defer buffer.Release() err := factory.Marshal(command, buffer) diff --git a/proxy/vmess/encoding/commands_test.go b/proxy/vmess/encoding/commands_test.go index 94dc363cd..f5cbe3310 100644 --- a/proxy/vmess/encoding/commands_test.go +++ b/proxy/vmess/encoding/commands_test.go @@ -3,7 +3,7 @@ package encoding_test import ( "testing" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" "v2ray.com/core/common/protocol" "v2ray.com/core/common/uuid" . "v2ray.com/core/proxy/vmess/encoding" @@ -21,7 +21,7 @@ func TestSwitchAccount(t *testing.T) { ValidMin: 16, } - buffer := alloc.NewBuffer() + buffer := buf.NewBuffer() err := MarshalCommand(sa, buffer) assert.Error(err).IsNil() diff --git a/proxy/vmess/encoding/encoding_test.go b/proxy/vmess/encoding/encoding_test.go index 48be15bbc..7948e920a 100644 --- a/proxy/vmess/encoding/encoding_test.go +++ b/proxy/vmess/encoding/encoding_test.go @@ -3,7 +3,7 @@ package encoding_test import ( "testing" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" "v2ray.com/core/common/loader" v2net "v2ray.com/core/common/net" "v2ray.com/core/common/protocol" @@ -36,7 +36,7 @@ func TestRequestSerialization(t *testing.T) { Security: protocol.Security(protocol.SecurityType_AES128_GCM), } - buffer := alloc.NewBuffer() + buffer := buf.NewBuffer() client := NewClientSession(protocol.DefaultIDHash) client.EncodeRequestHeader(expectedRequest, buffer) diff --git a/proxy/vmess/inbound/inbound.go b/proxy/vmess/inbound/inbound.go index f305b679c..2f43796e8 100644 --- a/proxy/vmess/inbound/inbound.go +++ b/proxy/vmess/inbound/inbound.go @@ -8,7 +8,7 @@ import ( "v2ray.com/core/app/dispatcher" "v2ray.com/core/app/proxyman" "v2ray.com/core/common" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" "v2ray.com/core/common/errors" v2io "v2ray.com/core/common/io" "v2ray.com/core/common/loader" @@ -225,7 +225,7 @@ func (v *VMessInboundHandler) HandleConnection(connection internet.Connection) { } output.Release() if request.Option.Has(protocol.RequestOptionChunkStream) { - if err := bodyWriter.Write(alloc.NewLocalBuffer(8)); err != nil { + if err := bodyWriter.Write(buf.NewLocalBuffer(8)); err != nil { connection.SetReusable(false) } } diff --git a/proxy/vmess/outbound/outbound.go b/proxy/vmess/outbound/outbound.go index 314ab2c05..44255e38d 100644 --- a/proxy/vmess/outbound/outbound.go +++ b/proxy/vmess/outbound/outbound.go @@ -4,7 +4,7 @@ import ( "sync" "v2ray.com/core/app" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" v2io "v2ray.com/core/common/io" "v2ray.com/core/common/loader" "v2ray.com/core/common/log" @@ -25,7 +25,7 @@ type VMessOutboundHandler struct { meta *proxy.OutboundHandlerMeta } -func (v *VMessOutboundHandler) Dispatch(target v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) { +func (v *VMessOutboundHandler) Dispatch(target v2net.Destination, payload *buf.Buffer, ray ray.OutboundRay) { defer ray.OutboundInput().Release() defer ray.OutboundOutput().Close() @@ -92,7 +92,7 @@ func (v *VMessOutboundHandler) Dispatch(target v2net.Destination, payload *alloc return } -func (v *VMessOutboundHandler) handleRequest(session *encoding.ClientSession, conn internet.Connection, request *protocol.RequestHeader, payload *alloc.Buffer, input v2io.Reader, finish *sync.Mutex) { +func (v *VMessOutboundHandler) handleRequest(session *encoding.ClientSession, conn internet.Connection, request *protocol.RequestHeader, payload *buf.Buffer, input v2io.Reader, finish *sync.Mutex) { defer finish.Unlock() writer := v2io.NewBufferedWriter(conn) @@ -116,7 +116,7 @@ func (v *VMessOutboundHandler) handleRequest(session *encoding.ClientSession, co } if request.Option.Has(protocol.RequestOptionChunkStream) { - err := bodyWriter.Write(alloc.NewLocalBuffer(8)) + err := bodyWriter.Write(buf.NewLocalBuffer(8)) if err != nil { conn.SetReusable(false) } diff --git a/testing/scenarios/shadowsocks_test.go b/testing/scenarios/shadowsocks_test.go index 490e522fd..df7a3fa9a 100644 --- a/testing/scenarios/shadowsocks_test.go +++ b/testing/scenarios/shadowsocks_test.go @@ -4,7 +4,7 @@ import ( "fmt" "net" "testing" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" v2net "v2ray.com/core/common/net" "v2ray.com/core/testing/assert" "v2ray.com/core/testing/servers/tcp" @@ -42,7 +42,7 @@ func TestShadowsocksTCP(t *testing.T) { //conn.CloseWrite() - response := alloc.NewBuffer() + response := buf.NewBuffer() finished := false expectedResponse := "Processed: " + payload for { diff --git a/transport/internet/authenticator.go b/transport/internet/authenticator.go index a9458db01..45391542b 100644 --- a/transport/internet/authenticator.go +++ b/transport/internet/authenticator.go @@ -2,12 +2,12 @@ package internet import ( "v2ray.com/core/common" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" ) type Authenticator interface { - Seal(*alloc.Buffer) - Open(*alloc.Buffer) bool + Seal(*buf.Buffer) + Open(*buf.Buffer) bool Overhead() int } @@ -53,7 +53,7 @@ func (v *AuthenticatorChain) Overhead() int { return total } -func (v *AuthenticatorChain) Open(payload *alloc.Buffer) bool { +func (v *AuthenticatorChain) Open(payload *buf.Buffer) bool { for _, auth := range v.authenticators { if !auth.Open(payload) { return false @@ -62,7 +62,7 @@ func (v *AuthenticatorChain) Open(payload *alloc.Buffer) bool { return true } -func (v *AuthenticatorChain) Seal(payload *alloc.Buffer) { +func (v *AuthenticatorChain) Seal(payload *buf.Buffer) { for i := len(v.authenticators) - 1; i >= 0; i-- { auth := v.authenticators[i] auth.Seal(payload) diff --git a/transport/internet/headers/http/http.go b/transport/internet/headers/http/http.go index c8c42e35b..7ef1b0610 100644 --- a/transport/internet/headers/http/http.go +++ b/transport/internet/headers/http/http.go @@ -7,7 +7,7 @@ import ( "net/http" "strings" "time" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" "v2ray.com/core/common/loader" "v2ray.com/core/common/serial" "v2ray.com/core/transport/internet" @@ -23,7 +23,7 @@ var ( ) type Reader interface { - Read(io.Reader) (*alloc.Buffer, error) + Read(io.Reader) (*buf.Buffer, error) } type Writer interface { @@ -32,7 +32,7 @@ type Writer interface { type NoOpReader struct{} -func (v *NoOpReader) Read(io.Reader) (*alloc.Buffer, error) { +func (v *NoOpReader) Read(io.Reader) (*buf.Buffer, error) { return nil, nil } @@ -45,8 +45,8 @@ func (v *NoOpWriter) Write(io.Writer) error { type HeaderReader struct { } -func (*HeaderReader) Read(reader io.Reader) (*alloc.Buffer, error) { - buffer := alloc.NewSmallBuffer() +func (*HeaderReader) Read(reader io.Reader) (*buf.Buffer, error) { + buffer := buf.NewSmallBuffer() for { _, err := buffer.FillFrom(reader) if err != nil { @@ -69,10 +69,10 @@ func (*HeaderReader) Read(reader io.Reader) (*alloc.Buffer, error) { } type HeaderWriter struct { - header *alloc.Buffer + header *buf.Buffer } -func NewHeaderWriter(header *alloc.Buffer) *HeaderWriter { +func NewHeaderWriter(header *buf.Buffer) *HeaderWriter { return &HeaderWriter{ header: header, } @@ -91,7 +91,7 @@ func (v *HeaderWriter) Write(writer io.Writer) error { type HttpConn struct { net.Conn - readBuffer *alloc.Buffer + readBuffer *buf.Buffer oneTimeReader Reader oneTimeWriter Writer } @@ -143,7 +143,7 @@ type HttpAuthenticator struct { } func (v HttpAuthenticator) GetClientWriter() *HeaderWriter { - header := alloc.NewSmallBuffer() + header := buf.NewSmallBuffer() config := v.config.Request header.AppendFunc(serial.WriteString(strings.Join([]string{config.Method.GetValue(), config.PickUri(), config.GetFullVersion()}, " "))) header.AppendFunc(writeCRLF) @@ -160,7 +160,7 @@ func (v HttpAuthenticator) GetClientWriter() *HeaderWriter { } func (v HttpAuthenticator) GetServerWriter() *HeaderWriter { - header := alloc.NewSmallBuffer() + header := buf.NewSmallBuffer() config := v.config.Response header.AppendFunc(serial.WriteString(strings.Join([]string{config.GetFullVersion(), config.Status.GetCode(), config.Status.GetReason()}, " "))) header.AppendFunc(writeCRLF) diff --git a/transport/internet/headers/http/http_test.go b/transport/internet/headers/http/http_test.go index 7602a0f64..612f08e72 100644 --- a/transport/internet/headers/http/http_test.go +++ b/transport/internet/headers/http/http_test.go @@ -3,7 +3,7 @@ package http_test import ( "testing" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" "v2ray.com/core/common/serial" "v2ray.com/core/testing/assert" . "v2ray.com/core/transport/internet/headers/http" @@ -12,8 +12,8 @@ import ( func TestReaderWriter(t *testing.T) { assert := assert.On(t) - cache := alloc.NewBuffer() - b := alloc.NewLocalBuffer(256) + cache := buf.NewBuffer() + b := buf.NewLocalBuffer(256) b.AppendFunc(serial.WriteString("abcd" + ENDING)) writer := NewHeaderWriter(b) writer.Write(cache) @@ -41,7 +41,7 @@ func TestRequestHeader(t *testing.T) { }, }).(HttpAuthenticator) - cache := alloc.NewBuffer() + cache := buf.NewBuffer() err := auth.GetClientWriter().Write(cache) assert.Error(err).IsNil() diff --git a/transport/internet/headers/srtp/srtp_test.go b/transport/internet/headers/srtp/srtp_test.go index ba67933b9..5e2c0de9c 100644 --- a/transport/internet/headers/srtp/srtp_test.go +++ b/transport/internet/headers/srtp/srtp_test.go @@ -3,7 +3,7 @@ package srtp_test import ( "testing" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" "v2ray.com/core/testing/assert" . "v2ray.com/core/transport/internet/headers/srtp" ) @@ -14,7 +14,7 @@ func TestSRTPWrite(t *testing.T) { content := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g'} srtp := SRTP{} - payload := alloc.NewLocalBuffer(2048) + payload := buf.NewLocalBuffer(2048) payload.AppendFunc(srtp.Write) payload.Append(content) diff --git a/transport/internet/headers/utp/utp_test.go b/transport/internet/headers/utp/utp_test.go index eb2b4e398..87808904d 100644 --- a/transport/internet/headers/utp/utp_test.go +++ b/transport/internet/headers/utp/utp_test.go @@ -3,7 +3,7 @@ package utp_test import ( "testing" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" "v2ray.com/core/testing/assert" . "v2ray.com/core/transport/internet/headers/utp" ) @@ -14,7 +14,7 @@ func TestUTPWrite(t *testing.T) { content := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g'} utp := UTP{} - payload := alloc.NewLocalBuffer(2048) + payload := buf.NewLocalBuffer(2048) payload.AppendFunc(utp.Write) payload.Append(content) diff --git a/transport/internet/kcp/dialer.go b/transport/internet/kcp/dialer.go index dc4d00c6e..e85fc81c5 100644 --- a/transport/internet/kcp/dialer.go +++ b/transport/internet/kcp/dialer.go @@ -8,7 +8,7 @@ import ( "crypto/cipher" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" "v2ray.com/core/common/dice" "v2ray.com/core/common/errors" "v2ray.com/core/common/log" @@ -88,7 +88,7 @@ func (o *ClientConnection) ResetSecurity(header internet.PacketHeader, security } func (o *ClientConnection) Run() { - payload := alloc.NewSmallBuffer() + payload := buf.NewSmallBuffer() defer payload.Release() for { diff --git a/transport/internet/kcp/listener.go b/transport/internet/kcp/listener.go index 3ce24da4a..fb737fe9b 100644 --- a/transport/internet/kcp/listener.go +++ b/transport/internet/kcp/listener.go @@ -9,7 +9,7 @@ import ( "crypto/cipher" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" "v2ray.com/core/common/errors" "v2ray.com/core/common/log" v2net "v2ray.com/core/common/net" @@ -140,7 +140,7 @@ func NewListener(address v2net.Address, port v2net.Port, options internet.Listen return l, nil } -func (v *Listener) OnReceive(payload *alloc.Buffer, session *proxy.SessionInfo) { +func (v *Listener) OnReceive(payload *buf.Buffer, session *proxy.SessionInfo) { defer payload.Release() src := session.Source diff --git a/transport/internet/kcp/output.go b/transport/internet/kcp/output.go index 1628daefa..3f5861ff1 100644 --- a/transport/internet/kcp/output.go +++ b/transport/internet/kcp/output.go @@ -4,7 +4,7 @@ import ( "io" "sync" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" ) type SegmentWriter interface { @@ -14,7 +14,7 @@ type SegmentWriter interface { type BufferedSegmentWriter struct { sync.Mutex mtu uint32 - buffer *alloc.Buffer + buffer *buf.Buffer writer io.Writer } @@ -22,7 +22,7 @@ func NewSegmentWriter(writer io.Writer, mtu uint32) *BufferedSegmentWriter { return &BufferedSegmentWriter{ mtu: mtu, writer: writer, - buffer: alloc.NewSmallBuffer(), + buffer: buf.NewSmallBuffer(), } } diff --git a/transport/internet/kcp/receiving.go b/transport/internet/kcp/receiving.go index 19032b002..c924f4701 100644 --- a/transport/internet/kcp/receiving.go +++ b/transport/internet/kcp/receiving.go @@ -3,7 +3,7 @@ package kcp import ( "sync" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" ) type ReceivingWindow struct { @@ -145,7 +145,7 @@ func (v *AckList) Flush(current uint32, rto uint32) { type ReceivingWorker struct { sync.RWMutex conn *Connection - leftOver *alloc.Buffer + leftOver *buf.Buffer window *ReceivingWindow acklist *AckList nextNumber uint32 diff --git a/transport/internet/kcp/segment.go b/transport/internet/kcp/segment.go index e133749d8..7514888f6 100644 --- a/transport/internet/kcp/segment.go +++ b/transport/internet/kcp/segment.go @@ -2,7 +2,7 @@ package kcp import ( "v2ray.com/core/common" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" "v2ray.com/core/common/serial" ) @@ -31,7 +31,7 @@ type Segment interface { Conversation() uint16 Command() Command ByteSize() int - Bytes() alloc.BytesWriter + Bytes() buf.BytesWriter } const ( @@ -44,7 +44,7 @@ type DataSegment struct { Timestamp uint32 Number uint32 SendingNext uint32 - Data *alloc.Buffer + Data *buf.Buffer timeout uint32 transmit uint32 @@ -64,13 +64,13 @@ func (v *DataSegment) Command() Command { func (v *DataSegment) SetData(b []byte) { if v.Data == nil { - v.Data = alloc.NewSmallBuffer() + v.Data = buf.NewSmallBuffer() } v.Data.Clear() v.Data.Append(b) } -func (v *DataSegment) Bytes() alloc.BytesWriter { +func (v *DataSegment) Bytes() buf.BytesWriter { return func(b []byte) int { b = serial.Uint16ToBytes(v.Conv, b[:0]) b = append(b, byte(CommandData), byte(v.Option)) @@ -137,7 +137,7 @@ func (v *AckSegment) ByteSize() int { return 2 + 1 + 1 + 4 + 4 + 4 + 1 + int(v.Count)*4 } -func (v *AckSegment) Bytes() alloc.BytesWriter { +func (v *AckSegment) Bytes() buf.BytesWriter { return func(b []byte) int { b = serial.Uint16ToBytes(v.Conv, b[:0]) b = append(b, byte(CommandACK), byte(v.Option)) @@ -181,7 +181,7 @@ func (v *CmdOnlySegment) ByteSize() int { return 2 + 1 + 1 + 4 + 4 + 4 } -func (v *CmdOnlySegment) Bytes() alloc.BytesWriter { +func (v *CmdOnlySegment) Bytes() buf.BytesWriter { return func(b []byte) int { b = serial.Uint16ToBytes(v.Conv, b[:0]) b = append(b, byte(v.Cmd), byte(v.Option)) diff --git a/transport/internet/kcp/segment_test.go b/transport/internet/kcp/segment_test.go index 341d30541..079969308 100644 --- a/transport/internet/kcp/segment_test.go +++ b/transport/internet/kcp/segment_test.go @@ -3,7 +3,7 @@ package kcp_test import ( "testing" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" "v2ray.com/core/testing/assert" . "v2ray.com/core/transport/internet/kcp" ) @@ -19,7 +19,7 @@ func TestBadSegment(t *testing.T) { func TestDataSegment(t *testing.T) { assert := assert.On(t) - b := alloc.NewLocalBuffer(512) + b := buf.NewLocalBuffer(512) b.Append([]byte{'a', 'b', 'c', 'd'}) seg := &DataSegment{ Conv: 1, diff --git a/transport/internet/udp/hub.go b/transport/internet/udp/hub.go index bc2babdc3..c95a6c412 100644 --- a/transport/internet/udp/hub.go +++ b/transport/internet/udp/hub.go @@ -4,7 +4,7 @@ import ( "net" "sync" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" "v2ray.com/core/common/dice" "v2ray.com/core/common/log" v2net "v2ray.com/core/common/net" @@ -14,11 +14,11 @@ import ( ) type UDPPayload struct { - payload *alloc.Buffer + payload *buf.Buffer session *proxy.SessionInfo } -type UDPPayloadHandler func(*alloc.Buffer, *proxy.SessionInfo) +type UDPPayloadHandler func(*buf.Buffer, *proxy.SessionInfo) type UDPPayloadQueue struct { queue []chan UDPPayload @@ -135,7 +135,7 @@ func (v *UDPHub) start() { oobBytes := make([]byte, 256) for v.Running() { - buffer := alloc.NewSmallBuffer() + buffer := buf.NewSmallBuffer() var noob int var addr *net.UDPAddr var err error diff --git a/transport/internet/udp/hub_linux_test.go b/transport/internet/udp/hub_linux_test.go index eaf11a18a..36b71b01a 100644 --- a/transport/internet/udp/hub_linux_test.go +++ b/transport/internet/udp/hub_linux_test.go @@ -7,7 +7,7 @@ import ( "syscall" "testing" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" v2net "v2ray.com/core/common/net" "v2ray.com/core/proxy" "v2ray.com/core/testing/assert" @@ -23,7 +23,7 @@ func TestHubSocksOption(t *testing.T) { } hub, err := ListenUDP(v2net.LocalHostIP, v2net.Port(0), ListenOption{ - Callback: func(*alloc.Buffer, *proxy.SessionInfo) {}, + Callback: func(*buf.Buffer, *proxy.SessionInfo) {}, ReceiveOriginalDest: true, }) assert.Error(err).IsNil() diff --git a/transport/internet/udp/udp_server.go b/transport/internet/udp/udp_server.go index febfe44ac..200f1af29 100644 --- a/transport/internet/udp/udp_server.go +++ b/transport/internet/udp/udp_server.go @@ -5,14 +5,14 @@ import ( "time" "v2ray.com/core/app/dispatcher" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" "v2ray.com/core/common/log" v2net "v2ray.com/core/common/net" "v2ray.com/core/proxy" "v2ray.com/core/transport/ray" ) -type UDPResponseCallback func(destination v2net.Destination, payload *alloc.Buffer) +type UDPResponseCallback func(destination v2net.Destination, payload *buf.Buffer) type TimedInboundRay struct { name string @@ -111,7 +111,7 @@ func (v *UDPServer) RemoveRay(name string) { delete(v.conns, name) } -func (v *UDPServer) locateExistingAndDispatch(name string, payload *alloc.Buffer) bool { +func (v *UDPServer) locateExistingAndDispatch(name string, payload *buf.Buffer) bool { log.Debug("UDP Server: Locating existing connection for ", name) v.RLock() defer v.RUnlock() @@ -130,7 +130,7 @@ func (v *UDPServer) locateExistingAndDispatch(name string, payload *alloc.Buffer return false } -func (v *UDPServer) Dispatch(session *proxy.SessionInfo, payload *alloc.Buffer, callback UDPResponseCallback) { +func (v *UDPServer) Dispatch(session *proxy.SessionInfo, payload *buf.Buffer, callback UDPResponseCallback) { source := session.Source destination := session.Destination diff --git a/transport/ray/direct.go b/transport/ray/direct.go index a4e446b76..b35412105 100644 --- a/transport/ray/direct.go +++ b/transport/ray/direct.go @@ -5,7 +5,7 @@ import ( "sync" "time" - "v2ray.com/core/common/alloc" + "v2ray.com/core/common/buf" ) const ( @@ -44,16 +44,16 @@ func (v *directRay) InboundOutput() InputStream { type Stream struct { access sync.RWMutex closed bool - buffer chan *alloc.Buffer + buffer chan *buf.Buffer } func NewStream() *Stream { return &Stream{ - buffer: make(chan *alloc.Buffer, bufferSize), + buffer: make(chan *buf.Buffer, bufferSize), } } -func (v *Stream) Read() (*alloc.Buffer, error) { +func (v *Stream) Read() (*buf.Buffer, error) { if v.buffer == nil { return nil, io.EOF } @@ -71,7 +71,7 @@ func (v *Stream) Read() (*alloc.Buffer, error) { return result, nil } -func (v *Stream) Write(data *alloc.Buffer) error { +func (v *Stream) Write(data *buf.Buffer) error { for !v.closed { err := v.TryWriteOnce(data) if err != io.ErrNoProgress { @@ -81,7 +81,7 @@ func (v *Stream) Write(data *alloc.Buffer) error { return io.ErrClosedPipe } -func (v *Stream) TryWriteOnce(data *alloc.Buffer) error { +func (v *Stream) TryWriteOnce(data *buf.Buffer) error { v.access.RLock() defer v.access.RUnlock() if v.closed {