From 9cfae3bdc1f6fa250fe527c4e076d75e8cd0deeb Mon Sep 17 00:00:00 2001 From: Aykhan Shahsuvarov Date: Fri, 22 Nov 2024 23:56:34 +0400 Subject: [PATCH 01/13] =?UTF-8?q?=E2=9C=A8=20Added=20'Option'=20type?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/types.go | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 utils/types.go diff --git a/utils/types.go b/utils/types.go new file mode 100644 index 0000000..fecb5a6 --- /dev/null +++ b/utils/types.go @@ -0,0 +1,53 @@ +package utils + +import ( + "encoding/json" + "errors" +) + +type Option[T any] struct { + value T + none bool +} + +func (o *Option[T]) IsNone() bool { + return o.none +} + +func (o *Option[T]) ValueOrErr() (*T, error) { + if o.IsNone() { + return nil, errors.New("Option is None") + } + return &o.value, nil +} + +func (o *Option[T]) ValueOr(def *T) *T { + if o.IsNone() { + return def + } + return &o.value +} + +func (o *Option[T]) ValueOrPanic() *T { + if o.IsNone() { + panic("Option is None") + } + return &o.value +} + +func (o *Option[T]) UnmarshalJSON(data []byte) error { + if string(data) == "null" { + o.none = true + return nil + } + o.none = false + return json.Unmarshal(data, &o.value) +} + +func NewOption[T any](value T) Option[T] { + return Option[T]{value: value} +} + +func NewNoneOption[T any]() Option[T] { + return Option[T]{none: true} +} From 011c7a7774fff1feda867f57d1bc4a238d8e917d Mon Sep 17 00:00:00 2001 From: Aykhan Shahsuvarov Date: Fri, 22 Nov 2024 23:58:17 +0400 Subject: [PATCH 02/13] =?UTF-8?q?=E2=9C=A8=20Added=20'no-proxy-check'=20pa?= =?UTF-8?q?rameter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.go | 20 +++++++++++++++----- main.go | 1 + readers/cli.go | 10 +++++++++- readers/json.go | 9 +++++++-- requests/client.go | 33 ++++++++++++++++++++++++++++++++- requests/run.go | 4 ++++ 6 files changed, 68 insertions(+), 9 deletions(-) diff --git a/config/config.go b/config/config.go index 37c65ce..48bf7ac 100644 --- a/config/config.go +++ b/config/config.go @@ -37,6 +37,7 @@ type RequestConfig struct { Proxies []Proxy Body []string Yes bool + NoProxyCheck bool } func (config *RequestConfig) Print() { @@ -66,6 +67,8 @@ func (config *RequestConfig) Print() { t.AppendSeparator() t.AppendRow(table.Row{"Proxies Count", len(config.Proxies)}) t.AppendSeparator() + t.AppendRow(table.Row{"Proxy Check", !config.NoProxyCheck}) + t.AppendSeparator() t.AppendRow(table.Row{"Body", utils.MarshalJSON(config.Body, 3)}) t.Render() @@ -87,11 +90,12 @@ func (config *RequestConfig) GetMaxConns(minConns uint) uint { } type Config struct { - Method string `json:"method" validate:"http_method"` // custom validations: http_method - URL string `json:"url" validate:"http_url,required"` - Timeout uint32 `json:"timeout" validate:"gte=1,lte=100000"` - DodosCount uint `json:"dodos_count" validate:"gte=1"` - RequestCount uint `json:"request_count" validation_name:"request-count" validate:"gte=1"` + Method string `json:"method" validate:"http_method"` // custom validations: http_method + URL string `json:"url" validate:"http_url,required"` + Timeout uint32 `json:"timeout" validate:"gte=1,lte=100000"` + DodosCount uint `json:"dodos_count" validate:"gte=1"` + RequestCount uint `json:"request_count" validation_name:"request-count" validate:"gte=1"` + NoProxyCheck utils.Option[bool] `json:"no_proxy_check"` } func (config *Config) MergeConfigs(newConfig *Config) { @@ -110,6 +114,9 @@ func (config *Config) MergeConfigs(newConfig *Config) { if newConfig.RequestCount != 0 { config.RequestCount = newConfig.RequestCount } + if !newConfig.NoProxyCheck.IsNone() { + config.NoProxyCheck = newConfig.NoProxyCheck + } } func (config *Config) SetDefaults() { @@ -125,6 +132,9 @@ func (config *Config) SetDefaults() { if config.RequestCount == 0 { config.RequestCount = DefaultRequestCount } + if config.NoProxyCheck.IsNone() { + config.NoProxyCheck = utils.NewOption(false) + } } type Proxy struct { diff --git a/main.go b/main.go index 1b5baec..b911f72 100644 --- a/main.go +++ b/main.go @@ -81,6 +81,7 @@ func main() { Proxies: jsonConf.Proxies, Body: jsonConf.Body, Yes: cliConf.Yes, + NoProxyCheck: *conf.NoProxyCheck.ValueOrPanic(), } requestConf.Print() if !cliConf.Yes { diff --git a/readers/cli.go b/readers/cli.go index 3880a1d..8aae3ea 100644 --- a/readers/cli.go +++ b/readers/cli.go @@ -13,10 +13,15 @@ import ( func CLIConfigReader() (*config.CLIConfig, error) { var ( returnNil = false - cliConfig = &config.CLIConfig{} + cliConfig = &config.CLIConfig{ + Config: config.Config{ + NoProxyCheck: utils.NewNoneOption[bool](), + }, + } dodosCount uint requestCount uint timeout uint32 + noProxyCheck bool rootCmd = &cobra.Command{ Use: "dodo [flags]", Example: ` Simple usage only with URL: @@ -51,6 +56,7 @@ func CLIConfigReader() (*config.CLIConfig, error) { 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().BoolVarP(&noProxyCheck, "no-proxy-check", "n", false, "Do not check for proxy") if err := rootCmd.Execute(); err != nil { utils.PrintErr(err) rootCmd.Println(rootCmd.UsageString()) @@ -68,6 +74,8 @@ func CLIConfigReader() (*config.CLIConfig, error) { cliConfig.RequestCount = requestCount case "timeout": cliConfig.Timeout = timeout + case "no-proxy-check": + cliConfig.NoProxyCheck = utils.NewOption(noProxyCheck) } }) if returnNil { diff --git a/readers/json.go b/readers/json.go index cf8725e..f88aca5 100644 --- a/readers/json.go +++ b/readers/json.go @@ -5,7 +5,8 @@ import ( "os" "github.com/aykhans/dodo/config" - "github.com/aykhans/dodo/custom_errors" + customerrors "github.com/aykhans/dodo/custom_errors" + "github.com/aykhans/dodo/utils" ) func JSONConfigReader(filePath string) (*config.JSONConfig, error) { @@ -13,7 +14,11 @@ func JSONConfigReader(filePath string) (*config.JSONConfig, error) { if err != nil { return nil, customerrors.OSErrorFormater(err) } - jsonConf := &config.JSONConfig{} + jsonConf := &config.JSONConfig{ + Config: config.Config{ + NoProxyCheck: utils.NewNoneOption[bool](), + }, + } err = json.Unmarshal(data, &jsonConf) if err != nil { diff --git a/requests/client.go b/requests/client.go index 52fef8d..da6010c 100644 --- a/requests/client.go +++ b/requests/client.go @@ -26,11 +26,42 @@ func getClients( dodosCount uint, maxConns uint, yes bool, + noProxyCheck bool, URL *url.URL, ) []*fasthttp.HostClient { isTLS := URL.Scheme == "https" - if len(proxies) > 0 { + if proxiesLen := len(proxies); proxiesLen > 0 { + // If noProxyCheck is true, we will return the clients without checking the proxies. + if noProxyCheck { + clients := make([]*fasthttp.HostClient, 0, proxiesLen) + addr := URL.Host + if isTLS && URL.Port() == "" { + addr += ":443" + } + + for _, proxy := range proxies { + dialFunc, err := getDialFunc(&proxy, timeout) + if err != nil { + continue + } + + clients = append(clients, &fasthttp.HostClient{ + MaxConns: int(maxConns), + IsTLS: isTLS, + Addr: addr, + Dial: dialFunc, + MaxIdleConnDuration: timeout, + MaxConnDuration: timeout, + WriteTimeout: timeout, + ReadTimeout: timeout, + }, + ) + } + return clients + } + + // Else, we will check the proxies and return the active ones. activeProxyClients := getActiveProxyClients( ctx, proxies, timeout, dodosCount, maxConns, URL, ) diff --git a/requests/run.go b/requests/run.go index c1ae2bc..3b2407a 100644 --- a/requests/run.go +++ b/requests/run.go @@ -2,6 +2,7 @@ package requests import ( "context" + "fmt" "sync" "time" @@ -24,6 +25,8 @@ import ( // - Responses: A collection of responses from the executed requests. // - error: An error if the operation fails, such as no internet connection or an interrupt. func Run(ctx context.Context, requestConfig *config.RequestConfig) (Responses, error) { + fmt.Println("No Proxy Check:", requestConfig.NoProxyCheck) + checkConnectionCtx, checkConnectionCtxCancel := context.WithTimeout(ctx, 8*time.Second) if !checkConnection(checkConnectionCtx) { checkConnectionCtxCancel() @@ -38,6 +41,7 @@ func Run(ctx context.Context, requestConfig *config.RequestConfig) (Responses, e requestConfig.GetValidDodosCountForProxies(), requestConfig.GetMaxConns(fasthttp.DefaultMaxConnsPerHost), requestConfig.Yes, + requestConfig.NoProxyCheck, requestConfig.URL, ) if clients == nil { From 9679fb1c7aa281d0a8d7bc4fb8158c5a8fa8e59d Mon Sep 17 00:00:00 2001 From: Aykhan Shahsuvarov Date: Sat, 23 Nov 2024 00:09:48 +0400 Subject: [PATCH 03/13] =?UTF-8?q?=F0=9F=94=A8=20Remove=20shorthand=20lette?= =?UTF-8?q?r=20from=20'no-proxy-check'=20parameter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- readers/cli.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readers/cli.go b/readers/cli.go index 8aae3ea..1a6bf48 100644 --- a/readers/cli.go +++ b/readers/cli.go @@ -56,7 +56,7 @@ func CLIConfigReader() (*config.CLIConfig, error) { 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().BoolVarP(&noProxyCheck, "no-proxy-check", "n", false, "Do not check for proxy") + rootCmd.Flags().BoolVar(&noProxyCheck, "no-proxy-check", false, "Do not check for proxy") if err := rootCmd.Execute(); err != nil { utils.PrintErr(err) rootCmd.Println(rootCmd.UsageString()) From 5080e908720cf0c7f407ddeb8a2814597e04eca8 Mon Sep 17 00:00:00 2001 From: Aykhan Shahsuvarov Date: Sat, 23 Nov 2024 00:10:19 +0400 Subject: [PATCH 04/13] =?UTF-8?q?=E2=9C=A8=20'no-proxy-check'=20parameter?= =?UTF-8?q?=20to=20docs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 29 +++++++++++++++-------------- config.json | 1 + 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 72e3d1e..e3a4918 100644 --- a/README.md +++ b/README.md @@ -101,17 +101,18 @@ docker run --rm -v ./path/config.json:/dodo/config.json -i aykhans/dodo -u https ## CLI and JSON Config Parameters If the Headers, Params, Cookies and Body fields have multiple values, each request will choose a random value from the list. -| Parameter | JSON config file | CLI Flag | CLI Short Flag | Type | Description | Default | -| ----------- | ----------- | ----------- | ----------- | ----------- | ----------- | ----------- | -| Config file | - | --config-file | -c | String | Path to the JSON config file | - | -| Yes | - | --yes | -y | Boolean | Answer yes to all questions | false | -| URL | url | --url | -u | String | URL to send the request to | - | -| Method | method | --method | -m | String | HTTP method | GET | -| Request count | request_count | --request-count | -r | Integer | Total number of requests to send | 1000 | -| Dodos count (Threads) | dodos_count | --dodos-count | -d | Integer | Number of dodos (threads) to send requests in parallel | 1 | -| Timeout | timeout | --timeout | -t | Integer | Timeout for canceling each request (milliseconds) | 10000 | -| Params | params | - | - | Key-Value {String: [String]} | Request parameters | - | -| Headers | headers | - | - | Key-Value {String: [String]} | Request headers | - | -| Cookies | cookies | - | - | Key-Value {String: [String]} | Request cookies | - | -| Body | body | - | - | [String] | Request body | - | -| Proxy | proxies | - | - | List[Key-Value {string: string}] | List of proxies (will check active proxies before sending requests) | - | +| Parameter | JSON config file | CLI Flag | CLI Short Flag | Type | Description | Default | +| --------------------- | ---------------- | --------------- | -------------- | -------------------------------- | ------------------------------------------------------------------- | ----------- | +| Config file | - | --config-file | -c | String | Path to the JSON config file | - | +| Yes | - | --yes | -y | Boolean | Answer yes to all questions | false | +| URL | url | --url | -u | String | URL to send the request to | - | +| Method | method | --method | -m | String | HTTP method | GET | +| Request count | request_count | --request-count | -r | Integer | Total number of requests to send | 1000 | +| Dodos count (Threads) | dodos_count | --dodos-count | -d | Integer | Number of dodos (threads) to send requests in parallel | 1 | +| Timeout | timeout | --timeout | -t | Integer | Timeout for canceling each request (milliseconds) | 10000 | +| No Proxy Check | no_proxy_check | --no-proxy-check| - | Boolean | Disable proxy check | false | +| Params | params | - | - | Key-Value {String: [String]} | Request parameters | - | +| Headers | headers | - | - | Key-Value {String: [String]} | Request headers | - | +| Cookies | cookies | - | - | Key-Value {String: [String]} | Request cookies | - | +| Body | body | - | - | [String] | Request body | - | +| Proxy | proxies | - | - | List[Key-Value {string: string}] | List of proxies (will check active proxies before sending requests) | - | diff --git a/config.json b/config.json index cfa8782..217392f 100644 --- a/config.json +++ b/config.json @@ -1,6 +1,7 @@ { "method": "GET", "url": "https://example.com", + "no_proxy_check": false, "timeout": 10000, "dodos_count": 50, "request_count": 1000, From adc5a34891c0f86f226f6200472f0d05e587260d Mon Sep 17 00:00:00 2001 From: Aykhan Shahsuvarov Date: Sat, 23 Nov 2024 00:11:54 +0400 Subject: [PATCH 05/13] =?UTF-8?q?=F0=9F=94=A8=20Remove=20short=20name=20fr?= =?UTF-8?q?om=20CLI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- readers/cli.go | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/readers/cli.go b/readers/cli.go index 1a6bf48..b5e70d7 100644 --- a/readers/cli.go +++ b/readers/cli.go @@ -32,16 +32,6 @@ func CLIConfigReader() (*config.CLIConfig, error) { Usage with all flags: dodo -c /path/to/config/file/config.json -u https://example.com -m POST -d 10 -r 1000 -t 2000`, - Short: ` -██████████ ███████ ██████████ ███████ -░░███░░░░███ ███░░░░░███ ░░███░░░░███ ███░░░░░███ - ░███ ░░███ ███ ░░███ ░███ ░░███ ███ ░░███ - ░███ ░███░███ ░███ ░███ ░███░███ ░███ - ░███ ░███░███ ░███ ░███ ░███░███ ░███ - ░███ ███ ░░███ ███ ░███ ███ ░░███ ███ - ██████████ ░░░███████░ ██████████ ░░░███████░ -░░░░░░░░░░ ░░░░░░░ ░░░░░░░░░░ ░░░░░░░ -`, Run: func(cmd *cobra.Command, args []string) {}, SilenceErrors: true, SilenceUsage: true, @@ -56,7 +46,7 @@ func CLIConfigReader() (*config.CLIConfig, error) { 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 proxy") + 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()) From ede74c17b2348e5e72ce14a06e30c02b3609de64 Mon Sep 17 00:00:00 2001 From: Aykhan Shahsuvarov Date: Sat, 23 Nov 2024 16:13:17 +0400 Subject: [PATCH 06/13] =?UTF-8?q?=F0=9F=93=9A=20Add=20docs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/types.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/utils/types.go b/utils/types.go index fecb5a6..4f1d18e 100644 --- a/utils/types.go +++ b/utils/types.go @@ -5,15 +5,19 @@ import ( "errors" ) +// Don't call this struct directly, use NewOption[T] or NewNoneOption[T] instead. type Option[T any] struct { + // value holds the actual value of the Option if it is not None. value T - none bool + // none indicates whether the Option is None (i.e., has no value). + none bool } func (o *Option[T]) IsNone() bool { return o.none } +// The returned value can be nil, if the Option is None, it will return nil and an error. func (o *Option[T]) ValueOrErr() (*T, error) { if o.IsNone() { return nil, errors.New("Option is None") @@ -21,6 +25,7 @@ func (o *Option[T]) ValueOrErr() (*T, error) { return &o.value, nil } +// The returned value can't be nil, if the Option is None, it will return the default value. func (o *Option[T]) ValueOr(def *T) *T { if o.IsNone() { return def @@ -28,6 +33,7 @@ func (o *Option[T]) ValueOr(def *T) *T { return &o.value } +// The returned value can't be nil, if the Option is None, it will panic. func (o *Option[T]) ValueOrPanic() *T { if o.IsNone() { panic("Option is None") From 9c5d7bf135f6b8e7d640ba0a28bdb520735230fd Mon Sep 17 00:00:00 2001 From: Aykhan Shahsuvarov Date: Sat, 23 Nov 2024 16:13:38 +0400 Subject: [PATCH 07/13] =?UTF-8?q?=F0=9F=93=9A=20Update=20docs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e3a4918..4df71dc 100644 --- a/README.md +++ b/README.md @@ -58,8 +58,9 @@ You can find an example config structure in the [config.json](https://github.com { "method": "GET", "url": "https://example.com", - "timeout": 10000, - "dodos_count": 50, + "no_proxy_check": false, + "timeout": 2000, + "dodos_count": 10, "request_count": 1000, "params": {}, "headers": {}, @@ -77,7 +78,7 @@ You can find an example config structure in the [config.json](https://github.com ] } ``` -Send 1000 GET requests to https://example.com with 5 parallel dodos (threads) and a timeout of 10000 milliseconds: +Send 1000 GET requests to https://example.com with 10 parallel dodos (threads) and a timeout of 2000 milliseconds: ```sh dodo -c /path/config.json From 0335b5cf6e41a8687c7893b1a1a78439b7695f39 Mon Sep 17 00:00:00 2001 From: Aykhan Shahsuvarov Date: Sat, 23 Nov 2024 16:18:21 +0400 Subject: [PATCH 08/13] =?UTF-8?q?=F0=9F=93=9A=20Update=20docs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4df71dc..c291b33 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ Follow the steps below to build dodo: 3. **Build the project:** ```sh - go build -o dodo + go build -ldflags "-s -w" -o dodo ``` This will generate an executable named `dodo` in the project directory. From bf169d3eb14b7841a65bfc03ac053fa2c52d3836 Mon Sep 17 00:00:00 2001 From: Aykhan Shahsuvarov Date: Sat, 23 Nov 2024 16:27:46 +0400 Subject: [PATCH 09/13] =?UTF-8?q?=F0=9F=94=A8=20Remove=20debug=20print?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- requests/run.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/requests/run.go b/requests/run.go index 3b2407a..a90e58a 100644 --- a/requests/run.go +++ b/requests/run.go @@ -2,7 +2,6 @@ package requests import ( "context" - "fmt" "sync" "time" @@ -25,8 +24,6 @@ import ( // - Responses: A collection of responses from the executed requests. // - error: An error if the operation fails, such as no internet connection or an interrupt. func Run(ctx context.Context, requestConfig *config.RequestConfig) (Responses, error) { - fmt.Println("No Proxy Check:", requestConfig.NoProxyCheck) - checkConnectionCtx, checkConnectionCtxCancel := context.WithTimeout(ctx, 8*time.Second) if !checkConnection(checkConnectionCtx) { checkConnectionCtxCancel() From d98fb477d423f7a48b3636af1bdbc4e022c73783 Mon Sep 17 00:00:00 2001 From: Aykhan Shahsuvarov Date: Sat, 23 Nov 2024 16:28:19 +0400 Subject: [PATCH 10/13] =?UTF-8?q?=F0=9F=94=A8=20Update=20usage=20text?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- readers/cli.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readers/cli.go b/readers/cli.go index b5e70d7..82e9c62 100644 --- a/readers/cli.go +++ b/readers/cli.go @@ -31,7 +31,7 @@ func CLIConfigReader() (*config.CLIConfig, error) { 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`, + 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, From a468f663bfb536ec5feb8748611eaf1ae0119c8e Mon Sep 17 00:00:00 2001 From: Aykhan Shahsuvarov Date: Sat, 23 Nov 2024 16:29:06 +0400 Subject: [PATCH 11/13] =?UTF-8?q?=F0=9F=94=96=20Bump=20version=20to=200.5.?= =?UTF-8?q?2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index 48bf7ac..99dae75 100644 --- a/config/config.go +++ b/config/config.go @@ -11,7 +11,7 @@ import ( ) const ( - VERSION string = "0.5.1" + VERSION string = "0.5.2" DefaultUserAgent string = "Dodo/" + VERSION ProxyCheckURL string = "https://www.google.com" DefaultMethod string = "GET" From 098c1d8cc489a45d0164c1bcafa1e63a010e3bad Mon Sep 17 00:00:00 2001 From: Aykhan Shahsuvarov Date: Sat, 23 Nov 2024 17:53:11 +0400 Subject: [PATCH 12/13] =?UTF-8?q?=F0=9F=94=A8=20Make=20the=20'Option'=20ty?= =?UTF-8?q?pe=20private=20and=20add=20the=20'IOption'=20type?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.go | 12 ++++++------ readers/cli.go | 4 ++-- requests/client.go | 18 +++++++++--------- utils/types.go | 28 ++++++++++++++++++---------- 4 files changed, 35 insertions(+), 27 deletions(-) diff --git a/config/config.go b/config/config.go index 99dae75..1f85d92 100644 --- a/config/config.go +++ b/config/config.go @@ -90,12 +90,12 @@ func (config *RequestConfig) GetMaxConns(minConns uint) uint { } type Config struct { - Method string `json:"method" validate:"http_method"` // custom validations: http_method - URL string `json:"url" validate:"http_url,required"` - Timeout uint32 `json:"timeout" validate:"gte=1,lte=100000"` - DodosCount uint `json:"dodos_count" validate:"gte=1"` - RequestCount uint `json:"request_count" validation_name:"request-count" validate:"gte=1"` - NoProxyCheck utils.Option[bool] `json:"no_proxy_check"` + Method string `json:"method" validate:"http_method"` // custom validations: http_method + URL string `json:"url" validate:"http_url,required"` + Timeout uint32 `json:"timeout" validate:"gte=1,lte=100000"` + DodosCount uint `json:"dodos_count" validate:"gte=1"` + RequestCount uint `json:"request_count" validation_name:"request-count" validate:"gte=1"` + NoProxyCheck utils.IOption[bool] `json:"no_proxy_check"` } func (config *Config) MergeConfigs(newConfig *Config) { diff --git a/readers/cli.go b/readers/cli.go index 82e9c62..2dfe975 100644 --- a/readers/cli.go +++ b/readers/cli.go @@ -12,8 +12,8 @@ import ( func CLIConfigReader() (*config.CLIConfig, error) { var ( - returnNil = false - cliConfig = &config.CLIConfig{ + returnNil = false + cliConfig = &config.CLIConfig{ Config: config.Config{ NoProxyCheck: utils.NewNoneOption[bool](), }, diff --git a/requests/client.go b/requests/client.go index da6010c..36a9859 100644 --- a/requests/client.go +++ b/requests/client.go @@ -47,15 +47,15 @@ func getClients( } clients = append(clients, &fasthttp.HostClient{ - MaxConns: int(maxConns), - IsTLS: isTLS, - Addr: addr, - Dial: dialFunc, - MaxIdleConnDuration: timeout, - MaxConnDuration: timeout, - WriteTimeout: timeout, - ReadTimeout: timeout, - }, + MaxConns: int(maxConns), + IsTLS: isTLS, + Addr: addr, + Dial: dialFunc, + MaxIdleConnDuration: timeout, + MaxConnDuration: timeout, + WriteTimeout: timeout, + ReadTimeout: timeout, + }, ) } return clients diff --git a/utils/types.go b/utils/types.go index 4f1d18e..109b3f0 100644 --- a/utils/types.go +++ b/utils/types.go @@ -5,20 +5,28 @@ import ( "errors" ) +type IOption[T any] interface { + IsNone() bool + ValueOrErr() (*T, error) + ValueOr(def *T) *T + ValueOrPanic() *T + UnmarshalJSON(data []byte) error +} + // Don't call this struct directly, use NewOption[T] or NewNoneOption[T] instead. -type Option[T any] struct { +type option[T any] struct { // value holds the actual value of the Option if it is not None. value T // none indicates whether the Option is None (i.e., has no value). none bool } -func (o *Option[T]) IsNone() bool { +func (o *option[T]) IsNone() bool { return o.none } // The returned value can be nil, if the Option is None, it will return nil and an error. -func (o *Option[T]) ValueOrErr() (*T, error) { +func (o *option[T]) ValueOrErr() (*T, error) { if o.IsNone() { return nil, errors.New("Option is None") } @@ -26,7 +34,7 @@ func (o *Option[T]) ValueOrErr() (*T, error) { } // The returned value can't be nil, if the Option is None, it will return the default value. -func (o *Option[T]) ValueOr(def *T) *T { +func (o *option[T]) ValueOr(def *T) *T { if o.IsNone() { return def } @@ -34,14 +42,14 @@ func (o *Option[T]) ValueOr(def *T) *T { } // The returned value can't be nil, if the Option is None, it will panic. -func (o *Option[T]) ValueOrPanic() *T { +func (o *option[T]) ValueOrPanic() *T { if o.IsNone() { panic("Option is None") } return &o.value } -func (o *Option[T]) UnmarshalJSON(data []byte) error { +func (o *option[T]) UnmarshalJSON(data []byte) error { if string(data) == "null" { o.none = true return nil @@ -50,10 +58,10 @@ func (o *Option[T]) UnmarshalJSON(data []byte) error { return json.Unmarshal(data, &o.value) } -func NewOption[T any](value T) Option[T] { - return Option[T]{value: value} +func NewOption[T any](value T) *option[T] { + return &option[T]{value: value} } -func NewNoneOption[T any]() Option[T] { - return Option[T]{none: true} +func NewNoneOption[T any]() *option[T] { + return &option[T]{none: true} } From c69c35bef4aa6305700fa4bab77142b6afc799fc Mon Sep 17 00:00:00 2001 From: Aykhan Shahsuvarov Date: Sat, 23 Nov 2024 18:16:01 +0400 Subject: [PATCH 13/13] =?UTF-8?q?=F0=9F=94=A8=20Replace=20type=20'any'=20w?= =?UTF-8?q?ith=20type=20'NonNilConcrete'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/types.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/utils/types.go b/utils/types.go index 109b3f0..6a070d3 100644 --- a/utils/types.go +++ b/utils/types.go @@ -5,7 +5,11 @@ import ( "errors" ) -type IOption[T any] interface { +type NonNilConcrete interface { + ~int | ~float64 | ~string | ~bool +} + +type IOption[T NonNilConcrete] interface { IsNone() bool ValueOrErr() (*T, error) ValueOr(def *T) *T @@ -14,7 +18,7 @@ type IOption[T any] interface { } // Don't call this struct directly, use NewOption[T] or NewNoneOption[T] instead. -type option[T any] struct { +type option[T NonNilConcrete] struct { // value holds the actual value of the Option if it is not None. value T // none indicates whether the Option is None (i.e., has no value). @@ -58,10 +62,10 @@ func (o *option[T]) UnmarshalJSON(data []byte) error { return json.Unmarshal(data, &o.value) } -func NewOption[T any](value T) *option[T] { +func NewOption[T NonNilConcrete](value T) *option[T] { return &option[T]{value: value} } -func NewNoneOption[T any]() *option[T] { +func NewNoneOption[T NonNilConcrete]() *option[T] { return &option[T]{none: true} }