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
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

stats_prometheus.go 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package stats
  2. import (
  3. "net"
  4. "net/http"
  5. "strconv"
  6. "github.com/prometheus/client_golang/prometheus"
  7. "github.com/prometheus/client_golang/prometheus/promhttp"
  8. "github.com/9seconds/mtg/config"
  9. "github.com/9seconds/mtg/conntypes"
  10. )
  11. type statsPrometheus struct {
  12. connections *prometheus.GaugeVec
  13. telegramConnections *prometheus.GaugeVec
  14. traffic *prometheus.GaugeVec
  15. crashes prometheus.Counter
  16. replayAttacks prometheus.Counter
  17. authenticationFailed prometheus.Counter
  18. cloakedRequests prometheus.Counter
  19. }
  20. func (s *statsPrometheus) IngressTraffic(traffic int) {
  21. s.traffic.WithLabelValues("ingress").Add(float64(traffic))
  22. }
  23. func (s *statsPrometheus) EgressTraffic(traffic int) {
  24. s.traffic.WithLabelValues("egress").Add(float64(traffic))
  25. }
  26. func (s *statsPrometheus) ClientConnected(connectionType conntypes.ConnectionType, addr *net.TCPAddr) {
  27. s.changeConnections(connectionType, addr, 1.0)
  28. }
  29. func (s *statsPrometheus) ClientDisconnected(connectionType conntypes.ConnectionType, addr *net.TCPAddr) {
  30. s.changeConnections(connectionType, addr, -1.0)
  31. }
  32. func (s *statsPrometheus) changeConnections(connectionType conntypes.ConnectionType,
  33. addr *net.TCPAddr,
  34. increment float64) {
  35. labels := [...]string{
  36. "intermediate",
  37. "ipv4",
  38. }
  39. switch connectionType {
  40. case conntypes.ConnectionTypeAbridged:
  41. labels[0] = "abridged"
  42. case conntypes.ConnectionTypeSecure:
  43. labels[0] = "secured"
  44. }
  45. if addr.IP.To4() == nil {
  46. labels[1] = "ipv6" // nolint: goconst
  47. }
  48. s.connections.WithLabelValues(labels[:]...).Add(increment)
  49. }
  50. func (s *statsPrometheus) TelegramConnected(dc conntypes.DC, addr *net.TCPAddr) {
  51. s.changeTelegramConnections(dc, addr, 1.0)
  52. }
  53. func (s *statsPrometheus) TelegramDisconnected(dc conntypes.DC, addr *net.TCPAddr) {
  54. s.changeTelegramConnections(dc, addr, -1.0)
  55. }
  56. func (s *statsPrometheus) changeTelegramConnections(dc conntypes.DC, addr *net.TCPAddr, increment float64) {
  57. labels := [...]string{
  58. strconv.Itoa(int(dc)),
  59. "ipv4",
  60. }
  61. if addr.IP.To4() == nil {
  62. labels[1] = "ipv6"
  63. }
  64. s.telegramConnections.WithLabelValues(labels[:]...).Add(increment)
  65. }
  66. func (s *statsPrometheus) Crash() {
  67. s.crashes.Inc()
  68. }
  69. func (s *statsPrometheus) ReplayDetected() {
  70. s.replayAttacks.Inc()
  71. }
  72. func (s *statsPrometheus) AuthenticationFailed() {
  73. s.authenticationFailed.Inc()
  74. }
  75. func (s *statsPrometheus) CloakedRequest() {
  76. s.cloakedRequests.Inc()
  77. }
  78. func newStatsPrometheus(mux *http.ServeMux) Interface {
  79. registry := prometheus.NewPedanticRegistry()
  80. instance := &statsPrometheus{
  81. connections: prometheus.NewGaugeVec(prometheus.GaugeOpts{
  82. Namespace: config.C.StatsNamespace,
  83. Name: "connections",
  84. Help: "Current number of client connections to the proxy.",
  85. }, []string{"type", "protocol"}),
  86. telegramConnections: prometheus.NewGaugeVec(prometheus.GaugeOpts{
  87. Namespace: config.C.StatsNamespace,
  88. Name: "telegram_connections",
  89. Help: "Current number of telegram connections established by this proxy.",
  90. }, []string{"dc", "protocol"}),
  91. traffic: prometheus.NewGaugeVec(prometheus.GaugeOpts{
  92. Namespace: config.C.StatsNamespace,
  93. Name: "traffic",
  94. Help: "Traffic passed through the proxy in bytes.",
  95. }, []string{"direction"}),
  96. crashes: prometheus.NewCounter(prometheus.CounterOpts{
  97. Namespace: config.C.StatsNamespace,
  98. Name: "crashes",
  99. Help: "How many crashes happened.",
  100. }),
  101. replayAttacks: prometheus.NewCounter(prometheus.CounterOpts{
  102. Namespace: config.C.StatsNamespace,
  103. Name: "replay_attacks",
  104. Help: "How many replay attacks were prevented.",
  105. }),
  106. authenticationFailed: prometheus.NewCounter(prometheus.CounterOpts{
  107. Namespace: config.C.StatsNamespace,
  108. Name: "authentication_failed",
  109. Help: "How many authentication failed events we've seen.",
  110. }),
  111. cloakedRequests: prometheus.NewCounter(prometheus.CounterOpts{
  112. Namespace: config.C.StatsNamespace,
  113. Name: "cloaked_requests",
  114. Help: "How many requests were proxified during cloaking.",
  115. }),
  116. }
  117. registry.MustRegister(instance.connections)
  118. registry.MustRegister(instance.telegramConnections)
  119. registry.MustRegister(instance.traffic)
  120. registry.MustRegister(instance.crashes)
  121. registry.MustRegister(instance.replayAttacks)
  122. registry.MustRegister(instance.authenticationFailed)
  123. registry.MustRegister(instance.cloakedRequests)
  124. handler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
  125. mux.Handle("/", handler)
  126. return instance
  127. }