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
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

conns.go 1.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package mtglib
  2. import (
  3. "bytes"
  4. "context"
  5. "io"
  6. "sync"
  7. "github.com/9seconds/mtg/v2/essentials"
  8. )
  9. type connTraffic struct {
  10. essentials.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. essentials.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) //nolint: wrapcheck
  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 essentials.Conn) *connRewind {
  46. rv := &connRewind{
  47. Conn: conn,
  48. }
  49. rv.active = io.TeeReader(conn, &rv.buf)
  50. return rv
  51. }