interface.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. InboundTraffic int64
  65. OutboundTraffic int64
  66. }
  67. func (e EventOnProxyConnectionOver) String() string {
  68. return fmt.Sprintf("Proxy connection closed")
  69. }
  70. type EventOnProxyStats struct {
  71. SnowflakeEvent
  72. ConnectionCount int
  73. InboundBytes, OutboundBytes int64
  74. InboundUnit, OutboundUnit string
  75. SummaryInterval time.Duration
  76. }
  77. func (e EventOnProxyStats) String() string {
  78. statString := fmt.Sprintf("In the last %v, there were %v completed connections. Traffic Relayed ↓ %v %v (%.2f %v%s), ↑ %v %v (%.2f %v%s).",
  79. e.SummaryInterval.String(), e.ConnectionCount,
  80. e.InboundBytes, e.InboundUnit, float64(e.InboundBytes)/e.SummaryInterval.Seconds(), e.InboundUnit, "/s",
  81. e.OutboundBytes, e.OutboundUnit, float64(e.OutboundBytes)/e.SummaryInterval.Seconds(), e.OutboundUnit, "/s")
  82. return statString
  83. }
  84. type EventOnCurrentNATTypeDetermined struct {
  85. SnowflakeEvent
  86. CurNATType string
  87. }
  88. func (e EventOnCurrentNATTypeDetermined) String() string {
  89. return fmt.Sprintf("NAT type: %v", e.CurNATType)
  90. }
  91. type SnowflakeEventReceiver interface {
  92. // OnNewSnowflakeEvent notify receiver about a new event
  93. // This method MUST not block
  94. OnNewSnowflakeEvent(event SnowflakeEvent)
  95. }
  96. type SnowflakeEventDispatcher interface {
  97. SnowflakeEventReceiver
  98. // AddSnowflakeEventListener allow receiver(s) to receive event notification
  99. // when OnNewSnowflakeEvent is called on the dispatcher.
  100. // Every event listener added will be called when an event is received by the dispatcher.
  101. // The order each listener is called is undefined.
  102. AddSnowflakeEventListener(receiver SnowflakeEventReceiver)
  103. RemoveSnowflakeEventListener(receiver SnowflakeEventReceiver)
  104. }