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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. // NewEventStart creates a new EventStart event.
  77. func NewEventStart(streamID string, remoteIP net.IP) EventStart {
  78. return EventStart{
  79. eventBase: eventBase{
  80. timestamp: time.Now(),
  81. streamID: streamID,
  82. },
  83. RemoteIP: remoteIP,
  84. }
  85. }
  86. // NewEventConnectedToDC creates a new EventConnectedToDC event.
  87. func NewEventConnectedToDC(streamID string, remoteIP net.IP, dc int) EventConnectedToDC {
  88. return EventConnectedToDC{
  89. eventBase: eventBase{
  90. timestamp: time.Now(),
  91. streamID: streamID,
  92. },
  93. RemoteIP: remoteIP,
  94. DC: dc,
  95. }
  96. }
  97. // NewEventTraffic creates a new EventTraffic event.
  98. func NewEventTraffic(streamID string, traffic uint, isRead bool) EventTraffic {
  99. return EventTraffic{
  100. eventBase: eventBase{
  101. timestamp: time.Now(),
  102. streamID: streamID,
  103. },
  104. Traffic: traffic,
  105. IsRead: isRead,
  106. }
  107. }
  108. // NewEventFinish creates a new EventFinish event.
  109. func NewEventFinish(streamID string) EventFinish {
  110. return EventFinish{
  111. eventBase: eventBase{
  112. timestamp: time.Now(),
  113. streamID: streamID,
  114. },
  115. }
  116. }
  117. // NewEventDomainFronting creates a new EventDomainFronting event.
  118. func NewEventDomainFronting(streamID string) EventDomainFronting {
  119. return EventDomainFronting{
  120. eventBase: eventBase{
  121. timestamp: time.Now(),
  122. streamID: streamID,
  123. },
  124. }
  125. }
  126. // NewEventConcurrencyLimited creates a new EventConcurrencyLimited
  127. // event.
  128. func NewEventConcurrencyLimited() EventConcurrencyLimited {
  129. return EventConcurrencyLimited{
  130. eventBase: eventBase{
  131. timestamp: time.Now(),
  132. },
  133. }
  134. }
  135. // NewEventIPBlocklisted creates a new EventIPBlocklisted event.
  136. func NewEventIPBlocklisted(remoteIP net.IP) EventIPBlocklisted {
  137. return EventIPBlocklisted{
  138. eventBase: eventBase{
  139. timestamp: time.Now(),
  140. },
  141. RemoteIP: remoteIP,
  142. }
  143. }
  144. // NewEventReplayAttack creates a new EventReplayAttack event.
  145. func NewEventReplayAttack(streamID string) EventReplayAttack {
  146. return EventReplayAttack{
  147. eventBase: eventBase{
  148. timestamp: time.Now(),
  149. streamID: streamID,
  150. },
  151. }
  152. }