Skip to content

Commit

Permalink
Close ssh tunnel when either end closes the connection (#2184)
Browse files Browse the repository at this point in the history
So that when users tracing through SSH with a specific number of frames
to capture, the connection will be closed after that number of frames
are captured.
  • Loading branch information
Qining authored Sep 11, 2018
1 parent 9871fcd commit 79d1e40
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions core/os/device/remotessh/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,31 @@ func (b binding) doTunnel(ctx context.Context, local net.Conn, remotePort int) e

wg := sync.WaitGroup{}

copy := func(writer io.Writer, reader io.Reader) {
_, err := io.Copy(writer, reader)
copy := func(writer net.Conn, reader net.Conn) {
// Use the same buffer size used in io.Copy
buf := make([]byte, 32*1024)
var err error
for {
nr, er := reader.Read(buf)
if nr > 0 {
nw, ew := writer.Write(buf[0:nr])
if ew != nil {
err = ew
break
}
if nr != nw {
err = fmt.Errorf("short write")
break
}
}
if er != nil {
if er != io.EOF {
err = er
}
break
}
}
writer.Close()
if err != nil {
log.E(ctx, "Copy Error %s", err)
}
Expand Down

0 comments on commit 79d1e40

Please sign in to comment.