diff --git a/carrier/websocket.go b/carrier/websocket.go index 7011ea50..704d3d5c 100644 --- a/carrier/websocket.go +++ b/carrier/websocket.go @@ -62,7 +62,7 @@ func (ws *Websocket) ServeStream(options *StartOptions, conn io.ReadWriter) erro _ = socksServer.Serve(conn) } else { - ingress.Stream(wsConn, conn) + ingress.Stream(wsConn, conn, ws.log) } return nil } diff --git a/ingress/origin_connection.go b/ingress/origin_connection.go index 8dab2c22..94cf76ca 100644 --- a/ingress/origin_connection.go +++ b/ingress/origin_connection.go @@ -8,30 +8,37 @@ import ( "github.com/cloudflare/cloudflared/connection" "github.com/cloudflare/cloudflared/websocket" gws "github.com/gorilla/websocket" + "github.com/rs/zerolog" ) // OriginConnection is a way to stream to a service running on the user's origin. // Different concrete implementations will stream different protocols as long as they are io.ReadWriters. type OriginConnection interface { // Stream should generally be implemented as a bidirectional io.Copy. - Stream(tunnelConn io.ReadWriter) + Stream(tunnelConn io.ReadWriter, log *zerolog.Logger) Close() Type() connection.Type } -type streamHandlerFunc func(originConn io.ReadWriter, remoteConn net.Conn) +type streamHandlerFunc func(originConn io.ReadWriter, remoteConn net.Conn, log *zerolog.Logger) // Stream copies copy data to & from provided io.ReadWriters. -func Stream(conn, backendConn io.ReadWriter) { +func Stream(conn, backendConn io.ReadWriter, log *zerolog.Logger) { proxyDone := make(chan struct{}, 2) go func() { - io.Copy(conn, backendConn) + _, err := io.Copy(conn, backendConn) + if err != nil { + log.Debug().Msgf("conn to backendConn copy: %v", err) + } proxyDone <- struct{}{} }() go func() { - io.Copy(backendConn, conn) + _, err := io.Copy(backendConn, conn) + if err != nil { + log.Debug().Msgf("backendConn to conn copy: %v", err) + } proxyDone <- struct{}{} }() @@ -41,8 +48,8 @@ func Stream(conn, backendConn io.ReadWriter) { // DefaultStreamHandler is an implementation of streamHandlerFunc that // performs a two way io.Copy between originConn and remoteConn. -func DefaultStreamHandler(originConn io.ReadWriter, remoteConn net.Conn) { - Stream(originConn, remoteConn) +func DefaultStreamHandler(originConn io.ReadWriter, remoteConn net.Conn, log *zerolog.Logger) { + Stream(originConn, remoteConn, log) } // tcpConnection is an OriginConnection that directly streams to raw TCP. @@ -51,8 +58,8 @@ type tcpConnection struct { streamHandler streamHandlerFunc } -func (tc *tcpConnection) Stream(tunnelConn io.ReadWriter) { - tc.streamHandler(tunnelConn, tc.conn) +func (tc *tcpConnection) Stream(tunnelConn io.ReadWriter, log *zerolog.Logger) { + tc.streamHandler(tunnelConn, tc.conn, log) } func (tc *tcpConnection) Close() { @@ -70,8 +77,8 @@ type wsConnection struct { resp *http.Response } -func (wsc *wsConnection) Stream(tunnelConn io.ReadWriter) { - Stream(tunnelConn, wsc.wsConn.UnderlyingConn()) +func (wsc *wsConnection) Stream(tunnelConn io.ReadWriter, log *zerolog.Logger) { + Stream(tunnelConn, wsc.wsConn.UnderlyingConn(), log) } func (wsc *wsConnection) Close() { diff --git a/origin/proxy.go b/origin/proxy.go index 09b39273..e2316f41 100644 --- a/origin/proxy.go +++ b/origin/proxy.go @@ -202,7 +202,7 @@ func (p *proxy) proxyConnection( originConn.Close() }() - originConn.Stream(eyeballConn) + originConn.Stream(eyeballConn, p.log) return resp, nil } diff --git a/socks/request_handler.go b/socks/request_handler.go index f94e0c1d..9a1e0bea 100644 --- a/socks/request_handler.go +++ b/socks/request_handler.go @@ -5,6 +5,8 @@ import ( "io" "net" "strings" + + "github.com/rs/zerolog" ) // RequestHandler is the functions needed to handle a SOCKS5 command @@ -106,7 +108,7 @@ func (h *StandardRequestHandler) handleAssociate(conn io.ReadWriter, req *Reques return nil } -func StreamHandler(tunnelConn io.ReadWriter, originConn net.Conn) { +func StreamHandler(tunnelConn io.ReadWriter, originConn net.Conn, log *zerolog.Logger) { dialer := NewConnDialer(originConn) requestHandler := NewRequestHandler(dialer) socksServer := NewConnectionHandler(requestHandler) diff --git a/websocket/websocket.go b/websocket/websocket.go index 39657117..9103da28 100644 --- a/websocket/websocket.go +++ b/websocket/websocket.go @@ -54,7 +54,7 @@ func StartProxyServer( listener net.Listener, staticHost string, shutdownC <-chan struct{}, - streamHandler func(originConn io.ReadWriter, remoteConn net.Conn), + streamHandler func(originConn io.ReadWriter, remoteConn net.Conn, log *zerolog.Logger), ) error { upgrader := websocket.Upgrader{ ReadBufferSize: 1024, @@ -81,7 +81,7 @@ type handler struct { log *zerolog.Logger staticHost string upgrader websocket.Upgrader - streamHandler func(originConn io.ReadWriter, remoteConn net.Conn) + streamHandler func(originConn io.ReadWriter, remoteConn net.Conn, log *zerolog.Logger) } func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -118,7 +118,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { go gorillaConn.pinger(r.Context()) defer conn.Close() - h.streamHandler(gorillaConn, stream) + h.streamHandler(gorillaConn, stream, h.log) } // NewResponseHeader returns headers needed to return to origin for completing handshake