Simplify ReadTLS

This commit is contained in:
Andy Wang 2020-04-08 16:31:55 +01:00
parent 43ae82ec0e
commit db9b848053

View File

@ -5,10 +5,8 @@ import (
"crypto/cipher" "crypto/cipher"
"crypto/rand" "crypto/rand"
"encoding/binary" "encoding/binary"
"errors"
"io" "io"
"net" "net"
"strconv"
"time" "time"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -74,28 +72,11 @@ func ReadTLS(conn net.Conn, buffer []byte) (n int, err error) {
dataLength := int(binary.BigEndian.Uint16(buffer[3:5])) dataLength := int(binary.BigEndian.Uint16(buffer[3:5]))
if dataLength > len(buffer) { if dataLength > len(buffer) {
err = errors.New("Reading TLS message: message size greater than buffer. message size: " + strconv.Itoa(dataLength)) err = io.ErrShortBuffer
return return
} }
left := dataLength n, err = io.ReadFull(conn, buffer[5:dataLength+5])
readPtr := 5 return n + 5, err
for left != 0 {
// If left > buffer size (i.e. our message got segmented), the entire MTU is read
// if left = buffer size, the entire buffer is all there left to read
// if left < buffer size (i.e. multiple messages came together),
// only the message we want is read
i, err := conn.Read(buffer[readPtr : readPtr+left])
if err != nil {
return i, err
}
left -= i
readPtr += i
}
n = 5 + dataLength
return
} }
// AddRecordLayer adds record layer to data // AddRecordLayer adds record layer to data