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
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

crypt.go 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.RPCNonceRequest,
  20. resp *rpc.RPCNonceResponse, client *net.TCPAddr, secret []byte) wrappers.ReadWriteCloserWithAddr {
  21. remote := conn.Addr()
  22. encKey, encIV := makeKeys(CipherPurposeClient, req, resp, client, remote, secret)
  23. decKey, decIV := makeKeys(CipherPurposeServer, req, resp, client, remote, 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.RPCNonceRequest, resp *rpc.RPCNonceResponse,
  29. client *net.TCPAddr, remote *net.TCPAddr, secret []byte) ([]byte, []byte) {
  30. message := bytes.Buffer{}
  31. message.Write(resp.Nonce[:])
  32. message.Write(req.Nonce[:])
  33. message.Write(req.CryptoTS[:])
  34. clientIPv4 := emptyIP[:]
  35. serverIPv4 := emptyIP[:]
  36. if client.IP.To4() != nil {
  37. clientIPv4 = reverseBytes(client.IP.To4())
  38. serverIPv4 = reverseBytes(remote.IP.To4())
  39. }
  40. message.Write(serverIPv4)
  41. var port [2]byte
  42. binary.LittleEndian.PutUint16(port[:], uint16(client.Port))
  43. message.Write(port[:])
  44. switch purpose {
  45. case CipherPurposeClient:
  46. message.WriteString("CLIENT")
  47. case CipherPurposeServer:
  48. message.WriteString("SERVER")
  49. default:
  50. panic("Unexpected cipher purpose")
  51. }
  52. message.Write(clientIPv4)
  53. binary.LittleEndian.PutUint16(port[:], uint16(remote.Port))
  54. message.Write(port[:])
  55. message.Write(secret)
  56. message.Write(resp.Nonce[:])
  57. if client.IP.To4() == nil {
  58. message.Write(client.IP.To16())
  59. message.Write(remote.IP.To16())
  60. }
  61. message.Write(req.Nonce[:])
  62. data := message.Bytes()
  63. md5sum := md5.Sum(data[1:])
  64. sha1sum := sha1.Sum(data)
  65. key := append(md5sum[:12], sha1sum[:]...)
  66. iv := md5.Sum(data[2:])
  67. return key, iv[:]
  68. }
  69. func makeEncrypterDecrypter(key, iv []byte) (cipher.BlockMode, cipher.BlockMode) {
  70. block, err := aes.NewCipher(key)
  71. if err != nil {
  72. panic(err)
  73. }
  74. return cipher.NewCBCEncrypter(block, iv), cipher.NewCBCDecrypter(block, iv)
  75. }
  76. func reverseBytes(data []byte) []byte {
  77. rv := make([]byte, len(data))
  78. for k, v := range data {
  79. rv[len(data)-1-k] = v
  80. }
  81. return rv
  82. }