1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-12 23:18:36 -04:00

rename alloc to buf

This commit is contained in:
Darien Raymond 2016-12-09 11:35:27 +01:00
parent cd24d6f2d0
commit 7a80409e30
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
60 changed files with 199 additions and 196 deletions

View File

@ -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)
}

View File

@ -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()

View File

@ -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)
}

View File

@ -1,5 +1,5 @@
// Package alloc provides a light-weight memory allocation mechanism.
package alloc
package buf
import (
"io"

View File

@ -1,4 +1,4 @@
package alloc
package buf
import (
"os"

View File

@ -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"
)

View File

@ -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,
}

View File

@ -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,
}
}

View File

@ -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()

View File

@ -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,
}
}

View File

@ -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()

View File

@ -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:]

View File

@ -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
}

View File

@ -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

View File

@ -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()

View File

@ -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())

View File

@ -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()...)

View File

@ -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()

View File

@ -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

View File

@ -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)

View File

@ -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())

View File

@ -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)
}

View File

@ -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))

View File

@ -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()

View File

@ -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()

View File

@ -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)

View File

@ -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)
}

View File

@ -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()

View File

@ -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:])

View File

@ -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()

View File

@ -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

View File

@ -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()

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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()

View File

@ -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
}

View File

@ -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()

View File

@ -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()