实装deadline

修复多处bug
This commit is contained in:
M09Ic 2022-11-10 16:52:00 +08:00
parent 217bf0ab0f
commit 9f2223aeca
3 changed files with 49 additions and 20 deletions

View File

@ -1,10 +1,12 @@
package cmd package cmd
import ( import (
"context"
"fmt" "fmt"
"github.com/chainreactors/logs" "github.com/chainreactors/logs"
"github.com/chainreactors/spray/internal" "github.com/chainreactors/spray/internal"
"github.com/jessevdk/go-flags" "github.com/jessevdk/go-flags"
"time"
) )
func Spray() { func Spray() {
@ -24,10 +26,13 @@ func Spray() {
return return
} }
err = runner.Prepare() ctx, _ := context.WithTimeout(context.Background(), time.Duration(runner.Deadline)*time.Second)
err = runner.Prepare(ctx)
if err != nil { if err != nil {
logs.Log.Errorf(err.Error()) logs.Log.Errorf(err.Error())
return return
} }
runner.Run()
runner.Run(ctx)
} }

View File

@ -37,7 +37,7 @@ type InputOptions struct {
} }
type OutputOptions struct { type OutputOptions struct {
Matches map[string]string `short:"m" long:"match" description:"String, "` Matches map[string]string `long:"match" description:"String, "`
Filters map[string]string `long:"filter" description:"String, "` Filters map[string]string `long:"filter" description:"String, "`
Extracts []string `long:"extract" description:"String, "` Extracts []string `long:"extract" description:"String, "`
OutputFile string `short:"f" description:"String, output filename"` OutputFile string `short:"f" description:"String, output filename"`
@ -74,6 +74,10 @@ func (opt *Option) PrepareRunner() (*Runner, error) {
Mod: opt.Mod, Mod: opt.Mod,
Timeout: opt.Timeout, Timeout: opt.Timeout,
Probes: strings.Split(opt.OutputProbe, ","), Probes: strings.Split(opt.OutputProbe, ","),
Deadline: opt.Deadline,
Offset: opt.Offset,
Limit: opt.Limit,
URLList: make(chan string),
} }
err = pkg.LoadTemplates() err = pkg.LoadTemplates()
@ -91,10 +95,11 @@ func (opt *Option) PrepareRunner() (*Runner, error) {
} }
// prepare url // prepare url
var urls []string
var file *os.File var file *os.File
urlfrom := opt.URLFile urlfrom := opt.URLFile
if opt.URL != "" { if opt.URL != "" {
r.URLList = append(r.URLList, opt.URL) urls = append(urls, opt.URL)
urlfrom = "cmd" urlfrom = "cmd"
} else if opt.URLFile != "" { } else if opt.URLFile != "" {
file, err = os.Open(opt.URLFile) file, err = os.Open(opt.URLFile)
@ -111,13 +116,19 @@ func (opt *Option) PrepareRunner() (*Runner, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
r.URLList = strings.Split(string(content), "\n") urls = strings.Split(string(content), "\n")
} }
for i, u := range r.URLList { for i, u := range urls {
r.URLList[i] = strings.TrimSpace(u) urls[i] = strings.TrimSpace(u)
} }
logs.Log.Importantf("load %d urls from %s", len(r.URLList), urlfrom) logs.Log.Importantf("load %d urls from %s", len(urls), urlfrom)
go func() {
for _, u := range urls {
r.URLList <- u
}
close(r.URLList)
}()
// prepare word // prepare word
dicts := make([][]string, len(opt.Dictionaries)) dicts := make([][]string, len(opt.Dictionaries))
@ -134,10 +145,10 @@ func (opt *Option) PrepareRunner() (*Runner, error) {
for i, _ := range dicts { for i, _ := range dicts {
opt.Word += strconv.Itoa(i) opt.Word += strconv.Itoa(i)
} }
opt.Word = "}" opt.Word += "}"
} }
if opt.Suffixes == nil { if opt.Suffixes != nil {
dicts = append(dicts, opt.Suffixes) dicts = append(dicts, opt.Suffixes)
opt.Word += fmt.Sprintf("{?%d}", len(dicts)-1) opt.Word += fmt.Sprintf("{?%d}", len(dicts)-1)
} }
@ -156,6 +167,9 @@ func (opt *Option) PrepareRunner() (*Runner, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if r.Limit == 0 {
r.Limit = len(r.Wordlist)
}
if opt.Uppercase { if opt.Uppercase {
r.Fns = append(r.Fns, strings.ToUpper) r.Fns = append(r.Fns, strings.ToUpper)
@ -184,7 +198,7 @@ func (opt *Option) PrepareRunner() (*Runner, error) {
}) })
} }
if opt.Replaces != nil { if len(opt.Replaces) > 0 {
r.Fns = append(r.Fns, func(s string) string { r.Fns = append(r.Fns, func(s string) string {
for k, v := range opt.Replaces { for k, v := range opt.Replaces {
s = strings.Replace(s, k, v, -1) s = strings.Replace(s, k, v, -1)

View File

@ -15,7 +15,7 @@ var BlackStatus = []int{400, 404, 410}
var FuzzyStatus = []int{403, 500, 501, 502, 503} var FuzzyStatus = []int{403, 500, 501, 502, 503}
type Runner struct { type Runner struct {
URLList []string URLList chan string
Wordlist []string Wordlist []string
Headers http.Header Headers http.Header
Fns []func(string) string Fns []func(string) string
@ -30,9 +30,10 @@ type Runner struct {
Progress *uiprogress.Progress Progress *uiprogress.Progress
Offset int Offset int
Limit int Limit int
Deadline int
} }
func (r *Runner) Prepare() error { func (r *Runner) Prepare(ctx context.Context) error {
var err error var err error
CheckStatusCode = func(status int) bool { CheckStatusCode = func(status int) bool {
for _, black := range BlackStatus { for _, black := range BlackStatus {
@ -44,7 +45,6 @@ func (r *Runner) Prepare() error {
} }
r.OutputCh = make(chan *baseline, 100) r.OutputCh = make(chan *baseline, 100)
ctx := context.Background()
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)
@ -75,7 +75,7 @@ func (r *Runner) Prepare() error {
logs.Log.Error(err.Error()) logs.Log.Error(err.Error())
return return
} }
// todo pool 总超时时间
pool.Run(ctx, r.Offset, r.Limit) pool.Run(ctx, r.Offset, r.Limit)
r.poolwg.Done() r.poolwg.Done()
}) })
@ -87,12 +87,22 @@ func (r *Runner) Prepare() error {
return nil return nil
} }
func (r *Runner) Run() { func (r *Runner) Run(ctx context.Context) {
// todo pool 结束与并发控制 Loop:
for _, u := range r.URLList { for {
r.poolwg.Add(1) select {
r.Pools.Invoke(u) case <-ctx.Done():
logs.Log.Error("cancel with deadline")
break Loop
case u, ok := <-r.URLList:
if !ok {
break Loop
}
r.poolwg.Add(1)
r.Pools.Invoke(u)
}
} }
r.poolwg.Wait() r.poolwg.Wait()
for { for {
if len(r.OutputCh) == 0 { if len(r.OutputCh) == 0 {