From f5715c4f4217054a2770a21ab285a71d9e16ad1d Mon Sep 17 00:00:00 2001 From: beyondkmp Date: Sat, 1 Dec 2018 09:32:02 +0800 Subject: [PATCH] Fix: chunk size limit in tls obfs (#54) * Fix: add chunkSize limit in TLSObfs * Chore: add length var for len(b) --- component/simple-obfs/tls.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/component/simple-obfs/tls.go b/component/simple-obfs/tls.go index e725944f1..b763eefb4 100644 --- a/component/simple-obfs/tls.go +++ b/component/simple-obfs/tls.go @@ -14,6 +14,10 @@ func init() { rand.Seed(time.Now().Unix()) } +const ( + chunkSize = 1 << 14 // 2 ** 14 == 16 * 1024 +) + var bufPool = sync.Pool{New: func() interface{} { return make([]byte, 2048) }} // TLSObfs is shadowsocks tls simple-obfs implementation @@ -75,8 +79,23 @@ func (to *TLSObfs) Read(b []byte) (int, error) { // type + ver = 3 return to.read(b, 3) } - func (to *TLSObfs) Write(b []byte) (int, error) { + length := len(b) + for i := 0; i < length; i += chunkSize { + end := i + chunkSize + if end > length { + end = length + } + + n, err := to.write(b[i:end]) + if err != nil { + return n, err + } + } + return length, nil +} + +func (to *TLSObfs) write(b []byte) (int, error) { if to.firstRequest { helloMsg := makeClientHelloMsg(b, to.server) _, err := to.Conn.Write(helloMsg)