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
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

client_protocol.go 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package obfuscated2
  2. import (
  3. "bytes"
  4. "crypto/sha256"
  5. "encoding/binary"
  6. "io"
  7. "time"
  8. "github.com/juju/errors"
  9. "github.com/9seconds/mtg/antireplay"
  10. "github.com/9seconds/mtg/config"
  11. "github.com/9seconds/mtg/conntypes"
  12. "github.com/9seconds/mtg/protocol"
  13. "github.com/9seconds/mtg/utils"
  14. "github.com/9seconds/mtg/wrappers"
  15. )
  16. const clientProtocolHandshakeTimeout = 10 * time.Second
  17. type ClientProtocol struct {
  18. protocol.BaseProtocol
  19. }
  20. func (c *ClientProtocol) Handshake(socket wrappers.StreamReadWriteCloser) (wrappers.StreamReadWriteCloser, error) {
  21. fm, err := c.ReadFrame(socket)
  22. if err != nil {
  23. return nil, errors.Annotate(err, "Cannot make client handshake")
  24. }
  25. decHasher := sha256.New()
  26. decHasher.Write(fm.Key()) // nolint: errcheck
  27. decHasher.Write(config.C.Secret) // nolint: errcheck
  28. decryptor := utils.MakeStreamCipher(decHasher.Sum(nil), fm.IV())
  29. invertedFrame := fm.Invert()
  30. encHasher := sha256.New()
  31. encHasher.Write(invertedFrame.Key()) // nolint: errcheck
  32. encHasher.Write(config.C.Secret) // nolint: errcheck
  33. encryptor := utils.MakeStreamCipher(encHasher.Sum(nil), invertedFrame.IV())
  34. decryptedFrame := Frame{}
  35. decryptor.XORKeyStream(decryptedFrame.Bytes(), fm.Bytes())
  36. magic := decryptedFrame.Magic()
  37. switch {
  38. case bytes.Equal(magic, conntypes.ConnectionTagAbridged):
  39. c.ConnectionType = conntypes.ConnectionTypeAbridged
  40. case bytes.Equal(magic, conntypes.ConnectionTagIntermediate):
  41. c.ConnectionType = conntypes.ConnectionTypeIntermediate
  42. case bytes.Equal(magic, conntypes.ConnectionTagSecure):
  43. c.ConnectionType = conntypes.ConnectionTypeSecure
  44. default:
  45. return nil, errors.New("Unknown connection type")
  46. }
  47. c.ConnectionProtocol = conntypes.ConnectionProtocolIPv4
  48. if socket.LocalAddr().IP.To4() == nil {
  49. c.ConnectionProtocol = conntypes.ConnectionProtocolIPv6
  50. }
  51. buf := bytes.NewReader(decryptedFrame.DC())
  52. if err := binary.Read(buf, binary.LittleEndian, &c.DC); err != nil {
  53. c.DC = conntypes.DCDefaultIdx
  54. }
  55. antiReplayKey := decryptedFrame.Unique()
  56. if antireplay.Has(antiReplayKey) {
  57. return nil, errors.New("Replay attack is detected")
  58. }
  59. antireplay.Add(antiReplayKey)
  60. return wrappers.NewObfuscated2(socket, encryptor, decryptor), nil
  61. }
  62. func (c *ClientProtocol) ReadFrame(socket wrappers.StreamReader) (fm Frame, err error) {
  63. if _, err = io.ReadFull(handshakeReader{socket}, fm.Bytes()); err != nil {
  64. err = errors.Annotate(err, "Cannot extract obfuscated2 frame")
  65. }
  66. return
  67. }
  68. type handshakeReader struct {
  69. parent wrappers.StreamReader
  70. }
  71. func (h handshakeReader) Read(p []byte) (int, error) {
  72. return h.parent.ReadTimeout(p, clientProtocolHandshakeTimeout)
  73. }
  74. func MakeClientProtocol() protocol.ClientProtocol {
  75. return &ClientProtocol{}
  76. }