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.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package proxy
  2. import (
  3. "context"
  4. "io"
  5. "github.com/juju/errors"
  6. )
  7. type CtxReadWriteCloser struct {
  8. ctx context.Context
  9. conn io.ReadWriteCloser
  10. cancel context.CancelFunc
  11. }
  12. func (c *CtxReadWriteCloser) Read(p []byte) (int, error) {
  13. select {
  14. case <-c.ctx.Done():
  15. return 0, errors.Annotate(c.ctx.Err(), "Read is failed because of closed context")
  16. default:
  17. n, err := c.conn.Read(p)
  18. if err != nil {
  19. c.cancel()
  20. }
  21. return n, err
  22. }
  23. }
  24. func (c *CtxReadWriteCloser) Write(p []byte) (int, error) {
  25. select {
  26. case <-c.ctx.Done():
  27. return 0, errors.Annotate(c.ctx.Err(), "Write is failed because of closed context")
  28. default:
  29. n, err := c.conn.Write(p)
  30. if err != nil {
  31. c.cancel()
  32. }
  33. return n, err
  34. }
  35. }
  36. func (c *CtxReadWriteCloser) Close() error {
  37. return c.conn.Close()
  38. }
  39. func newCtxReadWriteCloser(conn io.ReadWriteCloser, ctx context.Context, cancel context.CancelFunc) io.ReadWriteCloser {
  40. return &CtxReadWriteCloser{
  41. conn: conn,
  42. ctx: ctx,
  43. cancel: cancel,
  44. }
  45. }