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 82 additions and 27 deletions
-8
View File
@@ -130,14 +130,6 @@ func (c *UConn) WebsocketHandshakeContext(ctx context.Context) error {
config.NextProtos = []string{"http/1.1"}
return c.HandshakeContext(ctx)
}
// Chrome won't send ApplicationSettingsExtension for h1 request
if slices.Equal(ALPN, []string{"http/1.1"}) {
newExt := slices.DeleteFunc(c.Extensions, func(extension utls.TLSExtension) bool {
_, ok := extension.(*utls.ApplicationSettingsExtensionNew)
return ok
})
c.Extensions = newExt
}
// Iterate over extensions and check for utls.ALPNExtension
hasALPNExtension := false
for _, extension := range c.Extensions {
+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()
}