[client] Deduplicate SSH client handshake and bound it with a deadline

Extract the dial-then-handshake sequence into nbssh.Handshake, which
applies the context deadline to the socket for the duration of the
handshake. Previously only the Android client did this; the CLI, wasm
and SSH proxy paths could block forever on a peer that accepts the TCP
connection and then goes silent, since ClientConfig.Timeout is not used
by NewClientConn.
This commit is contained in:
Zoltan Papp
2026-08-14 21:57:21 +02:00
parent 0bb49fa144
commit 1aa1f915a2
5 changed files with 61 additions and 37 deletions

View File

@@ -534,30 +534,11 @@ func (s *SSHClient) dialAndHandshake(gen uint64, host string, port int, clientCo
return fmt.Errorf("dial %s: %w", addr, err)
}
// DialContext bounds only the TCP establishment; without a deadline on the
// socket a peer that accepts and then goes silent blocks the handshake
// forever.
if deadline, ok := ctx.Deadline(); ok {
if err := conn.SetDeadline(deadline); err != nil {
closeQuiet(conn, "conn after deadline error")
return fmt.Errorf("set handshake deadline: %w", err)
}
}
sshConn, chans, reqs, err := gossh.NewClientConn(conn, addr, clientConfig)
client, err := nbssh.Handshake(ctx, conn, addr, clientConfig)
if err != nil {
if cerr := conn.Close(); cerr != nil {
log.Debugf("ssh: close after handshake error: %v", cerr)
}
return fmt.Errorf("ssh handshake: %w", err)
return err
}
if err := conn.SetDeadline(time.Time{}); err != nil {
closeQuiet(sshConn, "ssh conn after deadline clear error")
return fmt.Errorf("clear handshake deadline: %w", err)
}
client := gossh.NewClient(sshConn, chans, reqs)
s.mu.Lock()
if gen != s.gen {
s.mu.Unlock()