Browse Source

Some minor fix (#430)

* fix typo pading to padding

* reorder common/buf/buffer.go code layout

* delete unused common/stack

Co-authored-by: Chinsyo <chinsyo@sina.cn>
Chinsyo 5 years ago
parent
commit
32e0e6e484
4 changed files with 24 additions and 35 deletions
  1. 17 17
      common/buf/buffer.go
  2. 0 11
      common/stack/bytes.go
  3. 4 4
      proxy/vmess/encoding/client.go
  4. 3 3
      proxy/vmess/encoding/server.go

+ 17 - 17
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),
-	}
-}

+ 0 - 11
common/stack/bytes.go

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

+ 4 - 4
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)))
 	}
 
 	{

+ 3 - 3
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 {