Fix not closing outConn

Co-authored-by: Mahdi-zarei  <mahdi.zrei@gmail.com>
Co-authored-by: 世界 <i@sekai.icu>
This commit is contained in:
世界 2023-11-13 13:53:52 +08:00
parent 6815f94180
commit e777b4c6dc
No known key found for this signature in database
GPG key ID: CD109927C34A63C4

View file

@ -71,6 +71,7 @@ func NewConnection(ctx context.Context, this N.Dialer, conn net.Conn, metadata a
} }
err = N.ReportHandshakeSuccess(conn) err = N.ReportHandshakeSuccess(conn)
if err != nil { if err != nil {
outConn.Close()
return err return err
} }
return CopyEarlyConn(ctx, conn, outConn) return CopyEarlyConn(ctx, conn, outConn)
@ -97,6 +98,7 @@ func NewDirectConnection(ctx context.Context, router adapter.Router, this N.Dial
} }
err = N.ReportHandshakeSuccess(conn) err = N.ReportHandshakeSuccess(conn)
if err != nil { if err != nil {
outConn.Close()
return err return err
} }
return CopyEarlyConn(ctx, conn, outConn) return CopyEarlyConn(ctx, conn, outConn)
@ -117,6 +119,7 @@ func NewPacketConnection(ctx context.Context, this N.Dialer, conn N.PacketConn,
} }
err = N.ReportHandshakeSuccess(conn) err = N.ReportHandshakeSuccess(conn)
if err != nil { if err != nil {
outConn.Close()
return err return err
} }
if destinationAddress.IsValid() { if destinationAddress.IsValid() {
@ -160,6 +163,7 @@ func NewDirectPacketConnection(ctx context.Context, router adapter.Router, this
} }
err = N.ReportHandshakeSuccess(conn) err = N.ReportHandshakeSuccess(conn)
if err != nil { if err != nil {
outConn.Close()
return err return err
} }
if destinationAddress.IsValid() { if destinationAddress.IsValid() {
@ -188,6 +192,7 @@ func CopyEarlyConn(ctx context.Context, conn net.Conn, serverConn net.Conn) erro
_, err := serverConn.Write(payload.Bytes()) _, err := serverConn.Write(payload.Bytes())
payload.Release() payload.Release()
if err != nil { if err != nil {
serverConn.Close()
return err return err
} }
return bufio.CopyConn(ctx, conn, serverConn) return bufio.CopyConn(ctx, conn, serverConn)
@ -199,22 +204,26 @@ func CopyEarlyConn(ctx context.Context, conn net.Conn, serverConn net.Conn) erro
if err != os.ErrInvalid { if err != os.ErrInvalid {
if err != nil { if err != nil {
payload.Release() payload.Release()
serverConn.Close()
return err return err
} }
_, err = payload.ReadOnceFrom(conn) _, err = payload.ReadOnceFrom(conn)
if err != nil && !E.IsTimeout(err) { if err != nil && !E.IsTimeout(err) {
payload.Release() payload.Release()
serverConn.Close()
return E.Cause(err, "read payload") return E.Cause(err, "read payload")
} }
err = conn.SetReadDeadline(time.Time{}) err = conn.SetReadDeadline(time.Time{})
if err != nil { if err != nil {
payload.Release() payload.Release()
serverConn.Close()
return err return err
} }
} }
_, err = serverConn.Write(payload.Bytes()) _, err = serverConn.Write(payload.Bytes())
payload.Release() payload.Release()
if err != nil { if err != nil {
serverConn.Close()
return N.ReportHandshakeFailure(conn, err) return N.ReportHandshakeFailure(conn, err)
} }
} }