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
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

scout.go 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package doppel
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "strings"
  8. "time"
  9. "github.com/9seconds/mtg/v2/essentials"
  10. "github.com/9seconds/mtg/v2/mtglib/internal/tls"
  11. )
  12. // ScoutResult holds measurements from a single scout HTTP request.
  13. type ScoutResult struct {
  14. Durations []time.Duration
  15. CertSize int // total ApplicationData bytes during TLS handshake; 0 if unknown
  16. }
  17. type Scout struct {
  18. network Network
  19. urls []string
  20. }
  21. func (s Scout) Learn(ctx context.Context) (ScoutResult, error) {
  22. var combined ScoutResult
  23. for _, url := range s.urls {
  24. learned, err := s.learn(ctx, url)
  25. if err != nil {
  26. return ScoutResult{}, err
  27. }
  28. combined.Durations = append(combined.Durations, learned.Durations...)
  29. if learned.CertSize > 0 && combined.CertSize == 0 {
  30. combined.CertSize = learned.CertSize
  31. }
  32. }
  33. return combined, nil
  34. }
  35. func (s Scout) learn(ctx context.Context, url string) (ScoutResult, error) {
  36. client, results := s.makeClient()
  37. if !strings.HasPrefix(url, "https://") {
  38. return ScoutResult{}, fmt.Errorf("url %s must be https", url)
  39. }
  40. req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
  41. if err != nil {
  42. return ScoutResult{}, err
  43. }
  44. resp, err := client.Do(req)
  45. if resp != nil {
  46. io.Copy(io.Discard, resp.Body) //nolint: errcheck
  47. resp.Body.Close() //nolint: errcheck
  48. client.CloseIdleConnections()
  49. }
  50. if err != nil {
  51. return ScoutResult{}, err
  52. }
  53. data, writeIndex := results.Snapshot()
  54. if len(data) == 0 {
  55. return ScoutResult{}, nil
  56. }
  57. var result ScoutResult
  58. // Compute inter-record durations (existing logic).
  59. lastTimestamp := time.Time{}
  60. for i, v := range data {
  61. if v.recordType != tls.TypeApplicationData {
  62. continue
  63. }
  64. if lastTimestamp.IsZero() {
  65. if i > 0 {
  66. lastTimestamp = data[i-1].timestamp
  67. } else {
  68. lastTimestamp = v.timestamp
  69. }
  70. }
  71. result.Durations = append(result.Durations, v.timestamp.Sub(lastTimestamp))
  72. lastTimestamp = v.timestamp
  73. }
  74. // Compute cert size: sum of ApplicationData payload between CCS and
  75. // the first client Write (which marks the end of server handshake).
  76. seenCCS := false
  77. boundary := writeIndex
  78. if boundary < 0 {
  79. boundary = len(data)
  80. }
  81. for i, v := range data {
  82. if i >= boundary {
  83. break
  84. }
  85. if v.recordType == tls.TypeChangeCipherSpec {
  86. seenCCS = true
  87. continue
  88. }
  89. if seenCCS && v.recordType == tls.TypeApplicationData {
  90. result.CertSize += v.payloadLen
  91. }
  92. }
  93. return result, nil
  94. }
  95. func (s Scout) makeClient() (*http.Client, *ScoutConnCollected) {
  96. dialer := s.network.NativeDialer()
  97. collected := NewScoutConnCollected()
  98. client := s.network.MakeHTTPClient(func(
  99. ctx context.Context,
  100. network string,
  101. address string,
  102. ) (essentials.Conn, error) {
  103. conn, err := dialer.DialContext(ctx, network, address)
  104. if err != nil {
  105. return nil, err
  106. }
  107. return NewScoutConn(essentials.WrapNetConn(conn), collected), nil
  108. })
  109. return client, collected
  110. }
  111. func NewScout(network Network, urls []string) Scout {
  112. return Scout{
  113. network: network,
  114. urls: urls,
  115. }
  116. }