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

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