forked from projectdiscovery/nuclei
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogress.go
281 lines (243 loc) · 8.4 KB
/
progress.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
package progress
import (
"context"
"encoding/json"
"fmt"
"os"
"strings"
"time"
"github.com/projectdiscovery/clistats"
"github.com/projectdiscovery/gologger"
)
// Progress is an interface implemented by nuclei progress display
// driver.
type Progress interface {
// Stop stops the progress recorder.
Stop()
// Init inits the progress bar with initial details for scan
Init(hostCount int64, rulesCount int, requestCount int64)
// AddToTotal adds a value to the total request count
AddToTotal(delta int64)
// IncrementRequests increments the requests counter by 1.
IncrementRequests()
// SetRequests sets the counter by incrementing it with a delta
SetRequests(count uint64)
// IncrementMatched increments the matched counter by 1.
IncrementMatched()
// IncrementErrorsBy increments the error counter by count.
IncrementErrorsBy(count int64)
// IncrementFailedRequestsBy increments the number of requests counter by count
// along with errors.
IncrementFailedRequestsBy(count int64)
}
var _ Progress = &StatsTicker{}
// StatsTicker is a progress instance for showing program stats
type StatsTicker struct {
cloud bool
active bool
outputJSON bool
stats clistats.StatisticsClient
tickDuration time.Duration
}
// NewStatsTicker creates and returns a new progress tracking object.
func NewStatsTicker(duration int, active, outputJSON, cloud bool, port int) (Progress, error) {
var tickDuration time.Duration
if active && duration != -1 {
tickDuration = time.Duration(duration) * time.Second
} else {
tickDuration = -1
}
progress := &StatsTicker{}
statsOpts := &clistats.DefaultOptions
statsOpts.ListenPort = port
// metrics port is enabled by default and is not configurable with new version of clistats
// by default 63636 is used and than can be modified with -mp flag
stats, err := clistats.NewWithOptions(context.TODO(), statsOpts)
if err != nil {
return nil, err
}
// only print in verbose mode
gologger.Verbose().Msgf("Started metrics server at localhost:%v", stats.Options.ListenPort)
progress.cloud = cloud
progress.active = active
progress.stats = stats
progress.tickDuration = tickDuration
progress.outputJSON = outputJSON
return progress, nil
}
// Init initializes the progress display mechanism by setting counters, etc.
func (p *StatsTicker) Init(hostCount int64, rulesCount int, requestCount int64) {
p.stats.AddStatic("templates", rulesCount)
p.stats.AddStatic("hosts", hostCount)
p.stats.AddStatic("startedAt", time.Now())
p.stats.AddCounter("requests", uint64(0))
p.stats.AddCounter("errors", uint64(0))
p.stats.AddCounter("matched", uint64(0))
p.stats.AddCounter("total", uint64(requestCount))
if p.active {
var printCallbackFunc clistats.DynamicCallback
if p.outputJSON {
printCallbackFunc = printCallbackJSON
} else {
printCallbackFunc = p.makePrintCallback()
}
p.stats.AddDynamic("summary", printCallbackFunc)
if err := p.stats.Start(); err != nil {
gologger.Warning().Msgf("Couldn't start statistics: %s", err)
}
// Note: this is needed and is responsible for the tick event
p.stats.GetStatResponse(p.tickDuration, func(s string, err error) error {
if err != nil {
gologger.Warning().Msgf("Could not read statistics: %s\n", err)
}
return nil
})
}
}
// AddToTotal adds a value to the total request count
func (p *StatsTicker) AddToTotal(delta int64) {
p.stats.IncrementCounter("total", int(delta))
}
// IncrementRequests increments the requests counter by 1.
func (p *StatsTicker) IncrementRequests() {
p.stats.IncrementCounter("requests", 1)
}
// SetRequests sets the counter by incrementing it with a delta
func (p *StatsTicker) SetRequests(count uint64) {
value, _ := p.stats.GetCounter("requests")
delta := count - value
p.stats.IncrementCounter("requests", int(delta))
}
// IncrementMatched increments the matched counter by 1.
func (p *StatsTicker) IncrementMatched() {
p.stats.IncrementCounter("matched", 1)
}
// IncrementErrorsBy increments the error counter by count.
func (p *StatsTicker) IncrementErrorsBy(count int64) {
p.stats.IncrementCounter("errors", int(count))
}
// IncrementFailedRequestsBy increments the number of requests counter by count along with errors.
func (p *StatsTicker) IncrementFailedRequestsBy(count int64) {
// mimic dropping by incrementing the completed requests
p.stats.IncrementCounter("requests", int(count))
p.stats.IncrementCounter("errors", int(count))
}
func (p *StatsTicker) makePrintCallback() func(stats clistats.StatisticsClient) interface{} {
return func(stats clistats.StatisticsClient) interface{} {
builder := &strings.Builder{}
var duration time.Duration
if startedAt, ok := stats.GetStatic("startedAt"); ok {
if startedAtTime, ok := startedAt.(time.Time); ok {
duration = time.Since(startedAtTime)
builder.WriteString(fmt.Sprintf("[%s]", fmtDuration(duration)))
}
}
if templates, ok := stats.GetStatic("templates"); ok {
builder.WriteString(" | Templates: ")
builder.WriteString(clistats.String(templates))
}
if hosts, ok := stats.GetStatic("hosts"); ok {
builder.WriteString(" | Hosts: ")
builder.WriteString(clistats.String(hosts))
}
requests, okRequests := stats.GetCounter("requests")
total, okTotal := stats.GetCounter("total")
// If input is not given, total is 0 which cause percentage overflow
if total == 0 {
total = requests
}
if okRequests && okTotal && duration > 0 && !p.cloud {
builder.WriteString(" | RPS: ")
builder.WriteString(clistats.String(uint64(float64(requests) / duration.Seconds())))
}
if matched, ok := stats.GetCounter("matched"); ok {
builder.WriteString(" | Matched: ")
builder.WriteString(clistats.String(matched))
}
if errors, ok := stats.GetCounter("errors"); ok && !p.cloud {
builder.WriteString(" | Errors: ")
builder.WriteString(clistats.String(errors))
}
if okRequests && okTotal {
if p.cloud {
builder.WriteString(" | Task: ")
} else {
builder.WriteString(" | Requests: ")
}
builder.WriteString(clistats.String(requests))
builder.WriteRune('/')
builder.WriteString(clistats.String(total))
builder.WriteRune(' ')
builder.WriteRune('(')
//nolint:gomnd // this is not a magic number
builder.WriteString(clistats.String(uint64(float64(requests) / float64(total) * 100.0)))
builder.WriteRune('%')
builder.WriteRune(')')
builder.WriteRune('\n')
}
fmt.Fprintf(os.Stderr, "%s", builder.String())
return builder.String()
}
}
func printCallbackJSON(stats clistats.StatisticsClient) interface{} {
builder := &strings.Builder{}
if err := json.NewEncoder(builder).Encode(metricsMap(stats)); err == nil {
fmt.Fprintf(os.Stderr, "%s", builder.String())
}
return builder.String()
}
func metricsMap(stats clistats.StatisticsClient) map[string]interface{} {
results := make(map[string]interface{})
var (
startedAt time.Time
duration time.Duration
)
if stAt, ok := stats.GetStatic("startedAt"); ok {
startedAt = stAt.(time.Time)
duration = time.Since(startedAt)
}
results["startedAt"] = startedAt
results["duration"] = fmtDuration(duration)
templates, _ := stats.GetStatic("templates")
results["templates"] = clistats.String(templates)
hosts, _ := stats.GetStatic("hosts")
results["hosts"] = clistats.String(hosts)
matched, _ := stats.GetCounter("matched")
results["matched"] = clistats.String(matched)
requests, _ := stats.GetCounter("requests")
results["requests"] = clistats.String(requests)
total, _ := stats.GetCounter("total")
results["total"] = clistats.String(total)
results["rps"] = clistats.String(uint64(float64(requests) / duration.Seconds()))
errors, _ := stats.GetCounter("errors")
results["errors"] = clistats.String(errors)
// nolint:gomnd // this is not a magic number
percentData := (float64(requests) * float64(100)) / float64(total)
percent := clistats.String(uint64(percentData))
results["percent"] = percent
return results
}
// fmtDuration formats the duration for the time elapsed
func fmtDuration(d time.Duration) string {
d = d.Round(time.Second)
h := d / time.Hour
d -= h * time.Hour
m := d / time.Minute
d -= m * time.Minute
s := d / time.Second
return fmt.Sprintf("%d:%02d:%02d", h, m, s)
}
// Stop stops the progress bar execution
func (p *StatsTicker) Stop() {
if p.active {
// Print one final summary
if p.outputJSON {
printCallbackJSON(p.stats)
} else {
p.makePrintCallback()(p.stats)
}
if err := p.stats.Stop(); err != nil {
gologger.Warning().Msgf("Couldn't stop statistics: %s", err)
}
}
}