forked from space-wizards/space-station-14
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rate limit ahelps (space-wizards#29219)
* Make chat rate limits a general-purpose system. Intending to use this with ahelps next. * Rate limt ahelps Fixes space-wizards#28762 * Review comments
- Loading branch information
1 parent
5206ba3
commit fe8158c
Showing
11 changed files
with
344 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,41 @@ | ||
using System.Runtime.InteropServices; | ||
using Content.Server.Players.RateLimiting; | ||
using Content.Shared.CCVar; | ||
using Content.Shared.Database; | ||
using Robust.Shared.Enums; | ||
using Robust.Shared.Player; | ||
using Robust.Shared.Timing; | ||
|
||
namespace Content.Server.Chat.Managers; | ||
|
||
internal sealed partial class ChatManager | ||
{ | ||
private readonly Dictionary<ICommonSession, RateLimitDatum> _rateLimitData = new(); | ||
private const string RateLimitKey = "Chat"; | ||
|
||
public bool HandleRateLimit(ICommonSession player) | ||
private void RegisterRateLimits() | ||
{ | ||
ref var datum = ref CollectionsMarshal.GetValueRefOrAddDefault(_rateLimitData, player, out _); | ||
var time = _gameTiming.RealTime; | ||
if (datum.CountExpires < time) | ||
{ | ||
// Period expired, reset it. | ||
var periodLength = _configurationManager.GetCVar(CCVars.ChatRateLimitPeriod); | ||
datum.CountExpires = time + TimeSpan.FromSeconds(periodLength); | ||
datum.Count = 0; | ||
datum.Announced = false; | ||
} | ||
|
||
var maxCount = _configurationManager.GetCVar(CCVars.ChatRateLimitCount); | ||
datum.Count += 1; | ||
|
||
if (datum.Count <= maxCount) | ||
return true; | ||
|
||
// Breached rate limits, inform admins if configured. | ||
if (_configurationManager.GetCVar(CCVars.ChatRateLimitAnnounceAdmins)) | ||
{ | ||
if (datum.NextAdminAnnounce < time) | ||
_rateLimitManager.Register(RateLimitKey, | ||
new RateLimitRegistration | ||
{ | ||
SendAdminAlert(Loc.GetString("chat-manager-rate-limit-admin-announcement", ("player", player.Name))); | ||
var delay = _configurationManager.GetCVar(CCVars.ChatRateLimitAnnounceAdminsDelay); | ||
datum.NextAdminAnnounce = time + TimeSpan.FromSeconds(delay); | ||
} | ||
} | ||
|
||
if (!datum.Announced) | ||
{ | ||
DispatchServerMessage(player, Loc.GetString("chat-manager-rate-limited"), suppressLog: true); | ||
_adminLogger.Add(LogType.ChatRateLimited, LogImpact.Medium, $"Player {player} breached chat rate limits"); | ||
|
||
datum.Announced = true; | ||
} | ||
|
||
return false; | ||
CVarLimitPeriodLength = CCVars.ChatRateLimitPeriod, | ||
CVarLimitCount = CCVars.ChatRateLimitCount, | ||
CVarAdminAnnounceDelay = CCVars.ChatRateLimitAnnounceAdminsDelay, | ||
PlayerLimitedAction = RateLimitPlayerLimited, | ||
AdminAnnounceAction = RateLimitAlertAdmins, | ||
AdminLogType = LogType.ChatRateLimited, | ||
}); | ||
} | ||
|
||
private void PlayerStatusChanged(object? sender, SessionStatusEventArgs e) | ||
private void RateLimitPlayerLimited(ICommonSession player) | ||
{ | ||
if (e.NewStatus == SessionStatus.Disconnected) | ||
_rateLimitData.Remove(e.Session); | ||
DispatchServerMessage(player, Loc.GetString("chat-manager-rate-limited"), suppressLog: true); | ||
} | ||
|
||
private struct RateLimitDatum | ||
private void RateLimitAlertAdmins(ICommonSession player) | ||
{ | ||
/// <summary> | ||
/// Time stamp (relative to <see cref="IGameTiming.RealTime"/>) this rate limit period will expire at. | ||
/// </summary> | ||
public TimeSpan CountExpires; | ||
|
||
/// <summary> | ||
/// How many messages have been sent in the current rate limit period. | ||
/// </summary> | ||
public int Count; | ||
|
||
/// <summary> | ||
/// Have we announced to the player that they've been blocked in this rate limit period? | ||
/// </summary> | ||
public bool Announced; | ||
if (_configurationManager.GetCVar(CCVars.ChatRateLimitAnnounceAdmins)) | ||
SendAdminAlert(Loc.GetString("chat-manager-rate-limit-admin-announcement", ("player", player.Name))); | ||
} | ||
|
||
/// <summary> | ||
/// Time stamp (relative to <see cref="IGameTiming.RealTime"/>) of the | ||
/// next time we can send an announcement to admins about rate limit breach. | ||
/// </summary> | ||
public TimeSpan NextAdminAnnounce; | ||
public RateLimitStatus HandleRateLimit(ICommonSession player) | ||
{ | ||
return _rateLimitManager.CountAction(player, RateLimitKey); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.