forked from fmgoncalves/bomber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bomber.go
200 lines (184 loc) · 4.66 KB
/
bomber.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"github.com/fmgoncalves/bomber/encoding/mail/sample"
"fmt"
"io"
"io/ioutil"
"net/smtp"
"flag"
"time"
"math/rand"
"strings"
"runtime"
"os"
)
const (
default_host = "localhost"
default_dir = "samples"
default_port = 25
)
func die(err error) {
if err != nil {
panic(fmt.Sprintf("%v", err))
}
return
}
func send_default_header(key string, value string, sample_keys map[string] struct{}, wc io.WriteCloser){
if _, contains := sample_keys[strings.ToLower(key)] ; !contains {
wc.Write([]byte(key + ":" + value + "\r\n"))
}
}
func hasCategory(s sample.MailSample, category string) bool {
c := strings.ToLower(category)
for _, s_category := range s.Type {
if c == strings.ToLower(s_category) {
return true
}
}
return false
}
func send_sample(host string, s sample.MailSample) error {
var err error
// Connect to the remote SMTP server.
var port int
if s.Port > 0 {
port = s.Port
} else {
port = default_port
}
c, err := smtp.Dial(fmt.Sprintf("%s:%d",host, port))
if err != nil { return err }
//die(err)
defer c.Quit()
// MAIL_FROM, TO
c.Mail(s.From)
c.Rcpt(s.To)
// DATA
wc, err := c.Data()
if err != nil { return err }
//die(err)
defer wc.Close()
// Default headers
keyz := s.DefinedHeaders()
send_default_header("From", s.From, keyz, wc)
send_default_header("To", s.To, keyz, wc)
send_default_header("Date", time.Now().UTC().Format(time.RFC822), keyz, wc)
send_default_header("Message-Id", fmt.Sprintf("<%v@crap>",rand.Int()), keyz, wc)
for _, h := range s.Headers {
wc.Write([]byte(h + "\r\n"))
}
wc.Write([]byte("\r\n"))
wc.Write([]byte(s.Body))
return err
}
func main() {
var (
SAMPLES_DIRECTORY string
HOST string
CATEGORY string
N_SENDERS int
N_MESSAGES int
THROTTLING float64
LIST_ONLY bool
RANDOM_SAMPLE_SELECTION bool
VERBOSE bool
)
flag.StringVar(&HOST, "s", default_host, "SMTP server to send the message to")
samples_dir := os.Getenv("BOMBER_SAMPLES")
if len(samples_dir) == 0 {
samples_dir = "samples"
}
flag.StringVar(&SAMPLES_DIRECTORY, "samples", samples_dir, "Directory containing email message samples in JSON")
flag.StringVar(&CATEGORY, "c", "all", "Category of messages to send")
flag.IntVar(&N_SENDERS, "j", 1, "Number of message senders in parallel")
flag.IntVar(&N_MESSAGES, "n", 1, "Total number of messages to be sent")
flag.Float64Var(&THROTTLING, "throttle", 0, "Throttle the message flow (msg/second).")
flag.BoolVar(&LIST_ONLY, "l", false, "Only list available categories.")
flag.BoolVar(&RANDOM_SAMPLE_SELECTION, "rand", false, "Choose samples randomly during execution.")
flag.BoolVar(&VERBOSE, "v", false, "Prints details of execution.")
flag.Parse()
rand.Seed(time.Now().UnixNano())
l, err := ioutil.ReadDir(SAMPLES_DIRECTORY)
die(err)
sample_list := make([]sample.MailSample,len(l))
categories := make(map[string]bool)
idx := 0
for _, val := range l {
if ! val.IsDir() && strings.HasSuffix(val.Name(),".json") {
s, err := sample.Unmarshal(SAMPLES_DIRECTORY+"/"+val.Name())
if err != nil {
if VERBOSE {
fmt.Printf("Failed to load %s sample: %v\n",val.Name(), err)
}
} else {
if VERBOSE {
fmt.Printf("Loaded %s sample\n",val.Name())
}
if LIST_ONLY {
for _, s_category := range s.Type {
asdfg := strings.ToLower(s_category)
categories[asdfg] = true
}
}
if CATEGORY == "all" || hasCategory(s,CATEGORY) {
sample_list[idx] = s
idx++
}
}
}
}
if LIST_ONLY {
fmt.Printf("Categories found in samples:\n")
for cat, _ := range categories {
fmt.Printf("\t%v\n",cat)
}
return
}
if ( idx > 0 ){
sample_list = sample_list[0:idx]
} else {
panic("No samples to use.")
}
pn := runtime.NumCPU()
//runtime.GOMAXPROCS(pn)
if N_SENDERS > 0 {
pn = int(N_SENDERS)
} else {
pn = 1
}
if VERBOSE {
fmt.Printf("Sending %v messages in batches of %v\n", N_MESSAGES, pn);
}
c := make(chan int, pn)
for i:= 0; i < pn; i++ {
c <- 1
}
for i:= 0; i < N_MESSAGES; i++ {
<-c
go func (j int) {
start_time := time.Now()
if VERBOSE {
fmt.Printf("Sending message #%v\n",j+1)
}
var samples_idx int
if RANDOM_SAMPLE_SELECTION {
samples_idx = rand.Intn(len(sample_list))
} else {
samples_idx = j % len(sample_list)
}
err := send_sample(HOST, sample_list[samples_idx])
if err != nil {
fmt.Printf("Failed to send message #%v (%v): %v\n", j+1, sample_list[samples_idx].SampleFileName, err)
}
if THROTTLING > 0 {
// TODO throttling should consider the running time of send_sample
time.Sleep( time.Duration( (float64(pn) /THROTTLING) * float64(time.Second) ) - time.Since(start_time) )
}
c <- 1
}(i)
}
for i:= 0; i < pn; i++ {
<- c
}
return
}