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
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

client_handshake.go 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package obfuscated2
  2. import (
  3. "crypto/cipher"
  4. "crypto/subtle"
  5. "encoding/hex"
  6. "fmt"
  7. "io"
  8. )
  9. type clientHandhakeFrame struct {
  10. handshakeFrame
  11. }
  12. func (c *clientHandhakeFrame) decryptor(secret []byte) cipher.Stream {
  13. hasher := acquireSha256Hasher()
  14. defer releaseSha256Hasher(hasher)
  15. hasher.Write(c.key()) // nolint: errcheck
  16. hasher.Write(secret) // nolint: errcheck
  17. return makeAesCtr(hasher.Sum(nil), c.iv())
  18. }
  19. func (c *clientHandhakeFrame) encryptor(secret []byte) cipher.Stream {
  20. arr := clientHandhakeFrame{}
  21. invertByteSlices(arr.data[:], c.data[:])
  22. hasher := acquireSha256Hasher()
  23. defer releaseSha256Hasher(hasher)
  24. hasher.Write(arr.key()) // nolint: errcheck
  25. hasher.Write(secret) // nolint: errcheck
  26. return makeAesCtr(hasher.Sum(nil), arr.iv())
  27. }
  28. func ClientHandshake(secret []byte, reader io.Reader) (int, cipher.Stream, cipher.Stream, error) {
  29. handshake := clientHandhakeFrame{}
  30. if _, err := io.ReadFull(reader, handshake.data[:]); err != nil {
  31. return 0, nil, nil, fmt.Errorf("cannot read frame: %w", err)
  32. }
  33. decryptor := handshake.decryptor(secret)
  34. encryptor := handshake.encryptor(secret)
  35. decryptor.XORKeyStream(handshake.data[:], handshake.data[:])
  36. if val := handshake.connectionType(); subtle.ConstantTimeCompare(handshakeConnectionType, val) != 1 {
  37. return 0, nil, nil, fmt.Errorf("unsupported connection type: %s", hex.EncodeToString(val))
  38. }
  39. return handshake.dc(), encryptor, decryptor, nil
  40. }