Procházet zdrojové kódy

Merge pull request #198 from 9seconds/simple-run

Simple run mode
tags/v2.1.0^2
Sergey Arkhipov před 4 roky
rodič
revize
7c70fd8079
Žádný účet není propojen s e-mailovou adresou tvůrce revize
56 změnil soubory, kde provedl 1605 přidání a 1783 odebrání
  1. 44
    0
      README.md
  2. 32
    41
      internal/cli/access.go
  3. 0
    197
      internal/cli/access_test.go
  4. 0
    81
      internal/cli/base.go
  5. 0
    33
      internal/cli/base_internal_test.go
  6. 2
    1
      internal/cli/cli.go
  7. 2
    2
      internal/cli/generate_secret.go
  8. 0
    51
      internal/cli/generate_secret_test.go
  9. 0
    37
      internal/cli/init_test.go
  10. 0
    167
      internal/cli/proxy.go
  11. 20
    0
      internal/cli/run.go
  12. 209
    0
      internal/cli/run_proxy.go
  13. 84
    0
      internal/cli/simple_run.go
  14. 16
    92
      internal/config/config.go
  15. 80
    0
      internal/config/parse.go
  16. 36
    27
      internal/config/type_blocklist_uri.go
  17. 37
    101
      internal/config/type_blocklist_uri_test.go
  18. 37
    0
      internal/config/type_bool.go
  19. 107
    0
      internal/config/type_bool_test.go
  20. 25
    24
      internal/config/type_bytes.go
  21. 11
    45
      internal/config/type_bytes_test.go
  22. 45
    0
      internal/config/type_concurrency.go
  23. 73
    0
      internal/config/type_concurrency_test.go
  24. 26
    19
      internal/config/type_duration.go
  25. 33
    41
      internal/config/type_duration_test.go
  26. 20
    16
      internal/config/type_error_rate.go
  27. 33
    77
      internal/config/type_error_rate_test.go
  28. 30
    34
      internal/config/type_hostport.go
  29. 21
    48
      internal/config/type_hostport_test.go
  30. 16
    18
      internal/config/type_http_path.go
  31. 24
    48
      internal/config/type_http_path_test.go
  32. 20
    20
      internal/config/type_ip.go
  33. 33
    44
      internal/config/type_ip_test.go
  34. 18
    19
      internal/config/type_metric_prefix.go
  35. 20
    65
      internal/config/type_metric_prefix_test.go
  36. 20
    20
      internal/config/type_port.go
  37. 24
    70
      internal/config/type_port_test.go
  38. 22
    21
      internal/config/type_prefer_ip.go
  39. 28
    57
      internal/config/type_prefer_ip_test.go
  40. 61
    0
      internal/config/type_proxy_url.go
  41. 96
    0
      internal/config/type_proxy_url_test.go
  42. 22
    21
      internal/config/type_statsd_tag_format.go
  43. 30
    58
      internal/config/type_statsd_tag_format_test.go
  44. 0
    71
      internal/config/type_url.go
  45. 0
    107
      internal/config/type_url_test.go
  46. 19
    0
      internal/utils/make_qr_code_url.go
  47. 31
    0
      internal/utils/make_qr_code_url_test.go
  48. 26
    0
      internal/utils/read_config.go
  49. 55
    0
      internal/utils/read_config_test.go
  50. 1
    0
      internal/utils/testdata/broken.toml
  51. 0
    0
      internal/utils/testdata/empty.toml
  52. 0
    0
      internal/utils/testdata/minimal.toml
  53. 2
    0
      internal/utils/testdata/missed-bindto.toml
  54. 1
    0
      internal/utils/testdata/missed-secret.toml
  55. 9
    9
      mtglib/proxy_opts.go
  56. 4
    1
      mtglib/secret.go

+ 44
- 0
README.md Zobrazit soubor

