Fix shadowtls conn

This commit is contained in:
世界 2022-10-20 12:27:23 +08:00
parent f3e1d1defc
commit 43d5b8598b
No known key found for this signature in database
GPG key ID: CD109927C34A63C4

View file

@ -9,24 +9,22 @@ import (
"github.com/sagernet/sing/common" "github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf" "github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/bufio" "github.com/sagernet/sing/common/bufio"
E "github.com/sagernet/sing/common/exceptions"
N "github.com/sagernet/sing/common/network" N "github.com/sagernet/sing/common/network"
) )
var ( var _ N.VectorisedWriter = (*Conn)(nil)
_ N.ExtendedConn = (*Conn)(nil)
_ N.VectorisedWriter = (*Conn)(nil)
)
type Conn struct { type Conn struct {
N.ExtendedConn net.Conn
writer N.VectorisedWriter writer N.VectorisedWriter
readRemaining int readRemaining int
} }
func NewConn(conn net.Conn) *Conn { func NewConn(conn net.Conn) *Conn {
return &Conn{ return &Conn{
ExtendedConn: bufio.NewExtendedConn(conn), Conn: conn,
writer: bufio.NewVectorisedWriter(conn), writer: bufio.NewVectorisedWriter(conn),
} }
} }
@ -35,21 +33,24 @@ func (c *Conn) Read(p []byte) (n int, err error) {
if len(p) > c.readRemaining { if len(p) > c.readRemaining {
p = p[:c.readRemaining] p = p[:c.readRemaining]
} }
n, err = c.ExtendedConn.Read(p) n, err = c.Conn.Read(p)
c.readRemaining -= n c.readRemaining -= n
return return
} }
var tlsHeader [5]byte var tlsHeader [5]byte
_, err = io.ReadFull(c.ExtendedConn, common.Dup(tlsHeader[:])) _, err = io.ReadFull(c.Conn, common.Dup(tlsHeader[:]))
if err != nil { if err != nil {
return return
} }
length := int(binary.BigEndian.Uint16(tlsHeader[3:5])) length := int(binary.BigEndian.Uint16(tlsHeader[3:5]))
if tlsHeader[0] != 23 {
return 0, E.New("unexpected TLS record type: ", tlsHeader[0])
}
readLen := len(p) readLen := len(p)
if readLen > length { if readLen > length {
readLen = length readLen = length
} }
n, err = c.ExtendedConn.Read(p[:readLen]) n, err = c.Conn.Read(p[:readLen])
if err != nil { if err != nil {
return return
} }
@ -92,5 +93,5 @@ func (c *Conn) WriteVectorised(buffers []*buf.Buffer) error {
} }
func (c *Conn) Upstream() any { func (c *Conn) Upstream() any {
return c.ExtendedConn return c.Conn
} }