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
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

scout.go 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. data, writeIndex := results.Snapshot()
  51. if err != nil || len(data) == 0 {
  52. return ScoutResult{}, err
  53. }
  54. var result ScoutResult
  55. // Compute inter-record durations (existing logic).
  56. lastTimestamp := time.Time{}
  57. for i, v := range data {
  58. if v.recordType != tls.TypeApplicationData {
  59. continue
  60. }
  61. if lastTimestamp.IsZero() {
  62. if i > 0 {
  63. lastTimestamp = data[i-1].timestamp
  64. } else {
  65. lastTimestamp = v.timestamp
  66. }
  67. }
  68. result.Durations = append(result.Durations, v.timestamp.Sub(lastTimestamp))
  69. lastTimestamp = v.timestamp
  70. }
  71. // Compute cert size: sum of ApplicationData payload between CCS and
  72. // the first client Write (which marks the end of server handshake).
  73. seenCCS := false
  74. boundary := writeIndex
  75. if boundary < 0 {
  76. boundary = len(data)
  77. }
  78. for i, v := range data {
  79. if i >= boundary {
  80. break
  81. }
  82. if v.recordType == tls.TypeChangeCipherSpec {
  83. seenCCS = true
  84. continue
  85. }
  86. if seenCCS && v.recordType == tls.TypeApplicationData {
  87. result.CertSize += v.payloadLen
  88. }
  89. }
  90. return result, nil
  91. }
  92. func (s Scout) makeClient() (*http.Client, *ScoutConnCollected) {
  93. dialer := s.network.NativeDialer()
  94. collected := NewScoutConnCollected()
  95. client := s.network.MakeHTTPClient(func(
  96. ctx context.Context,
  97. network string,
  98. address string,
  99. ) (essentials.Conn, error) {
  100. conn, err := dialer.DialContext(ctx, network, address)
  101. if err != nil {
  102. return nil, err
  103. }
  104. return NewScoutConn(essentials.WrapNetConn(conn), collected), nil
  105. })
  106. return client, collected
  107. }
  108. func NewScout(network Network, urls []string) Scout {
  109. return Scout{
  110. network: network,
  111. urls: urls,
  112. }
  113. }