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.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 uint32 `json:"all"`
  40. Abridged uint32 `json:"abridged"`
  41. Intermediate uint32 `json:"intermediate"`
  42. }
  43. type traffic struct {
  44. Ingress trafficValue `json:"ingress"`
  45. Egress trafficValue `json:"egress"`
  46. }
  47. type speed struct {
  48. Ingress trafficSpeedValue `json:"ingress"`
  49. Egress trafficSpeedValue `json:"egress"`
  50. }
  51. type stats struct {
  52. URLs config.IPURLs `json:"urls"`
  53. ActiveConnections connections `json:"active_connections"`
  54. AllConnections connections `json:"all_connections"`
  55. Traffic traffic `json:"traffic"`
  56. Speed speed `json:"speed"`
  57. Uptime uptime `json:"uptime"`
  58. speedCurrent *speed
  59. mutex *sync.RWMutex
  60. }