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

mtproto_cipher.go 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package wrappers
  2. import (
  3. "bytes"
  4. "crypto/aes"
  5. "crypto/cipher"
  6. "crypto/md5" // nolint: gas
  7. "crypto/sha1"
  8. "encoding/binary"
  9. "net"
  10. "github.com/9seconds/mtg/mtproto/rpc"
  11. "github.com/9seconds/mtg/utils"
  12. )
  13. type cipherPurpose uint8
  14. const (
  15. cipherPurposeClient cipherPurpose = iota
  16. cipherPurposeServer
  17. )
  18. var emptyIP = [4]byte{0x00, 0x00, 0x00, 0x00}
  19. // NewMiddleProxyCipher creates new block cipher to proxy<->telegram
  20. // connection.
  21. func NewMiddleProxyCipher(conn StreamReadWriteCloser, req *rpc.NonceRequest, resp *rpc.NonceResponse, secret []byte) StreamReadWriteCloser {
  22. localAddr := conn.LocalAddr()
  23. remoteAddr := conn.RemoteAddr()
  24. encKey, encIV := deriveKeys(cipherPurposeClient, req, resp, localAddr, remoteAddr, secret)
  25. decKey, decIV := deriveKeys(cipherPurposeServer, req, resp, localAddr, remoteAddr, secret)
  26. enc, _ := makeEncrypterDecrypter(encKey, encIV)
  27. _, dec := makeEncrypterDecrypter(decKey, decIV)
  28. return NewBlockCipher(conn, enc, dec)
  29. }
  30. func deriveKeys(purpose cipherPurpose, req *rpc.NonceRequest, resp *rpc.NonceResponse, client *net.TCPAddr, remote *net.TCPAddr, secret []byte) ([]byte, []byte) {
  31. message := bytes.Buffer{}
  32. message.Write(resp.Nonce[:])
  33. message.Write(req.Nonce[:])
  34. message.Write(req.CryptoTS[:])
  35. clientIPv4 := emptyIP[:]
  36. serverIPv4 := emptyIP[:]
  37. if client.IP.To4() != nil {
  38. clientIPv4 = utils.ReverseBytes(client.IP.To4())
  39. serverIPv4 = utils.ReverseBytes(remote.IP.To4())
  40. }
  41. message.Write(serverIPv4)
  42. var port [2]byte
  43. binary.LittleEndian.PutUint16(port[:], uint16(client.Port))
  44. message.Write(port[:])
  45. switch purpose {
  46. case cipherPurposeClient:
  47. message.WriteString("CLIENT")
  48. case cipherPurposeServer:
  49. message.WriteString("SERVER")
  50. default:
  51. panic("Unexpected cipher purpose")
  52. }
  53. message.Write(clientIPv4)
  54. binary.LittleEndian.PutUint16(port[:], uint16(remote.Port))
  55. message.Write(port[:])
  56. message.Write(secret)
  57. message.Write(resp.Nonce[:])
  58. if client.IP.To4() == nil {
  59. message.Write(client.IP.To16())
  60. message.Write(remote.IP.To16())
  61. }
  62. message.Write(req.Nonce[:])
  63. data := message.Bytes()
  64. md5sum := md5.Sum(data[1:]) // nolint: gas
  65. sha1sum := sha1.Sum(data)
  66. key := append(md5sum[:12], sha1sum[:]...)
  67. iv := md5.Sum(data[2:]) // nolint: gas
  68. return key, iv[:]
  69. }
  70. func makeEncrypterDecrypter(key, iv []byte) (cipher.BlockMode, cipher.BlockMode) {
  71. block, err := aes.NewCipher(key)
  72. if err != nil {
  73. panic(err)
  74. }
  75. return cipher.NewCBCEncrypter(block, iv), cipher.NewCBCDecrypter(block, iv)
  76. }