Skip to content

Commit

Permalink
fix: handling for error
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanbekhen committed Nov 22, 2023
1 parent a389e04 commit f2fb48f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
7 changes: 5 additions & 2 deletions pkg/socks5/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down
6 changes: 4 additions & 2 deletions pkg/socks5/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/socks5/socks5.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f2fb48f

Please sign in to comment.