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
|
return
|
||||||
}
|
}
|
||||||
log.Trace("client hello sent successfully")
|
log.Trace("client hello sent successfully")
|
||||||
tls.TLSConn = &common.TLSConn{Conn: rawConn}
|
tls.TLSConn = common.NewTLSConn(rawConn)
|
||||||
|
|
||||||
buf := make([]byte, 1024)
|
buf := make([]byte, 1024)
|
||||||
log.Trace("waiting for ServerHello")
|
log.Trace("waiting for ServerHello")
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -37,6 +38,15 @@ func AddRecordLayer(input []byte, typ byte, ver uint16) []byte {
|
||||||
|
|
||||||
type TLSConn struct {
|
type TLSConn struct {
|
||||||
net.Conn
|
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 {
|
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) {
|
func (tls *TLSConn) Write(in []byte) (n int, err error) {
|
||||||
// TODO: write record layer directly first?
|
msgLen := len(in)
|
||||||
toWrite := AddRecordLayer(in, ApplicationData, VersionTLS13)
|
tls.writeM.Lock()
|
||||||
n, err = tls.Conn.Write(toWrite)
|
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
|
return n - recordLayerLength, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -51,8 +51,8 @@ func makeSessionPair(numConn int) (*Session, *Session, []*connPair) {
|
||||||
paris := make([]*connPair, numConn)
|
paris := make([]*connPair, numConn)
|
||||||
for i := 0; i < numConn; i++ {
|
for i := 0; i < numConn; i++ {
|
||||||
c, s := connutil.AsyncPipe()
|
c, s := connutil.AsyncPipe()
|
||||||
clientConn := &common.TLSConn{Conn: c}
|
clientConn := common.NewTLSConn(c)
|
||||||
serverConn := &common.TLSConn{Conn: s}
|
serverConn := common.NewTLSConn(s)
|
||||||
paris[i] = &connPair{
|
paris[i] = &connPair{
|
||||||
clientConn: clientConn,
|
clientConn: clientConn,
|
||||||
serverConn: serverConn,
|
serverConn: serverConn,
|
||||||
|
|
|
||||||
|
|
@ -132,8 +132,8 @@ func TestStream_WriteSync(t *testing.T) {
|
||||||
clientSesh := setupSesh(false, sessionKey, E_METHOD_PLAIN)
|
clientSesh := setupSesh(false, sessionKey, E_METHOD_PLAIN)
|
||||||
serverSesh := setupSesh(false, sessionKey, E_METHOD_PLAIN)
|
serverSesh := setupSesh(false, sessionKey, E_METHOD_PLAIN)
|
||||||
w, r := connutil.AsyncPipe()
|
w, r := connutil.AsyncPipe()
|
||||||
clientSesh.AddConnection(&common.TLSConn{Conn: w})
|
clientSesh.AddConnection(common.NewTLSConn(w))
|
||||||
serverSesh.AddConnection(&common.TLSConn{Conn: r})
|
serverSesh.AddConnection(common.NewTLSConn(r))
|
||||||
testData := make([]byte, payloadLen)
|
testData := make([]byte, payloadLen)
|
||||||
rand.Read(testData)
|
rand.Read(testData)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -65,7 +65,7 @@ func (TLS) makeResponder(clientHelloSessionId []byte, sharedSecret [32]byte) Res
|
||||||
originalConn.Close()
|
originalConn.Close()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
preparedConn = &common.TLSConn{Conn: originalConn}
|
preparedConn = common.NewTLSConn(originalConn)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return respond
|
return respond
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue