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 2.7KB

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