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
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

scout.go 3.0KB

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