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
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

proxy_opts.go 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. package mtglib
  2. import "time"
  3. // ProxyOpts is a structure with settings to mtg proxy.
  4. //
  5. // This is not required per se, but this is to shorten function signature and
  6. // give an ability to conveniently provide default values.
  7. type ProxyOpts struct {
  8. // Secret defines a secret which should be used by a proxy.
  9. //
  10. // This is a mandatory setting.
  11. Secret Secret
  12. // Network defines a network instance which should be used for all network
  13. // communications made by proxies.
  14. //
  15. // This is a mandatory setting.
  16. Network Network
  17. // AntiReplayCache defines an instance of antireplay cache.
  18. //
  19. // This is a mandatory setting.
  20. AntiReplayCache AntiReplayCache
  21. // IPBlocklist defines an instance of IP blocklist.
  22. //
  23. // This is a mandatory setting.
  24. IPBlocklist IPBlocklist
  25. // IPAllowlist defines a whitelist of IPs to allow to use proxy.
  26. //
  27. // This is an optional setting, ignored by default (no restrictions).
  28. IPAllowlist IPBlocklist
  29. // EventStream defines an instance of event stream.
  30. //
  31. // This ia a mandatory setting.
  32. EventStream EventStream
  33. // Logger defines an instance of the logger.
  34. //
  35. // This is a mandatory setting.
  36. Logger Logger
  37. // BufferSize is a size of the copy buffer in bytes.
  38. //
  39. // Please remember that we multiply this number in 2, because when we relay
  40. // between proxies, we have to create 2 intermediate buffers: to and from.
  41. //
  42. // This is an optional setting.
  43. //
  44. // Deprecated: this setting is no longer makes any effect.
  45. BufferSize uint
  46. // Concurrency is a size of the worker pool for connection management.
  47. //
  48. // If we have more connections than this number, they are going to be
  49. // rejected.
  50. //
  51. // This is an optional setting.
  52. Concurrency uint
  53. // IdleTimeout is a timeout for relay when we have to break a stream.
  54. //
  55. // This is a timeout for any activity. So, if we have any message which will
  56. // pass to either direction, a timer is reset. If we have no any reads or
  57. // writes for this timeout, a connection will be aborted.
  58. //
  59. // This is an optional setting.
  60. IdleTimeout time.Duration
  61. // HandshakeTimeout is a timeout during which all handshake ceremonies must
  62. // be completed, otherwise this process will be aborted
  63. //
  64. // This is an optional setting.
  65. HandshakeTimeout time.Duration
  66. // TolerateTimeSkewness is a time boundary that defines a time range where
  67. // faketls timestamp is acceptable.
  68. //
  69. // This means that if if you got a timestamp X, now is Y, then if |X-Y| <
  70. // TolerateTimeSkewness, then you accept a packet.
  71. //
  72. // This is an optional setting.
  73. TolerateTimeSkewness time.Duration
  74. // PreferIP defines an IP connectivity preference. Valid values are:
  75. // 'prefer-ipv4', 'prefer-ipv6', 'only-ipv4', 'only-ipv6'.
  76. //
  77. // This is an optional setting.
  78. PreferIP string
  79. // AutoUpdate defines if it is required to auto update proxy list from
  80. // Telegram instead of relying on a hardcoded list.
  81. //
  82. // This is an optional setting.
  83. AutoUpdate bool
  84. // DomainFrontingPort is a port we use to connect to a fronting domain.
  85. //
  86. // This is required because secret does not specify a port. It specifies a
  87. // hostname only.
  88. //
  89. // This is an optional setting.
  90. DomainFrontingPort uint
  91. // DomainFrontingHost is the address to use when connecting to the
  92. // fronting domain instead of resolving the hostname from the secret via
  93. // DNS. It can be a literal IP or a hostname; hostnames are resolved at
  94. // dial time via the native dialer (which honours dual-stack and Happy
  95. // Eyeballs).
  96. //
  97. // This is useful when DNS resolution of the secret's hostname is blocked
  98. // or loops back to this server. The hostname from the secret is still
  99. // used for SNI in the TLS handshake.
  100. //
  101. // This is an optional setting.
  102. DomainFrontingHost string
  103. // DomainFrontingIP previously held the dial target for the fronting
  104. // domain. The setting is no longer honoured: setting it logs a warning
  105. // at proxy startup and the value is dropped.
  106. //
  107. // Deprecated: use DomainFrontingHost. Setting this field has no effect.
  108. DomainFrontingIP string
  109. // DomainFrontingProxyProtocol is used if communication between upstream
  110. // endpoint and mtg supports proxy protocol. This is useful in case
  111. // if mtg is also placed behind load balancer, and this will make
  112. // fronting webserver to know about real IP addresses
  113. //
  114. // This is an optional setting.
  115. DomainFrontingProxyProtocol bool
  116. // AllowFallbackOnUnknownDC defines how proxy behaves if unknown DC was
  117. // requested. If this setting is set to false, then such connection will be
  118. // rejected. Otherwise, proxy will chose any DC.
  119. //
  120. // Telegram is designed in a way that any DC can serve any request, the
  121. // problem is a latency.
  122. //
  123. // This is an optional setting.
  124. AllowFallbackOnUnknownDC bool
  125. // UseTestDCs defines if we have to connect to production or to staging DCs of
  126. // Telegram.
  127. //
  128. // This is required if you use mtglib as an integration library for your
  129. // Telegram-related projects.
  130. //
  131. // This is an optional setting.
  132. //
  133. // OBSOLETE and DEPRECATED. Ignored.
  134. UseTestDCs bool
  135. // DCOverrides defines a set of IP addresses that should be used
  136. // with a higher priority to those that are calculated somehow by mtg.
  137. //
  138. // OBSOLETE and DEPRECATED. Ignored.
  139. DCOverrides map[int][]string
  140. // DoppelGangerURLs is a list of URLs that should be crawled by
  141. // mtg to calculate parameters for statistical distribution of a
  142. // traffic for fronting domains. If nothing is given, then predefined
  143. // statistics is going to be used.
  144. DoppelGangerURLs []string
  145. // DoppelGangerPerRaid defines how many time each URL from
  146. // DoppelGangerURLs list should be crawled per raid. We recommend to
  147. // have this number ~10.
  148. DoppelGangerPerRaid uint
  149. // DoppelGangerEach defines a time period between each raid. We recommend
  150. // to use hours here.
  151. DoppelGangerEach time.Duration
  152. // DoppelGangerDRS defines if TLS Dynamic Record Sizing is active.
  153. DoppelGangerDRS bool
  154. }
  155. func (p ProxyOpts) valid() error {
  156. switch {
  157. case p.Network == nil:
  158. return ErrNetworkIsNotDefined
  159. case p.AntiReplayCache == nil:
  160. return ErrAntiReplayCacheIsNotDefined
  161. case p.IPBlocklist == nil:
  162. return ErrIPBlocklistIsNotDefined
  163. case p.IPAllowlist == nil:
  164. return ErrIPAllowlistIsNotDefined
  165. case p.EventStream == nil:
  166. return ErrEventStreamIsNotDefined
  167. case p.Logger == nil:
  168. return ErrLoggerIsNotDefined
  169. case !p.Secret.Valid():
  170. return ErrSecretInvalid
  171. }
  172. return nil
  173. }
  174. func (p ProxyOpts) getConcurrency() int {
  175. if p.Concurrency == 0 {
  176. return DefaultConcurrency
  177. }
  178. return int(p.Concurrency)
  179. }
  180. func (p ProxyOpts) getDomainFrontingPort() int {
  181. if p.DomainFrontingPort == 0 {
  182. return DefaultDomainFrontingPort
  183. }
  184. return int(p.DomainFrontingPort)
  185. }
  186. func (p ProxyOpts) getTolerateTimeSkewness() time.Duration {
  187. if p.TolerateTimeSkewness == 0 {
  188. return DefaultTolerateTimeSkewness
  189. }
  190. return p.TolerateTimeSkewness
  191. }
  192. func (p ProxyOpts) getPreferIP() string {
  193. if p.PreferIP == "" {
  194. return DefaultPreferIP
  195. }
  196. return p.PreferIP
  197. }
  198. func (p ProxyOpts) getHandshakeTimeout() time.Duration {
  199. if p.HandshakeTimeout == 0 {
  200. return DefaultHandshakeTimeout
  201. }
  202. return p.HandshakeTimeout
  203. }
  204. func (p ProxyOpts) getIdleTimeout() time.Duration {
  205. if p.IdleTimeout == 0 {
  206. return DefaultIdleTimeout
  207. }
  208. return p.IdleTimeout
  209. }
  210. func (p ProxyOpts) getLogger(name string) Logger {
  211. return p.Logger.Named(name)
  212. }