RateLimitRegistration.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using Content.Shared.Database;
  2. using Robust.Shared.Configuration;
  3. using Robust.Shared.Player;
  4. namespace Content.Shared.Players.RateLimiting;
  5. /// <summary>
  6. /// Contains all data necessary to register a rate limit with <see cref="SharedPlayerRateLimitManager.Register"/>.
  7. /// </summary>
  8. public sealed class RateLimitRegistration(
  9. CVarDef<float> cVarLimitPeriodLength,
  10. CVarDef<int> cVarLimitCount,
  11. Action<ICommonSession>? playerLimitedAction,
  12. CVarDef<int>? cVarAdminAnnounceDelay = null,
  13. Action<ICommonSession>? adminAnnounceAction = null,
  14. LogType adminLogType = LogType.RateLimited)
  15. {
  16. /// <summary>
  17. /// CVar that controls the period over which the rate limit is counted, measured in seconds.
  18. /// </summary>
  19. public readonly CVarDef<float> CVarLimitPeriodLength = cVarLimitPeriodLength;
  20. /// <summary>
  21. /// CVar that controls how many actions are allowed in a single rate limit period.
  22. /// </summary>
  23. public readonly CVarDef<int> CVarLimitCount = cVarLimitCount;
  24. /// <summary>
  25. /// An action that gets invoked when this rate limit has been breached by a player.
  26. /// </summary>
  27. /// <remarks>
  28. /// This can be used for informing players or taking administrative action.
  29. /// </remarks>
  30. public readonly Action<ICommonSession>? PlayerLimitedAction = playerLimitedAction;
  31. /// <summary>
  32. /// CVar that controls the minimum delay between admin notifications, measured in seconds.
  33. /// This can be omitted to have no admin notification system.
  34. /// If the cvar is set to 0, there every breach will be reported.
  35. /// If the cvar is set to a negative number, admin announcements are disabled.
  36. /// </summary>
  37. /// <remarks>
  38. /// If set, <see cref="AdminAnnounceAction"/> must be set too.
  39. /// </remarks>
  40. public readonly CVarDef<int>? CVarAdminAnnounceDelay = cVarAdminAnnounceDelay;
  41. /// <summary>
  42. /// An action that gets invoked when a rate limit was breached and admins should be notified.
  43. /// </summary>
  44. /// <remarks>
  45. /// If set, <see cref="CVarAdminAnnounceDelay"/> must be set too.
  46. /// </remarks>
  47. public readonly Action<ICommonSession>? AdminAnnounceAction = adminAnnounceAction;
  48. /// <summary>
  49. /// Log type used to log rate limit violations to the admin logs system.
  50. /// </summary>
  51. public readonly LogType AdminLogType = adminLogType;
  52. }
  53. /// <summary>
  54. /// Result of a rate-limited operation.
  55. /// </summary>
  56. /// <seealso cref="SharedPlayerRateLimitManager.CountAction"/>
  57. public enum RateLimitStatus : byte
  58. {
  59. /// <summary>
  60. /// The action was not blocked by the rate limit.
  61. /// </summary>
  62. Allowed,
  63. /// <summary>
  64. /// The action was blocked by the rate limit.
  65. /// </summary>
  66. Blocked,
  67. }