Просмотр исходного кода

Propagate doppelganger to config

tags/v2.2.0^2^2
9seconds 1 месяц назад
Родитель
Сommit
25ad776b6f
4 измененных файлов: 165 добавлений и 2 удалений
  1. 7
    2
      internal/config/config.go
  2. 5
    0
      internal/config/parse.go
  3. 53
    0
      internal/config/type_https_url.go
  4. 100
    0
      internal/config/type_https_url_test.go

+ 7
- 2
internal/config/config.go Просмотреть файл

47
 			MaxSize   TypeBytes     `json:"maxSize"`
47
 			MaxSize   TypeBytes     `json:"maxSize"`
48
 			ErrorRate TypeErrorRate `json:"errorRate"`
48
 			ErrorRate TypeErrorRate `json:"errorRate"`
49
 		} `json:"antiReplay"`
49
 		} `json:"antiReplay"`
50
-		Blocklist ListConfig `json:"blocklist"`
51
-		Allowlist ListConfig `json:"allowlist"`
50
+		Blocklist    ListConfig `json:"blocklist"`
51
+		Allowlist    ListConfig `json:"allowlist"`
52
+		Doppelganger struct {
53
+			URLs       []TypeHttpsURL  `json:"urls"`
54
+			Repeats    TypeConcurrency `json:"repeats_per_raid"`
55
+			UpdateEach TypeDuration    `json:"raid_each"`
56
+		} `json:"doppelganger"`
52
 	} `json:"defense"`
57
 	} `json:"defense"`
53
 	Network struct {
58
 	Network struct {
54
 		Timeout struct {
59
 		Timeout struct {

+ 5
- 0
internal/config/parse.go Просмотреть файл

44
 			URLs                []string `toml:"urls" json:"urls,omitempty"`
44
 			URLs                []string `toml:"urls" json:"urls,omitempty"`
45
 			UpdateEach          string   `toml:"update-each" json:"updateEach,omitempty"`
45
 			UpdateEach          string   `toml:"update-each" json:"updateEach,omitempty"`
46
 		} `toml:"allowlist" json:"allowlist,omitempty"`
46
 		} `toml:"allowlist" json:"allowlist,omitempty"`
47
+		Doppelganger struct {
48
+			URLs       []string `toml:"urls" json:"urls,omitempty"`
49
+			Repeats    uint     `toml:"repeats-per-raid" json:"repeats_per_raid,omitempty"`
50
+			UpdateEach string   `toml:"raid-each" json:"raid_each,omitempty"`
51
+		} `toml:"doppelganger" json:"doppelganger,omitempty"`
47
 	} `toml:"defense" json:"defense,omitempty"`
52
 	} `toml:"defense" json:"defense,omitempty"`
48
 	Network struct {
53
 	Network struct {
49
 		Timeout struct {
54
 		Timeout struct {

+ 53
- 0
internal/config/type_https_url.go Просмотреть файл

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

+ 100
- 0
internal/config/type_https_url_test.go Просмотреть файл

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 typeHttpsURLTestStruct struct {
14
+	Value config.TypeHttpsURL `json:"value"`
15
+}
16
+
17
+type HttpsURLTestSuite struct {
18
+	suite.Suite
19
+}
20
+
21
+func (suite *HttpsURLTestSuite) TestUnmarshalFail() {
22
+	testData := []string{
23
+		"",
24
+		"https://",
25
+		"://lala",
26
+		"/path",
27
+		"http://example.com",
28
+		"socks5://example.com",
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, &typeHttpsURLTestStruct{}))
39
+		})
40
+	}
41
+}
42
+
43
+func (suite *HttpsURLTestSuite) TestUnmarshalOk() {
44
+	testData := map[string]string{
45
+		"https://example.com":            "https://example.com",
46
+		"https://example.com:8443":       "https://example.com:8443",
47
+		"https://example.com/path?q=1":   "https://example.com/path?q=1",
48
+		"https://user:pass@example.com":  "https://user:pass@example.com",
49
+	}
50
+
51
+	for k, v := range testData {
52
+		value := v
53
+
54
+		data, err := json.Marshal(map[string]string{
55
+			"value": k,
56
+		})
57
+		suite.NoError(err)
58
+
59
+		suite.T().Run(k, func(t *testing.T) {
60
+			testStruct := &typeHttpsURLTestStruct{}
61
+			assert.NoError(t, json.Unmarshal(data, testStruct))
62
+
63
+			parsed, _ := url.Parse(value)
64
+
65
+			assert.Equal(t, parsed.Scheme, testStruct.Value.Get(nil).Scheme)
66
+			assert.Equal(t, parsed.Host, testStruct.Value.Get(nil).Host)
67
+			assert.Equal(t, parsed.RawQuery, testStruct.Value.Get(nil).RawQuery)
68
+			assert.Equal(t, parsed.Path, testStruct.Value.Get(nil).Path)
69
+		})
70
+	}
71
+}
72
+
73
+func (suite *HttpsURLTestSuite) TestMarshalOk() {
74
+	parsed, _ := url.Parse("https://example.com/path?q=1")
75
+	testStruct := &typeHttpsURLTestStruct{
76
+		Value: config.TypeHttpsURL{
77
+			Value: parsed,
78
+		},
79
+	}
80
+
81
+	encodedJSON, err := json.Marshal(testStruct)
82
+	suite.NoError(err)
83
+	suite.JSONEq(`{"value": "https://example.com/path?q=1"}`,
84
+		string(encodedJSON))
85
+}
86
+
87
+func (suite *HttpsURLTestSuite) TestGet() {
88
+	emptyURL := &url.URL{}
89
+
90
+	value := config.TypeHttpsURL{}
91
+	suite.Equal(emptyURL, value.Get(emptyURL))
92
+
93
+	value.Value = &url.URL{}
94
+	suite.Equal(value.Value, value.Get(emptyURL))
95
+}
96
+
97
+func TestTypeHttpsURL(t *testing.T) {
98
+	t.Parallel()
99
+	suite.Run(t, &HttpsURLTestSuite{})
100
+}

Загрузка…
Отмена
Сохранить