Skip to content

Commit f24beaf

Browse files
author
resin-io-versionbot[bot]
authored
Auto-merge for PR #32 via VersionBot
Wait until all channels/requests have been serviced before closing connection
2 parents c907f6d + f9e154d commit f24beaf

File tree

4 files changed

+20
-19
lines changed

4 files changed

+20
-19
lines changed

.errcheck.exclude

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
io.Copy
2+
(net.Conn).Close

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file
44
automatically by Versionist. DO NOT EDIT THIS FILE MANUALLY!
55
This project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## v1.4.3 - 2018-02-02
8+
9+
* Remove excessively verbose logging #32 [Will Boyce]
10+
* Wait until all channels/requests have been serviced before closing connection #32 [Alexis Svinartchouk]
11+
712
## v1.4.2 - 2017-11-30
813

914
* Use keyType during key generation to create correct key type #28 [Andreas Fitzek]

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ lint: lint-dep
2222
gofmt -e -l -s .
2323
golint -set_exit_status ./...
2424
go tool vet .
25-
errcheck -verbose ./...
25+
errcheck -exclude .errcheck.exclude -verbose ./...
2626

2727
test-dep: dep
2828
go test -i -v ./...

sshproxy.go

+12-18
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"os"
3131
"os/exec"
3232
"path/filepath"
33+
"sync"
3334
"syscall"
3435
"time"
3536

@@ -143,9 +144,7 @@ func (s *Server) upgradeConnection(conn net.Conn) {
143144
log.Printf("New SSH connection from %s (%s)", conn.RemoteAddr(), sshConn.ClientVersion())
144145

145146
defer func() {
146-
if err := conn.Close(); err != nil {
147-
s.handleError(err, nil)
148-
}
147+
conn.Close()
149148
log.Printf("Closed connection to %s", conn.RemoteAddr())
150149
}()
151150
go ssh.DiscardRequests(reqs)
@@ -154,6 +153,7 @@ func (s *Server) upgradeConnection(conn net.Conn) {
154153

155154
// After successful handshake, handle new channels. Only the "session" type is supported.
156155
func (s *Server) handleChannels(chans <-chan ssh.NewChannel, conn *ssh.ServerConn) {
156+
var wg sync.WaitGroup
157157
for newChannel := range chans {
158158
log.Printf("New SSH channel from %s", conn.RemoteAddr())
159159
if chanType := newChannel.ChannelType(); chanType != "session" {
@@ -170,12 +170,15 @@ func (s *Server) handleChannels(chans <-chan ssh.NewChannel, conn *ssh.ServerCon
170170
}
171171

172172
// Do not block handling requests so we can service new channels
173+
wg.Add(1)
173174
go func() {
175+
defer wg.Done()
174176
if err := s.handleRequests(reqs, channel, conn); err != nil {
175177
s.handleError(err, nil)
176178
}
177179
}()
178180
}
181+
wg.Wait()
179182
}
180183

181184
// Service requests on given channel
@@ -256,10 +259,7 @@ func (s *Server) handleRequests(reqs <-chan *ssh.Request, channel ssh.Channel, c
256259
}
257260
break Loop
258261
}
259-
case err := <-done:
260-
if err != nil {
261-
s.handleError(err, nil)
262-
}
262+
case <-done:
263263
break Loop
264264
}
265265
}
@@ -303,12 +303,6 @@ func (s *Server) handleRequests(reqs <-chan *ssh.Request, channel ssh.Channel, c
303303
}
304304

305305
func (s *Server) launchCommand(channel ssh.Channel, cmd *exec.Cmd, terminal *pty.Terminal) error {
306-
ioCopy := func(dst io.Writer, src io.Reader) {
307-
if _, err := io.Copy(dst, src); err != nil {
308-
s.handleError(err, nil)
309-
}
310-
}
311-
312306
if s.shellCreds != nil {
313307
cmd.SysProcAttr = &syscall.SysProcAttr{Credential: s.shellCreds}
314308
}
@@ -319,8 +313,8 @@ func (s *Server) launchCommand(channel ssh.Channel, cmd *exec.Cmd, terminal *pty
319313
return err
320314
}
321315

322-
go ioCopy(terminal, channel)
323-
go ioCopy(channel, terminal)
316+
go io.Copy(terminal, channel)
317+
go io.Copy(channel, terminal)
324318
} else {
325319
stdout, err := cmd.StdoutPipe()
326320
if err != nil {
@@ -341,9 +335,9 @@ func (s *Server) launchCommand(channel ssh.Channel, cmd *exec.Cmd, terminal *pty
341335
return err
342336
}
343337

344-
go ioCopy(stdin, channel)
345-
go ioCopy(channel, stdout)
346-
go ioCopy(channel.Stderr(), stderr)
338+
go io.Copy(stdin, channel)
339+
go io.Copy(channel, stdout)
340+
go io.Copy(channel.Stderr(), stderr)
347341
}
348342

349343
return nil

0 commit comments

Comments
 (0)