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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package obfuscated2
  2. import (
  3. "bytes"
  4. "crypto/rand"
  5. "encoding/binary"
  6. "io"
  7. "github.com/juju/errors"
  8. "github.com/9seconds/mtg/mtproto"
  9. )
  10. // [frameOffsetFirst:frameOffsetKey:frameOffsetIV:frameOffsetMagic:frameOffsetDC:frameOffsetEnd]
  11. const (
  12. frameLenKey = 32
  13. frameLenIV = 16
  14. frameLenMagic = 4
  15. frameLenDC = 2
  16. frameOffsetFirst = 8
  17. frameOffsetKey = frameOffsetFirst + frameLenKey
  18. frameOffsetIV = frameOffsetKey + frameLenIV
  19. frameOffsetMagic = frameOffsetIV + frameLenMagic
  20. frameOffsetDC = frameOffsetMagic + frameLenDC
  21. FrameLen = 64
  22. )
  23. // Frame represents handshake frame. Telegram sends 64 bytes of obfuscated2
  24. // initialization data first.
  25. // https://blog.susanka.eu/how-telegram-obfuscates-its-mtproto-traffic/
  26. type Frame []byte
  27. // Key returns AES encryption key.
  28. func (f Frame) Key() []byte {
  29. return f[frameOffsetFirst:frameOffsetKey]
  30. }
  31. // IV returns AES encryption initialization vector
  32. func (f Frame) IV() []byte {
  33. return f[frameOffsetKey:frameOffsetIV]
  34. }
  35. // Magic returns magic bytes from last 8 bytes of frame. Telegram checks
  36. // for values there. If after decryption magic is not as expected,
  37. // connection considered as failed.
  38. func (f Frame) Magic() []byte {
  39. return f[frameOffsetIV:frameOffsetMagic]
  40. }
  41. // DC returns number of datacenter IP client wants to use.
  42. func (f Frame) DC() (n int16) {
  43. buf := bytes.NewReader(f[frameOffsetMagic:frameOffsetDC])
  44. if err := binary.Read(buf, binary.LittleEndian, &n); err != nil {
  45. n = 1
  46. }
  47. return
  48. }
  49. // ConnectionType identifies connection type of the handshake frame.
  50. func (f Frame) ConnectionType() (mtproto.ConnectionType, error) {
  51. return mtproto.ConnectionTagFromHandshake(f.Magic())
  52. }
  53. // Invert inverts frame for extracting encryption keys. Pkease check that link:
  54. // https://blog.susanka.eu/how-telegram-obfuscates-its-mtproto-traffic/
  55. func (f Frame) Invert() *Frame {
  56. reversed := MakeFrame()
  57. copy(*reversed, f)
  58. for i := 0; i < frameLenKey+frameLenIV; i++ {
  59. (*reversed)[frameOffsetFirst+i] = f[frameOffsetIV-1-i]
  60. }
  61. return reversed
  62. }
  63. // ExtractFrame extracts exact obfuscated2 handshake frame from given reader.
  64. func ExtractFrame(conn io.Reader) (*Frame, error) {
  65. frame := MakeFrame()
  66. buf := bytes.NewBuffer(*frame)
  67. buf.Reset()
  68. if _, err := io.CopyN(buf, conn, FrameLen); err != nil {
  69. ReturnFrame(frame)
  70. return nil, errors.Annotate(err, "Cannot extract obfuscated header")
  71. }
  72. copy(*frame, buf.Bytes())
  73. return frame, nil
  74. }
  75. func generateFrame(connectionType mtproto.ConnectionType) *Frame {
  76. frame := MakeFrame()
  77. data := *frame
  78. for {
  79. if _, err := rand.Read(data); err != nil {
  80. continue
  81. }
  82. if data[0] == 0xef {
  83. continue
  84. }
  85. val := (uint32(data[3]) << 24) | (uint32(data[2]) << 16) | (uint32(data[1]) << 8) | uint32(data[0])
  86. if val == 0x44414548 || val == 0x54534f50 || val == 0x20544547 || val == 0x4954504f || val == 0xeeeeeeee {
  87. continue
  88. }
  89. val = (uint32(data[7]) << 24) | (uint32(data[6]) << 16) | (uint32(data[5]) << 8) | uint32(data[4])
  90. if val == 0x00000000 {
  91. continue
  92. }
  93. // error has to be checked before calling this function
  94. tag, _ := connectionType.Tag() // nolint: errcheck
  95. copy(data.Magic(), tag)
  96. return frame
  97. }
  98. }