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
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

events.go 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package mtglib
  2. import (
  3. "net"
  4. "time"
  5. )
  6. type eventBase struct {
  7. streamID string
  8. timestamp time.Time
  9. }
  10. // StreamID returns a ID of the stream this event belongs to.
  11. func (e eventBase) StreamID() string {
  12. return e.streamID
  13. }
  14. // Timestamp return a time when this event was generated.
  15. func (e eventBase) Timestamp() time.Time {
  16. return e.timestamp
  17. }
  18. // EventStart is emitted when mtg proxy starts to process a new
  19. // connection.
  20. type EventStart struct {
  21. eventBase
  22. // RemoteIP is an IP address of the client.
  23. RemoteIP net.IP
  24. }
  25. // EventConnectedToDC is emitted when mtg proxy has connected to a
  26. // Telegram server.
  27. type EventConnectedToDC struct {
  28. eventBase
  29. // RemoteIP is an IP address of the Telegram server proxy has been
  30. // connected to.
  31. RemoteIP net.IP
  32. // DC is an index of the datacenter proxy has been connected to.
  33. DC int
  34. }
  35. // EventTraffic is emitted when we read/write some bytes on a connection.
  36. type EventTraffic struct {
  37. eventBase
  38. // Traffic is a count of bytes which were transmitted.
  39. Traffic uint
  40. // IsRead defines if we _read_ or _write_ to connection. A rule of
  41. // thumb is simple: EventTraffic is bound to a remote connection. Not
  42. // to a client one, but either to Telegram or front domain one.
  43. //
  44. // In the case of Telegram, isRead means that we've fetched some bytes
  45. // from Telegram to send it to a client.
  46. //
  47. // In the case of the front domain, it means that we've fetched some
  48. // bytes from this domain to send it to a client.
  49. IsRead bool
  50. }
  51. // EventFinish is emitted when we stop to manage a connection.
  52. type EventFinish struct {
  53. eventBase
  54. }
  55. // EventDomainFronting is emitted when we connect to a front domain
  56. // instead of Telegram server.
  57. type EventDomainFronting struct {
  58. eventBase
  59. }
  60. // EventConcurrencyLimited is emitted when connection was declined
  61. // because of the concurrency limit of the worker pool.
  62. type EventConcurrencyLimited struct {
  63. eventBase
  64. }
  65. // EventIPBlocklisted is emitted when connection was declined because
  66. // IP address was found in IP blocklist.
  67. type EventIPBlocklisted struct {
  68. eventBase
  69. RemoteIP net.IP
  70. IsBlockList bool
  71. }
  72. // EventReplayAttack is emitted when mtg detects a replay attack on a
  73. // connection.
  74. type EventReplayAttack struct {
  75. eventBase
  76. }
  77. // EventIPListSize is emitted when mtg updates a contents of the ip lists:
  78. // allowlist or blocklist.
  79. type EventIPListSize struct {
  80. eventBase
  81. Size int
  82. IsBlockList bool
  83. }
  84. // NewEventStart creates a new EventStart event.
  85. func NewEventStart(streamID string, remoteIP net.IP) EventStart {
  86. return EventStart{
  87. eventBase: eventBase{
  88. timestamp: time.Now(),
  89. streamID: streamID,
  90. },
  91. RemoteIP: remoteIP,
  92. }
  93. }
  94. // NewEventConnectedToDC creates a new EventConnectedToDC event.
  95. func NewEventConnectedToDC(streamID string, remoteIP net.IP, dc int) EventConnectedToDC {
  96. return EventConnectedToDC{
  97. eventBase: eventBase{
  98. timestamp: time.Now(),
  99. streamID: streamID,
  100. },
  101. RemoteIP: remoteIP,
  102. DC: dc,
  103. }
  104. }
  105. // NewEventTraffic creates a new EventTraffic event.
  106. func NewEventTraffic(streamID string, traffic uint, isRead bool) EventTraffic {
  107. return EventTraffic{
  108. eventBase: eventBase{
  109. timestamp: time.Now(),
  110. streamID: streamID,
  111. },
  112. Traffic: traffic,
  113. IsRead: isRead,
  114. }
  115. }
  116. // NewEventFinish creates a new EventFinish event.
  117. func NewEventFinish(streamID string) EventFinish {
  118. return EventFinish{
  119. eventBase: eventBase{
  120. timestamp: time.Now(),
  121. streamID: streamID,
  122. },
  123. }
  124. }
  125. // NewEventDomainFronting creates a new EventDomainFronting event.
  126. func NewEventDomainFronting(streamID string) EventDomainFronting {
  127. return EventDomainFronting{
  128. eventBase: eventBase{
  129. timestamp: time.Now(),
  130. streamID: streamID,
  131. },
  132. }
  133. }
  134. // NewEventConcurrencyLimited creates a new EventConcurrencyLimited
  135. // event.
  136. func NewEventConcurrencyLimited() EventConcurrencyLimited {
  137. return EventConcurrencyLimited{
  138. eventBase: eventBase{
  139. timestamp: time.Now(),
  140. },
  141. }
  142. }
  143. // NewEventIPBlocklisted creates a new EventIPBlocklisted event.
  144. func NewEventIPBlocklisted(remoteIP net.IP) EventIPBlocklisted {
  145. return EventIPBlocklisted{
  146. eventBase: eventBase{
  147. timestamp: time.Now(),
  148. },
  149. RemoteIP: remoteIP,
  150. IsBlockList: true,
  151. }
  152. }
  153. // NewEventIPAllowlisted creates a NewEventIPBlocklisted event with a mark that
  154. // it is supposed to be for allow list.
  155. func NewEventIPAllowlisted(remoteIP net.IP) EventIPBlocklisted {
  156. return EventIPBlocklisted{
  157. eventBase: eventBase{
  158. timestamp: time.Now(),
  159. },
  160. RemoteIP: remoteIP,
  161. IsBlockList: false,
  162. }
  163. }
  164. // NewEventReplayAttack creates a new EventReplayAttack event.
  165. func NewEventReplayAttack(streamID string) EventReplayAttack {
  166. return EventReplayAttack{
  167. eventBase: eventBase{
  168. timestamp: time.Now(),
  169. streamID: streamID,
  170. },
  171. }
  172. }
  173. // NewEventIPListSize creates a new EventIPListSize event.
  174. func NewEventIPListSize(size int, isBlockList bool) EventIPListSize {
  175. return EventIPListSize{
  176. eventBase: eventBase{
  177. timestamp: time.Now(),
  178. },
  179. Size: size,
  180. IsBlockList: isBlockList,
  181. }
  182. }