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
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stats_prometheus.go 4.5KB

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