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
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

proxy_opts.go 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. // TimeAttackDetector defines an instance of timeattack detector.
  22. //
  23. // This is a mandatory setting.
  24. TimeAttackDetector TimeAttackDetector
  25. // IPBlocklist defines an instance of IP blocklist.
  26. //
  27. // This is a mandatory setting.
  28. IPBlocklist 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. BufferSize uint
  45. // Concurrency is a size of the worker pool for connection management.
  46. //
  47. // If we have more connections than this number, they are going to be
  48. // rejected.
  49. //
  50. // This is an optional setting.
  51. Concurrency uint
  52. // DomainFrontingPort is a port we use to connect to a fronting
  53. // domain.
  54. //
  55. // This is required because secret does not specify a port. It
  56. // specifies a hostname only.
  57. //
  58. // This is an optional setting.
  59. DomainFrontingPort uint
  60. // IdleTimeout is a timeout for relay when we have to break a
  61. // stream.
  62. //
  63. // This is a timeout for any activity. So, if we have any message
  64. // which will pass to either direction, a timer is reset. If we have
  65. // no any reads or writes for this timeout, a connection will be
  66. // aborted.
  67. //
  68. // This is an optional setting.
  69. IdleTimeout time.Duration
  70. // PreferIP defines an IP connectivity preference. Valid values are:
  71. // 'prefer-ipv4', 'prefer-ipv6', 'only-ipv4', 'only-ipv6'.
  72. //
  73. // This is an optional setting.
  74. PreferIP string
  75. }
  76. func (p ProxyOpts) valid() error {
  77. switch {
  78. case p.Network == nil:
  79. return ErrNetworkIsNotDefined
  80. case p.AntiReplayCache == nil:
  81. return ErrAntiReplayCacheIsNotDefined
  82. case p.IPBlocklist == nil:
  83. return ErrIPBlocklistIsNotDefined
  84. case p.EventStream == nil:
  85. return ErrEventStreamIsNotDefined
  86. case p.TimeAttackDetector == nil:
  87. return ErrTimeAttackDetectorIsNotDefined
  88. case p.Logger == nil:
  89. return ErrLoggerIsNotDefined
  90. case !p.Secret.Valid():
  91. return ErrSecretInvalid
  92. }
  93. return nil
  94. }
  95. func (p ProxyOpts) getBufferSize() int {
  96. if p.BufferSize < 1 {
  97. return DefaultBufferSize
  98. }
  99. return int(p.BufferSize)
  100. }
  101. func (p ProxyOpts) getConcurrency() int {
  102. if p.Concurrency == 0 {
  103. return DefaultConcurrency
  104. }
  105. return int(p.Concurrency)
  106. }
  107. func (p ProxyOpts) getDomainFrontingPort() int {
  108. if p.DomainFrontingPort == 0 {
  109. return DefaultDomainFrontingPort
  110. }
  111. return int(p.DomainFrontingPort)
  112. }
  113. func (p ProxyOpts) getIdleTimeout() time.Duration {
  114. if p.IdleTimeout == 0 {
  115. return DefaultIdleTimeout
  116. }
  117. return p.IdleTimeout
  118. }
  119. func (p ProxyOpts) getPreferIP() string {
  120. if p.PreferIP == "" {
  121. return DefaultPreferIP
  122. }
  123. return p.PreferIP
  124. }
  125. func (p ProxyOpts) getLogger(name string) Logger {
  126. return p.Logger.Named(name)
  127. }