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个字符

events.go 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. }
  71. // EventReplayAttack is emitted when mtg detects a replay attack on a
  72. // connection.
  73. type EventReplayAttack struct {
  74. eventBase
  75. }
  76. // EventIPListSize is emitted when mtg updates a contents of the ip lists:
  77. // allowlist or blocklist.
  78. type EventIPListSize struct {
  79. eventBase
  80. Size int
  81. IsBlockList bool
  82. }
  83. // NewEventStart creates a new EventStart event.
  84. func NewEventStart(streamID string, remoteIP net.IP) EventStart {
  85. return EventStart{
  86. eventBase: eventBase{
  87. timestamp: time.Now(),
  88. streamID: streamID,
  89. },
  90. RemoteIP: remoteIP,
  91. }
  92. }
  93. // NewEventConnectedToDC creates a new EventConnectedToDC event.
  94. func NewEventConnectedToDC(streamID string, remoteIP net.IP, dc int) EventConnectedToDC {
  95. return EventConnectedToDC{
  96. eventBase: eventBase{
  97. timestamp: time.Now(),
  98. streamID: streamID,
  99. },
  100. RemoteIP: remoteIP,
  101. DC: dc,
  102. }
  103. }
  104. // NewEventTraffic creates a new EventTraffic event.
  105. func NewEventTraffic(streamID string, traffic uint, isRead bool) EventTraffic {
  106. return EventTraffic{
  107. eventBase: eventBase{
  108. timestamp: time.Now(),
  109. streamID: streamID,
  110. },
  111. Traffic: traffic,
  112. IsRead: isRead,
  113. }
  114. }
  115. // NewEventFinish creates a new EventFinish event.
  116. func NewEventFinish(streamID string) EventFinish {
  117. return EventFinish{
  118. eventBase: eventBase{
  119. timestamp: time.Now(),
  120. streamID: streamID,
  121. },
  122. }
  123. }
  124. // NewEventDomainFronting creates a new EventDomainFronting event.
  125. func NewEventDomainFronting(streamID string) EventDomainFronting {
  126. return EventDomainFronting{
  127. eventBase: eventBase{
  128. timestamp: time.Now(),
  129. streamID: streamID,
  130. },
  131. }
  132. }
  133. // NewEventConcurrencyLimited creates a new EventConcurrencyLimited
  134. // event.
  135. func NewEventConcurrencyLimited() EventConcurrencyLimited {
  136. return EventConcurrencyLimited{
  137. eventBase: eventBase{
  138. timestamp: time.Now(),
  139. },
  140. }
  141. }
  142. // NewEventIPBlocklisted creates a new EventIPBlocklisted event.
  143. func NewEventIPBlocklisted(remoteIP net.IP) EventIPBlocklisted {
  144. return EventIPBlocklisted{
  145. eventBase: eventBase{
  146. timestamp: time.Now(),
  147. },
  148. RemoteIP: remoteIP,
  149. }
  150. }
  151. // NewEventReplayAttack creates a new EventReplayAttack event.
  152. func NewEventReplayAttack(streamID string) EventReplayAttack {
  153. return EventReplayAttack{
  154. eventBase: eventBase{
  155. timestamp: time.Now(),
  156. streamID: streamID,
  157. },
  158. }
  159. }
  160. // NewEventIPListSize creates a new EventIPListSize event.
  161. func NewEventIPListSize(size int, isBlockList bool) EventIPListSize {
  162. return EventIPListSize{
  163. eventBase: eventBase{
  164. timestamp: time.Now(),
  165. },
  166. Size: size,
  167. IsBlockList: isBlockList,
  168. }
  169. }