Improve v3 writer

This commit is contained in:
世界 2023-05-31 10:57:48 +08:00
parent 2b3732dc6d
commit 74b210a0e6
No known key found for this signature in database
GPG key ID: CD109927C34A63C4

View file

@ -17,10 +17,11 @@ import (
type verifiedConn struct {
net.Conn
writer N.VectorisedWriter
hmacAdd hash.Hash
hmacVerify hash.Hash
hmacIgnore hash.Hash
writer N.ExtendedWriter
vectorisedWriter N.VectorisedWriter
hmacAdd hash.Hash
hmacVerify hash.Hash
hmacIgnore hash.Hash
buffer *buf.Buffer
}
@ -32,11 +33,12 @@ func newVerifiedConn(
hmacIgnore hash.Hash,
) *verifiedConn {
return &verifiedConn{
Conn: conn,
writer: bufio.NewVectorisedWriter(conn),
hmacAdd: hmacAdd,
hmacVerify: hmacVerify,
hmacIgnore: hmacIgnore,
Conn: conn,
writer: bufio.NewExtendedWriter(conn),
vectorisedWriter: bufio.NewVectorisedWriter(conn),
hmacAdd: hmacAdd,
hmacVerify: hmacVerify,
hmacIgnore: hmacIgnore,
}
}
@ -121,13 +123,27 @@ 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.writer, [][]byte{common.Dup(header[:]), p})
_, err = bufio.WriteVectorised(c.vectorisedWriter, [][]byte{common.Dup(header[:]), p})
if err == nil {
n = len(p)
}
return
}
func (c *verifiedConn) WriteBuffer(buffer *buf.Buffer) error {
c.hmacAdd.Write(buffer.Bytes())
dateLen := buffer.Len()
header := buffer.ExtendHeader(tlsHmacHeaderSize)
header[0] = applicationData
header[1] = 3
header[2] = 3
binary.BigEndian.PutUint16(header[3:tlsHeaderSize], hmacSize+uint16(dateLen))
hmacHash := c.hmacAdd.Sum(nil)[:hmacSize]
c.hmacAdd.Write(hmacHash)
copy(header[tlsHeaderSize:], hmacHash)
return c.writer.WriteBuffer(buffer)
}
func (c *verifiedConn) WriteVectorised(buffers []*buf.Buffer) error {
var header [tlsHmacHeaderSize]byte
header[0] = applicationData
@ -139,7 +155,11 @@ func (c *verifiedConn) WriteVectorised(buffers []*buf.Buffer) error {
}
c.hmacAdd.Write(c.hmacAdd.Sum(nil)[:hmacSize])
copy(header[tlsHeaderSize:], c.hmacAdd.Sum(nil)[:hmacSize])
return c.writer.WriteVectorised(append([]*buf.Buffer{buf.As(header[:])}, buffers...))
return c.vectorisedWriter.WriteVectorised(append([]*buf.Buffer{buf.As(header[:])}, buffers...))
}
func (c *verifiedConn) FrontHeadroom() int {
return tlsHmacHeaderSize
}
func (c *verifiedConn) NeedAdditionalReadDeadline() bool {