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ů.

server_side.go 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package fake
  2. import (
  3. "bytes"
  4. "crypto/hmac"
  5. "crypto/rand"
  6. "crypto/sha256"
  7. "encoding/binary"
  8. "io"
  9. rnd "math/rand/v2"
  10. "github.com/9seconds/mtg/v2/mtglib/internal/doppel"
  11. "github.com/9seconds/mtg/v2/mtglib/internal/tls"
  12. "golang.org/x/crypto/curve25519"
  13. )
  14. const (
  15. TypeHandshakeServer = 0x02
  16. ChangeCipherValue = 0x01
  17. EllipticCurveLen = 32
  18. )
  19. var serverHelloSuffix = []byte{
  20. 0x00, // no compression
  21. 0x00, 0x2e, // 46 bytes of data
  22. 0x00, 0x2b, // Extension - Supported Versions
  23. 0x00, 0x02, // 2 bytes are following
  24. 0x03, 0x04, // TLS 1.3
  25. 0x00, 0x33, // Extension - Key Share
  26. 0x00, 0x24, // 36 bytes
  27. 0x00, 0x1d, // x25519 curve
  28. 0x00, 0x20, // 32 bytes of key
  29. }
  30. func SendServerHello(w io.Writer, secret []byte, clientHello *ClientHello) error {
  31. buf := &bytes.Buffer{}
  32. buf.Grow(tls.MaxRecordSize)
  33. generateServerHello(buf, clientHello)
  34. generateChangeCipherValue(buf)
  35. generateNoise(buf)
  36. packet := buf.Bytes()
  37. digest := hmac.New(sha256.New, secret)
  38. digest.Write(clientHello.Random[:])
  39. digest.Write(packet)
  40. copy(packet[RandomOffset:], digest.Sum(nil))
  41. _, err := w.Write(packet)
  42. return err
  43. }
  44. func generateServerHello(buf *bytes.Buffer, hello *ClientHello) {
  45. payload := acquireBuffer()
  46. defer releaseBuffer(payload)
  47. generateServerHelloPayload(payload, hello)
  48. // 16 - type is 0x16 (handshake record)
  49. // 03 03 - legacy protocol version of "3,3" (TLS 1.2)
  50. // 00 7a - 0x7A (122) bytes of handshake message follows
  51. // 16 - type is 0x16 (handshake record)
  52. buf.WriteByte(tls.TypeHandshake)
  53. // 03 03 - legacy protocol version of "3,3" (TLS 1.2)
  54. buf.Write(tls.TLSVersion[:])
  55. // 00 7a - 0x7A (122) bytes of handshake message follows
  56. binary.Write(buf, binary.BigEndian, uint16(payload.Len())) //nolint: errcheck
  57. payload.WriteTo(buf) //nolint: errcheck
  58. }
  59. func generateServerHelloPayload(buf *bytes.Buffer, hello *ClientHello) {
  60. data := [4]byte{}
  61. payload := acquireBuffer()
  62. defer releaseBuffer(payload)
  63. generateServerHelloHandshakePayload(payload, hello)
  64. // 02 - handshake message type 0x02 (server hello)
  65. // 00 00 76 - 0x76 (118) bytes of server hello data follows
  66. buf.WriteByte(TypeHandshakeServer)
  67. // 00 00 76 - 0x76 (118) bytes of server hello data follows
  68. binary.BigEndian.PutUint32(data[:], uint32(payload.Len()))
  69. buf.Write(data[1:])
  70. payload.WriteTo(buf) //nolint: errcheck
  71. }
  72. func generateServerHelloHandshakePayload(buf *bytes.Buffer, hello *ClientHello) {
  73. // The unusual version number ("3,3" representing TLS 1.2) is due to
  74. // TLS 1.0 being a minor revision of the SSL 3.0 protocol. Therefore
  75. // TLS 1.0 is represented by "3,1", TLS 1.1 is "3,2", and so on.
  76. buf.Write(tls.TLSVersion[:])
  77. buf.Write(emptyRandom[:])
  78. // 20 - 0x20 (32) bytes of session ID follow
  79. // e0 e1 ... fe ff - session ID copied from Client Hello
  80. buf.WriteByte(byte(len(hello.SessionID)))
  81. buf.Write(hello.SessionID)
  82. binary.Write(buf, binary.BigEndian, hello.CipherSuite) //nolint: errcheck
  83. buf.Write(serverHelloSuffix)
  84. scalar := [EllipticCurveLen]byte{}
  85. if _, err := rand.Read(scalar[:]); err != nil {
  86. panic(err)
  87. }
  88. curve, _ := curve25519.X25519(scalar[:], curve25519.Basepoint)
  89. buf.Write(curve)
  90. }
  91. func generateChangeCipherValue(buf *bytes.Buffer) {
  92. buf.WriteByte(tls.TypeChangeCipherSpec)
  93. buf.Write(tls.TLSVersion[:])
  94. binary.Write(buf, binary.BigEndian, uint16(1)) //nolint: errcheck
  95. buf.WriteByte(ChangeCipherValue)
  96. }
  97. func generateNoise(buf *bytes.Buffer) {
  98. data := make(
  99. []byte,
  100. int64(
  101. doppel.TLSRecordSizeStart+rnd.IntN(
  102. doppel.TLSRecordSizeAccel-doppel.TLSRecordSizeStart,
  103. ),
  104. ),
  105. )
  106. if _, err := rand.Read(data[:]); err != nil {
  107. panic(err)
  108. }
  109. tls.WriteRecord(buf, data[:]) //nolint: errcheck
  110. }