-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
155 lines (131 loc) · 3.24 KB
/
main.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
package main
import (
"fmt"
"log"
"os"
"sync"
"time"
"github.com/jreisinger/waf-tester/waftest"
"github.com/jreisinger/waf-tester/wafyaml"
"github.com/schollz/progressbar/v3"
)
func init() {
// Set up CLI tool style of logging.
log.SetPrefix(os.Args[0] + ": ")
log.SetFlags(0) // no timestamp
}
// Take from https://goreleaser.com/cookbooks/using-main.version
var (
version = "dev"
commit = "none"
date = "unknown"
builtBy = "unknown"
)
func main() {
flags, err := ParseFlags()
if err != nil {
log.Fatalf("error parsing flags: %v", err)
}
if flags.Version {
fmt.Printf("waf-tester %s, commit %s, built at %s by %s\n",
version, commit, date, builtBy)
os.Exit(0)
}
if flags.Template {
fmt.Print(wafyaml.Template())
os.Exit(0)
}
testsToExecute, err := waftest.GetTests(flags.TestsPath, flags.Execute, flags.NoExecute, flags.Header, flags.LogsPath)
if err != nil {
log.Fatalf("cannot get tests: %v", err)
}
testsToExecuteCh := make(chan *waftest.Test)
var wg sync.WaitGroup
// Send the tests to execute down the channel.
wg.Add(1)
go func() {
defer close(testsToExecuteCh)
defer wg.Done()
for i := range testsToExecute {
test := &testsToExecute[i]
testsToExecuteCh <- test
}
}()
testsExecutedCh := make(chan *waftest.Test)
// Limit the number of requests (tests) per second.
rate := make(chan bool, flags.Rate)
for i := 0; i < cap(rate); i++ {
rate <- true
}
// Leaky bucket.
go func() {
ticker := time.NewTicker(time.Duration(1000/flags.Rate) * time.Millisecond)
defer ticker.Stop()
for range ticker.C {
_, ok := <-rate
// If this isn't going to run indefinitely, signal
// this to return by closing the rate channel.
if !ok {
return
}
}
}()
// Limit concurrency of requests.
conc := make(chan bool, flags.Concurrency)
client := waftest.NewHTTPClient(time.Second * time.Duration(flags.Timeout))
// Get the tests to execute from the channel. Send the executed ones down
// another channel. Spawn twice as many workers as the number of tests to
// execute.
for i := 0; i < len(testsToExecute)*2; i++ {
wg.Add(1)
go func(rate chan bool) {
defer wg.Done()
conc <- true
for t := range testsToExecuteCh {
rate <- true
t.Execute(flags.URL, client)
testsExecutedCh <- t
}
<-conc
}(rate)
}
go func() {
wg.Wait()
close(testsExecutedCh)
close(rate)
}()
var testsExecuted waftest.Tests
// Wait for all tests to finish so we can evaluate logs if needed.
bar := progressbar.Default(int64(len(testsToExecute)), "Executing tests")
for i := range testsExecutedCh {
bar.Add(1)
testsExecuted = append(testsExecuted, i)
}
if flags.LogsPath != "" {
var logsFound, i int
for {
i++
logsFound = testsExecuted.AddLogs(flags.LogsPath)
if logsFound >= len(testsExecuted) {
break
}
time.Sleep(time.Microsecond * 100) // wait a bit for logs to be written
if i > 10 { // don't wait forever
break
}
}
}
// Evaluate and print the tests.
for _, test := range testsExecuted {
test.Evaluate(flags.LogsPath)
if !flags.Verbose && flags.Print == "" {
continue
}
if flags.Verbose {
test.PrintVerbose(flags.Print)
} else {
test.Print(flags.Print)
}
}
waftest.PrintReport(testsToExecute)
}