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

client_protocol.go 3.0KB

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