diff --git a/.errcheck.exclude b/.errcheck.exclude new file mode 100644 index 0000000..d7cfacd --- /dev/null +++ b/.errcheck.exclude @@ -0,0 +1,2 @@ +io.Copy +(net.Conn).Close diff --git a/CHANGELOG.md b/CHANGELOG.md index db3e03b..cb5a944 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file automatically by Versionist. DO NOT EDIT THIS FILE MANUALLY! This project adheres to [Semantic Versioning](http://semver.org/). +## v1.4.3 - 2018-02-02 + +* Remove excessively verbose logging #32 [Will Boyce] +* Wait until all channels/requests have been serviced before closing connection #32 [Alexis Svinartchouk] + ## v1.4.2 - 2017-11-30 * Use keyType during key generation to create correct key type #28 [Andreas Fitzek] diff --git a/Makefile b/Makefile index be8be44..d7f032b 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ lint: lint-dep gofmt -e -l -s . golint -set_exit_status ./... go tool vet . - errcheck -verbose ./... + errcheck -exclude .errcheck.exclude -verbose ./... test-dep: dep go test -i -v ./... diff --git a/sshproxy.go b/sshproxy.go index 0bb2a88..89735b5 100644 --- a/sshproxy.go +++ b/sshproxy.go @@ -30,6 +30,7 @@ import ( "os" "os/exec" "path/filepath" + "sync" "syscall" "time" @@ -143,9 +144,7 @@ func (s *Server) upgradeConnection(conn net.Conn) { log.Printf("New SSH connection from %s (%s)", conn.RemoteAddr(), sshConn.ClientVersion()) defer func() { - if err := conn.Close(); err != nil { - s.handleError(err, nil) - } + conn.Close() log.Printf("Closed connection to %s", conn.RemoteAddr()) }() go ssh.DiscardRequests(reqs) @@ -154,6 +153,7 @@ func (s *Server) upgradeConnection(conn net.Conn) { // After successful handshake, handle new channels. Only the "session" type is supported. func (s *Server) handleChannels(chans <-chan ssh.NewChannel, conn *ssh.ServerConn) { + var wg sync.WaitGroup for newChannel := range chans { log.Printf("New SSH channel from %s", conn.RemoteAddr()) if chanType := newChannel.ChannelType(); chanType != "session" { @@ -170,12 +170,15 @@ func (s *Server) handleChannels(chans <-chan ssh.NewChannel, conn *ssh.ServerCon } // Do not block handling requests so we can service new channels + wg.Add(1) go func() { + defer wg.Done() if err := s.handleRequests(reqs, channel, conn); err != nil { s.handleError(err, nil) } }() } + wg.Wait() } // Service requests on given channel @@ -256,10 +259,7 @@ func (s *Server) handleRequests(reqs <-chan *ssh.Request, channel ssh.Channel, c } break Loop } - case err := <-done: - if err != nil { - s.handleError(err, nil) - } + case <-done: break Loop } } @@ -303,12 +303,6 @@ func (s *Server) handleRequests(reqs <-chan *ssh.Request, channel ssh.Channel, c } func (s *Server) launchCommand(channel ssh.Channel, cmd *exec.Cmd, terminal *pty.Terminal) error { - ioCopy := func(dst io.Writer, src io.Reader) { - if _, err := io.Copy(dst, src); err != nil { - s.handleError(err, nil) - } - } - if s.shellCreds != nil { cmd.SysProcAttr = &syscall.SysProcAttr{Credential: s.shellCreds} } @@ -319,8 +313,8 @@ func (s *Server) launchCommand(channel ssh.Channel, cmd *exec.Cmd, terminal *pty return err } - go ioCopy(terminal, channel) - go ioCopy(channel, terminal) + go io.Copy(terminal, channel) + go io.Copy(channel, terminal) } else { stdout, err := cmd.StdoutPipe() if err != nil { @@ -341,9 +335,9 @@ func (s *Server) launchCommand(channel ssh.Channel, cmd *exec.Cmd, terminal *pty return err } - go ioCopy(stdin, channel) - go ioCopy(channel, stdout) - go ioCopy(channel.Stderr(), stderr) + go io.Copy(stdin, channel) + go io.Copy(channel, stdout) + go io.Copy(channel.Stderr(), stderr) } return nil