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.

mtproto_abridged.go 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package wrappers
  2. import (
  3. "bytes"
  4. "io"
  5. "net"
  6. "github.com/juju/errors"
  7. "go.uber.org/zap"
  8. "github.com/9seconds/mtg/mtproto"
  9. "github.com/9seconds/mtg/utils"
  10. )
  11. const (
  12. mtprotoAbridgedSmallPacketLength = 0x7f
  13. mtprotoAbridgedQuickAckLength = 0x80
  14. mtprotoAbridgedLargePacketLength = 16777216 // 256 ^ 3
  15. )
  16. type MTProtoAbridged struct {
  17. conn StreamReadWriteCloser
  18. opts *mtproto.ConnectionOpts
  19. logger *zap.SugaredLogger
  20. readCounter uint32
  21. writeCounter uint32
  22. }
  23. func (m *MTProtoAbridged) Read() ([]byte, error) {
  24. defer func() {
  25. m.readCounter++
  26. }()
  27. m.logger.Debugw("Read packet",
  28. "simple_ack", m.opts.ReadHacks.SimpleAck,
  29. "quick_ack", m.opts.ReadHacks.QuickAck,
  30. "counter", m.readCounter,
  31. )
  32. buf := &bytes.Buffer{}
  33. buf.Grow(3)
  34. if _, err := io.CopyN(buf, m.conn, 1); err != nil {
  35. return nil, errors.Annotate(err, "Cannot read message length")
  36. }
  37. msgLength := uint32(buf.Bytes()[0])
  38. buf.Reset()
  39. m.logger.Debugw("Packet first byte",
  40. "byte", msgLength,
  41. "counter", m.readCounter,
  42. "simple_ack", m.opts.ReadHacks.SimpleAck,
  43. "quick_ack", m.opts.ReadHacks.QuickAck,
  44. )
  45. if msgLength >= mtprotoAbridgedQuickAckLength {
  46. m.opts.ReadHacks.QuickAck = true
  47. msgLength -= mtprotoAbridgedQuickAckLength
  48. }
  49. if msgLength == mtprotoAbridgedSmallPacketLength {
  50. if _, err := io.CopyN(buf, m.conn, 3); err != nil {
  51. return nil, errors.Annotate(err, "Cannot read the correct message length")
  52. }
  53. number := utils.Uint24{}
  54. copy(number[:], buf.Bytes())
  55. msgLength = utils.FromUint24(number)
  56. }
  57. msgLength *= 4
  58. m.logger.Debugw("Packet length",
  59. "length", msgLength,
  60. "simple_ack", m.opts.ReadHacks.SimpleAck,
  61. "quick_ack", m.opts.ReadHacks.QuickAck,
  62. "counter", m.readCounter,
  63. )
  64. buf.Reset()
  65. buf.Grow(int(msgLength))
  66. if _, err := io.CopyN(buf, m.conn, int64(msgLength)); err != nil {
  67. return nil, errors.Annotate(err, "Cannot read message")
  68. }
  69. return buf.Bytes(), nil
  70. }
  71. func (m *MTProtoAbridged) Write(p []byte) (int, error) {
  72. defer func() {
  73. m.writeCounter++
  74. }()
  75. m.logger.Debugw("Write packet",
  76. "length", len(p),
  77. "simple_ack", m.opts.WriteHacks.SimpleAck,
  78. "quick_ack", m.opts.WriteHacks.QuickAck,
  79. "counter", m.writeCounter,
  80. )
  81. if len(p)%4 != 0 {
  82. return 0, errors.Errorf("Incorrect packet length %d", len(p))
  83. }
  84. if m.opts.WriteHacks.SimpleAck {
  85. return m.conn.Write(utils.ReverseBytes(p))
  86. }
  87. packetLength := len(p) / 4
  88. switch {
  89. case packetLength < mtprotoAbridgedSmallPacketLength:
  90. newData := append([]byte{byte(packetLength)}, p...)
  91. return m.conn.Write(newData)
  92. case packetLength < mtprotoAbridgedLargePacketLength:
  93. length24 := utils.ToUint24(uint32(packetLength))
  94. buf := &bytes.Buffer{}
  95. buf.Grow(1 + 3 + len(p))
  96. buf.WriteByte(byte(mtprotoAbridgedSmallPacketLength))
  97. buf.Write(length24[:])
  98. buf.Write(p)
  99. return m.conn.Write(buf.Bytes())
  100. }
  101. return 0, errors.Errorf("Packet is too big %d", len(p))
  102. }
  103. func (m *MTProtoAbridged) Logger() *zap.SugaredLogger {
  104. return m.logger
  105. }
  106. func (m *MTProtoAbridged) LocalAddr() *net.TCPAddr {
  107. return m.conn.LocalAddr()
  108. }
  109. func (m *MTProtoAbridged) RemoteAddr() *net.TCPAddr {
  110. return m.conn.RemoteAddr()
  111. }
  112. func (m *MTProtoAbridged) Close() error {
  113. return m.conn.Close()
  114. }
  115. func NewMTProtoAbridged(conn StreamReadWriteCloser, opts *mtproto.ConnectionOpts) PacketReadWriteCloser {
  116. return &MTProtoAbridged{
  117. conn: conn,
  118. opts: opts,
  119. logger: conn.Logger().Named("mtproto-abridged"),
  120. }
  121. }