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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package mtglib
  2. import (
  3. "context"
  4. "errors"
  5. "net"
  6. "net/http"
  7. "time"
  8. )
  9. var (
  10. ErrSecretEmpty = errors.New("secret is empty")
  11. ErrSecretInvalid = errors.New("secret is invalid")
  12. ErrNetworkIsNotDefined = errors.New("network is not defined")
  13. ErrAntiReplayCacheIsNotDefined = errors.New("anti-replay cache is not defined")
  14. ErrTimeAttackDetectorIsNotDefined = errors.New("time attack detector is not defined")
  15. ErrIPBlocklistIsNotDefined = errors.New("ip blocklist is not defined")
  16. ErrEventStreamIsNotDefined = errors.New("event stream is not defined")
  17. ErrLoggerIsNotDefined = errors.New("logger is not defined")
  18. )
  19. const (
  20. DefaultConcurrency = 4096
  21. DefaultBufferSize = 16 * 1024 // 16 kib
  22. DefaultDomainFrontingPort = 443
  23. DefaultIdleTimeout = time.Minute
  24. DefaultPreferIP = "prefer-ipv6"
  25. )
  26. // Network defines a knowledge how to work with a network. It may sound
  27. // fun but it encapsulates all the knowledge how to properly establish
  28. // connections to remote hosts and configure HTTP clients.
  29. //
  30. // For example, if you want to use SOCKS5 proxy, you probably want to
  31. // have all traffic routed to this proxy: telegram connections, http
  32. // requests and so on. This knowledge is encapsulated into instances of
  33. // such interface.
  34. //
  35. // mtglib uses Network for:
  36. //
  37. // 1. Dialing to Telegram
  38. //
  39. // 2. Dialing to front domain
  40. //
  41. // 3. Doing HTTP requests (for example, for FireHOL ipblocklist).
  42. type Network interface {
  43. // Dial establishes context-free TCP connections.
  44. Dial(network, address string) (net.Conn, error)
  45. // DialContext dials using a context. This is a preferrable
  46. // way of establishing TCP connections.
  47. DialContext(ctx context.Context, network, address string) (net.Conn, error)
  48. // MakeHTTPClient build an HTTP client with given dial function. If
  49. // nothing is provided, then DialContext of this interface is going
  50. // to be used.
  51. MakeHTTPClient(func(ctx context.Context, network, address string) (net.Conn, error)) *http.Client
  52. }
  53. // AntiReplayCache is an interface that is used to detect replay attacks
  54. // based on some traffic fingerprints.
  55. //
  56. // Replay attacks are probe attacks whose main goal is to identify if
  57. // server software can be classified in some way. For example, if you
  58. // send some HTTP request to a web server, then you can expect that this
  59. // server will respond with HTTP response back.
  60. //
  61. // There is a problem though. Let's imagine, that connection is
  62. // encrypted. Let's imagine, that it is encrypted with some static key
  63. // like ShadowSocks (https://shadowsocks.org/assets/whitepaper.pdf).
  64. // In that case, in theory, if you repeat the same bytes, you can get
  65. // the same responses. Let's imagine, that you've cracked the key. then
  66. // if you send the same bytes, you can decrypt a response and see its
  67. // structure. Based on its structure you can identify if this server is
  68. // SOCKS5, MTPROTO proxy etc.
  69. //
  70. // This is just one example, maybe not the best or not the most
  71. // relevant. In real life, different organizations use such replay
  72. // attacks to perform some reverse engineering of the proxy, do some
  73. // statical analysis to identify server software.
  74. //
  75. // There are many ways how to protect your proxy against them. One
  76. // is domain fronting which is a core part of mtg. Another one is to
  77. // collect some 'handshake fingerprints' and forbid duplication.
  78. //
  79. // So, it one is sending the same byte flow right after you (or a couple
  80. // of hours after), mtg should detect that and reject this connection
  81. // (or redirect to fronting domain).
  82. type AntiReplayCache interface {
  83. // Seen before checks if this set of bytes was observed before or
  84. // not. If it is required to store this information somewhere else,
  85. // then it has to do that.
  86. SeenBefore(data []byte) bool
  87. }
  88. // IPBlocklist filters requests based on IP address.
  89. //
  90. // If this filter has an IP address, then mtg closes a request without
  91. // reading anything from a socket. It also does not give such request to
  92. // a worker pool, so in worst cases you can expect that you invoke this
  93. // object more frequent than defined proxy concurrency.
  94. type IPBlocklist interface {
  95. // Contains checks if given IP address belongs to this blocklist If.
  96. // it is, a connection is terminated .
  97. Contains(net.IP) bool
  98. }
  99. type Event interface {
  100. StreamID() string
  101. Timestamp() time.Time
  102. }
  103. // EventStream is an abstraction that accepts a set of events produced
  104. // by mtg. Its main goal is to inject your logging or monitoring system.
  105. //
  106. // The idea is simple. When mtg works, it emits a set of events during
  107. // a lifecycle of the requestor: EventStart, EventFinish etc. mtg is a
  108. // producer which puts these events into a stream. Responsibility of
  109. // the stream is to deliver this event to consumers/observers. There
  110. // might be many different observers (for example, you want to have both
  111. // statsd and prometheus), mtg should know nothing about them.
  112. type EventStream interface {
  113. // Send delivers an event to observers. Given context has to be
  114. // respected. If the context is closed, all blocking operations should
  115. // be released ASAP.
  116. //
  117. // It is possible that context is closed but the message is delivered.
  118. // EventStream implementations should solve this issue somehow.
  119. Send(context.Context, Event)
  120. }
  121. // TimeAttackDetector is an abstraction that checks a time, taken from
  122. // the faketls client hello message. This timestamp is encoded into
  123. // client-generated random bytes and can be extracted after some client
  124. // hello verification.
  125. //
  126. // This is mostly to prevent replay attacks.
  127. type TimeAttackDetector interface {
  128. // Valid returns an error if timestamp is invalid or should not be
  129. // accepted.
  130. Valid(time.Time) error
  131. }
  132. // Logger defines an interface of the logger used by mtglib.
  133. //
  134. // Each logger has a name. It is possible to stack names to organize
  135. // poor-man namespaces. Also, each logger must be able to bind
  136. // parameters to avoid pushing them all the time.
  137. //
  138. // Example
  139. //
  140. // logger := SomeLogger{}
  141. // logger = logger.BindStr("ip", net.IP{127, 0, 0, 1})
  142. // logger.Info("Hello")
  143. //
  144. // In that case, ip is bound as a parameter. It is a great idea to
  145. // put this parameter somewhere in a log message.
  146. //
  147. // logger1 = logger.BindStr("param1", "11")
  148. // logger2 = logger.BindInt("param2", 11)
  149. //
  150. // logger1 should see no param2 and vice versa, logger2 should not see param1
  151. // If you attach a parameter to a logger, parents should not know about that.
  152. type Logger interface {
  153. Named(name string) Logger
  154. BindInt(name string, value int) Logger
  155. BindStr(name, value string) Logger
  156. Printf(format string, args ...interface{})
  157. Info(msg string)
  158. InfoError(msg string, err error)
  159. Warning(msg string)
  160. WarningError(msg string, err error)
  161. Debug(msg string)
  162. DebugError(msg string, err error)
  163. }