From 93bbad3057e41ad5ae0a7fe17bc78ee6e5c05f69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Mon, 3 Jul 2023 21:25:09 +0800 Subject: [PATCH] Remove stack buffer usage --- go.mod | 2 +- go.sum | 4 ++-- v2_conn.go | 10 +++------- v3_conn.go | 2 +- 4 files changed, 7 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index a1c7b29..f9e8938 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/sagernet/sing-shadowtls go 1.18 require ( - github.com/sagernet/sing v0.2.5 + github.com/sagernet/sing v0.2.8-0.20230703002104-c68251b6d059 golang.org/x/crypto v0.10.0 golang.org/x/sys v0.9.0 ) diff --git a/go.sum b/go.sum index 1f9a0a5..b5cf8cb 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/sagernet/sing v0.2.5 h1:N8sUluR8GZvR9DqUiH3FA3vBb4m/EDdOVTYUrDzJvmY= -github.com/sagernet/sing v0.2.5/go.mod h1:Ta8nHnDLAwqySzKhGoKk4ZIB+vJ3GTKj7UPrWYvM+4w= +github.com/sagernet/sing v0.2.8-0.20230703002104-c68251b6d059 h1:nqTONy58Gq1mdoGx9GX+GKXdSTwOPTKF/DXK+Wn4B+A= +github.com/sagernet/sing v0.2.8-0.20230703002104-c68251b6d059/go.mod h1:Ta8nHnDLAwqySzKhGoKk4ZIB+vJ3GTKj7UPrWYvM+4w= golang.org/x/crypto v0.10.0 h1:LKqV2xt9+kDzSTfOhx4FrkEBcMrAgHSYgzywV9zcGmM= golang.org/x/crypto v0.10.0/go.mod h1:o4eNf7Ede1fv+hwOwZsTHl9EsPFO6q6ZvYR8vYfY45I= golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s= diff --git a/v2_conn.go b/v2_conn.go index f334016..5f36345 100644 --- a/v2_conn.go +++ b/v2_conn.go @@ -6,7 +6,6 @@ import ( "io" "net" - "github.com/sagernet/sing/common" "github.com/sagernet/sing/common/buf" "github.com/sagernet/sing/common/bufio" E "github.com/sagernet/sing/common/exceptions" @@ -36,7 +35,7 @@ func (c *shadowConn) Read(p []byte) (n int, err error) { return } var tlsHeader [5]byte - _, err = io.ReadFull(c.Conn, common.Dup(tlsHeader[:])) + _, err = io.ReadFull(c.Conn, tlsHeader[:]) if err != nil { return } @@ -58,13 +57,11 @@ func (c *shadowConn) Read(p []byte) (n int, err error) { func (c *shadowConn) Write(p []byte) (n int, err error) { var header [tlsHeaderSize]byte - defer common.KeepAlive(header) header[0] = 23 for len(p) > 16384 { binary.BigEndian.PutUint16(header[1:3], tls.VersionTLS12) binary.BigEndian.PutUint16(header[3:5], uint16(16384)) - _, err = bufio.WriteVectorised(c.writer, [][]byte{common.Dup(header[:]), p[:16384]}) - common.KeepAlive(header) + _, err = bufio.WriteVectorised(c.writer, [][]byte{header[:], p[:16384]}) if err != nil { return } @@ -73,7 +70,7 @@ func (c *shadowConn) Write(p []byte) (n int, err error) { } binary.BigEndian.PutUint16(header[1:3], tls.VersionTLS12) binary.BigEndian.PutUint16(header[3:5], uint16(len(p))) - _, err = bufio.WriteVectorised(c.writer, [][]byte{common.Dup(header[:]), p}) + _, err = bufio.WriteVectorised(c.writer, [][]byte{header[:], p}) if err == nil { n += len(p) } @@ -82,7 +79,6 @@ func (c *shadowConn) Write(p []byte) (n int, err error) { func (c *shadowConn) WriteVectorised(buffers []*buf.Buffer) error { var header [tlsHeaderSize]byte - defer common.KeepAlive(header) header[0] = 23 dataLen := buf.LenMulti(buffers) binary.BigEndian.PutUint16(header[1:3], tls.VersionTLS12) diff --git a/v3_conn.go b/v3_conn.go index 36b5d9a..0d68c16 100644 --- a/v3_conn.go +++ b/v3_conn.go @@ -123,7 +123,7 @@ func (c *verifiedConn) write(p []byte) (n int, err error) { hmacHash := c.hmacAdd.Sum(nil)[:hmacSize] c.hmacAdd.Write(hmacHash) copy(header[tlsHeaderSize:], hmacHash) - _, err = bufio.WriteVectorised(c.vectorisedWriter, [][]byte{common.Dup(header[:]), p}) + _, err = bufio.WriteVectorised(c.vectorisedWriter, [][]byte{header[:], p}) if err == nil { n = len(p) }