-
Notifications
You must be signed in to change notification settings - Fork 7
/
hammer.go
82 lines (67 loc) · 1.13 KB
/
hammer.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
package main
import (
"fmt"
"golang.org/x/crypto/ssh"
)
type Hammer struct {
host string
done chan struct{}
num int
}
func NewHammer(host string, num int) *Hammer {
return &Hammer{
host: host,
num: num,
done: make(chan struct{}, 1),
}
}
func (h *Hammer) Wait() {
<-h.done
}
func (h *Hammer) Stop() {
close(h.done)
}
func (h *Hammer) Start() error {
key, err := MakeKey()
if err != nil {
return err
}
auth := []ssh.AuthMethod{
ssh.PublicKeys(key),
}
for i := 1; i <= h.num; i++ {
name := fmt.Sprintf("hammer%d", i)
err := h.connect(name, auth)
if err != nil {
return err
}
}
return nil
}
func (h *Hammer) connect(name string, auth []ssh.AuthMethod) error {
config := &ssh.ClientConfig{
User: name,
Auth: auth,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
conn, err := ssh.Dial("tcp", h.host, config)
if err != nil {
return err
}
session, err := conn.NewSession()
if err != nil {
return err
}
go func() {
h.Wait()
session.Close()
}()
r, w, err := NewSessionIO(session)
if err != nil {
return err
}
go func() {
Spam(NewActor(r, w))
}()
return nil
}