Skip to content

Commit b3af3f6

Browse files
authored
Merge pull request #98 from aykhans/feat/config-yaml
✨ Add yaml file reader to config
2 parents 3cd7285 + ed52fff commit b3af3f6

File tree

15 files changed

+404
-119
lines changed

15 files changed

+404
-119
lines changed

Dockerfile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@ RUN go mod download
77
COPY . .
88

99
RUN go build -ldflags "-s -w" -o dodo
10-
RUN echo "{}" > config.json
1110

1211
FROM gcr.io/distroless/static-debian12:latest
1312

1413
WORKDIR /
1514

1615
COPY --from=builder /src/dodo /dodo
17-
COPY --from=builder /src/config.json /config.json
1816

19-
ENTRYPOINT ["./dodo", "-f", "/config.json"]
17+
ENTRYPOINT ["./dodo"]

README.md

Lines changed: 107 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,49 @@
33
<img width="30%" height="30%" src="https://ftp.aykhans.me/web/client/pubshares/VzPtSHS7yPQT7ngoZzZSNU/browse?path=%2Fdodo.png">
44
</p>
55

6+
## Table of Contents
7+
8+
- [Installation](#installation)
9+
- [Using Docker (Recommended)](#using-docker-recommended)
10+
- [Using Pre-built Binaries](#using-pre-built-binaries)
11+
- [Building from Source](#building-from-source)
12+
- [Usage](#usage)
13+
- [1. CLI Usage](#1-cli-usage)
14+
- [2. Config File Usage](#2-config-file-usage)
15+
- [2.1 JSON Example](#21-json-example)
16+
- [2.2 YAML/YML Example](#22-yamlyml-example)
17+
- [3. CLI & Config File Combination](#3-cli--config-file-combination)
18+
- [Config Parameters Reference](#config-parameters-reference)
19+
620
## Installation
721

822
### Using Docker (Recommended)
923

10-
Pull the Dodo image from Docker Hub:
24+
Pull the latest Dodo image from Docker Hub:
1125

1226
```sh
1327
docker pull aykhans/dodo:latest
1428
```
1529

16-
When using Dodo with Docker and a local config file, you must provide the config.json file as a volume to the Docker run command (not as the "-f config.json" argument):
30+
To use Dodo with Docker and a local config file, mount the config file as a volume and pass it as an argument:
1731

1832
```sh
19-
docker run -v /path/to/config.json:/config.json aykhans/dodo
33+
docker run -v /path/to/config.json:/config.json aykhans/dodo -f /config.json
2034
```
2135

22-
If you're using Dodo with Docker and providing a config file via URL, you don't need to set a volume:
36+
If you're using a remote config file via URL, you don't need to mount a volume:
2337

2438
```sh
25-
docker run aykhans/dodo -f https://raw.githubusercontent.com/aykhans/dodo/main/config.json
39+
docker run aykhans/dodo -f https://raw.githubusercontent.com/aykhans/dodo/main/config.yaml
2640
```
2741

28-
### Using Binary Files
42+
### Using Pre-built Binaries
2943

30-
You can download pre-built binaries from the [releases](https://github.com/aykhans/dodo/releases) section.
44+
Download the latest binaries from the [releases](https://github.com/aykhans/dodo/releases) section.
3145

3246
### Building from Source
3347

34-
To build Dodo from source, you need to have [Go 1.24+](https://golang.org/dl/) installed.
35-
Follow these steps:
48+
To build Dodo from source, ensure you have [Go 1.24+](https://golang.org/dl/) installed. Then follow these steps:
3649

3750
1. **Clone the repository:**
3851

@@ -56,9 +69,9 @@ This will generate an executable named `dodo` in the project directory.
5669

5770
## Usage
5871

59-
You can use Dodo with CLI arguments, a JSON config file, or both. When using both, CLI arguments will override JSON config values if there's a conflict.
72+
Dodo supports CLI arguments, configuration files (JSON/YAML), or a combination of both. If both are used, CLI arguments take precedence.
6073

61-
### 1. CLI
74+
### 1. CLI Usage
6275

6376
Send 1000 GET requests to https://example.com with 10 parallel dodos (threads) and a timeout of 2 seconds:
6477

@@ -72,7 +85,9 @@ With Docker:
7285
docker run --rm -i aykhans/dodo -u https://example.com -m GET -d 10 -r 1000 -t 2s
7386
```
7487

75-
### 2. JSON Config File
88+
### 2. Config File Usage
89+
90+
#### 2.1 JSON Example
7691

7792
Send 1000 GET requests to https://example.com with 10 parallel dodos (threads) and a timeout of 800 milliseconds:
7893

@@ -152,21 +167,96 @@ docker run --rm -i -v /path/to/config.json:/config.json aykhans/dodo
152167
docker run --rm -i aykhans/dodo -f https://example.com/config.json
153168
```
154169
155-
### 3. Combined (CLI & JSON)
170+
#### 2.2 YAML/YML Example
171+
172+
```yaml
173+
method: "GET"
174+
url: "https://example.com"
175+
yes: false
176+
timeout: "800ms"
177+
dodos: 10
178+
requests: 1000
179+
180+
params:
181+
# A random value will be selected from the list for first "key1" param on each request
182+
# And always "value" for second "key1" param on each request
183+
# e.g. "?key1=value2&key1=value"
184+
- key1: ["value1", "value2", "value3", "value4"]
185+
- key1: "value"
186+
187+
# A random value will be selected from the list for param "key2" on each request
188+
# e.g. "?key2=value2"
189+
- key2: ["value1", "value2"]
190+
191+
headers:
192+
# A random value will be selected from the list for first "key1" header on each request
193+
# And always "value" for second "key1" header on each request
194+
# e.g. "key1: value3", "key1: value"
195+
- key1: ["value1", "value2", "value3", "value4"]
196+
- key1: "value"
197+
198+
# A random value will be selected from the list for header "key2" on each request
199+
# e.g. "key2: value2"
200+
- key2: ["value1", "value2"]
201+
202+
cookies:
203+
# A random value will be selected from the list for first "key1" cookie on each request
204+
# And always "value" for second "key1" cookie on each request
205+
# e.g. "key1=value4; key1=value"
206+
- key1: ["value1", "value2", "value3", "value4"]
207+
- key1: "value"
208+
209+
# A random value will be selected from the list for cookie "key2" on each request
210+
# e.g. "key2=value1"
211+
- key2: ["value1", "value2"]
212+
213+
body: "body-text"
214+
# OR
215+
# A random body value will be selected from the list for each request
216+
body:
217+
- "body-text1"
218+
- "body-text2"
219+
- "body-text3"
220+
221+
proxy: "http://example.com:8080"
222+
# OR
223+
# A random proxy will be selected from the list for each request
224+
proxy:
225+
- "http://example.com:8080"
226+
- "http://username:[email protected]:8080"
227+
- "socks5://example.com:8080"
228+
- "socks5h://example.com:8080"
229+
```
230+
231+
```sh
232+
dodo -f /path/config.yaml
233+
# OR
234+
dodo -f https://example.com/config.yaml
235+
```
236+
237+
With Docker:
238+
239+
```sh
240+
docker run --rm -i -v /path/to/config.yaml:/config.yaml aykhans/dodo -f /config.yaml
241+
# OR
242+
docker run --rm -i aykhans/dodo -f https://example.com/config.yaml
243+
```
244+
245+
### 3. CLI & Config File Combination
156246
157-
Override the config file arguments with CLI arguments:
247+
CLI arguments override config file values:
158248
159249
```sh
160-
dodo -f /path/to/config.json -u https://example.com -m GET -d 10 -r 1000 -t 5s
250+
dodo -f /path/to/config.yaml -u https://example.com -m GET -d 10 -r 1000 -t 5s
161251
```
162252
163253
With Docker:
164254
165255
```sh
166-
docker run --rm -i -v /path/to/config.json:/config.json aykhans/dodo -u https://example.com -m GET -d 10 -r 1000 -t 5s
256+
docker run --rm -i -v /path/to/config.json:/config.json aykhans/dodo -f /config.json -u https://example.com -m GET -d 10 -r 1000 -t 5s
167257
```
168258
169-
## CLI and JSON Config Parameters
259+
## Config Parameters Reference
170260
171261
If `Headers`, `Params`, `Cookies`, `Body`, or `Proxy` fields have multiple values, each request will choose a random value from the list.
172262

config.yaml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
# YAML/YML config file option is not implemented yet.
2-
# This file is a example for future implementation.
3-
41
method: "GET"
52
url: "https://example.com"
63
yes: false
74
timeout: "5s"
8-
dodos: 10
5+
dodos: 8
96
requests: 1000
107

118
params:
@@ -25,13 +22,15 @@ cookies:
2522

2623
# body: "body-text"
2724
# OR
25+
# A random body value will be selected from the list for each request
2826
body:
2927
- "body-text1"
3028
- "body-text2"
3129
- "body-text3"
3230

3331
# proxy: "http://example.com:8080"
3432
# OR
33+
# A random proxy will be selected from the list for each request
3534
proxy:
3635
- "http://example.com:8080"
3736
- "http://username:[email protected]:8080"

config/config.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
)
1616

1717
const (
18-
VERSION string = "0.6.0"
18+
VERSION string = "0.6.1"
1919
DefaultUserAgent string = "Dodo/" + VERSION
2020
DefaultMethod string = "GET"
2121
DefaultTimeout time.Duration = time.Second * 10
@@ -27,17 +27,17 @@ const (
2727
var SupportedProxySchemes []string = []string{"http", "socks5", "socks5h"}
2828

2929
type RequestConfig struct {
30-
Method string `json:"method"`
31-
URL url.URL `json:"url"`
32-
Timeout time.Duration `json:"timeout"`
33-
DodosCount uint `json:"dodos"`
34-
RequestCount uint `json:"requests"`
35-
Yes bool `json:"yes"`
36-
Params types.Params `json:"params"`
37-
Headers types.Headers `json:"headers"`
38-
Cookies types.Cookies `json:"cookies"`
39-
Body types.Body `json:"body"`
40-
Proxies types.Proxies `json:"proxies"`
30+
Method string
31+
URL url.URL
32+
Timeout time.Duration
33+
DodosCount uint
34+
RequestCount uint
35+
Yes bool
36+
Params types.Params
37+
Headers types.Headers
38+
Cookies types.Cookies
39+
Body types.Body
40+
Proxies types.Proxies
4141
}
4242

4343
func NewRequestConfig(conf *Config) *RequestConfig {
@@ -111,17 +111,17 @@ func (rc *RequestConfig) Print() {
111111
}
112112

113113
type Config struct {
114-
Method *string `json:"method"`
115-
URL *types.RequestURL `json:"url"`
116-
Timeout *types.Timeout `json:"timeout"`
117-
DodosCount *uint `json:"dodos"`
118-
RequestCount *uint `json:"requests"`
119-
Yes *bool `json:"yes"`
120-
Params types.Params `json:"params"`
121-
Headers types.Headers `json:"headers"`
122-
Cookies types.Cookies `json:"cookies"`
123-
Body types.Body `json:"body"`
124-
Proxies types.Proxies `json:"proxy"`
114+
Method *string `json:"method" yaml:"method"`
115+
URL *types.RequestURL `json:"url" yaml:"url"`
116+
Timeout *types.Timeout `json:"timeout" yaml:"timeout"`
117+
DodosCount *uint `json:"dodos" yaml:"dodos"`
118+
RequestCount *uint `json:"requests" yaml:"requests"`
119+
Yes *bool `json:"yes" yaml:"yes"`
120+
Params types.Params `json:"params" yaml:"params"`
121+
Headers types.Headers `json:"headers" yaml:"headers"`
122+
Cookies types.Cookies `json:"cookies" yaml:"cookies"`
123+
Body types.Body `json:"body" yaml:"body"`
124+
Proxies types.Proxies `json:"proxy" yaml:"proxy"`
125125
}
126126

127127
func NewConfig() *Config {

config/file.go

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,54 @@ import (
77
"io"
88
"net/http"
99
"os"
10+
"slices"
11+
"strings"
1012
"time"
1113

1214
"github.com/aykhans/dodo/types"
15+
"gopkg.in/yaml.v3"
1316
)
1417

18+
var supportedFileTypes = []string{"json", "yaml", "yml"}
19+
1520
func (config *Config) ReadFile(filePath types.ConfigFile) error {
1621
var (
1722
data []byte
1823
err error
1924
)
2025

21-
if filePath.LocationType() == types.FileLocationTypeRemoteHTTP {
22-
client := &http.Client{
23-
Timeout: 10 * time.Second,
24-
}
26+
fileExt := filePath.Extension()
27+
if slices.Contains(supportedFileTypes, fileExt) {
28+
if filePath.LocationType() == types.FileLocationTypeRemoteHTTP {
29+
client := &http.Client{
30+
Timeout: 10 * time.Second,
31+
}
2532

26-
resp, err := client.Get(filePath.String())
27-
if err != nil {
28-
return fmt.Errorf("failed to fetch config file from %s", filePath)
29-
}
30-
defer resp.Body.Close()
33+
resp, err := client.Get(filePath.String())
34+
if err != nil {
35+
return fmt.Errorf("failed to fetch config file from %s", filePath)
36+
}
37+
defer resp.Body.Close()
3138

32-
data, err = io.ReadAll(io.Reader(resp.Body))
33-
if err != nil {
34-
return fmt.Errorf("failed to read config file from %s", filePath)
39+
data, err = io.ReadAll(io.Reader(resp.Body))
40+
if err != nil {
41+
return fmt.Errorf("failed to read config file from %s", filePath)
42+
}
43+
} else {
44+
data, err = os.ReadFile(filePath.String())
45+
if err != nil {
46+
return errors.New("failed to read config file from " + filePath.String())
47+
}
3548
}
36-
} else {
37-
data, err = os.ReadFile(filePath.String())
38-
if err != nil {
39-
return errors.New("failed to read config file from " + filePath.String())
49+
50+
if fileExt == "json" {
51+
return parseJSONConfig(data, config)
52+
} else if fileExt == "yml" || fileExt == "yaml" {
53+
return parseYAMLConfig(data, config)
4054
}
4155
}
4256

43-
return parseJSONConfig(data, config)
57+
return fmt.Errorf("unsupported config file type (supported types: %v)", strings.Join(supportedFileTypes, ", "))
4458
}
4559

4660
func parseJSONConfig(data []byte, config *Config) error {
@@ -58,3 +72,12 @@ func parseJSONConfig(data []byte, config *Config) error {
5872

5973
return nil
6074
}
75+
76+
func parseYAMLConfig(data []byte, config *Config) error {
77+
err := yaml.Unmarshal(data, &config)
78+
if err != nil {
79+
return fmt.Errorf("YAML Config file: %s", err.Error())
80+
}
81+
82+
return nil
83+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.24.0
55
require (
66
github.com/jedib0t/go-pretty/v6 v6.6.7
77
github.com/valyala/fasthttp v1.59.0
8+
gopkg.in/yaml.v3 v3.0.1
89
)
910

1011
require (

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,7 @@ golang.org/x/term v0.29.0 h1:L6pJp37ocefwRRtYPKSWOWzOtWSxVajvz2ldH/xi3iU=
2929
golang.org/x/term v0.29.0/go.mod h1:6bl4lRlvVuDgSf3179VpIxBF0o10JUpXWOnI7nErv7s=
3030
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
3131
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
32+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
33+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3234
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
3335
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)