Highly-opinionated (ex-bullshit-free) MTPROTO proxy for Telegram. If you use v1.0 or upgrade broke you proxy, please read the chapter Version 2
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package stats
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "github.com/9seconds/mtg/v2/events"
  7. "github.com/9seconds/mtg/v2/logger"
  8. "github.com/9seconds/mtg/v2/mtglib"
  9. statsd "github.com/smira/go-statsd"
  10. )
  11. type statsdProcessor struct {
  12. streams map[string]*streamInfo
  13. client *statsd.Client
  14. }
  15. func (s statsdProcessor) EventStart(evt mtglib.EventStart) {
  16. info := acquireStreamInfo()
  17. if evt.RemoteIP.To4() != nil {
  18. info.tags[TagIPFamily] = TagIPFamilyIPv4
  19. } else {
  20. info.tags[TagIPFamily] = TagIPFamilyIPv6
  21. }
  22. s.streams[evt.StreamID()] = info
  23. s.client.GaugeDelta(MetricClientConnections,
  24. 1,
  25. info.T(TagIPFamily))
  26. }
  27. func (s statsdProcessor) EventConnectedToDC(evt mtglib.EventConnectedToDC) {
  28. info, ok := s.streams[evt.StreamID()]
  29. if !ok {
  30. return
  31. }
  32. info.tags[TagTelegramIP] = evt.RemoteIP.String()
  33. info.tags[TagDC] = strconv.Itoa(evt.DC)
  34. s.client.GaugeDelta(MetricTelegramConnections,
  35. 1,
  36. info.T(TagTelegramIP),
  37. info.T(TagDC))
  38. }
  39. func (s statsdProcessor) EventDomainFronting(evt mtglib.EventDomainFronting) {
  40. info, ok := s.streams[evt.StreamID()]
  41. if !ok {
  42. return
  43. }
  44. info.isDomainFronted = true
  45. s.client.Incr(MetricDomainFronting, 1)
  46. s.client.GaugeDelta(MetricDomainFrontingConnections,
  47. 1,
  48. info.T(TagIPFamily))
  49. }
  50. func (s statsdProcessor) EventTraffic(evt mtglib.EventTraffic) {
  51. info, ok := s.streams[evt.StreamID()]
  52. if !ok {
  53. return
  54. }
  55. directionTag := statsd.StringTag(TagDirection, getDirection(evt.IsRead))
  56. if info.isDomainFronted {
  57. s.client.Incr(MetricDomainFrontingTraffic,
  58. int64(evt.Traffic),
  59. directionTag)
  60. } else {
  61. s.client.Incr(MetricTelegramTraffic,
  62. int64(evt.Traffic),
  63. info.T(TagTelegramIP),
  64. info.T(TagDC),
  65. directionTag)
  66. }
  67. }
  68. func (s statsdProcessor) EventFinish(evt mtglib.EventFinish) {
  69. info, ok := s.streams[evt.StreamID()]
  70. if !ok {
  71. return
  72. }
  73. defer func() {
  74. delete(s.streams, evt.StreamID())
  75. releaseStreamInfo(info)
  76. }()
  77. s.client.GaugeDelta(MetricClientConnections,
  78. -1,
  79. info.T(TagIPFamily))
  80. if info.isDomainFronted {
  81. s.client.GaugeDelta(MetricDomainFrontingConnections,
  82. -1,
  83. info.T(TagIPFamily))
  84. } else if _, ok := info.tags[TagTelegramIP]; ok {
  85. s.client.GaugeDelta(MetricTelegramConnections,
  86. -1,
  87. info.T(TagTelegramIP),
  88. info.T(TagDC))
  89. }
  90. }
  91. func (s statsdProcessor) EventConcurrencyLimited(_ mtglib.EventConcurrencyLimited) {
  92. s.client.Incr(MetricConcurrencyLimited, 1)
  93. }
  94. func (s statsdProcessor) EventIPBlocklisted(_ mtglib.EventIPBlocklisted) {
  95. s.client.Incr(MetricIPBlocklisted, 1)
  96. }
  97. func (s statsdProcessor) EventReplayAttack(_ mtglib.EventReplayAttack) {
  98. s.client.Incr(MetricReplayAttacks, 1)
  99. }
  100. func (s statsdProcessor) EventIPListSize(evt mtglib.EventIPListSize) {
  101. tag := TagIPListBlock
  102. if !evt.IsBlockList {
  103. tag = TagIPListAllow
  104. }
  105. s.client.Gauge(MetricIPListSize, int64(evt.Size), statsd.StringTag(TagIPList, tag))
  106. }
  107. func (s statsdProcessor) Shutdown() {
  108. events := make([]mtglib.EventFinish, 0, len(s.streams))
  109. for k := range s.streams {
  110. events = append(events, mtglib.NewEventFinish(k))
  111. }
  112. for i := range events {
  113. s.EventFinish(events[i])
  114. }
  115. }
  116. // StatsdFactory is a factory of events.Observers which dumps
  117. // information to statsd.
  118. //
  119. // Please beware that we support ONLY UDP endpoints there. And this
  120. // factory won't use mtglib.Network so it won't use a proxy if you
  121. // provide any. If you need it, I would recommend starting a local
  122. // statsd and route metrics further by features of the chosen server.
  123. type StatsdFactory struct {
  124. client *statsd.Client
  125. }
  126. // Close stops sending requests to statsd.
  127. func (s StatsdFactory) Close() error {
  128. return s.client.Close() // nolint: wrapcheck
  129. }
  130. // Make build a new observer.
  131. func (s StatsdFactory) Make() events.Observer {
  132. return statsdProcessor{
  133. client: s.client,
  134. streams: make(map[string]*streamInfo),
  135. }
  136. }
  137. // NewStatsd builds an events.ObserverFactory that sends events
  138. // to statsd.
  139. //
  140. // Valid tagFormats are 'datadog', 'influxdb' and 'graphite'.
  141. func NewStatsd(address string, log logger.StdLikeLogger,
  142. metricPrefix, tagFormat string) (StatsdFactory, error) {
  143. options := []statsd.Option{
  144. statsd.MetricPrefix(metricPrefix),
  145. statsd.Logger(log),
  146. }
  147. switch strings.ToLower(tagFormat) {
  148. case "datadog":
  149. options = append(options, statsd.TagStyle(statsd.TagFormatDatadog))
  150. case "influxdb":
  151. options = append(options, statsd.TagStyle(statsd.TagFormatInfluxDB))
  152. case "graphite":
  153. options = append(options, statsd.TagStyle(statsd.TagFormatGraphite))
  154. default:
  155. return StatsdFactory{}, fmt.Errorf("unknown tag format %s", tagFormat)
  156. }
  157. return StatsdFactory{
  158. client: statsd.NewClient(address, options...),
  159. }, nil
  160. }