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

server_hello.go 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package tlstypes
  2. import (
  3. "bytes"
  4. "crypto/hmac"
  5. "crypto/rand"
  6. "crypto/sha256"
  7. "io"
  8. mrand "math/rand"
  9. "github.com/9seconds/mtg/config"
  10. "golang.org/x/crypto/curve25519"
  11. )
  12. type ServerHello struct {
  13. Handshake
  14. clientHello *ClientHello
  15. }
  16. func (s ServerHello) WelcomePacket() []byte {
  17. buf := &bytes.Buffer{}
  18. s.Random = [32]byte{}
  19. rec := Record{
  20. Type: RecordTypeHandshake,
  21. Version: Version12,
  22. Data: &s,
  23. }
  24. rec.WriteBytes(buf)
  25. recChangeCipher := Record{
  26. Type: RecordTypeChangeCipherSpec,
  27. Version: Version12,
  28. Data: RawBytes([]byte{0x01}),
  29. }
  30. recChangeCipher.WriteBytes(buf)
  31. hostCert := make([]byte, 1024+mrand.Intn(3092)) // nolint: gosec
  32. rand.Read(hostCert) // nolint: errcheck
  33. recData := Record{
  34. Type: RecordTypeApplicationData,
  35. Version: Version12,
  36. Data: RawBytes(hostCert),
  37. }
  38. recData.WriteBytes(buf)
  39. packet := buf.Bytes()
  40. mac := hmac.New(sha256.New, config.C.Secret)
  41. mac.Write(s.clientHello.Random[:]) // nolint: errcheck
  42. mac.Write(packet) // nolint: errcheck
  43. copy(packet[11:], mac.Sum(nil))
  44. return packet
  45. }
  46. func NewServerHello(clientHello *ClientHello) *ServerHello {
  47. rv := &ServerHello{
  48. clientHello: clientHello,
  49. }
  50. rv.Type = HandshakeTypeServer
  51. rv.Version = Version12
  52. rv.SessionID = make([]byte, len(clientHello.SessionID))
  53. copy(rv.SessionID, clientHello.SessionID)
  54. tail := bytes.NewBuffer(CipherSuiteType_TLS_AES_128_GCM_SHA256_Bytes)
  55. tail.WriteByte(0x00) // no compression
  56. makeTLSExtensions(tail)
  57. rv.Tail = RawBytes(tail.Bytes())
  58. return rv
  59. }
  60. func makeTLSExtensions(buf io.Writer) {
  61. buf.Write([]byte{ // nolint: errcheck
  62. 0x00, 0x2e, // 46 bytes of data
  63. 0x00, 0x33, // Extension - Key Share
  64. 0x00, 0x24, // 36 bytes
  65. 0x00, 0x1d, // x25519 curve
  66. 0x00, 0x20, // 32 bytes of key
  67. })
  68. var scalar [32]byte
  69. rand.Read(scalar[:]) // nolint: errcheck
  70. curve, _ := curve25519.X25519(scalar[:], curve25519.Basepoint)
  71. buf.Write(curve) // nolint: errcheck
  72. buf.Write([]byte{ // nolint: errcheck
  73. 0x00, 0x2b, // Extension - Supported Versions
  74. 0x00, 0x02, // 2 bytes are following
  75. 0x03, 0x04, // TLS 1.3
  76. })
  77. }