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 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. Secure connectionType `json:"secure"`
  43. }
  44. func (c connections) MarshalJSON() ([]byte, error) {
  45. c.All.IPv4 = c.Abridged.IPv4 + c.Intermediate.IPv4 + c.Secure.IPv4
  46. c.All.IPv6 = c.Abridged.IPv6 + c.Intermediate.IPv6 + c.Secure.IPv6
  47. value := struct {
  48. All connectionType `json:"all"`
  49. Abridged connectionType `json:"abridged"`
  50. Intermediate connectionType `json:"intermediate"`
  51. Secure connectionType `json:"secure"`
  52. }{
  53. All: c.All,
  54. Abridged: c.Abridged,
  55. Intermediate: c.Intermediate,
  56. Secure: c.Secure,
  57. }
  58. return json.Marshal(value)
  59. }
  60. type connectionType struct {
  61. IPv6 uint32 `json:"ipv6"`
  62. IPv4 uint32 `json:"ipv4"`
  63. }
  64. type traffic struct {
  65. Ingress trafficValue `json:"ingress"`
  66. Egress trafficValue `json:"egress"`
  67. }
  68. type speed struct {
  69. Ingress trafficSpeedValue `json:"ingress"`
  70. Egress trafficSpeedValue `json:"egress"`
  71. }
  72. type stats struct {
  73. URLs config.IPURLs `json:"urls"`
  74. Connections connections `json:"connections"`
  75. Traffic traffic `json:"traffic"`
  76. Speed speed `json:"speed"`
  77. Uptime uptime `json:"uptime"`
  78. Crashes uint32 `json:"crashes"`
  79. speedCurrent speed
  80. mutex *sync.RWMutex
  81. }