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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package faketls
  2. import (
  3. "bufio"
  4. "encoding/binary"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "time"
  9. "github.com/9seconds/mtg/antireplay"
  10. "github.com/9seconds/mtg/conntypes"
  11. "github.com/9seconds/mtg/obfuscated2"
  12. "github.com/9seconds/mtg/protocol"
  13. "github.com/9seconds/mtg/stats"
  14. "github.com/9seconds/mtg/tlstypes"
  15. "github.com/9seconds/mtg/wrappers/stream"
  16. )
  17. type ClientProtocol struct {
  18. obfuscated2.ClientProtocol
  19. }
  20. func (c *ClientProtocol) Handshake(socket conntypes.StreamReadWriteCloser) (conntypes.StreamReadWriteCloser, error) {
  21. rewinded := stream.NewRewind(socket)
  22. bufferedReader := bufio.NewReader(rewinded)
  23. for _, expected := range faketlsStartBytes {
  24. if actual, err := bufferedReader.ReadByte(); err != nil || actual != expected {
  25. fmt.Println("!!!!!!!!!!!! ERROR !!!!!!!!!!!!", err)
  26. return nil, errors.New("qqq")
  27. }
  28. }
  29. rewinded.Rewind()
  30. rewinded = stream.NewRewind(rewinded)
  31. if err := c.tlsHandshake(rewinded); err != nil {
  32. fmt.Println("!!!!!!!!!!!! ERROR !!!!!!!!!!!!", err)
  33. return nil, errors.New("qqq")
  34. }
  35. conn := stream.NewFakeTLS(socket)
  36. conn, err := c.ClientProtocol.Handshake(conn)
  37. if err != nil {
  38. return nil, err
  39. }
  40. return conn, err
  41. }
  42. func (c *ClientProtocol) tlsHandshake(conn io.ReadWriter) error {
  43. helloRecord, err := tlstypes.ReadRecord(conn)
  44. if err != nil {
  45. return fmt.Errorf("cannot read initial record: %w", err)
  46. }
  47. clientHello, err := tlstypes.ParseClientHello(helloRecord.Data.Bytes())
  48. if err != nil {
  49. return fmt.Errorf("cannot parse client hello: %w", err)
  50. }
  51. digest := clientHello.Digest()
  52. for i := 0; i < len(digest)-4; i++ {
  53. if digest[i] != 0 {
  54. return errBadDigest
  55. }
  56. }
  57. timestamp := int64(binary.LittleEndian.Uint32(digest[len(digest)-4:]))
  58. createdAt := time.Unix(timestamp, 0)
  59. timeDiff := time.Since(createdAt)
  60. if (timeDiff > TimeSkew || timeDiff < -TimeSkew) && timestamp > TimeFromBoot {
  61. return errBadTime
  62. }
  63. if antireplay.Cache.HasTLS(clientHello.Random[:]) {
  64. stats.Stats.AntiReplayDetected()
  65. return errors.New("antireplay detected")
  66. }
  67. antireplay.Cache.AddTLS(clientHello.Random[:])
  68. hostCert, err := connectionServerInstance.get()
  69. if err != nil {
  70. return fmt.Errorf("cannot get host certificate: %w", err)
  71. }
  72. serverHello := tlstypes.NewServerHello(clientHello)
  73. serverHelloPacket := serverHello.WelcomePacket(hostCert)
  74. if _, err := conn.Write(serverHelloPacket); err != nil {
  75. return fmt.Errorf("cannot send welcome packet: %w", err)
  76. }
  77. return nil
  78. }
  79. func MakeClientProtocol() protocol.ClientProtocol {
  80. return &ClientProtocol{}
  81. }