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.go 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package stats
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strconv"
  6. "sync"
  7. "time"
  8. humanize "github.com/dustin/go-humanize"
  9. "github.com/9seconds/mtg/config"
  10. )
  11. type uptime time.Time
  12. func (u uptime) MarshalJSON() ([]byte, error) {
  13. duration := time.Since(time.Time(u))
  14. value := map[string]string{
  15. "seconds": strconv.Itoa(int(duration.Seconds())),
  16. "human": humanize.Time(time.Time(u)),
  17. }
  18. return json.Marshal(value)
  19. }
  20. type trafficValue uint64
  21. func (t trafficValue) MarshalJSON() ([]byte, error) {
  22. tv := uint64(t)
  23. value := map[string]interface{}{
  24. "bytes": tv,
  25. "human": humanize.Bytes(tv),
  26. }
  27. return json.Marshal(value)
  28. }
  29. type trafficSpeedValue uint64
  30. func (t trafficSpeedValue) MarshalJSON() ([]byte, error) {
  31. speed := uint64(t)
  32. value := map[string]interface{}{
  33. "bytes/s": speed,
  34. "human": fmt.Sprintf("%s/S", humanize.Bytes(speed)),
  35. }
  36. return json.Marshal(value)
  37. }
  38. type connections struct {
  39. All connectionType `json:"all"`
  40. Abridged connectionType `json:"abridged"`
  41. Intermediate connectionType `json:"intermediate"`
  42. }
  43. func (c connections) MarshalJSON() ([]byte, error) {
  44. c.All.IPv4 = c.Abridged.IPv4 + c.Intermediate.IPv4
  45. c.All.IPv6 = c.Abridged.IPv6 + c.Intermediate.IPv6
  46. value := struct {
  47. All connectionType `json:"all"`
  48. Abridged connectionType `json:"abridged"`
  49. Intermediate connectionType `json:"intermediate"`
  50. }{
  51. All: c.All,
  52. Abridged: c.Abridged,
  53. Intermediate: c.Intermediate,
  54. }
  55. return json.Marshal(value)
  56. }
  57. type connectionType struct {
  58. IPv6 uint32 `json:"ipv6"`
  59. IPv4 uint32 `json:"ipv4"`
  60. }
  61. type traffic struct {
  62. Ingress trafficValue `json:"ingress"`
  63. Egress trafficValue `json:"egress"`
  64. }
  65. type speed struct {
  66. Ingress trafficSpeedValue `json:"ingress"`
  67. Egress trafficSpeedValue `json:"egress"`
  68. }
  69. type stats struct {
  70. URLs config.IPURLs `json:"urls"`
  71. ActiveConnections connections `json:"active_connections"`
  72. AllConnections connections `json:"all_connections"`
  73. Traffic traffic `json:"traffic"`
  74. Speed speed `json:"speed"`
  75. Uptime uptime `json:"uptime"`
  76. Crashes uint32 `json:"crashes"`
  77. speedCurrent speed
  78. mutex *sync.RWMutex
  79. }