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

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