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.

ctxrwc.go 1.2KB

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