-
Notifications
You must be signed in to change notification settings - Fork 3
/
targets.go
285 lines (247 loc) · 5.47 KB
/
targets.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"log"
"math"
"math/rand"
"os"
"strings"
"sync"
"time"
)
const LETTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
const NUMS = "0123456789"
func randBytes(n int, letters string) []byte {
b := make([]byte, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return b
}
func randString(n int, letters string) string {
return string(randBytes(n, letters))
}
func randRunes(n int, letters string) []rune {
return bytes.Runes(randBytes(n, letters))
}
func startingSequenceLength(s []rune, startsWith rune) int {
matched := 0
for _, char := range s {
if char != startsWith {
break
}
matched++
}
return matched
}
type TemplatedTarget struct {
targets chan string
formatter URLFormatter
}
type replacementFunc func(requestNum int64) []rune
type templatePart struct {
length int
offset int
replacementFunc replacementFunc
}
type templatePartMatcher interface {
isMatching([]rune) (matchedLength int)
makeReplacementFunc([]rune) replacementFunc
}
type randomNumMatcher struct {
}
func (*randomNumMatcher) isMatching(s []rune) (matchedLength int) {
matched := startingSequenceLength(s, 'N')
if matched > 1 {
return matched
}
return 0
}
func (*randomNumMatcher) makeReplacementFunc(rr []rune) replacementFunc {
length := len(rr)
return func(requestNum int64) []rune {
return randRunes(length, NUMS)
}
}
type randomStringMatcher struct {
}
func (*randomStringMatcher) isMatching(s []rune) (matchedLength int) {
matched := startingSequenceLength(s, 'R')
if matched > 1 {
return matched
}
return 0
}
func (*randomStringMatcher) makeReplacementFunc(rr []rune) replacementFunc {
length := len(rr)
return func(requestNum int64) []rune {
return randRunes(length, LETTERS)
}
}
type incrementalMatcher struct {
}
func (*incrementalMatcher) isMatching(s []rune) (matchedLength int) {
matched := startingSequenceLength(s, 'X')
if matched > 1 {
return matched
}
return 0
}
func (*incrementalMatcher) makeReplacementFunc(rr []rune) replacementFunc {
length := len(rr)
format := fmt.Sprintf("%%0%dd", length)
limiter := int64(math.Pow(10.0, float64(length)))
return func(requestNum int64) []rune {
ret := []rune(fmt.Sprintf(format, requestNum%limiter))
return ret
}
}
type templateFormatter struct {
base []rune
templateParts []templatePart
}
var templatePartMatchers []templatePartMatcher
func init() {
templatePartMatchers = []templatePartMatcher{
&randomNumMatcher{},
&randomStringMatcher{},
&incrementalMatcher{},
}
}
func newTemplateFormatter(url string) *templateFormatter {
if url == "" {
panic("Must specify url for requests")
}
rurl := []rune(url)
formatter := templateFormatter{
base: rurl,
}
for i := 0; i < len(rurl); i++ {
for _, matcher := range templatePartMatchers {
if matched := matcher.isMatching(rurl[i:]); matched != 0 {
formatter.templateParts = append(formatter.templateParts, templatePart{
offset: i,
length: matched,
replacementFunc: matcher.makeReplacementFunc(rurl[i : i+matched]),
})
i += matched - 1
}
}
}
return &formatter
}
func (f *templateFormatter) format(requestNum int64) string {
rr := make([]rune, len(f.base))
copy(rr, f.base)
for _, part := range f.templateParts {
for i, replacementPart := range part.replacementFunc(requestNum) {
rr[part.offset+i] = replacementPart
}
}
return string(rr)
}
func (i *TemplatedTarget) get() string {
return <-i.targets
}
func newTemplatedTarget() *TemplatedTarget {
i := TemplatedTarget{}
i.formatter = newTemplateFormatter(config.url)
i.targets = make(chan string, 1000)
go func() {
n := int64(0)
for {
n++
i.targets <- i.formatter.format(n)
}
}()
return &i
}
type SourceFileTarget struct {
file string
targets chan string
}
func (s *SourceFileTarget) get() string {
return <-s.targets
}
func (s *SourceFileTarget) keepPopulated() string {
file, err := os.Open(s.file)
if err != nil {
log.Panic(err)
}
for {
file.Seek(0, io.SeekStart)
r := bufio.NewReaderSize(file, 64*1024)
for {
url, err := r.ReadString('\n')
if url != "" {
url := strings.TrimSpace(url)
s.targets <- url
}
if err == io.EOF {
break
}
}
}
}
func newSourceFileTarget() *SourceFileTarget {
i := SourceFileTarget{
file: config.urlsSourceFile,
}
i.targets = make(chan string, 1000)
go i.keepPopulated()
go func() {
}()
return &i
}
func selectTargetByConfig() Target {
if config.url != EmptyString {
return newTemplatedTarget()
} else if config.urlsSourceFile != EmptyString {
return newSourceFileTarget()
} else {
log.Println("None of [url/url-source] supplied")
os.Exit(1)
return nil
}
}
// BoundTarget will set number of requests randomaly selected from bound slice
type BoundTarget struct {
bound *[]string
sync sync.Locker
}
func (b *BoundTarget) get() (ret string) {
for {
b.sync.Lock()
urls := *b.bound
if len(urls) == 0 {
b.sync.Unlock()
time.Sleep(time.Millisecond)
continue
}
ret = urls[rand.Intn(int(len(urls)))]
b.sync.Unlock()
return
}
}
type BadUrlTarget struct{}
func (t *BadUrlTarget) get() string {
return "BAD_URL"
}
func dumpWrittenUrls(goodUrls []string) {
if config.writtenUrlsDump == EmptyString {
return
}
f, err := os.Create(config.writtenUrlsDump)
if err != nil {
log.Println("Error dumping urls:", err)
return
}
w := bufio.NewWriterSize(f, 1024*64)
for _, url := range goodUrls {
w.WriteString(url + "\n")
}
w.Flush()
}