-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathhost_key.go
49 lines (38 loc) · 1001 Bytes
/
host_key.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package proxy
import (
"net"
"golang.org/x/crypto/ssh"
)
type HostKey struct{}
func NewHostKey() HostKey {
return HostKey{}
}
func (h HostKey) Get(username, privateKey, serverURL string) (ssh.PublicKey, error) {
publicKeyChannel := make(chan ssh.PublicKey, 1)
dialErrorChannel := make(chan error)
if username == "" {
username = "jumpbox"
}
signer, err := ssh.ParsePrivateKey([]byte(privateKey))
if err != nil {
return nil, err
}
clientConfig := NewSSHClientConfig(username, keyScanCallback(publicKeyChannel), ssh.PublicKeys(signer))
go func() {
conn, err := ssh.Dial("tcp", serverURL, clientConfig)
if err != nil {
publicKeyChannel <- nil
dialErrorChannel <- err
return
}
defer conn.Close()
dialErrorChannel <- nil
}()
return <-publicKeyChannel, <-dialErrorChannel
}
func keyScanCallback(publicKeyChannel chan ssh.PublicKey) ssh.HostKeyCallback {
return func(_ string, _ net.Addr, key ssh.PublicKey) error {
publicKeyChannel <- key
return nil
}
}