Add EventIPBlocklisted

This commit is contained in:
9seconds
2021-03-17 21:24:24 +03:00
parent 23519913f2
commit 2408f1530f
15 changed files with 128 additions and 2 deletions
+9
View File
@@ -31,3 +31,12 @@ type EventConcurrencyLimited struct {
func (e EventConcurrencyLimited) StreamID() string {
return ""
}
type EventIPBlocklisted struct {
CreatedAt time.Time
RemoteIP net.IP
}
func (e EventIPBlocklisted) StreamID() string {
return ""
}
+4
View File
@@ -36,6 +36,10 @@ func (suite *EventsTestSuite) TestEventConcurrencyLimited() {
suite.Empty(mtglib.EventConcurrencyLimited{}.StreamID())
}
func (suite *EventsTestSuite) TestEventIPBlocklisted() {
suite.Empty(mtglib.EventIPBlocklisted{}.StreamID())
}
func TestEvents(t *testing.T) {
t.Parallel()
suite.Run(t, &EventsTestSuite{})
+10
View File
@@ -52,6 +52,16 @@ func (p *Proxy) Serve(listener net.Listener) error {
return fmt.Errorf("cannot accept a new connection: %w", err)
}
if addr := conn.RemoteAddr().(*net.TCPAddr).IP; p.ipBlocklist.Contains(addr) {
conn.Close()
p.eventStream.Send(p.ctx, EventIPBlocklisted{
CreatedAt: time.Now(),
RemoteIP: addr,
})
continue
}
err = p.workerPool.Invoke(conn)
switch {