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 символов.

proxy_opts.go 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. // EventStream defines an instance of event stream.
  26. //
  27. // This ia a mandatory setting.
  28. EventStream EventStream
  29. // Logger defines an instance of the logger.
  30. //
  31. // This is a mandatory setting.
  32. Logger Logger
  33. // BufferSize is a size of the copy buffer in bytes.
  34. //
  35. // Please remember that we multiply this number in 2, because when
  36. // we relay between proxies, we have to create 2 intermediate
  37. // buffers: to and from.
  38. //
  39. // This is an optional setting.
  40. BufferSize uint
  41. // Concurrency is a size of the worker pool for connection management.
  42. //
  43. // If we have more connections than this number, they are going to be
  44. // rejected.
  45. //
  46. // This is an optional setting.
  47. Concurrency uint
  48. // DomainFrontingPort is a port we use to connect to a fronting
  49. // domain.
  50. //
  51. // This is required because secret does not specify a port. It
  52. // specifies a hostname only.
  53. //
  54. // This is an optional setting.
  55. DomainFrontingPort uint
  56. // IdleTimeout is a timeout for relay when we have to break a
  57. // stream.
  58. //
  59. // This is a timeout for any activity. So, if we have any message
  60. // which will pass to either direction, a timer is reset. If we have
  61. // no any reads or writes for this timeout, a connection will be
  62. // aborted.
  63. //
  64. // This is an optional setting.
  65. IdleTimeout time.Duration
  66. // TolerateTimeSkewness is a time boundary that defines a time
  67. // range where faketls timestamp is acceptable.
  68. //
  69. // This means that if if you got a timestamp X, now is Y, then
  70. // if |X-Y| < 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. // UseTestDCs defines if we have to connect to production or to staging
  80. // DCs of Telegram.
  81. //
  82. // This is required if you use mtglib as an integration library for
  83. // your Telegram-related projects.
  84. //
  85. // This is an optional setting.
  86. UseTestDCs bool
  87. }
  88. func (p ProxyOpts) valid() error {
  89. switch {
  90. case p.Network == nil:
  91. return ErrNetworkIsNotDefined
  92. case p.AntiReplayCache == nil:
  93. return ErrAntiReplayCacheIsNotDefined
  94. case p.IPBlocklist == nil:
  95. return ErrIPBlocklistIsNotDefined
  96. case p.EventStream == nil:
  97. return ErrEventStreamIsNotDefined
  98. case p.Logger == nil:
  99. return ErrLoggerIsNotDefined
  100. case !p.Secret.Valid():
  101. return ErrSecretInvalid
  102. }
  103. return nil
  104. }
  105. func (p ProxyOpts) getBufferSize() int {
  106. if p.BufferSize < 1 {
  107. return DefaultBufferSize
  108. }
  109. return int(p.BufferSize)
  110. }
  111. func (p ProxyOpts) getConcurrency() int {
  112. if p.Concurrency == 0 {
  113. return DefaultConcurrency
  114. }
  115. return int(p.Concurrency)
  116. }
  117. func (p ProxyOpts) getDomainFrontingPort() int {
  118. if p.DomainFrontingPort == 0 {
  119. return DefaultDomainFrontingPort
  120. }
  121. return int(p.DomainFrontingPort)
  122. }
  123. func (p ProxyOpts) getIdleTimeout() time.Duration {
  124. if p.IdleTimeout == 0 {
  125. return DefaultIdleTimeout
  126. }
  127. return p.IdleTimeout
  128. }
  129. func (p ProxyOpts) getTolerateTimeSkewness() time.Duration {
  130. if p.TolerateTimeSkewness == 0 {
  131. return DefaultTolerateTimeSkewness
  132. }
  133. return p.TolerateTimeSkewness
  134. }
  135. func (p ProxyOpts) getPreferIP() string {
  136. if p.PreferIP == "" {
  137. return DefaultPreferIP
  138. }
  139. return p.PreferIP
  140. }
  141. func (p ProxyOpts) getLogger(name string) Logger {
  142. return p.Logger.Named(name)
  143. }