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
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package wrappers
  2. import (
  3. "context"
  4. "net"
  5. "github.com/juju/errors"
  6. )
  7. type Ctx struct {
  8. cancel context.CancelFunc
  9. conn WrapStreamReadWriteCloser
  10. ctx context.Context
  11. }
  12. func (c *Ctx) 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 *Ctx) 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 *Ctx) LogDebug(msg string, data ...interface{}) {
  37. c.conn.LogDebug(msg, data...)
  38. }
  39. func (c *Ctx) LogInfo(msg string, data ...interface{}) {
  40. c.conn.LogInfo(msg, data...)
  41. }
  42. func (c *Ctx) LogWarn(msg string, data ...interface{}) {
  43. c.conn.LogWarn(msg, data...)
  44. }
  45. func (c *Ctx) LogError(msg string, data ...interface{}) {
  46. c.conn.LogError(msg, data...)
  47. }
  48. func (c *Ctx) LocalAddr() *net.TCPAddr {
  49. return c.conn.LocalAddr()
  50. }
  51. func (c *Ctx) RemoteAddr() *net.TCPAddr {
  52. return c.conn.RemoteAddr()
  53. }
  54. func (c *Ctx) Close() error {
  55. return c.conn.Close()
  56. }
  57. func NewCtx(ctx context.Context, cancel context.CancelFunc, conn WrapStreamReadWriteCloser) WrapStreamReadWriteCloser {
  58. return &Ctx{
  59. ctx: ctx,
  60. cancel: cancel,
  61. conn: conn,
  62. }
  63. }