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
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

server_hello.go 1.9KB

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