From 8b0e8c990e68dc9b74ed3d40f4c73c9a24bacb7b Mon Sep 17 00:00:00 2001 From: Jose Garcia Date: Thu, 9 Sep 2021 17:31:18 +0000 Subject: [PATCH] ignore pf conn errors --- port_forwarder.go | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/port_forwarder.go b/port_forwarder.go index dc91222ed..1351025cb 100644 --- a/port_forwarder.go +++ b/port_forwarder.go @@ -33,9 +33,7 @@ func NewPortForwarder(session *Session, name string, remotePort int) *PortForwar // connecting to the socket prematurely.) // // ForwardToListener accepts and handles connections on the local port -// until it encounters the first error, which may include context -// cancellation. Its error result is always non-nil. The caller is -// responsible for closing the listening port. +// until the context is cancelled. The caller is responsible for closing the listening port. func (fwd *PortForwarder) ForwardToListener(ctx context.Context, listen net.Listener) (err error) { id, err := fwd.shareRemotePort(ctx) if err != nil { @@ -124,21 +122,14 @@ func (fwd *PortForwarder) handleConnection(ctx context.Context, id channelID, co } }() - errs := make(chan error, 2) - copyConn := func(w io.Writer, r io.Reader) { - _, err := io.Copy(w, r) - errs <- err - } - go copyConn(conn, channel) - go copyConn(channel, conn) + // Bi-directional copy of data. + // If any individual connection has an error, we can safely ignore them + // and defer to connection clients to handle data loss as necessary. + go io.Copy(conn, channel) + go io.Copy(channel, conn) - // await result - for i := 0; i < 2; i++ { - if err := <-errs; err != nil && err != io.EOF { - return fmt.Errorf("tunnel connection: %v", err) - } - } - return nil + <-ctx.Done() + return ctx.Err() } // safeClose reports the error (to *err) from closing the stream only