spray/cmd/cmd.go

157 lines
3.2 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/files"
2022-09-08 15:57:17 +08:00
"github.com/chainreactors/logs"
"github.com/chainreactors/spray/internal"
2024-02-08 15:26:01 +08:00
"github.com/chainreactors/spray/internal/ihttp"
2024-02-10 18:23:50 +08:00
"github.com/chainreactors/spray/internal/pool"
"github.com/chainreactors/spray/pkg"
2023-12-28 14:34:19 +08:00
"github.com/chainreactors/utils/iutils"
2022-09-15 19:27:07 +08:00
"github.com/jessevdk/go-flags"
"os"
"os/signal"
"syscall"
2022-11-10 16:52:00 +08:00
"time"
2022-09-08 15:57:17 +08:00
)
2024-03-04 20:04:34 +08:00
var ver = "v0.9.5"
var DefaultConfig = "config.yaml"
2023-05-04 12:23:24 +08:00
func Spray() {
var option internal.Option
if files.IsExist(DefaultConfig) {
err := internal.LoadConfig(DefaultConfig, &option)
if err != nil {
logs.Log.Error(err.Error())
return
}
}
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
}
2024-02-12 16:49:44 +08:00
// logs
logs.AddLevel(pkg.LogVerbose, "verbose", "[=] %s {{suffix}}")
if option.Debug {
logs.Log.SetLevel(logs.Debug)
} else if len(option.Verbose) > 0 {
logs.Log.SetLevel(pkg.LogVerbose)
}
logs.Log.SetColorMap(map[logs.Level]func(string) string{
logs.Info: logs.PurpleBold,
logs.Important: logs.GreenBold,
pkg.LogVerbose: logs.Green,
})
if option.InitConfig {
configStr := internal.InitDefaultConfig(&option, 0)
err := os.WriteFile("config.yaml", []byte(configStr), 0o744)
2024-02-12 16:49:44 +08:00
if err != nil {
logs.Log.Warn("cannot create config: config.yaml, " + err.Error())
2024-02-12 16:49:44 +08:00
return
}
return
}
if option.Config != "" {
err := internal.LoadConfig(option.Config, &option)
if err != nil {
logs.Log.Error(err.Error())
return
}
2024-02-12 16:49:44 +08:00
}
2023-05-04 12:23:24 +08:00
if option.Version {
fmt.Println(ver)
return
}
if option.Format != "" {
internal.Format(option.Format, !option.NoColor)
2024-02-12 16:49:44 +08:00
return
}
2024-03-04 20:03:06 +08:00
err = pkg.Load()
2024-02-20 18:25:43 +08:00
if err != nil {
iutils.Fatal(err.Error())
}
// 初始化全局变量
2024-02-10 18:23:50 +08:00
pkg.Distance = uint8(option.SimhashDistance)
if option.MaxBodyLength == -1 {
ihttp.DefaultMaxBodySize = -1
} else {
ihttp.DefaultMaxBodySize = option.MaxBodyLength * 1024
}
2024-02-10 18:23:50 +08:00
pool.MaxCrawl = option.CrawlDepth
2022-12-02 19:59:15 +08:00
var runner *internal.Runner
if option.ResumeFrom != "" {
runner, err = option.PrepareRunner()
} else {
runner, err = option.PrepareRunner()
}
if err != nil {
logs.Log.Errorf(err.Error())
return
}
2023-12-28 14:34:19 +08:00
if option.ReadAll || runner.Crawl {
ihttp.DefaultMaxBodySize = -1
2023-12-28 14:34:19 +08:00
}
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
logs.Log.Important("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
}