From 45bffbffcaabfeb945d5026f3db77b68c7a99f74 Mon Sep 17 00:00:00 2001 From: Joona Hoikkala Date: Sun, 28 Apr 2019 01:08:09 +0300 Subject: [PATCH] Refactor progress functionality to correct modules (#33) --- pkg/ffuf/config.go | 55 ++++++++++++++++++++++-------------------- pkg/ffuf/interfaces.go | 2 +- pkg/ffuf/job.go | 22 ++++++----------- pkg/ffuf/progress.go | 12 +++++++++ pkg/output/stdout.go | 22 ++++++++++++++--- 5 files changed, 68 insertions(+), 45 deletions(-) create mode 100644 pkg/ffuf/progress.go diff --git a/pkg/ffuf/config.go b/pkg/ffuf/config.go index def26e5..1ad37f6 100644 --- a/pkg/ffuf/config.go +++ b/pkg/ffuf/config.go @@ -16,32 +16,33 @@ type optRange struct { } type Config struct { - StaticHeaders map[string]string - FuzzHeaders map[string]string - Extensions []string - DirSearchCompat bool - Method string - Url string - TLSVerify bool - Data string - Quiet bool - Colors bool - Wordlist string - OutputFile string - OutputFormat string - StopOn403 bool - StopOnErrors bool - StopOnAll bool - FollowRedirects bool - AutoCalibration bool - Timeout int - Delay optRange - Filters []FilterProvider - Matchers []FilterProvider - Threads int - Context context.Context - ProxyURL func(*http.Request) (*url.URL, error) - CommandLine string + StaticHeaders map[string]string + FuzzHeaders map[string]string + Extensions []string + DirSearchCompat bool + Method string + Url string + TLSVerify bool + Data string + Quiet bool + Colors bool + Wordlist string + OutputFile string + OutputFormat string + StopOn403 bool + StopOnErrors bool + StopOnAll bool + FollowRedirects bool + AutoCalibration bool + Timeout int + ProgressFrequency int + Delay optRange + Filters []FilterProvider + Matchers []FilterProvider + Threads int + Context context.Context + ProxyURL func(*http.Request) (*url.URL, error) + CommandLine string } func NewConfig(ctx context.Context) Config { @@ -63,6 +64,8 @@ func NewConfig(ctx context.Context) Config { conf.Delay = optRange{0, 0, false, false} conf.Extensions = make([]string, 0) conf.Timeout = 10 + // Progress update frequency, in milliseconds + conf.ProgressFrequency = 100 conf.DirSearchCompat = false return conf } diff --git a/pkg/ffuf/interfaces.go b/pkg/ffuf/interfaces.go index 4f8e931..3ea076c 100644 --- a/pkg/ffuf/interfaces.go +++ b/pkg/ffuf/interfaces.go @@ -23,7 +23,7 @@ type InputProvider interface { type OutputProvider interface { Banner() error Finalize() error - Progress(status string) + Progress(status Progress) Error(errstring string) Warning(warnstring string) Result(resp Response) diff --git a/pkg/ffuf/job.go b/pkg/ffuf/job.go index f39dff7..a0d590c 100644 --- a/pkg/ffuf/job.go +++ b/pkg/ffuf/job.go @@ -115,26 +115,18 @@ func (j *Job) runProgress(wg *sync.WaitGroup) { if j.Counter == totalProgress { return } - time.Sleep(time.Millisecond * 100) + time.Sleep(time.Millisecond * time.Duration(j.Config.ProgressFrequency)) } } func (j *Job) updateProgress() { - runningSecs := int((time.Now().Sub(j.startTime)) / time.Second) - var reqRate int - if runningSecs > 0 { - reqRate = int(j.Counter / runningSecs) - } else { - reqRate = 0 + prog := Progress{ + StartedAt: j.startTime, + ReqCount: j.Counter, + ReqTotal: j.Input.Total(), + ErrorCount: j.ErrorCounter, } - dur := time.Now().Sub(j.startTime) - hours := dur / time.Hour - dur -= hours * time.Hour - mins := dur / time.Minute - dur -= mins * time.Minute - secs := dur / time.Second - progString := fmt.Sprintf(":: Progress: [%d/%d] :: %d req/sec :: Duration: [%d:%02d:%02d] :: Errors: %d ::", j.Counter, j.Total, int(reqRate), hours, mins, secs, j.ErrorCounter) - j.Output.Progress(progString) + j.Output.Progress(prog) } //Calibrate runs a self-calibration task for filtering options, requesting random resources and acting accordingly diff --git a/pkg/ffuf/progress.go b/pkg/ffuf/progress.go new file mode 100644 index 0000000..316285a --- /dev/null +++ b/pkg/ffuf/progress.go @@ -0,0 +1,12 @@ +package ffuf + +import ( + "time" +) + +type Progress struct { + StartedAt time.Time + ReqCount int + ReqTotal int + ErrorCount int +} diff --git a/pkg/output/stdout.go b/pkg/output/stdout.go index 492f605..375d27a 100644 --- a/pkg/output/stdout.go +++ b/pkg/output/stdout.go @@ -3,6 +3,7 @@ package output import ( "fmt" "os" + "time" "github.com/ffuf/ffuf/pkg/ffuf" ) @@ -52,13 +53,28 @@ func (s *Stdoutput) Banner() error { return nil } -func (s *Stdoutput) Progress(status string) { +func (s *Stdoutput) Progress(status ffuf.Progress) { if s.config.Quiet { // No progress for quiet mode return - } else { - fmt.Fprintf(os.Stderr, "%s%s", TERMINAL_CLEAR_LINE, status) } + + dur := time.Now().Sub(status.StartedAt) + runningSecs := int(dur / time.Second) + var reqRate int + if runningSecs > 0 { + reqRate = int(status.ReqCount / runningSecs) + } else { + reqRate = 0 + } + + hours := dur / time.Hour + dur -= hours * time.Hour + mins := dur / time.Minute + dur -= mins * time.Minute + secs := dur / time.Second + + fmt.Fprintf(os.Stderr, "%s:: Progress: [%d/%d] :: %d req/sec :: Duration: [%d:%02d:%02d] :: Errors: %d ::", TERMINAL_CLEAR_LINE, status.ReqCount, status.ReqTotal, reqRate, hours, mins, secs, status.ErrorCount) } func (s *Stdoutput) Error(errstring string) {