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_conn_collected.go 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package doppel
  2. import (
  3. "slices"
  4. "sync"
  5. "time"
  6. )
  7. const (
  8. ScoutConnCollectedPreallocSize = 100
  9. )
  10. type ScoutConnResult struct {
  11. timestamp time.Time
  12. recordType byte
  13. payloadLen int
  14. }
  15. type ScoutConnCollected struct {
  16. mu sync.Mutex
  17. data []ScoutConnResult
  18. writeIndex int // index at which client first wrote post-handshake data; -1 if not set
  19. }
  20. func (s *ScoutConnCollected) Add(record byte, payloadLen int) {
  21. s.mu.Lock()
  22. s.data = append(s.data, ScoutConnResult{
  23. timestamp: time.Now(),
  24. recordType: record,
  25. payloadLen: payloadLen,
  26. })
  27. s.mu.Unlock()
  28. }
  29. // MarkWrite records the current data length as the handshake boundary.
  30. func (s *ScoutConnCollected) MarkWrite() {
  31. s.mu.Lock()
  32. if s.writeIndex < 0 {
  33. s.writeIndex = len(s.data)
  34. }
  35. s.mu.Unlock()
  36. }
  37. // Snapshot returns a copy of the collected data and the write index.
  38. func (s *ScoutConnCollected) Snapshot() ([]ScoutConnResult, int) {
  39. s.mu.Lock()
  40. snapshot := slices.Clone(s.data)
  41. writeIndex := s.writeIndex
  42. s.mu.Unlock()
  43. return snapshot, writeIndex
  44. }
  45. func NewScoutConnCollected() *ScoutConnCollected {
  46. return &ScoutConnCollected{
  47. data: make([]ScoutConnResult, 0, ScoutConnCollectedPreallocSize),
  48. writeIndex: -1,
  49. }
  50. }