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.

conns.go 1.0KB

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