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
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

proxy_test.go 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package mtglib_test
  2. import (
  3. "crypto/tls"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "net"
  8. "net/http"
  9. "testing"
  10. "time"
  11. "github.com/9seconds/mtg/v2/antireplay"
  12. "github.com/9seconds/mtg/v2/events"
  13. "github.com/9seconds/mtg/v2/ipblocklist"
  14. "github.com/9seconds/mtg/v2/ipblocklist/files"
  15. "github.com/9seconds/mtg/v2/logger"
  16. "github.com/9seconds/mtg/v2/mtglib"
  17. "github.com/9seconds/mtg/v2/network"
  18. "github.com/stretchr/testify/suite"
  19. "github.com/yl2chen/cidranger"
  20. )
  21. type ProxyTestSuite struct {
  22. suite.Suite
  23. opts *mtglib.ProxyOpts
  24. p *mtglib.Proxy
  25. listener net.Listener
  26. }
  27. func (suite *ProxyTestSuite) ProxyAddress() string {
  28. _, port, _ := net.SplitHostPort(suite.listener.Addr().String())
  29. return net.JoinHostPort("127.0.0.1", port)
  30. }
  31. func (suite *ProxyTestSuite) ProxySecret() string {
  32. return suite.opts.Secret.Hex()
  33. }
  34. func (suite *ProxyTestSuite) SetupSuite() {
  35. dialer, err := network.NewDefaultDialer(0, 0)
  36. suite.NoError(err)
  37. ntw, err := network.NewNetwork(dialer, "mtgtest", "1.1.1.1", 0)
  38. suite.NoError(err)
  39. allowlist, _ := ipblocklist.NewFireholFromFiles(
  40. logger.NewNoopLogger(),
  41. 1,
  42. []files.File{
  43. files.NewMem([]*net.IPNet{
  44. cidranger.AllIPv4,
  45. cidranger.AllIPv6,
  46. }),
  47. },
  48. nil,
  49. )
  50. go allowlist.Run(time.Second)
  51. suite.opts = &mtglib.ProxyOpts{
  52. Secret: mtglib.GenerateSecret("httpbin.org"),
  53. Network: ntw,
  54. AntiReplayCache: antireplay.NewNoop(),
  55. IPBlocklist: ipblocklist.NewNoop(),
  56. IPAllowlist: allowlist,
  57. EventStream: events.NewNoopStream(),
  58. Logger: logger.NewNoopLogger(),
  59. UseTestDCs: true,
  60. }
  61. proxy, err := mtglib.NewProxy(*suite.opts)
  62. suite.NoError(err)
  63. suite.p = proxy
  64. listener, err := net.Listen("tcp", ":0")
  65. suite.NoError(err)
  66. suite.listener = listener
  67. go suite.p.Serve(suite.listener) //nolint: errcheck
  68. }
  69. func (suite *ProxyTestSuite) TearDownSuite() {
  70. if suite.listener != nil {
  71. suite.listener.Close() //nolint: errcheck
  72. }
  73. if suite.p != nil {
  74. suite.p.Shutdown()
  75. }
  76. }
  77. func (suite *ProxyTestSuite) TestCannotInitNoSecret() {
  78. opts := *suite.opts
  79. opts.Secret = mtglib.Secret{}
  80. _, err := mtglib.NewProxy(opts)
  81. suite.Error(err)
  82. }
  83. func (suite *ProxyTestSuite) TestCannotInitNoNetwork() {
  84. opts := *suite.opts
  85. opts.Network = nil
  86. _, err := mtglib.NewProxy(opts)
  87. suite.Error(err)
  88. }
  89. func (suite *ProxyTestSuite) TestCannotInitNoAntiReplayCache() {
  90. opts := *suite.opts
  91. opts.AntiReplayCache = nil
  92. _, err := mtglib.NewProxy(opts)
  93. suite.Error(err)
  94. }
  95. func (suite *ProxyTestSuite) TestCannotInitNoIPBlocklist() {
  96. opts := *suite.opts
  97. opts.IPBlocklist = nil
  98. _, err := mtglib.NewProxy(opts)
  99. suite.Error(err)
  100. }
  101. func (suite *ProxyTestSuite) TestCannotInitNoIPAllowlist() {
  102. opts := *suite.opts
  103. opts.IPAllowlist = nil
  104. _, err := mtglib.NewProxy(opts)
  105. suite.Error(err)
  106. }
  107. func (suite *ProxyTestSuite) TestCannotInitNoEventStream() {
  108. opts := *suite.opts
  109. opts.EventStream = nil
  110. _, err := mtglib.NewProxy(opts)
  111. suite.Error(err)
  112. }
  113. func (suite *ProxyTestSuite) TestCannotInitNoLogger() {
  114. opts := *suite.opts
  115. opts.Logger = nil
  116. _, err := mtglib.NewProxy(opts)
  117. suite.Error(err)
  118. }
  119. func (suite *ProxyTestSuite) TestCannotInitIncorrectPreferIP() {
  120. opts := *suite.opts
  121. opts.PreferIP = "xxx"
  122. _, err := mtglib.NewProxy(opts)
  123. suite.Error(err)
  124. }
  125. func (suite *ProxyTestSuite) TestDomainFrontingAddress() {
  126. suite.Equal("httpbin.org:443", suite.p.DomainFrontingAddress())
  127. }
  128. func (suite *ProxyTestSuite) TestHTTPSRequest() {
  129. client := &http.Client{
  130. Transport: &http.Transport{
  131. TLSClientConfig: &tls.Config{
  132. InsecureSkipVerify: true,
  133. },
  134. },
  135. Timeout: 5 * time.Second,
  136. }
  137. addr := fmt.Sprintf("https://%s/headers", suite.ProxyAddress())
  138. resp, err := client.Get(addr) //nolint: noctx
  139. suite.NoError(err)
  140. defer resp.Body.Close() //nolint: errcheck
  141. suite.Equal(http.StatusOK, resp.StatusCode)
  142. data, err := io.ReadAll(resp.Body)
  143. suite.NoError(err)
  144. jsonStruct := struct {
  145. Headers struct {
  146. TraceID string `json:"X-Amzn-Trace-Id"` //nolint: tagliatelle
  147. } `json:"headers"`
  148. }{}
  149. suite.NoError(json.Unmarshal(data, &jsonStruct))
  150. suite.NotEmpty(jsonStruct.Headers.TraceID)
  151. }
  152. func TestProxy(t *testing.T) {
  153. t.Parallel()
  154. suite.Run(t, &ProxyTestSuite{})
  155. }