-
Notifications
You must be signed in to change notification settings - Fork 0
/
hassh.go
59 lines (49 loc) · 1.07 KB
/
hassh.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
package hassh
import (
"crypto/md5"
"net"
"strings"
"sync"
"github.com/edutko/hassh-go/x/crypto/ssh"
)
type SSHInfo struct {
Banner string
Kexinit ssh.KexinitInfo
}
func Digest(info ssh.KexinitInfo) []byte {
kex := strings.Join(info.KexAlgos, ",")
cphr := strings.Join(info.PeerCiphers, ",")
macs := strings.Join(info.PeerMACs, ",")
comp := strings.Join(info.PeerCompression, ",")
all := strings.Join([]string{kex, cphr, macs, comp}, ";")
d := md5.Sum([]byte(all))
return d[:]
}
func Connect(host, port string) (SSHInfo, error) {
info := SSHInfo{}
connected := false
l := sync.Mutex{}
config := &ssh.ClientConfig{
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
BannerCallback: func(b string) error {
l.Lock()
defer l.Unlock()
info.Banner = b
return nil
},
KexinitCallback: func(ki ssh.KexinitInfo) {
l.Lock()
defer l.Unlock()
info.Kexinit = ki
connected = true
},
}
conn, err := ssh.Dial("tcp", net.JoinHostPort(host, port), config)
if err == nil {
_ = conn.Close()
}
if connected {
return info, nil
}
return info, err
}