mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2026-12-02 18:26:47 +00:00
fix: snell v2 listener can't pass test
This commit is contained in:
1 parent
1f93334106
commit
5ab5eb31f6
3 files changed
+70
-6
No files matched your search
@@ -61,7 +61,7 @@ func TestInboundSnell(t *testing.T) {
|
||||
version int
|
||||
}{
|
||||
{"v1", 1},
|
||||
//{"v2", 2},
|
||||
{"v2", 2},
|
||||
{"v3", 3},
|
||||
{"v4", 4},
|
||||
{"v5", 5},
|
||||
|
||||
@@ -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{})
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user