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.

mtproto_cipher.go 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package wrappers
  2. import (
  3. "bytes"
  4. "crypto/aes"
  5. "crypto/cipher"
  6. "crypto/md5"
  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. func NewMiddleProxyCipher(conn StreamReadWriteCloser, req *rpc.NonceRequest, resp *rpc.NonceResponse, secret []byte) StreamReadWriteCloser {
  20. localAddr := conn.LocalAddr()
  21. remoteAddr := conn.RemoteAddr()
  22. encKey, encIV := deriveKeys(CipherPurposeClient, req, resp, localAddr, remoteAddr, secret)
  23. decKey, decIV := deriveKeys(CipherPurposeServer, req, resp, localAddr, remoteAddr, secret)
  24. enc, _ := makeEncrypterDecrypter(encKey, encIV)
  25. _, dec := makeEncrypterDecrypter(decKey, decIV)
  26. return NewBlockCipher(conn, enc, dec)
  27. }
  28. func deriveKeys(purpose CipherPurpose, req *rpc.NonceRequest, resp *rpc.NonceResponse, client *net.TCPAddr, remote *net.TCPAddr, secret []byte) ([]byte, []byte) {
  29. message := bytes.Buffer{}
  30. message.Write(resp.Nonce[:])
  31. message.Write(req.Nonce[:])
  32. message.Write(req.CryptoTS[:])
  33. clientIPv4 := emptyIP[:]
  34. serverIPv4 := emptyIP[:]
  35. if client.IP.To4() != nil {
  36. clientIPv4 = utils.ReverseBytes(client.IP.To4())
  37. serverIPv4 = utils.ReverseBytes(remote.IP.To4())
  38. }
  39. message.Write(serverIPv4)
  40. var port [2]byte
  41. binary.LittleEndian.PutUint16(port[:], uint16(client.Port))
  42. message.Write(port[:])
  43. switch purpose {
  44. case CipherPurposeClient:
  45. message.WriteString("CLIENT")
  46. case CipherPurposeServer:
  47. message.WriteString("SERVER")
  48. default:
  49. panic("Unexpected cipher purpose")
  50. }
  51. message.Write(clientIPv4)
  52. binary.LittleEndian.PutUint16(port[:], uint16(remote.Port))
  53. message.Write(port[:])
  54. message.Write(secret)
  55. message.Write(resp.Nonce[:])
  56. if client.IP.To4() == nil {
  57. message.Write(client.IP.To16())
  58. message.Write(remote.IP.To16())
  59. }
  60. message.Write(req.Nonce[:])
  61. data := message.Bytes()
  62. md5sum := md5.Sum(data[1:])
  63. sha1sum := sha1.Sum(data)
  64. key := append(md5sum[:12], sha1sum[:]...)
  65. iv := md5.Sum(data[2:])
  66. return key, iv[:]
  67. }
  68. func makeEncrypterDecrypter(key, iv []byte) (cipher.BlockMode, cipher.BlockMode) {
  69. block, err := aes.NewCipher(key)
  70. if err != nil {
  71. panic(err)
  72. }
  73. return cipher.NewCBCEncrypter(block, iv), cipher.NewCBCDecrypter(block, iv)
  74. }