TUN-3799: extended the Stream interface to take a logger and added debug logs for io.Copy errors

This commit is contained in:
Sudarsan Reddy 2021-02-04 15:07:18 +00:00 committed by Nuno Diegues
parent a6c2348127
commit 8b794390e5
5 changed files with 26 additions and 17 deletions

View File

@ -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
}

View File

@ -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() {

View File

@ -202,7 +202,7 @@ func (p *proxy) proxyConnection(
originConn.Close()
}()
originConn.Stream(eyeballConn)
originConn.Stream(eyeballConn, p.log)
return resp, nil
}

View File

@ -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)

View File

@ -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