Compare commits

...
1 Commits
Author SHA1 Message Date
Fangliding bf3c512a70 Fix delayDialConn and some race 2026-11-30 21:02:58 +08:00
+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()
}