From 95c03c9373d84977f2c99d60cf6b7f8a16a743a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Thu, 20 Oct 2022 10:57:57 +0800 Subject: [PATCH] Fix copy pipe --- outbound/default.go | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/outbound/default.go b/outbound/default.go index c38aff3f..e1be9129 100644 --- a/outbound/default.go +++ b/outbound/default.go @@ -3,6 +3,7 @@ package outbound import ( "context" "net" + "os" "runtime" "time" @@ -136,22 +137,25 @@ func CopyEarlyConn(ctx context.Context, conn net.Conn, serverConn net.Conn) erro _payload := buf.StackNew() payload := common.Dup(_payload) err := conn.SetReadDeadline(time.Now().Add(C.ReadPayloadTimeout)) - if err != nil { - return err - } - _, err = payload.ReadOnceFrom(conn) - if err != nil && !E.IsTimeout(err) { - return E.Cause(err, "read payload") - } - err = conn.SetReadDeadline(time.Time{}) - if err != nil { - payload.Release() - return err + if err != os.ErrInvalid { + if err != nil { + return err + } + _, err = payload.ReadOnceFrom(conn) + if err != nil && !E.IsTimeout(err) { + return E.Cause(err, "read payload") + } + err = conn.SetReadDeadline(time.Time{}) + if err != nil { + payload.Release() + return err + } } _, err = serverConn.Write(payload.Bytes()) if err != nil { return N.HandshakeFailure(conn, err) } runtime.KeepAlive(_payload) + payload.Release() return bufio.CopyConn(ctx, conn, serverConn) }