Skip to content

Commit

Permalink
Modify keepAliveLoop to async for enabling timeout on no-response ping
Browse files Browse the repository at this point in the history
  • Loading branch information
VHSgunzo committed Jun 23, 2024
1 parent dfba2dd commit dd3b19b
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions share/tunnel/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,44 @@ func (t *Tunnel) BindRemotes(ctx context.Context, remotes []*settings.Remote) er
}

func (t *Tunnel) keepAliveLoop(sshConn ssh.Conn) {
//ping forever
// ping forever with a timeout
PingCheckOLoop:
for {
time.Sleep(t.Config.KeepAlive)
_, b, err := sshConn.SendRequest("ping", true, nil)
if err != nil {
break
}
if len(b) > 0 && !bytes.Equal(b, []byte("pong")) {
t.Debugf("strange ping response")
break

ctx, cancel := context.WithTimeout(context.Background(), t.Config.KeepAlive)
defer cancel()

responseCh := make(chan []byte, 1)
errCh := make(chan error, 1)

// Asynchronously send a 'ping' request via SSH
go func() {
_, b, err := sshConn.SendRequest("ping", true, nil)
if err != nil {
errCh <- err
return
}
responseCh <- b
}()

// Wait for a response, error, or timeout from the asynchronous 'ping' request
select {
case response := <-responseCh:
if len(response) > 0 && !bytes.Equal(response, []byte("pong")) {
t.Debugf("Unexpected ping response: %s", response)
break PingCheckOLoop
}
case err := <-errCh:
if err != nil {
t.Debugf("Failed to send ping: %s", err)
break PingCheckOLoop
}
case <-ctx.Done():
t.Debugf("Ping timed out")
break PingCheckOLoop
}
}
//close ssh connection on abnormal ping
// Close the SSH connection on abnormal ping
sshConn.Close()
}

0 comments on commit dd3b19b

Please sign in to comment.