From 0c5c378d6e2e1bdccad22fd110580cfe88e81e05 Mon Sep 17 00:00:00 2001 From: Artem Nistratov Date: Wed, 27 Nov 2024 19:19:24 +0300 Subject: [PATCH] imitate ssh.Dial func for simplicity previously ScanHostKey ignored any SSH/network errors in case it managed to get host keys to make it more obvious we imitate `ssh.Dial` with `sshDial` func --- ssh/host_key.go | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/ssh/host_key.go b/ssh/host_key.go index cde1be88..e603d168 100644 --- a/ssh/host_key.go +++ b/ssh/host_key.go @@ -47,24 +47,31 @@ func ScanHostKey(host string, timeout time.Duration, clientHostKeyAlgos []string config.HostKeyAlgorithms = clientHostKeyAlgos } - ctx, cancel := context.WithTimeout(context.Background(), timeout) + err := sshDial(host, config) + + if len(col.knownKeys) > 0 { + return col.knownKeys, nil + } + + return col.knownKeys, err +} + +func sshDial(host string, config *ssh.ClientConfig) error { + ctx, cancel := context.WithTimeout(context.Background(), config.Timeout) defer cancel() - // support for ALL_PROXY ENV varaible + // this reads the ALL_PROXY environment varaible conn, err := proxy.Dial(ctx, "tcp", host) if err != nil { - return nil, err + return err } c, chans, reqs, err := ssh.NewClientConn(conn, host, config) if err != nil { - return nil, err + return err } client := ssh.NewClient(c, chans, reqs) defer client.Close() - if len(col.knownKeys) > 0 { - return col.knownKeys, nil - } - return col.knownKeys, err + return nil } // HostKeyCollector offers a StoreKey method which provides an