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.

client_side_snapshot_test.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package fake_test
  2. import (
  3. "bytes"
  4. "encoding/base64"
  5. "encoding/json"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "testing"
  10. "github.com/9seconds/mtg/v2/mtglib"
  11. "github.com/9seconds/mtg/v2/mtglib/internal/tls/fake"
  12. "github.com/stretchr/testify/assert"
  13. "github.com/stretchr/testify/mock"
  14. "github.com/stretchr/testify/require"
  15. "github.com/stretchr/testify/suite"
  16. )
  17. type clientHelloSnapshot struct {
  18. Time int `json:"time"`
  19. Random string `json:"random"`
  20. SessionID string `json:"sessionId"`
  21. Host string `json:"host"`
  22. CipherSuite int `json:"cipherSuite"`
  23. Full string `json:"full"`
  24. }
  25. func (c clientHelloSnapshot) GetRandom() []byte {
  26. data, _ := base64.StdEncoding.DecodeString(c.Random)
  27. return data
  28. }
  29. func (c clientHelloSnapshot) GetSessionID() []byte {
  30. data, _ := base64.StdEncoding.DecodeString(c.SessionID)
  31. return data
  32. }
  33. func (c clientHelloSnapshot) GetCipherSuite() uint16 {
  34. return uint16(c.CipherSuite)
  35. }
  36. func (c clientHelloSnapshot) GetFull() []byte {
  37. data, _ := base64.StdEncoding.DecodeString(c.Full)
  38. return data
  39. }
  40. type ParseClientHelloSnapshotTestSuite struct {
  41. suite.Suite
  42. secret mtglib.Secret
  43. }
  44. func (suite *ParseClientHelloSnapshotTestSuite) SetupSuite() {
  45. parsed, err := mtglib.ParseSecret(
  46. "ee367a189aee18fa31c190054efd4a8e9573746f726167652e676f6f676c65617069732e636f6d",
  47. )
  48. require.NoError(suite.T(), err)
  49. suite.secret = parsed
  50. }
  51. func (suite *ParseClientHelloSnapshotTestSuite) makeConn(data []byte) *parseClientHelloConnMock {
  52. readBuf := &bytes.Buffer{}
  53. readBuf.Write(data)
  54. connMock := &parseClientHelloConnMock{
  55. readBuf: readBuf,
  56. }
  57. connMock.
  58. On("SetReadDeadline", mock.AnythingOfType("time.Time")).
  59. Twice().
  60. Return(nil)
  61. return connMock
  62. }
  63. func (suite *ParseClientHelloSnapshotTestSuite) TestSnapshotOk() {
  64. files, err := os.ReadDir("testdata")
  65. require.NoError(suite.T(), err)
  66. for _, v := range files {
  67. if !strings.HasPrefix(v.Name(), "client-hello-ok") {
  68. continue
  69. }
  70. path := filepath.Join("testdata", v.Name())
  71. suite.T().Run(v.Name(), func(t *testing.T) {
  72. fileData, err := os.ReadFile(path)
  73. assert.NoError(t, err)
  74. snapshot := &clientHelloSnapshot{}
  75. assert.NoError(t, json.Unmarshal(fileData, snapshot))
  76. connMock := suite.makeConn(snapshot.GetFull())
  77. defer connMock.AssertExpectations(t)
  78. hello, err := fake.ReadClientHello(
  79. connMock,
  80. suite.secret.Key[:],
  81. suite.secret.Host,
  82. TolerateTime,
  83. )
  84. require.NoError(t, err)
  85. assert.Equal(t, snapshot.GetRandom(), hello.Random[:])
  86. assert.Equal(t, snapshot.GetSessionID(), hello.SessionID)
  87. assert.Equal(t, snapshot.GetCipherSuite(), hello.CipherSuite)
  88. })
  89. }
  90. }
  91. func (suite *ParseClientHelloSnapshotTestSuite) TestSnapshotBad() {
  92. files, err := os.ReadDir("testdata")
  93. require.NoError(suite.T(), err)
  94. for _, v := range files {
  95. if !strings.HasPrefix(v.Name(), "client-hello-bad") {
  96. continue
  97. }
  98. path := filepath.Join("testdata", v.Name())
  99. suite.T().Run(v.Name(), func(t *testing.T) {
  100. fileData, err := os.ReadFile(path)
  101. assert.NoError(t, err)
  102. snapshot := &clientHelloSnapshot{}
  103. assert.NoError(t, json.Unmarshal(fileData, snapshot))
  104. connMock := suite.makeConn(snapshot.GetFull())
  105. defer connMock.AssertExpectations(t)
  106. _, err = fake.ReadClientHello(
  107. connMock,
  108. suite.secret.Key[:],
  109. suite.secret.Host,
  110. TolerateTime,
  111. )
  112. assert.ErrorIs(t, err, fake.ErrBadDigest)
  113. })
  114. }
  115. }
  116. func TestParseClientHelloSnapshot(t *testing.T) {
  117. t.Parallel()
  118. suite.Run(t, &ParseClientHelloSnapshotTestSuite{})
  119. }