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.

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