优化statistor, 现在是线程安全的

This commit is contained in:
M09Ic 2022-12-11 04:21:42 +08:00
parent 71393bfeb4
commit faf0812858
4 changed files with 19 additions and 18 deletions

View File

@ -271,7 +271,7 @@ func (opt *Option) PrepareRunner() (*Runner, error) {
} }
taskfrom = "resume " + opt.ResumeFrom taskfrom = "resume " + opt.ResumeFrom
for _, stat := range stats { for _, stat := range stats {
tasks = append(tasks, &Task{baseUrl: stat.BaseUrl, offset: stat.Offset + stat.ReqNumber, total: r.Total}) tasks = append(tasks, &Task{baseUrl: stat.BaseUrl, offset: stat.Offset + stat.End, total: r.Total})
} }
} else { } else {
var file *os.File var file *os.File

View File

@ -15,6 +15,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"sync/atomic"
"time" "time"
) )
@ -37,6 +38,7 @@ func NewPool(ctx context.Context, config *pkg.Config) (*Pool, error) {
worder: words.NewWorder(config.Wordlist, config.Fns), worder: words.NewWorder(config.Wordlist, config.Fns),
baselines: make(map[int]*pkg.Baseline), baselines: make(map[int]*pkg.Baseline),
tempCh: make(chan *pkg.Baseline, config.Thread), tempCh: make(chan *pkg.Baseline, config.Thread),
checkCh: make(chan *Unit),
wg: sync.WaitGroup{}, wg: sync.WaitGroup{},
initwg: sync.WaitGroup{}, initwg: sync.WaitGroup{},
reqCount: 1, reqCount: 1,
@ -45,9 +47,8 @@ func NewPool(ctx context.Context, config *pkg.Config) (*Pool, error) {
pool.worder.Rules = pool.Rules pool.worder.Rules = pool.Rules
pool.worder.RunWithRules() pool.worder.RunWithRules()
p, _ := ants.NewPoolWithFunc(config.Thread, func(i interface{}) { p, _ := ants.NewPoolWithFunc(config.Thread, func(i interface{}) {
pool.Statistor.Total++ atomic.AddInt32(&pool.Statistor.ReqTotal, 1)
unit := i.(*Unit) unit := i.(*Unit)
req, err := pool.genReq(unit.path) req, err := pool.genReq(unit.path)
if err != nil { if err != nil {
@ -65,7 +66,7 @@ func NewPool(ctx context.Context, config *pkg.Config) (*Pool, error) {
var bl *pkg.Baseline var bl *pkg.Baseline
if reqerr != nil && reqerr != fasthttp.ErrBodyTooLarge { if reqerr != nil && reqerr != fasthttp.ErrBodyTooLarge {
pool.failedCount++ pool.failedCount++
pool.Statistor.FailedNumber++ atomic.AddInt32(&pool.Statistor.FailedNumber, 1)
bl = &pkg.Baseline{UrlString: pool.BaseURL + unit.path, IsValid: false, ErrString: reqerr.Error(), Reason: ErrRequestFailed.Error()} bl = &pkg.Baseline{UrlString: pool.BaseURL + unit.path, IsValid: false, ErrString: reqerr.Error(), Reason: ErrRequestFailed.Error()}
pool.failedBaselines = append(pool.failedBaselines, bl) pool.failedBaselines = append(pool.failedBaselines, bl)
} else { } else {
@ -313,7 +314,7 @@ Loop:
break Loop break Loop
} }
pool.Statistor.End++ pool.Statistor.End++
if pool.reqCount < offset { if int(pool.reqCount) < offset {
pool.reqCount++ pool.reqCount++
continue continue
} }
@ -340,7 +341,6 @@ Loop:
} }
} }
pool.wg.Wait() pool.wg.Wait()
pool.Statistor.ReqNumber = pool.reqCount
pool.Statistor.EndTime = time.Now().Unix() pool.Statistor.EndTime = time.Now().Unix()
pool.Close() pool.Close()
} }

View File

@ -136,7 +136,7 @@ func (r *Runner) Prepare(ctx context.Context) error {
r.Done() r.Done()
return return
} }
pool.Statistor.Total = r.Total
pool.bar = pkg.NewBar(config.BaseURL, t.total-t.offset, r.Progress) pool.bar = pkg.NewBar(config.BaseURL, t.total-t.offset, r.Progress)
err = pool.Init() err = pool.Init()
if err != nil { if err != nil {

View File

@ -23,13 +23,14 @@ func NewStatistor(url string) *Statistor {
type Statistor struct { type Statistor struct {
BaseUrl string `json:"url"` BaseUrl string `json:"url"`
Counts map[int]int `json:"counts"` Counts map[int]int `json:"counts"`
ReqNumber int `json:"req"` FailedNumber int32 `json:"failed"`
FailedNumber int `json:"failed"` ReqTotal int32 `json:"req_total"`
CheckNumber int `json:"check"` CheckNumber int `json:"check"`
FoundNumber int `json:"found"` FoundNumber int `json:"found"`
FilteredNumber int `json:"filtered"` FilteredNumber int `json:"filtered"`
FuzzyNumber int `json:"fuzzy"` FuzzyNumber int `json:"fuzzy"`
WafedNumber int `json:"wafed"` WafedNumber int `json:"wafed"`
End int `json:"end"` End int `json:"end"`
Offset int `json:"offset"` Offset int `json:"offset"`
Total int `json:"total"` Total int `json:"total"`
@ -42,7 +43,7 @@ type Statistor struct {
func (stat *Statistor) String() string { func (stat *Statistor) String() string {
var s strings.Builder var s strings.Builder
s.WriteString(fmt.Sprintf("[stat] %s took %d s, request total: %d, found: %d, check: %d, failed: %d", stat.BaseUrl, stat.EndTime-stat.StartTime, stat.ReqNumber, stat.FoundNumber, stat.CheckNumber, stat.FailedNumber)) s.WriteString(fmt.Sprintf("[stat] %s took %d s, request total: %d, finish: %d/%d, found: %d, check: %d, failed: %d", stat.BaseUrl, stat.EndTime-stat.StartTime, stat.ReqTotal, stat.End, stat.Total, stat.FoundNumber, stat.CheckNumber, stat.FailedNumber))
if stat.FuzzyNumber != 0 { if stat.FuzzyNumber != 0 {
s.WriteString(", fuzzy: " + strconv.Itoa(stat.FuzzyNumber)) s.WriteString(", fuzzy: " + strconv.Itoa(stat.FuzzyNumber))