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
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package mtglib
  2. import (
  3. "crypto/rand"
  4. "encoding/base64"
  5. "encoding/hex"
  6. "fmt"
  7. )
  8. const secretFakeTLSFirstByte byte = 0xee
  9. var secretEmptyKey [SecretKeyLength]byte
  10. // Secret is a data structure that presents a secret.
  11. //
  12. // Telegram secret is not a simple string like
  13. // "ee367a189aee18fa31c190054efd4a8e9573746f726167652e676f6f676c65617069732e636f6d".
  14. // Actually, this is a serialized datastructure of 2 parts: key and host.
  15. //
  16. // ee367a189aee18fa31c190054efd4a8e9573746f726167652e676f6f676c65617069732e636f6d
  17. // |-|-------------------------------|-------------------------------------------
  18. // p key hostname
  19. //
  20. // Serialized secret starts with 'ee'. Actually, in the past we also had 'dd'
  21. // secrets and prefixless ones. But this is history. Currently, we do have only
  22. // 'ee' secrets which mean faketls + protection from statistical attacks on a
  23. // length. 'ee' is a byte 238 (0xee).
  24. //
  25. // After that, we have 16 bytes of the key. This is a random generated secret
  26. // data of the proxy and this data is used to derive authentication schemas.
  27. // These secrets are mixed into hmacs and sha256 checksums which are used to
  28. // build AEAD ciphers for obfuscated2 protocol and ensure faketls handshake.
  29. //
  30. // Host is a domain fronting hostname in latin1 (ASCII) encoding. This hostname
  31. // should be used for SNI in faketls and MTG verifies it. Also, this is when
  32. // mtg gets about a domain fronting hostname.
  33. //
  34. // Secrets can be serialized into 2 forms: hex and base64. If you decode both
  35. // forms into bytes, you'll get the same byte array. Telegram clients nowadays
  36. // accept all forms.
  37. type Secret struct {
  38. // Key is a set of bytes used for traffic authentication.
  39. Key [SecretKeyLength]byte
  40. // Host is a domain fronting hostname.
  41. Host string
  42. }
  43. // MarshalText is to support text.Marshaller interface.
  44. func (s Secret) MarshalText() ([]byte, error) {
  45. if s.Valid() {
  46. return []byte(s.String()), nil
  47. }
  48. return nil, nil
  49. }
  50. // UnmarshalText is to support text.Unmarshaller interface.
  51. func (s *Secret) UnmarshalText(data []byte) error {
  52. return s.Set(string(data))
  53. }
  54. func (s *Secret) Set(text string) error {
  55. if text == "" {
  56. return ErrSecretEmpty
  57. }
  58. decoded, err := hex.DecodeString(text)
  59. if err != nil {
  60. decoded, err = base64.RawURLEncoding.DecodeString(text)
  61. }
  62. if err != nil {
  63. return fmt.Errorf("incorrect secret format: %w", err)
  64. }
  65. if len(decoded) < 2 { // we need at least 1 byte here
  66. return fmt.Errorf("secret is truncated, length=%d", len(decoded))
  67. }
  68. if decoded[0] != secretFakeTLSFirstByte {
  69. return fmt.Errorf("incorrect first byte of secret: %#x", decoded[0])
  70. }
  71. decoded = decoded[1:]
  72. if len(decoded) < SecretKeyLength {
  73. return fmt.Errorf("secret has incorrect length %d", len(decoded))
  74. }
  75. copy(s.Key[:], decoded[:SecretKeyLength])
  76. s.Host = string(decoded[SecretKeyLength:])
  77. if s.Host == "" {
  78. return fmt.Errorf("hostname cannot be empty: %s", text)
  79. }
  80. return nil
  81. }
  82. // Valid checks if this secret is valid and can be used in proxy.
  83. func (s Secret) Valid() bool {
  84. return s.Key != secretEmptyKey && s.Host != ""
  85. }
  86. // String is to support fmt.Stringer interface.
  87. func (s Secret) String() string {
  88. return s.Base64()
  89. }
  90. // Base64 returns a base64-encoded form of this secret.
  91. func (s Secret) Base64() string {
  92. return base64.RawURLEncoding.EncodeToString(s.makeBytes())
  93. }
  94. // Hex returns a hex-encoded form of this secret (ee-secret).
  95. func (s Secret) Hex() string {
  96. return hex.EncodeToString(s.makeBytes())
  97. }
  98. func (s *Secret) makeBytes() []byte {
  99. data := append([]byte{secretFakeTLSFirstByte}, s.Key[:]...)
  100. data = append(data, s.Host...)
  101. return data
  102. }
  103. // GenerateSecret makes a new secret with a given hostname.
  104. func GenerateSecret(hostname string) Secret {
  105. s := Secret{
  106. Host: hostname,
  107. }
  108. if _, err := rand.Read(s.Key[:]); err != nil {
  109. panic(err)
  110. }
  111. return s
  112. }
  113. // ParseSecret parses a secret (both hex and base64 forms).
  114. func ParseSecret(secret string) (Secret, error) {
  115. s := Secret{}
  116. return s, s.UnmarshalText([]byte(secret))
  117. }