Refactor progress functionality to correct modules (#33)

This commit is contained in:
Joona Hoikkala 2019-04-28 01:08:09 +03:00 committed by GitHub
parent 5264d85fc6
commit 45bffbffca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 68 additions and 45 deletions

View File

@ -35,6 +35,7 @@ type Config struct {
FollowRedirects bool FollowRedirects bool
AutoCalibration bool AutoCalibration bool
Timeout int Timeout int
ProgressFrequency int
Delay optRange Delay optRange
Filters []FilterProvider Filters []FilterProvider
Matchers []FilterProvider Matchers []FilterProvider
@ -63,6 +64,8 @@ func NewConfig(ctx context.Context) Config {
conf.Delay = optRange{0, 0, false, false} conf.Delay = optRange{0, 0, false, false}
conf.Extensions = make([]string, 0) conf.Extensions = make([]string, 0)
conf.Timeout = 10 conf.Timeout = 10
// Progress update frequency, in milliseconds
conf.ProgressFrequency = 100
conf.DirSearchCompat = false conf.DirSearchCompat = false
return conf return conf
} }

View File

@ -23,7 +23,7 @@ type InputProvider interface {
type OutputProvider interface { type OutputProvider interface {
Banner() error Banner() error
Finalize() error Finalize() error
Progress(status string) Progress(status Progress)
Error(errstring string) Error(errstring string)
Warning(warnstring string) Warning(warnstring string)
Result(resp Response) Result(resp Response)

View File

@ -115,26 +115,18 @@ func (j *Job) runProgress(wg *sync.WaitGroup) {
if j.Counter == totalProgress { if j.Counter == totalProgress {
return return
} }
time.Sleep(time.Millisecond * 100) time.Sleep(time.Millisecond * time.Duration(j.Config.ProgressFrequency))
} }
} }
func (j *Job) updateProgress() { func (j *Job) updateProgress() {
runningSecs := int((time.Now().Sub(j.startTime)) / time.Second) prog := Progress{
var reqRate int StartedAt: j.startTime,
if runningSecs > 0 { ReqCount: j.Counter,
reqRate = int(j.Counter / runningSecs) ReqTotal: j.Input.Total(),
} else { ErrorCount: j.ErrorCounter,
reqRate = 0
} }
dur := time.Now().Sub(j.startTime) j.Output.Progress(prog)
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)
} }
//Calibrate runs a self-calibration task for filtering options, requesting random resources and acting accordingly //Calibrate runs a self-calibration task for filtering options, requesting random resources and acting accordingly

12
pkg/ffuf/progress.go Normal file
View File

@ -0,0 +1,12 @@
package ffuf
import (
"time"
)
type Progress struct {
StartedAt time.Time
ReqCount int
ReqTotal int
ErrorCount int
}

View File

@ -3,6 +3,7 @@ package output
import ( import (
"fmt" "fmt"
"os" "os"
"time"
"github.com/ffuf/ffuf/pkg/ffuf" "github.com/ffuf/ffuf/pkg/ffuf"
) )
@ -52,13 +53,28 @@ func (s *Stdoutput) Banner() error {
return nil return nil
} }
func (s *Stdoutput) Progress(status string) { func (s *Stdoutput) Progress(status ffuf.Progress) {
if s.config.Quiet { if s.config.Quiet {
// No progress for quiet mode // No progress for quiet mode
return 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) { func (s *Stdoutput) Error(errstring string) {