interface.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package event
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/pion/webrtc/v4"
  6. "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/ptutil/safelog"
  7. )
  8. type SnowflakeEvent interface {
  9. IsSnowflakeEvent()
  10. String() string
  11. }
  12. type EventOnOfferCreated struct {
  13. SnowflakeEvent
  14. WebRTCLocalDescription *webrtc.SessionDescription
  15. Error error
  16. }
  17. func (e EventOnOfferCreated) String() string {
  18. if e.Error != nil {
  19. scrubbed := safelog.Scrub([]byte(e.Error.Error()))
  20. return fmt.Sprintf("offer creation failure %s", scrubbed)
  21. }
  22. return "offer created"
  23. }
  24. type EventOnBrokerRendezvous struct {
  25. SnowflakeEvent
  26. WebRTCRemoteDescription *webrtc.SessionDescription
  27. Error error
  28. }
  29. func (e EventOnBrokerRendezvous) String() string {
  30. if e.Error != nil {
  31. scrubbed := safelog.Scrub([]byte(e.Error.Error()))
  32. return fmt.Sprintf("broker failure %s", scrubbed)
  33. }
  34. return "broker rendezvous peer received"
  35. }
  36. type EventOnSnowflakeConnected struct {
  37. SnowflakeEvent
  38. }
  39. func (e EventOnSnowflakeConnected) String() string {
  40. return "connected"
  41. }
  42. type EventOnSnowflakeConnectionFailed struct {
  43. SnowflakeEvent
  44. Error error
  45. }
  46. func (e EventOnSnowflakeConnectionFailed) String() string {
  47. scrubbed := safelog.Scrub([]byte(e.Error.Error()))
  48. return fmt.Sprintf("trying a new proxy: %s", scrubbed)
  49. }
  50. type EventOnProxyStarting struct {
  51. SnowflakeEvent
  52. }
  53. func (e EventOnProxyStarting) String() string {
  54. return "Proxy starting"
  55. }
  56. type EventOnProxyClientConnected struct {
  57. SnowflakeEvent
  58. }
  59. func (e EventOnProxyClientConnected) String() string {
  60. return fmt.Sprintf("client connected")
  61. }
  62. type EventOnProxyConnectionOver struct {
  63. SnowflakeEvent
  64. Country string
  65. }
  66. func (e EventOnProxyConnectionOver) String() string {
  67. return fmt.Sprintf("Proxy connection closed")
  68. }
  69. type EventOnProxyStats struct {
  70. SnowflakeEvent
  71. ConnectionCount int
  72. InboundBytes, OutboundBytes int64
  73. InboundUnit, OutboundUnit string
  74. SummaryInterval time.Duration
  75. }
  76. func (e EventOnProxyStats) String() string {
  77. statString := fmt.Sprintf("In the last %v, there were %v completed connections. Traffic Relayed ↓ %v %v (%.2f %v%s), ↑ %v %v (%.2f %v%s).",
  78. e.SummaryInterval.String(), e.ConnectionCount,
  79. e.InboundBytes, e.InboundUnit, float64(e.InboundBytes)/e.SummaryInterval.Seconds(), e.InboundUnit, "/s",
  80. e.OutboundBytes, e.OutboundUnit, float64(e.OutboundBytes)/e.SummaryInterval.Seconds(), e.OutboundUnit, "/s")
  81. return statString
  82. }
  83. type EventOnCurrentNATTypeDetermined struct {
  84. SnowflakeEvent
  85. CurNATType string
  86. }
  87. func (e EventOnCurrentNATTypeDetermined) String() string {
  88. return fmt.Sprintf("NAT type: %v", e.CurNATType)
  89. }
  90. type SnowflakeEventReceiver interface {
  91. // OnNewSnowflakeEvent notify receiver about a new event
  92. // This method MUST not block
  93. OnNewSnowflakeEvent(event SnowflakeEvent)
  94. }
  95. type SnowflakeEventDispatcher interface {
  96. SnowflakeEventReceiver
  97. // AddSnowflakeEventListener allow receiver(s) to receive event notification
  98. // when OnNewSnowflakeEvent is called on the dispatcher.
  99. // Every event listener added will be called when an event is received by the dispatcher.
  100. // The order each listener is called is undefined.
  101. AddSnowflakeEventListener(receiver SnowflakeEventReceiver)
  102. RemoveSnowflakeEventListener(receiver SnowflakeEventReceiver)
  103. }