From 32e0e6e484eed429df3c334de2f431c7ecc1b2ba Mon Sep 17 00:00:00 2001 From: Chinsyo Date: Wed, 18 Nov 2020 04:23:30 +0800 Subject: [PATCH] Some minor fix (#430) * fix typo pading to padding * reorder common/buf/buffer.go code layout * delete unused common/stack Co-authored-by: Chinsyo --- common/buf/buffer.go | 34 +++++++++++++++++----------------- common/stack/bytes.go | 11 ----------- proxy/vmess/encoding/client.go | 8 ++++---- proxy/vmess/encoding/server.go | 6 +++--- 4 files changed, 24 insertions(+), 35 deletions(-) delete mode 100644 common/stack/bytes.go diff --git a/common/buf/buffer.go b/common/buf/buffer.go index 17124ce7e..a6cd4bbb5 100644 --- a/common/buf/buffer.go +++ b/common/buf/buffer.go @@ -11,6 +11,8 @@ const ( Size = 2048 ) +var pool = bytespool.GetPool(Size) + // Buffer is a recyclable allocation of a byte array. Buffer.Release() recycles // the buffer into an internal buffer pool, in order to recreate a buffer more // quickly. @@ -20,6 +22,21 @@ type Buffer struct { end int32 } +// New creates a Buffer with 0 length and 2K capacity. +func New() *Buffer { + return &Buffer{ + v: pool.Get().([]byte), + } +} + +// StackNew creates a new Buffer object on stack. +// This method is for buffers that is released in the same function. +func StackNew() Buffer { + return Buffer{ + v: pool.Get().([]byte), + } +} + // Release recycles the buffer into an internal buffer pool. func (b *Buffer) Release() { if b == nil || b.v == nil { @@ -193,20 +210,3 @@ func (b *Buffer) ReadFullFrom(reader io.Reader, size int32) (int64, error) { func (b *Buffer) String() string { return string(b.Bytes()) } - -var pool = bytespool.GetPool(Size) - -// New creates a Buffer with 0 length and 2K capacity. -func New() *Buffer { - return &Buffer{ - v: pool.Get().([]byte), - } -} - -// StackNew creates a new Buffer object on stack. -// This method is for buffers that is released in the same function. -func StackNew() Buffer { - return Buffer{ - v: pool.Get().([]byte), - } -} diff --git a/common/stack/bytes.go b/common/stack/bytes.go deleted file mode 100644 index 1e4719eee..000000000 --- a/common/stack/bytes.go +++ /dev/null @@ -1,11 +0,0 @@ -package stack - -// TwoBytes is a [2]byte which is always allocated on stack. -// -//go:notinheap -type TwoBytes [2]byte - -// EightBytes is a [8]byte which is always allocated on stack. -// -//go:notinheap -type EightBytes [8]byte diff --git a/proxy/vmess/encoding/client.go b/proxy/vmess/encoding/client.go index 564ba51e0..3e4ad0474 100644 --- a/proxy/vmess/encoding/client.go +++ b/proxy/vmess/encoding/client.go @@ -90,8 +90,8 @@ func (c *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, writ common.Must(buffer.WriteByte(c.responseHeader)) common.Must(buffer.WriteByte(byte(header.Option))) - padingLen := dice.Roll(16) - security := byte(padingLen<<4) | byte(header.Security) + paddingLen := dice.Roll(16) + security := byte(paddingLen<<4) | byte(header.Security) common.Must2(buffer.Write([]byte{security, byte(0), byte(header.Command)})) if header.Command != protocol.RequestCommandMux { @@ -100,8 +100,8 @@ func (c *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, writ } } - if padingLen > 0 { - common.Must2(buffer.ReadFullFrom(rand.Reader, int32(padingLen))) + if paddingLen > 0 { + common.Must2(buffer.ReadFullFrom(rand.Reader, int32(paddingLen))) } { diff --git a/proxy/vmess/encoding/server.go b/proxy/vmess/encoding/server.go index 6dbc2d696..88876625e 100644 --- a/proxy/vmess/encoding/server.go +++ b/proxy/vmess/encoding/server.go @@ -233,7 +233,7 @@ func (s *ServerSession) DecodeRequestHeader(reader io.Reader) (*protocol.Request s.responseHeader = buffer.Byte(33) // 1 byte request.Option = bitmask.Byte(buffer.Byte(34)) // 1 byte - padingLen := int(buffer.Byte(35) >> 4) + paddingLen := int(buffer.Byte(35) >> 4) request.Security = parseSecurityType(buffer.Byte(35) & 0x0F) // 1 bytes reserved request.Command = protocol.RequestCommand(buffer.Byte(37)) @@ -250,8 +250,8 @@ func (s *ServerSession) DecodeRequestHeader(reader io.Reader) (*protocol.Request } } - if padingLen > 0 { - if _, err := buffer.ReadFullFrom(decryptor, int32(padingLen)); err != nil { + if paddingLen > 0 { + if _, err := buffer.ReadFullFrom(decryptor, int32(paddingLen)); err != nil { if !s.isAEADRequest { burnErr := s.userValidator.BurnTaintFuse(fixedSizeAuthID[:]) if burnErr != nil {