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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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/wrappers"
  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 NewMiddleProxyCipherRWC(conn wrappers.ReadWriteCloserWithAddr, req *rpc.NonceRequest, resp *rpc.NonceResponse, secret []byte) wrappers.ReadWriteCloserWithAddr {
  20. localAddr := conn.LocalAddr()
  21. remoteAddr := conn.RemoteAddr()
  22. encKey, encIV := makeKeys(CipherPurposeClient, req, resp, localAddr, remoteAddr, secret)
  23. decKey, decIV := makeKeys(CipherPurposeServer, req, resp, localAddr, remoteAddr, secret)
  24. enc, _ := makeEncrypterDecrypter(encKey, encIV)
  25. _, dec := makeEncrypterDecrypter(decKey, decIV)
  26. return wrappers.NewBlockCipherRWC(conn, enc, dec)
  27. }
  28. func makeKeys(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 = reverseBytes(client.IP.To4())
  37. serverIPv4 = 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. }
  75. func reverseBytes(data []byte) []byte {
  76. dataLen := len(data)
  77. rv := make([]byte, dataLen)
  78. rv[dataLen/2] = data[dataLen/2]
  79. for i := dataLen/2 - 1; i >= 0; i-- {
  80. opp := dataLen - i - 1
  81. rv[i], rv[opp] = data[opp], data[i]
  82. }
  83. return rv
  84. }