Respect color output user flag, remove unused Progress WaitGroup

dev
Manuel Bua 2020-07-12 18:09:29 +02:00
parent e877d613e4
commit d0174c047c
3 changed files with 33 additions and 17 deletions

View File

@ -20,29 +20,34 @@ type Progress struct {
stdCaptureMutex *sync.Mutex stdCaptureMutex *sync.Mutex
stdout *strings.Builder stdout *strings.Builder
stderr *strings.Builder stderr *strings.Builder
colorizer aurora.Aurora
} }
// Creates and returns a new progress tracking object. // Creates and returns a new progress tracking object.
func NewProgress(group *sync.WaitGroup) *Progress { func NewProgress(noColor bool) *Progress {
p := &Progress{ p := &Progress{
progress: mpb.New( progress: mpb.New(
mpb.WithWaitGroup(group),
mpb.WithOutput(os.Stderr), mpb.WithOutput(os.Stderr),
mpb.PopCompletedMode(), mpb.PopCompletedMode(),
), ),
stdCaptureMutex: &sync.Mutex{}, stdCaptureMutex: &sync.Mutex{},
stdout: &strings.Builder{}, stdout: &strings.Builder{},
stderr: &strings.Builder{}, stderr: &strings.Builder{},
colorizer: aurora.NewAurora(!noColor),
} }
return p return p
} }
// Creates and returns a progress bar that tracks request progress for a specific template. // Creates and returns a progress bar that tracks request progress for a specific template.
func (p *Progress) SetupTemplateProgressbar(templateIndex int, templateCount int, name string, requestCount int64) { func (p *Progress) SetupTemplateProgressbar(templateIndex int, templateCount int, name string, requestCount int64) {
barName := "[" + aurora.Green(name).String() + "]" color := p.colorizer
barName := "[" + color.Green(name).String() + "]"
if templateIndex > -1 && templateCount > -1 { if templateIndex > -1 && templateCount > -1 {
barName = aurora.Sprintf("[%d/%d] ", aurora.Bold(aurora.Cyan(templateIndex)), aurora.Cyan(templateCount)) + barName barName = color.Sprintf("[%d/%d] ",
color.Bold(color.Cyan(templateIndex)),
color.Cyan(templateCount)) + barName
} }
bar := p.setupProgressbar(barName, requestCount) bar := p.setupProgressbar(barName, requestCount)
@ -62,15 +67,17 @@ func (p *Progress) SetupTemplateProgressbar(templateIndex int, templateCount int
// Creates and returns a progress bar that tracks all the requests progress. // 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. // This is only useful when multiple templates are processed within the same run.
func (p *Progress) SetupGlobalProgressbar(hostCount int64, templateCount int, requestCount int64) { func (p *Progress) SetupGlobalProgressbar(hostCount int64, templateCount int, requestCount int64) {
color := p.colorizer
hostPlural := "host" hostPlural := "host"
if hostCount > 1 { if hostCount > 1 {
hostPlural = "hosts" hostPlural = "hosts"
} }
barName := "[" + aurora.Sprintf( barName := "[" + color.Sprintf(
aurora.Cyan("%d templates, %d %s"), color.Cyan("%d templates, %d %s"),
aurora.Bold(aurora.Cyan(templateCount)), color.Bold(color.Cyan(templateCount)),
aurora.Bold(aurora.Cyan(hostCount)), color.Bold(color.Cyan(hostCount)),
hostPlural) + "]" hostPlural) + "]"
bar := p.setupProgressbar(barName, requestCount) bar := p.setupProgressbar(barName, requestCount)
@ -118,17 +125,19 @@ func (p *Progress) Wait() {
// Creates and returns a progress bar. // Creates and returns a progress bar.
func (p *Progress) setupProgressbar(name string, total int64) *mpb.Bar { func (p *Progress) setupProgressbar(name string, total int64) *mpb.Bar {
color := p.colorizer
return p.progress.AddBar( return p.progress.AddBar(
total, total,
mpb.BarNoPop(), mpb.BarNoPop(),
mpb.BarRemoveOnComplete(), mpb.BarRemoveOnComplete(),
mpb.PrependDecorators( mpb.PrependDecorators(
decor.Name(name, decor.WCSyncSpaceR), decor.Name(name, decor.WCSyncSpaceR),
decor.CountersNoUnit(aurora.Blue(" %d/%d").String(), decor.WCSyncSpace), decor.CountersNoUnit(color.Blue(" %d/%d").String(), decor.WCSyncSpace),
decor.NewPercentage(aurora.Bold("%d").String(), decor.WCSyncSpace), decor.NewPercentage(color.Bold("%d").String(), decor.WCSyncSpace),
), ),
mpb.AppendDecorators( mpb.AppendDecorators(
decor.AverageSpeed(0, aurora.Yellow("%.2f r/s ").String(), decor.WCSyncSpace), decor.AverageSpeed(0, color.Yellow("%.2f r/s ").String(), decor.WCSyncSpace),
decor.Elapsed(decor.ET_STYLE_GO, decor.WCSyncSpace),
decor.AverageETA(decor.ET_STYLE_GO, decor.WCSyncSpace), decor.AverageETA(decor.ET_STYLE_GO, decor.WCSyncSpace),
), ),
) )

View File

@ -33,6 +33,9 @@ type Runner struct {
templatesConfig *nucleiConfig templatesConfig *nucleiConfig
// options contains configuration options for runner // options contains configuration options for runner
options *Options options *Options
// progress tracking
progress *progress.Progress
} }
// New creates a new client for running enumeration process. // New creates a new client for running enumeration process.
@ -99,6 +102,10 @@ func New(options *Options) (*Runner, error) {
} }
runner.output = output runner.output = output
} }
// Creates the progress tracking object
runner.progress = progress.NewProgress(runner.options.NoColor)
return runner, nil return runner, nil
} }
@ -125,18 +132,18 @@ func (r *Runner) RunEnumeration() {
r.options.Templates = newPath r.options.Templates = newPath
} }
p := r.progress
// Single yaml provided // Single yaml provided
if strings.HasSuffix(r.options.Templates, ".yaml") { if strings.HasSuffix(r.options.Templates, ".yaml") {
t, err := r.parse(r.options.Templates) t, err := r.parse(r.options.Templates)
// track progress
p := progress.NewProgress(nil)
switch t.(type) { switch t.(type) {
case *templates.Template: case *templates.Template:
var results bool var results bool
template := t.(*templates.Template) template := t.(*templates.Template)
// track single template progress
p.SetupTemplateProgressbar(-1, -1, template.ID, r.inputCount * template.GetHTTPRequestsCount()) p.SetupTemplateProgressbar(-1, -1, template.ID, r.inputCount * template.GetHTTPRequestsCount())
// process http requests // process http requests
@ -211,8 +218,7 @@ func (r *Runner) RunEnumeration() {
} }
} }
// track progress // track global progress
p := progress.NewProgress(nil)
p.SetupGlobalProgressbar(r.inputCount, len(matches), r.inputCount * totalRequests) p.SetupGlobalProgressbar(r.inputCount, len(matches), r.inputCount * totalRequests)
var results bool var results bool
@ -222,6 +228,7 @@ func (r *Runner) RunEnumeration() {
case *templates.Template: case *templates.Template:
template := t.(*templates.Template) template := t.(*templates.Template)
// track template progress
p.SetupTemplateProgressbar(i, totalTemplates, template.ID, r.inputCount * template.GetHTTPRequestsCount()) p.SetupTemplateProgressbar(i, totalTemplates, template.ID, r.inputCount * template.GetHTTPRequestsCount())
for _, request := range template.RequestsDNS { for _, request := range template.RequestsDNS {

View File

@ -35,7 +35,7 @@ func (n *NucleiVar) Call(args ...tengo.Object) (ret tengo.Object, err error) {
var gotResult bool var gotResult bool
// track progress // track progress
p := progress.NewProgress(nil) p := progress.NewProgress(false)
for _, template := range n.Templates { for _, template := range n.Templates {
if template.HTTPOptions != nil { if template.HTTPOptions != nil {