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
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

proxy_opts.go 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. // TolerateTimeSkewness is a time boundary that defines a time range where
  62. // faketls timestamp is acceptable.
  63. //
  64. // This means that if if you got a timestamp X, now is Y, then if |X-Y| <
  65. // TolerateTimeSkewness, then you accept a packet.
  66. //
  67. // This is an optional setting.
  68. TolerateTimeSkewness time.Duration
  69. // PreferIP defines an IP connectivity preference. Valid values are:
  70. // 'prefer-ipv4', 'prefer-ipv6', 'only-ipv4', 'only-ipv6'.
  71. //
  72. // This is an optional setting.
  73. PreferIP string
  74. // DomainFrontingPort is a port we use to connect to a fronting domain.
  75. //
  76. // This is required because secret does not specify a port. It specifies a
  77. // hostname only.
  78. //
  79. // This is an optional setting.
  80. DomainFrontingPort uint
  81. // DomainFrontingIP is an IP address to use when connecting to the fronting
  82. // domain instead of resolving the hostname from the secret via DNS.
  83. //
  84. // This is useful when DNS resolution of the fronting host is blocked.
  85. // The hostname from the secret is still used for SNI in the TLS handshake.
  86. //
  87. // This is an optional setting.
  88. DomainFrontingIP string
  89. // DomainFrontingProxyProtocol is used if communication between upstream
  90. // endpoint and mtg supports proxy protocol. This is useful in case
  91. // if mtg is also placed behind load balancer, and this will make
  92. // fronting webserver to know about real IP addresses
  93. //
  94. // This is an optional setting.
  95. DomainFrontingProxyProtocol bool
  96. // AllowFallbackOnUnknownDC defines how proxy behaves if unknown DC was
  97. // requested. If this setting is set to false, then such connection will be
  98. // rejected. Otherwise, proxy will chose any DC.
  99. //
  100. // Telegram is designed in a way that any DC can serve any request, the
  101. // problem is a latency.
  102. //
  103. // This is an optional setting.
  104. AllowFallbackOnUnknownDC bool
  105. // UseTestDCs defines if we have to connect to production or to staging DCs of
  106. // Telegram.
  107. //
  108. // This is required if you use mtglib as an integration library for your
  109. // Telegram-related projects.
  110. //
  111. // This is an optional setting.
  112. //
  113. // OBSOLETE and DEPRECATED. Ignored.
  114. UseTestDCs bool
  115. // DCOverrides defines a set of IP addresses that should be used
  116. // with a higher priority to those that are calculated somehow by mtg.
  117. //
  118. // OBSOLETE and DEPRECATED. Ignored.
  119. DCOverrides map[int][]string
  120. }
  121. func (p ProxyOpts) valid() error {
  122. switch {
  123. case p.Network == nil:
  124. return ErrNetworkIsNotDefined
  125. case p.AntiReplayCache == nil:
  126. return ErrAntiReplayCacheIsNotDefined
  127. case p.IPBlocklist == nil:
  128. return ErrIPBlocklistIsNotDefined
  129. case p.IPAllowlist == nil:
  130. return ErrIPAllowlistIsNotDefined
  131. case p.EventStream == nil:
  132. return ErrEventStreamIsNotDefined
  133. case p.Logger == nil:
  134. return ErrLoggerIsNotDefined
  135. case !p.Secret.Valid():
  136. return ErrSecretInvalid
  137. }
  138. return nil
  139. }
  140. func (p ProxyOpts) getConcurrency() int {
  141. if p.Concurrency == 0 {
  142. return DefaultConcurrency
  143. }
  144. return int(p.Concurrency)
  145. }
  146. func (p ProxyOpts) getDomainFrontingPort() int {
  147. if p.DomainFrontingPort == 0 {
  148. return DefaultDomainFrontingPort
  149. }
  150. return int(p.DomainFrontingPort)
  151. }
  152. func (p ProxyOpts) getTolerateTimeSkewness() time.Duration {
  153. if p.TolerateTimeSkewness == 0 {
  154. return DefaultTolerateTimeSkewness
  155. }
  156. return p.TolerateTimeSkewness
  157. }
  158. func (p ProxyOpts) getPreferIP() string {
  159. if p.PreferIP == "" {
  160. return DefaultPreferIP
  161. }
  162. return p.PreferIP
  163. }
  164. func (p ProxyOpts) getLogger(name string) Logger {
  165. return p.Logger.Named(name)
  166. }