allow unknown hosts to be accessed

This commit is contained in:
William Banfield
2022-11-30 18:37:35 -05:00
parent 3cfb90fb5b
commit 5a6b3abe27

View File

@@ -2,6 +2,8 @@ package ssh
import (
"errors"
"fmt"
"log"
"net"
"os"
"path/filepath"
@@ -49,8 +51,28 @@ func NewClientConfig() (*ssh.ClientConfig, error) {
}
return &ssh.ClientConfig{
User: "root",
HostKeyCallback: hkc,
HostKeyCallback: hkcWrapper(hkc),
Auth: am,
HostKeyAlgorithms: []string{ssh.KeyAlgoED25519},
}, nil
}
func hkcWrapper(hkc ssh.HostKeyCallback) ssh.HostKeyCallback {
return func(hostname string, remote net.Addr, key ssh.PublicKey) error {
err := hkc(hostname, remote, key)
if err == nil {
return nil
}
ke := &knownhosts.KeyError{}
if errors.As(err, &ke) && len(ke.Want) == 0 {
h, _, err := net.SplitHostPort(hostname)
if err != nil {
panic(fmt.Errorf("hostname incorrectly formatted: %w", err))
}
log.Printf("ignoring knownhosts error for unknown host: %s", h)
return nil
}
return err
}
}