Compare commits

..

6 Commits

Author SHA1 Message Date
77ddcf722a
Merge pull request #58 from aykhans/hotfix/maxuint
🚑 Replace 'math.MaxUint32' with static variable
2024-12-18 23:47:53 +04:00
048cb68898 🚑 Replace 'math.MaxUint32' with static variable 2024-12-18 23:47:24 +04:00
fd5e87ee68
Merge pull request #57 from aykhans/refactor/cli-readers
🔨 Cobra package replaced with standard flag package
2024-12-18 23:40:03 +04:00
61b9ce943b 📚 Update docs 2024-12-18 23:38:52 +04:00
6fceb0094b 🔖 Bump version to 0.5.4 2024-12-18 23:31:13 +04:00
186683fce5 🔨Cobra package replaced with standard flag package 2024-12-18 20:58:14 +04:00
9 changed files with 131 additions and 80 deletions

View File

@ -59,13 +59,13 @@ You can find an example config structure in the [config.json](https://github.com
"method": "GET", "method": "GET",
"url": "https://example.com", "url": "https://example.com",
"no_proxy_check": false, "no_proxy_check": false,
"timeout": 2000, "timeout": 10000,
"dodos_count": 10, "dodos_count": 1,
"request_count": 1000, "request_count": 1,
"params": {}, "params": {},
"headers": {}, "headers": {},
"cookies": {}, "cookies": {},
"body": [""], "body": [],
"proxies": [ "proxies": [
{ {
"url": "http://example.com:8080", "url": "http://example.com:8080",

View File

@ -3,12 +3,12 @@
"url": "https://example.com", "url": "https://example.com",
"no_proxy_check": false, "no_proxy_check": false,
"timeout": 10000, "timeout": 10000,
"dodos_count": 50, "dodos_count": 1,
"request_count": 1000, "request_count": 1,
"params": {}, "params": {},
"headers": {}, "headers": {},
"cookies": {}, "cookies": {},
"body": [""], "body": [],
"proxies": [ "proxies": [
{ {
"url": "http://example.com:8080", "url": "http://example.com:8080",

View File

@ -12,13 +12,13 @@ import (
) )
const ( const (
VERSION string = "0.5.3" VERSION string = "0.5.4"
DefaultUserAgent string = "Dodo/" + VERSION DefaultUserAgent string = "Dodo/" + VERSION
ProxyCheckURL string = "https://www.google.com" ProxyCheckURL string = "https://www.google.com"
DefaultMethod string = "GET" DefaultMethod string = "GET"
DefaultTimeout uint32 = 10000 // Milliseconds (10 seconds) DefaultTimeout uint32 = 10000 // Milliseconds (10 seconds)
DefaultDodosCount uint = 1 DefaultDodosCount uint = 1
DefaultRequestCount uint = 1000 DefaultRequestCount uint = 1
MaxDodosCountForProxies uint = 20 // Max dodos count for proxy check MaxDodosCountForProxies uint = 20 // Max dodos count for proxy check
) )
@ -209,13 +209,13 @@ func (config *JSONConfig) MergeConfigs(newConfig *JSONConfig) {
type CLIConfig struct { type CLIConfig struct {
*Config *Config
Yes bool `json:"yes" validate:"omitempty"` Yes Option[bool] `json:"yes" validate:"omitempty"`
ConfigFile string `validation_name:"config-file" validate:"omitempty,filepath"` ConfigFile string `validation_name:"config-file" validate:"omitempty,filepath"`
} }
func NewCLIConfig( func NewCLIConfig(
config *Config, config *Config,
yes bool, yes Option[bool],
configFile string, configFile string,
) *CLIConfig { ) *CLIConfig {
return &CLIConfig{ return &CLIConfig{
@ -228,4 +228,7 @@ func (config *CLIConfig) MergeConfigs(newConfig *CLIConfig) {
if newConfig.ConfigFile != "" { if newConfig.ConfigFile != "" {
config.ConfigFile = newConfig.ConfigFile config.ConfigFile = newConfig.ConfigFile
} }
if !newConfig.Yes.IsNone() {
config.Yes = newConfig.Yes
}
} }

View File

@ -19,10 +19,6 @@ func OSErrorFormater(err error) error {
return ErrInvalidFile return ErrInvalidFile
} }
func CobraErrorFormater(err error) error {
return err
}
func shortenNamespace(namespace string) string { func shortenNamespace(namespace string) string {
return namespace[strings.Index(namespace, ".")+1:] return namespace[strings.Index(namespace, ".")+1:]
} }

3
go.mod
View File

@ -5,8 +5,6 @@ go 1.23.2
require ( require (
github.com/go-playground/validator/v10 v10.23.0 github.com/go-playground/validator/v10 v10.23.0
github.com/jedib0t/go-pretty/v6 v6.6.5 github.com/jedib0t/go-pretty/v6 v6.6.5
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
github.com/valyala/fasthttp v1.58.0 github.com/valyala/fasthttp v1.58.0
golang.org/x/net v0.32.0 golang.org/x/net v0.32.0
) )
@ -16,7 +14,6 @@ require (
github.com/gabriel-vasile/mimetype v1.4.4 // indirect github.com/gabriel-vasile/mimetype v1.4.4 // indirect
github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/klauspost/compress v1.17.11 // indirect github.com/klauspost/compress v1.17.11 // indirect
github.com/leodido/go-urn v1.4.0 // indirect github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect github.com/mattn/go-runewidth v0.0.15 // indirect

9
go.sum
View File

@ -1,6 +1,5 @@
github.com/andybalholm/brotli v1.1.1 h1:PR2pgnyFznKEugtsUo0xLdDop5SKXd5Qf5ysW+7XdTA= github.com/andybalholm/brotli v1.1.1 h1:PR2pgnyFznKEugtsUo0xLdDop5SKXd5Qf5ysW+7XdTA=
github.com/andybalholm/brotli v1.1.1/go.mod h1:05ib4cKhjx3OQYUY22hTVd34Bc8upXjOLL2rKwwZBoA= github.com/andybalholm/brotli v1.1.1/go.mod h1:05ib4cKhjx3OQYUY22hTVd34Bc8upXjOLL2rKwwZBoA=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gabriel-vasile/mimetype v1.4.4 h1:QjV6pZ7/XZ7ryI2KuyeEDE8wnh7fHP9YnQy+R0LnH8I= github.com/gabriel-vasile/mimetype v1.4.4 h1:QjV6pZ7/XZ7ryI2KuyeEDE8wnh7fHP9YnQy+R0LnH8I=
@ -13,8 +12,6 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.23.0 h1:/PwmTwZhS0dPkav3cdK9kV1FsAmrL8sThn8IHr/sO+o= github.com/go-playground/validator/v10 v10.23.0 h1:/PwmTwZhS0dPkav3cdK9kV1FsAmrL8sThn8IHr/sO+o=
github.com/go-playground/validator/v10 v10.23.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= github.com/go-playground/validator/v10 v10.23.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jedib0t/go-pretty/v6 v6.6.5 h1:9PgMJOVBedpgYLI56jQRJYqngxYAAzfEUua+3NgSqAo= github.com/jedib0t/go-pretty/v6 v6.6.5 h1:9PgMJOVBedpgYLI56jQRJYqngxYAAzfEUua+3NgSqAo=
github.com/jedib0t/go-pretty/v6 v6.6.5/go.mod h1:Uq/HrbhuFty5WSVNfjpQQe47x16RwVGXIveNGEyGtHs= github.com/jedib0t/go-pretty/v6 v6.6.5/go.mod h1:Uq/HrbhuFty5WSVNfjpQQe47x16RwVGXIveNGEyGtHs=
github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc= github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc=
@ -28,11 +25,6 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
@ -51,6 +43,5 @@ golang.org/x/term v0.27.0 h1:WP60Sv1nlK1T6SupCHbXzSaN0b9wUmsPoRS9b61A23Q=
golang.org/x/term v0.27.0/go.mod h1:iMsnZpn0cago0GOrHO2+Y7u7JPn5AylBrcoWkElMTSM= golang.org/x/term v0.27.0/go.mod h1:iMsnZpn0cago0GOrHO2+Y7u7JPn5AylBrcoWkElMTSM=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

12
main.go
View File

@ -27,9 +27,13 @@ func main() {
) )
cliConf, err := readers.CLIConfigReader() cliConf, err := readers.CLIConfigReader()
if err != nil || cliConf == nil { if err != nil {
utils.PrintAndExit(err.Error())
}
if cliConf == nil {
os.Exit(0) os.Exit(0)
} }
if err := validator.StructPartial(cliConf, "ConfigFile"); err != nil { if err := validator.StructPartial(cliConf, "ConfigFile"); err != nil {
utils.PrintErrAndExit( utils.PrintErrAndExit(
customerrors.ValidationErrorsFormater( customerrors.ValidationErrorsFormater(
@ -82,11 +86,11 @@ func main() {
Cookies: jsonConf.Cookies, Cookies: jsonConf.Cookies,
Proxies: jsonConf.Proxies, Proxies: jsonConf.Proxies,
Body: jsonConf.Body, Body: jsonConf.Body,
Yes: cliConf.Yes, Yes: cliConf.Yes.ValueOr(false),
NoProxyCheck: conf.NoProxyCheck.ValueOrPanic(), NoProxyCheck: conf.NoProxyCheck.ValueOr(false),
} }
requestConf.Print() requestConf.Print()
if !cliConf.Yes { if !cliConf.Yes.ValueOr(false) {
response := readers.CLIYesOrNoReader("Do you want to continue?", true) response := readers.CLIYesOrNoReader("Do you want to continue?", true)
if !response { if !response {
utils.PrintAndExit("Exiting...") utils.PrintAndExit("Exiting...")

View File

@ -1,70 +1,128 @@
package readers package readers
import ( import (
"flag"
"fmt" "fmt"
"strings"
"github.com/aykhans/dodo/config" "github.com/aykhans/dodo/config"
"github.com/aykhans/dodo/custom_errors"
. "github.com/aykhans/dodo/types" . "github.com/aykhans/dodo/types"
"github.com/aykhans/dodo/utils" "github.com/aykhans/dodo/utils"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
) )
const usageText = `Usage:
dodo [flags]
Examples:
Simple usage only with URL:
dodo -u https://example.com
Simple usage with config file:
dodo -c /path/to/config/file/config.json
Usage with all flags:
dodo -c /path/to/config/file/config.json -u https://example.com -m POST -d 10 -r 1000 -t 2000 --no-proxy-check -y
Flags:
-h, --help help for dodo
-v, --version version for dodo
-c, --config-file string Path to the config file
-d, --dodos-count uint Number of dodos(threads) (default %d)
-m, --method string HTTP Method (default %s)
-r, --request-count uint Number of total requests (default %d)
-t, --timeout uint32 Timeout for each request in milliseconds (default %d)
-u, --url string URL for stress testing
--no-proxy-check bool Do not check for proxies (default false)
-y, --yes bool Answer yes to all questions (default false)`
func CLIConfigReader() (*config.CLIConfig, error) { func CLIConfigReader() (*config.CLIConfig, error) {
var ( flag.Usage = func() {
returnNil = false fmt.Printf(
cliConfig = config.NewCLIConfig(config.NewConfig("", 0, 0, 0, nil), false, "") usageText+"\n",
dodosCount uint config.DefaultDodosCount,
requestCount uint config.DefaultMethod,
timeout uint32 config.DefaultRequestCount,
noProxyCheck bool config.DefaultTimeout,
rootCmd = &cobra.Command{ )
Use: "dodo [flags]",
Example: ` Simple usage only with URL:
dodo -u https://example.com
Simple usage with config file:
dodo -c /path/to/config/file/config.json
Usage with all flags:
dodo -c /path/to/config/file/config.json -u https://example.com -m POST -d 10 -r 1000 -t 2000 --no-proxy-check -y`,
Run: func(cmd *cobra.Command, args []string) {},
SilenceErrors: true,
SilenceUsage: true,
Version: config.VERSION,
}
)
rootCmd.Flags().StringVarP(&cliConfig.ConfigFile, "config-file", "c", "", "Path to the config file")
rootCmd.Flags().BoolVarP(&cliConfig.Yes, "yes", "y", false, "Answer yes to all questions")
rootCmd.Flags().StringVarP(&cliConfig.Method, "method", "m", "", fmt.Sprintf("HTTP Method (default %s)", config.DefaultMethod))
rootCmd.Flags().StringVarP(&cliConfig.URL, "url", "u", "", "URL for stress testing")
rootCmd.Flags().UintVarP(&dodosCount, "dodos-count", "d", config.DefaultDodosCount, "Number of dodos(threads)")
rootCmd.Flags().UintVarP(&requestCount, "request-count", "r", config.DefaultRequestCount, "Number of total requests")
rootCmd.Flags().Uint32VarP(&timeout, "timeout", "t", config.DefaultTimeout, "Timeout for each request in milliseconds")
rootCmd.Flags().BoolVar(&noProxyCheck, "no-proxy-check", false, "Do not check for proxies")
if err := rootCmd.Execute(); err != nil {
utils.PrintErr(err)
rootCmd.Println(rootCmd.UsageString())
return nil, customerrors.CobraErrorFormater(err)
} }
rootCmd.Flags().Visit(func(f *pflag.Flag) {
var (
cliConfig = config.NewCLIConfig(config.NewConfig("", 0, 0, 0, nil), NewOption(false), "")
configFile = ""
yes = false
method = ""
url = ""
dodosCount uint = 0
requestsCount uint = 0
timeout uint = 0
noProxyCheck bool = false
)
{
flag.Bool("version", false, "Prints the version of the program")
flag.Bool("v", false, "Prints the version of the program")
flag.StringVar(&configFile, "config-file", "", "Path to the configuration file")
flag.StringVar(&configFile, "c", "", "Path to the configuration file")
flag.BoolVar(&yes, "yes", false, "Answer yes to all questions")
flag.BoolVar(&yes, "y", false, "Answer yes to all questions")
flag.StringVar(&method, "method", "", "HTTP Method")
flag.StringVar(&method, "m", "", "HTTP Method")
flag.StringVar(&url, "url", "", "URL to send the request")
flag.StringVar(&url, "u", "", "URL to send the request")
flag.UintVar(&dodosCount, "dodos-count", 0, "Number of dodos(threads)")
flag.UintVar(&dodosCount, "d", 0, "Number of dodos(threads)")
flag.UintVar(&requestsCount, "requests-count", 0, "Number of total requests")
flag.UintVar(&requestsCount, "r", 0, "Number of total requests")
flag.UintVar(&timeout, "timeout", 0, "Timeout for each request in milliseconds")
flag.UintVar(&timeout, "t", 0, "Timeout for each request in milliseconds")
flag.BoolVar(&noProxyCheck, "no-proxy-check", false, "Do not check for active proxies")
}
flag.Parse()
args := flag.Args()
if len(args) > 0 {
return nil, fmt.Errorf("unexpected arguments: %v", strings.Join(args, ", "))
}
returnNil := false
flag.Visit(func(f *flag.Flag) {
switch f.Name { switch f.Name {
case "help": case "version", "v":
fmt.Printf("dodo version %s\n", config.VERSION)
returnNil = true returnNil = true
case "version": case "config-file", "c":
returnNil = true cliConfig.ConfigFile = configFile
case "dodos-count": case "yes", "y":
cliConfig.Yes.SetValue(yes)
case "method", "m":
cliConfig.Method = method
case "url", "u":
cliConfig.URL = url
case "dodos-count", "d":
cliConfig.DodosCount = dodosCount cliConfig.DodosCount = dodosCount
case "request-count": case "requests-count", "r":
cliConfig.RequestCount = requestCount cliConfig.RequestCount = requestsCount
case "timeout": case "timeout", "t":
cliConfig.Timeout = timeout var maxUint32 uint = 4294967295
if timeout > maxUint32 {
utils.PrintfC(utils.Colors.Yellow, "timeout value is too large, setting to %d\n", maxUint32)
timeout = maxUint32
}
cliConfig.Timeout = uint32(timeout)
case "no-proxy-check": case "no-proxy-check":
cliConfig.NoProxyCheck = NewOption(noProxyCheck) cliConfig.NoProxyCheck.SetValue(noProxyCheck)
} }
}) })
if returnNil { if returnNil {
return nil, nil return nil, nil
} }

View File

@ -14,6 +14,8 @@ type Option[T NonNilT] interface {
ValueOrErr() (T, error) ValueOrErr() (T, error)
ValueOr(def T) T ValueOr(def T) T
ValueOrPanic() T ValueOrPanic() T
SetValue(value T)
SetNone()
UnmarshalJSON(data []byte) error UnmarshalJSON(data []byte) error
} }