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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package obfuscated2
  2. import (
  3. "crypto/aes"
  4. "crypto/cipher"
  5. "crypto/sha256"
  6. "github.com/juju/errors"
  7. "github.com/9seconds/mtg/mtproto"
  8. )
  9. // Obfuscated2 contains AES CTR encryption and decryption streams
  10. // for telegram connection.
  11. type Obfuscated2 struct {
  12. Decryptor cipher.Stream
  13. Encryptor cipher.Stream
  14. }
  15. // ParseObfuscated2ClientFrame parses client frame. Please check this link for
  16. // details: http://telegra.ph/telegram-blocks-wtf-05-26
  17. //
  18. // Beware, link above is in russian.
  19. func ParseObfuscated2ClientFrame(secret []byte, frame Frame) (*Obfuscated2, *mtproto.ConnectionOpts, error) {
  20. decHasher := sha256.New()
  21. decHasher.Write(frame.Key()) // nolint: errcheck
  22. decHasher.Write(secret) // nolint: errcheck
  23. decryptor := makeStreamCipher(decHasher.Sum(nil), frame.IV())
  24. invertedFrame := frame.Invert()
  25. encHasher := sha256.New()
  26. encHasher.Write(invertedFrame.Key()) // nolint: errcheck
  27. encHasher.Write(secret) // nolint: errcheck
  28. encryptor := makeStreamCipher(encHasher.Sum(nil), invertedFrame.IV())
  29. decryptedFrame := make(Frame, FrameLen)
  30. decryptor.XORKeyStream(decryptedFrame, frame)
  31. connType, err := decryptedFrame.ConnectionType()
  32. if err != nil {
  33. return nil, nil, errors.Annotate(err, "Unknown protocol")
  34. }
  35. obfs := &Obfuscated2{
  36. Decryptor: decryptor,
  37. Encryptor: encryptor,
  38. }
  39. connOpts := &mtproto.ConnectionOpts{
  40. DC: decryptedFrame.DC(),
  41. ConnectionType: connType,
  42. }
  43. return obfs, connOpts, nil
  44. }
  45. // MakeTelegramObfuscated2Frame creates new handshake frame to send to
  46. // Telegram.
  47. // https://blog.susanka.eu/how-telegram-obfuscates-its-mtproto-traffic/
  48. func MakeTelegramObfuscated2Frame(opts *mtproto.ConnectionOpts) (*Obfuscated2, Frame) {
  49. frame := generateFrame(opts.ConnectionType)
  50. encryptor := makeStreamCipher(frame.Key(), frame.IV())
  51. decryptorFrame := frame.Invert()
  52. decryptor := makeStreamCipher(decryptorFrame.Key(), decryptorFrame.IV())
  53. copyFrame := make(Frame, FrameLen)
  54. copy(copyFrame[:frameOffsetIV], frame[:frameOffsetIV])
  55. encryptor.XORKeyStream(frame, frame)
  56. copy(frame[:frameOffsetIV], copyFrame[:frameOffsetIV])
  57. obfs := &Obfuscated2{
  58. Decryptor: decryptor,
  59. Encryptor: encryptor,
  60. }
  61. return obfs, frame
  62. }
  63. func makeStreamCipher(key, iv []byte) cipher.Stream {
  64. block, _ := aes.NewCipher(key)
  65. return cipher.NewCTR(block, iv)
  66. }