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
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

obfuscated2_test.go 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package obfuscated2
  2. import (
  3. "crypto/sha256"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/9seconds/mtg/mtproto"
  7. )
  8. func TestObfs2TelegramFrameDecrypt(t *testing.T) {
  9. connOpts := &mtproto.ConnectionOpts{
  10. DC: 1,
  11. ConnectionType: mtproto.ConnectionTypeIntermediate,
  12. }
  13. _, frame := MakeTelegramObfuscated2Frame(connOpts)
  14. decryptor := makeStreamCipher(frame.Key(), frame.IV())
  15. decrypted := make(Frame, FrameLen)
  16. decryptor.XORKeyStream(decrypted, frame)
  17. _, err := decrypted.ConnectionType()
  18. assert.Nil(t, err)
  19. }
  20. func TestObfs2TelegramDecryptEncryptDecrypt(t *testing.T) {
  21. connOpts := &mtproto.ConnectionOpts{
  22. DC: 1,
  23. ConnectionType: mtproto.ConnectionTypeIntermediate,
  24. }
  25. obfs2, frame := MakeTelegramObfuscated2Frame(connOpts)
  26. inverted := frame.Invert()
  27. encryptor := makeStreamCipher(inverted.Key(), inverted.IV())
  28. data := []byte{1, 2, 3}
  29. encrypted := make([]byte, 3)
  30. encryptor.XORKeyStream(encrypted, data)
  31. decrypted := make([]byte, 3)
  32. obfs2.Decryptor.XORKeyStream(decrypted, encrypted)
  33. assert.Equal(t, data, decrypted)
  34. }
  35. func TestObfs2Full(t *testing.T) {
  36. secret := []byte{1, 2, 3, 4, 5}
  37. clientFrame := generateFrame(mtproto.ConnectionTypeIntermediate)
  38. clientHasher := sha256.New()
  39. clientHasher.Write(clientFrame.Key()) // nolint: errcheck, gosec
  40. clientHasher.Write(secret) // nolint: errcheck, gosec
  41. clientKey := clientHasher.Sum(nil)
  42. encryptor := makeStreamCipher(clientKey, clientFrame.IV())
  43. encrypted := make(Frame, FrameLen)
  44. encryptor.XORKeyStream(encrypted, clientFrame)
  45. copy(encrypted[:56], clientFrame[:56])
  46. invertedClientFrame := clientFrame.Invert()
  47. clientHasher = sha256.New()
  48. clientHasher.Write(invertedClientFrame.Key()) // nolint: errcheck, gosec
  49. clientHasher.Write(secret) // nolint: errcheck, gosec
  50. invertedClientKey := clientHasher.Sum(nil)
  51. clientDecryptor := makeStreamCipher(invertedClientKey, invertedClientFrame.IV())
  52. clientObfs, _, err := ParseObfuscated2ClientFrame(secret, encrypted)
  53. assert.Nil(t, err)
  54. connOpts := &mtproto.ConnectionOpts{
  55. DC: 1,
  56. ConnectionType: mtproto.ConnectionTypeIntermediate,
  57. }
  58. tgObfs, tgFrame := MakeTelegramObfuscated2Frame(connOpts)
  59. tgDecryptor := makeStreamCipher(tgFrame.Key(), tgFrame.IV())
  60. decrypted := make(Frame, FrameLen)
  61. tgDecryptor.XORKeyStream(decrypted, tgFrame)
  62. _, err = decrypted.ConnectionType()
  63. assert.Nil(t, err)
  64. tgInvertedFrame := tgFrame.Invert()
  65. tgEncryptor := makeStreamCipher(tgInvertedFrame.Key(), tgInvertedFrame.IV())
  66. message := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9}
  67. tgEncryptedMessage := make([]byte, len(message))
  68. tgEncryptor.XORKeyStream(tgEncryptedMessage, message)
  69. tgEncDecryptedMessage := make([]byte, len(tgEncryptedMessage))
  70. tgObfs.Decryptor.XORKeyStream(tgEncDecryptedMessage, tgEncryptedMessage)
  71. assert.Equal(t, message, tgEncDecryptedMessage)
  72. clientEncryptedMessage := make([]byte, len(tgEncDecryptedMessage))
  73. clientObfs.Encryptor.XORKeyStream(clientEncryptedMessage, tgEncDecryptedMessage)
  74. finalMessage := make([]byte, len(clientEncryptedMessage))
  75. clientDecryptor.XORKeyStream(finalMessage, clientEncryptedMessage)
  76. assert.Equal(t, finalMessage, message)
  77. }