From bc80cc72c7e0912a64bcb912930f3dce85f03a7a Mon Sep 17 00:00:00 2001
From: Darien Raymond <admin@v2ray.com>
Date: Sat, 10 Dec 2016 01:05:55 +0100
Subject: [PATCH] pading compatibitlity

---
 proxy/vmess/encoding/client.go | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/proxy/vmess/encoding/client.go b/proxy/vmess/encoding/client.go
index 24329ed0..e655145b 100644
--- a/proxy/vmess/encoding/client.go
+++ b/proxy/vmess/encoding/client.go
@@ -74,6 +74,10 @@ func (v *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, writ
 	buffer = append(buffer, v.requestBodyKey...)
 	buffer = append(buffer, v.responseHeader, byte(header.Option))
 	padingLen := dice.Roll(16)
+	if header.Security.Is(protocol.SecurityType_LEGACY) {
+		// Disable padding in legacy mode for a smooth transition.
+		padingLen = 0
+	}
 	security := byte(padingLen<<4) | byte(header.Security)
 	buffer = append(buffer, security, byte(0), byte(header.Command))
 	buffer = header.Port.Bytes(buffer)
@@ -90,9 +94,11 @@ func (v *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, writ
 		buffer = append(buffer, header.Address.Domain()...)
 	}
 
-	pading := make([]byte, padingLen)
-	rand.Read(pading)
-	buffer = append(buffer, pading...)
+	if padingLen > 0 {
+		pading := make([]byte, padingLen)
+		rand.Read(pading)
+		buffer = append(buffer, pading...)
+	}
 
 	fnv1a := fnv.New32a()
 	fnv1a.Write(buffer)