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
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

conns.go 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package mtglib
  2. import (
  3. "bytes"
  4. "context"
  5. "io"
  6. "net"
  7. "sync"
  8. "time"
  9. )
  10. type connTraffic struct {
  11. net.Conn
  12. connID string
  13. stream EventStream
  14. ctx context.Context
  15. }
  16. func (c connTraffic) Read(b []byte) (int, error) {
  17. n, err := c.Conn.Read(b)
  18. if n > 0 {
  19. c.stream.Send(c.ctx, EventTraffic{
  20. CreatedAt: time.Now(),
  21. ConnID: c.connID,
  22. Traffic: uint(n),
  23. IsRead: true,
  24. })
  25. }
  26. return n, err // nolint: wrapcheck
  27. }
  28. func (c connTraffic) Write(b []byte) (int, error) {
  29. n, err := c.Conn.Write(b)
  30. if n > 0 {
  31. c.stream.Send(c.ctx, EventTraffic{
  32. CreatedAt: time.Now(),
  33. ConnID: c.connID,
  34. Traffic: uint(n),
  35. IsRead: false,
  36. })
  37. }
  38. return n, err // nolint: wrapcheck
  39. }
  40. type connRewind struct {
  41. net.Conn
  42. active io.Reader
  43. buf bytes.Buffer
  44. mutex sync.RWMutex
  45. }
  46. func (c *connRewind) Read(p []byte) (int, error) {
  47. c.mutex.RLock()
  48. defer c.mutex.RUnlock()
  49. return c.active.Read(p)
  50. }
  51. func (c *connRewind) Rewind() {
  52. c.mutex.Lock()
  53. defer c.mutex.Unlock()
  54. c.active = io.MultiReader(&c.buf, c.Conn)
  55. }
  56. func newConnRewind(conn net.Conn) *connRewind {
  57. rv := &connRewind{
  58. Conn: conn,
  59. }
  60. rv.active = io.TeeReader(conn, &rv.buf)
  61. return rv
  62. }