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