-
Notifications
You must be signed in to change notification settings - Fork 46
/
client.go
86 lines (79 loc) · 1.78 KB
/
client.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
/*
* client.go
* SSH client to connect upstream
* By J. Stuart McMurray
* Created 20160515
* Last Modified 20160517
*/
import (
"bytes"
"fmt"
"log"
"net"
"time"
"golang.org/x/crypto/ssh"
)
const TIMEOUT = time.Minute
/* clientDial dials the real server and makes an SSH client */
func clientDial(
addr string,
conf *ssh.ClientConfig,
) (ssh.Conn, <-chan ssh.NewChannel, <-chan *ssh.Request, error) {
/* Connect to the server */
c, err := net.Dial("tcp", addr)
if nil != err {
return nil, nil, nil, err
}
return ssh.NewClientConn(c, addr, conf)
//sc,chans,reqs,err :=
//func NewClientConn(c net.Conn, addr string, config *ClientConfig)
///* Connect to server */
//c, err := ssh.Dial("tcp", addr, conf)
//if nil != err {
// return nil, err
//}
//return c, nil
}
/* clientConfig makes an SSH client config which uses the given username and
key */
func makeClientConfig(user, key, fingerprint string) *ssh.ClientConfig {
/* Get SSH key */
k, g, err := getKey(key)
if nil != err {
log.Fatalf("Unable to get client key: %v", err)
}
if g {
log.Printf("Generated client key in %v", key)
log.Printf(
"Public Key: %s",
bytes.TrimSpace(ssh.MarshalAuthorizedKey(k.PublicKey())),
)
} else {
log.Printf("Loaded client key from %v", key)
}
/* Config to return */
cc := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(k),
},
Timeout: TIMEOUT,
}
/* Check key against provided fingerprint */
cc.HostKeyCallback = func(
hostname string,
remote net.Addr,
key ssh.PublicKey,
) error {
if fingerprint != ssh.FingerprintSHA256(key) &&
fingerprint != ssh.FingerprintLegacyMD5(key) {
return fmt.Errorf(
"Server host key fingerprint %v incorrect",
ssh.FingerprintSHA256(key),
)
}
return nil
}
return cc
}