优化日志输出, 新增总的命令行进度条

This commit is contained in:
M09Ic 2022-11-21 11:35:38 +08:00
parent be0fc35cab
commit 5dddf1933c
4 changed files with 40 additions and 22 deletions

View File

@ -57,7 +57,6 @@ func NewCheckPool(ctx context.Context, config *pkg.Config) (*CheckPool, error) {
bl.Collect() bl.Collect()
} }
// 异步进行性能消耗较大的深度对比
pool.OutputCh <- bl pool.OutputCh <- bl
pool.reqCount++ pool.reqCount++
pool.wg.Done() pool.wg.Done()

View File

@ -90,7 +90,7 @@ func (opt *Option) PrepareRunner() (*Runner, error) {
Deadline: opt.Deadline, Deadline: opt.Deadline,
Offset: opt.Offset, Offset: opt.Offset,
Limit: opt.Limit, Limit: opt.Limit,
URLCh: make(chan string), urlCh: make(chan string),
OutputCh: make(chan *pkg.Baseline, 100), OutputCh: make(chan *pkg.Baseline, 100),
FuzzyCh: make(chan *pkg.Baseline, 100), FuzzyCh: make(chan *pkg.Baseline, 100),
Fuzzy: opt.Fuzzy, Fuzzy: opt.Fuzzy,
@ -173,20 +173,8 @@ func (opt *Option) PrepareRunner() (*Runner, error) {
urls = strings.Split(string(content), "\n") urls = strings.Split(string(content), "\n")
} }
for i, u := range urls {
urls[i] = strings.TrimSpace(u)
}
logs.Log.Importantf("load %d urls from %s", len(urls), urlfrom)
if !opt.CheckOnly {
go func() {
for _, u := range urls {
r.URLCh <- u
}
close(r.URLCh)
}()
} else {
r.URLList = urls r.URLList = urls
} logs.Log.Importantf("load %d urls from %s", len(urls), urlfrom)
// prepare word // prepare word
dicts := make([][]string, len(opt.Dictionaries)) dicts := make([][]string, len(opt.Dictionaries))

View File

@ -2,6 +2,7 @@ package internal
import ( import (
"context" "context"
"fmt"
"github.com/chainreactors/files" "github.com/chainreactors/files"
"github.com/chainreactors/logs" "github.com/chainreactors/logs"
"github.com/chainreactors/spray/pkg" "github.com/chainreactors/spray/pkg"
@ -9,6 +10,7 @@ import (
"github.com/gosuri/uiprogress" "github.com/gosuri/uiprogress"
"github.com/panjf2000/ants/v2" "github.com/panjf2000/ants/v2"
"net/http" "net/http"
"strings"
"sync" "sync"
"time" "time"
) )
@ -21,7 +23,11 @@ var (
) )
type Runner struct { type Runner struct {
URLCh chan string urlCh chan string
poolwg sync.WaitGroup
bar *uiprogress.Bar
finished int
URLList []string URLList []string
Wordlist []string Wordlist []string
Headers http.Header Headers http.Header
@ -29,7 +35,6 @@ type Runner struct {
Threads int Threads int
PoolSize int PoolSize int
Pools *ants.PoolWithFunc Pools *ants.PoolWithFunc
poolwg sync.WaitGroup
Timeout int Timeout int
Mod string Mod string
Probes []string Probes []string
@ -88,6 +93,22 @@ func (r *Runner) Prepare(ctx context.Context) error {
r.poolwg.Done() r.poolwg.Done()
}) })
} else { } else {
go func() {
for _, u := range r.URLList {
r.urlCh <- strings.TrimSpace(u)
}
close(r.urlCh)
}()
if len(r.URLList) > 0 {
r.bar = r.Progress.AddBar(len(r.URLList))
r.bar.PrependCompleted()
r.bar.PrependFunc(func(b *uiprogress.Bar) string {
return fmt.Sprintf("total progressive: %d/%d ", r.finished, len(r.URLList))
})
r.bar.AppendElapsed()
}
r.Pools, err = ants.NewPoolWithFunc(r.PoolSize, func(i interface{}) { r.Pools, err = ants.NewPoolWithFunc(r.PoolSize, func(i interface{}) {
u := i.(string) u := i.(string)
config := r.PrepareConfig() config := r.PrepareConfig()
@ -97,9 +118,10 @@ func (r *Runner) Prepare(ctx context.Context) error {
if err != nil { if err != nil {
logs.Log.Error(err.Error()) logs.Log.Error(err.Error())
pool.cancel() pool.cancel()
r.poolwg.Done() r.Done()
return return
} }
pool.bar = pkg.NewBar(u, r.Limit-r.Offset, r.Progress) pool.bar = pkg.NewBar(u, r.Limit-r.Offset, r.Progress)
err = pool.Init() err = pool.Init()
if err != nil { if err != nil {
@ -107,13 +129,13 @@ func (r *Runner) Prepare(ctx context.Context) error {
if !r.Force { if !r.Force {
// 如果没开启force, init失败将会关闭pool // 如果没开启force, init失败将会关闭pool
pool.cancel() pool.cancel()
r.poolwg.Done() r.Done()
return return
} }
} }
pool.Run(ctx, r.Offset, r.Limit) pool.Run(ctx, r.Offset, r.Limit)
r.poolwg.Done() r.Done()
}) })
} }
@ -132,7 +154,7 @@ Loop:
case <-ctx.Done(): case <-ctx.Done():
logs.Log.Error("cancel with deadline") logs.Log.Error("cancel with deadline")
break Loop break Loop
case u, ok := <-r.URLCh: case u, ok := <-r.urlCh:
if !ok { if !ok {
break Loop break Loop
} }
@ -142,6 +164,8 @@ Loop:
} }
r.poolwg.Wait() r.poolwg.Wait()
time.Sleep(100) // 延迟100ms, 等所有数据处理完毕
for { for {
if len(r.OutputCh) == 0 { if len(r.OutputCh) == 0 {
close(r.OutputCh) close(r.OutputCh)
@ -169,6 +193,7 @@ func (r *Runner) RunWithCheck(ctx context.Context) {
r.poolwg.Wait() r.poolwg.Wait()
stopCh <- struct{}{} stopCh <- struct{}{}
}() }()
Loop: Loop:
for { for {
select { select {
@ -190,6 +215,12 @@ Loop:
time.Sleep(100) // 延迟100ms, 等所有数据处理完毕 time.Sleep(100) // 延迟100ms, 等所有数据处理完毕
} }
func (r *Runner) Done() {
r.bar.Incr()
r.finished++
r.poolwg.Done()
}
func (r *Runner) Outputting() { func (r *Runner) Outputting() {
go func() { go func() {
var outFunc func(*pkg.Baseline) var outFunc func(*pkg.Baseline)

View File

@ -16,7 +16,7 @@ func NewBar(u string, total int, progress *uiprogress.Progress) *Bar {
metrics.Register(bar.url, bar.m) metrics.Register(bar.url, bar.m)
bar.PrependCompleted() bar.PrependCompleted()
bar.PrependFunc(func(b *uiprogress.Bar) string { bar.PrependFunc(func(b *uiprogress.Bar) string {
return fmt.Sprintf("%f/s %v/%v", bar.m.Rate1(), bar.m.Count(), bar.Bar.Total) return fmt.Sprintf("%f/s %d/%d", bar.m.Rate1(), bar.m.Count(), bar.Bar.Total)
}) })
bar.PrependFunc(func(b *uiprogress.Bar) string { bar.PrependFunc(func(b *uiprogress.Bar) string {
return u return u