nuclei/v2/internal/progress/progress.go

162 lines
4.6 KiB
Go
Raw Normal View History

2020-07-04 23:00:11 +02:00
package progress
import (
"fmt"
"github.com/logrusorgru/aurora"
"github.com/vbauerster/mpb/v5"
"github.com/vbauerster/mpb/v5/decor"
"os"
"strings"
"sync"
)
2020-07-11 22:57:44 +02:00
// Encapsulates progress tracking.
2020-07-04 23:00:11 +02:00
type Progress struct {
progress *mpb.Progress
bars map[string]*Bar
gbar *Bar
2020-07-09 21:20:00 +02:00
captureData *captureData
stdCaptureMutex *sync.Mutex
stdout *strings.Builder
stderr *strings.Builder
colorizer aurora.Aurora
2020-07-04 23:00:11 +02:00
}
2020-07-11 22:57:44 +02:00
// Creates and returns a new progress tracking object.
func NewProgress(noColor bool) *Progress {
2020-07-04 23:00:11 +02:00
p := &Progress{
progress: mpb.New(
mpb.WithOutput(os.Stderr),
mpb.PopCompletedMode(),
),
2020-07-09 21:20:00 +02:00
stdCaptureMutex: &sync.Mutex{},
stdout: &strings.Builder{},
stderr: &strings.Builder{},
colorizer: aurora.NewAurora(!noColor),
bars: make(map[string]*Bar),
2020-07-04 23:00:11 +02:00
}
return p
}
2020-07-11 22:57:44 +02:00
// Creates and returns a progress bar that tracks request progress for a specific template.
func (p *Progress) SetupTemplateProgressbar(templateId string, requestCount int64, priority int) {
if p.bars[templateId] != nil {
panic(fmt.Sprintf("A progressbar is already bound to [%s].", templateId))
2020-07-11 22:57:44 +02:00
}
color := p.colorizer
barName := color.Green(templateId).String()
bar := p.setupProgressbar(barName, requestCount, priority)
2020-07-11 22:57:44 +02:00
p.bars[templateId] = &Bar{
2020-07-11 22:57:44 +02:00
bar: bar,
total: requestCount,
initialTotal: requestCount,
}
2020-07-05 23:38:58 +02:00
}
2020-07-11 22:57:44 +02:00
// Creates and returns a progress bar that tracks all the requests progress.
// This is only useful when multiple templates are processed within the same run.
func (p *Progress) SetupGlobalProgressbar(hostCount int64, templateCount int, requestCount int64) {
if p.gbar != nil {
panic("A global progressbar is already present.")
}
color := p.colorizer
2020-07-11 22:57:44 +02:00
hostPlural := "host"
if hostCount > 1 {
hostPlural = "hosts"
}
barName := color.Sprintf(
color.Cyan("%d templates, %d %s"),
color.Bold(color.Cyan(templateCount)),
color.Bold(color.Cyan(hostCount)),
hostPlural)
2020-07-11 22:57:44 +02:00
bar := p.setupProgressbar(barName, requestCount, 0)
2020-07-11 22:57:44 +02:00
p.gbar = &Bar{
2020-07-11 22:57:44 +02:00
bar: bar,
total: requestCount,
initialTotal: requestCount,
}
}
// Update progress tracking information and increments the request counter by one unit.
// If a global progress bar is present it will be updated as well.
func (p *Progress) Update(templateId string) {
p.bars[templateId].increment()
if p.gbar != nil {
p.gbar.increment()
2020-07-11 22:57:44 +02:00
}
2020-07-04 23:00:11 +02:00
}
2020-07-11 22:57:44 +02:00
// Drops the specified number of requests from the progress bar total.
// This may be the case when uncompleted requests are encountered and shouldn't be part of the total count.
// If a global progress bar is present it will be updated as well.
func (p *Progress) Drop(templateId string, count int64) {
p.bars[templateId].drop(count)
if p.gbar != nil {
p.gbar.drop(count)
2020-07-11 22:57:44 +02:00
}
}
2020-07-11 22:57:44 +02:00
// Ensures that a progress bar's total count is up-to-date if during an enumeration there were uncompleted requests and
// wait for all the progress bars to finish.
// If a global progress bar is present it will be updated as well.
2020-07-04 23:00:11 +02:00
func (p *Progress) Wait() {
p.progress.Wait()
}
2020-07-11 22:57:44 +02:00
// Creates and returns a progress bar.
func (p *Progress) setupProgressbar(name string, total int64, priority int) *mpb.Bar {
color := p.colorizer
2020-07-11 22:57:44 +02:00
return p.progress.AddBar(
total,
mpb.BarPriority(priority),
2020-07-11 22:57:44 +02:00
mpb.BarNoPop(),
mpb.BarRemoveOnComplete(),
mpb.PrependDecorators(
decor.Name(fmt.Sprintf("[%s]", name), decor.WCSyncSpaceR),
decor.CountersNoUnit(color.Blue(" %d/%d").String(), decor.WCSyncSpace),
decor.NewPercentage(color.Bold("%d").String(), decor.WCSyncSpace),
2020-07-11 22:57:44 +02:00
),
mpb.AppendDecorators(
decor.AverageSpeed(0, color.Yellow("%.2f r/s ").String(), decor.WCSyncSpace),
decor.Elapsed(decor.ET_STYLE_GO, decor.WCSyncSpace),
2020-07-11 22:57:44 +02:00
decor.AverageETA(decor.ET_STYLE_GO, decor.WCSyncSpace),
),
)
}
2020-07-04 23:00:11 +02:00
2020-07-11 22:57:44 +02:00
// Starts capturing stdout and stderr instead of producing visual output that may interfere with the progress bars.
2020-07-04 23:00:11 +02:00
func (p *Progress) StartStdCapture() {
2020-07-09 21:20:00 +02:00
p.stdCaptureMutex.Lock()
2020-07-04 23:00:11 +02:00
p.captureData = startStdCapture()
}
2020-07-11 22:57:44 +02:00
// Stops capturing stdout and stderr and store both output to be shown later.
func (p *Progress) StopStdCapture() {
2020-07-04 23:00:11 +02:00
stopStdCapture(p.captureData)
p.stdout.Write(p.captureData.DataStdOut.Bytes())
p.stderr.Write(p.captureData.DataStdErr.Bytes())
2020-07-09 21:20:00 +02:00
p.stdCaptureMutex.Unlock()
}
2020-07-11 22:57:44 +02:00
// Writes the captured stdout data to stdout, if any.
func (p *Progress) ShowStdOut() {
if p.stdout.Len() > 0 {
fmt.Fprint(os.Stdout, p.stdout.String())
2020-07-04 23:00:11 +02:00
}
}
2020-07-11 22:57:44 +02:00
// Writes the captured stderr data to stderr, if any.
func (p *Progress) ShowStdErr() {
if p.stderr.Len() > 0 {
fmt.Fprint(os.Stderr, p.stderr.String())
}
2020-07-04 23:00:11 +02:00
}