spray/cmd/cmd.go

117 lines
2.3 KiB
Go
Raw Normal View History

package cmd
2022-09-08 15:57:17 +08:00
import (
2022-11-10 16:52:00 +08:00
"context"
2022-09-15 19:27:07 +08:00
"fmt"
"github.com/chainreactors/gogo/v2/pkg/fingers"
"github.com/chainreactors/gogo/v2/pkg/utils"
2022-09-08 15:57:17 +08:00
"github.com/chainreactors/logs"
"github.com/chainreactors/spray/internal"
"github.com/chainreactors/spray/pkg"
"github.com/chainreactors/spray/pkg/ihttp"
2022-09-15 19:27:07 +08:00
"github.com/jessevdk/go-flags"
"os"
"os/signal"
"regexp"
"syscall"
2022-11-10 16:52:00 +08:00
"time"
2022-09-08 15:57:17 +08:00
)
func Spray() {
var option internal.Option
parser := flags.NewParser(&option, flags.Default)
parser.Usage = `
WIKI: https://chainreactors.github.io/wiki/spray
QUICKSTART:
simple example:
spray -u http://example.com -d wordlist1.txt -d wordlist2.txt
mask-base wordlist:
spray -u http://example.com -w "/aaa/bbb{?l#4}/ccc"
rule-base wordlist:
spray -u http://example.com -r rule.txt -d 1.txt
list input spray:
spray -l url.txt -r rule.txt -d 1.txt
resume:
spray --resume stat.json
`
2022-09-15 19:27:07 +08:00
_, err := parser.Parse()
if err != nil {
if err.(*flags.Error).Type != flags.ErrHelp {
fmt.Println(err.Error())
}
return
}
if option.Format != "" {
internal.Format(option.Format)
os.Exit(0)
}
err = pkg.LoadTemplates()
if err != nil {
utils.Fatal(err.Error())
}
if option.Extracts != nil {
for _, e := range option.Extracts {
if reg, ok := fingers.PresetExtracts[e]; ok {
pkg.Extractors[e] = reg
} else {
pkg.Extractors[e] = regexp.MustCompile(e)
}
}
}
// 一些全局变量初始化
if option.Debug {
logs.Log.Level = logs.Debug
}
pkg.Distance = uint8(option.SimhashDistance)
ihttp.DefaultMaxBodySize = option.MaxBodyLength * 1024
if option.ReadAll {
ihttp.DefaultMaxBodySize = 0
}
2022-12-02 19:59:15 +08:00
var runner *internal.Runner
if option.ResumeFrom != "" {
runner, err = option.PrepareRunner()
} else {
runner, err = option.PrepareRunner()
}
2022-09-08 15:57:17 +08:00
if err != nil {
logs.Log.Errorf(err.Error())
return
}
ctx, canceler := context.WithTimeout(context.Background(), time.Duration(runner.Deadline)*time.Second)
2022-11-10 16:52:00 +08:00
err = runner.Prepare(ctx)
2022-09-08 15:57:17 +08:00
if err != nil {
logs.Log.Errorf(err.Error())
return
}
2022-11-10 16:52:00 +08:00
go func() {
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
fmt.Println("exit signal, save stat and exit")
canceler()
}()
}()
if runner.CheckOnly {
runner.RunWithCheck(ctx)
} else {
runner.Run(ctx)
}
2022-09-08 15:57:17 +08:00
}