Compare commits

..
1 Commits
Author SHA1 Message Date
Fangliding cc23e8c51e Return ErrProtoNeedMoreData for incomplete TLS record 2026-12-01 20:14:25 +08:00
2 changed files with 45 additions and 87 deletions
+26 -5
View File
@@ -26,7 +26,14 @@ var (
)
func IsValidTLSVersion(major, minor byte) bool {
return major == 3
if major != 3 {
return false
}
// 1 = TLS 1.0, 4 = TLS 1.3
if minor < 1 || minor > 4 {
return false
}
return true
}
// ReadClientHello returns server name (if any) from TLS client hello message.
@@ -133,19 +140,33 @@ func ReadClientHello(data []byte, h *SniffHeader) error {
}
func SniffTLS(b []byte) (*SniffHeader, error) {
if len(b) < 5 {
if len(b) < 11 {
return nil, common.ErrNoClue
}
if b[0] != 0x16 /* TLS Handshake */ {
// Record type: Handshake
if b[0] != 0x16 {
return nil, errNotTLS
}
// Record version
if !IsValidTLSVersion(b[1], b[2]) {
return nil, errNotTLS
}
// Message type: ClientHello
if b[5] != 0x01 {
return nil, errNotClientHello
}
// Handshake version
if !IsValidTLSVersion(b[9], b[10]) {
return nil, errNotTLS
}
headerLen := int(binary.BigEndian.Uint16(b[3:5]))
// TLS maximum record length
if headerLen > 16384 {
return nil, errNotTLS
}
// In rare cases, the record might still be incomplete after 2 read operations
if 5+headerLen > len(b) {
return nil, common.ErrNoClue
return nil, protocol.ErrProtoNeedMoreData
}
h := &SniffHeader{}
+19 -82
View File
@@ -5,7 +5,6 @@ import (
_ "embed"
"encoding/base64"
"io"
"sync/atomic"
"time"
"github.com/gorilla/websocket"
@@ -25,7 +24,7 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me
if streamSettings.ProtocolSettings.(*Config).Ed > 0 {
ctx, cancel := context.WithCancel(ctx)
conn = &delayDialConn{
dialed: make(chan bool),
dialed: make(chan bool, 1),
cancel: cancel,
ctx: ctx,
dest: dest,
@@ -159,8 +158,8 @@ func dialWebSocket(ctx context.Context, dest net.Destination, streamSettings *in
}
type delayDialConn struct {
conn atomic.Value // net.Conn
closed atomic.Bool
net.Conn
closed bool
dialed chan bool
cancel context.CancelFunc
ctx context.Context
@@ -172,124 +171,62 @@ type delayDialConn struct {
// Without this method, Go promotes LocalAddr from the embedded net.Conn; the
// embedded interface is nil before the first Write, so the promoted call panics.
func (d *delayDialConn) LocalAddr() net.Addr {
conn := d.conn.Load()
if conn == nil {
if d.Conn == nil {
return nil
}
return conn.(net.Conn).LocalAddr()
return d.Conn.LocalAddr()
}
// RemoteAddr returns nil until the deferred WebSocket dial has completed.
// See LocalAddr for why an explicit method is required here.
func (d *delayDialConn) RemoteAddr() net.Addr {
conn := d.conn.Load()
if conn == nil {
if d.Conn == nil {
return nil
}
return conn.(net.Conn).RemoteAddr()
return d.Conn.RemoteAddr()
}
func (d *delayDialConn) Write(b []byte) (int, error) {
if d.closed.Load() {
if d.closed {
return 0, io.ErrClosedPipe
}
conn := d.conn.Load()
if conn == nil {
if d.Conn == nil {
ed := b
if len(ed) > int(d.streamSettings.ProtocolSettings.(*Config).Ed) {
ed = nil
}
c, err := dialWebSocket(d.ctx, d.dest, d.streamSettings, ed)
if err != nil {
var err error
if d.Conn, err = dialWebSocket(d.ctx, d.dest, d.streamSettings, ed); err != nil {
d.Close()
return 0, errors.New("failed to dial WebSocket").Base(err)
}
d.conn.Store(c)
// someone close the conn after dial success
if d.closed.Load() {
d.Close()
return 0, io.ErrClosedPipe
}
conn = c
close(d.dialed)
d.dialed <- true
if ed != nil {
return len(ed), nil
}
}
return conn.(net.Conn).Write(b)
return d.Conn.Write(b)
}
func (d *delayDialConn) Read(b []byte) (int, error) {
if d.closed.Load() {
if d.closed {
return 0, io.ErrClosedPipe
}
conn := d.conn.Load()
if conn == nil {
if d.Conn == nil {
select {
case <-d.ctx.Done():
return 0, io.ErrUnexpectedEOF
case <-d.dialed:
}
conn = d.conn.Load()
}
return conn.(net.Conn).Read(b)
}
func (d *delayDialConn) SetDeadline(t time.Time) error {
if d.closed.Load() {
return io.ErrClosedPipe
}
conn := d.conn.Load()
if conn == nil {
d.setDelayedDeadline(t, d.SetDeadline)
return nil
}
return conn.(net.Conn).SetDeadline(t)
}
func (d *delayDialConn) SetReadDeadline(t time.Time) error {
if d.closed.Load() {
return io.ErrClosedPipe
}
conn := d.conn.Load()
if conn == nil {
d.setDelayedDeadline(t, d.SetReadDeadline)
return nil
}
return conn.(net.Conn).SetReadDeadline(t)
}
func (d *delayDialConn) SetWriteDeadline(t time.Time) error {
if d.closed.Load() {
return io.ErrClosedPipe
}
conn := d.conn.Load()
if conn == nil {
d.setDelayedDeadline(t, d.SetWriteDeadline)
return nil
}
return conn.(net.Conn).SetWriteDeadline(t)
}
func (d *delayDialConn) setDelayedDeadline(t time.Time, f func(time.Time) error) {
ctx, cancel := context.WithTimeout(d.ctx, time.Until(t))
go func() {
defer cancel()
select {
case <-d.dialed:
f(t)
case <-ctx.Done():
return
}
}()
return d.Conn.Read(b)
}
func (d *delayDialConn) Close() error {
d.closed.Store(true)
d.closed = true
d.cancel()
conn := d.conn.Load()
if conn == nil {
if d.Conn == nil {
return nil
}
return conn.(net.Conn).Close()
return d.Conn.Close()
}