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文字以内のものにしてください。

init.go 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // mtglib defines a package with MTPROTO proxy.
  2. //
  3. // Since mtg itself is build as an example of how to work with mtglib,
  4. // it worth to telling a couple of words about a project organization.
  5. //
  6. // A core object of the project is mtglib.Proxy. This is a proxy you
  7. // expect: that one which you configure, set to serve on a listener
  8. // and/or shutdown on application termination.
  9. //
  10. // But it also has a core logic unrelated to Telegram per se: anti
  11. // replay cache, network connectivity (who knows, maybe you want to have
  12. // a native VMESS integration) and so on.
  13. //
  14. // You can supply such parts to a proxy with interfaces. The rest of
  15. // the packages in mtg define some default implementations of these
  16. // interfaces. But if you want to integrate it with, let say, influxdb,
  17. // you can do it easily.
  18. package mtglib
  19. import (
  20. "context"
  21. "errors"
  22. "net"
  23. "net/http"
  24. "time"
  25. "github.com/9seconds/mtg/v2/essentials"
  26. )
  27. var (
  28. // ErrSecretEmpty is returned if you are trying to create a proxy
  29. // but do not provide a secret.
  30. ErrSecretEmpty = errors.New("secret is empty")
  31. // ErrSecretInvalid is returned if you are trying to create a proxy
  32. // but secret value is invalid (no host or payload are zeroes).
  33. ErrSecretInvalid = errors.New("secret is invalid")
  34. // ErrNetworkIsNotDefined is returned if you are trying to create a
  35. // proxy but network value is undefined.
  36. ErrNetworkIsNotDefined = errors.New("network is not defined")
  37. // ErrAntiReplayCacheIsNotDefined is returned if you are trying to
  38. // create a proxy but anti replay cache value is undefined.
  39. ErrAntiReplayCacheIsNotDefined = errors.New("anti-replay cache is not defined")
  40. // ErrIPBlocklistIsNotDefined is returned if you are trying to
  41. // create a proxy but ip blocklist instance is not defined.
  42. ErrIPBlocklistIsNotDefined = errors.New("ip blocklist is not defined")
  43. // ErrIPAllowlistIsNotDefined is returned if you are trying to
  44. // create a proxy but ip allowlist instance is not defined.
  45. ErrIPAllowlistIsNotDefined = errors.New("ip allowlist is not defined")
  46. // ErrEventStreamIsNotDefined is returned if you are trying to create a
  47. // proxy but event stream instance is not defined.
  48. ErrEventStreamIsNotDefined = errors.New("event stream is not defined")
  49. // ErrLoggerIsNotDefined is returned if you are trying to
  50. // create a proxy but logger is not defined.
  51. ErrLoggerIsNotDefined = errors.New("logger is not defined")
  52. )
  53. const (
  54. // DefaultConcurrency is a default max count of simultaneously
  55. // connected clients.
  56. DefaultConcurrency = 4096
  57. // DefaultBufferSize is a default size of a copy buffer.
  58. //
  59. // Deprecated: this setting no longer makes any effect.
  60. DefaultBufferSize = 16 * 1024 // 16 kib
  61. // DefaultDomainFrontingPort is a default port (HTTPS) to connect to in
  62. // case of probe-resistance activity.
  63. DefaultDomainFrontingPort = 443
  64. // DefaultIdleTimeout is a default timeout for closing a connection
  65. // in case of idling.
  66. //
  67. // Deprecated: no longer in use because of changed TCP relay
  68. // algorithm.
  69. DefaultIdleTimeout = time.Minute
  70. // DefaultTolerateTimeSkewness is a default timeout for time
  71. // skewness on a faketls timeout verification.
  72. DefaultTolerateTimeSkewness = 3 * time.Second
  73. // DefaultPreferIP is a default value for Telegram IP connectivity
  74. // preference.
  75. DefaultPreferIP = "prefer-ipv6"
  76. // SecretKeyLength defines a length of the secret bytes used
  77. // by Telegram and a proxy.
  78. SecretKeyLength = 16
  79. // ConnectionIDBytesLength defines a count of random bytes used to generate
  80. // a stream/connection ids.
  81. ConnectionIDBytesLength = 16
  82. // TCPRelayReadTimeout defines a max time period between two consecuitive
  83. // reads from Telegram after which connection will be terminated. This is
  84. // required to abort stale connections.
  85. TCPRelayReadTimeout = 20 * time.Second
  86. )
  87. // Network defines a knowledge how to work with a network. It may sound
  88. // fun but it encapsulates all the knowledge how to properly establish
  89. // connections to remote hosts and configure HTTP clients.
  90. //
  91. // For example, if you want to use SOCKS5 proxy, you probably want to
  92. // have all traffic routed to this proxy: telegram connections, http
  93. // requests and so on. This knowledge is encapsulated into instances of
  94. // such interface.
  95. //
  96. // mtglib uses Network for:
  97. //
  98. // 1. Dialing to Telegram
  99. //
  100. // 2. Dialing to front domain
  101. //
  102. // 3. Doing HTTP requests (for example, for FireHOL ipblocklist).
  103. type Network interface {
  104. // Dial establishes context-free TCP connections.
  105. Dial(network, address string) (essentials.Conn, error)
  106. // DialContext dials using a context. This is a preferrable
  107. // way of establishing TCP connections.
  108. DialContext(ctx context.Context, network, address string) (essentials.Conn, error)
  109. // MakeHTTPClient build an HTTP client with given dial function. If
  110. // nothing is provided, then DialContext of this interface is going
  111. // to be used.
  112. MakeHTTPClient(func(ctx context.Context, network, address string) (essentials.Conn, error)) *http.Client
  113. }
  114. // AntiReplayCache is an interface that is used to detect replay attacks
  115. // based on some traffic fingerprints.
  116. //
  117. // Replay attacks are probe attacks whose main goal is to identify if
  118. // server software can be classified in some way. For example, if you
  119. // send some HTTP request to a web server, then you can expect that this
  120. // server will respond with HTTP response back.
  121. //
  122. // There is a problem though. Let's imagine, that connection is
  123. // encrypted. Let's imagine, that it is encrypted with some static key
  124. // like ShadowSocks (https://shadowsocks.org/assets/whitepaper.pdf).
  125. // In that case, in theory, if you repeat the same bytes, you can get
  126. // the same responses. Let's imagine, that you've cracked the key. then
  127. // if you send the same bytes, you can decrypt a response and see its
  128. // structure. Based on its structure you can identify if this server is
  129. // SOCKS5, MTPROTO proxy etc.
  130. //
  131. // This is just one example, maybe not the best or not the most
  132. // relevant. In real life, different organizations use such replay
  133. // attacks to perform some reverse engineering of the proxy, do some
  134. // statical analysis to identify server software.
  135. //
  136. // There are many ways how to protect your proxy against them. One
  137. // is domain fronting which is a core part of mtg. Another one is to
  138. // collect some 'handshake fingerprints' and forbid duplication.
  139. //
  140. // So, it one is sending the same byte flow right after you (or a couple
  141. // of hours after), mtg should detect that and reject this connection
  142. // (or redirect to fronting domain).
  143. type AntiReplayCache interface {
  144. // Seen before checks if this set of bytes was observed before or
  145. // not. If it is required to store this information somewhere else,
  146. // then it has to do that.
  147. SeenBefore(data []byte) bool
  148. }
  149. // IPBlocklist filters requests based on IP address.
  150. //
  151. // If this filter has an IP address, then mtg closes a request without
  152. // reading anything from a socket. It also does not give such request to
  153. // a worker pool, so in worst cases you can expect that you invoke this
  154. // object more frequent than defined proxy concurrency.
  155. type IPBlocklist interface {
  156. // Contains checks if given IP address belongs to this blocklist If.
  157. // it is, a connection is terminated .
  158. Contains(net.IP) bool
  159. // Run starts a background update procedure for a blocklist
  160. Run(time.Duration)
  161. // Shutdown stops a blocklist. It is assumed that none will access it after.
  162. Shutdown()
  163. }
  164. // Event is a data structure which is populated during mtg request
  165. // processing lifecycle. Each request popluates many events:
  166. //
  167. // 1. Client connected
  168. //
  169. // 2. Request is finished
  170. //
  171. // 3. Connection to Telegram server is established
  172. //
  173. // and so on. All these events are data structures but all of them
  174. // must conform the same interface.
  175. type Event interface {
  176. // StreamID returns an identifier of the stream, connection,
  177. // request, you name it. All events within the same stream returns
  178. // the same stream id.
  179. StreamID() string
  180. // Timestamp returns a timestamp when this event was generated.
  181. Timestamp() time.Time
  182. }
  183. // EventStream is an abstraction that accepts a set of events produced
  184. // by mtg. Its main goal is to inject your logging or monitoring system.
  185. //
  186. // The idea is simple. When mtg works, it emits a set of events during
  187. // a lifecycle of the requestor: EventStart, EventFinish etc. mtg is a
  188. // producer which puts these events into a stream. Responsibility of
  189. // the stream is to deliver this event to consumers/observers. There
  190. // might be many different observers (for example, you want to have both
  191. // statsd and prometheus), mtg should know nothing about them.
  192. type EventStream interface {
  193. // Send delivers an event to observers. Given context has to be
  194. // respected. If the context is closed, all blocking operations should
  195. // be released ASAP.
  196. //
  197. // It is possible that context is closed but the message is delivered.
  198. // EventStream implementations should solve this issue somehow.
  199. Send(context.Context, Event)
  200. }
  201. // Logger defines an interface of the logger used by mtglib.
  202. //
  203. // Each logger has a name. It is possible to stack names to organize
  204. // poor-man namespaces. Also, each logger must be able to bind
  205. // parameters to avoid pushing them all the time.
  206. //
  207. // Example
  208. //
  209. // logger := SomeLogger{}
  210. // logger = logger.BindStr("ip", net.IP{127, 0, 0, 1})
  211. // logger.Info("Hello")
  212. //
  213. // In that case, ip is bound as a parameter. It is a great idea to
  214. // put this parameter somewhere in a log message.
  215. //
  216. // logger1 = logger.BindStr("param1", "11")
  217. // logger2 = logger.BindInt("param2", 11)
  218. //
  219. // logger1 should see no param2 and vice versa, logger2 should not see param1
  220. // If you attach a parameter to a logger, parents should not know about that.
  221. type Logger interface {
  222. // Named returns a new logger with a bound name. Name chaining is
  223. // allowed and appreciated.
  224. Named(name string) Logger
  225. // BindInt binds new integer parameter to a new logger instance.
  226. BindInt(name string, value int) Logger
  227. // BindStr binds new string parameter to a new logger instance.
  228. BindStr(name, value string) Logger
  229. // BindJSON binds a new JSON-encoded string to a new logger instance.
  230. BindJSON(name, value string) Logger
  231. // Printf is to support log.Logger behavior.
  232. Printf(format string, args ...interface{})
  233. // Info puts a message about some normal situation.
  234. Info(msg string)
  235. // InfoError puts a message about some normal situation but this
  236. // situation is related to a given error.
  237. InfoError(msg string, err error)
  238. // Warning puts a message about some extraordinary situation
  239. // worth to look at.
  240. Warning(msg string)
  241. // WarningError puts a message about some extraordinary situation
  242. // worth to look at. This situation is related to a given error.
  243. WarningError(msg string, err error)
  244. // Debug puts a message useful for debugging only.
  245. Debug(msg string)
  246. // Debug puts a message useful for debugging only. This message is
  247. // related to a given error.
  248. DebugError(msg string, err error)
  249. }