Sfoglia il codice sorgente

Rework events

tags/v2.0.0-rc1
9seconds 5 anni fa
parent
commit
463af648ce

+ 17
- 49
events/event_stream_test.go Vedi File

@@ -39,11 +39,7 @@ func (suite *EventStreamTestSuite) SetupTest() {
39 39
 }
40 40
 
41 41
 func (suite *EventStreamTestSuite) TestEventStart() {
42
-	evt := mtglib.EventStart{
43
-		CreatedAt: time.Now(),
44
-		ConnID:    "connID",
45
-		RemoteIP:  net.ParseIP("10.0.0.1"),
46
-	}
42
+	evt := mtglib.NewEventStart("connID", net.ParseIP("10.0.0.1"))
47 43
 
48 44
 	for _, v := range []*ObserverMock{suite.observerMock1, suite.observerMock2} {
49 45
 		v.
@@ -52,10 +48,9 @@ func (suite *EventStreamTestSuite) TestEventStart() {
52 48
 			Run(func(args mock.Arguments) {
53 49
 				caught := args.Get(0).(mtglib.EventStart)
54 50
 
55
-				suite.Equal(evt.CreatedAt, caught.CreatedAt)
56
-				suite.Equal(evt.ConnID, caught.ConnID)
57 51
 				suite.Equal(evt.RemoteIP.String(), caught.RemoteIP.String())
58 52
 				suite.Equal(evt.StreamID(), caught.StreamID())
53
+				suite.Equal(evt.Timestamp(), caught.Timestamp())
59 54
 			})
60 55
 	}
61 56
 
@@ -64,12 +59,7 @@ func (suite *EventStreamTestSuite) TestEventStart() {
64 59
 }
65 60
 
66 61
 func (suite *EventStreamTestSuite) TestEventConnectedToDC() {
67
-	evt := mtglib.EventConnectedToDC{
68
-		CreatedAt: time.Now(),
69
-		ConnID:    "connID",
70
-		RemoteIP:  net.ParseIP("10.0.0.1"),
71
-		DC:        3,
72
-	}
62
+	evt := mtglib.NewEventConnectedToDC("connID", net.ParseIP("10.0.0.1"), 3)
73 63
 
74 64
 	for _, v := range []*ObserverMock{suite.observerMock1, suite.observerMock2} {
75 65
 		v.
@@ -78,11 +68,10 @@ func (suite *EventStreamTestSuite) TestEventConnectedToDC() {
78 68
 			Run(func(args mock.Arguments) {
79 69
 				caught := args.Get(0).(mtglib.EventConnectedToDC)
80 70
 
81
-				suite.Equal(evt.CreatedAt, caught.CreatedAt)
82
-				suite.Equal(evt.ConnID, caught.ConnID)
83 71
 				suite.Equal(evt.RemoteIP.String(), caught.RemoteIP.String())
84 72
 				suite.Equal(evt.StreamID(), caught.StreamID())
85 73
 				suite.Equal(evt.DC, caught.DC)
74
+				suite.Equal(evt.Timestamp(), caught.Timestamp())
86 75
 			})
87 76
 	}
88 77
 
@@ -91,10 +80,7 @@ func (suite *EventStreamTestSuite) TestEventConnectedToDC() {
91 80
 }
92 81
 
93 82
 func (suite *EventStreamTestSuite) TestEventDomainFronting() {
94
-	evt := mtglib.EventDomainFronting{
95
-		CreatedAt: time.Now(),
96
-		ConnID:    "connID",
97
-	}
83
+	evt := mtglib.NewEventDomainFronting("connID")
98 84
 
99 85
 	for _, v := range []*ObserverMock{suite.observerMock1, suite.observerMock2} {
100 86
 		v.
@@ -103,9 +89,8 @@ func (suite *EventStreamTestSuite) TestEventDomainFronting() {
103 89
 			Run(func(args mock.Arguments) {
104 90
 				caught := args.Get(0).(mtglib.EventDomainFronting)
105 91
 
106
-				suite.Equal(evt.CreatedAt, caught.CreatedAt)
107
-				suite.Equal(evt.ConnID, caught.ConnID)
108 92
 				suite.Equal(evt.StreamID(), caught.StreamID())
93
+				suite.Equal(evt.Timestamp(), caught.Timestamp())
109 94
 			})
110 95
 	}
111 96
 
@@ -114,12 +99,7 @@ func (suite *EventStreamTestSuite) TestEventDomainFronting() {
114 99
 }
115 100
 
116 101
 func (suite *EventStreamTestSuite) TestEventTraffic() {
117
-	evt := mtglib.EventTraffic{
118
-		CreatedAt: time.Now(),
119
-		ConnID:    "connID",
120
-		Traffic:   1024,
121
-		IsRead:    true,
122
-	}
102
+	evt := mtglib.NewEventTraffic("connID", 1024, true)
123 103
 
124 104
 	for _, v := range []*ObserverMock{suite.observerMock1, suite.observerMock2} {
125 105
 		v.
@@ -128,9 +108,8 @@ func (suite *EventStreamTestSuite) TestEventTraffic() {
128 108
 			Run(func(args mock.Arguments) {
129 109
 				caught := args.Get(0).(mtglib.EventTraffic)
130 110
 
131
-				suite.Equal(evt.CreatedAt, caught.CreatedAt)
132
-				suite.Equal(evt.ConnID, caught.ConnID)
133 111
 				suite.Equal(evt.StreamID(), caught.StreamID())
112
+				suite.Equal(evt.Timestamp(), caught.Timestamp())
134 113
 				suite.Equal(evt.Traffic, caught.Traffic)
135 114
 				suite.Equal(evt.IsRead, caught.IsRead)
136 115
 			})
@@ -141,10 +120,7 @@ func (suite *EventStreamTestSuite) TestEventTraffic() {
141 120
 }
142 121
 
143 122
 func (suite *EventStreamTestSuite) TestEventFinish() {
144
-	evt := mtglib.EventFinish{
145
-		CreatedAt: time.Now(),
146
-		ConnID:    "connID",
147
-	}
123
+	evt := mtglib.NewEventFinish("connID")
148 124
 
149 125
 	for _, v := range []*ObserverMock{suite.observerMock1, suite.observerMock2} {
150 126
 		v.
@@ -153,9 +129,8 @@ func (suite *EventStreamTestSuite) TestEventFinish() {
153 129
 			Run(func(args mock.Arguments) {
154 130
 				caught := args.Get(0).(mtglib.EventFinish)
155 131
 
156
-				suite.Equal(evt.CreatedAt, caught.CreatedAt)
157
-				suite.Equal(evt.ConnID, caught.ConnID)
158 132
 				suite.Equal(evt.StreamID(), caught.StreamID())
133
+				suite.Equal(evt.Timestamp(), caught.Timestamp())
159 134
 			})
160 135
 	}
161 136
 
@@ -164,9 +139,7 @@ func (suite *EventStreamTestSuite) TestEventFinish() {
164 139
 }
165 140
 
166 141
 func (suite *EventStreamTestSuite) TestEventConcurrencyLimited() {
167
-	evt := mtglib.EventConcurrencyLimited{
168
-		CreatedAt: time.Now(),
169
-	}
142
+	evt := mtglib.NewEventConcurrencyLimited()
170 143
 
171 144
 	for _, v := range []*ObserverMock{suite.observerMock1, suite.observerMock2} {
172 145
 		v.
@@ -175,7 +148,8 @@ func (suite *EventStreamTestSuite) TestEventConcurrencyLimited() {
175 148
 			Run(func(args mock.Arguments) {
176 149
 				caught := args.Get(0).(mtglib.EventConcurrencyLimited)
177 150
 
178
-				suite.Equal(evt.CreatedAt, caught.CreatedAt)
151
+				suite.Equal(evt.Timestamp(), caught.Timestamp())
152
+				suite.Empty(evt.StreamID())
179 153
 			})
180 154
 	}
181 155
 
@@ -184,10 +158,7 @@ func (suite *EventStreamTestSuite) TestEventConcurrencyLimited() {
184 158
 }
185 159
 
186 160
 func (suite *EventStreamTestSuite) TestEventIPBlocklisted() {
187
-	evt := mtglib.EventIPBlocklisted{
188
-		CreatedAt: time.Now(),
189
-		RemoteIP:  net.ParseIP("10.0.0.10"),
190
-	}
161
+	evt := mtglib.NewEventIPBlocklisted(net.ParseIP("10.0.0.10"))
191 162
 
192 163
 	for _, v := range []*ObserverMock{suite.observerMock1, suite.observerMock2} {
193 164
 		v.
@@ -196,8 +167,8 @@ func (suite *EventStreamTestSuite) TestEventIPBlocklisted() {
196 167
 			Run(func(args mock.Arguments) {
197 168
 				caught := args.Get(0).(mtglib.EventIPBlocklisted)
198 169
 
199
-				suite.Equal(evt.CreatedAt, caught.CreatedAt)
200 170
 				suite.Equal(evt.StreamID(), caught.StreamID())
171
+				suite.Equal(evt.Timestamp(), caught.Timestamp())
201 172
 				suite.Equal(evt.RemoteIP.String(), caught.RemoteIP.String())
202 173
 			})
203 174
 	}
@@ -207,10 +178,7 @@ func (suite *EventStreamTestSuite) TestEventIPBlocklisted() {
207 178
 }
208 179
 
209 180
 func (suite *EventStreamTestSuite) TestEventReplayAttack() {
210
-	evt := mtglib.EventReplayAttack{
211
-		CreatedAt: time.Now(),
212
-		ConnID:    "CONNID",
213
-	}
181
+	evt := mtglib.NewEventReplayAttack("CONNID")
214 182
 
215 183
 	for _, v := range []*ObserverMock{suite.observerMock1, suite.observerMock2} {
216 184
 		v.
@@ -219,8 +187,8 @@ func (suite *EventStreamTestSuite) TestEventReplayAttack() {
219 187
 			Run(func(args mock.Arguments) {
220 188
 				caught := args.Get(0).(mtglib.EventReplayAttack)
221 189
 
222
-				suite.Equal(evt.CreatedAt, caught.CreatedAt)
223 190
 				suite.Equal(evt.StreamID(), caught.StreamID())
191
+				suite.Equal(evt.Timestamp(), caught.Timestamp())
224 192
 			})
225 193
 	}
226 194
 

+ 8
- 37
events/noop_test.go Vedi File

@@ -4,7 +4,6 @@ import (
4 4
 	"context"
5 5
 	"net"
6 6
 	"testing"
7
-	"time"
8 7
 
9 8
 	"github.com/9seconds/mtg/v2/events"
10 9
 	"github.com/9seconds/mtg/v2/mtglib"
@@ -20,42 +19,14 @@ type NoopTestSuite struct {
20 19
 
21 20
 func (suite *NoopTestSuite) SetupSuite() {
22 21
 	suite.testData = map[string]mtglib.Event{
23
-		"start": mtglib.EventStart{
24
-			CreatedAt: time.Now(),
25
-			ConnID:    "connID",
26
-			RemoteIP:  net.ParseIP("127.0.0.1"),
27
-		},
28
-		"connected-to-dc": mtglib.EventConnectedToDC{
29
-			CreatedAt: time.Now(),
30
-			ConnID:    "connID",
31
-			RemoteIP:  net.ParseIP("127.1.0.1"),
32
-			DC:        2,
33
-		},
34
-		"domain-fronting": mtglib.EventDomainFronting{
35
-			CreatedAt: time.Now(),
36
-			ConnID:    "connID",
37
-		},
38
-		"traffic": mtglib.EventTraffic{
39
-			CreatedAt: time.Now(),
40
-			ConnID:    "connID",
41
-			Traffic:   1000,
42
-			IsRead:    true,
43
-		},
44
-		"finish": mtglib.EventFinish{
45
-			CreatedAt: time.Now(),
46
-			ConnID:    "connID",
47
-		},
48
-		"concurrency-limited": mtglib.EventConcurrencyLimited{
49
-			CreatedAt: time.Now(),
50
-		},
51
-		"ip-blacklisted": mtglib.EventIPBlocklisted{
52
-			RemoteIP:  net.ParseIP("10.0.0.10"),
53
-			CreatedAt: time.Now(),
54
-		},
55
-		"replay-attack": mtglib.EventReplayAttack{
56
-			CreatedAt: time.Now(),
57
-			ConnID:    "connID",
58
-		},
22
+		"start":               mtglib.NewEventStart("connID", net.ParseIP("127.0.0.1")),
23
+		"connected-to-dc":     mtglib.NewEventConnectedToDC("connID", net.ParseIP("127.1.0.1"), 2),
24
+		"domain-fronting":     mtglib.NewEventDomainFronting("connID"),
25
+		"traffic":             mtglib.NewEventTraffic("connID", 1000, true),
26
+		"finish":              mtglib.NewEventFinish("connID"),
27
+		"concurrency-limited": mtglib.NewEventConcurrencyLimited(),
28
+		"ip-blacklisted":      mtglib.NewEventIPBlocklisted(net.ParseIP("10.0.0.10")),
29
+		"replay-attack":       mtglib.NewEventReplayAttack("connID"),
59 30
 	}
60 31
 	suite.ctx = context.Background()
61 32
 }

+ 5
- 16
mtglib/conns.go Vedi File

@@ -6,27 +6,21 @@ import (
6 6
 	"io"
7 7
 	"net"
8 8
 	"sync"
9
-	"time"
10 9
 )
11 10
 
12 11
 type connTraffic struct {
13 12
 	net.Conn
14 13
 
15
-	connID string
16
-	stream EventStream
17
-	ctx    context.Context
14
+	streamID string
15
+	stream   EventStream
16
+	ctx      context.Context
18 17
 }
19 18
 
20 19
 func (c connTraffic) Read(b []byte) (int, error) {
21 20
 	n, err := c.Conn.Read(b)
22 21
 
23 22
 	if n > 0 {
24
-		c.stream.Send(c.ctx, EventTraffic{
25
-			CreatedAt: time.Now(),
26
-			ConnID:    c.connID,
27
-			Traffic:   uint(n),
28
-			IsRead:    true,
29
-		})
23
+		c.stream.Send(c.ctx, NewEventTraffic(c.streamID, uint(n), true))
30 24
 	}
31 25
 
32 26
 	return n, err // nolint: wrapcheck
@@ -36,12 +30,7 @@ func (c connTraffic) Write(b []byte) (int, error) {
36 30
 	n, err := c.Conn.Write(b)
37 31
 
38 32
 	if n > 0 {
39
-		c.stream.Send(c.ctx, EventTraffic{
40
-			CreatedAt: time.Now(),
41
-			ConnID:    c.connID,
42
-			Traffic:   uint(n),
43
-			IsRead:    false,
44
-		})
33
+		c.stream.Send(c.ctx, NewEventTraffic(c.streamID, uint(n), false))
45 34
 	}
46 35
 
47 36
 	return n, err // nolint: wrapcheck

+ 4
- 4
mtglib/conns_internal_test.go Vedi File

@@ -37,10 +37,10 @@ func (suite *ConnTrafficTestSuite) SetupTest() {
37 37
 	suite.eventStreamMock = &EventStreamMock{}
38 38
 	suite.connMock = &testlib.NetConnMock{}
39 39
 	suite.conn = connTraffic{
40
-		Conn:   suite.connMock,
41
-		connID: "CONNID",
42
-		ctx:    context.Background(),
43
-		stream: suite.eventStreamMock,
40
+		Conn:     suite.connMock,
41
+		streamID: "CONNID",
42
+		ctx:      context.Background(),
43
+		stream:   suite.eventStreamMock,
44 44
 	}
45 45
 }
46 46
 

+ 100
- 77
mtglib/events.go Vedi File

@@ -5,110 +5,133 @@ import (
5 5
 	"time"
6 6
 )
7 7
 
8
-type EventStart struct {
9
-	CreatedAt time.Time
10
-	ConnID    string
11
-	RemoteIP  net.IP
8
+type eventBase struct {
9
+	streamID  string
10
+	timestamp time.Time
12 11
 }
13 12
 
14
-func (e EventStart) StreamID() string {
15
-	return e.ConnID
13
+func (e eventBase) StreamID() string {
14
+	return e.streamID
16 15
 }
17 16
 
18
-func (e EventStart) Timestamp() time.Time {
19
-	return e.CreatedAt
17
+func (e eventBase) Timestamp() time.Time {
18
+	return e.timestamp
20 19
 }
21 20
 
22
-type EventConnectedToDC struct {
23
-	CreatedAt time.Time
24
-	ConnID    string
25
-	RemoteIP  net.IP
26
-	DC        int
27
-}
21
+type EventStart struct {
22
+	eventBase
28 23
 
29
-func (e EventConnectedToDC) StreamID() string {
30
-	return e.ConnID
24
+	RemoteIP net.IP
31 25
 }
32 26
 
33
-func (e EventConnectedToDC) Timestamp() time.Time {
34
-	return e.CreatedAt
35
-}
27
+type EventConnectedToDC struct {
28
+	eventBase
36 29
 
37
-type EventTraffic struct {
38
-	CreatedAt time.Time
39
-	ConnID    string
40
-	Traffic   uint
41
-	IsRead    bool
30
+	RemoteIP net.IP
31
+	DC       int
42 32
 }
43 33
 
44
-func (e EventTraffic) StreamID() string {
45
-	return e.ConnID
46
-}
34
+type EventTraffic struct {
35
+	eventBase
47 36
 
48
-func (e EventTraffic) Timestamp() time.Time {
49
-	return e.CreatedAt
37
+	Traffic uint
38
+	IsRead  bool
50 39
 }
51 40
 
52 41
 type EventFinish struct {
53
-	CreatedAt time.Time
54
-	ConnID    string
55
-}
56
-
57
-func (e EventFinish) StreamID() string {
58
-	return e.ConnID
59
-}
60
-
61
-func (e EventFinish) Timestamp() time.Time {
62
-	return e.CreatedAt
42
+	eventBase
63 43
 }
64 44
 
65 45
 type EventDomainFronting struct {
66
-	CreatedAt time.Time
67
-	ConnID    string
68
-}
69
-
70
-func (e EventDomainFronting) StreamID() string {
71
-	return e.ConnID
72
-}
73
-
74
-func (e EventDomainFronting) Timestamp() time.Time {
75
-	return e.CreatedAt
46
+	eventBase
76 47
 }
77 48
 
78 49
 type EventConcurrencyLimited struct {
79
-	CreatedAt time.Time
80
-}
81
-
82
-func (e EventConcurrencyLimited) StreamID() string {
83
-	return ""
84
-}
85
-
86
-func (e EventConcurrencyLimited) Timestamp() time.Time {
87
-	return e.CreatedAt
50
+	eventBase
88 51
 }
89 52
 
90 53
 type EventIPBlocklisted struct {
91
-	CreatedAt time.Time
92
-	RemoteIP  net.IP
93
-}
94
-
95
-func (e EventIPBlocklisted) StreamID() string {
96
-	return ""
97
-}
54
+	eventBase
98 55
 
99
-func (e EventIPBlocklisted) Timestamp() time.Time {
100
-	return e.CreatedAt
56
+	RemoteIP net.IP
101 57
 }
102 58
 
103 59
 type EventReplayAttack struct {
104
-	CreatedAt time.Time
105
-	ConnID    string
106
-}
107
-
108
-func (e EventReplayAttack) StreamID() string {
109
-	return e.ConnID
110
-}
111
-
112
-func (e EventReplayAttack) Timestamp() time.Time {
113
-	return e.CreatedAt
60
+	eventBase
61
+}
62
+
63
+func NewEventStart(streamID string, remoteIP net.IP) EventStart {
64
+	return EventStart{
65
+		eventBase: eventBase{
66
+			timestamp: time.Now(),
67
+			streamID:  streamID,
68
+		},
69
+		RemoteIP: remoteIP,
70
+	}
71
+}
72
+
73
+func NewEventConnectedToDC(streamID string, remoteIP net.IP, dc int) EventConnectedToDC {
74
+	return EventConnectedToDC{
75
+		eventBase: eventBase{
76
+			timestamp: time.Now(),
77
+			streamID:  streamID,
78
+		},
79
+		RemoteIP: remoteIP,
80
+		DC:       dc,
81
+	}
82
+}
83
+
84
+func NewEventTraffic(streamID string, traffic uint, isRead bool) EventTraffic {
85
+	return EventTraffic{
86
+		eventBase: eventBase{
87
+			timestamp: time.Now(),
88
+			streamID:  streamID,
89
+		},
90
+		Traffic: traffic,
91
+		IsRead:  isRead,
92
+	}
93
+}
94
+
95
+func NewEventFinish(streamID string) EventFinish {
96
+	return EventFinish{
97
+		eventBase: eventBase{
98
+			timestamp: time.Now(),
99
+			streamID:  streamID,
100
+		},
101
+	}
102
+}
103
+
104
+func NewEventDomainFronting(streamID string) EventDomainFronting {
105
+	return EventDomainFronting{
106
+		eventBase: eventBase{
107
+			timestamp: time.Now(),
108
+			streamID:  streamID,
109
+		},
110
+	}
111
+}
112
+
113
+func NewEventConcurrencyLimited() EventConcurrencyLimited {
114
+	return EventConcurrencyLimited{
115
+		eventBase: eventBase{
116
+			timestamp: time.Now(),
117
+		},
118
+	}
119
+}
120
+
121
+func NewEventIPBlocklisted(remoteIP net.IP) EventIPBlocklisted {
122
+	return EventIPBlocklisted{
123
+		eventBase: eventBase{
124
+			timestamp: time.Now(),
125
+		},
126
+		RemoteIP: remoteIP,
127
+	}
128
+}
129
+
130
+func NewEventReplayAttack(streamID string) EventReplayAttack {
131
+	return EventReplayAttack{
132
+		eventBase: eventBase{
133
+			timestamp: time.Now(),
134
+			streamID:  streamID,
135
+		},
136
+	}
114 137
 }

+ 8
- 36
mtglib/events_test.go Vedi File

@@ -14,84 +14,56 @@ type EventsTestSuite struct {
14 14
 }
15 15
 
16 16
 func (suite *EventsTestSuite) TestEventStart() {
17
-	evt := mtglib.EventStart{
18
-		CreatedAt: time.Now(),
19
-		ConnID:    "CONNID",
20
-		RemoteIP:  net.ParseIP("10.0.0.10"),
21
-	}
17
+	evt := mtglib.NewEventStart("CONNID", net.ParseIP("10.0.0.10"))
22 18
 
23 19
 	suite.Equal("CONNID", evt.StreamID())
24 20
 	suite.WithinDuration(time.Now(), evt.Timestamp(), 10*time.Millisecond)
25 21
 }
26 22
 
27 23
 func (suite *EventsTestSuite) TestEventFinish() {
28
-	evt := mtglib.EventFinish{
29
-		CreatedAt: time.Now(),
30
-		ConnID:    "CONNID",
31
-	}
24
+	evt := mtglib.NewEventFinish("CONNID")
32 25
 
33 26
 	suite.Equal("CONNID", evt.StreamID())
34 27
 	suite.WithinDuration(time.Now(), evt.Timestamp(), 10*time.Millisecond)
35 28
 }
36 29
 
37 30
 func (suite *EventsTestSuite) TestEventConnectedToDC() {
38
-	evt := mtglib.EventConnectedToDC{
39
-		CreatedAt: time.Now(),
40
-		ConnID:    "CONNID",
41
-		RemoteIP:  net.ParseIP("10.0.0.10"),
42
-		DC:        3,
43
-	}
31
+	evt := mtglib.NewEventConnectedToDC("CONNID", net.ParseIP("10.0.0.10"), 3)
44 32
 
45 33
 	suite.Equal("CONNID", evt.StreamID())
46 34
 	suite.WithinDuration(time.Now(), evt.Timestamp(), 10*time.Millisecond)
47 35
 }
48 36
 
49 37
 func (suite *EventsTestSuite) TestEventTraffic() {
50
-	evt := mtglib.EventTraffic{
51
-		CreatedAt: time.Now(),
52
-		ConnID:    "CONNID",
53
-		Traffic:   3,
54
-		IsRead:    true,
55
-	}
38
+	evt := mtglib.NewEventTraffic("CONNID", 1000, true)
56 39
 
57 40
 	suite.Equal("CONNID", evt.StreamID())
58 41
 	suite.WithinDuration(time.Now(), evt.Timestamp(), 10*time.Millisecond)
59 42
 }
60 43
 
61 44
 func (suite *EventsTestSuite) TestEventDomainFronting() {
62
-	evt := mtglib.EventDomainFronting{
63
-		CreatedAt: time.Now(),
64
-		ConnID:    "CONNID",
65
-	}
45
+	evt := mtglib.NewEventDomainFronting("CONNID")
66 46
 
67 47
 	suite.Equal("CONNID", evt.StreamID())
68 48
 	suite.WithinDuration(time.Now(), evt.Timestamp(), 10*time.Millisecond)
69 49
 }
70 50
 
71 51
 func (suite *EventsTestSuite) TestEventConcurrencyLimited() {
72
-	evt := mtglib.EventConcurrencyLimited{
73
-		CreatedAt: time.Now(),
74
-	}
52
+	evt := mtglib.NewEventConcurrencyLimited()
75 53
 
76 54
 	suite.Empty(evt.StreamID())
77 55
 	suite.WithinDuration(time.Now(), evt.Timestamp(), 10*time.Millisecond)
78 56
 }
79 57
 
80 58
 func (suite *EventsTestSuite) TestEventIPBlocklisted() {
81
-	evt := mtglib.EventIPBlocklisted{
82
-		CreatedAt: time.Now(),
83
-		RemoteIP:  net.ParseIP("10.0.0.10"),
84
-	}
59
+	evt := mtglib.NewEventIPBlocklisted(net.ParseIP("10.0.0.10"))
85 60
 
86 61
 	suite.Empty(evt.StreamID())
87 62
 	suite.WithinDuration(time.Now(), evt.Timestamp(), 10*time.Millisecond)
88 63
 }
89 64
 
90 65
 func (suite *EventsTestSuite) TestEventReplayAttack() {
91
-	evt := mtglib.EventReplayAttack{
92
-		CreatedAt: time.Now(),
93
-		ConnID:    "CONNID",
94
-	}
66
+	evt := mtglib.NewEventReplayAttack("CONNID")
95 67
 
96 68
 	suite.Equal("CONNID", evt.StreamID())
97 69
 	suite.WithinDuration(time.Now(), evt.Timestamp(), 10*time.Millisecond)

+ 71
- 11
mtglib/init.go Vedi File

@@ -9,22 +9,66 @@ import (
9 9
 )
10 10
 
11 11
 var (
12
-	ErrSecretEmpty                    = errors.New("secret is empty")
13
-	ErrSecretInvalid                  = errors.New("secret is invalid")
14
-	ErrNetworkIsNotDefined            = errors.New("network is not defined")
15
-	ErrAntiReplayCacheIsNotDefined    = errors.New("anti-replay cache is not defined")
12
+	// ErrSecretEmpty is returned if you are trying to create a proxy
13
+	// but do not provide a secret.
14
+	ErrSecretEmpty = errors.New("secret is empty")
15
+
16
+	// ErrSecretInvalid is returned if you are trying to create a proxy
17
+	// but secret value is invalid (no host or payload are zeroes).
18
+	ErrSecretInvalid = errors.New("secret is invalid")
19
+
20
+	// ErrNetworkIsNotDefined is returned if you are trying to create a
21
+	// proxy but network value is undefined.
22
+	ErrNetworkIsNotDefined = errors.New("network is not defined")
23
+
24
+	// ErrAntiReplayCacheIsNotDefined is returned if you are trying to
25
+	// create a proxy but anti replay cache value is undefined.
26
+	ErrAntiReplayCacheIsNotDefined = errors.New("anti-replay cache is not defined")
27
+
28
+	// ErrTimeAttackDetectorIsNotDefined is returned if you are trying to
29
+	// create a proxy but time attack detector is not defined.
16 30
 	ErrTimeAttackDetectorIsNotDefined = errors.New("time attack detector is not defined")
17
-	ErrIPBlocklistIsNotDefined        = errors.New("ip blocklist is not defined")
18
-	ErrEventStreamIsNotDefined        = errors.New("event stream is not defined")
19
-	ErrLoggerIsNotDefined             = errors.New("logger is not defined")
31
+
32
+	// ErrIPBlocklistIsNotDefined is returned if you are trying to
33
+	// create a proxy but ip blocklist instance is not defined.
34
+	ErrIPBlocklistIsNotDefined = errors.New("ip blocklist is not defined")
35
+
36
+	// ErrEventStreamIsNotDefined is returned if you are trying to create a
37
+	// proxy but event stream instance is not defined.
38
+	ErrEventStreamIsNotDefined = errors.New("event stream is not defined")
39
+
40
+	// ErrLoggerIsNotDefined is returned if you are trying to
41
+	// create a proxy but logger is not defined.
42
+	ErrLoggerIsNotDefined = errors.New("logger is not defined")
20 43
 )
21 44
 
22 45
 const (
23
-	DefaultConcurrency        = 4096
24
-	DefaultBufferSize         = 16 * 1024 // 16 kib
46
+	// DefaultConcurrency is a default max count of simultaneously
47
+	// connected clients.
48
+	DefaultConcurrency = 4096
49
+
50
+	// DefaultBufferSize is a default size of a copy buffer.
51
+	DefaultBufferSize = 16 * 1024 // 16 kib
52
+
53
+	// DefaultDomainFrontingPort is a default port (HTTPS) to connect to in
54
+	// case of probe-resistance activity.
25 55
 	DefaultDomainFrontingPort = 443
26
-	DefaultIdleTimeout        = time.Minute
27
-	DefaultPreferIP           = "prefer-ipv6"
56
+
57
+	// DefaultIdleTimeout is a default timeout for closing a connection
58
+	// in case of idling.
59
+	DefaultIdleTimeout = time.Minute
60
+
61
+	// DefaultPreferIP is a default value for Telegram IP connectivity
62
+	// preference.
63
+	DefaultPreferIP = "prefer-ipv6"
64
+
65
+	// SecretKeyLength defines a length of the secret bytes used
66
+	// by Telegram and a proxy.
67
+	SecretKeyLength = 16
68
+
69
+	// ConnectionIDBytesLength defines a count of random bytes
70
+	// used to generate a stream/connection ids.
71
+	ConnectionIDBytesLength = 16
28 72
 )
29 73
 
30 74
 // Network defines a knowledge how to work with a network. It may sound
@@ -105,8 +149,24 @@ type IPBlocklist interface {
105 149
 	Contains(net.IP) bool
106 150
 }
107 151
 
152
+// Event is a data structure which is populated during mtg request
153
+// processing lifecycle. Each request popluates many events:
154
+//
155
+// 1. Client connected
156
+//
157
+// 2. Request is finished
158
+//
159
+// 3. Connection to Telegram server is established
160
+//
161
+// and so on. All these events are data structures but all of them
162
+// must conform the same interface.
108 163
 type Event interface {
164
+	// StreamID returns an identifier of the stream, connection,
165
+	// request, you name it. All events within the same stream returns
166
+	// the same stream id.
109 167
 	StreamID() string
168
+
169
+	// Timestamp returns a timestamp when this event was generated.
110 170
 	Timestamp() time.Time
111 171
 }
112 172
 

+ 16
- 39
mtglib/proxy.go Vedi File

@@ -53,18 +53,11 @@ func (p *Proxy) ServeConn(conn net.Conn) {
53 53
 		ctx.Close()
54 54
 	}()
55 55
 
56
-	p.eventStream.Send(ctx, EventStart{
57
-		CreatedAt: time.Now(),
58
-		ConnID:    ctx.connID,
59
-		RemoteIP:  ctx.ClientIP(),
60
-	})
56
+	p.eventStream.Send(ctx, NewEventStart(ctx.streamID, ctx.ClientIP()))
61 57
 	ctx.logger.Info("Stream has been started")
62 58
 
63 59
 	defer func() {
64
-		p.eventStream.Send(ctx, EventFinish{
65
-			CreatedAt: time.Now(),
66
-			ConnID:    ctx.connID,
67
-		})
60
+		p.eventStream.Send(ctx, NewEventFinish(ctx.streamID))
68 61
 		ctx.logger.Info("Stream has been finished")
69 62
 	}()
70 63
 
@@ -109,10 +102,7 @@ func (p *Proxy) Serve(listener net.Listener) error {
109 102
 		if p.ipBlocklist.Contains(ipAddr) {
110 103
 			conn.Close()
111 104
 			logger.Info("ip was blacklisted")
112
-			p.eventStream.Send(p.ctx, EventIPBlocklisted{
113
-				CreatedAt: time.Now(),
114
-				RemoteIP:  ipAddr,
115
-			})
105
+			p.eventStream.Send(p.ctx, NewEventIPBlocklisted(ipAddr))
116 106
 
117 107
 			continue
118 108
 		}
@@ -125,9 +115,7 @@ func (p *Proxy) Serve(listener net.Listener) error {
125 115
 			return nil
126 116
 		case errors.Is(err, ants.ErrPoolOverload):
127 117
 			logger.Info("connection was concurrency limited")
128
-			p.eventStream.Send(p.ctx, EventConcurrencyLimited{
129
-				CreatedAt: time.Now(),
130
-			})
118
+			p.eventStream.Send(p.ctx, NewEventConcurrencyLimited())
131 119
 		}
132 120
 
133 121
 		select {
@@ -181,10 +169,7 @@ func (p *Proxy) doFakeTLSHandshake(ctx *streamContext) bool {
181 169
 
182 170
 	if p.antiReplayCache.SeenBefore(hello.SessionID) {
183 171
 		p.logger.Warning("replay attack has been detected!")
184
-		p.eventStream.Send(p.ctx, EventReplayAttack{
185
-			CreatedAt: time.Now(),
186
-			ConnID:    ctx.connID,
187
-		})
172
+		p.eventStream.Send(p.ctx, NewEventReplayAttack(ctx.streamID))
188 173
 		p.doDomainFronting(ctx, rewind)
189 174
 
190 175
 		return false
@@ -235,31 +220,23 @@ func (p *Proxy) doTelegramCall(ctx *streamContext) error {
235 220
 
236 221
 	ctx.telegramConn = obfuscated2.Conn{
237 222
 		Conn: connTraffic{
238
-			Conn:   conn,
239
-			connID: ctx.connID,
240
-			stream: p.eventStream,
241
-			ctx:    ctx,
223
+			Conn:     conn,
224
+			streamID: ctx.streamID,
225
+			stream:   p.eventStream,
226
+			ctx:      ctx,
242 227
 		},
243 228
 		Encryptor: encryptor,
244 229
 		Decryptor: decryptor,
245 230
 	}
246 231
 
247
-	p.eventStream.Send(ctx, EventConnectedToDC{
248
-		CreatedAt: time.Now(),
249
-		ConnID:    ctx.connID,
250
-		RemoteIP:  conn.RemoteAddr().(*net.TCPAddr).IP,
251
-		DC:        ctx.dc,
252
-	})
232
+	p.eventStream.Send(ctx,
233
+		NewEventConnectedToDC(ctx.streamID, conn.RemoteAddr().(*net.TCPAddr).IP, ctx.dc))
253 234
 
254 235
 	return nil
255 236
 }
256 237
 
257 238
 func (p *Proxy) doDomainFronting(ctx *streamContext, conn *connRewind) {
258
-	p.eventStream.Send(p.ctx, EventDomainFronting{
259
-		CreatedAt: time.Now(),
260
-		ConnID:    ctx.connID,
261
-	})
262
-
239
+	p.eventStream.Send(p.ctx, NewEventDomainFronting(ctx.streamID))
263 240
 	conn.Rewind()
264 241
 
265 242
 	frontConn, err := p.network.DialContext(ctx, "tcp", p.DomainFrontingAddress())
@@ -270,10 +247,10 @@ func (p *Proxy) doDomainFronting(ctx *streamContext, conn *connRewind) {
270 247
 	}
271 248
 
272 249
 	frontConn = connTraffic{
273
-		Conn:   frontConn,
274
-		ctx:    ctx,
275
-		connID: ctx.connID,
276
-		stream: p.eventStream,
250
+		Conn:     frontConn,
251
+		ctx:      ctx,
252
+		streamID: ctx.streamID,
253
+		stream:   p.eventStream,
277 254
 	}
278 255
 
279 256
 	rel := relay.AcquireRelay(ctx,

+ 1
- 5
mtglib/secret.go Vedi File

@@ -7,11 +7,7 @@ import (
7 7
 	"fmt"
8 8
 )
9 9
 
10
-const (
11
-	SecretKeyLength = 16
12
-
13
-	secretFakeTLSFirstByte byte = 0xee
14
-)
10
+const secretFakeTLSFirstByte byte = 0xee
15 11
 
16 12
 var secretEmptyKey [SecretKeyLength]byte
17 13
 

+ 3
- 5
mtglib/stream_context.go Vedi File

@@ -8,14 +8,12 @@ import (
8 8
 	"time"
9 9
 )
10 10
 
11
-const ConnectionIDBytesLength = 16
12
-
13 11
 type streamContext struct {
14 12
 	ctx          context.Context
15 13
 	ctxCancel    context.CancelFunc
16 14
 	clientConn   net.Conn
17 15
 	telegramConn net.Conn
18
-	connID       string
16
+	streamID     string
19 17
 	dc           int
20 18
 	logger       Logger
21 19
 }
@@ -64,10 +62,10 @@ func newStreamContext(ctx context.Context, logger Logger, clientConn net.Conn) *
64 62
 		ctx:        ctx,
65 63
 		ctxCancel:  cancel,
66 64
 		clientConn: clientConn,
67
-		connID:     base64.RawURLEncoding.EncodeToString(connIDBytes),
65
+		streamID:   base64.RawURLEncoding.EncodeToString(connIDBytes),
68 66
 	}
69 67
 	streamCtx.logger = logger.
70
-		BindStr("stream-id", streamCtx.connID).
68
+		BindStr("stream-id", streamCtx.streamID).
71 69
 		BindStr("client-ip", streamCtx.ClientIP().String())
72 70
 
73 71
 	return streamCtx

+ 21
- 63
stats/prometheus_test.go Vedi File

@@ -55,57 +55,39 @@ func (suite *PrometheusTestSuite) TearDownTest() {
55 55
 }
56 56
 
57 57
 func (suite *PrometheusTestSuite) TestTelegramPath() {
58
-	suite.prometheus.EventStart(mtglib.EventStart{
59
-		CreatedAt: time.Now(),
60
-		ConnID:    "connID",
61
-		RemoteIP:  net.ParseIP("10.0.0.10"),
62
-	})
58
+	suite.prometheus.EventStart(
59
+		mtglib.NewEventStart("connID", net.ParseIP("10.0.0.10")))
63 60
 	time.Sleep(100 * time.Millisecond)
64 61
 
65 62
 	data, err := suite.Get()
66 63
 	suite.NoError(err)
67 64
 	suite.Contains(data, `mtg_client_connections{ip_family="ipv4"} 1`)
68 65
 
69
-	suite.prometheus.EventConnectedToDC(mtglib.EventConnectedToDC{
70
-		CreatedAt: time.Now(),
71
-		ConnID:    "connID",
72
-		RemoteIP:  net.ParseIP("10.0.0.1"),
73
-		DC:        4,
74
-	})
66
+	suite.prometheus.EventConnectedToDC(
67
+		mtglib.NewEventConnectedToDC("connID", net.ParseIP("10.0.0.1"), 4))
75 68
 	time.Sleep(100 * time.Millisecond)
76 69
 
77 70
 	data, err = suite.Get()
78 71
 	suite.NoError(err)
79 72
 	suite.Contains(data, `mtg_telegram_connections{dc="4",telegram_ip="10.0.0.1"} 1`)
80 73
 
81
-	suite.prometheus.EventTraffic(mtglib.EventTraffic{
82
-		CreatedAt: time.Now(),
83
-		ConnID:    "connID",
84
-		Traffic:   200,
85
-		IsRead:    true,
86
-	})
74
+	suite.prometheus.EventTraffic(
75
+		mtglib.NewEventTraffic("connID", 200, true))
87 76
 	time.Sleep(100 * time.Millisecond)
88 77
 
89 78
 	data, err = suite.Get()
90 79
 	suite.NoError(err)
91 80
 	suite.Contains(data, `mtg_telegram_traffic{dc="4",direction="to_client",telegram_ip="10.0.0.1"} 200`)
92 81
 
93
-	suite.prometheus.EventTraffic(mtglib.EventTraffic{
94
-		CreatedAt: time.Now(),
95
-		ConnID:    "connID",
96
-		Traffic:   100,
97
-		IsRead:    false,
98
-	})
82
+	suite.prometheus.EventTraffic(
83
+		mtglib.NewEventTraffic("connID", 100, false))
99 84
 	time.Sleep(100 * time.Millisecond)
100 85
 
101 86
 	data, err = suite.Get()
102 87
 	suite.NoError(err)
103 88
 	suite.Contains(data, `mtg_telegram_traffic{dc="4",direction="from_client",telegram_ip="10.0.0.1"} 100`)
104 89
 
105
-	suite.prometheus.EventFinish(mtglib.EventFinish{
106
-		CreatedAt: time.Now(),
107
-		ConnID:    "connID",
108
-	})
90
+	suite.prometheus.EventFinish(mtglib.NewEventFinish("connID"))
109 91
 	time.Sleep(100 * time.Millisecond)
110 92
 
111 93
 	data, err = suite.Get()
@@ -115,21 +97,15 @@ func (suite *PrometheusTestSuite) TestTelegramPath() {
115 97
 }
116 98
 
117 99
 func (suite *PrometheusTestSuite) TestDomainFrontingPath() {
118
-	suite.prometheus.EventStart(mtglib.EventStart{
119
-		CreatedAt: time.Now(),
120
-		ConnID:    "connID",
121
-		RemoteIP:  net.ParseIP("10.0.0.10"),
122
-	})
100
+	suite.prometheus.EventStart(
101
+		mtglib.NewEventStart("connID", net.ParseIP("10.0.0.10")))
123 102
 	time.Sleep(100 * time.Millisecond)
124 103
 
125 104
 	data, err := suite.Get()
126 105
 	suite.NoError(err)
127 106
 	suite.Contains(data, `mtg_client_connections{ip_family="ipv4"} 1`)
128 107
 
129
-	suite.prometheus.EventDomainFronting(mtglib.EventDomainFronting{
130
-		CreatedAt: time.Now(),
131
-		ConnID:    "connID",
132
-	})
108
+	suite.prometheus.EventDomainFronting(mtglib.NewEventDomainFronting("connID"))
133 109
 	time.Sleep(100 * time.Millisecond)
134 110
 
135 111
 	data, err = suite.Get()
@@ -137,34 +113,23 @@ func (suite *PrometheusTestSuite) TestDomainFrontingPath() {
137 113
 	suite.Contains(data, `mtg_domain_fronting 1`)
138 114
 	suite.Contains(data, `mtg_domain_fronting_connections{ip_family="ipv4"} 1`)
139 115
 
140
-	suite.prometheus.EventTraffic(mtglib.EventTraffic{
141
-		CreatedAt: time.Now(),
142
-		ConnID:    "connID",
143
-		Traffic:   200,
144
-		IsRead:    true,
145
-	})
116
+	suite.prometheus.EventTraffic(
117
+		mtglib.NewEventTraffic("connID", 200, true))
146 118
 	time.Sleep(100 * time.Millisecond)
147 119
 
148 120
 	data, err = suite.Get()
149 121
 	suite.NoError(err)
150 122
 	suite.Contains(data, `mtg_domain_fronting_traffic{direction="to_client"} 200`)
151 123
 
152
-	suite.prometheus.EventTraffic(mtglib.EventTraffic{
153
-		CreatedAt: time.Now(),
154
-		ConnID:    "connID",
155
-		Traffic:   100,
156
-		IsRead:    false,
157
-	})
124
+	suite.prometheus.EventTraffic(
125
+		mtglib.NewEventTraffic("connID", 100, false))
158 126
 	time.Sleep(100 * time.Millisecond)
159 127
 
160 128
 	data, err = suite.Get()
161 129
 	suite.NoError(err)
162 130
 	suite.Contains(data, `mtg_domain_fronting_traffic{direction="from_client"} 100`)
163 131
 
164
-	suite.prometheus.EventFinish(mtglib.EventFinish{
165
-		CreatedAt: time.Now(),
166
-		ConnID:    "connID",
167
-	})
132
+	suite.prometheus.EventFinish(mtglib.NewEventFinish("connID"))
168 133
 	time.Sleep(100 * time.Millisecond)
169 134
 
170 135
 	data, err = suite.Get()
@@ -174,9 +139,7 @@ func (suite *PrometheusTestSuite) TestDomainFrontingPath() {
174 139
 }
175 140
 
176 141
 func (suite *PrometheusTestSuite) TestEventConcurrencyLimited() {
177
-	suite.prometheus.EventConcurrencyLimited(mtglib.EventConcurrencyLimited{
178
-		CreatedAt: time.Now(),
179
-	})
142
+	suite.prometheus.EventConcurrencyLimited(mtglib.NewEventConcurrencyLimited())
180 143
 
181 144
 	time.Sleep(100 * time.Millisecond)
182 145
 
@@ -186,10 +149,8 @@ func (suite *PrometheusTestSuite) TestEventConcurrencyLimited() {
186 149
 }
187 150
 
188 151
 func (suite *PrometheusTestSuite) TestEventIPBlocklisted() {
189
-	suite.prometheus.EventIPBlocklisted(mtglib.EventIPBlocklisted{
190
-		CreatedAt: time.Now(),
191
-		RemoteIP:  net.ParseIP("2001:db8::68"),
192
-	})
152
+	suite.prometheus.EventIPBlocklisted(
153
+		mtglib.NewEventIPBlocklisted(net.ParseIP("2001:db8::68")))
193 154
 
194 155
 	time.Sleep(100 * time.Millisecond)
195 156
 
@@ -199,10 +160,7 @@ func (suite *PrometheusTestSuite) TestEventIPBlocklisted() {
199 160
 }
200 161
 
201 162
 func (suite *PrometheusTestSuite) TestEventReplayAttack() {
202
-	suite.prometheus.EventReplayAttack(mtglib.EventReplayAttack{
203
-		CreatedAt: time.Now(),
204
-		ConnID:    "connID",
205
-	})
163
+	suite.prometheus.EventReplayAttack(mtglib.NewEventReplayAttack("connID"))
206 164
 
207 165
 	time.Sleep(100 * time.Millisecond)
208 166
 

+ 1
- 6
stats/statsd.go Vedi File

@@ -4,7 +4,6 @@ import (
4 4
 	"fmt"
5 5
 	"strconv"
6 6
 	"strings"
7
-	"time"
8 7
 
9 8
 	"github.com/9seconds/mtg/v2/events"
10 9
 	"github.com/9seconds/mtg/v2/logger"
@@ -123,14 +122,10 @@ func (s statsdProcessor) EventReplayAttack(_ mtglib.EventReplayAttack) {
123 122
 }
124 123
 
125 124
 func (s statsdProcessor) Shutdown() {
126
-	now := time.Now()
127 125
 	events := make([]mtglib.EventFinish, 0, len(s.streams))
128 126
 
129 127
 	for k := range s.streams {
130
-		events = append(events, mtglib.EventFinish{
131
-			CreatedAt: now,
132
-			ConnID:    k,
133
-		})
128
+		events = append(events, mtglib.NewEventFinish(k))
134 129
 	}
135 130
 
136 131
 	for i := range events {

+ 21
- 63
stats/statsd_test.go Vedi File

@@ -105,48 +105,30 @@ func (suite *StatsdTestSuite) TearDownTest() {
105 105
 }
106 106
 
107 107
 func (suite *StatsdTestSuite) TestTelegramPath() {
108
-	suite.statsd.EventStart(mtglib.EventStart{
109
-		CreatedAt: time.Now(),
110
-		ConnID:    "connID",
111
-		RemoteIP:  net.ParseIP("10.0.0.10"),
112
-	})
108
+	suite.statsd.EventStart(
109
+		mtglib.NewEventStart("connID", net.ParseIP("10.0.0.10")))
113 110
 	time.Sleep(statsdSleepTime)
114 111
 	suite.Equal("mtg.client_connections:+1|g|#ip_family:ipv4", suite.statsdServer.String())
115 112
 
116
-	suite.statsd.EventConnectedToDC(mtglib.EventConnectedToDC{
117
-		CreatedAt: time.Now(),
118
-		ConnID:    "connID",
119
-		RemoteIP:  net.ParseIP("10.1.0.10"),
120
-		DC:        2,
121
-	})
113
+	suite.statsd.EventConnectedToDC(
114
+		mtglib.NewEventConnectedToDC("connID", net.ParseIP("10.1.0.10"), 2))
122 115
 	time.Sleep(statsdSleepTime)
123 116
 	suite.Contains(suite.statsdServer.String(),
124 117
 		"mtg.telegram_connections:+1|g|#telegram_ip:10.1.0.10,dc:2")
125 118
 
126
-	suite.statsd.EventTraffic(mtglib.EventTraffic{
127
-		CreatedAt: time.Now(),
128
-		ConnID:    "connID",
129
-		Traffic:   30,
130
-		IsRead:    true,
131
-	})
119
+	suite.statsd.EventTraffic(
120
+		mtglib.NewEventTraffic("connID", 30, true))
132 121
 	time.Sleep(statsdSleepTime)
133 122
 	suite.Contains(suite.statsdServer.String(),
134 123
 		"mtg.telegram_traffic:30|c|#telegram_ip:10.1.0.10,dc:2,direction:to_client")
135 124
 
136
-	suite.statsd.EventTraffic(mtglib.EventTraffic{
137
-		CreatedAt: time.Now(),
138
-		ConnID:    "connID",
139
-		Traffic:   90,
140
-		IsRead:    false,
141
-	})
125
+	suite.statsd.EventTraffic(
126
+		mtglib.NewEventTraffic("connID", 90, false))
142 127
 	time.Sleep(statsdSleepTime)
143 128
 	suite.Contains(suite.statsdServer.String(),
144 129
 		"mtg.telegram_traffic:90|c|#telegram_ip:10.1.0.10,dc:2,direction:from_client")
145 130
 
146
-	suite.statsd.EventFinish(mtglib.EventFinish{
147
-		CreatedAt: time.Now(),
148
-		ConnID:    "connID",
149
-	})
131
+	suite.statsd.EventFinish(mtglib.NewEventFinish("connID"))
150 132
 	time.Sleep(statsdSleepTime)
151 133
 	suite.Contains(suite.statsdServer.String(),
152 134
 		"mtg.telegram_connections:-1|g|#telegram_ip:10.1.0.10,dc:2")
@@ -158,47 +140,30 @@ func (suite *StatsdTestSuite) TestTelegramPath() {
158 140
 }
159 141
 
160 142
 func (suite *StatsdTestSuite) TestDomainFrontingPath() {
161
-	suite.statsd.EventStart(mtglib.EventStart{
162
-		CreatedAt: time.Now(),
163
-		ConnID:    "connID",
164
-		RemoteIP:  net.ParseIP("10.0.0.10"),
165
-	})
143
+	suite.statsd.EventStart(
144
+		mtglib.NewEventStart("connID", net.ParseIP("10.0.0.10")))
166 145
 	time.Sleep(statsdSleepTime)
167 146
 	suite.Equal("mtg.client_connections:+1|g|#ip_family:ipv4", suite.statsdServer.String())
168 147
 
169
-	suite.statsd.EventDomainFronting(mtglib.EventDomainFronting{
170
-		CreatedAt: time.Now(),
171
-		ConnID:    "connID",
172
-	})
148
+	suite.statsd.EventDomainFronting(mtglib.NewEventDomainFronting("connID"))
173 149
 	time.Sleep(statsdSleepTime)
174 150
 	suite.Contains(suite.statsdServer.String(), "mtg.domain_fronting:1|c")
175 151
 	suite.Contains(suite.statsdServer.String(),
176 152
 		`mtg.domain_fronting_connections:+1|g|#ip_family:ipv4`)
177 153
 
178
-	suite.statsd.EventTraffic(mtglib.EventTraffic{
179
-		CreatedAt: time.Now(),
180
-		ConnID:    "connID",
181
-		Traffic:   30,
182
-		IsRead:    true,
183
-	})
154
+	suite.statsd.EventTraffic(
155
+		mtglib.NewEventTraffic("connID", 30, true))
184 156
 	time.Sleep(statsdSleepTime)
185 157
 	suite.Contains(suite.statsdServer.String(),
186 158
 		`mtg.domain_fronting_traffic:30|c|#direction:to_client`)
187 159
 
188
-	suite.statsd.EventTraffic(mtglib.EventTraffic{
189
-		CreatedAt: time.Now(),
190
-		ConnID:    "connID",
191
-		Traffic:   90,
192
-		IsRead:    false,
193
-	})
160
+	suite.statsd.EventTraffic(
161
+		mtglib.NewEventTraffic("connID", 90, false))
194 162
 	time.Sleep(statsdSleepTime)
195 163
 	suite.Contains(suite.statsdServer.String(),
196 164
 		`mtg.domain_fronting_traffic:90|c|#direction:from_client`)
197 165
 
198
-	suite.statsd.EventFinish(mtglib.EventFinish{
199
-		CreatedAt: time.Now(),
200
-		ConnID:    "connID",
201
-	})
166
+	suite.statsd.EventFinish(mtglib.NewEventFinish("connID"))
202 167
 	time.Sleep(statsdSleepTime)
203 168
 	suite.Contains(suite.statsdServer.String(),
204 169
 		"mtg.domain_fronting_connections:-1|g|#ip_family:ipv4")
@@ -210,29 +175,22 @@ func (suite *StatsdTestSuite) TestDomainFrontingPath() {
210 175
 }
211 176
 
212 177
 func (suite *StatsdTestSuite) TestEventConcurrencyLimited() {
213
-	suite.statsd.EventConcurrencyLimited(mtglib.EventConcurrencyLimited{
214
-		CreatedAt: time.Now(),
215
-	})
178
+	suite.statsd.EventConcurrencyLimited(mtglib.NewEventConcurrencyLimited())
216 179
 
217 180
 	time.Sleep(statsdSleepTime)
218 181
 	suite.Equal("mtg.concurrency_limited:1|c", suite.statsdServer.String())
219 182
 }
220 183
 
221 184
 func (suite *StatsdTestSuite) TestEventIPBlocklisted() {
222
-	suite.statsd.EventIPBlocklisted(mtglib.EventIPBlocklisted{
223
-		CreatedAt: time.Now(),
224
-		RemoteIP:  net.ParseIP("10.0.0.10"),
225
-	})
185
+	suite.statsd.EventIPBlocklisted(
186
+		mtglib.NewEventIPBlocklisted(net.ParseIP("10.0.0.10")))
226 187
 
227 188
 	time.Sleep(statsdSleepTime)
228 189
 	suite.Equal("mtg.ip_blocklisted:1|c", suite.statsdServer.String())
229 190
 }
230 191
 
231 192
 func (suite *StatsdTestSuite) TestEventReplayAttack() {
232
-	suite.statsd.EventReplayAttack(mtglib.EventReplayAttack{
233
-		CreatedAt: time.Now(),
234
-		ConnID:    "connID",
235
-	})
193
+	suite.statsd.EventReplayAttack(mtglib.NewEventReplayAttack("connID"))
236 194
 
237 195
 	time.Sleep(statsdSleepTime)
238 196
 	suite.Equal("mtg.replay_attacks:1|c", suite.statsdServer.String())

Loading…
Annulla
Salva