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 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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
  6. // signature and 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
  13. // network 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
  40. // we relay between proxies, we have to create 2 intermediate
  41. // buffers: to and from.
  42. //
  43. // This is an optional setting.
  44. //
  45. // Deprecated: this setting is no longer makes any effect.
  46. BufferSize uint
  47. // Concurrency is a size of the worker pool for connection management.
  48. //
  49. // If we have more connections than this number, they are going to be
  50. // rejected.
  51. //
  52. // This is an optional setting.
  53. Concurrency uint
  54. // IdleTimeout is a timeout for relay when we have to break a
  55. // stream.
  56. //
  57. // This is a timeout for any activity. So, if we have any message
  58. // which will pass to either direction, a timer is reset. If we have
  59. // no any reads or writes for this timeout, a connection will be
  60. // aborted.
  61. //
  62. // This is an optional setting.
  63. IdleTimeout time.Duration
  64. // TolerateTimeSkewness is a time boundary that defines a time
  65. // range where faketls timestamp is acceptable.
  66. //
  67. // This means that if if you got a timestamp X, now is Y, then
  68. // if |X-Y| < TolerateTimeSkewness, then you accept a packet.
  69. //
  70. // This is an optional setting.
  71. TolerateTimeSkewness time.Duration
  72. // PreferIP defines an IP connectivity preference. Valid values are:
  73. // 'prefer-ipv4', 'prefer-ipv6', 'only-ipv4', 'only-ipv6'.
  74. //
  75. // This is an optional setting.
  76. PreferIP string
  77. // DomainFrontingPort is a port we use to connect to a fronting
  78. // domain.
  79. //
  80. // This is required because secret does not specify a port. It
  81. // specifies a hostname only.
  82. //
  83. // This is an optional setting.
  84. DomainFrontingPort uint
  85. // AllowFallbackOnUnknownDC defines how proxy behaves if unknown DC was
  86. // requested. If this setting is set to false, then such connection
  87. // will be rejected. Otherwise, proxy will chose any DC.
  88. //
  89. // Telegram is designed in a way that any DC can serve any request,
  90. // the problem is a latency.
  91. //
  92. // This is an optional setting.
  93. AllowFallbackOnUnknownDC bool
  94. // UseTestDCs defines if we have to connect to production or to staging
  95. // DCs of Telegram.
  96. //
  97. // This is required if you use mtglib as an integration library for
  98. // your Telegram-related projects.
  99. //
  100. // This is an optional setting.
  101. UseTestDCs bool
  102. }
  103. func (p ProxyOpts) valid() error {
  104. switch {
  105. case p.Network == nil:
  106. return ErrNetworkIsNotDefined
  107. case p.AntiReplayCache == nil:
  108. return ErrAntiReplayCacheIsNotDefined
  109. case p.IPBlocklist == nil:
  110. return ErrIPBlocklistIsNotDefined
  111. case p.IPAllowlist == nil:
  112. return ErrIPAllowlistIsNotDefined
  113. case p.EventStream == nil:
  114. return ErrEventStreamIsNotDefined
  115. case p.Logger == nil:
  116. return ErrLoggerIsNotDefined
  117. case !p.Secret.Valid():
  118. return ErrSecretInvalid
  119. }
  120. return nil
  121. }
  122. func (p ProxyOpts) getConcurrency() int {
  123. if p.Concurrency == 0 {
  124. return DefaultConcurrency
  125. }
  126. return int(p.Concurrency)
  127. }
  128. func (p ProxyOpts) getDomainFrontingPort() int {
  129. if p.DomainFrontingPort == 0 {
  130. return DefaultDomainFrontingPort
  131. }
  132. return int(p.DomainFrontingPort)
  133. }
  134. func (p ProxyOpts) getTolerateTimeSkewness() time.Duration {
  135. if p.TolerateTimeSkewness == 0 {
  136. return DefaultTolerateTimeSkewness
  137. }
  138. return p.TolerateTimeSkewness
  139. }
  140. func (p ProxyOpts) getPreferIP() string {
  141. if p.PreferIP == "" {
  142. return DefaultPreferIP
  143. }
  144. return p.PreferIP
  145. }
  146. func (p ProxyOpts) getLogger(name string) Logger {
  147. return p.Logger.Named(name)
  148. }