mirror of https://github.com/cbeuw/Cloak
Use a pre-made buffer for TLSConn.Write
This commit is contained in:
parent
8b0a7425f8
commit
86e2935607
|
|
@ -71,7 +71,7 @@ func (tls *DirectTLS) Handshake(rawConn net.Conn, authInfo AuthInfo) (sessionKey
|
|||
return
|
||||
}
|
||||
log.Trace("client hello sent successfully")
|
||||
tls.TLSConn = &common.TLSConn{Conn: rawConn}
|
||||
tls.TLSConn = common.NewTLSConn(rawConn)
|
||||
|
||||
buf := make([]byte, 1024)
|
||||
log.Trace("waiting for ServerHello")
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import (
|
|||
"encoding/binary"
|
||||
"io"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
|
@ -37,6 +38,15 @@ func AddRecordLayer(input []byte, typ byte, ver uint16) []byte {
|
|||
|
||||
type TLSConn struct {
|
||||
net.Conn
|
||||
writeM sync.Mutex
|
||||
writeBuf []byte
|
||||
}
|
||||
|
||||
func NewTLSConn(conn net.Conn) *TLSConn {
|
||||
return &TLSConn{
|
||||
Conn: conn,
|
||||
writeBuf: make([]byte, 15000),
|
||||
}
|
||||
}
|
||||
|
||||
func (tls *TLSConn) LocalAddr() net.Addr {
|
||||
|
|
@ -79,9 +89,16 @@ func (tls *TLSConn) Read(buffer []byte) (n int, err error) {
|
|||
}
|
||||
|
||||
func (tls *TLSConn) Write(in []byte) (n int, err error) {
|
||||
// TODO: write record layer directly first?
|
||||
toWrite := AddRecordLayer(in, ApplicationData, VersionTLS13)
|
||||
n, err = tls.Conn.Write(toWrite)
|
||||
msgLen := len(in)
|
||||
tls.writeM.Lock()
|
||||
tls.writeBuf = append(tls.writeBuf[:5], in...)
|
||||
tls.writeBuf[0] = ApplicationData
|
||||
tls.writeBuf[1] = byte(VersionTLS13 >> 8)
|
||||
tls.writeBuf[2] = byte(VersionTLS13 & 0xFF)
|
||||
tls.writeBuf[3] = byte(msgLen >> 8)
|
||||
tls.writeBuf[4] = byte(msgLen & 0xFF)
|
||||
n, err = tls.Conn.Write(tls.writeBuf[:recordLayerLength+msgLen])
|
||||
tls.writeM.Unlock()
|
||||
return n - recordLayerLength, err
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -51,8 +51,8 @@ func makeSessionPair(numConn int) (*Session, *Session, []*connPair) {
|
|||
paris := make([]*connPair, numConn)
|
||||
for i := 0; i < numConn; i++ {
|
||||
c, s := connutil.AsyncPipe()
|
||||
clientConn := &common.TLSConn{Conn: c}
|
||||
serverConn := &common.TLSConn{Conn: s}
|
||||
clientConn := common.NewTLSConn(c)
|
||||
serverConn := common.NewTLSConn(s)
|
||||
paris[i] = &connPair{
|
||||
clientConn: clientConn,
|
||||
serverConn: serverConn,
|
||||
|
|
|
|||
|
|
@ -132,8 +132,8 @@ func TestStream_WriteSync(t *testing.T) {
|
|||
clientSesh := setupSesh(false, sessionKey, E_METHOD_PLAIN)
|
||||
serverSesh := setupSesh(false, sessionKey, E_METHOD_PLAIN)
|
||||
w, r := connutil.AsyncPipe()
|
||||
clientSesh.AddConnection(&common.TLSConn{Conn: w})
|
||||
serverSesh.AddConnection(&common.TLSConn{Conn: r})
|
||||
clientSesh.AddConnection(common.NewTLSConn(w))
|
||||
serverSesh.AddConnection(common.NewTLSConn(r))
|
||||
testData := make([]byte, payloadLen)
|
||||
rand.Read(testData)
|
||||
|
||||
|
|
|
|||
|
|
@ -65,7 +65,7 @@ func (TLS) makeResponder(clientHelloSessionId []byte, sharedSecret [32]byte) Res
|
|||
originalConn.Close()
|
||||
return
|
||||
}
|
||||
preparedConn = &common.TLSConn{Conn: originalConn}
|
||||
preparedConn = common.NewTLSConn(originalConn)
|
||||
return
|
||||
}
|
||||
return respond
|
||||
|
|
|
|||
Loading…
Reference in New Issue