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 символов.

mtproto_intermediate_secure.go 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package wrappers
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "math/rand"
  6. "github.com/9seconds/mtg/mtproto"
  7. )
  8. type MTProtoIntermediateSecure struct {
  9. MTProtoIntermediate
  10. }
  11. func (m *MTProtoIntermediateSecure) Read() ([]byte, error) {
  12. data, err := m.MTProtoIntermediate.Read()
  13. if err != nil {
  14. return nil, err
  15. }
  16. length := len(data) - (len(data) % 4)
  17. return data[:length], nil
  18. }
  19. func (m *MTProtoIntermediateSecure) Write(p []byte) (int, error) {
  20. defer func() {
  21. m.writeCounter++
  22. }()
  23. m.logger.Debugw("Write packet",
  24. "simple_ack", m.opts.WriteHacks.SimpleAck,
  25. "quick_ack", m.opts.WriteHacks.QuickAck,
  26. "counter", m.writeCounter,
  27. )
  28. if m.opts.ReadHacks.SimpleAck {
  29. return m.conn.Write(p)
  30. }
  31. buf := &bytes.Buffer{}
  32. paddingLength := rand.Intn(4)
  33. buf.Grow(4 + len(p) + paddingLength)
  34. binary.Write(buf, binary.LittleEndian, uint32(len(p)+paddingLength))
  35. buf.Write(p)
  36. buf.Write(make([]byte, paddingLength))
  37. m.logger.Debugw("Write packet with padding",
  38. "simple_ack", m.opts.WriteHacks.SimpleAck,
  39. "quick_ack", m.opts.WriteHacks.QuickAck,
  40. "counter", m.writeCounter,
  41. "padding_length", paddingLength,
  42. "length", len(p),
  43. )
  44. _, err := m.conn.Write(buf.Bytes())
  45. return len(p), err
  46. }
  47. func NewMTProtoIntermediateSecure(conn StreamReadWriteCloser, opts *mtproto.ConnectionOpts) PacketReadWriteCloser {
  48. return &MTProtoIntermediateSecure{
  49. MTProtoIntermediate: MTProtoIntermediate{
  50. conn: conn,
  51. logger: conn.Logger().Named("mtproto-intermediate-secure"),
  52. opts: opts,
  53. },
  54. }
  55. }