@@ -30,6 +30,7 @@ import (
30
30
"os"
31
31
"os/exec"
32
32
"path/filepath"
33
+ "sync"
33
34
"syscall"
34
35
"time"
35
36
@@ -143,9 +144,7 @@ func (s *Server) upgradeConnection(conn net.Conn) {
143
144
log .Printf ("New SSH connection from %s (%s)" , conn .RemoteAddr (), sshConn .ClientVersion ())
144
145
145
146
defer func () {
146
- if err := conn .Close (); err != nil {
147
- s .handleError (err , nil )
148
- }
147
+ conn .Close ()
149
148
log .Printf ("Closed connection to %s" , conn .RemoteAddr ())
150
149
}()
151
150
go ssh .DiscardRequests (reqs )
@@ -154,6 +153,7 @@ func (s *Server) upgradeConnection(conn net.Conn) {
154
153
155
154
// After successful handshake, handle new channels. Only the "session" type is supported.
156
155
func (s * Server ) handleChannels (chans <- chan ssh.NewChannel , conn * ssh.ServerConn ) {
156
+ var wg sync.WaitGroup
157
157
for newChannel := range chans {
158
158
log .Printf ("New SSH channel from %s" , conn .RemoteAddr ())
159
159
if chanType := newChannel .ChannelType (); chanType != "session" {
@@ -170,12 +170,15 @@ func (s *Server) handleChannels(chans <-chan ssh.NewChannel, conn *ssh.ServerCon
170
170
}
171
171
172
172
// Do not block handling requests so we can service new channels
173
+ wg .Add (1 )
173
174
go func () {
175
+ defer wg .Done ()
174
176
if err := s .handleRequests (reqs , channel , conn ); err != nil {
175
177
s .handleError (err , nil )
176
178
}
177
179
}()
178
180
}
181
+ wg .Wait ()
179
182
}
180
183
181
184
// Service requests on given channel
@@ -256,10 +259,7 @@ func (s *Server) handleRequests(reqs <-chan *ssh.Request, channel ssh.Channel, c
256
259
}
257
260
break Loop
258
261
}
259
- case err := <- done :
260
- if err != nil {
261
- s .handleError (err , nil )
262
- }
262
+ case <- done :
263
263
break Loop
264
264
}
265
265
}
@@ -303,12 +303,6 @@ func (s *Server) handleRequests(reqs <-chan *ssh.Request, channel ssh.Channel, c
303
303
}
304
304
305
305
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
-
312
306
if s .shellCreds != nil {
313
307
cmd .SysProcAttr = & syscall.SysProcAttr {Credential : s .shellCreds }
314
308
}
@@ -319,8 +313,8 @@ func (s *Server) launchCommand(channel ssh.Channel, cmd *exec.Cmd, terminal *pty
319
313
return err
320
314
}
321
315
322
- go ioCopy (terminal , channel )
323
- go ioCopy (channel , terminal )
316
+ go io . Copy (terminal , channel )
317
+ go io . Copy (channel , terminal )
324
318
} else {
325
319
stdout , err := cmd .StdoutPipe ()
326
320
if err != nil {
@@ -341,9 +335,9 @@ func (s *Server) launchCommand(channel ssh.Channel, cmd *exec.Cmd, terminal *pty
341
335
return err
342
336
}
343
337
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 )
347
341
}
348
342
349
343
return nil
0 commit comments