Remove stack buffer usage

This commit is contained in:
世界 2023-07-03 21:25:09 +08:00
parent c2ae42ea12
commit 93bbad3057
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
4 changed files with 7 additions and 11 deletions

2
go.mod
View file

@ -3,7 +3,7 @@ module github.com/sagernet/sing-shadowtls
go 1.18 go 1.18
require ( 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/crypto v0.10.0
golang.org/x/sys v0.9.0 golang.org/x/sys v0.9.0
) )

4
go.sum
View file

@ -1,5 +1,5 @@
github.com/sagernet/sing v0.2.5 h1:N8sUluR8GZvR9DqUiH3FA3vBb4m/EDdOVTYUrDzJvmY= github.com/sagernet/sing v0.2.8-0.20230703002104-c68251b6d059 h1:nqTONy58Gq1mdoGx9GX+GKXdSTwOPTKF/DXK+Wn4B+A=
github.com/sagernet/sing v0.2.5/go.mod h1:Ta8nHnDLAwqySzKhGoKk4ZIB+vJ3GTKj7UPrWYvM+4w= 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 h1:LKqV2xt9+kDzSTfOhx4FrkEBcMrAgHSYgzywV9zcGmM=
golang.org/x/crypto v0.10.0/go.mod h1:o4eNf7Ede1fv+hwOwZsTHl9EsPFO6q6ZvYR8vYfY45I= golang.org/x/crypto v0.10.0/go.mod h1:o4eNf7Ede1fv+hwOwZsTHl9EsPFO6q6ZvYR8vYfY45I=
golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s= golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s=

View file

@ -6,7 +6,6 @@ import (
"io" "io"
"net" "net"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf" "github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/bufio" "github.com/sagernet/sing/common/bufio"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
@ -36,7 +35,7 @@ func (c *shadowConn) Read(p []byte) (n int, err error) {
return return
} }
var tlsHeader [5]byte var tlsHeader [5]byte
_, err = io.ReadFull(c.Conn, common.Dup(tlsHeader[:])) _, err = io.ReadFull(c.Conn, tlsHeader[:])
if err != nil { if err != nil {
return 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) { func (c *shadowConn) Write(p []byte) (n int, err error) {
var header [tlsHeaderSize]byte var header [tlsHeaderSize]byte
defer common.KeepAlive(header)
header[0] = 23 header[0] = 23
for len(p) > 16384 { for len(p) > 16384 {
binary.BigEndian.PutUint16(header[1:3], tls.VersionTLS12) binary.BigEndian.PutUint16(header[1:3], tls.VersionTLS12)
binary.BigEndian.PutUint16(header[3:5], uint16(16384)) binary.BigEndian.PutUint16(header[3:5], uint16(16384))
_, err = bufio.WriteVectorised(c.writer, [][]byte{common.Dup(header[:]), p[:16384]}) _, err = bufio.WriteVectorised(c.writer, [][]byte{header[:], p[:16384]})
common.KeepAlive(header)
if err != nil { if err != nil {
return 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[1:3], tls.VersionTLS12)
binary.BigEndian.PutUint16(header[3:5], uint16(len(p))) 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 { if err == nil {
n += len(p) 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 { func (c *shadowConn) WriteVectorised(buffers []*buf.Buffer) error {
var header [tlsHeaderSize]byte var header [tlsHeaderSize]byte
defer common.KeepAlive(header)
header[0] = 23 header[0] = 23
dataLen := buf.LenMulti(buffers) dataLen := buf.LenMulti(buffers)
binary.BigEndian.PutUint16(header[1:3], tls.VersionTLS12) binary.BigEndian.PutUint16(header[1:3], tls.VersionTLS12)

View file

@ -123,7 +123,7 @@ func (c *verifiedConn) write(p []byte) (n int, err error) {
hmacHash := c.hmacAdd.Sum(nil)[:hmacSize] hmacHash := c.hmacAdd.Sum(nil)[:hmacSize]
c.hmacAdd.Write(hmacHash) c.hmacAdd.Write(hmacHash)
copy(header[tlsHeaderSize:], 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 { if err == nil {
n = len(p) n = len(p)
} }