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_traffic.go 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package stream
  2. import (
  3. "net"
  4. "time"
  5. "github.com/9seconds/mtg/conntypes"
  6. "github.com/9seconds/mtg/stats"
  7. "go.uber.org/zap"
  8. )
  9. type wrapperTrafficStats struct {
  10. parent conntypes.StreamReadWriteCloser
  11. }
  12. func (w *wrapperTrafficStats) Write(p []byte) (int, error) {
  13. n, err := w.parent.Write(p)
  14. stats.Stats.EgressTraffic(n)
  15. return n, err //nolint: wrapcheck
  16. }
  17. func (w *wrapperTrafficStats) WriteTimeout(p []byte, timeout time.Duration) (int, error) {
  18. n, err := w.parent.WriteTimeout(p, timeout)
  19. stats.Stats.EgressTraffic(n)
  20. return n, err //nolint: wrapcheck
  21. }
  22. func (w *wrapperTrafficStats) Read(p []byte) (int, error) {
  23. n, err := w.parent.Read(p)
  24. stats.Stats.IngressTraffic(n)
  25. return n, err //nolint: wrapcheck
  26. }
  27. func (w *wrapperTrafficStats) ReadTimeout(p []byte, timeout time.Duration) (int, error) {
  28. n, err := w.parent.ReadTimeout(p, timeout)
  29. stats.Stats.IngressTraffic(n)
  30. return n, err //nolint: wrapcheck
  31. }
  32. func (w *wrapperTrafficStats) Conn() net.Conn {
  33. return w.parent.Conn()
  34. }
  35. func (w *wrapperTrafficStats) Logger() *zap.SugaredLogger {
  36. return w.parent.Logger().Named("stats-traffic")
  37. }
  38. func (w *wrapperTrafficStats) LocalAddr() *net.TCPAddr {
  39. return w.parent.LocalAddr()
  40. }
  41. func (w *wrapperTrafficStats) RemoteAddr() *net.TCPAddr {
  42. return w.parent.RemoteAddr()
  43. }
  44. func (w *wrapperTrafficStats) Close() error {
  45. return w.parent.Close() //nolint: wrapcheck
  46. }
  47. func NewTrafficStats(parent conntypes.StreamReadWriteCloser) conntypes.StreamReadWriteCloser {
  48. return &wrapperTrafficStats{parent}
  49. }