mirror of
https://github.com/SagerNet/sing-shadowtls.git
synced 2025-04-01 19:27:36 +03:00
Improve v3 writer
This commit is contained in:
parent
2b3732dc6d
commit
74b210a0e6
1 changed files with 31 additions and 11 deletions
42
v3_conn.go
42
v3_conn.go
|
@ -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 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue