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

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