From f2fb48f2e419d37e846d58396d342a6ecf423011 Mon Sep 17 00:00:00 2001 From: ryanbekhen Date: Thu, 23 Nov 2023 06:51:50 +0700 Subject: [PATCH] fix: handling for error --- pkg/socks5/auth.go | 7 +++++-- pkg/socks5/request.go | 6 ++++-- pkg/socks5/socks5.go | 7 ++++++- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/pkg/socks5/auth.go b/pkg/socks5/auth.go index 577d27e..0c40471 100644 --- a/pkg/socks5/auth.go +++ b/pkg/socks5/auth.go @@ -42,7 +42,7 @@ func (a NoAuthAuthenticator) GetCode() uint8 { return NoAuth } -func (a NoAuthAuthenticator) Authenticate(reader io.Reader, writer io.Writer) (*AuthContext, error) { +func (a NoAuthAuthenticator) Authenticate(_ io.Reader, writer io.Writer) (*AuthContext, error) { _, err := writer.Write([]byte{socks5Version, NoAuth}) return &AuthContext{NoAuth, nil}, err } @@ -131,7 +131,10 @@ func (s *Server) authenticate(conn io.Writer, bufConn io.Reader) (*AuthContext, // noAcceptableAuth is used to handle when we have no eligible // authentication mechanism func noAcceptableAuth(conn io.Writer) error { - conn.Write([]byte{socks5Version, noAcceptable}) + _, err := conn.Write([]byte{socks5Version, noAcceptable}) + if err != nil { + return err + } return NoSupportedAuth } diff --git a/pkg/socks5/request.go b/pkg/socks5/request.go index 41d61e9..039f2b9 100644 --- a/pkg/socks5/request.go +++ b/pkg/socks5/request.go @@ -197,7 +197,9 @@ func (s *Server) handleConnect(ctx context.Context, conn conn, req *Request) err } return fmt.Errorf("connect to %v failed: %v", req.DestAddr, err) } - defer target.Close() + defer func(target net.Conn) { + _ = target.Close() + }(target) // Send success local := target.LocalAddr().(*net.TCPAddr) @@ -367,7 +369,7 @@ type closeWriter interface { func proxy(dst io.Writer, src io.Reader, errCh chan error) { _, err := io.Copy(dst, src) if tcpConn, ok := dst.(closeWriter); ok { - tcpConn.CloseWrite() + _ = tcpConn.CloseWrite() } errCh <- err } diff --git a/pkg/socks5/socks5.go b/pkg/socks5/socks5.go index 2cb3693..28d1b28 100644 --- a/pkg/socks5/socks5.go +++ b/pkg/socks5/socks5.go @@ -120,7 +120,12 @@ func (s *Server) Serve(l net.Listener) error { // ServeConn is used to serve a single connection. func (s *Server) ServeConn(conn net.Conn) { - defer conn.Close() + defer func(conn net.Conn) { + err := conn.Close() + if err != nil { + s.config.Logger.Err(err).Msg("failed to close connection") + } + }(conn) bufConn := bufio.NewReader(conn) // Read the version byte