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 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 Telegram
  26. // server.
  27. type EventConnectedToDC struct {
  28. eventBase
  29. // RemoteIP is an IP address of the Telegram server proxy has been connected
  30. // 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 thumb is
  41. // simple: EventTraffic is bound to a remote connection. Not to a client one,
  42. // but either to Telegram or front domain one.
  43. //
  44. // In the case of Telegram, isRead means that we've fetched some bytes from
  45. // Telegram to send it to a client.
  46. //
  47. // In the case of the front domain, it means that we've fetched some bytes
  48. // 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 instead of
  56. // Telegram server.
  57. type EventDomainFronting struct {
  58. eventBase
  59. }
  60. // EventConcurrencyLimited is emitted when connection was declined because of
  61. // the concurrency limit of the worker pool.
  62. type EventConcurrencyLimited struct {
  63. eventBase
  64. }
  65. // EventIPBlocklisted is emitted when connection was declined because IP
  66. // 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. // EventThrottled is emitted when a connection is rejected because the
  78. // per-user connection cap has been reached.
  79. type EventThrottled struct {
  80. eventBase
  81. SecretName string
  82. }
  83. // EventIPListSize is emitted when mtg updates a contents of the ip lists:
  84. // allowlist or blocklist.
  85. type EventIPListSize struct {
  86. eventBase
  87. Size int
  88. IsBlockList bool
  89. }
  90. // NewEventStart creates a new EventStart event.
  91. func NewEventStart(streamID string, remoteIP net.IP) EventStart {
  92. return EventStart{
  93. eventBase: eventBase{
  94. timestamp: time.Now(),
  95. streamID: streamID,
  96. },
  97. RemoteIP: remoteIP,
  98. }
  99. }
  100. // NewEventConnectedToDC creates a new EventConnectedToDC event.
  101. func NewEventConnectedToDC(streamID string, remoteIP net.IP, dc int) EventConnectedToDC {
  102. return EventConnectedToDC{
  103. eventBase: eventBase{
  104. timestamp: time.Now(),
  105. streamID: streamID,
  106. },
  107. RemoteIP: remoteIP,
  108. DC: dc,
  109. }
  110. }
  111. // NewEventTraffic creates a new EventTraffic event.
  112. func NewEventTraffic(streamID string, traffic uint, isRead bool) EventTraffic {
  113. return EventTraffic{
  114. eventBase: eventBase{
  115. timestamp: time.Now(),
  116. streamID: streamID,
  117. },
  118. Traffic: traffic,
  119. IsRead: isRead,
  120. }
  121. }
  122. // NewEventFinish creates a new EventFinish event.
  123. func NewEventFinish(streamID string) EventFinish {
  124. return EventFinish{
  125. eventBase: eventBase{
  126. timestamp: time.Now(),
  127. streamID: streamID,
  128. },
  129. }
  130. }
  131. // NewEventDomainFronting creates a new EventDomainFronting event.
  132. func NewEventDomainFronting(streamID string) EventDomainFronting {
  133. return EventDomainFronting{
  134. eventBase: eventBase{
  135. timestamp: time.Now(),
  136. streamID: streamID,
  137. },
  138. }
  139. }
  140. // NewEventConcurrencyLimited creates a new EventConcurrencyLimited
  141. // event.
  142. func NewEventConcurrencyLimited() EventConcurrencyLimited {
  143. return EventConcurrencyLimited{
  144. eventBase: eventBase{
  145. timestamp: time.Now(),
  146. },
  147. }
  148. }
  149. // NewEventIPBlocklisted creates a new EventIPBlocklisted event.
  150. func NewEventIPBlocklisted(remoteIP net.IP) EventIPBlocklisted {
  151. return EventIPBlocklisted{
  152. eventBase: eventBase{
  153. timestamp: time.Now(),
  154. },
  155. RemoteIP: remoteIP,
  156. IsBlockList: true,
  157. }
  158. }
  159. // NewEventIPAllowlisted creates a NewEventIPBlocklisted event with a mark that
  160. // it is supposed to be for allow list.
  161. func NewEventIPAllowlisted(remoteIP net.IP) EventIPBlocklisted {
  162. return EventIPBlocklisted{
  163. eventBase: eventBase{
  164. timestamp: time.Now(),
  165. },
  166. RemoteIP: remoteIP,
  167. IsBlockList: false,
  168. }
  169. }
  170. // NewEventReplayAttack creates a new EventReplayAttack event.
  171. func NewEventReplayAttack(streamID string) EventReplayAttack {
  172. return EventReplayAttack{
  173. eventBase: eventBase{
  174. timestamp: time.Now(),
  175. streamID: streamID,
  176. },
  177. }
  178. }
  179. // NewEventThrottled creates a new EventThrottled event.
  180. func NewEventThrottled(streamID, secretName string) EventThrottled {
  181. return EventThrottled{
  182. eventBase: eventBase{
  183. timestamp: time.Now(),
  184. streamID: streamID,
  185. },
  186. SecretName: secretName,
  187. }
  188. }
  189. // NewEventIPListSize creates a new EventIPListSize event.
  190. func NewEventIPListSize(size int, isBlockList bool) EventIPListSize {
  191. return EventIPListSize{
  192. eventBase: eventBase{
  193. timestamp: time.Now(),
  194. },
  195. Size: size,
  196. IsBlockList: isBlockList,
  197. }
  198. }