-
Notifications
You must be signed in to change notification settings - Fork 0
/
smasher.go
126 lines (103 loc) · 3.02 KB
/
smasher.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
package hulksmash
import (
"context"
"io/ioutil"
"log"
nethttp "net/http"
"time"
"github.com/ynori7/hulksmash/anonymizer"
"github.com/ynori7/hulksmash/http"
"github.com/ynori7/hulksmash/sequence"
"github.com/ynori7/workerpool"
)
// SuccessResponse is sent to the success callback. This response contains the original request as well as the response.
type SuccessResponse struct {
StatusCode int
RequestBody []byte
ResponseBody []byte
RawRequest *nethttp.Request
RawResponse *nethttp.Response
}
type HttpClient interface {
Do(req *nethttp.Request) (*nethttp.Response, error)
}
type (
// BuildRequestFunc is a function which accepts a iteration item and returns an http request
BuildRequestFunc func(item string) (*nethttp.Request, error)
// SuccessResponseCallback is a callback function which is called after a successful http request is performed
SuccessResponseCallback func(resp SuccessResponse)
)
type smasher struct {
anonymizeRequets bool
anonymizer anonymizer.Anonymizer
client HttpClient
iterations int
startIndex int
sequenceFunc sequence.SequenceFunc
workers int
onError func(err error)
onSuccess SuccessResponseCallback
}
// NewSmasher returns a new smasher with the specified configuration
func NewSmasher(options ...SmasherOption) *smasher {
s := &smasher{
anonymizer: anonymizer.New(time.Now().UnixNano()),
// Set defaults
iterations: defaultIterations,
startIndex: defaultStartIndex,
client: http.NewClient(),
workers: defaultWorkerCount,
onError: defaultOnError,
onSuccess: defaultSuccessResponseCallback,
anonymizeRequets: defaultAnonymizeRequests,
sequenceFunc: sequence.Numeric,
}
// apply options
for _, opt := range options {
opt(s)
}
return s
}
// Smash will perform the configured requests repeatedly based on the configuration
func (s *smasher) Smash(ctx context.Context, buildRequest BuildRequestFunc) {
workerPool := workerpool.NewWorkerPool(
func(result interface{}) { //will be a SuccessResponse
resp := result.(SuccessResponse)
s.onSuccess(resp)
},
s.onError,
func(job interface{}) (result interface{}, err error) {
item := job.(string)
req, err := buildRequest(item)
if err != nil {
return nil, err
}
if s.anonymizeRequets {
s.anonymizer.AnonymizeRequest(req) //disguise the traffic
}
resp, err := s.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
successResp := SuccessResponse{
StatusCode: resp.StatusCode,
RawRequest: req,
RawResponse: resp,
}
if req.GetBody != nil {
b, _ := req.GetBody()
successResp.RequestBody, _ = ioutil.ReadAll(b)
}
successResp.ResponseBody, _ = ioutil.ReadAll(resp.Body)
return successResp, nil
})
list := s.sequenceFunc(s.startIndex, s.startIndex+s.iterations)
if err := workerPool.Work(
ctx,
s.workers, //The number of workers which should work in parallel
list, //The items to be processed
); err != nil {
log.Println(err.Error())
}
}