Compare commits

..
2 Commits
Author SHA1 Message Date
Fangliding 19e9a819b6 fmt 2026-12-01 03:09:27 +08:00
Fangliding bf3c512a70 Fix delayDialConn and some race 2026-11-30 21:02:58 +08:00
2 changed files with 87 additions and 45 deletions
+5 -26
View File
@@ -26,14 +26,7 @@ var (
)
func IsValidTLSVersion(major, minor byte) bool {
if major != 3 {
return false
}
// 1 = TLS 1.0, 4 = TLS 1.3
if minor < 1 || minor > 4 {
return false
}
return true
return major == 3
}
// ReadClientHello returns server name (if any) from TLS client hello message.
@@ -140,33 +133,19 @@ func ReadClientHello(data []byte, h *SniffHeader) error {
}
func SniffTLS(b []byte) (*SniffHeader, error) {
if len(b) < 11 {
if len(b) < 5 {
return nil, common.ErrNoClue
}
// Record type: Handshake
if b[0] != 0x16 {
if b[0] != 0x16 /* TLS Handshake */ {
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, protocol.ErrProtoNeedMoreData
return nil, common.ErrNoClue
}
h := &SniffHeader{}
+82 -19
View File
@@ -5,6 +5,7 @@ import (
_ "embed"
"encoding/base64"
"io"
"sync/atomic"
"time"
"github.com/gorilla/websocket"
@@ -24,7 +25,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, 1),
dialed: make(chan bool),
cancel: cancel,
ctx: ctx,
dest: dest,
@@ -158,8 +159,8 @@ func dialWebSocket(ctx context.Context, dest net.Destination, streamSettings *in
}
type delayDialConn struct {
net.Conn
closed bool
conn atomic.Value // net.Conn
closed atomic.Bool
dialed chan bool
cancel context.CancelFunc
ctx context.Context
@@ -171,62 +172,124 @@ 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 {
if d.Conn == nil {
conn := d.conn.Load()
if conn == nil {
return nil
}
return d.Conn.LocalAddr()
return conn.(net.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 {
if d.Conn == nil {
conn := d.conn.Load()
if conn == nil {
return nil
}
return d.Conn.RemoteAddr()
return conn.(net.Conn).RemoteAddr()
}
func (d *delayDialConn) Write(b []byte) (int, error) {
if d.closed {
if d.closed.Load() {
return 0, io.ErrClosedPipe
}
if d.Conn == nil {
conn := d.conn.Load()
if conn == nil {
ed := b
if len(ed) > int(d.streamSettings.ProtocolSettings.(*Config).Ed) {
ed = nil
}
var err error
if d.Conn, err = dialWebSocket(d.ctx, d.dest, d.streamSettings, ed); err != nil {
c, err := dialWebSocket(d.ctx, d.dest, d.streamSettings, ed)
if err != nil {
d.Close()
return 0, errors.New("failed to dial WebSocket").Base(err)
}
d.dialed <- true
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)
if ed != nil {
return len(ed), nil
}
}
return d.Conn.Write(b)
return conn.(net.Conn).Write(b)
}
func (d *delayDialConn) Read(b []byte) (int, error) {
if d.closed {
if d.closed.Load() {
return 0, io.ErrClosedPipe
}
if d.Conn == nil {
conn := d.conn.Load()
if conn == nil {
select {
case <-d.ctx.Done():
return 0, io.ErrUnexpectedEOF
case <-d.dialed:
}
conn = d.conn.Load()
}
return d.Conn.Read(b)
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
}
}()
}
func (d *delayDialConn) Close() error {
d.closed = true
d.closed.Store(true)
d.cancel()
if d.Conn == nil {
conn := d.conn.Load()
if conn == nil {
return nil
}
return d.Conn.Close()
return conn.(net.Conn).Close()
}