1
0

SharedPlayerRateLimitManager.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using Robust.Shared.Player;
  2. namespace Content.Shared.Players.RateLimiting;
  3. /// <summary>
  4. /// General-purpose system to rate limit actions taken by clients, such as chat messages.
  5. /// </summary>
  6. /// <remarks>
  7. /// <para>
  8. /// Different categories of rate limits must be registered ahead of time by calling <see cref="Register"/>.
  9. /// Once registered, you can simply call <see cref="CountAction"/> to count a rate-limited action for a player.
  10. /// </para>
  11. /// <para>
  12. /// This system is intended for rate limiting player actions over short periods,
  13. /// to ward against spam that can cause technical issues such as admin client load.
  14. /// It should not be used for in-game actions or similar.
  15. /// </para>
  16. /// <para>
  17. /// Rate limits are reset when a client reconnects.
  18. /// This should not be an issue for the reasonably short rate limit periods this system is intended for.
  19. /// </para>
  20. /// </remarks>
  21. /// <seealso cref="RateLimitRegistration"/>
  22. public abstract class SharedPlayerRateLimitManager
  23. {
  24. /// <summary>
  25. /// Count and validate an action performed by a player against rate limits.
  26. /// </summary>
  27. /// <param name="player">The player performing the action.</param>
  28. /// <param name="key">The key string that was previously used to register a rate limit category.</param>
  29. /// <returns>Whether the action counted should be blocked due to surpassing rate limits or not.</returns>
  30. /// <exception cref="ArgumentException">
  31. /// <paramref name="player"/> is not a connected player
  32. /// OR <paramref name="key"/> is not a registered rate limit category.
  33. /// </exception>
  34. /// <seealso cref="Register"/>
  35. public abstract RateLimitStatus CountAction(ICommonSession player, string key);
  36. /// <summary>
  37. /// Register a new rate limit category.
  38. /// </summary>
  39. /// <param name="key">
  40. /// The key string that will be referred to later with <see cref="CountAction"/>.
  41. /// Must be unique and should probably just be a constant somewhere.
  42. /// </param>
  43. /// <param name="registration">The data specifying the rate limit's parameters.</param>
  44. /// <exception cref="InvalidOperationException"><paramref name="key"/> has already been registered.</exception>
  45. /// <exception cref="ArgumentException"><paramref name="registration"/> is invalid.</exception>
  46. public abstract void Register(string key, RateLimitRegistration registration);
  47. /// <summary>
  48. /// Initialize the manager's functionality at game startup.
  49. /// </summary>
  50. public abstract void Initialize();
  51. }