2020-07-04 23:00:11 +02:00
|
|
|
package progress
|
|
|
|
|
|
|
|
|
|
import (
|
2020-12-17 20:33:42 +05:30
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
2020-07-04 23:00:11 +02:00
|
|
|
"fmt"
|
2020-12-17 20:33:42 +05:30
|
|
|
"net"
|
|
|
|
|
"net/http"
|
|
|
|
|
"strconv"
|
2020-07-04 23:00:11 +02:00
|
|
|
"strings"
|
2020-07-31 23:07:33 +02:00
|
|
|
"time"
|
2020-08-25 23:24:31 +02:00
|
|
|
|
2020-11-01 19:42:25 +05:30
|
|
|
"github.com/projectdiscovery/clistats"
|
2020-08-25 23:24:31 +02:00
|
|
|
"github.com/projectdiscovery/gologger"
|
2020-07-04 23:00:11 +02:00
|
|
|
)
|
|
|
|
|
|
2020-11-01 19:42:25 +05:30
|
|
|
// Progress is a progress instance for showing program stats
|
2020-07-04 23:00:11 +02:00
|
|
|
type Progress struct {
|
2020-11-16 00:40:32 +01:00
|
|
|
active bool
|
2020-11-01 19:42:25 +05:30
|
|
|
tickDuration time.Duration
|
2020-12-17 20:33:42 +05:30
|
|
|
stats clistats.StatisticsClient
|
|
|
|
|
server *http.Server
|
2020-07-04 23:00:11 +02:00
|
|
|
}
|
|
|
|
|
|
2020-09-19 14:43:35 +02:00
|
|
|
// NewProgress creates and returns a new progress tracking object.
|
2020-12-17 20:33:42 +05:30
|
|
|
func NewProgress(active, metrics bool, port int) (*Progress, error) {
|
2020-11-01 19:42:25 +05:30
|
|
|
var tickDuration time.Duration
|
|
|
|
|
if active {
|
2020-11-16 23:00:09 +01:00
|
|
|
tickDuration = 5 * time.Second
|
2020-11-01 19:42:25 +05:30
|
|
|
} else {
|
|
|
|
|
tickDuration = -1
|
2020-07-29 23:11:17 +02:00
|
|
|
}
|
|
|
|
|
|
2020-12-17 20:33:42 +05:30
|
|
|
progress := &Progress{}
|
|
|
|
|
|
|
|
|
|
stats, err := clistats.New()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
progress.active = active
|
|
|
|
|
progress.stats = stats
|
|
|
|
|
progress.tickDuration = tickDuration
|
|
|
|
|
|
|
|
|
|
if metrics {
|
|
|
|
|
http.HandleFunc("/metrics", func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
|
metrics := progress.getMetrics()
|
|
|
|
|
_ = json.NewEncoder(w).Encode(metrics)
|
|
|
|
|
})
|
|
|
|
|
progress.server = &http.Server{
|
|
|
|
|
Addr: net.JoinHostPort("127.0.0.1", strconv.Itoa(port)),
|
|
|
|
|
Handler: http.DefaultServeMux,
|
2020-11-16 00:40:32 +01:00
|
|
|
}
|
2020-12-17 20:33:42 +05:30
|
|
|
go func() {
|
|
|
|
|
if err := progress.server.ListenAndServe(); err != nil {
|
2020-12-25 20:33:52 +05:30
|
|
|
gologger.Warning().Msgf("Could not serve metrics: %s", err)
|
2020-12-17 20:33:42 +05:30
|
|
|
}
|
|
|
|
|
}()
|
2020-07-04 23:00:11 +02:00
|
|
|
}
|
2020-12-17 20:33:42 +05:30
|
|
|
return progress, nil
|
2020-07-04 23:00:11 +02:00
|
|
|
}
|
|
|
|
|
|
2020-11-01 19:42:25 +05:30
|
|
|
// Init initializes the progress display mechanism by setting counters, etc.
|
|
|
|
|
func (p *Progress) Init(hostCount int64, rulesCount int, requestCount int64) {
|
2020-12-17 20:33:42 +05:30
|
|
|
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))
|
2021-01-16 12:26:38 +05:30
|
|
|
p.stats.AddCounter("matched", uint64(0))
|
2020-12-17 20:33:42 +05:30
|
|
|
p.stats.AddCounter("total", uint64(requestCount))
|
|
|
|
|
|
2020-11-16 00:40:32 +01:00
|
|
|
if p.active {
|
|
|
|
|
if err := p.stats.Start(makePrintCallback(), p.tickDuration); err != nil {
|
2020-12-25 20:33:52 +05:30
|
|
|
gologger.Warning().Msgf("Couldn't start statistics: %s", err)
|
2020-11-16 00:40:32 +01:00
|
|
|
}
|
|
|
|
|
}
|
2020-07-25 21:03:18 +02:00
|
|
|
}
|
|
|
|
|
|
2020-11-01 19:42:25 +05:30
|
|
|
// AddToTotal adds a value to the total request count
|
2020-07-27 00:00:06 +02:00
|
|
|
func (p *Progress) AddToTotal(delta int64) {
|
2020-12-17 20:33:42 +05:30
|
|
|
p.stats.IncrementCounter("total", int(delta))
|
2020-07-27 00:00:06 +02:00
|
|
|
}
|
|
|
|
|
|
2020-12-25 20:33:52 +05:30
|
|
|
// IncrementRequests increments the requests counter by 1.
|
|
|
|
|
func (p *Progress) IncrementRequests() {
|
2020-12-17 20:33:42 +05:30
|
|
|
p.stats.IncrementCounter("requests", 1)
|
2020-07-04 23:00:11 +02:00
|
|
|
}
|
|
|
|
|
|
2021-01-16 12:26:38 +05:30
|
|
|
// IncrementMatched increments the matched counter by 1.
|
|
|
|
|
func (p *Progress) IncrementMatched() {
|
|
|
|
|
p.stats.IncrementCounter("matched", 1)
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-25 20:33:52 +05:30
|
|
|
// DecrementRequests decrements the number of requests from total.
|
|
|
|
|
func (p *Progress) DecrementRequests(count int64) {
|
2020-12-17 20:33:42 +05:30
|
|
|
// mimic dropping by incrementing the completed requests
|
2020-12-25 20:33:52 +05:30
|
|
|
p.stats.IncrementCounter("requests", int(count))
|
2020-12-17 20:33:42 +05:30
|
|
|
p.stats.IncrementCounter("errors", int(count))
|
2020-07-06 00:09:58 +02:00
|
|
|
}
|
|
|
|
|
|
2020-11-01 21:58:19 +05:30
|
|
|
const bufferSize = 128
|
|
|
|
|
|
2020-11-01 19:42:25 +05:30
|
|
|
func makePrintCallback() func(stats clistats.StatisticsClient) {
|
|
|
|
|
builder := &strings.Builder{}
|
2020-11-01 21:58:19 +05:30
|
|
|
builder.Grow(bufferSize)
|
2020-11-01 19:42:25 +05:30
|
|
|
|
|
|
|
|
return func(stats clistats.StatisticsClient) {
|
|
|
|
|
builder.WriteRune('[')
|
|
|
|
|
startedAt, _ := stats.GetStatic("startedAt")
|
|
|
|
|
duration := time.Since(startedAt.(time.Time))
|
|
|
|
|
builder.WriteString(fmtDuration(duration))
|
|
|
|
|
builder.WriteRune(']')
|
|
|
|
|
|
|
|
|
|
templates, _ := stats.GetStatic("templates")
|
2020-11-01 21:58:19 +05:30
|
|
|
builder.WriteString(" | Templates: ")
|
2020-11-01 19:42:25 +05:30
|
|
|
builder.WriteString(clistats.String(templates))
|
|
|
|
|
hosts, _ := stats.GetStatic("hosts")
|
2020-11-01 21:58:19 +05:30
|
|
|
builder.WriteString(" | Hosts: ")
|
2020-11-01 19:42:25 +05:30
|
|
|
builder.WriteString(clistats.String(hosts))
|
|
|
|
|
|
|
|
|
|
requests, _ := stats.GetCounter("requests")
|
|
|
|
|
total, _ := stats.GetCounter("total")
|
2020-11-01 21:58:19 +05:30
|
|
|
|
2020-11-01 22:04:55 +05:30
|
|
|
builder.WriteString(" | RPS: ")
|
2020-11-01 21:58:19 +05:30
|
|
|
builder.WriteString(clistats.String(uint64(float64(requests) / duration.Seconds())))
|
|
|
|
|
|
2021-01-16 12:26:38 +05:30
|
|
|
matched, _ := stats.GetCounter("matched")
|
|
|
|
|
|
|
|
|
|
builder.WriteString(" | Matched: ")
|
|
|
|
|
builder.WriteString(clistats.String(matched))
|
|
|
|
|
|
2020-11-01 21:58:19 +05:30
|
|
|
errors, _ := stats.GetCounter("errors")
|
|
|
|
|
builder.WriteString(" | Errors: ")
|
|
|
|
|
builder.WriteString(clistats.String(errors))
|
|
|
|
|
|
|
|
|
|
builder.WriteString(" | Requests: ")
|
2020-11-01 19:42:25 +05:30
|
|
|
builder.WriteString(clistats.String(requests))
|
|
|
|
|
builder.WriteRune('/')
|
|
|
|
|
builder.WriteString(clistats.String(total))
|
|
|
|
|
builder.WriteRune(' ')
|
|
|
|
|
builder.WriteRune('(')
|
2020-11-01 21:58:19 +05:30
|
|
|
//nolint:gomnd // this is not a magic number
|
2020-11-01 19:42:25 +05:30
|
|
|
builder.WriteString(clistats.String(uint64(float64(requests) / float64(total) * 100.0)))
|
|
|
|
|
builder.WriteRune('%')
|
|
|
|
|
builder.WriteRune(')')
|
2020-11-01 21:58:19 +05:30
|
|
|
builder.WriteRune('\n')
|
2020-11-01 19:42:25 +05:30
|
|
|
|
2021-01-17 00:51:43 +05:30
|
|
|
gologger.Print().Msgf("%s", builder.String())
|
2020-11-01 19:42:25 +05:30
|
|
|
builder.Reset()
|
2020-07-27 00:00:06 +02:00
|
|
|
}
|
2020-08-01 21:44:14 +02:00
|
|
|
}
|
2020-08-01 15:07:04 +02:00
|
|
|
|
2020-12-17 20:33:42 +05:30
|
|
|
// getMetrics returns a map of important metrics for client
|
|
|
|
|
func (p *Progress) getMetrics() map[string]interface{} {
|
|
|
|
|
results := make(map[string]interface{})
|
|
|
|
|
|
|
|
|
|
startedAt, _ := p.stats.GetStatic("startedAt")
|
|
|
|
|
duration := time.Since(startedAt.(time.Time))
|
|
|
|
|
|
|
|
|
|
results["startedAt"] = startedAt.(time.Time)
|
|
|
|
|
results["duration"] = fmtDuration(duration)
|
|
|
|
|
templates, _ := p.stats.GetStatic("templates")
|
|
|
|
|
results["templates"] = clistats.String(templates)
|
|
|
|
|
hosts, _ := p.stats.GetStatic("hosts")
|
|
|
|
|
results["hosts"] = clistats.String(hosts)
|
2021-01-16 12:26:38 +05:30
|
|
|
matched, _ := p.stats.GetStatic("matched")
|
|
|
|
|
results["matched"] = clistats.String(matched)
|
2020-12-17 20:33:42 +05:30
|
|
|
requests, _ := p.stats.GetCounter("requests")
|
|
|
|
|
results["requests"] = clistats.String(requests)
|
|
|
|
|
total, _ := p.stats.GetCounter("total")
|
|
|
|
|
results["total"] = clistats.String(total)
|
|
|
|
|
results["rps"] = clistats.String(uint64(float64(requests) / duration.Seconds()))
|
|
|
|
|
errors, _ := p.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
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-01 19:42:25 +05:30
|
|
|
// 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)
|
2020-07-31 23:07:33 +02:00
|
|
|
}
|
|
|
|
|
|
2020-11-01 19:42:25 +05:30
|
|
|
// Stop stops the progress bar execution
|
|
|
|
|
func (p *Progress) Stop() {
|
2020-11-16 00:40:32 +01:00
|
|
|
if p.active {
|
|
|
|
|
if err := p.stats.Stop(); err != nil {
|
2020-12-25 20:33:52 +05:30
|
|
|
gologger.Warning().Msgf("Couldn't stop statistics: %s", err)
|
2020-11-16 00:40:32 +01:00
|
|
|
}
|
|
|
|
|
}
|
2020-12-19 00:10:58 +05:30
|
|
|
if p.server != nil {
|
|
|
|
|
_ = p.server.Shutdown(context.Background())
|
|
|
|
|
}
|
2020-08-01 15:07:04 +02:00
|
|
|
}
|