From 5ab5eb31f6b05e914bf2e63c99f43588f786247b Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Sun, 5 Jul 2026 22:49:37 +0800 Subject: [PATCH] fix: snell v2 listener can't pass test --- listener/inbound/snell_test.go | 2 +- transport/snell/pool.go | 6 +++ transport/snell/pool_test.go | 68 +++++++++++++++++++++++++++++++--- 3 files changed, 70 insertions(+), 6 deletions(-) diff --git a/listener/inbound/snell_test.go b/listener/inbound/snell_test.go index 6a5770a13..5dbb4ddbc 100644 --- a/listener/inbound/snell_test.go +++ b/listener/inbound/snell_test.go @@ -61,7 +61,7 @@ func TestInboundSnell(t *testing.T) { version int }{ {"v1", 1}, - //{"v2", 2}, + {"v2", 2}, {"v3", 3}, {"v4", 4}, {"v5", 5}, diff --git a/transport/snell/pool.go b/transport/snell/pool.go index 9e65d9b06..811721e8b 100644 --- a/transport/snell/pool.go +++ b/transport/snell/pool.go @@ -57,11 +57,13 @@ type PoolConn struct { closeOnce sync.Once closeErr error reusableState atomic.Int32 + peerClosed atomic.Bool } func (pc *PoolConn) Read(b []byte) (int, error) { n, err := pc.Snell.Read(b) if err == shadowaead.ErrZeroChunk { + pc.peerClosed.Store(true) return n, io.EOF } return n, err @@ -105,6 +107,10 @@ func (pc *PoolConn) Close() error { return } + if !pc.peerClosed.Load() { + _ = pc.Snell.Close() + return + } // mihomo use SetReadDeadline to break bidirectional copy between client and server. // reset it before reuse connection to avoid io timeout error. _ = pc.Snell.Conn.SetReadDeadline(time.Time{}) diff --git a/transport/snell/pool_test.go b/transport/snell/pool_test.go index 0dcc964f3..73f7ec368 100644 --- a/transport/snell/pool_test.go +++ b/transport/snell/pool_test.go @@ -12,7 +12,10 @@ import ( ) func TestPoolConnCloseIsIdempotent(t *testing.T) { - rawConn := &recordingConn{} + rawConn := &recordingConn{ + readData: []byte{CommandTunnel}, + readErr: shadowaead.ErrZeroChunk, + } pooledConn := &Snell{Conn: rawConn} pool := NewPool(func(context.Context) (*Snell, error) { return nil, errors.New("factory should not be called") @@ -23,6 +26,9 @@ func TestPoolConnCloseIsIdempotent(t *testing.T) { t.Fatal(err) } conn.MarkReusable() + if _, err := conn.Read(make([]byte, 1)); !errors.Is(err, io.EOF) { + t.Fatalf("expected peer half-close before pooling, got %v", err) + } if err := conn.Close(); err != nil { t.Fatal(err) } @@ -33,6 +39,9 @@ func TestPoolConnCloseIsIdempotent(t *testing.T) { if rawConn.writes != 2 { t.Fatalf("close should send the request and one half-close record, got %d writes", rawConn.writes) } + if rawConn.reads != 2 { + t.Fatalf("close should observe the peer half-close before pooling, got %d reads", rawConn.reads) + } got, err := pool.pool.Get() if err != nil { @@ -141,7 +150,9 @@ func TestPoolConnCloseWriteBeforeRequestClosesRawConnection(t *testing.T) { } func TestPoolConnCloseWriteDoesNotReturnConnectionToPool(t *testing.T) { - rawConn := &recordingConn{} + rawConn := &recordingConn{ + readErr: shadowaead.ErrZeroChunk, + } pooledConn := &Snell{Conn: rawConn, reply: true} factoryConn := &Snell{Conn: &recordingConn{}} pool := NewPool(func(context.Context) (*Snell, error) { @@ -170,6 +181,9 @@ func TestPoolConnCloseWriteDoesNotReturnConnectionToPool(t *testing.T) { if !pooledConn.reply { t.Fatal("CloseWrite should not reset reply while the read side may still be active") } + if _, err = conn.Read(make([]byte, 1)); !errors.Is(err, io.EOF) { + t.Fatalf("expected peer half-close before pooling, got %v", err) + } if err = conn.Close(); err != nil { t.Fatal(err) @@ -184,11 +198,43 @@ func TestPoolConnCloseWriteDoesNotReturnConnectionToPool(t *testing.T) { if rawConn.writes != 2 { t.Fatalf("Close after CloseWrite should not send another half-close record, got %d writes", rawConn.writes) } + if rawConn.reads != 1 { + t.Fatalf("Close should observe the peer half-close before pooling, got %d reads", rawConn.reads) + } if pooledConn.reply { t.Fatal("Close should reset reply before returning the connection to the pool") } } +func TestPoolConnCloseWithoutPeerHalfCloseClosesRawConnection(t *testing.T) { + rawConn := &recordingConn{} + pooledConn := &Snell{Conn: rawConn} + factoryConn := &Snell{Conn: &recordingConn{}} + pool := NewPool(func(context.Context) (*Snell, error) { + return factoryConn, nil + }) + conn := &PoolConn{Snell: pooledConn, pool: pool} + + if _, err := conn.Write([]byte{Version, CommandConnectV2, 0}); err != nil { + t.Fatal(err) + } + conn.MarkReusable() + if err := conn.Close(); err != nil { + t.Fatal(err) + } + + if !rawConn.closed { + t.Fatal("connection without peer half-close should close the raw connection") + } + got, err := pool.pool.Get() + if err != nil { + t.Fatal(err) + } + if got != factoryConn { + t.Fatal("connection without peer half-close should not be returned to the pool") + } +} + func TestPoolConnReadZeroChunkReturnsEOF(t *testing.T) { conn := &PoolConn{Snell: &Snell{Conn: zeroChunkConn{}, reply: true}} @@ -202,11 +248,23 @@ func TestPoolConnReadZeroChunkReturnsEOF(t *testing.T) { } type recordingConn struct { - writes int - closed bool + writes int + reads int + readData []byte + readErr error + closed bool } -func (c *recordingConn) Read([]byte) (int, error) { +func (c *recordingConn) Read(b []byte) (int, error) { + c.reads++ + if len(c.readData) > 0 { + n := copy(b, c.readData) + c.readData = c.readData[n:] + return n, nil + } + if c.readErr != nil { + return 0, c.readErr + } return 0, io.EOF }