@@ -237,6 +237,50 @@ For example, you've bought a VPS from [Digital
237 237
 Ocean](https://www.digitalocean.com/). Then it might be a good idea to
238 238
 generate a secret for _digitalocean.com_ then.
239 239
 
240
+
241
+### Simple run mode
242
+
243
+mtg supports 2 modes: simple and normal. Simple mode allows starting
244
+proxy with a small subset of configuration options you usually want to
245
+modify. This is quite good for oneliners that you can copy-paste and do
246
+not bother about external files whatsoever.
247
+
248
+Let's take a look:
249
+
250
+```console
251
+Usage: mtg simple-run <bind-to> <secret>
252
+
253
+Run proxy without config file.
254
+
255
+Arguments:
256
+  <bind-to>    A host:port to bind proxy to.
257
+  <secret>     Proxy secret.
258
+
259
+Flags:
260
+  -h, --help                           Show context-sensitive help.
261
+  -v, --version                        Print version.
262
+
263
+  -d, --debug                          Run in debug mode.
264
+  -c, --concurrency=8192               Max number of concurrent connection to proxy.
265
+  -b, --tcp-buffer="4KB"               Size of TCP buffer to use.
266
+  -i, --prefer-ip="prefer-ipv6"        IP preference. By default we prefer IPv6 with fallback to IPv4.
267
+  -p, --domain-fronting-port=443       A port to access for domain fronting.
268
+  -n, --doh-ip=9.9.9.9                 IP address of DNS-over-HTTP to use.
269
+  -t, --timeout=10s                    Network timeout to use
270
+  -a, --antireplay-cache-size="1MB"    A size of anti-replay cache to use.
271
+```
272
+
273
+So, if you want to startup a proxy with CLI only, you can do something like
274
+
275
+```console
276
+$ mtg simple-run -n 1.1.1.1 -t 30s -a 512kib 127.0.0.1:3128 7hBO-dCS4EBzenlKbdLFxyNnb29nbGUuY29t
277
+```
278
+
279
+The rest of the configuration will be taken from default values. But
280
+a simple run is fine if you do not have any special requirements or
281
+granular tuning. If you want it, please checkout the configuration
282
+files.
283
+
240 284
 ### Prepare a configuration file
241 285
 
242 286
 Please checkout an example configuration file. All options except of

+ 32
- 41
internal/cli/access.go Zobrazit soubor

@@ -12,6 +12,10 @@ import (
12 12
 	"strconv"
13 13
 	"strings"
14 14
 	"sync"
15
+
16
+	"github.com/9seconds/mtg/v2/internal/config"
17
+	"github.com/9seconds/mtg/v2/internal/utils"
18
+	"github.com/9seconds/mtg/v2/mtglib"
15 19
 )
16 20
 
17 21
 type accessResponse struct {
@@ -33,26 +37,27 @@ type accessResponseURLs struct {
33 37
 }
34 38
 
35 39
 type Access struct {
36
-	base
37
-
40
+	ConfigPath string `kong:"arg,required,type='existingfile',help='Path to the configuration file.',name='config-path'"`                 // nolint: lll
38 41
 	PublicIPv4 net.IP `kong:"help='Public IPv4 address for proxy. By default it is resolved via remote website',name='ipv4',short='i'"`   // nolint: lll
39 42
 	PublicIPv6 net.IP `kong:"help='Public IPv6 address for proxy. By default it is resolved via remote website',name='ipv6',short='I'"`   // nolint: lll
40 43
 	Port       uint   `kong:"help='Port number. Default port is taken from configuration file, bind-to parameter',type:'uint',short='p'"` // nolint: lll
41 44
 	Hex        bool   `kong:"help='Print secret in hex encoding.',short='x'"`
42 45
 }
43 46
 
44
-func (c *Access) Run(cli *CLI, version string) error {
45
-	if err := c.ReadConfig(version); err != nil {
47
+func (a *Access) Run(cli *CLI, version string) error {
48
+	conf, err := utils.ReadConfig(a.ConfigPath)
49
+	if err != nil {
46 50
 		return fmt.Errorf("cannot init config: %w", err)
47 51
 	}
48 52
 
49
-	return c.Execute(cli)
50
-}
51
-
52
-func (c *Access) Execute(cli *CLI) error {
53 53
 	resp := &accessResponse{}
54
-	resp.Secret.Base64 = c.Config.Secret.Base64()
55
-	resp.Secret.Hex = c.Config.Secret.Hex()
54
+	resp.Secret.Base64 = conf.Secret.Base64()
55
+	resp.Secret.Hex = conf.Secret.Hex()
56
+
57
+	ntw, err := makeNetwork(conf, version)
58
+	if err != nil {
59
+		return fmt.Errorf("cannot init network: %w", err)
60
+	}
56 61
 
57 62
 	wg := &sync.WaitGroup{}
58 63
 	wg.Add(2) // nolint: gomnd
@@ -60,31 +65,31 @@ func (c *Access) Execute(cli *CLI) error {
60 65
 	go func() {
61 66
 		defer wg.Done()
62 67
 
63
-		ip := cli.Access.PublicIPv4
68
+		ip := a.PublicIPv4
64 69
 		if ip == nil {
65
-			ip = c.getIP("tcp4")
70
+			ip = a.getIP(ntw, "tcp4")
66 71
 		}
67 72
 
68 73
 		if ip != nil {
69 74
 			ip = ip.To4()
70 75
 		}
71 76
 
72
-		resp.IPv4 = c.makeURLs(ip, cli)
77
+		resp.IPv4 = a.makeURLs(conf, ip)
73 78
 	}()
74 79
 
75 80
 	go func() {
76 81
 		defer wg.Done()
77 82
 
78
-		ip := cli.Access.PublicIPv6
83
+		ip := a.PublicIPv6
79 84
 		if ip == nil {
80
-			ip = c.getIP("tcp6")
85
+			ip = a.getIP(ntw, "tcp6")
81 86
 		}
82 87
 
83 88
 		if ip != nil {
84 89
 			ip = ip.To16()
85 90
 		}
86 91
 
87
-		resp.IPv6 = c.makeURLs(ip, cli)
92
+		resp.IPv6 = a.makeURLs(conf, ip)
88 93
 	}()
89 94
 
90 95
 	wg.Wait()
@@ -100,9 +105,9 @@ func (c *Access) Execute(cli *CLI) error {
100 105
 	return nil
101 106
 }
102 107
 
103
-func (c *Access) getIP(protocol string) net.IP {
104
-	client := c.Network.MakeHTTPClient(func(ctx context.Context, network, address string) (net.Conn, error) {
105
-		return c.Network.DialContext(ctx, protocol, address) // nolint: wrapcheck
108
+func (a *Access) getIP(ntw mtglib.Network, protocol string) net.IP {
109
+	client := ntw.MakeHTTPClient(func(ctx context.Context, network, address string) (net.Conn, error) {
110
+		return ntw.DialContext(ctx, protocol, address) // nolint: wrapcheck
106 111
 	})
107 112
 
108 113
 	req, err := http.NewRequest(http.MethodGet, "https://ifconfig.co", nil) // nolint: noctx
@@ -134,24 +139,24 @@ func (c *Access) getIP(protocol string) net.IP {
134 139
 	return net.ParseIP(strings.TrimSpace(string(data)))
135 140
 }
136 141
 
137
-func (c *Access) makeURLs(ip net.IP, cli *CLI) *accessResponseURLs {
142
+func (a *Access) makeURLs(conf *config.Config, ip net.IP) *accessResponseURLs {
138 143
 	if ip == nil {
139 144
 		return nil
140 145
 	}
141 146
 
142
-	portNo := cli.Access.Port
147
+	portNo := a.Port
143 148
 	if portNo == 0 {
144
-		portNo = c.Config.BindTo.PortValue(0)
149
+		portNo = conf.BindTo.Port
145 150
 	}
146 151
 
147 152
 	values := url.Values{}
148 153
 	values.Set("server", ip.String())
149 154
 	values.Set("port", strconv.Itoa(int(portNo)))
150 155
 
151
-	if cli.Access.Hex {
152
-		values.Set("secret", c.Config.Secret.Hex())
156
+	if a.Hex {
157
+		values.Set("secret", conf.Secret.Hex())
153 158
 	} else {
154
-		values.Set("secret", c.Config.Secret.Base64())
159
+		values.Set("secret", conf.Secret.Base64())
155 160
 	}
156 161
 
157 162
 	urlQuery := values.Encode()
@@ -171,22 +176,8 @@ func (c *Access) makeURLs(ip net.IP, cli *CLI) *accessResponseURLs {
171 176
 			RawQuery: urlQuery,
172 177
 		}).String(),
173 178
 	}
174
-	rv.TgQrCode = c.makeQRCode(rv.TgURL)
175
-	rv.TmeQrCode = c.makeQRCode(rv.TmeURL)
179
+	rv.TgQrCode = utils.MakeQRCodeURL(rv.TgURL)
180
+	rv.TmeQrCode = utils.MakeQRCodeURL(rv.TmeURL)
176 181
 
177 182
 	return rv
178 183
 }
179
-
180
-func (c *Access) makeQRCode(data string) string {
181
-	values := url.Values{}
182
-	values.Set("qzone", "4")
183
-	values.Set("format", "svg")
184
-	values.Set("data", data)
185
-
186
-	return (&url.URL{
187
-		Scheme:   "https",
188
-		Host:     "api.qrserver.com",
189
-		Path:     "v1/create-qr-code",
190
-		RawQuery: values.Encode(),
191
-	}).String()
192
-}

+ 0
- 197
internal/cli/access_test.go Zobrazit soubor

@@ -1,197 +0,0 @@
1
-package cli_test
2
-
3
-import (
4
-	"net"
5
-	"net/http"
6
-	"testing"
7
-
8
-	"github.com/9seconds/mtg/v2/internal/config"
9
-	"github.com/9seconds/mtg/v2/internal/testlib"
10
-	"github.com/9seconds/mtg/v2/mtglib"
11
-	"github.com/jarcoal/httpmock"
12
-	"github.com/stretchr/testify/suite"
13
-	"github.com/xeipuuv/gojsonschema"
14
-)
15
-
16
-var accressResponseJSONSchema = func() *gojsonschema.Schema {
17
-	schema, err := gojsonschema.NewSchema(gojsonschema.NewStringLoader(`
18
-{
19
-    "type": "object",
20
-    "required": ["secret"],
21
-    "additionalProperties": true,
22
-    "properties": {
23
-        "secret": {
24
-            "type": "object",
25
-            "required": [
26
-                "hex",
27
-                "base64"
28
-            ],
29
-            "additionalProperties": false,
30
-            "properties": {
31
-                "hex": {
32
-                    "type": "string",
33
-                    "minLength": 34
34
-                },
35
-                "base64": {
36
-                    "type": "string",
37
-                    "minLength": 10
38
-                }
39
-            }
40
-        },
41
-        "ipv4": {
42
-            "$ref": "#/definitions/ip"
43
-        },
44
-        "ipv6": {
45
-            "$ref": "#/definitions/ip"
46
-        }
47
-    },
48
-    "definitions": {
49
-        "ip": {
50
-            "type": "object",
51
-            "required": [
52
-                "ip",
53
-                "port",
54
-                "tg_url",
55
-                "tg_qrcode",
56
-                "tme_url",
57
-                "tme_qrcode"
58
-            ],
59
-            "additionalProperties": false,
60
-            "properties": {
61
-                "ip": {
62
-                    "type": "string",
63
-                    "minLength": 1,
64
-                    "anyOf": [
65
-                        {
66
-                            "format": "ipv4"
67
-                        },
68
-                        {
69
-                            "format": "ipv6"
70
-                        }
71
-                    ]
72
-                },
73
-                "port": {
74
-                    "type": "integer",
75
-                    "multipleOf": 1.0,
76
-                    "exclusiveMinimum": 0,
77
-                    "exclusiveMaximum": 65536
78
-                },
79
-                "tg_url": {
80
-                    "type": "string",
81
-                    "minLength": 1,
82
-                    "format": "uri"
83
-                },
84
-                "tg_qrcode": {
85
-                    "type": "string",
86
-                    "minLength": 1,
87
-                    "format": "uri"
88
-                },
89
-                "tme_url": {
90
-                    "type": "string",
91
-                    "minLength": 1,
92
-                    "format": "uri"
93
-                },
94
-                "tme_qrcode": {
95
-                    "type": "string",
96
-                    "minLength": 1,
97
-                    "format": "uri"
98
-                }
99
-            }
100
-        }
101
-    }
102
-}
103
-    `))
104
-	if err != nil {
105
-		panic(err)
106
-	}
107
-
108
-	return schema
109
-}()
110
-
111
-type AccessTestSuite struct {
112
-	CommonTestSuite
113
-}
114
-
115
-func (suite *AccessTestSuite) SetupTest() {
116
-	suite.CommonTestSuite.SetupTest()
117
-
118
-	suite.cli.Access.Config = &config.Config{}
119
-	suite.cli.Access.Config.Secret = mtglib.GenerateSecret("google.com")
120
-	suite.cli.Access.Network = suite.networkMock
121
-
122
-	suite.NoError(
123
-		suite.cli.Access.Config.BindTo.UnmarshalText([]byte("0.0.0.0:80")))
124
-}
125
-
126
-func (suite *AccessTestSuite) TestGenerateNoCalls() {
127
-	suite.cli.Access.PublicIPv4 = net.ParseIP("10.0.0.10")
128
-	suite.cli.Access.PublicIPv6 = net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334")
129
-
130
-	output := testlib.CaptureStdout(func() {
131
-		suite.NoError(suite.cli.Access.Execute(suite.cli))
132
-	})
133
-
134
-	validated, err := accressResponseJSONSchema.Validate(
135
-		gojsonschema.NewStringLoader(output))
136
-	suite.NoError(err)
137
-	suite.Empty(validated.Errors())
138
-	suite.True(validated.Valid())
139
-
140
-	suite.Contains(output, "10.0.0.10")
141
-	suite.Contains(output, "2001:db8:85a3::8a2e:370:7334")
142
-	suite.Contains(output, "ipv4")
143
-	suite.Contains(output, "ipv6")
144
-	suite.Contains(output, suite.cli.Access.Config.Secret.Base64())
145
-	suite.Contains(output, suite.cli.Access.Config.Secret.Hex())
146
-}
147
-
148
-func (suite *AccessTestSuite) TestGenerateIPv4Call() {
149
-	suite.cli.Access.PublicIPv6 = net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334")
150
-
151
-	httpmock.RegisterResponder(http.MethodGet, "https://ifconfig.co",
152
-		httpmock.NewStringResponder(http.StatusOK, "10.11.12.13"))
153
-
154
-	output := testlib.CaptureStdout(func() {
155
-		suite.NoError(suite.cli.Access.Execute(suite.cli))
156
-	})
157
-
158
-	validated, err := accressResponseJSONSchema.Validate(
159
-		gojsonschema.NewStringLoader(output))
160
-	suite.NoError(err)
161
-	suite.Empty(validated.Errors())
162
-	suite.True(validated.Valid())
163
-
164
-	suite.Contains(output, "10.11.12.13")
165
-	suite.Contains(output, "2001:db8:85a3::8a2e:370:7334")
166
-	suite.Contains(output, "ipv4")
167
-	suite.Contains(output, "ipv6")
168
-	suite.Contains(output, suite.cli.Access.Config.Secret.Base64())
169
-	suite.Contains(output, suite.cli.Access.Config.Secret.Hex())
170
-}
171
-
172
-func (suite *AccessTestSuite) TestIPv4CallFail() {
173
-	suite.cli.Access.PublicIPv6 = net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334")
174
-
175
-	httpmock.RegisterResponder(http.MethodGet, "https://ifconfig.co",
176
-		httpmock.NewStringResponder(http.StatusForbidden, ""))
177
-
178
-	output := testlib.CaptureStdout(func() {
179
-		suite.NoError(suite.cli.Access.Execute(suite.cli))
180
-	})
181
-
182
-	validated, err := accressResponseJSONSchema.Validate(
183
-		gojsonschema.NewStringLoader(output))
184
-	suite.NoError(err)
185
-	suite.Empty(validated.Errors())
186
-	suite.True(validated.Valid())
187
-
188
-	suite.Contains(output, "2001:db8:85a3::8a2e:370:7334")
189
-	suite.NotContains(output, "ipv4")
190
-	suite.Contains(output, "ipv6")
191
-	suite.Contains(output, suite.cli.Access.Config.Secret.Base64())
192
-	suite.Contains(output, suite.cli.Access.Config.Secret.Hex())
193
-}
194
-
195
-func TestAccess(t *testing.T) { // nolint: paralleltest
196
-	suite.Run(t, &AccessTestSuite{})
197
-}

+ 0
- 81
internal/cli/base.go Zobrazit soubor

@@ -1,81 +0,0 @@
1
-package cli
2
-
3
-import (
4
-	"fmt"
5
-	"net"
6
-	"net/url"
7
-	"os"
8
-
9
-	"github.com/9seconds/mtg/v2/internal/config"
10
-	"github.com/9seconds/mtg/v2/mtglib"
11
-	"github.com/9seconds/mtg/v2/network"
12
-)
13
-
14
-type base struct {
15
-	ConfigPath string `kong:"arg,required,type='existingfile',help='Path to the configuration file.',name='config-path'"` // nolint: lll
16
-
17
-	Network mtglib.Network `kong:"-"`
18
-	Config  *config.Config `kong:"-"`
19
-}
20
-
21
-func (b *base) ReadConfig(version string) error {
22
-	content, err := os.ReadFile(b.ConfigPath)
23
-	if err != nil {
24
-		return fmt.Errorf("cannot read config file: %w", err)
25
-	}
26
-
27
-	conf, err := config.Parse(content)
28
-	if err != nil {
29
-		return fmt.Errorf("cannot parse config: %w", err)
30
-	}
31
-
32
-	ntw, err := b.makeNetwork(conf, version)
33
-	if err != nil {
34
-		return fmt.Errorf("cannot build a network: %w", err)
35
-	}
36
-
37
-	b.Config = conf
38
-	b.Network = ntw
39
-
40
-	return nil
41
-}
42
-
43
-func (b *base) makeNetwork(conf *config.Config, version string) (mtglib.Network, error) {
44
-	tcpTimeout := conf.Network.Timeout.TCP.Value(network.DefaultTimeout)
45
-	httpTimeout := conf.Network.Timeout.HTTP.Value(network.DefaultHTTPTimeout)
46
-	dohIP := conf.Network.DOHIP.Value(net.ParseIP(network.DefaultDOHHostname)).String()
47
-	bufferSize := conf.TCPBuffer.Value(network.DefaultBufferSize)
48
-	userAgent := "mtg/" + version
49
-
50
-	baseDialer, err := network.NewDefaultDialer(tcpTimeout, int(bufferSize))
51
-	if err != nil {
52
-		return nil, fmt.Errorf("cannot build a default dialer: %w", err)
53
-	}
54
-
55
-	proxyURLs := make([]*url.URL, 0, len(conf.Network.Proxies))
56
-
57
-	for _, v := range conf.Network.Proxies {
58
-		if value := v.Value(nil); value != nil {
59
-			proxyURLs = append(proxyURLs, v.Value(nil))
60
-		}
61
-	}
62
-
63
-	switch len(proxyURLs) {
64
-	case 0:
65
-		return network.NewNetwork(baseDialer, userAgent, dohIP, httpTimeout) // nolint: wrapcheck
66
-	case 1:
67
-		socksDialer, err := network.NewSocks5Dialer(baseDialer, proxyURLs[0])
68
-		if err != nil {
69
-			return nil, fmt.Errorf("cannot build socks5 dialer: %w", err)
70
-		}
71
-
72
-		return network.NewNetwork(socksDialer, userAgent, dohIP, httpTimeout) // nolint: wrapcheck
73
-	}
74
-
75
-	socksDialer, err := network.NewLoadBalancedSocks5Dialer(baseDialer, proxyURLs)
76
-	if err != nil {
77
-		return nil, fmt.Errorf("cannot build socks5 dialer: %w", err)
78
-	}
79
-
80
-	return network.NewNetwork(socksDialer, userAgent, dohIP, httpTimeout) // nolint: wrapcheck
81
-}

+ 0
- 33
internal/cli/base_internal_test.go Zobrazit soubor

@@ -1,33 +0,0 @@
1
-package cli
2
-
3
-import (
4
-	"path/filepath"
5
-	"testing"
6
-
7
-	"github.com/stretchr/testify/suite"
8
-)
9
-
10
-type BaseTestSuite struct {
11
-	suite.Suite
12
-
13
-	b base
14
-}
15
-
16
-func (suite *BaseTestSuite) SetupTest() {
17
-	suite.b = base{}
18
-}
19
-
20
-func (suite *BaseTestSuite) TestReadConfigNok() {
21
-	suite.b.ConfigPath = filepath.Join("testdata", "unknown")
22
-	suite.Error(suite.b.ReadConfig("dev"))
23
-}
24
-
25
-func (suite *BaseTestSuite) TestReadConfig() {
26
-	suite.b.ConfigPath = filepath.Join("testdata", "minimal.toml")
27
-	suite.NoError(suite.b.ReadConfig("dev"))
28
-}
29
-
30
-func TestBase(t *testing.T) {
31
-	t.Parallel()
32
-	suite.Run(t, &BaseTestSuite{})
33
-}

+ 2
- 1
internal/cli/cli.go Zobrazit soubor

@@ -5,6 +5,7 @@ import "github.com/alecthomas/kong"
5 5
 type CLI struct {
6 6
 	GenerateSecret GenerateSecret   `kong:"cmd,help='Generate new proxy secret'"`
7 7
 	Access         Access           `kong:"cmd,help='Print access information.'"`
8
-	Run            Proxy            `kong:"cmd,help='Run proxy.'"`
8
+	Run            Run              `kong:"cmd,help='Run proxy.'"`
9
+	SimpleRun      SimpleRun        `kong:"cmd,help='Run proxy without config file.'"`
9 10
 	Version        kong.VersionFlag `kong:"help='Print version.',short='v'"`
10 11
 }

+ 2
- 2
internal/cli/generate_secret.go Zobrazit soubor

@@ -11,10 +11,10 @@ type GenerateSecret struct {
11 11
 	Hex      bool   `kong:"help='Print secret in hex encoding.',short='x'"`
12 12
 }
13 13
 
14
-func (c *GenerateSecret) Run(cli *CLI, _ string) error {
14
+func (g *GenerateSecret) Run(cli *CLI, _ string) error {
15 15
 	secret := mtglib.GenerateSecret(cli.GenerateSecret.HostName)
16 16
 
17
-	if cli.GenerateSecret.Hex {
17
+	if g.Hex {
18 18
 		fmt.Println(secret.Hex()) // nolint: forbidigo
19 19
 	} else {
20 20
 		fmt.Println(secret.Base64()) // nolint: forbidigo

+ 0
- 51
internal/cli/generate_secret_test.go Zobrazit soubor

@@ -1,51 +0,0 @@
1
-package cli_test
2
-
3
-import (
4
-	"strings"
5
-	"testing"
6
-
7
-	"github.com/9seconds/mtg/v2/internal/testlib"
8
-	"github.com/9seconds/mtg/v2/mtglib"
9
-	"github.com/stretchr/testify/suite"
10
-)
11
-
12
-type GenerateSecretTestSuite struct {
13
-	CommonTestSuite
14
-}
15
-
16
-func (suite *GenerateSecretTestSuite) SetupTest() {
17
-	suite.CommonTestSuite.SetupTest()
18
-
19
-	suite.cli.GenerateSecret.HostName = "google.com"
20
-}
21
-
22
-func (suite *GenerateSecretTestSuite) TestDefault() {
23
-	output := testlib.CaptureStdout(func() {
24
-		suite.NoError(suite.cli.GenerateSecret.Run(suite.cli, "dev"))
25
-	})
26
-	suite.True(strings.HasPrefix(output, "7"))
27
-
28
-	secret, err := mtglib.ParseSecret(output)
29
-	suite.NoError(err)
30
-	suite.True(secret.Valid())
31
-	suite.Equal("google.com", secret.Host)
32
-}
33
-
34
-func (suite *GenerateSecretTestSuite) TestHex() {
35
-	suite.cli.GenerateSecret.Hex = true
36
-
37
-	output := testlib.CaptureStdout(func() {
38
-		suite.NoError(suite.cli.GenerateSecret.Run(suite.cli, "dev"))
39
-	})
40
-	suite.True(strings.HasPrefix(output, "ee"))
41
-
42
-	secret, err := mtglib.ParseSecret(output)
43
-	suite.NoError(err)
44
-	suite.True(secret.Valid())
45
-	suite.Equal("google.com", secret.Host)
46
-}
47
-
48
-func TestGenerateSecret(t *testing.T) {
49
-	t.Parallel()
50
-	suite.Run(t, &GenerateSecretTestSuite{})
51
-}

+ 0
- 37
internal/cli/init_test.go Zobrazit soubor

@@ -1,37 +0,0 @@
1
-package cli_test
2
-
3
-import (
4
-	"net/http"
5
-
6
-	"github.com/9seconds/mtg/v2/internal/cli"
7
-	"github.com/9seconds/mtg/v2/internal/testlib"
8
-	"github.com/jarcoal/httpmock"
9
-	"github.com/stretchr/testify/mock"
10
-	"github.com/stretchr/testify/suite"
11
-)
12
-
13
-type CommonTestSuite struct {
14
-	suite.Suite
15
-
16
-	cli         *cli.CLI
17
-	networkMock *testlib.MtglibNetworkMock
18
-	httpClient  *http.Client
19
-}
20
-
21
-func (suite *CommonTestSuite) SetupTest() {
22
-	suite.networkMock = &testlib.MtglibNetworkMock{}
23
-	suite.httpClient = &http.Client{}
24
-	suite.cli = &cli.CLI{}
25
-
26
-	httpmock.ActivateNonDefault(suite.httpClient)
27
-
28
-	suite.networkMock.
29
-		On("MakeHTTPClient", mock.Anything).
30
-		Maybe().
31
-		Return(suite.httpClient)
32
-}
33
-
34
-func (suite *CommonTestSuite) TearDownTest() {
35
-	suite.networkMock.AssertExpectations(suite.T())
36
-	httpmock.DeactivateAndReset()
37
-}

+ 0
- 167
internal/cli/proxy.go Zobrazit soubor

@@ -1,167 +0,0 @@
1
-package cli
2
-
3
-import (
4
-	"fmt"
5
-	"net"
6
-	"os"
7
-
8
-	"github.com/9seconds/mtg/v2/antireplay"
9
-	"github.com/9seconds/mtg/v2/events"
10
-	"github.com/9seconds/mtg/v2/internal/utils"
11
-	"github.com/9seconds/mtg/v2/ipblocklist"
12
-	"github.com/9seconds/mtg/v2/logger"
13
-	"github.com/9seconds/mtg/v2/mtglib"
14
-	"github.com/9seconds/mtg/v2/stats"
15
-	"github.com/rs/zerolog"
16
-)
17
-
18
-type Proxy struct {
19
-	base
20
-}
21
-
22
-func (c *Proxy) Run(cli *CLI, version string) error {
23
-	if err := c.ReadConfig(version); err != nil {
24
-		return fmt.Errorf("cannot init config: %w", err)
25
-	}
26
-
27
-	return c.Execute()
28
-}
29
-
30
-func (c *Proxy) Execute() error {
31
-	zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMs
32
-	zerolog.TimestampFieldName = "timestamp"
33
-	zerolog.LevelFieldName = "level"
34
-
35
-	if c.Config.Debug {
36
-		zerolog.SetGlobalLevel(zerolog.DebugLevel)
37
-	} else {
38
-		zerolog.SetGlobalLevel(zerolog.WarnLevel)
39
-	}
40
-
41
-	ctx := utils.RootContext()
42
-	opts := mtglib.ProxyOpts{
43
-		Logger:          logger.NewZeroLogger(zerolog.New(os.Stdout).With().Timestamp().Logger()),
44
-		Network:         c.Network,
45
-		AntiReplayCache: antireplay.NewNoop(),
46
-		IPBlocklist:     ipblocklist.NewNoop(),
47
-		EventStream:     events.NewNoopStream(),
48
-
49
-		Secret:             c.Config.Secret,
50
-		BufferSize:         c.Config.TCPBuffer.Value(mtglib.DefaultBufferSize),
51
-		DomainFrontingPort: c.Config.DomainFrontingPort.Value(mtglib.DefaultDomainFrontingPort),
52
-		IdleTimeout:        c.Config.Network.Timeout.Idle.Value(mtglib.DefaultIdleTimeout),
53
-		PreferIP:           c.Config.PreferIP.Value(mtglib.DefaultPreferIP),
54
-	}
55
-
56
-	opts.Logger.BindStr("configuration", c.Config.String()).Debug("configuration")
57
-
58
-	c.setupAntiReplayCache(&opts)
59
-
60
-	if err := c.setupIPBlocklist(&opts); err != nil {
61
-		return fmt.Errorf("cannot setup ipblocklist: %w", err)
62
-	}
63
-
64
-	if err := c.setupEventStream(&opts); err != nil {
65
-		return fmt.Errorf("cannot setup event stream: %w", err)
66
-	}
67
-
68
-	proxy, err := mtglib.NewProxy(opts)
69
-	if err != nil {
70
-		return fmt.Errorf("cannot create a proxy: %w", err)
71
-	}
72
-
73
-	listener, err := net.Listen("tcp", c.Config.BindTo.String())
74
-	if err != nil {
75
-		return fmt.Errorf("cannot start proxy: %w", err)
76
-	}
77
-
78
-	go proxy.Serve(listener) // nolint: errcheck
79
-
80
-	<-ctx.Done()
81
-	listener.Close()
82
-	proxy.Shutdown()
83
-
84
-	return nil
85
-}
86
-
87
-func (c *Proxy) setupAntiReplayCache(opts *mtglib.ProxyOpts) {
88
-	if !c.Config.Defense.AntiReplay.Enabled {
89
-		return
90
-	}
91
-
92
-	opts.AntiReplayCache = antireplay.NewStableBloomFilter(
93
-		c.Config.Defense.AntiReplay.MaxSize.Value(antireplay.DefaultStableBloomFilterMaxSize),
94
-		c.Config.Defense.AntiReplay.ErrorRate.Value(antireplay.DefaultStableBloomFilterErrorRate),
95
-	)
96
-}
97
-
98
-func (c *Proxy) setupIPBlocklist(opts *mtglib.ProxyOpts) error {
99
-	if !c.Config.Defense.Blocklist.Enabled {
100
-		return nil
101
-	}
102
-
103
-	remoteURLs := []string{}
104
-	localFiles := []string{}
105
-
106
-	for _, v := range c.Config.Defense.Blocklist.URLs {
107
-		if v.IsRemote() {
108
-			remoteURLs = append(remoteURLs, v.String())
109
-		} else {
110
-			localFiles = append(localFiles, v.String())
111
-		}
112
-	}
113
-
114
-	firehol, err := ipblocklist.NewFirehol(opts.Logger.Named("ipblockist"),
115
-		c.Network,
116
-		c.Config.Defense.Blocklist.DownloadConcurrency,
117
-		remoteURLs,
118
-		localFiles)
119
-	if err != nil {
120
-		return err // nolint: wrapcheck
121
-	}
122
-
123
-	go firehol.Run(c.Config.Defense.Blocklist.UpdateEach.Value(ipblocklist.DefaultFireholUpdateEach))
124
-
125
-	opts.IPBlocklist = firehol
126
-
127
-	return nil
128
-}
129
-
130
-func (c *Proxy) setupEventStream(opts *mtglib.ProxyOpts) error {
131
-	factories := make([]events.ObserverFactory, 0, 2)
132
-
133
-	if c.Config.Stats.StatsD.Enabled {
134
-		statsdFactory, err := stats.NewStatsd(
135
-			c.Config.Stats.StatsD.Address.String(),
136
-			opts.Logger.Named("statsd"),
137
-			c.Config.Stats.StatsD.MetricPrefix.Value(stats.DefaultStatsdMetricPrefix),
138
-			c.Config.Stats.StatsD.TagFormat.Value(stats.DefaultStatsdTagFormat))
139
-		if err != nil {
140
-			return fmt.Errorf("cannot build statsd observer: %w", err)
141
-		}
142
-
143
-		factories = append(factories, statsdFactory.Make)
144
-	}
145
-
146
-	if c.Config.Stats.Prometheus.Enabled {
147
-		prometheus := stats.NewPrometheus(
148
-			c.Config.Stats.Prometheus.MetricPrefix.Value(stats.DefaultMetricPrefix),
149
-			c.Config.Stats.Prometheus.HTTPPath.Value("/"),
150
-		)
151
-
152
-		listener, err := net.Listen("tcp", c.Config.Stats.Prometheus.BindTo.String())
153
-		if err != nil {
154
-			return fmt.Errorf("cannot start a listener for prometheus: %w", err)
155
-		}
156
-
157
-		go prometheus.Serve(listener) // nolint: errcheck
158
-
159
-		factories = append(factories, prometheus.Make)
160
-	}
161
-
162
-	if len(factories) > 0 {
163
-		opts.EventStream = events.NewEventStream(factories)
164
-	}
165
-
166
-	return nil
167
-}

+ 20
- 0
internal/cli/run.go Zobrazit soubor

@@ -0,0 +1,20 @@
1
+package cli
2
+
3
+import (
4
+	"fmt"
5
+
6
+	"github.com/9seconds/mtg/v2/internal/utils"
7
+)
8
+
9
+type Run struct {
10
+	ConfigPath string `kong:"arg,required,type='existingfile',help='Path to the configuration file.',name='config-path'"` // nolint: lll
11
+}
12
+
13
+func (r *Run) Run(cli *CLI, version string) error {
14
+	conf, err := utils.ReadConfig(r.ConfigPath)
15
+	if err != nil {
16
+		return fmt.Errorf("cannot init config: %w", err)
17
+	}
18
+
19
+	return runProxy(conf, version)
20
+}

+ 209
- 0
internal/cli/run_proxy.go Zobrazit soubor

@@ -0,0 +1,209 @@
1
+package cli
2
+
3
+import (
4
+	"fmt"
5
+	"net"
6
+	"net/url"
7
+	"os"
8
+
9
+	"github.com/9seconds/mtg/v2/antireplay"
10
+	"github.com/9seconds/mtg/v2/events"
11
+	"github.com/9seconds/mtg/v2/internal/config"
12
+	"github.com/9seconds/mtg/v2/internal/utils"
13
+	"github.com/9seconds/mtg/v2/ipblocklist"
14
+	"github.com/9seconds/mtg/v2/logger"
15
+	"github.com/9seconds/mtg/v2/mtglib"
16
+	"github.com/9seconds/mtg/v2/network"
17
+	"github.com/9seconds/mtg/v2/stats"
18
+	"github.com/rs/zerolog"
19
+)
20
+
21
+func makeLogger(conf *config.Config) mtglib.Logger {
22
+	zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMs
23
+	zerolog.TimestampFieldName = "timestamp"
24
+	zerolog.LevelFieldName = "level"
25
+
26
+	if conf.Debug.Get(false) {
27
+		zerolog.SetGlobalLevel(zerolog.DebugLevel)
28
+	} else {
29
+		zerolog.SetGlobalLevel(zerolog.WarnLevel)
30
+	}
31
+
32
+	baseLogger := zerolog.New(os.Stdout).With().Timestamp().Logger()
33
+
34
+	return logger.NewZeroLogger(baseLogger)
35
+}
36
+
37
+func makeNetwork(conf *config.Config, version string) (mtglib.Network, error) {
38
+	tcpTimeout := conf.Network.Timeout.TCP.Get(network.DefaultTimeout)
39
+	httpTimeout := conf.Network.Timeout.HTTP.Get(network.DefaultHTTPTimeout)
40
+	dohIP := conf.Network.DOHIP.Get(net.ParseIP(network.DefaultDOHHostname)).String()
41
+	bufferSize := conf.TCPBuffer.Get(network.DefaultBufferSize)
42
+	userAgent := "mtg/" + version
43
+
44
+	baseDialer, err := network.NewDefaultDialer(tcpTimeout, int(bufferSize))
45
+	if err != nil {
46
+		return nil, fmt.Errorf("cannot build a default dialer: %w", err)
47
+	}
48
+
49
+	if len(conf.Network.Proxies) == 0 {
50
+		return network.NewNetwork(baseDialer, userAgent, dohIP, httpTimeout) // nolint: wrapcheck
51
+	}
52
+
53
+	proxyURLs := make([]*url.URL, 0, len(conf.Network.Proxies))
54
+
55
+	for _, v := range conf.Network.Proxies {
56
+		if value := v.Get(nil); value != nil {
57
+			proxyURLs = append(proxyURLs, value)
58
+		}
59
+	}
60
+
61
+	if len(proxyURLs) == 1 {
62
+		socksDialer, err := network.NewSocks5Dialer(baseDialer, proxyURLs[0])
63
+		if err != nil {
64
+			return nil, fmt.Errorf("cannot build socks5 dialer: %w", err)
65
+		}
66
+
67
+		return network.NewNetwork(socksDialer, userAgent, dohIP, httpTimeout) // nolint: wrapcheck
68
+	}
69
+
70
+	socksDialer, err := network.NewLoadBalancedSocks5Dialer(baseDialer, proxyURLs)
71
+	if err != nil {
72
+		return nil, fmt.Errorf("cannot build socks5 dialer: %w", err)
73
+	}
74
+
75
+	return network.NewNetwork(socksDialer, userAgent, dohIP, httpTimeout) // nolint: wrapcheck
76
+}
77
+
78
+func makeAntiReplayCache(conf *config.Config) mtglib.AntiReplayCache {
79
+	if !conf.Defense.AntiReplay.Enabled.Get(false) {
80
+		return antireplay.NewNoop()
81
+	}
82
+
83
+	return antireplay.NewStableBloomFilter(
84
+		conf.Defense.AntiReplay.MaxSize.Get(antireplay.DefaultStableBloomFilterMaxSize),
85
+		conf.Defense.AntiReplay.ErrorRate.Get(antireplay.DefaultStableBloomFilterErrorRate),
86
+	)
87
+}
88
+
89
+func makeIPBlocklist(conf *config.Config, logger mtglib.Logger, ntw mtglib.Network) (mtglib.IPBlocklist, error) {
90
+	if !conf.Defense.Blocklist.Enabled.Get(false) {
91
+		return ipblocklist.NewNoop(), nil
92
+	}
93
+
94
+	remoteURLs := []string{}
95
+	localFiles := []string{}
96
+
97
+	for _, v := range conf.Defense.Blocklist.URLs {
98
+		if v.IsRemote() {
99
+			remoteURLs = append(remoteURLs, v.String())
100
+		} else {
101
+			localFiles = append(localFiles, v.String())
102
+		}
103
+	}
104
+
105
+	firehol, err := ipblocklist.NewFirehol(logger.Named("ipblockist"),
106
+		ntw,
107
+		conf.Defense.Blocklist.DownloadConcurrency.Get(1),
108
+		remoteURLs,
109
+		localFiles)
110
+	if err != nil {
111
+		return nil, fmt.Errorf("incorrect parameters for firehol: %w", err)
112
+	}
113
+
114
+	return firehol, nil
115
+}
116
+
117
+func makeEventStream(conf *config.Config, logger mtglib.Logger) (mtglib.EventStream, error) {
118
+	factories := make([]events.ObserverFactory, 0, 2)
119
+
120
+	if conf.Stats.StatsD.Enabled.Get(false) {
121
+		statsdFactory, err := stats.NewStatsd(
122
+			conf.Stats.StatsD.Address.Get(""),
123
+			logger.Named("statsd"),
124
+			conf.Stats.StatsD.MetricPrefix.Get(stats.DefaultStatsdMetricPrefix),
125
+			conf.Stats.StatsD.TagFormat.Get(stats.DefaultStatsdTagFormat))
126
+		if err != nil {
127
+			return nil, fmt.Errorf("cannot build statsd observer: %w", err)
128
+		}
129
+
130
+		factories = append(factories, statsdFactory.Make)
131
+	}
132
+
133
+	if conf.Stats.Prometheus.Enabled.Get(false) {
134
+		prometheus := stats.NewPrometheus(
135
+			conf.Stats.Prometheus.MetricPrefix.Get(stats.DefaultMetricPrefix),
136
+			conf.Stats.Prometheus.HTTPPath.Get("/"),
137
+		)
138
+
139
+		listener, err := net.Listen("tcp", conf.Stats.Prometheus.BindTo.Get(""))
140
+		if err != nil {
141
+			return nil, fmt.Errorf("cannot start a listener for prometheus: %w", err)
142
+		}
143
+
144
+		go prometheus.Serve(listener) // nolint: errcheck
145
+
146
+		factories = append(factories, prometheus.Make)
147
+	}
148
+
149
+	if len(factories) > 0 {
150
+		return events.NewEventStream(factories), nil
151
+	}
152
+
153
+	return events.NewNoopStream(), nil
154
+}
155
+
156
+func runProxy(conf *config.Config, version string) error {
157
+	logger := makeLogger(conf)
158
+
159
+	logger.BindStr("configuration", conf.String()).Debug("configuration")
160
+
161
+	ntw, err := makeNetwork(conf, version)
162
+	if err != nil {
163
+		return fmt.Errorf("cannot build network: %w", err)
164
+	}
165
+
166
+	blocklist, err := makeIPBlocklist(conf, logger, ntw)
167
+	if err != nil {
168
+		return fmt.Errorf("cannot build ip blocklist: %w", err)
169
+	}
170
+
171
+	eventStream, err := makeEventStream(conf, logger)
172
+	if err != nil {
173
+		return fmt.Errorf("cannot build event stream: %w", err)
174
+	}
175
+
176
+	opts := mtglib.ProxyOpts{
177
+		Logger:          logger,
178
+		Network:         ntw,
179
+		AntiReplayCache: makeAntiReplayCache(conf),
180
+		IPBlocklist:     blocklist,
181
+		EventStream:     eventStream,
182
+
183
+		Secret:             conf.Secret,
184
+		BufferSize:         conf.TCPBuffer.Get(mtglib.DefaultBufferSize),
185
+		DomainFrontingPort: conf.DomainFrontingPort.Get(mtglib.DefaultDomainFrontingPort),
186
+		IdleTimeout:        conf.Network.Timeout.Idle.Get(mtglib.DefaultIdleTimeout),
187
+		PreferIP:           conf.PreferIP.Get(mtglib.DefaultPreferIP),
188
+	}
189
+
190
+	proxy, err := mtglib.NewProxy(opts)
191
+	if err != nil {
192
+		return fmt.Errorf("cannot create a proxy: %w", err)
193
+	}
194
+
195
+	listener, err := net.Listen("tcp", conf.BindTo.Get(""))
196
+	if err != nil {
197
+		return fmt.Errorf("cannot start proxy: %w", err)
198
+	}
199
+
200
+	ctx := utils.RootContext()
201
+
202
+	go proxy.Serve(listener) // nolint: errcheck
203
+
204
+	<-ctx.Done()
205
+	listener.Close()
206
+	proxy.Shutdown()
207
+
208
+	return nil
209
+}

+ 84
- 0
internal/cli/simple_run.go Zobrazit soubor

@@ -0,0 +1,84 @@
1
+package cli
2
+
3
+import (
4
+	"fmt"
5
+	"net"
6
+	"strconv"
7
+	"time"
8
+
9
+	"github.com/9seconds/mtg/v2/internal/config"
10
+)
11
+
12
+type SimpleRun struct {
13
+	BindTo string `kong:"arg,required,name='bind-to',help='A host:port to bind proxy to.'"`
14
+	Secret string `kong:"arg,required,name='secret',help='Proxy secret.'"`
15
+
16
+	Debug               bool          `kong:"name='debug',short='d',help='Run in debug mode.'"`                                                                        // nolint: lll
17
+	Concurrency         uint64        `kong:"name='concurrency',short='c',default='8192',help='Max number of concurrent connection to proxy.'"`                        // nolint: lll
18
+	TCPBuffer           string        `kong:"name='tcp-buffer',short='b',default='4KB',help='Size of TCP buffer to use.'"`                                             // nolint: lll
19
+	PreferIP            string        `kong:"name='prefer-ip',short='i',default='prefer-ipv6',help='IP preference. By default we prefer IPv6 with fallback to IPv4.'"` // nolint: lll
20
+	DomainFrontingPort  uint64        `kong:"name='domain-fronting-port',short='p',default='443',help='A port to access for domain fronting.'"`                        // nolint: lll
21
+	DOHIP               net.IP        `kong:"name='doh-ip',short='n',default='9.9.9.9',help='IP address of DNS-over-HTTP to use.'"`                                    // nolint: lll
22
+	Timeout             time.Duration `kong:"name='timeout',short='t',default='10s',help='Network timeout to use'"`                                                    // nolint: lll
23
+	AntiReplayCacheSize string        `kong:"name='antireplay-cache-size',short='a',default='1MB',help='A size of anti-replay cache to use.'"`                         // nolint: lll
24
+}
25
+
26
+func (s *SimpleRun) Run(cli *CLI, version string) error { // nolint: cyclop
27
+	conf := &config.Config{}
28
+
29
+	if err := conf.BindTo.Set(s.BindTo); err != nil {
30
+		return fmt.Errorf("incorrect bind-to parameter: %w", err)
31
+	}
32
+
33
+	if err := conf.Secret.Set(s.Secret); err != nil {
34
+		return fmt.Errorf("incorrect secret: %w", err)
35
+	}
36
+
37
+	if err := conf.Concurrency.Set(strconv.FormatUint(s.Concurrency, 10)); err != nil {
38
+		return fmt.Errorf("incorrect concurrency: %w", err)
39
+	}
40
+
41
+	if err := conf.TCPBuffer.Set(s.TCPBuffer); err != nil {
42
+		return fmt.Errorf("incorrect tcp-buffer: %w", err)
43
+	}
44
+
45
+	if err := conf.PreferIP.Set(s.PreferIP); err != nil {
46
+		return fmt.Errorf("incorrect prefer-ip: %w", err)
47
+	}
48
+
49
+	if err := conf.DomainFrontingPort.Set(strconv.FormatUint(s.DomainFrontingPort, 10)); err != nil {
50
+		return fmt.Errorf("incorrect domain-fronting-port: %w", err)
51
+	}
52
+
53
+	if err := conf.Network.DOHIP.Set(s.DOHIP.String()); err != nil {
54
+		return fmt.Errorf("incorrect doh-ip: %w", err)
55
+	}
56
+
57
+	if err := conf.Network.Timeout.TCP.Set(s.Timeout.String()); err != nil {
58
+		return fmt.Errorf("incorrect timeout: %w", err)
59
+	}
60
+
61
+	if err := conf.Network.Timeout.HTTP.Set(s.Timeout.String()); err != nil {
62
+		return fmt.Errorf("incorrect timeout: %w", err)
63
+	}
64
+
65
+	if err := conf.Network.Timeout.Idle.Set(s.Timeout.String()); err != nil {
66
+		return fmt.Errorf("incorrect timeout: %w", err)
67
+	}
68
+
69
+	if err := conf.Defense.AntiReplay.MaxSize.Set(s.AntiReplayCacheSize); err != nil {
70
+		return fmt.Errorf("incorrect antireplay-cache-size: %w", err)
71
+	}
72
+
73
+	conf.Debug.Value = s.Debug
74
+	conf.Defense.AntiReplay.Enabled.Value = true
75
+	conf.Defense.Blocklist.Enabled.Value = false
76
+	conf.Stats.StatsD.Enabled.Value = false
77
+	conf.Stats.Prometheus.Enabled.Value = false
78
+
79
+	if err := conf.Validate(); err != nil {
80
+		return fmt.Errorf("invalid result configuration: %w", err)
81
+	}
82
+
83
+	return runProxy(conf, version)
84
+}

+ 16
- 92
internal/config/config.go Zobrazit soubor

@@ -6,27 +6,26 @@ import (
6 6
 	"fmt"
7 7
 
8 8
 	"github.com/9seconds/mtg/v2/mtglib"
9
-	"github.com/pelletier/go-toml"
10 9
 )
11 10
 
12 11
 type Config struct {
13
-	Debug                bool          `json:"debug"`
14
-	Secret               mtglib.Secret `json:"secret"`
15
-	BindTo               TypeHostPort  `json:"bindTo"`
16
-	TCPBuffer            TypeBytes     `json:"tcpBuffer"`
17
-	PreferIP             TypePreferIP  `json:"preferIp"`
18
-	DomainFrontingPort   TypePort      `json:"domainFrontingPort"`
19
-	TolerateTimeSkewness TypeDuration  `json:"tolerateTimeSkewness"`
20
-	Concurrency          uint          `json:"concurrency"`
12
+	Debug                TypeBool        `json:"debug"`
13
+	Secret               mtglib.Secret   `json:"secret"`
14
+	BindTo               TypeHostPort    `json:"bindTo"`
15
+	TCPBuffer            TypeBytes       `json:"tcpBuffer"`
16
+	PreferIP             TypePreferIP    `json:"preferIp"`
17
+	DomainFrontingPort   TypePort        `json:"domainFrontingPort"`
18
+	TolerateTimeSkewness TypeDuration    `json:"tolerateTimeSkewness"`
19
+	Concurrency          TypeConcurrency `json:"concurrency"`
21 20
 	Defense              struct {
22 21
 		AntiReplay struct {
23
-			Enabled   bool          `json:"enabled"`
22
+			Enabled   TypeBool      `json:"enabled"`
24 23
 			MaxSize   TypeBytes     `json:"maxSize"`
25 24
 			ErrorRate TypeErrorRate `json:"errorRate"`
26 25
 		} `json:"antiReplay"`
27 26
 		Blocklist struct {
28
-			Enabled             bool               `json:"enabled"`
29
-			DownloadConcurrency uint               `json:"downloadConcurrency"`
27
+			Enabled             TypeBool           `json:"enabled"`
28
+			DownloadConcurrency TypeConcurrency    `json:"downloadConcurrency"`
30 29
 			URLs                []TypeBlocklistURI `json:"urls"`
31 30
 			UpdateEach          TypeDuration       `json:"updateEach"`
32 31
 		} `json:"blocklist"`
@@ -37,18 +36,18 @@ type Config struct {
37 36
 			HTTP TypeDuration `json:"http"`
38 37
 			Idle TypeDuration `json:"idle"`
39 38
 		} `json:"timeout"`
40
-		DOHIP   TypeIP    `json:"dohIp"`
41
-		Proxies []TypeURL `json:"proxies"`
39
+		DOHIP   TypeIP         `json:"dohIp"`
40
+		Proxies []TypeProxyURL `json:"proxies"`
42 41
 	} `json:"network"`
43 42
 	Stats struct {
44 43
 		StatsD struct {
45
-			Enabled      bool                `json:"enabled"`
44
+			Enabled      TypeBool            `json:"enabled"`
46 45
 			Address      TypeHostPort        `json:"address"`
47 46
 			MetricPrefix TypeMetricPrefix    `json:"metricPrefix"`
48 47
 			TagFormat    TypeStatsdTagFormat `json:"tagFormat"`
49 48
 		} `json:"statsd"`
50 49
 		Prometheus struct {
51
-			Enabled      bool             `json:"enabled"`
50
+			Enabled      TypeBool         `json:"enabled"`
52 51
 			BindTo       TypeHostPort     `json:"bindTo"`
53 52
 			HTTPPath     TypeHTTPPath     `json:"httpPath"`
54 53
 			MetricPrefix TypeMetricPrefix `json:"metricPrefix"`
@@ -61,7 +60,7 @@ func (c *Config) Validate() error {
61 60
 		return fmt.Errorf("invalid secret %s", c.Secret.String())
62 61
 	}
63 62
 
64
-	if len(c.BindTo.HostValue(nil)) == 0 || c.BindTo.PortValue(0) == 0 {
63
+	if c.BindTo.Get("") == "" {
65 64
 		return fmt.Errorf("incorrect bind-to parameter %s", c.BindTo.String())
66 65
 	}
67 66
 
@@ -80,78 +79,3 @@ func (c *Config) String() string {
80 79
 
81 80
 	return buf.String()
82 81
 }
83
-
84
-type configRaw struct {
85
-	Debug                bool   `toml:"debug" json:"debug,omitempty"`
86
-	Secret               string `toml:"secret" json:"secret"`
87
-	BindTo               string `toml:"bind-to" json:"bindTo"`
88
-	TCPBuffer            string `toml:"tcp-buffer" json:"tcpBuffer,omitempty"`
89
-	PreferIP             string `toml:"prefer-ip" json:"preferIp,omitempty"`
90
-	DomainFrontingPort   uint   `toml:"domain-fronting-port" json:"domainFrontingPort,omitempty"`
91
-	TolerateTimeSkewness string `toml:"tolerate-time-skewness" json:"tolerateTimeSkewness,omitempty"`
92
-	Concurrency          uint   `toml:"concurrency" json:"concurrency,omitempty"`
93
-	Defense              struct {
94
-		AntiReplay struct {
95
-			Enabled   bool    `toml:"enabled" json:"enabled,omitempty"`
96
-			MaxSize   string  `toml:"max-size" json:"maxSize,omitempty"`
97
-			ErrorRate float64 `toml:"error-rate" json:"errorRate,omitempty"`
98
-		} `toml:"anti-replay" json:"antiReplay,omitempty"`
99
-		Blocklist struct {
100
-			Enabled             bool     `toml:"enabled" json:"enabled,omitempty"`
101
-			DownloadConcurrency uint     `toml:"download-concurrency" json:"downloadConcurrency,omitempty"`
102
-			URLs                []string `toml:"urls" json:"urls,omitempty"`
103
-			UpdateEach          string   `toml:"update-each" json:"updateEach,omitempty"`
104
-		} `toml:"blocklist" json:"blocklist,omitempty"`
105
-	} `toml:"defense" json:"defense,omitempty"`
106
-	Network struct {
107
-		Timeout struct {
108
-			TCP  string `toml:"tcp" json:"tcp,omitempty"`
109
-			HTTP string `toml:"http" json:"http,omitempty"`
110
-			Idle string `toml:"idle" json:"idle,omitempty"`
111
-		} `toml:"timeout" json:"timeout,omitempty"`
112
-		DOHIP   string   `toml:"doh-ip" json:"dohIp,omitempty"`
113
-		Proxies []string `toml:"proxies" json:"proxies,omitempty"`
114
-	} `toml:"network" json:"network,omitempty"`
115
-	Stats struct {
116
-		StatsD struct {
117
-			Enabled      bool   `toml:"enabled" json:"enabled,omitempty"`
118
-			Address      string `toml:"address" json:"address,omitempty"`
119
-			MetricPrefix string `toml:"metric-prefix" json:"metricPrefix,omitempty"`
120
-			TagFormat    string `toml:"tag-format" json:"tagFormat,omitempty"`
121
-		} `toml:"statsd" json:"statsd,omitempty"`
122
-		Prometheus struct {
123
-			Enabled      bool   `toml:"enabled" json:"enabled,omitempty"`
124
-			BindTo       string `toml:"bind-to" json:"bindTo,omitempty"`
125
-			HTTPPath     string `toml:"http-path" json:"httpPath,omitempty"`
126
-			MetricPrefix string `toml:"metric-prefix" json:"metricPrefix,omitempty"`
127
-		} `toml:"prometheus" json:"prometheus,omitempty"`
128
-	} `toml:"stats" json:"stats,omitempty"`
129
-}
130
-
131
-func Parse(rawData []byte) (*Config, error) {
132
-	rawConf := &configRaw{}
133
-	jsonBuf := &bytes.Buffer{}
134
-	conf := &Config{}
135
-
136
-	jsonEncoder := json.NewEncoder(jsonBuf)
137
-	jsonEncoder.SetEscapeHTML(false)
138
-	jsonEncoder.SetIndent("", "")
139
-
140
-	if err := toml.Unmarshal(rawData, rawConf); err != nil {
141
-		return nil, fmt.Errorf("cannot parse toml config: %w", err)
142
-	}
143
-
144
-	if err := jsonEncoder.Encode(rawConf); err != nil {
145
-		panic(err)
146
-	}
147
-
148
-	if err := json.NewDecoder(jsonBuf).Decode(conf); err != nil {
149
-		return nil, fmt.Errorf("cannot parse a config: %w", err)
150
-	}
151
-
152
-	if err := conf.Validate(); err != nil {
153
-		return nil, fmt.Errorf("cannot validate config: %w", err)
154
-	}
155
-
156
-	return conf, nil
157
-}

+ 80
- 0
internal/config/parse.go Zobrazit soubor

@@ -0,0 +1,80 @@
1
+package config
2
+
3
+import (
4
+	"bytes"
5
+	"encoding/json"
6
+	"fmt"
7
+
8
+	"github.com/pelletier/go-toml"
9
+)
10
+
11
+type tomlConfig struct {
12
+	Debug                bool   `toml:"debug" json:"debug,omitempty"`
13
+	Secret               string `toml:"secret" json:"secret"`
14
+	BindTo               string `toml:"bind-to" json:"bindTo"`
15
+	TCPBuffer            string `toml:"tcp-buffer" json:"tcpBuffer,omitempty"`
16
+	PreferIP             string `toml:"prefer-ip" json:"preferIp,omitempty"`
17
+	DomainFrontingPort   uint   `toml:"domain-fronting-port" json:"domainFrontingPort,omitempty"`
18
+	TolerateTimeSkewness string `toml:"tolerate-time-skewness" json:"tolerateTimeSkewness,omitempty"`
19
+	Concurrency          uint   `toml:"concurrency" json:"concurrency,omitempty"`
20
+	Defense              struct {
21
+		AntiReplay struct {
22
+			Enabled   bool    `toml:"enabled" json:"enabled,omitempty"`
23
+			MaxSize   string  `toml:"max-size" json:"maxSize,omitempty"`
24
+			ErrorRate float64 `toml:"error-rate" json:"errorRate,omitempty"`
25
+		} `toml:"anti-replay" json:"antiReplay,omitempty"`
26
+		Blocklist struct {
27
+			Enabled             bool     `toml:"enabled" json:"enabled,omitempty"`
28
+			DownloadConcurrency uint     `toml:"download-concurrency" json:"downloadConcurrency,omitempty"`
29
+			URLs                []string `toml:"urls" json:"urls,omitempty"`
30
+			UpdateEach          string   `toml:"update-each" json:"updateEach,omitempty"`
31
+		} `toml:"blocklist" json:"blocklist,omitempty"`
32
+	} `toml:"defense" json:"defense,omitempty"`
33
+	Network struct {
34
+		Timeout struct {
35
+			TCP  string `toml:"tcp" json:"tcp,omitempty"`
36
+			HTTP string `toml:"http" json:"http,omitempty"`
37
+			Idle string `toml:"idle" json:"idle,omitempty"`
38
+		} `toml:"timeout" json:"timeout,omitempty"`
39
+		DOHIP   string   `toml:"doh-ip" json:"dohIp,omitempty"`
40
+		Proxies []string `toml:"proxies" json:"proxies,omitempty"`
41
+	} `toml:"network" json:"network,omitempty"`
42
+	Stats struct {
43
+		StatsD struct {
44
+			Enabled      bool   `toml:"enabled" json:"enabled,omitempty"`
45
+			Address      string `toml:"address" json:"address,omitempty"`
46
+			MetricPrefix string `toml:"metric-prefix" json:"metricPrefix,omitempty"`
47
+			TagFormat    string `toml:"tag-format" json:"tagFormat,omitempty"`
48
+		} `toml:"statsd" json:"statsd,omitempty"`
49
+		Prometheus struct {
50
+			Enabled      bool   `toml:"enabled" json:"enabled,omitempty"`
51
+			BindTo       string `toml:"bind-to" json:"bindTo,omitempty"`
52
+			HTTPPath     string `toml:"http-path" json:"httpPath,omitempty"`
53
+			MetricPrefix string `toml:"metric-prefix" json:"metricPrefix,omitempty"`
54
+		} `toml:"prometheus" json:"prometheus,omitempty"`
55
+	} `toml:"stats" json:"stats,omitempty"`
56
+}
57
+
58
+func Parse(rawData []byte) (*Config, error) {
59
+	tomlConf := &tomlConfig{}
60
+	jsonBuf := &bytes.Buffer{}
61
+	conf := &Config{}
62
+
63
+	jsonEncoder := json.NewEncoder(jsonBuf)
64
+	jsonEncoder.SetEscapeHTML(false)
65
+	jsonEncoder.SetIndent("", "")
66
+
67
+	if err := toml.Unmarshal(rawData, tomlConf); err != nil {
68
+		return nil, fmt.Errorf("cannot parse toml config: %w", err)
69
+	}
70
+
71
+	if err := jsonEncoder.Encode(tomlConf); err != nil {
72
+		panic(err)
73
+	}
74
+
75
+	if err := json.NewDecoder(jsonBuf).Decode(conf); err != nil {
76
+		return nil, fmt.Errorf("cannot parse a config: %w", err)
77
+	}
78
+
79
+	return conf, nil
80
+}

+ 36
- 27
internal/config/type_blocklist_uri.go Zobrazit soubor

@@ -8,61 +8,70 @@ import (
8 8
 )
9 9
 
10 10
 type TypeBlocklistURI struct {
11
-	value string
11
+	Value string
12 12
 }
13 13
 
14
-func (c *TypeBlocklistURI) UnmarshalText(data []byte) error {
15
-	if len(data) == 0 {
16
-		return nil
17
-	}
14
+func (t *TypeBlocklistURI) Set(value string) error {
15
+	if stat, err := os.Stat(value); err == nil || os.IsExist(err) {
16
+		switch {
17
+		case stat.IsDir():
18
+			return fmt.Errorf("value is correct filepath but directory")
19
+		case stat.Mode().Perm()&0o400 == 0:
20
+			return fmt.Errorf("value is correct filepath but not readable")
21
+		}
18 22
 
19
-	text := string(data)
20
-	if filepath.IsAbs(text) {
21
-		if _, err := os.Stat(text); os.IsNotExist(err) {
22
-			return fmt.Errorf("filepath %s does not exist", text)
23
+		value, err = filepath.Abs(value)
24
+		if err != nil {
25
+			return fmt.Errorf(
26
+				"value is correct filepath but cannot resolve absolute (%s): %w",
27
+				value, err)
23 28
 		}
24 29
 
25
-		c.value = text
30
+		t.Value = value
26 31
 
27 32
 		return nil
28 33
 	}
29 34
 
30
-	parsedURL, err := url.Parse(text)
35
+	parsedURL, err := url.Parse(value)
31 36
 	if err != nil {
32
-		return fmt.Errorf("incorrect url: %w", err)
37
+		return fmt.Errorf("incorrect url (%s): %w", value, err)
33 38
 	}
34 39
 
35 40
 	switch parsedURL.Scheme {
36
-	case "http", "https": // nolint: goconst
41
+	case "http", "https":
37 42
 	default:
38
-		return fmt.Errorf("unknown schema %s", parsedURL.Scheme)
43
+		return fmt.Errorf("unknown schema %s (%s)", parsedURL.Scheme, value)
39 44
 	}
40 45
 
41 46
 	if parsedURL.Host == "" {
42
-		return fmt.Errorf("incorrect url %s", text)
47
+		return fmt.Errorf("incorrect url %s", value)
43 48
 	}
44 49
 
45
-	c.value = parsedURL.String()
50
+	t.Value = parsedURL.String()
46 51
 
47 52
 	return nil
48 53
 }
49 54
 
50
-func (c TypeBlocklistURI) MarshalText() ([]byte, error) {
51
-	return []byte(c.value), nil
55
+func (t TypeBlocklistURI) Get(defaultValue string) string {
56
+	if t.Value == "" {
57
+		return defaultValue
58
+	}
59
+
60
+	return t.Value
52 61
 }
53 62
 
54
-func (c TypeBlocklistURI) String() string {
55
-	return c.value
63
+func (t TypeBlocklistURI) IsRemote() bool {
64
+	return !filepath.IsAbs(t.Value)
56 65
 }
57 66
 
58
-func (c TypeBlocklistURI) IsRemote() bool {
59
-	return !filepath.IsAbs(c.value)
67
+func (t *TypeBlocklistURI) UnmarshalText(data []byte) error {
68
+	return t.Set(string(data))
60 69
 }
61 70
 
62
-func (c TypeBlocklistURI) Value(defaultValue string) string {
63
-	if c.value == "" {
64
-		return defaultValue
65
-	}
71
+func (t TypeBlocklistURI) MarshalText() ([]byte, error) {
72
+	return []byte(t.String()), nil
73
+}
66 74
 
67
-	return c.value
75
+func (t TypeBlocklistURI) String() string {
76
+	return t.Value
68 77
 }

+ 37
- 101
internal/config/type_blocklist_uri_test.go Zobrazit soubor

@@ -1,12 +1,10 @@
1 1
 package config_test
2 2
 
3 3
 import (
4
-	"crypto/rand"
5
-	"encoding/base64"
6 4
 	"encoding/json"
7 5
 	"os"
8 6
 	"path/filepath"
9
-	"strconv"
7
+	"strings"
10 8
 	"testing"
11 9
 
12 10
 	"github.com/9seconds/mtg/v2/internal/config"
@@ -20,42 +18,28 @@ type typeBlocklistURITestStruct struct {
20 18
 
21 19
 type TypeBlocklistURITestSuite struct {
22 20
 	suite.Suite
23
-}
24
-
25
-func (suite *TypeBlocklistURITestSuite) TestUnmarshalNil() {
26
-	typ := &config.TypeBlocklistURI{}
27
-	suite.NoError(typ.UnmarshalText(nil))
28
-	suite.Empty(typ.String())
29
-}
30 21
 
31
-func (suite *TypeBlocklistURITestSuite) TestUnknownSchema() {
32
-	typ := &config.TypeBlocklistURI{}
33
-	suite.Error(typ.UnmarshalText([]byte("gopher://lalala")))
22
+	directory    string
23
+	absDirectory string
34 24
 }
35 25
 
36
-func (suite *TypeBlocklistURITestSuite) TestEmptyHost() {
37
-	typ := &config.TypeBlocklistURI{}
38
-	suite.Error(typ.UnmarshalText([]byte("https:///path")))
39
-}
26
+func (suite *TypeBlocklistURITestSuite) SetupSuite() {
27
+	dir, _ := os.Getwd()
28
+	absDir, _ := filepath.Abs(dir)
40 29
 
41
-func (suite *TypeBlocklistURITestSuite) TestIncorrectURL() {
42
-	typ := &config.TypeBlocklistURI{}
43
-	suite.Error(typ.UnmarshalText([]byte("h:/--")))
30
+	suite.directory = dir
31
+	suite.absDirectory = absDir
44 32
 }
45 33
 
46 34
 func (suite *TypeBlocklistURITestSuite) TestUnmarshalFail() {
47
-	rnd := make([]byte, 48)
48
-
49
-	rand.Read(rnd) // nolint: errcheck
50
-
51
-	unknownPath := base64.StdEncoding.EncodeToString(rnd)
52
-
53 35
 	testData := []string{
54
-		"1",
55
-		unknownPath,
56
-		"/" + unknownPath,
57
-		"http:/",
58
-		"gopher://lalalal",
36
+		"gopher://lalala",
37
+		"https:///paths",
38
+		"h:/=",
39
+		filepath.Join(suite.directory, "___"),
40
+		filepath.Join(suite.absDirectory, "___"),
41
+		suite.directory,
42
+		suite.absDirectory,
59 43
 	}
60 44
 
61 45
 	for _, v := range testData {
@@ -71,13 +55,12 @@ func (suite *TypeBlocklistURITestSuite) TestUnmarshalFail() {
71 55
 }
72 56
 
73 57
 func (suite *TypeBlocklistURITestSuite) TestUnmarshalOk() {
74
-	dir, _ := os.Getwd()
75
-	dir, _ = filepath.Abs(dir)
76
-
77 58
 	testData := []string{
78 59
 		"http://lalala",
79
-		filepath.Join(dir, "config.go"),
80 60
 		"https://lalala",
61
+		"https://lalala/path",
62
+		filepath.Join(suite.directory, "config.go"),
63
+		filepath.Join(suite.absDirectory, "config.go"),
81 64
 	}
82 65
 
83 66
 	for _, v := range testData {
@@ -92,83 +75,36 @@ func (suite *TypeBlocklistURITestSuite) TestUnmarshalOk() {
92 75
 			testStruct := &typeBlocklistURITestStruct{}
93 76
 
94 77
 			assert.NoError(t, json.Unmarshal(data, testStruct))
95
-			assert.EqualValues(t, value, testStruct.Value.Value(""))
78
+			assert.EqualValues(t, value, testStruct.Value.Get(""))
79
+
80
+			if strings.HasPrefix(value, "http") {
81
+				assert.True(t, testStruct.Value.IsRemote())
82
+			} else {
83
+				assert.False(t, testStruct.Value.IsRemote())
84
+			}
96 85
 		})
97 86
 	}
98 87
 }
99 88
 
100 89
 func (suite *TypeBlocklistURITestSuite) TestMarshalOk() {
101
-	dir, _ := os.Getwd()
102
-	dir, _ = filepath.Abs(dir)
103
-
104
-	testData := []string{
105
-		"http://lalalal",
106
-		filepath.Join(dir, "config.go"),
90
+	testStruct := &typeBlocklistURITestStruct{
91
+		Value: config.TypeBlocklistURI{
92
+			Value: "http://some.url/with/path",
93
+		},
107 94
 	}
108 95
 
109
-	for _, v := range testData {
110
-		name := v
111
-
112
-		data, err := json.Marshal(map[string]string{
113
-			"value": name,
114
-		})
115
-		suite.NoError(err)
116
-
117
-		suite.T().Run(name, func(t *testing.T) {
118
-			testStruct := &typeBlocklistURITestStruct{}
119
-
120
-			assert.NoError(t, json.Unmarshal(data, testStruct))
121
-			assert.Equal(t, name, testStruct.Value.String())
122
-
123
-			marshalled, err := testStruct.Value.MarshalText()
124
-			assert.NoError(t, err)
125
-			assert.Equal(t, name, string(marshalled))
126
-		})
127
-	}
128
-}
129
-
130
-func (suite *TypeBlocklistURITestSuite) TestValue() {
131
-	testStruct := &typeBlocklistURITestStruct{}
132
-
133
-	suite.Equal("http://lalala", testStruct.Value.Value("http://lalala"))
134
-
135
-	data, err := json.Marshal(map[string]string{
136
-		"value": "http://blablabla",
137
-	})
96
+	data, err := json.Marshal(testStruct)
138 97
 	suite.NoError(err)
139
-	suite.NoError(json.Unmarshal(data, testStruct))
140
-
141
-	suite.Equal("http://blablabla", testStruct.Value.Value(""))
98
+	suite.JSONEq(`{"value": "http://some.url/with/path"}`, string(data))
142 99
 }
143 100
 
144
-func (suite *TypeBlocklistURITestSuite) TestIsRemote() {
145
-	dir, _ := os.Getwd()
146
-	dir, _ = filepath.Abs(dir)
147
-
148
-	testData := map[bool]string{
149
-		true:  "http://lalalal",
150
-		false: filepath.Join(dir, "config.go"),
151
-	}
152
-
153
-	for k, v := range testData {
154
-		ok := k
155
-
156
-		data, err := json.Marshal(map[string]string{
157
-			"value": v,
158
-		})
159
-		suite.NoError(err)
160
-
161
-		suite.T().Run(strconv.FormatBool(ok), func(t *testing.T) {
162
-			testStruct := &typeBlocklistURITestStruct{}
163
-			assert.NoError(t, json.Unmarshal(data, testStruct))
101
+func (suite *TypeBlocklistURITestSuite) TestGet() {
102
+	value := config.TypeBlocklistURI{}
103
+	suite.Equal("/path", value.Get("/path"))
164 104
 
165
-			if ok {
166
-				assert.True(t, testStruct.Value.IsRemote())
167
-			} else {
168
-				assert.False(t, testStruct.Value.IsRemote())
169
-			}
170
-		})
171
-	}
105
+	suite.NoError(value.Set("http://lalala.ru"))
106
+	suite.Equal("http://lalala.ru", value.Get("/path"))
107
+	suite.Equal("http://lalala.ru", value.Get(""))
172 108
 }
173 109
 
174 110
 func TestTypeBlocklistURI(t *testing.T) {

+ 37
- 0
internal/config/type_bool.go Zobrazit soubor

@@ -0,0 +1,37 @@
1
+package config
2
+
3
+import (
4
+	"fmt"
5
+	"strconv"
6
+)
7
+
8
+type TypeBool struct {
9
+	Value bool
10
+}
11
+
12
+func (t *TypeBool) Set(data string) error {
13
+	parsed, err := strconv.ParseBool(data)
14
+	if err != nil {
15
+		return fmt.Errorf("incorrect bool value: %s", data)
16
+	}
17
+
18
+	t.Value = parsed
19
+
20
+	return nil
21
+}
22
+
23
+func (t TypeBool) Get(defaultValue bool) bool {
24
+	return t.Value || defaultValue
25
+}
26
+
27
+func (t *TypeBool) UnmarshalJSON(data []byte) error {
28
+	return t.Set(string(data))
29
+}
30
+
31
+func (t TypeBool) MarshalJSON() ([]byte, error) {
32
+	return []byte(t.String()), nil
33
+}
34
+
35
+func (t TypeBool) String() string {
36
+	return strconv.FormatBool(t.Value)
37
+}

+ 107
- 0
internal/config/type_bool_test.go Zobrazit soubor

@@ -0,0 +1,107 @@
1
+package config_test
2
+
3
+import (
4
+	"encoding/json"
5
+	"fmt"
6
+	"strconv"
7
+	"testing"
8
+
9
+	"github.com/9seconds/mtg/v2/internal/config"
10
+	"github.com/stretchr/testify/assert"
11
+	"github.com/stretchr/testify/suite"
12
+)
13
+
14
+type typeBoolTestStruct struct {
15
+	Value config.TypeBool `json:"value"`
16
+}
17
+
18
+type TypeBoolTestSuite struct {
19
+	suite.Suite
20
+}
21
+
22
+func (suite *TypeBoolTestSuite) TestUnmarshalFail() {
23
+	testData := []interface{}{
24
+		"",
25
+		"np",
26
+		"нет",
27
+		int(10),
28
+		[]int{},
29
+	}
30
+
31
+	for _, v := range testData {
32
+		data, err := json.Marshal(map[string]interface{}{
33
+			"value": v,
34
+		})
35
+		suite.NoError(err)
36
+
37
+		suite.T().Run(fmt.Sprintf("%v", v), func(t *testing.T) {
38
+			assert.Error(t, json.Unmarshal(data, &typeBoolTestStruct{}))
39
+		})
40
+	}
41
+}
42
+
43
+func (suite *TypeBoolTestSuite) TestUnmarshalOk() {
44
+	testData := []bool{
45
+		true,
46
+		false,
47
+	}
48
+
49
+	for _, v := range testData {
50
+		value := v
51
+
52
+		data, err := json.Marshal(map[string]bool{
53
+			"value": v,
54
+		})
55
+		suite.NoError(err)
56
+
57
+		suite.T().Run(strconv.FormatBool(v), func(t *testing.T) {
58
+			testStruct := &typeBoolTestStruct{}
59
+			assert.NoError(t, json.Unmarshal(data, testStruct))
60
+
61
+			if value {
62
+				assert.True(t, testStruct.Value.Value)
63
+			} else {
64
+				assert.False(t, testStruct.Value.Value)
65
+			}
66
+		})
67
+	}
68
+}
69
+
70
+func (suite *TypeBoolTestSuite) TestMarshalOk() {
71
+	for _, v := range []bool{true, false} {
72
+		value := v
73
+
74
+		suite.T().Run(strconv.FormatBool(v), func(t *testing.T) {
75
+			testStruct := typeBoolTestStruct{
76
+				Value: config.TypeBool{
77
+					Value: value,
78
+				},
79
+			}
80
+
81
+			encodedJSON, err := json.Marshal(testStruct)
82
+			assert.NoError(t, err)
83
+
84
+			expectedJSON, err := json.Marshal(map[string]bool{
85
+				"value": value,
86
+			})
87
+			assert.NoError(t, err)
88
+
89
+			assert.JSONEq(t, string(expectedJSON), string(encodedJSON))
90
+		})
91
+	}
92
+}
93
+
94
+func (suite *TypeBoolTestSuite) TestGet() {
95
+	value := config.TypeBool{}
96
+	suite.False(value.Get(false))
97
+	suite.True(value.Get(true))
98
+
99
+	value.Value = true
100
+	suite.True(value.Get(false))
101
+	suite.True(value.Get(true))
102
+}
103
+
104
+func TestTypeBool(t *testing.T) {
105
+	t.Parallel()
106
+	suite.Run(t, &TypeBoolTestSuite{})
107
+}

+ 25
- 24
internal/config/type_bytes.go Zobrazit soubor

@@ -7,48 +7,49 @@ import (
7 7
 	"github.com/alecthomas/units"
8 8
 )
9 9
 
10
+var typeBytesStringCleaner = strings.NewReplacer(" ", "", "\t", "", "IB", "iB")
11
+
10 12
 type TypeBytes struct {
11
-	value units.Base2Bytes
13
+	Value units.Base2Bytes
12 14
 }
13 15
 
14
-func (c *TypeBytes) UnmarshalText(data []byte) error {
15
-	if len(data) == 0 {
16
-		return nil
17
-	}
18
-
19
-	normalizedData := strings.ToUpper(string(data))
20
-	normalizedData = strings.ReplaceAll(normalizedData, "IB", "iB")
16
+func (t *TypeBytes) Set(value string) error {
17
+	normalizedValue := typeBytesStringCleaner.Replace(strings.ToUpper(value))
21 18
 
22
-	value, err := units.ParseBase2Bytes(normalizedData)
19
+	parsedValue, err := units.ParseBase2Bytes(normalizedValue)
23 20
 	if err != nil {
24
-		return fmt.Errorf("incorrect bytes value: %w", err)
21
+		return fmt.Errorf("incorrect bytes value (%v): %w", value, err)
25 22
 	}
26 23
 
27
-	if value < 0 {
28
-		return fmt.Errorf("%d should be positive number", value)
24
+	if parsedValue < 0 {
25
+		return fmt.Errorf("bytes should be positive (%s)", value)
29 26
 	}
30 27
 
31
-	c.value = value
28
+	t.Value = parsedValue
32 29
 
33 30
 	return nil
34 31
 }
35 32
 
36
-func (c TypeBytes) MarshalText() ([]byte, error) {
37
-	return []byte(c.String()), nil
33
+func (t TypeBytes) Get(defaultValue uint) uint {
34
+	if t.Value == 0 {
35
+		return defaultValue
36
+	}
37
+
38
+	return uint(t.Value)
38 39
 }
39 40
 
40
-func (c TypeBytes) String() string {
41
-	if c.value == 0 {
42
-		return ""
43
-	}
41
+func (t *TypeBytes) UnmarshalText(data []byte) error {
42
+	return t.Set(string(data))
43
+}
44 44
 
45
-	return strings.ToLower(c.value.String())
45
+func (t TypeBytes) MarshalText() ([]byte, error) {
46
+	return []byte(t.String()), nil
46 47
 }
47 48
 
48
-func (c TypeBytes) Value(defaultValue uint) uint {
49
-	if c.value == 0 {
50
-		return defaultValue
49
+func (t TypeBytes) String() string {
50
+	if t.Value == 0 {
51
+		return ""
51 52
 	}
52 53
 
53
-	return uint(c.value)
54
+	return strings.ToLower(t.Value.String())
54 55
 }

+ 11
- 45
internal/config/type_bytes_test.go Zobrazit soubor

@@ -17,19 +17,12 @@ type TypeBytesTestSuite struct {
17 17
 	suite.Suite
18 18
 }
19 19
 
20
-func (suite *TypeBytesTestSuite) TestUnmarshalNil() {
21
-	typ := &config.TypeBytes{}
22
-	suite.NoError(typ.UnmarshalText(nil))
23
-	suite.Empty(typ.String())
24
-}
25
-
26 20
 func (suite *TypeBytesTestSuite) TestUnmarshalFail() {
27 21
 	testData := []string{
28 22
 		"1m",
29 23
 		"1",
30 24
 		"-1kb",
31 25
 		"-1kib",
32
-		"-1QB",
33 26
 	}
34 27
 
35 28
 	for _, v := range testData {
@@ -65,53 +58,26 @@ func (suite *TypeBytesTestSuite) TestUnmarshalOk() {
65 58
 			testStruct := &typeBytesTestStruct{}
66 59
 
67 60
 			assert.NoError(t, json.Unmarshal(data, testStruct))
68
-			assert.EqualValues(t, value, testStruct.Value.Value(0))
61
+			assert.EqualValues(t, value, testStruct.Value.Get(0))
69 62
 		})
70 63
 	}
71 64
 }
72 65
 
73 66
 func (suite *TypeBytesTestSuite) TestMarshalOk() {
74
-	testData := []string{
75
-		"1b",
76
-		"1kib",
77
-		"2mib",
78
-	}
79
-
80
-	for _, v := range testData {
81
-		name := v
82
-
83
-		data, err := json.Marshal(map[string]string{
84
-			"value": name,
85
-		})
86
-		suite.NoError(err)
67
+	value := typeBytesTestStruct{}
68
+	suite.NoError(value.Value.Set("1kib"))
87 69
 
88
-		suite.T().Run(name, func(t *testing.T) {
89
-			testStruct := &typeBytesTestStruct{}
90
-
91
-			assert.NoError(t, json.Unmarshal(data, testStruct))
92
-			assert.Equal(t, name, testStruct.Value.String())
93
-
94
-			marshalled, err := testStruct.Value.MarshalText()
95
-			assert.NoError(t, err)
96
-			assert.Equal(t, name, string(marshalled))
97
-		})
98
-	}
70
+	data, err := json.Marshal(value)
71
+	suite.NoError(err)
72
+	suite.JSONEq(`{"value": "1kib"}`, string(data))
99 73
 }
100 74
 
101
-func (suite *TypeBytesTestSuite) TestValue() {
102
-	testStruct := &typeBytesTestStruct{}
103
-
104
-	suite.EqualValues(0, testStruct.Value.Value(0))
105
-	suite.EqualValues(1, testStruct.Value.Value(1))
106
-
107
-	data, err := json.Marshal(map[string]string{
108
-		"value": "1kb",
109
-	})
110
-	suite.NoError(err)
111
-	suite.NoError(json.Unmarshal(data, testStruct))
75
+func (suite *TypeBytesTestSuite) TestGet() {
76
+	value := config.TypeBytes{}
77
+	suite.EqualValues(1000, value.Get(1000))
112 78
 
113
-	suite.EqualValues(1024, testStruct.Value.Value(0))
114
-	suite.EqualValues(1024, testStruct.Value.Value(1))
79
+	suite.NoError(value.Set("1mib"))
80
+	suite.EqualValues(1048576, value.Get(1000))
115 81
 }
116 82
 
117 83
 func TestTypeBytes(t *testing.T) {

+ 45
- 0
internal/config/type_concurrency.go Zobrazit soubor

@@ -0,0 +1,45 @@
1
+package config
2
+
3
+import (
4
+	"fmt"
5
+	"strconv"
6
+)
7
+
8
+type TypeConcurrency struct {
9
+	Value uint
10
+}
11
+
12
+func (t *TypeConcurrency) Set(value string) error {
13
+	concurrencyValue, err := strconv.ParseUint(value, 10, 64)
14
+	if err != nil {
15
+		return fmt.Errorf("value is not uint (%s): %w", value, err)
16
+	}
17
+
18
+	if concurrencyValue == 0 {
19
+		return fmt.Errorf("value should be >0 (%s)", value)
20
+	}
21
+
22
+	t.Value = uint(concurrencyValue)
23
+
24
+	return nil
25
+}
26
+
27
+func (t TypeConcurrency) Get(defaultValue uint) uint {
28
+	if t.Value == 0 {
29
+		return defaultValue
30
+	}
31
+
32
+	return t.Value
33
+}
34
+
35
+func (t *TypeConcurrency) UnmarshalJSON(data []byte) error {
36
+	return t.Set(string(data))
37
+}
38
+
39
+func (t TypeConcurrency) MarshalJSON() ([]byte, error) {
40
+	return []byte(t.String()), nil
41
+}
42
+
43
+func (t TypeConcurrency) String() string {
44
+	return strconv.FormatUint(uint64(t.Value), 10)
45
+}

+ 73
- 0
internal/config/type_concurrency_test.go Zobrazit soubor

@@ -0,0 +1,73 @@
1
+package config_test
2
+
3
+import (
4
+	"encoding/json"
5
+	"testing"
6
+
7
+	"github.com/9seconds/mtg/v2/internal/config"
8
+	"github.com/stretchr/testify/assert"
9
+	"github.com/stretchr/testify/suite"
10
+)
11
+
12
+type typeConcurrencyTestStruct struct {
13
+	Value config.TypeConcurrency `json:"value"`
14
+}
15
+
16
+type TypeConcurrencyTestSuite struct {
17
+	suite.Suite
18
+}
19
+
20
+func (suite *TypeConcurrencyTestSuite) TestUnmarshalFail() {
21
+	testData := []string{
22
+		"-1",
23
+		"0",
24
+		"0.0",
25
+		"1.0",
26
+		"1.1",
27
+		".",
28
+		"some_value",
29
+	}
30
+
31
+	for _, v := range testData {
32
+		data, err := json.Marshal(map[string]string{
33
+			"value": v,
34
+		})
35
+		suite.NoError(err)
36
+
37
+		suite.T().Run(v, func(t *testing.T) {
38
+			assert.Error(t, json.Unmarshal(data, &typeConcurrencyTestStruct{}))
39
+		})
40
+	}
41
+}
42
+
43
+func (suite *TypeConcurrencyTestSuite) TestUnmarshalOk() {
44
+	testStruct := &typeConcurrencyTestStruct{}
45
+
46
+	suite.NoError(json.Unmarshal([]byte(`{"value": 1}`), testStruct))
47
+	suite.EqualValues(1, testStruct.Value.Get(2))
48
+}
49
+
50
+func (suite *TypeConcurrencyTestSuite) TestMarshalOk() {
51
+	testStruct := &typeConcurrencyTestStruct{
52
+		Value: config.TypeConcurrency{
53
+			Value: 2,
54
+		},
55
+	}
56
+
57
+	data, err := json.Marshal(testStruct)
58
+	suite.NoError(err)
59
+	suite.JSONEq(`{"value": 2}`, string(data))
60
+}
61
+
62
+func (suite *TypeConcurrencyTestSuite) TestGet() {
63
+	value := config.TypeConcurrency{}
64
+	suite.EqualValues(1, value.Get(1))
65
+
66
+	value.Value = 3
67
+	suite.EqualValues(3, value.Get(1))
68
+}
69
+
70
+func TestTypeConcurrency(t *testing.T) {
71
+	t.Parallel()
72
+	suite.Run(t, &TypeConcurrencyTestSuite{})
73
+}

+ 26
- 19
internal/config/type_duration.go Zobrazit soubor

@@ -6,41 +6,48 @@ import (
6 6
 	"time"
7 7
 )
8 8
 
9
+var typeDurationStringCleaner = strings.NewReplacer(" ", "", "\t", "")
10
+
9 11
 type TypeDuration struct {
10
-	value time.Duration
12
+	Value time.Duration
11 13
 }
12 14
 
13
-func (c *TypeDuration) UnmarshalText(data []byte) error {
14
-	if len(data) == 0 {
15
-		return nil
16
-	}
17
-
18
-	dur, err := time.ParseDuration(strings.ToLower(string(data)))
15
+func (t *TypeDuration) Set(value string) error {
16
+	parsedValue, err := time.ParseDuration(
17
+		typeDurationStringCleaner.Replace(strings.ToLower(value)))
19 18
 	if err != nil {
20
-		return fmt.Errorf("incorrect duration: %w", err)
19
+		return fmt.Errorf("incorrect duration (%s): %w", value, err)
21 20
 	}
22 21
 
23
-	if dur < 0 {
24
-		return fmt.Errorf("%s should be positive duration", dur)
22
+	if parsedValue < 0 {
23
+		return fmt.Errorf("duration has to be a positive: %s", value)
25 24
 	}
26 25
 
27
-	c.value = dur
26
+	t.Value = parsedValue
28 27
 
29 28
 	return nil
30 29
 }
31 30
 
32
-func (c TypeDuration) MarshalText() ([]byte, error) {
33
-	return []byte(c.value.String()), nil
31
+func (t TypeDuration) Get(defaultValue time.Duration) time.Duration {
32
+	if t.Value == 0 {
33
+		return defaultValue
34
+	}
35
+
36
+	return t.Value
34 37
 }
35 38
 
36
-func (c TypeDuration) String() string {
37
-	return c.value.String()
39
+func (t *TypeDuration) UnmarshalText(data []byte) error {
40
+	return t.Set(string(data))
38 41
 }
39 42
 
40
-func (c TypeDuration) Value(defaultValue time.Duration) time.Duration {
41
-	if c.value == 0 {
42
-		return defaultValue
43
+func (t TypeDuration) MarshalText() ([]byte, error) {
44
+	return []byte(t.String()), nil
45
+}
46
+
47
+func (t TypeDuration) String() string {
48
+	if t.Value == 0 {
49
+		return ""
43 50
 	}
44 51
 
45
-	return c.value
52
+	return t.Value.String()
46 53
 }

+ 33
- 41
internal/config/type_duration_test.go Zobrazit soubor

@@ -18,18 +18,12 @@ type TypeDurationTestSuite struct {
18 18
 	suite.Suite
19 19
 }
20 20
 
21
-func (suite *TypeDurationTestSuite) TestUnmarshalNil() {
22
-	typ := &config.TypeDuration{}
23
-	suite.NoError(typ.UnmarshalText(nil))
24
-	suite.EqualValues(0, typ.Value(0))
25
-}
26
-
27 21
 func (suite *TypeDurationTestSuite) TestUnmarshalFail() {
28 22
 	testData := []string{
29
-		"1t",
30
-		"1",
31 23
 		"-1s",
32
-		"-1h",
24
+		"1 seconds ago",
25
+		"1s ago",
26
+		"",
33 27
 	}
34 28
 
35 29
 	for _, v := range testData {
@@ -47,8 +41,11 @@ func (suite *TypeDurationTestSuite) TestUnmarshalFail() {
47 41
 func (suite *TypeDurationTestSuite) TestUnmarshalOk() {
48 42
 	testData := map[string]time.Duration{
49 43
 		"1s":   time.Second,
50
-		"1m":   time.Minute,
51
-		"2h1s": 2*time.Hour + time.Second,
44
+		"0":    0 * time.Second,
45
+		"0s":   0 * time.Second,
46
+		"1\tM": time.Minute,
47
+		"1H":   time.Hour,
48
+		"1 h":  time.Hour,
52 49
 	}
53 50
 
54 51
 	for k, v := range testData {
@@ -63,53 +60,48 @@ func (suite *TypeDurationTestSuite) TestUnmarshalOk() {
63 60
 			testStruct := &typeDurationTestStruct{}
64 61
 
65 62
 			assert.NoError(t, json.Unmarshal(data, testStruct))
66
-			assert.Equal(t, value, testStruct.Value.Value(0))
63
+			assert.Equal(t, value, testStruct.Value.Value)
67 64
 		})
68 65
 	}
69 66
 }
70 67
 
71 68
 func (suite *TypeDurationTestSuite) TestMarshalOk() {
72
-	testData := []string{
73
-		"1s",
74
-		"1m0s",
75
-		"2h0m1s",
69
+	testData := map[string]string{
70
+		"1s":  "1s",
71
+		"0":   "",
72
+		"0s":  "",
73
+		"0ms": "",
74
+		"1 H": "1h0m0s",
76 75
 	}
77 76
 
78
-	for _, v := range testData {
79
-		name := v
80
-
81
-		data, err := json.Marshal(map[string]string{
82
-			"value": name,
83
-		})
84
-		suite.NoError(err)
77
+	for k, v := range testData {
78
+		value := k
79
+		expected := v
85 80
 
86
-		suite.T().Run(name, func(t *testing.T) {
81
+		suite.T().Run(value, func(t *testing.T) {
87 82
 			testStruct := &typeDurationTestStruct{}
88 83
 
89
-			assert.NoError(t, json.Unmarshal(data, testStruct))
90
-			assert.Equal(t, name, testStruct.Value.String())
84
+			assert.NoError(t, testStruct.Value.Set(value))
85
+
86
+			data, err := json.Marshal(testStruct)
87
+			assert.NoError(t, err)
91 88
 
92
-			marshalled, err := testStruct.Value.MarshalText()
89
+			expectedJSON, err := json.Marshal(map[string]string{
90
+				"value": expected,
91
+			})
93 92
 			assert.NoError(t, err)
94
-			assert.Equal(t, name, string(marshalled))
93
+
94
+			assert.JSONEq(t, string(expectedJSON), string(data))
95 95
 		})
96 96
 	}
97 97
 }
98 98
 
99
-func (suite *TypeDurationTestSuite) TestValue() {
100
-	testStruct := &typeDurationTestStruct{}
101
-
102
-	suite.EqualValues(0, testStruct.Value.Value(0))
103
-	suite.Equal(time.Second, testStruct.Value.Value(time.Second))
104
-
105
-	data, err := json.Marshal(map[string]string{
106
-		"value": "1s",
107
-	})
108
-	suite.NoError(err)
109
-	suite.NoError(json.Unmarshal(data, testStruct))
99
+func (suite *TypeDurationTestSuite) TestGet() {
100
+	value := config.TypeDuration{}
101
+	suite.Equal(time.Second, value.Get(time.Second))
110 102
 
111
-	suite.Equal(time.Second, testStruct.Value.Value(0))
112
-	suite.Equal(time.Second, testStruct.Value.Value(time.Minute))
103
+	value.Value = 3 * time.Second
104
+	suite.Equal(3*time.Second, value.Get(time.Hour))
113 105
 }
114 106
 
115 107
 func TestTypeDuration(t *testing.T) {

+ 20
- 16
internal/config/type_error_rate.go Zobrazit soubor

@@ -8,36 +8,40 @@ import (
8 8
 const typeErrorRateIgnoreLess = 1e-8
9 9
 
10 10
 type TypeErrorRate struct {
11
-	value float64
11
+	Value float64
12 12
 }
13 13
 
14
-func (c *TypeErrorRate) UnmarshalJSON(data []byte) error {
15
-	value, err := strconv.ParseFloat(string(data), 64)
14
+func (t *TypeErrorRate) Set(value string) error {
15
+	parsedValue, err := strconv.ParseFloat(value, 64)
16 16
 	if err != nil {
17
-		return fmt.Errorf("incorrect float value: %w", err)
17
+		return fmt.Errorf("value is not a float (%s): %w", value, err)
18 18
 	}
19 19
 
20
-	if value <= 0 || value >= 100 {
21
-		return fmt.Errorf("%f should be 0 < x < 100", value)
20
+	if parsedValue <= 0.0 || parsedValue >= 100.0 {
21
+		return fmt.Errorf("value should be 0 < x < 100 (%s)", value)
22 22
 	}
23 23
 
24
-	c.value = value
24
+	t.Value = parsedValue
25 25
 
26 26
 	return nil
27 27
 }
28 28
 
29
-func (c *TypeErrorRate) MarshalText() ([]byte, error) {
30
-	return []byte(c.String()), nil
29
+func (t TypeErrorRate) Get(defaultValue float64) float64 {
30
+	if t.Value < typeErrorRateIgnoreLess {
31
+		return defaultValue
32
+	}
33
+
34
+	return t.Value
31 35
 }
32 36
 
33
-func (c TypeErrorRate) String() string {
34
-	return strconv.FormatFloat(c.value, 'f', -1, 64)
37
+func (t *TypeErrorRate) UnmarshalJSON(data []byte) error {
38
+	return t.Set(string(data))
35 39
 }
36 40
 
37
-func (c TypeErrorRate) Value(defaultValue float64) float64 {
38
-	if c.value < typeErrorRateIgnoreLess {
39
-		return defaultValue
40
-	}
41
+func (t TypeErrorRate) MarshalJSON() ([]byte, error) {
42
+	return []byte(t.String()), nil
43
+}
41 44
 
42
-	return c.value
45
+func (t TypeErrorRate) String() string {
46
+	return strconv.FormatFloat(t.Value, 'f', -1, 64)
43 47
 }

+ 33
- 77
internal/config/type_error_rate_test.go Zobrazit soubor

@@ -2,7 +2,6 @@ package config_test
2 2
 
3 3
 import (
4 4
 	"encoding/json"
5
-	"strconv"
6 5
 	"testing"
7 6
 
8 7
 	"github.com/9seconds/mtg/v2/internal/config"
@@ -19,104 +18,61 @@ type TypeErrorRateTestSuite struct {
19 18
 }
20 19
 
21 20
 func (suite *TypeErrorRateTestSuite) TestUnmarshalFail() {
22
-	testData := []float64{
23
-		1000,
24
-		-100,
25
-		-0.0001,
21
+	testData := []string{
22
+		"",
23
+		"1s",
24
+		"1,",
25
+		"1,2",
26
+		".",
27
+		"3.4.5",
28
+		"3.5.",
29
+		".3.5",
30
+		"some word",
31
+		"1e2",
32
+		"-1.0",
26 33
 	}
27 34
 
28 35
 	for _, v := range testData {
29
-		data, err := json.Marshal(map[string]float64{
36
+		data, err := json.Marshal(map[string]string{
30 37
 			"value": v,
31 38
 		})
32 39
 		suite.NoError(err)
33 40
 
34
-		suite.T().Run(strconv.FormatFloat(v, 'f', -1, 64), func(t *testing.T) {
41
+		suite.T().Run(v, func(t *testing.T) {
35 42
 			assert.Error(t, json.Unmarshal(data, &typeErrorRateTestStruct{}))
36 43
 		})
37 44
 	}
38
-
39
-	data, err := json.Marshal(map[string]string{
40
-		"value": "hello",
41
-	})
42
-	suite.NoError(err)
43
-	suite.Error(json.Unmarshal(data, &typeErrorRateTestStruct{}))
44 45
 }
45 46
 
46 47
 func (suite *TypeErrorRateTestSuite) TestUnmarshalOk() {
47
-	testData := []float64{
48
-		1,
49
-		55.5,
50
-		0.0001,
51
-		1e-6,
52
-	}
53
-
54
-	for _, v := range testData {
55
-		value := v
56
-
57
-		data, err := json.Marshal(map[string]float64{
58
-			"value": v,
59
-		})
60
-		suite.NoError(err)
61
-
62
-		suite.T().Run(strconv.FormatFloat(v, 'f', -1, 64), func(t *testing.T) {
63
-			testStruct := &typeErrorRateTestStruct{}
48
+	data, err := json.Marshal(map[string]float64{
49
+		"value": 1.0,
50
+	})
51
+	suite.NoError(err)
64 52
 
65
-			assert.NoError(t, json.Unmarshal(data, testStruct))
66
-			assert.InEpsilon(t, value, testStruct.Value.Value(0), 1e-10)
67
-		})
68
-	}
53
+	testStruct := &typeErrorRateTestStruct{}
54
+	suite.NoError(json.Unmarshal(data, testStruct))
55
+	suite.InEpsilon(1.0, testStruct.Value.Value, 1e-10)
69 56
 }
70 57
 
71 58
 func (suite *TypeErrorRateTestSuite) TestMarshalOk() {
72
-	testData := []float64{
73
-		1,
74
-		55.5,
75
-		0.0001,
76
-		1e-6,
59
+	testStruct := typeErrorRateTestStruct{
60
+		Value: config.TypeErrorRate{
61
+			Value: 1.01,
62
+		},
77 63
 	}
78 64
 
79
-	for _, v := range testData {
80
-		value := v
81
-
82
-		data, err := json.Marshal(map[string]float64{
83
-			"value": v,
84
-		})
85
-		suite.NoError(err)
86
-
87
-		suite.T().Run(strconv.FormatFloat(v, 'f', -1, 64), func(t *testing.T) {
88
-			testStruct := &typeErrorRateTestStruct{}
89
-
90
-			assert.NoError(t, json.Unmarshal(data, testStruct))
91
-
92
-			parsed, err := strconv.ParseFloat(testStruct.Value.String(), 64)
93
-			assert.NoError(t, err)
94
-			assert.InEpsilon(t, value, parsed, 1e-10)
95
-
96
-			marshalled, err := testStruct.Value.MarshalText()
97
-			assert.NoError(t, err)
98
-
99
-			parsed, err = strconv.ParseFloat(string(marshalled), 64)
100
-			assert.NoError(t, err)
101
-			assert.InEpsilon(t, value, parsed, 1e-10)
102
-		})
103
-	}
65
+	encodedJSON, err := json.Marshal(testStruct)
66
+	suite.NoError(err)
67
+	suite.JSONEq(`{"value": 1.01}`, string(encodedJSON))
104 68
 }
105 69
 
106
-func (suite *TypeErrorRateTestSuite) TestValue() {
107
-	testStruct := &typeErrorRateTestStruct{}
108
-
109
-	suite.InEpsilon(1, testStruct.Value.Value(1), 1e-10)
110
-	suite.InEpsilon(2, testStruct.Value.Value(2), 1e-10)
111
-
112
-	data, err := json.Marshal(map[string]float64{
113
-		"value": 1,
114
-	})
115
-	suite.NoError(err)
116
-	suite.NoError(json.Unmarshal(data, testStruct))
70
+func (suite *TypeErrorRateTestSuite) TestGet() {
71
+	value := config.TypeErrorRate{}
72
+	suite.InEpsilon(1.0, value.Get(1.0), 1e-10)
117 73
 
118
-	suite.InEpsilon(1, testStruct.Value.Value(2), 1e-10)
119
-	suite.InEpsilon(1, testStruct.Value.Value(3), 1e-10)
74
+	value.Value = 5.0
75
+	suite.InEpsilon(5.0, value.Get(1.0), 1e-10)
120 76
 }
121 77
 
122 78
 func TestTypeErrorRate(t *testing.T) {

+ 30
- 34
internal/config/type_hostport.go Zobrazit soubor

@@ -7,61 +7,57 @@ import (
7 7
 )
8 8
 
9 9
 type TypeHostPort struct {
10
-	host TypeIP
11
-	port TypePort
10
+	Value string
11
+	Host  string
12
+	Port  uint
12 13
 }
13 14
 
14
-func (c *TypeHostPort) UnmarshalText(data []byte) error {
15
-	if len(data) == 0 {
16
-		return nil
15
+func (t *TypeHostPort) Set(value string) error {
16
+	host, port, err := net.SplitHostPort(value)
17
+	if err != nil {
18
+		return fmt.Errorf("incorrect host:port value (%v): %w", value, err)
17 19
 	}
18 20
 
19
-	text := string(data)
20
-
21
-	host, port, err := net.SplitHostPort(text)
21
+	portValue, err := strconv.ParseUint(port, 10, 16)
22 22
 	if err != nil {
23
-		return fmt.Errorf("incorrect host:port syntax: %w", err)
23
+		return fmt.Errorf("incorrect port number (%v): %w", value, err)
24 24
 	}
25 25
 
26
-	if port == "" {
27
-		return fmt.Errorf("port in %s host:port pair cannot be empty", text)
26
+	if portValue == 0 {
27
+		return fmt.Errorf("incorrect port number (%s)", value)
28 28
 	}
29 29
 
30
-	if err := c.port.UnmarshalJSON([]byte(port)); err != nil {
31
-		return fmt.Errorf("incorrect port in host:port: %w", err)
30
+	if host == "" {
31
+		return fmt.Errorf("empty host: %s", value)
32 32
 	}
33 33
 
34
-	if err := c.host.UnmarshalText([]byte(host)); err != nil {
35
-		return fmt.Errorf("incorrect host: %w", err)
34
+	if net.ParseIP(host) == nil {
35
+		return fmt.Errorf("host is not an IP address: %s", value)
36 36
 	}
37 37
 
38
+	t.Value = net.JoinHostPort(host, port)
39
+	t.Port = uint(portValue)
40
+	t.Host = host
41
+
38 42
 	return nil
39 43
 }
40 44
 
41
-func (c TypeHostPort) MarshalText() ([]byte, error) {
42
-	return []byte(c.String()), nil
43
-}
45
+func (t TypeHostPort) Get(defaultValue string) string {
46
+	if t.Value == "" {
47
+		return defaultValue
48
+	}
44 49
 
45
-func (c TypeHostPort) String() string {
46
-	return c.Value(net.IP{}, 0)
50
+	return t.Value
47 51
 }
48 52
 
49
-func (c TypeHostPort) HostValue(defaultValue net.IP) net.IP {
50
-	return c.host.Value(defaultValue)
53
+func (t *TypeHostPort) UnmarshalText(data []byte) error {
54
+	return t.Set(string(data))
51 55
 }
52 56
 
53
-func (c TypeHostPort) PortValue(defaultValue uint) uint {
54
-	return c.port.Value(defaultValue)
57
+func (t TypeHostPort) MarshalText() ([]byte, error) {
58
+	return []byte(t.String()), nil
55 59
 }
56 60
 
57
-func (c TypeHostPort) Value(defaultHostValue net.IP, defaultPortValue uint) string {
58
-	host := c.HostValue(defaultHostValue)
59
-	port := c.PortValue(defaultPortValue)
60
-
61
-	hostStr := ""
62
-	if len(host) > 0 {
63
-		hostStr = host.String()
64
-	}
65
-
66
-	return net.JoinHostPort(hostStr, strconv.Itoa(int(port)))
61
+func (t TypeHostPort) String() string {
62
+	return t.Value
67 63
 }

+ 21
- 48
internal/config/type_hostport_test.go Zobrazit soubor

@@ -2,7 +2,6 @@ package config_test
2 2
 
3 3
 import (
4 4
 	"encoding/json"
5
-	"net"
6 5
 	"testing"
7 6
 
8 7
 	"github.com/9seconds/mtg/v2/internal/config"
@@ -20,11 +19,13 @@ type TypeHostPortTestSuite struct {
20 19
 
21 20
 func (suite *TypeHostPortTestSuite) TestUnmarshalFail() {
22 21
 	testData := []string{
23
-		"10.0.0.10:aaa",
24
-		"10.0.0.10:",
25 22
 		":",
26
-		"xxx",
27
-		"xxx:80",
23
+		":800",
24
+		"127.0.0.1:8000000",
25
+		"12...:80",
26
+		"",
27
+		"localhost",
28
+		"google.com:",
28 29
 	}
29 30
 
30 31
 	for _, v := range testData {
@@ -41,9 +42,8 @@ func (suite *TypeHostPortTestSuite) TestUnmarshalFail() {
41 42
 
42 43
 func (suite *TypeHostPortTestSuite) TestUnmarshalOk() {
43 44
 	testData := []string{
44
-		"10.0.0.10:80",
45
-		"0.0.0.0:80",
46
-		":8000",
45
+		"127.0.0.1:80",
46
+		"10.0.0.10:6553",
47 47
 	}
48 48
 
49 49
 	for _, v := range testData {
@@ -56,57 +56,30 @@ func (suite *TypeHostPortTestSuite) TestUnmarshalOk() {
56 56
 
57 57
 		suite.T().Run(v, func(t *testing.T) {
58 58
 			testStruct := &typeHostPortTestStruct{}
59
-
60 59
 			assert.NoError(t, json.Unmarshal(data, testStruct))
61
-			assert.EqualValues(t, value, testStruct.Value.Value(nil, 0))
60
+			assert.Equal(t, value, testStruct.Value.Value)
62 61
 		})
63 62
 	}
64 63
 }
65 64
 
66 65
 func (suite *TypeHostPortTestSuite) TestMarshalOk() {
67
-	testData := []string{
68
-		"10.0.0.10:80",
69
-		"0.0.0.0:80",
70
-		":8000",
66
+	testStruct := typeHostPortTestStruct{
67
+		Value: config.TypeHostPort{
68
+			Value: "127.0.0.1:8000",
69
+		},
71 70
 	}
72 71
 
73
-	for _, v := range testData {
74
-		value := v
75
-
76
-		data, err := json.Marshal(map[string]string{
77
-			"value": v,
78
-		})
79
-		suite.NoError(err)
80
-
81
-		suite.T().Run(v, func(t *testing.T) {
82
-			testStruct := &typeHostPortTestStruct{}
83
-
84
-			assert.NoError(t, json.Unmarshal(data, testStruct))
85
-			assert.Equal(t, value, testStruct.Value.String())
86
-
87
-			marshalled, err := testStruct.Value.MarshalText()
88
-			assert.NoError(t, err)
89
-			assert.Equal(t, value, string(marshalled))
90
-		})
91
-	}
72
+	data, err := json.Marshal(testStruct)
73
+	suite.NoError(err)
74
+	suite.JSONEq(`{"value": "127.0.0.1:8000"}`, string(data))
92 75
 }
93 76
 
94
-func (suite *TypeHostPortTestSuite) TestValue() {
95
-	testStruct := &typeHostPortTestStruct{}
96
-
97
-	suite.EqualValues("127.0.0.1:80",
98
-		testStruct.Value.Value(net.ParseIP("127.0.0.1"), 80))
99
-	suite.EqualValues("127.1.0.1:80",
100
-		testStruct.Value.Value(net.ParseIP("127.1.0.1"), 80))
101
-
102
-	data, err := json.Marshal(map[string]string{
103
-		"value": "127.0.0.1:80",
104
-	})
105
-	suite.NoError(err)
106
-	suite.NoError(json.Unmarshal(data, testStruct))
77
+func (suite *TypeHostPortTestSuite) TestGet() {
78
+	value := config.TypeHostPort{}
79
+	suite.Equal("127.0.0.1:9000", value.Get("127.0.0.1:9000"))
107 80
 
108
-	suite.EqualValues("127.0.0.1:80", testStruct.Value.Value(nil, 0))
109
-	suite.EqualValues("127.0.0.1:80", testStruct.Value.Value(net.ParseIP("10.0.0.10"), 3000))
81
+	value.Value = "127.0.0.1:80"
82
+	suite.Equal("127.0.0.1:80", value.Get("127.0.0.1:9000"))
110 83
 }
111 84
 
112 85
 func TestTypeHostPort(t *testing.T) {

+ 16
- 18
internal/config/type_http_path.go Zobrazit soubor

@@ -3,33 +3,31 @@ package config
3 3
 import "strings"
4 4
 
5 5
 type TypeHTTPPath struct {
6
-	value string
6
+	Value string
7 7
 }
8 8
 
9
-func (c *TypeHTTPPath) UnmarshalText(data []byte) error {
10
-	if len(data) > 0 {
11
-		c.value = "/" + strings.Trim(string(data), "/")
12
-	}
9
+func (t *TypeHTTPPath) Set(value string) error {
10
+	t.Value = "/" + strings.Trim(value, "/")
13 11
 
14 12
 	return nil
15 13
 }
16 14
 
17
-func (c TypeHTTPPath) MarshalText() ([]byte, error) {
18
-	return []byte(c.String()), nil
19
-}
20
-
21
-func (c TypeHTTPPath) String() string {
22
-	if c.value == "" {
23
-		return "/"
15
+func (t TypeHTTPPath) Get(defaultValue string) string {
16
+	if t.Value == "" {
17
+		return defaultValue
24 18
 	}
25 19
 
26
-	return c.value
20
+	return t.Value
27 21
 }
28 22
 
29
-func (c TypeHTTPPath) Value(defaultValue string) string {
30
-	if c.value == "" {
31
-		return defaultValue
32
-	}
23
+func (t *TypeHTTPPath) UnmarshalText(data []byte) error {
24
+	return t.Set(string(data))
25
+}
26
+
27
+func (t TypeHTTPPath) MarshalText() ([]byte, error) {
28
+	return []byte(t.String()), nil
29
+}
33 30
 
34
-	return c.value
31
+func (t TypeHTTPPath) String() string {
32
+	return t.Value
35 33
 }

+ 24
- 48
internal/config/type_http_path_test.go Zobrazit soubor

@@ -17,72 +17,48 @@ type TypeHTTPPathTestSuite struct {
17 17
 	suite.Suite
18 18
 }
19 19
 
20
-func (suite *TypeHTTPPathTestSuite) TestUnmarshal() {
21
-	testData := []string{
22
-		"/hello",
23
-		"hello",
24
-		"hello/",
25
-		"/hello/",
26
-	}
27
-
28
-	for _, v := range testData {
29
-		data, err := json.Marshal(map[string]string{
30
-			"value": v,
31
-		})
32
-		suite.NoError(err)
33
-
34
-		suite.T().Run(v, func(t *testing.T) {
35
-			testStruct := &typeHTTPPathTestStruct{}
36
-
37
-			assert.NoError(t, json.Unmarshal(data, testStruct))
38
-			assert.Equal(t, "/hello", testStruct.Value.Value(""))
39
-		})
40
-	}
41
-}
42
-
43
-func (suite *TypeHTTPPathTestSuite) TestMarshalOk() {
20
+func (suite *TypeHTTPPathTestSuite) TestUnmarshalOk() {
44 21
 	testData := map[string]string{
45
-		"":        "/",
46
-		"/hello":  "/hello",
47
-		"/hello/": "/hello",
48
-		"hello/":  "/hello",
49
-		"hello":   "/hello",
22
+		"":      "/",
23
+		"/":     "/",
24
+		"/path": "/path",
25
+		"path":  "/path",
50 26
 	}
51 27
 
52 28
 	for k, v := range testData {
53
-		toPass := k
54
-		compareWith := v
29
+		value := v
55 30
 
56 31
 		data, err := json.Marshal(map[string]string{
57
-			"value": toPass,
32
+			"value": k,
58 33
 		})
59 34
 		suite.NoError(err)
60 35
 
61
-		suite.T().Run(toPass, func(t *testing.T) {
36
+		suite.T().Run(k, func(t *testing.T) {
62 37
 			testStruct := &typeHTTPPathTestStruct{}
63
-
64 38
 			assert.NoError(t, json.Unmarshal(data, testStruct))
65
-			assert.Equal(t, compareWith, testStruct.Value.String())
66
-
67
-			marshalled, err := testStruct.Value.MarshalText()
68
-			assert.NoError(t, err)
69
-			assert.Equal(t, compareWith, string(marshalled))
39
+			assert.Equal(t, value, testStruct.Value.Get(""))
70 40
 		})
71 41
 	}
72 42
 }
73 43
 
74
-func (suite *TypeHTTPPathTestSuite) TestValue() {
75
-	testStruct := &typeHTTPPathTestStruct{}
76
-
77
-	suite.Equal("/hello", testStruct.Value.Value("/hello"))
44
+func (suite *TypeHTTPPathTestSuite) TestMarshalOk() {
45
+	value := typeHTTPPathTestStruct{
46
+		Value: config.TypeHTTPPath{
47
+			Value: "/path",
48
+		},
49
+	}
78 50
 
79
-	data, err := json.Marshal(map[string]string{
80
-		"value": "/map",
81
-	})
51
+	data, err := json.Marshal(value)
82 52
 	suite.NoError(err)
83
-	suite.NoError(json.Unmarshal(data, testStruct))
53
+	suite.JSONEq(`{"value": "/path"}`, string(data))
54
+}
55
+
56
+func (suite *TypeHTTPPathTestSuite) TestGet() {
57
+	value := config.TypeHTTPPath{}
58
+	suite.Equal("/hello", value.Get("/hello"))
84 59
 
85
-	suite.Equal("/map", testStruct.Value.Value("/hello"))
60
+	suite.NoError(value.Set("/lalala"))
61
+	suite.Equal("/lalala", value.Get("/hello"))
86 62
 }
87 63
 
88 64
 func TestTypeHTTPPath(t *testing.T) {

+ 20
- 20
internal/config/type_ip.go Zobrazit soubor

@@ -6,40 +6,40 @@ import (
6 6
 )
7 7
 
8 8
 type TypeIP struct {
9
-	value net.IP
9
+	Value net.IP
10 10
 }
11 11
 
12
-func (c *TypeIP) UnmarshalText(data []byte) error {
13
-	if len(data) == 0 {
14
-		return nil
15
-	}
16
-
17
-	ip := net.ParseIP(string(data))
12
+func (t *TypeIP) Set(value string) error {
13
+	ip := net.ParseIP(value)
18 14
 	if ip == nil {
19
-		return fmt.Errorf("incorrect ip address: %s", string(data))
15
+		return fmt.Errorf("incorret ip %s", value)
20 16
 	}
21 17
 
22
-	c.value = ip
18
+	t.Value = ip
23 19
 
24 20
 	return nil
25 21
 }
26 22
 
27
-func (c *TypeIP) MarshalText() ([]byte, error) {
28
-	return []byte(c.String()), nil
23
+func (t *TypeIP) Get(defaultValue net.IP) net.IP {
24
+	if len(t.Value) == 0 {
25
+		return defaultValue
26
+	}
27
+
28
+	return t.Value
29 29
 }
30 30
 
31
-func (c TypeIP) String() string {
32
-	if len(c.value) > 0 {
33
-		return c.value.String()
34
-	}
31
+func (t *TypeIP) UnmarshalText(data []byte) error {
32
+	return t.Set(string(data))
33
+}
35 34
 
36
-	return ""
35
+func (t TypeIP) MarshalText() ([]byte, error) {
36
+	return []byte(t.String()), nil
37 37
 }
38 38
 
39
-func (c TypeIP) Value(defaultValue net.IP) net.IP {
40
-	if c.value == nil {
41
-		return defaultValue
39
+func (t TypeIP) String() string {
40
+	if len(t.Value) == 0 {
41
+		return ""
42 42
 	}
43 43
 
44
-	return c.value
44
+	return t.Value.String()
45 45
 }

+ 33
- 44
internal/config/type_ip_test.go Zobrazit soubor

@@ -20,10 +20,11 @@ type TypeIPTestSuite struct {
20 20
 
21 21
 func (suite *TypeIPTestSuite) TestUnmarshalFail() {
22 22
 	testData := []string{
23
-		"0.0.10",
24
-		"10.0.0.10:",
25
-		"xxx:80",
26
-		"2001:0db8:85a3:0000:0000:8a2e:4",
23
+		"",
24
+		"....",
25
+		"0...",
26
+		"300.200.200.800",
27
+		"[]",
27 28
 	}
28 29
 
29 30
 	for _, v := range testData {
@@ -39,74 +40,62 @@ func (suite *TypeIPTestSuite) TestUnmarshalFail() {
39 40
 }
40 41
 
41 42
 func (suite *TypeIPTestSuite) TestUnmarshalOk() {
42
-	testData := []string{
43
-		"0.0.0.0",
44
-		"10.0.0.10",
45
-		"2001:0db8:85a3:0000:0000:8a2e:0370:7334",
43
+	testData := map[string]string{
44
+		"2001:0db8:85a3:0000:0000:8a2e:0370:7334": "2001:db8:85a3::8a2e:370:7334",
45
+		"127.0.0.1": "127.0.0.1",
46 46
 	}
47 47
 
48
-	for _, v := range testData {
49
-		value := v
48
+	for k, v := range testData {
49
+		expected := v
50 50
 
51 51
 		data, err := json.Marshal(map[string]string{
52
-			"value": v,
52
+			"value": k,
53 53
 		})
54 54
 		suite.NoError(err)
55 55
 
56
-		suite.T().Run(v, func(t *testing.T) {
56
+		suite.T().Run(k, func(t *testing.T) {
57 57
 			testStruct := &typeIPTestStruct{}
58
-
59 58
 			assert.NoError(t, json.Unmarshal(data, testStruct))
60
-			assert.Equal(t,
61
-				net.ParseIP(value).String(),
62
-				testStruct.Value.Value(nil).String())
59
+			assert.Equal(t, expected, testStruct.Value.Get(nil).String())
63 60
 		})
64 61
 	}
65 62
 }
66 63
 
67 64
 func (suite *TypeIPTestSuite) TestMarshalOk() {
68 65
 	testData := []string{
69
-		"0.0.0.0",
70
-		"10.0.0.10",
71
-		"2001:0db8:85a3:0000:0000:8a2e:0370:7334",
66
+		"2001:db8:85a3::8a2e:370:7334",
67
+		"127.0.0.1",
72 68
 	}
73 69
 
74 70
 	for _, v := range testData {
75
-		value := net.ParseIP(v).String()
76
-
77
-		data, err := json.Marshal(map[string]string{
78
-			"value": v,
79
-		})
80
-		suite.NoError(err)
71
+		value := v
81 72
 
82 73
 		suite.T().Run(v, func(t *testing.T) {
83
-			testStruct := &typeIPTestStruct{}
74
+			testStruct := &typeIPTestStruct{
75
+				Value: config.TypeIP{
76
+					Value: net.ParseIP(value),
77
+				},
78
+			}
84 79
 
85
-			assert.NoError(t, json.Unmarshal(data, testStruct))
86
-			assert.Equal(t, value, testStruct.Value.String())
80
+			encodedJSON, err := json.Marshal(testStruct)
81
+			assert.NoError(t, err)
87 82
 
88
-			marshalled, err := testStruct.Value.MarshalText()
83
+			expectedJSON, err := json.Marshal(map[string]string{
84
+				"value": value,
85
+			})
89 86
 			assert.NoError(t, err)
90
-			assert.Equal(t, value, string(marshalled))
87
+
88
+			assert.JSONEq(t, string(expectedJSON), string(encodedJSON))
91 89
 		})
92 90
 	}
93 91
 }
94 92
 
95
-func (suite *TypeIPTestSuite) TestValue() {
96
-	testStruct := &typeIPTestStruct{}
97
-	suite.Empty(testStruct.Value.String())
98
-
99
-	suite.Nil(testStruct.Value.Value(nil))
100
-	suite.Equal("127.1.0.1", testStruct.Value.Value(net.ParseIP("127.1.0.1")).String())
101
-
102
-	data, err := json.Marshal(map[string]string{
103
-		"value": "127.0.0.1",
104
-	})
105
-	suite.NoError(err)
106
-	suite.NoError(json.Unmarshal(data, testStruct))
93
+func (suite *TypeIPTestSuite) TestGet() {
94
+	value := config.TypeIP{}
95
+	suite.Equal("127.0.0.1", value.Get(net.ParseIP("127.0.0.1")).String())
107 96
 
108
-	suite.Equal("127.0.0.1", testStruct.Value.Value(nil).String())
109
-	suite.Equal("127.0.0.1", testStruct.Value.Value(net.ParseIP("10.0.0.10")).String())
97
+	suite.NoError(value.Set("127.0.0.2"))
98
+	suite.Equal("127.0.0.2", value.Get(net.ParseIP("127.0.0.1")).String())
110 99
 }
111 100
 
112 101
 func TestTypeIP(t *testing.T) {

+ 18
- 19
internal/config/type_metric_prefix.go Zobrazit soubor

@@ -6,36 +6,35 @@ import (
6 6
 )
7 7
 
8 8
 type TypeMetricPrefix struct {
9
-	value string
9
+	Value string
10 10
 }
11 11
 
12
-func (c *TypeMetricPrefix) UnmarshalText(data []byte) error {
13
-	if len(data) == 0 {
14
-		return nil
12
+func (t *TypeMetricPrefix) Set(value string) error {
13
+	if ok, err := regexp.MatchString("^[a-z0-9]+$", value); !ok || err != nil {
14
+		return fmt.Errorf("incorrect metric prefix %s: %w", value, err)
15 15
 	}
16 16
 
17
-	prefix := string(data)
18
-	if ok, err := regexp.MatchString("^[a-z0-9]+$", prefix); !ok || err != nil {
19
-		return fmt.Errorf("incorrect metric prefix: %s", prefix)
20
-	}
21
-
22
-	c.value = prefix
17
+	t.Value = value
23 18
 
24 19
 	return nil
25 20
 }
26 21
 
27
-func (c TypeMetricPrefix) MarshalText() ([]byte, error) {
28
-	return []byte(c.String()), nil
22
+func (t TypeMetricPrefix) Get(defaultValue string) string {
23
+	if t.Value == "" {
24
+		return defaultValue
25
+	}
26
+
27
+	return t.Value
29 28
 }
30 29
 
31
-func (c TypeMetricPrefix) String() string {
32
-	return c.value
30
+func (t *TypeMetricPrefix) UnmarshalText(data []byte) error {
31
+	return t.Set(string(data))
33 32
 }
34 33
 
35
-func (c TypeMetricPrefix) Value(defaultValue string) string {
36
-	if c.value == "" {
37
-		return defaultValue
38
-	}
34
+func (t TypeMetricPrefix) MarshalText() ([]byte, error) {
35
+	return []byte(t.String()), nil
36
+}
39 37
 
40
-	return c.value
38
+func (t TypeMetricPrefix) String() string {
39
+	return t.Value
41 40
 }

+ 20
- 65
internal/config/type_metric_prefix_test.go Zobrazit soubor

@@ -17,18 +17,13 @@ type TypeMetricPrefixTestSuite struct {
17 17
 	suite.Suite
18 18
 }
19 19
 
20
-func (suite *TypeMetricPrefixTestSuite) TestUnmarshalNil() {
21
-	typ := &config.TypeMetricPrefix{}
22
-	suite.NoError(typ.UnmarshalText(nil))
23
-	suite.Empty(typ.String())
24
-}
25
-
26 20
 func (suite *TypeMetricPrefixTestSuite) TestUnmarshalFail() {
27 21
 	testData := []string{
28
-		"aaa.aaa",
29
-		"aaa-bbb",
30
-		"aaa:ccc",
31
-		"metric prefix",
22
+		"",
23
+		"-",
24
+		"hello/world",
25
+		"lala*",
26
+		"++sdf++",
32 27
 	}
33 28
 
34 29
 	for _, v := range testData {
@@ -44,69 +39,29 @@ func (suite *TypeMetricPrefixTestSuite) TestUnmarshalFail() {
44 39
 }
45 40
 
46 41
 func (suite *TypeMetricPrefixTestSuite) TestUnmarshalOk() {
47
-	testData := []string{
48
-		"mtg",
49
-		"mtg111",
50
-	}
51
-
52
-	for _, v := range testData {
53
-		value := v
54
-
55
-		data, err := json.Marshal(map[string]string{
56
-			"value": v,
57
-		})
58
-		suite.NoError(err)
59
-
60
-		suite.T().Run(v, func(t *testing.T) {
61
-			testStruct := &typeMetricPrefixTestStruct{}
62
-
63
-			assert.NoError(t, json.Unmarshal(data, testStruct))
64
-			assert.Equal(t, value, testStruct.Value.Value(""))
65
-		})
66
-	}
42
+	testStruct := &typeMetricPrefixTestStruct{}
43
+	suite.NoError(json.Unmarshal([]byte(`{"value": "mtg"}`), testStruct))
44
+	suite.Equal("mtg", testStruct.Value.Get("lalala"))
67 45
 }
68 46
 
69 47
 func (suite *TypeMetricPrefixTestSuite) TestMarshalOk() {
70
-	testData := []string{
71
-		"mtg",
72
-		"mtg111",
48
+	testStruct := &typeMetricPrefixTestStruct{
49
+		Value: config.TypeMetricPrefix{
50
+			Value: "mtg",
51
+		},
73 52
 	}
74 53
 
75
-	for _, v := range testData {
76
-		value := v
77
-
78
-		data, err := json.Marshal(map[string]string{
79
-			"value": v,
80
-		})
81
-		suite.NoError(err)
82
-
83
-		suite.T().Run(v, func(t *testing.T) {
84
-			testStruct := &typeMetricPrefixTestStruct{}
85
-
86
-			assert.NoError(t, json.Unmarshal(data, testStruct))
87
-			assert.Equal(t, value, testStruct.Value.String())
88
-
89
-			marshalled, err := testStruct.Value.MarshalText()
90
-			assert.NoError(t, err)
91
-			assert.Equal(t, value, string(marshalled))
92
-		})
93
-	}
54
+	data, err := json.Marshal(testStruct)
55
+	suite.NoError(err)
56
+	suite.JSONEq(`{"value": "mtg"}`, string(data))
94 57
 }
95 58
 
96
-func (suite *TypeMetricPrefixTestSuite) TestValue() {
97
-	testStruct := &typeMetricPrefixTestStruct{}
98
-
99
-	suite.Equal("mtg", testStruct.Value.Value("mtg"))
100
-	suite.Equal("vvv", testStruct.Value.Value("vvv"))
101
-
102
-	data, err := json.Marshal(map[string]string{
103
-		"value": "aaa",
104
-	})
105
-	suite.NoError(err)
106
-	suite.NoError(json.Unmarshal(data, testStruct))
59
+func (suite *TypeMetricPrefixTestSuite) TestGet() {
60
+	value := config.TypeMetricPrefix{}
61
+	suite.Equal("lalala", value.Get("lalala"))
107 62
 
108
-	suite.Equal("aaa", testStruct.Value.Value("mtg"))
109
-	suite.Equal("aaa", testStruct.Value.Value("vvv"))
63
+	value.Value = "mtg"
64
+	suite.Equal("mtg", value.Get("lalala"))
110 65
 }
111 66
 
112 67
 func TestTypeMetricPrefix(t *testing.T) {

+ 20
- 20
internal/config/type_port.go Zobrazit soubor

@@ -6,40 +6,40 @@ import (
6 6
 )
7 7
 
8 8
 type TypePort struct {
9
-	value uint
9
+	Value uint
10 10
 }
11 11
 
12
-func (c *TypePort) UnmarshalJSON(data []byte) error {
13
-	if len(data) == 0 {
14
-		return nil
15
-	}
16
-
17
-	intValue, err := strconv.ParseUint(string(data), 10, 64)
12
+func (t *TypePort) Set(value string) error {
13
+	portValue, err := strconv.ParseUint(value, 10, 16)
18 14
 	if err != nil {
19
-		return fmt.Errorf("port number is not a number: %w", err)
15
+		return fmt.Errorf("incorrect port number (%v): %w", value, err)
20 16
 	}
21 17
 
22
-	if intValue == 0 || intValue >= 65536 {
23
-		return fmt.Errorf("port number should be 0 < portNo < 65536: %d", intValue)
18
+	if portValue == 0 {
19
+		return fmt.Errorf("incorrect port number (%s)", value)
24 20
 	}
25 21
 
26
-	c.value = uint(intValue)
22
+	t.Value = uint(portValue)
27 23
 
28 24
 	return nil
29 25
 }
30 26
 
31
-func (c *TypePort) MarshalJSON() ([]byte, error) {
32
-	return []byte(c.String()), nil
27
+func (t TypePort) Get(defaultValue uint) uint {
28
+	if t.Value == 0 {
29
+		return defaultValue
30
+	}
31
+
32
+	return t.Value
33 33
 }
34 34
 
35
-func (c TypePort) String() string {
36
-	return strconv.Itoa(int(c.value))
35
+func (t *TypePort) UnmarshalJSON(data []byte) error {
36
+	return t.Set(string(data))
37 37
 }
38 38
 
39
-func (c TypePort) Value(defaultValue uint) uint {
40
-	if c.value == 0 {
41
-		return defaultValue
42
-	}
39
+func (t TypePort) MarshalJSON() ([]byte, error) {
40
+	return []byte(t.String()), nil
41
+}
43 42
 
44
-	return c.value
43
+func (t TypePort) String() string {
44
+	return strconv.Itoa(int(t.Value))
45 45
 }

+ 24
- 70
internal/config/type_port_test.go Zobrazit soubor

@@ -2,7 +2,6 @@ package config_test
2 2
 
3 3
 import (
4 4
 	"encoding/json"
5
-	"strconv"
6 5
 	"testing"
7 6
 
8 7
 	"github.com/9seconds/mtg/v2/internal/config"
@@ -18,97 +17,52 @@ type TypePortTestSuite struct {
18 17
 	suite.Suite
19 18
 }
20 19
 
21
-func (suite *TypePortTestSuite) TestUnmarshalNil() {
22
-	typ := &config.TypePort{}
23
-	suite.NoError(typ.UnmarshalJSON(nil))
24
-	suite.Equal("0", typ.String())
25
-}
26
-
27 20
 func (suite *TypePortTestSuite) TestUnmarshalFail() {
28
-	testData := []int{
29
-		-1,
30
-		1_000_000,
21
+	testData := []string{
22
+		"",
23
+		"port",
24
+		"0",
25
+		"-1",
26
+		"1.5",
27
+		"70000",
31 28
 	}
32 29
 
33 30
 	for _, v := range testData {
34
-		data, err := json.Marshal(map[string]int{
31
+		data, err := json.Marshal(map[string]string{
35 32
 			"value": v,
36 33
 		})
37 34
 		suite.NoError(err)
38 35
 
39
-		suite.T().Run(strconv.Itoa(v), func(t *testing.T) {
36
+		suite.T().Run(v, func(t *testing.T) {
40 37
 			assert.Error(t, json.Unmarshal(data, &typePortTestStruct{}))
41 38
 		})
42 39
 	}
43 40
 }
44 41
 
45 42
 func (suite *TypePortTestSuite) TestUnmarshalOk() {
46
-	testData := []int{
47
-		1,
48
-		1_000,
49
-		65535,
50
-	}
51
-
52
-	for _, v := range testData {
53
-		value := v
54
-
55
-		data, err := json.Marshal(map[string]int{
56
-			"value": v,
57
-		})
58
-		suite.NoError(err)
59
-
60
-		suite.T().Run(strconv.Itoa(v), func(t *testing.T) {
61
-			testStruct := &typePortTestStruct{}
62
-
63
-			assert.NoError(t, json.Unmarshal(data, testStruct))
64
-			assert.EqualValues(t, value, testStruct.Value.Value(0))
65
-		})
66
-	}
43
+	testStruct := &typePortTestStruct{}
44
+	suite.NoError(json.Unmarshal([]byte(`{"value": 5}`), testStruct))
45
+	suite.EqualValues(5, testStruct.Value.Value)
67 46
 }
68 47
 
69 48
 func (suite *TypePortTestSuite) TestMarshalOk() {
70
-	testData := map[string]int{
71
-		"1":     1,
72
-		"1000":  1000,
73
-		"65535": 65535,
49
+	testStruct := &typePortTestStruct{
50
+		Value: config.TypePort{
51
+			Value: 10,
52
+		},
74 53
 	}
75 54
 
76
-	for k, v := range testData {
77
-		name := k
78
-		value := v
79
-
80
-		data, err := json.Marshal(map[string]int{
81
-			"value": value,
82
-		})
83
-		suite.NoError(err)
84
-
85
-		suite.T().Run(name, func(t *testing.T) {
86
-			testStruct := &typePortTestStruct{}
87
-
88
-			assert.NoError(t, json.Unmarshal(data, testStruct))
89
-			assert.Equal(t, name, testStruct.Value.String())
90
-
91
-			marshalled, err := testStruct.Value.MarshalJSON()
92
-			assert.NoError(t, err)
93
-			assert.Equal(t, name, string(marshalled))
94
-		})
95
-	}
55
+	data, err := json.Marshal(testStruct)
56
+	suite.NoError(err)
57
+	suite.JSONEq(`{"value":10}`, string(data))
96 58
 }
97 59
 
98
-func (suite *TypePortTestSuite) TestValue() {
99
-	testStruct := &typePortTestStruct{}
100
-
101
-	suite.EqualValues(0, testStruct.Value.Value(0))
102
-	suite.EqualValues(1, testStruct.Value.Value(1))
103
-
104
-	data, err := json.Marshal(map[string]int{
105
-		"value": 5,
106
-	})
107
-	suite.NoError(err)
108
-	suite.NoError(json.Unmarshal(data, testStruct))
60
+func (suite *TypePortTestSuite) TestGet() {
61
+	value := config.TypePort{}
62
+	suite.EqualValues(10, value.Get(10))
109 63
 
110
-	suite.EqualValues(5, testStruct.Value.Value(0))
111
-	suite.EqualValues(5, testStruct.Value.Value(1))
64
+	value.Value = 100
65
+	suite.EqualValues(100, value.Get(10))
112 66
 }
113 67
 
114 68
 func TestTypePort(t *testing.T) {

+ 22
- 21
internal/config/type_prefer_ip.go Zobrazit soubor

@@ -24,38 +24,39 @@ const (
24 24
 )
25 25
 
26 26
 type TypePreferIP struct {
27
-	value string
27
+	Value string
28 28
 }
29 29
 
30
-func (c *TypePreferIP) UnmarshalText(data []byte) error {
31
-	if len(data) == 0 {
32
-		return nil
33
-	}
30
+func (t *TypePreferIP) Set(value string) error {
31
+	value = strings.ToLower(value)
34 32
 
35
-	text := strings.ToLower(string(data))
33
+	switch value {
34
+	case TypePreferIPPreferIPv4, TypePreferIPPreferIPv6,
35
+		TypePreferOnlyIPv4, TypePreferOnlyIPv6:
36
+		t.Value = value
36 37
 
37
-	switch text {
38
-	case TypePreferIPPreferIPv4, TypePreferIPPreferIPv6, TypePreferOnlyIPv4, TypePreferOnlyIPv6:
39
-		c.value = text
38
+		return nil
40 39
 	default:
41
-		return fmt.Errorf("incorrect prefer-ip value: %s", string(data))
40
+		return fmt.Errorf("unsupported ip preference: %s", value)
42 41
 	}
43
-
44
-	return nil
45 42
 }
46 43
 
47
-func (c TypePreferIP) MarshalText() ([]byte, error) {
48
-	return []byte(c.value), nil
44
+func (t *TypePreferIP) Get(defaultValue string) string {
45
+	if t.Value == "" {
46
+		return defaultValue
47
+	}
48
+
49
+	return t.Value
49 50
 }
50 51
 
51
-func (c *TypePreferIP) String() string {
52
-	return c.value
52
+func (t *TypePreferIP) UnmarshalText(data []byte) error {
53
+	return t.Set(string(data))
53 54
 }
54 55
 
55
-func (c *TypePreferIP) Value(defaultValue string) string {
56
-	if c.value == "" {
57
-		return defaultValue
58
-	}
56
+func (t TypePreferIP) MarshalText() ([]byte, error) {
57
+	return []byte(t.String()), nil
58
+}
59 59
 
60
-	return c.value
60
+func (t TypePreferIP) String() string {
61
+	return t.Value
61 62
 }

+ 28
- 57
internal/config/type_prefer_ip_test.go Zobrazit soubor

@@ -18,18 +18,12 @@ type TypePreferIPTestSuite struct {
18 18
 	suite.Suite
19 19
 }
20 20
 
21
-func (suite *TypePreferIPTestSuite) TestUnmarshalNil() {
22
-	typ := &config.TypePreferIP{}
23
-	suite.NoError(typ.UnmarshalText(nil))
24
-	suite.Empty(typ.String())
25
-}
26
-
27 21
 func (suite *TypePreferIPTestSuite) TestUnmarshalFail() {
28 22
 	testData := []string{
29
-		"p",
30
-		"ipv4",
31
-		"onlyipv4",
32
-		"ipv6prefer",
23
+		"",
24
+		"prefer",
25
+		"preferipv4",
26
+		config.TypePreferIPPreferIPv4 + "_",
33 27
 	}
34 28
 
35 29
 	for _, v := range testData {
@@ -50,14 +44,10 @@ func (suite *TypePreferIPTestSuite) TestUnmarshalOk() {
50 44
 		config.TypePreferIPPreferIPv6,
51 45
 		config.TypePreferOnlyIPv4,
52 46
 		config.TypePreferOnlyIPv6,
53
-		strings.ToUpper(config.TypePreferIPPreferIPv4),
54
-		strings.ToUpper(config.TypePreferIPPreferIPv6),
55
-		strings.ToUpper(config.TypePreferOnlyIPv4),
56
-		strings.ToUpper(config.TypePreferOnlyIPv6),
57
-		strings.ToLower(config.TypePreferIPPreferIPv4),
58
-		strings.ToLower(config.TypePreferIPPreferIPv6),
59
-		strings.ToLower(config.TypePreferOnlyIPv4),
60
-		strings.ToLower(config.TypePreferOnlyIPv6),
47
+		strings.ToTitle(config.TypePreferOnlyIPv4),
48
+		strings.ToTitle(config.TypePreferOnlyIPv6),
49
+		strings.ToTitle(config.TypePreferIPPreferIPv4),
50
+		strings.ToTitle(config.TypePreferIPPreferIPv6),
61 51
 	}
62 52
 
63 53
 	for _, v := range testData {
@@ -70,11 +60,8 @@ func (suite *TypePreferIPTestSuite) TestUnmarshalOk() {
70 60
 
71 61
 		suite.T().Run(v, func(t *testing.T) {
72 62
 			testStruct := &typePreferIPTestStruct{}
73
-
74 63
 			assert.NoError(t, json.Unmarshal(data, testStruct))
75
-			assert.EqualValues(t,
76
-				strings.ToLower(value),
77
-				testStruct.Value.Value(config.TypePreferIPPreferIPv4))
64
+			assert.Equal(t, strings.ToLower(value), testStruct.Value.Value)
78 65
 		})
79 66
 	}
80 67
 }
@@ -85,55 +72,39 @@ func (suite *TypePreferIPTestSuite) TestMarshalOk() {
85 72
 		config.TypePreferIPPreferIPv6,
86 73
 		config.TypePreferOnlyIPv4,
87 74
 		config.TypePreferOnlyIPv6,
88
-		strings.ToUpper(config.TypePreferIPPreferIPv4),
89
-		strings.ToUpper(config.TypePreferIPPreferIPv6),
90
-		strings.ToUpper(config.TypePreferOnlyIPv4),
91
-		strings.ToUpper(config.TypePreferOnlyIPv6),
92
-		strings.ToLower(config.TypePreferIPPreferIPv4),
93
-		strings.ToLower(config.TypePreferIPPreferIPv6),
94
-		strings.ToLower(config.TypePreferOnlyIPv4),
95
-		strings.ToLower(config.TypePreferOnlyIPv6),
96 75
 	}
97 76
 
98 77
 	for _, v := range testData {
99 78
 		value := v
100 79
 
101
-		data, err := json.Marshal(map[string]string{
102
-			"value": v,
103
-		})
104
-		suite.NoError(err)
105
-
106 80
 		suite.T().Run(v, func(t *testing.T) {
107
-			testStruct := &typePreferIPTestStruct{}
81
+			testStruct := &typePreferIPTestStruct{
82
+				Value: config.TypePreferIP{
83
+					Value: value,
84
+				},
85
+			}
108 86
 
109
-			assert.NoError(t, json.Unmarshal(data, testStruct))
110
-			assert.Equal(t, strings.ToLower(value), testStruct.Value.String())
87
+			encodedJSON, err := json.Marshal(testStruct)
88
+			assert.NoError(t, err)
111 89
 
112
-			marshalled, err := testStruct.Value.MarshalText()
90
+			expectedJSON, err := json.Marshal(map[string]string{
91
+				"value": value,
92
+			})
113 93
 			assert.NoError(t, err)
114
-			assert.Equal(t, strings.ToLower(value), string(marshalled))
94
+
95
+			assert.JSONEq(t, string(expectedJSON), string(encodedJSON))
115 96
 		})
116 97
 	}
117 98
 }
118 99
 
119
-func (suite *TypePreferIPTestSuite) TestValue() {
120
-	testStruct := &typePreferIPTestStruct{}
121
-
122
-	suite.EqualValues(config.TypePreferIPPreferIPv4,
123
-		testStruct.Value.Value(config.TypePreferIPPreferIPv4))
124
-	suite.EqualValues(config.TypePreferIPPreferIPv6,
125
-		testStruct.Value.Value(config.TypePreferIPPreferIPv6))
126
-
127
-	data, err := json.Marshal(map[string]string{
128
-		"value": config.TypePreferOnlyIPv4,
129
-	})
130
-	suite.NoError(err)
131
-	suite.NoError(json.Unmarshal(data, testStruct))
100
+func (suite *TypePreferIPTestSuite) TestGet() {
101
+	value := config.TypePreferIP{}
102
+	suite.Equal(config.TypePreferIPPreferIPv4,
103
+		value.Get(config.TypePreferIPPreferIPv4))
132 104
 
133
-	suite.EqualValues(config.TypePreferOnlyIPv4,
134
-		testStruct.Value.Value(config.TypePreferOnlyIPv6))
135
-	suite.EqualValues(config.TypePreferOnlyIPv4,
136
-		testStruct.Value.Value(config.TypePreferIPPreferIPv6))
105
+	suite.NoError(value.Set(config.TypePreferIPPreferIPv6))
106
+	suite.Equal(config.TypePreferIPPreferIPv6,
107
+		value.Get(config.TypePreferIPPreferIPv4))
137 108
 }
138 109
 
139 110
 func TestTypePreferIP(t *testing.T) {

+ 61
- 0
internal/config/type_proxy_url.go Zobrazit soubor

@@ -0,0 +1,61 @@
1
+package config
2
+
3
+import (
4
+	"fmt"
5
+	"net"
6
+	"net/url"
7
+)
8
+
9
+const typeProxyURLDefaultSOCKS5Port = "1080"
10
+
11
+type TypeProxyURL struct {
12
+	Value *url.URL
13
+}
14
+
15
+func (t *TypeProxyURL) Set(value string) error {
16
+	parsedURL, err := url.Parse(value)
17
+	if err != nil {
18
+		return fmt.Errorf("value is not corect URL (%s): %w", value, err)
19
+	}
20
+
21
+	if parsedURL.Host == "" {
22
+		return fmt.Errorf("url has to have a schema: %s", value)
23
+	}
24
+
25
+	if parsedURL.Scheme != "socks5" {
26
+		return fmt.Errorf("unsupported schema: %s", parsedURL.Scheme)
27
+	}
28
+
29
+	if _, _, err := net.SplitHostPort(parsedURL.Host); err != nil {
30
+		parsedURL.Host = net.JoinHostPort(parsedURL.Host,
31
+			typeProxyURLDefaultSOCKS5Port)
32
+	}
33
+
34
+	t.Value = parsedURL
35
+
36
+	return nil
37
+}
38
+
39
+func (t *TypeProxyURL) Get(defaultValue *url.URL) *url.URL {
40
+	if t.Value == nil {
41
+		return defaultValue
42
+	}
43
+
44
+	return t.Value
45
+}
46
+
47
+func (t *TypeProxyURL) UnmarshalText(data []byte) error {
48
+	return t.Set(string(data))
49
+}
50
+
51
+func (t TypeProxyURL) MarshalText() ([]byte, error) {
52
+	return []byte(t.String()), nil
53
+}
54
+
55
+func (t TypeProxyURL) String() string {
56
+	if t.Value == nil {
57
+		return ""
58
+	}
59
+
60
+	return t.Value.String()
61
+}

+ 96
- 0
internal/config/type_proxy_url_test.go Zobrazit soubor

@@ -0,0 +1,96 @@
1
+package config_test
2
+
3
+import (
4
+	"encoding/json"
5
+	"net/url"
6
+	"testing"
7
+
8
+	"github.com/9seconds/mtg/v2/internal/config"
9
+	"github.com/stretchr/testify/assert"
10
+	"github.com/stretchr/testify/suite"
11
+)
12
+
13
+type typeProxyURLTestStruct struct {
14
+	Value config.TypeProxyURL `json:"value"`
15
+}
16
+
17
+type ProxyURLTestSuite struct {
18
+	suite.Suite
19
+}
20
+
21
+func (suite *ProxyURLTestSuite) TestUnmarshalFail() {
22
+	testData := []string{
23
+		"",
24
+		"socks5://",
25
+		"://lala",
26
+		"/path",
27
+	}
28
+
29
+	for _, v := range testData {
30
+		data, err := json.Marshal(map[string]string{
31
+			"value": v,
32
+		})
33
+		suite.NoError(err)
34
+
35
+		suite.T().Run(v, func(t *testing.T) {
36
+			assert.Error(t, json.Unmarshal(data, &typeProxyURLTestStruct{}))
37
+		})
38
+	}
39
+}
40
+
41
+func (suite *ProxyURLTestSuite) TestUnmarshalOk() {
42
+	testData := map[string]string{
43
+		"socks5://127.0.0.1/?open_threshold=1": "socks5://127.0.0.1:1080/?open_threshold=1",
44
+		"socks5://127.0.0.1:80":                "socks5://127.0.0.1:80",
45
+	}
46
+
47
+	for k, v := range testData {
48
+		value := v
49
+
50
+		data, err := json.Marshal(map[string]string{
51
+			"value": k,
52
+		})
53
+		suite.NoError(err)
54
+
55
+		suite.T().Run(k, func(t *testing.T) {
56
+			testStruct := &typeProxyURLTestStruct{}
57
+			assert.NoError(t, json.Unmarshal(data, testStruct))
58
+
59
+			parsed, _ := url.Parse(value)
60
+
61
+			assert.Equal(t, parsed.Scheme, testStruct.Value.Get(nil).Scheme)
62
+			assert.Equal(t, parsed.Host, testStruct.Value.Get(nil).Host)
63
+			assert.Equal(t, parsed.RawQuery, testStruct.Value.Get(nil).RawQuery)
64
+			assert.Equal(t, parsed.Path, testStruct.Value.Get(nil).Path)
65
+		})
66
+	}
67
+}
68
+
69
+func (suite *ProxyURLTestSuite) TestMarshalOk() {
70
+	parsed, _ := url.Parse("socks5://127.0.0.1:1080?open_threshold=1")
71
+	testStruct := &typeProxyURLTestStruct{
72
+		Value: config.TypeProxyURL{
73
+			Value: parsed,
74
+		},
75
+	}
76
+
77
+	encodedJSON, err := json.Marshal(testStruct)
78
+	suite.NoError(err)
79
+	suite.JSONEq(`{"value": "socks5://127.0.0.1:1080?open_threshold=1"}`,
80
+		string(encodedJSON))
81
+}
82
+
83
+func (suite *ProxyURLTestSuite) TestGet() {
84
+	emptyURL := &url.URL{}
85
+
86
+	value := config.TypeProxyURL{}
87
+	suite.Equal(emptyURL, value.Get(emptyURL))
88
+
89
+	value.Value = &url.URL{}
90
+	suite.Equal(value.Value, value.Get(emptyURL))
91
+}
92
+
93
+func TestTypeProxyURL(t *testing.T) {
94
+	t.Parallel()
95
+	suite.Run(t, &ProxyURLTestSuite{})
96
+}

+ 22
- 21
internal/config/type_statsd_tag_format.go Zobrazit soubor

@@ -20,38 +20,39 @@ const (
20 20
 )
21 21
 
22 22
 type TypeStatsdTagFormat struct {
23
-	value string
23
+	Value string
24 24
 }
25 25
 
26
-func (c *TypeStatsdTagFormat) UnmarshalText(data []byte) error {
27
-	if len(data) == 0 {
28
-		return nil
29
-	}
26
+func (t *TypeStatsdTagFormat) Set(value string) error {
27
+	lowercasedValue := strings.ToLower(value)
30 28
 
31
-	text := strings.ToLower(string(data))
29
+	switch lowercasedValue {
30
+	case TypeStatsdTagFormatDatadog, TypeStatsdTagFormatInfluxdb,
31
+		TypeStatsdTagFormatGraphite:
32
+		t.Value = lowercasedValue
32 33
 
33
-	switch text {
34
-	case TypeStatsdTagFormatInfluxdb, TypeStatsdTagFormatDatadog, TypeStatsdTagFormatGraphite:
35
-		c.value = text
34
+		return nil
36 35
 	default:
37
-		return fmt.Errorf("incorrect tag format value: %s", string(data))
36
+		return fmt.Errorf("unknown tag format %s", value)
38 37
 	}
39
-
40
-	return nil
41 38
 }
42 39
 
43
-func (c TypeStatsdTagFormat) MarshalText() ([]byte, error) {
44
-	return []byte(c.value), nil
40
+func (t TypeStatsdTagFormat) Get(defaultValue string) string {
41
+	if t.Value == "" {
42
+		return defaultValue
43
+	}
44
+
45
+	return t.Value
45 46
 }
46 47
 
47
-func (c *TypeStatsdTagFormat) String() string {
48
-	return c.value
48
+func (t *TypeStatsdTagFormat) UnmarshalText(data []byte) error {
49
+	return t.Set(string(data))
49 50
 }
50 51
 
51
-func (c *TypeStatsdTagFormat) Value(defaultValue string) string {
52
-	if c.value == "" {
53
-		return defaultValue
54
-	}
52
+func (t *TypeStatsdTagFormat) MarshalText() ([]byte, error) {
53
+	return []byte(t.String()), nil
54
+}
55 55
 
56
-	return c.value
56
+func (t *TypeStatsdTagFormat) String() string {
57
+	return t.Value
57 58
 }

+ 30
- 58
internal/config/type_statsd_tag_format_test.go Zobrazit soubor

@@ -14,22 +14,14 @@ type typeStatsdTagFormatTestStruct struct {
14 14
 	Value config.TypeStatsdTagFormat `json:"value"`
15 15
 }
16 16
 
17
-type TypeStatsdTagFormatTestSuite struct {
17
+type StatsdTagFormatTestSuite struct {
18 18
 	suite.Suite
19 19
 }
20 20
 
21
-func (suite *TypeStatsdTagFormatTestSuite) TestUnmarshalNil() {
22
-	typ := &config.TypeStatsdTagFormat{}
23
-	suite.NoError(typ.UnmarshalText(nil))
24
-	suite.Equal("lalala", typ.Value("lalala"))
25
-}
26
-
27
-func (suite *TypeStatsdTagFormatTestSuite) TestUnmarshalFail() {
21
+func (suite *StatsdTagFormatTestSuite) TestUnmarshalFail() {
28 22
 	testData := []string{
29
-		"p",
30
-		"ipv4",
31
-		"onlyipv4",
32
-		"ipv6prefer",
23
+		"",
24
+		"dogdog",
33 25
 	}
34 26
 
35 27
 	for _, v := range testData {
@@ -44,17 +36,14 @@ func (suite *TypeStatsdTagFormatTestSuite) TestUnmarshalFail() {
44 36
 	}
45 37
 }
46 38
 
47
-func (suite *TypeStatsdTagFormatTestSuite) TestUnmarshalOk() {
39
+func (suite *StatsdTagFormatTestSuite) TestUnmarshalOk() {
48 40
 	testData := []string{
49
-		config.TypeStatsdTagFormatDatadog,
50 41
 		config.TypeStatsdTagFormatInfluxdb,
51 42
 		config.TypeStatsdTagFormatGraphite,
52
-		strings.ToUpper(config.TypeStatsdTagFormatDatadog),
43
+		config.TypeStatsdTagFormatDatadog,
53 44
 		strings.ToUpper(config.TypeStatsdTagFormatInfluxdb),
54 45
 		strings.ToUpper(config.TypeStatsdTagFormatGraphite),
55
-		strings.ToLower(config.TypeStatsdTagFormatDatadog),
56
-		strings.ToLower(config.TypeStatsdTagFormatInfluxdb),
57
-		strings.ToLower(config.TypeStatsdTagFormatGraphite),
46
+		strings.ToUpper(config.TypeStatsdTagFormatDatadog),
58 47
 	}
59 48
 
60 49
 	for _, v := range testData {
@@ -67,70 +56,53 @@ func (suite *TypeStatsdTagFormatTestSuite) TestUnmarshalOk() {
67 56
 
68 57
 		suite.T().Run(v, func(t *testing.T) {
69 58
 			testStruct := &typeStatsdTagFormatTestStruct{}
70
-
71 59
 			assert.NoError(t, json.Unmarshal(data, testStruct))
72
-			assert.EqualValues(t,
73
-				strings.ToLower(value),
74
-				testStruct.Value.Value(config.TypeStatsdTagFormatDatadog))
60
+			assert.Equal(t, strings.ToLower(value), testStruct.Value.Value)
75 61
 		})
76 62
 	}
77 63
 }
78 64
 
79
-func (suite *TypeStatsdTagFormatTestSuite) TestMarshalOk() {
65
+func (suite *StatsdTagFormatTestSuite) TestMarshalOk() {
80 66
 	testData := []string{
81
-		config.TypeStatsdTagFormatDatadog,
82 67
 		config.TypeStatsdTagFormatInfluxdb,
83 68
 		config.TypeStatsdTagFormatGraphite,
84
-		strings.ToUpper(config.TypeStatsdTagFormatDatadog),
85
-		strings.ToUpper(config.TypeStatsdTagFormatInfluxdb),
86
-		strings.ToUpper(config.TypeStatsdTagFormatGraphite),
87
-		strings.ToLower(config.TypeStatsdTagFormatDatadog),
88
-		strings.ToLower(config.TypeStatsdTagFormatInfluxdb),
89
-		strings.ToLower(config.TypeStatsdTagFormatGraphite),
69
+		config.TypeStatsdTagFormatDatadog,
90 70
 	}
91 71
 
92 72
 	for _, v := range testData {
93 73
 		value := v
94 74
 
95
-		data, err := json.Marshal(map[string]string{
96
-			"value": v,
97
-		})
98
-		suite.NoError(err)
99
-
100 75
 		suite.T().Run(v, func(t *testing.T) {
101
-			testStruct := &typeStatsdTagFormatTestStruct{}
76
+			testStruct := &typeStatsdTagFormatTestStruct{
77
+				Value: config.TypeStatsdTagFormat{
78
+					Value: value,
79
+				},
80
+			}
102 81
 
103
-			assert.NoError(t, json.Unmarshal(data, testStruct))
104
-			assert.Equal(t, strings.ToLower(value), testStruct.Value.String())
82
+			encodedJSON, err := json.Marshal(testStruct)
83
+			assert.NoError(t, err)
105 84
 
106
-			marshalled, err := testStruct.Value.MarshalText()
85
+			expectedJSON, err := json.Marshal(map[string]string{
86
+				"value": value,
87
+			})
107 88
 			assert.NoError(t, err)
108
-			assert.Equal(t, strings.ToLower(value), string(marshalled))
89
+
90
+			assert.JSONEq(t, string(expectedJSON), string(encodedJSON))
109 91
 		})
110 92
 	}
111 93
 }
112 94
 
113
-func (suite *TypeStatsdTagFormatTestSuite) TestValue() {
114
-	testStruct := &typePreferIPTestStruct{}
115
-
116
-	suite.EqualValues(config.TypePreferIPPreferIPv4,
117
-		testStruct.Value.Value(config.TypePreferIPPreferIPv4))
118
-	suite.EqualValues(config.TypePreferIPPreferIPv6,
119
-		testStruct.Value.Value(config.TypePreferIPPreferIPv6))
120
-
121
-	data, err := json.Marshal(map[string]string{
122
-		"value": config.TypePreferOnlyIPv4,
123
-	})
124
-	suite.NoError(err)
125
-	suite.NoError(json.Unmarshal(data, testStruct))
95
+func (suite *StatsdTagFormatTestSuite) TestGet() {
96
+	value := config.TypeStatsdTagFormat{}
97
+	suite.Equal(config.TypeStatsdTagFormatDatadog,
98
+		value.Get(config.TypeStatsdTagFormatDatadog))
126 99
 
127
-	suite.EqualValues(config.TypePreferOnlyIPv4,
128
-		testStruct.Value.Value(config.TypePreferOnlyIPv6))
129
-	suite.EqualValues(config.TypePreferOnlyIPv4,
130
-		testStruct.Value.Value(config.TypePreferIPPreferIPv6))
100
+	suite.NoError(value.Set(config.TypeStatsdTagFormatInfluxdb))
101
+	suite.Equal(config.TypeStatsdTagFormatInfluxdb,
102
+		value.Get(config.TypeStatsdTagFormatDatadog))
131 103
 }
132 104
 
133 105
 func TestTypeStatsdTagFormat(t *testing.T) {
134 106
 	t.Parallel()
135
-	suite.Run(t, &TypeStatsdTagFormatTestSuite{})
107
+	suite.Run(t, &StatsdTagFormatTestSuite{})
136 108
 }

+ 0
- 71
internal/config/type_url.go Zobrazit soubor

@@ -1,71 +0,0 @@
1
-package config
2
-
3
-import (
4
-	"fmt"
5
-	"net"
6
-	"net/url"
7
-)
8
-
9
-type TypeURL struct {
10
-	value *url.URL
11
-}
12
-
13
-func (c *TypeURL) UnmarshalText(data []byte) error { // nolint: cyclop
14
-	if len(data) == 0 {
15
-		return nil
16
-	}
17
-
18
-	value, err := url.Parse(string(data))
19
-	if err != nil {
20
-		return fmt.Errorf("incorrect URL: %w", err)
21
-	}
22
-
23
-	switch value.Scheme {
24
-	case "http", "https", "socks5":
25
-	case "":
26
-		return fmt.Errorf("url %s has to have a schema", value)
27
-	default:
28
-		return fmt.Errorf("unsupported schema %s", value.Scheme)
29
-	}
30
-
31
-	if value.Host == "" {
32
-		return fmt.Errorf("url %s has to have a host", value)
33
-	}
34
-
35
-	if _, _, err := net.SplitHostPort(value.Host); err != nil {
36
-		switch value.Scheme {
37
-		case "http":
38
-			value.Host = net.JoinHostPort(value.Host, "80")
39
-		case "https":
40
-			value.Host = net.JoinHostPort(value.Host, "443")
41
-		case "socks5":
42
-			value.Host = net.JoinHostPort(value.Host, "1080")
43
-		default:
44
-			return fmt.Errorf("cannot set a default port for %s", value)
45
-		}
46
-	}
47
-
48
-	c.value = value
49
-
50
-	return nil
51
-}
52
-
53
-func (c *TypeURL) MarshalText() ([]byte, error) {
54
-	return []byte(c.String()), nil
55
-}
56
-
57
-func (c TypeURL) String() string {
58
-	if c.value == nil {
59
-		return ""
60
-	}
61
-
62
-	return c.value.String()
63
-}
64
-
65
-func (c TypeURL) Value(defaultValue *url.URL) *url.URL {
66
-	if c.value == nil {
67
-		return defaultValue
68
-	}
69
-
70
-	return c.value
71
-}

+ 0
- 107
internal/config/type_url_test.go Zobrazit soubor

@@ -1,107 +0,0 @@
1
-package config_test
2
-
3
-import (
4
-	"encoding/json"
5
-	"net/url"
6
-	"testing"
7
-
8
-	"github.com/9seconds/mtg/v2/internal/config"
9
-	"github.com/stretchr/testify/assert"
10
-	"github.com/stretchr/testify/suite"
11
-)
12
-
13
-type typeURLTestStruct struct {
14
-	Value config.TypeURL `json:"value"`
15
-}
16
-
17
-type TypeURLTestSuite struct {
18
-	suite.Suite
19
-}
20
-
21
-func (suite *TypeURLTestSuite) TestUnmarshalNil() {
22
-	u, _ := url.Parse("https://google.com")
23
-
24
-	typ := &config.TypeURL{}
25
-	suite.NoError(typ.UnmarshalText(nil))
26
-	suite.Empty(typ.String())
27
-	suite.Equal("https://google.com", typ.Value(u).String())
28
-}
29
-
30
-func (suite *TypeURLTestSuite) TestUnmarshalFail() {
31
-	testData := []string{
32
-		"http:/aaa.com",
33
-		"ipv4",
34
-		"111",
35
-		"://111",
36
-		"http://aaa.com:xxx",
37
-		"gopher://aaa.com:888",
38
-		"gopher://aaa.com",
39
-	}
40
-
41
-	for _, v := range testData {
42
-		data, err := json.Marshal(map[string]string{
43
-			"value": v,
44
-		})
45
-		suite.NoError(err)
46
-
47
-		suite.T().Run(v, func(t *testing.T) {
48
-			assert.Error(t, json.Unmarshal(data, &typeURLTestStruct{}))
49
-		})
50
-	}
51
-}
52
-
53
-func (suite *TypeURLTestSuite) TestUnmarshalOk() {
54
-	testData := map[string]string{
55
-		"https://10.0.0.10:80":    "https://10.0.0.10:80",
56
-		"https://10.0.0.10:443":   "https://10.0.0.10",
57
-		"http://10.0.0.10:8":      "http://10.0.0.10:8",
58
-		"http://10.0.0.10:80":     "http://10.0.0.10",
59
-		"socks5://10.0.0.10:1080": "socks5://10.0.0.10",
60
-		"socks5://10.0.0.10:888":  "socks5://10.0.0.10:888",
61
-	}
62
-
63
-	for k, v := range testData {
64
-		expected := k
65
-		actual := v
66
-
67
-		data, err := json.Marshal(map[string]string{
68
-			"value": actual,
69
-		})
70
-		suite.NoError(err)
71
-
72
-		suite.T().Run(actual, func(t *testing.T) {
73
-			testStruct := &typeURLTestStruct{}
74
-
75
-			assert.NoError(t, json.Unmarshal(data, testStruct))
76
-			assert.Equal(t, expected, testStruct.Value.Value(nil).String())
77
-
78
-			marshalled, err := testStruct.Value.MarshalText()
79
-			assert.NoError(t, err)
80
-			assert.Equal(t, expected, string(marshalled))
81
-		})
82
-	}
83
-}
84
-
85
-func (suite *TypeURLTestSuite) TestValue() {
86
-	testStruct := &typeURLTestStruct{}
87
-
88
-	u1, _ := url.Parse("https://10.0.0.10:80")
89
-	u2, _ := url.Parse("https://10.1.0.10:80")
90
-
91
-	suite.Equal("https://10.0.0.10:80", testStruct.Value.Value(u1).String())
92
-	suite.Equal("https://10.1.0.10:80", testStruct.Value.Value(u2).String())
93
-
94
-	data, err := json.Marshal(map[string]string{
95
-		"value": "http://127.0.0.1:80",
96
-	})
97
-	suite.NoError(err)
98
-	suite.NoError(json.Unmarshal(data, testStruct))
99
-
100
-	suite.Equal("http://127.0.0.1:80", testStruct.Value.Value(u1).String())
101
-	suite.Equal("http://127.0.0.1:80", testStruct.Value.Value(u2).String())
102
-}
103
-
104
-func TestTypeURL(t *testing.T) {
105
-	t.Parallel()
106
-	suite.Run(t, &TypeURLTestSuite{})
107
-}

+ 19
- 0
internal/utils/make_qr_code_url.go Zobrazit soubor

@@ -0,0 +1,19 @@
1
+package utils
2
+
3
+import "net/url"
4
+
5
+func MakeQRCodeURL(data string) string {
6
+	values := url.Values{}
7
+	values.Set("qzone", "4")
8
+	values.Set("format", "svg")
9
+	values.Set("data", data)
10
+
11
+	rv := url.URL{
12
+		Scheme:   "https",
13
+		Host:     "api.qrserver.com",
14
+		Path:     "/v1/create-qr-code",
15
+		RawQuery: values.Encode(),
16
+	}
17
+
18
+	return rv.String()
19
+}

+ 31
- 0
internal/utils/make_qr_code_url_test.go Zobrazit soubor

@@ -0,0 +1,31 @@
1
+package utils_test
2
+
3
+import (
4
+	"net/url"
5
+	"strings"
6
+	"testing"
7
+
8
+	"github.com/9seconds/mtg/v2/internal/utils"
9
+	"github.com/stretchr/testify/suite"
10
+)
11
+
12
+type MakeQRCodeURLTestSuite struct {
13
+	suite.Suite
14
+}
15
+
16
+func (suite *MakeQRCodeURLTestSuite) TestSomeData() {
17
+	value := utils.MakeQRCodeURL("some data")
18
+
19
+	parsed, err := url.Parse(value)
20
+	suite.NoError(err)
21
+
22
+	suite.Equal("some data", parsed.Query().Get("data"))
23
+	suite.Equal("svg", parsed.Query().Get("format"))
24
+	suite.Equal("api.qrserver.com", strings.TrimPrefix(parsed.Host, "www."))
25
+	suite.Equal("v1/create-qr-code", strings.Trim(parsed.Path, "/"))
26
+}
27
+
28
+func TestMakeQRCodeURL(t *testing.T) {
29
+	t.Parallel()
30
+	suite.Run(t, &MakeQRCodeURLTestSuite{})
31
+}

+ 26
- 0
internal/utils/read_config.go Zobrazit soubor

@@ -0,0 +1,26 @@
1
+package utils
2
+
3
+import (
4
+	"fmt"
5
+	"os"
6
+
7
+	"github.com/9seconds/mtg/v2/internal/config"
8
+)
9
+
10
+func ReadConfig(path string) (*config.Config, error) {
11
+	content, err := os.ReadFile(path)
12
+	if err != nil {
13
+		return nil, fmt.Errorf("cannot read config file: %w", err)
14
+	}
15
+
16
+	conf, err := config.Parse(content)
17
+	if err != nil {
18
+		return nil, fmt.Errorf("cannot parse config: %w", err)
19
+	}
20
+
21
+	if err := conf.Validate(); err != nil {
22
+		return nil, fmt.Errorf("invalid config: %w", err)
23
+	}
24
+
25
+	return conf, nil
26
+}

+ 55
- 0
internal/utils/read_config_test.go Zobrazit soubor

@@ -0,0 +1,55 @@
1
+package utils_test
2
+
3
+import (
4
+	"path/filepath"
5
+	"testing"
6
+
7
+	"github.com/9seconds/mtg/v2/internal/utils"
8
+	"github.com/stretchr/testify/suite"
9
+)
10
+
11
+type ReadConfigTestSuite struct {
12
+	suite.Suite
13
+}
14
+
15
+func (suite *ReadConfigTestSuite) GetConfigPath(filename string) string {
16
+	return filepath.Join("testdata", filename)
17
+}
18
+
19
+func (suite *ReadConfigTestSuite) TestReadMinimal() {
20
+	conf, err := utils.ReadConfig(suite.GetConfigPath("minimal.toml"))
21
+	suite.NoError(err)
22
+	suite.NoError(conf.Validate())
23
+	suite.Equal("0.0.0.0:80", conf.BindTo.Get(""))
24
+	suite.Equal("7mqFMMq3P2Tvvt_rPx5qhmFnb29nbGUuY29t", conf.Secret.Base64())
25
+}
26
+
27
+func (suite *ReadConfigTestSuite) TestReadAbsentFile() {
28
+	_, err := utils.ReadConfig(suite.GetConfigPath("unknown.file"))
29
+	suite.Error(err)
30
+}
31
+
32
+func (suite *ReadConfigTestSuite) TestBrokenFile() {
33
+	_, err := utils.ReadConfig(suite.GetConfigPath("broken.toml"))
34
+	suite.Error(err)
35
+}
36
+
37
+func (suite *ReadConfigTestSuite) TestMissedBindTo() {
38
+	_, err := utils.ReadConfig(suite.GetConfigPath("missed-bindto.toml"))
39
+	suite.Error(err)
40
+}
41
+
42
+func (suite *ReadConfigTestSuite) TestMissedSecret() {
43
+	_, err := utils.ReadConfig(suite.GetConfigPath("missed-secret.toml"))
44
+	suite.Error(err)
45
+}
46
+
47
+func (suite *ReadConfigTestSuite) TestEmpty() {
48
+	_, err := utils.ReadConfig(suite.GetConfigPath("empty.toml"))
49
+	suite.Error(err)
50
+}
51
+
52
+func TestReadConfig(t *testing.T) {
53
+	t.Parallel()
54
+	suite.Run(t, &ReadConfigTestSuite{})
55
+}

+ 1
- 0
internal/utils/testdata/broken.toml Zobrazit soubor

@@ -0,0 +1 @@
1
+11

+ 0
- 0
internal/utils/testdata/empty.toml Zobrazit soubor


internal/cli/testdata/minimal.toml → internal/utils/testdata/minimal.toml Zobrazit soubor


+ 2
- 0
internal/utils/testdata/missed-bindto.toml Zobrazit soubor

@@ -0,0 +1,2 @@
1
+
2
+secret = "7mqFMMq3P2Tvvt_rPx5qhmFnb29nbGUuY29t"

+ 1
- 0
internal/utils/testdata/missed-secret.toml Zobrazit soubor

@@ -0,0 +1 @@
1
+bind-to = "0.0.0.0:80"

+ 9
- 9
mtglib/proxy_opts.go Zobrazit soubor

@@ -55,15 +55,6 @@ type ProxyOpts struct {
55 55
 	// This is an optional setting.
56 56
 	Concurrency uint
57 57
 
58
-	// DomainFrontingPort is a port we use to connect to a fronting
59
-	// domain.
60
-	//
61
-	// This is required because secret does not specify a port. It
62
-	// specifies a hostname only.
63
-	//
64
-	// This is an optional setting.
65
-	DomainFrontingPort uint
66
-
67 58
 	// IdleTimeout is a timeout for relay when we have to break a
68 59
 	// stream.
69 60
 	//
@@ -90,6 +81,15 @@ type ProxyOpts struct {
90 81
 	// This is an optional setting.
91 82
 	PreferIP string
92 83
 
84
+	// DomainFrontingPort is a port we use to connect to a fronting
85
+	// domain.
86
+	//
87
+	// This is required because secret does not specify a port. It
88
+	// specifies a hostname only.
89
+	//
90
+	// This is an optional setting.
91
+	DomainFrontingPort uint
92
+
93 93
 	// UseTestDCs defines if we have to connect to production or to staging
94 94
 	// DCs of Telegram.
95 95
 	//

+ 4
- 1
mtglib/secret.go Zobrazit soubor

@@ -58,7 +58,10 @@ func (s Secret) MarshalText() ([]byte, error) {
58 58
 
59 59
 // UnmarshalText is to support text.Unmarshaller interface.
60 60
 func (s *Secret) UnmarshalText(data []byte) error {
61
-	text := string(data)
61
+	return s.Set(string(data))
62
+}
63
+
64
+func (s *Secret) Set(text string) error {
62 65
 	if text == "" {
63 66
 		return ErrSecretEmpty
64 67
 	}

Načítá se…
Zrušit
Uložit