From ab728bd8cc7707c4460f4017047ad0b67f9aa74b Mon Sep 17 00:00:00 2001 From: xjasonlyu Date: Tue, 5 Apr 2022 23:13:38 +0800 Subject: [PATCH] Chore(tunnel): log tcp error --- tunnel/tcp.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/tunnel/tcp.go b/tunnel/tcp.go index 121584b..ba4b0b4 100644 --- a/tunnel/tcp.go +++ b/tunnel/tcp.go @@ -55,23 +55,34 @@ func relay(left, right net.Conn) { go func() { defer wg.Done() - _ = copyBuffer(right, left) /* ignore error */ + if err := copyBuffer(right, left); err != nil { + log.Warnf("[TCP] copy buffer: %v", err) + } right.SetReadDeadline(time.Now().Add(tcpWaitTimeout)) }() go func() { defer wg.Done() - _ = copyBuffer(left, right) /* ignore error */ + if err := copyBuffer(left, right); err != nil { + log.Warnf("[TCP] copy buffer: %v", err) + } left.SetReadDeadline(time.Now().Add(tcpWaitTimeout)) }() wg.Wait() } -func copyBuffer(dst io.Writer, src io.Reader) error { +func copyBuffer(dst io.Writer, src io.Reader) (err error) { buf := pool.Get(pool.RelayBufferSize) defer pool.Put(buf) - _, err := io.CopyBuffer(dst, src, buf) + defer func() { + if err != nil { + if ne, ok := err.(net.Error); ok && ne.Timeout() { + err = nil /* ignore I/O timeout */ + } + } + }() + _, err = io.CopyBuffer(dst, src, buf) return err }