25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

146 satır
5.3 KiB

  1. # Copyright 2014-2016 OpenMarket Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import Any, Dict, Optional
  15. import attr
  16. from synapse.types import JsonDict
  17. from ._base import Config
  18. class RateLimitConfig:
  19. def __init__(
  20. self,
  21. config: Dict[str, float],
  22. defaults: Optional[Dict[str, float]] = None,
  23. ):
  24. defaults = defaults or {"per_second": 0.17, "burst_count": 3.0}
  25. self.per_second = config.get("per_second", defaults["per_second"])
  26. self.burst_count = int(config.get("burst_count", defaults["burst_count"]))
  27. @attr.s(auto_attribs=True)
  28. class FederationRateLimitConfig:
  29. window_size: int = 1000
  30. sleep_limit: int = 10
  31. sleep_delay: int = 500
  32. reject_limit: int = 50
  33. concurrent: int = 3
  34. class RatelimitConfig(Config):
  35. section = "ratelimiting"
  36. def read_config(self, config: JsonDict, **kwargs: Any) -> None:
  37. # Load the new-style messages config if it exists. Otherwise fall back
  38. # to the old method.
  39. if "rc_message" in config:
  40. self.rc_message = RateLimitConfig(
  41. config["rc_message"], defaults={"per_second": 0.2, "burst_count": 10.0}
  42. )
  43. else:
  44. self.rc_message = RateLimitConfig(
  45. {
  46. "per_second": config.get("rc_messages_per_second", 0.2),
  47. "burst_count": config.get("rc_message_burst_count", 10.0),
  48. }
  49. )
  50. # Load the new-style federation config, if it exists. Otherwise, fall
  51. # back to the old method.
  52. if "rc_federation" in config:
  53. self.rc_federation = FederationRateLimitConfig(**config["rc_federation"])
  54. else:
  55. self.rc_federation = FederationRateLimitConfig(
  56. **{
  57. k: v
  58. for k, v in {
  59. "window_size": config.get("federation_rc_window_size"),
  60. "sleep_limit": config.get("federation_rc_sleep_limit"),
  61. "sleep_delay": config.get("federation_rc_sleep_delay"),
  62. "reject_limit": config.get("federation_rc_reject_limit"),
  63. "concurrent": config.get("federation_rc_concurrent"),
  64. }.items()
  65. if v is not None
  66. }
  67. )
  68. self.rc_registration = RateLimitConfig(config.get("rc_registration", {}))
  69. self.rc_registration_token_validity = RateLimitConfig(
  70. config.get("rc_registration_token_validity", {}),
  71. defaults={"per_second": 0.1, "burst_count": 5},
  72. )
  73. rc_login_config = config.get("rc_login", {})
  74. self.rc_login_address = RateLimitConfig(rc_login_config.get("address", {}))
  75. self.rc_login_account = RateLimitConfig(rc_login_config.get("account", {}))
  76. self.rc_login_failed_attempts = RateLimitConfig(
  77. rc_login_config.get("failed_attempts", {})
  78. )
  79. self.federation_rr_transactions_per_room_per_second = config.get(
  80. "federation_rr_transactions_per_room_per_second", 50
  81. )
  82. rc_admin_redaction = config.get("rc_admin_redaction")
  83. self.rc_admin_redaction = None
  84. if rc_admin_redaction:
  85. self.rc_admin_redaction = RateLimitConfig(rc_admin_redaction)
  86. self.rc_joins_local = RateLimitConfig(
  87. config.get("rc_joins", {}).get("local", {}),
  88. defaults={"per_second": 0.1, "burst_count": 10},
  89. )
  90. self.rc_joins_remote = RateLimitConfig(
  91. config.get("rc_joins", {}).get("remote", {}),
  92. defaults={"per_second": 0.01, "burst_count": 10},
  93. )
  94. # Ratelimit cross-user key requests:
  95. # * For local requests this is keyed by the sending device.
  96. # * For requests received over federation this is keyed by the origin.
  97. #
  98. # Note that this isn't exposed in the configuration as it is obscure.
  99. self.rc_key_requests = RateLimitConfig(
  100. config.get("rc_key_requests", {}),
  101. defaults={"per_second": 20, "burst_count": 100},
  102. )
  103. self.rc_3pid_validation = RateLimitConfig(
  104. config.get("rc_3pid_validation") or {},
  105. defaults={"per_second": 0.003, "burst_count": 5},
  106. )
  107. self.rc_invites_per_room = RateLimitConfig(
  108. config.get("rc_invites", {}).get("per_room", {}),
  109. defaults={"per_second": 0.3, "burst_count": 10},
  110. )
  111. self.rc_invites_per_user = RateLimitConfig(
  112. config.get("rc_invites", {}).get("per_user", {}),
  113. defaults={"per_second": 0.003, "burst_count": 5},
  114. )
  115. self.rc_third_party_invite = RateLimitConfig(
  116. config.get("rc_third_party_invite", {}),
  117. defaults={
  118. "per_second": self.rc_message.per_second,
  119. "burst_count": self.rc_message.burst_count,
  120. },
  121. )