mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-09-01 05:54:01 +03:00
Rework stats
This commit is contained in:
+2
-2
@@ -21,7 +21,7 @@ func (c connTelegramTraffic) Read(b []byte) (int, error) {
|
||||
n, err := c.Conn.Read(b)
|
||||
|
||||
if n > 0 {
|
||||
c.stream.Send(c.ctx, EventTraffic{
|
||||
c.stream.Send(c.ctx, EventTelegramTraffic{
|
||||
CreatedAt: time.Now(),
|
||||
ConnID: c.connID,
|
||||
Traffic: uint(n),
|
||||
@@ -36,7 +36,7 @@ func (c connTelegramTraffic) Write(b []byte) (int, error) {
|
||||
n, err := c.Conn.Write(b)
|
||||
|
||||
if n > 0 {
|
||||
c.stream.Send(c.ctx, EventTraffic{
|
||||
c.stream.Send(c.ctx, EventTelegramTraffic{
|
||||
CreatedAt: time.Now(),
|
||||
ConnID: c.connID,
|
||||
Traffic: uint(n),
|
||||
|
||||
+26
-2
@@ -15,6 +15,10 @@ func (e EventStart) StreamID() string {
|
||||
return e.ConnID
|
||||
}
|
||||
|
||||
func (e EventStart) Timestamp() time.Time {
|
||||
return e.CreatedAt
|
||||
}
|
||||
|
||||
type EventConnectedToDC struct {
|
||||
CreatedAt time.Time
|
||||
ConnID string
|
||||
@@ -26,17 +30,25 @@ func (e EventConnectedToDC) StreamID() string {
|
||||
return e.ConnID
|
||||
}
|
||||
|
||||
type EventTraffic struct {
|
||||
func (e EventConnectedToDC) Timestamp() time.Time {
|
||||
return e.CreatedAt
|
||||
}
|
||||
|
||||
type EventTelegramTraffic struct {
|
||||
CreatedAt time.Time
|
||||
ConnID string
|
||||
Traffic uint
|
||||
IsRead bool
|
||||
}
|
||||
|
||||
func (e EventTraffic) StreamID() string {
|
||||
func (e EventTelegramTraffic) StreamID() string {
|
||||
return e.ConnID
|
||||
}
|
||||
|
||||
func (e EventTelegramTraffic) Timestamp() time.Time {
|
||||
return e.CreatedAt
|
||||
}
|
||||
|
||||
type EventFinish struct {
|
||||
CreatedAt time.Time
|
||||
ConnID string
|
||||
@@ -46,6 +58,10 @@ func (e EventFinish) StreamID() string {
|
||||
return e.ConnID
|
||||
}
|
||||
|
||||
func (e EventFinish) Timestamp() time.Time {
|
||||
return e.CreatedAt
|
||||
}
|
||||
|
||||
type EventConcurrencyLimited struct {
|
||||
CreatedAt time.Time
|
||||
}
|
||||
@@ -54,6 +70,10 @@ func (e EventConcurrencyLimited) StreamID() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (e EventConcurrencyLimited) Timestamp() time.Time {
|
||||
return e.CreatedAt
|
||||
}
|
||||
|
||||
type EventIPBlocklisted struct {
|
||||
CreatedAt time.Time
|
||||
RemoteIP net.IP
|
||||
@@ -62,3 +82,7 @@ type EventIPBlocklisted struct {
|
||||
func (e EventIPBlocklisted) StreamID() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (e EventIPBlocklisted) Timestamp() time.Time {
|
||||
return e.CreatedAt
|
||||
}
|
||||
|
||||
+19
-4
@@ -21,6 +21,7 @@ func (suite *EventsTestSuite) TestEventStart() {
|
||||
}
|
||||
|
||||
suite.Equal("CONNID", evt.StreamID())
|
||||
suite.WithinDuration(time.Now(), evt.Timestamp(), 10*time.Millisecond)
|
||||
}
|
||||
|
||||
func (suite *EventsTestSuite) TestEventFinish() {
|
||||
@@ -30,6 +31,7 @@ func (suite *EventsTestSuite) TestEventFinish() {
|
||||
}
|
||||
|
||||
suite.Equal("CONNID", evt.StreamID())
|
||||
suite.WithinDuration(time.Now(), evt.Timestamp(), 10*time.Millisecond)
|
||||
}
|
||||
|
||||
func (suite *EventsTestSuite) TestEventConnectedToDC() {
|
||||
@@ -41,10 +43,11 @@ func (suite *EventsTestSuite) TestEventConnectedToDC() {
|
||||
}
|
||||
|
||||
suite.Equal("CONNID", evt.StreamID())
|
||||
suite.WithinDuration(time.Now(), evt.Timestamp(), 10*time.Millisecond)
|
||||
}
|
||||
|
||||
func (suite *EventsTestSuite) TestEventTraffic() {
|
||||
evt := mtglib.EventTraffic{
|
||||
func (suite *EventsTestSuite) TestEventTelegramTraffic() {
|
||||
evt := mtglib.EventTelegramTraffic{
|
||||
CreatedAt: time.Now(),
|
||||
ConnID: "CONNID",
|
||||
Traffic: 3,
|
||||
@@ -52,14 +55,26 @@ func (suite *EventsTestSuite) TestEventTraffic() {
|
||||
}
|
||||
|
||||
suite.Equal("CONNID", evt.StreamID())
|
||||
suite.WithinDuration(time.Now(), evt.Timestamp(), 10*time.Millisecond)
|
||||
}
|
||||
|
||||
func (suite *EventsTestSuite) TestEventConcurrencyLimited() {
|
||||
suite.Empty(mtglib.EventConcurrencyLimited{}.StreamID())
|
||||
evt := mtglib.EventConcurrencyLimited{
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
suite.Empty(evt.StreamID())
|
||||
suite.WithinDuration(time.Now(), evt.Timestamp(), 10*time.Millisecond)
|
||||
}
|
||||
|
||||
func (suite *EventsTestSuite) TestEventIPBlocklisted() {
|
||||
suite.Empty(mtglib.EventIPBlocklisted{}.StreamID())
|
||||
evt := mtglib.EventIPBlocklisted{
|
||||
CreatedAt: time.Now(),
|
||||
RemoteIP: net.ParseIP("10.0.0.10"),
|
||||
}
|
||||
|
||||
suite.Empty(evt.StreamID())
|
||||
suite.WithinDuration(time.Now(), evt.Timestamp(), 10*time.Millisecond)
|
||||
}
|
||||
|
||||
func TestEvents(t *testing.T) {
|
||||
|
||||
@@ -45,6 +45,7 @@ type IPBlocklist interface {
|
||||
|
||||
type Event interface {
|
||||
StreamID() string
|
||||
Timestamp() time.Time
|
||||
}
|
||||
|
||||
type EventStream interface {
|
||||
|
||||
@@ -95,6 +95,9 @@ func (p *Proxy) Serve(listener net.Listener) error {
|
||||
|
||||
if addr := conn.RemoteAddr().(*net.TCPAddr).IP; p.ipBlocklist.Contains(addr) {
|
||||
conn.Close()
|
||||
p.logger.
|
||||
BindStr("ip", conn.RemoteAddr().(*net.TCPAddr).IP.String()).
|
||||
Info("ip was blacklisted")
|
||||
p.eventStream.Send(p.ctx, EventIPBlocklisted{
|
||||
CreatedAt: time.Now(),
|
||||
RemoteIP: addr,
|
||||
@@ -110,6 +113,9 @@ func (p *Proxy) Serve(listener net.Listener) error {
|
||||
case errors.Is(err, ants.ErrPoolClosed):
|
||||
return nil
|
||||
case errors.Is(err, ants.ErrPoolOverload):
|
||||
p.logger.
|
||||
BindStr("ip", conn.RemoteAddr().(*net.TCPAddr).IP.String()).
|
||||
Info("connection was concurrency limited")
|
||||
p.eventStream.Send(p.ctx, EventConcurrencyLimited{
|
||||
CreatedAt: time.Now(),
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user