-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCommandRateLimiter.cs
More file actions
204 lines (177 loc) · 7.26 KB
/
CommandRateLimiter.cs
File metadata and controls
204 lines (177 loc) · 7.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using System;
using System.Collections.Generic;
using System.Linq;
using Oxide.Core;
using UnityEngine;
using Oxide.Core.Libraries.Covalence;
namespace Oxide.Plugins
{
[Info("CommandRateLimiter", "Calytic", "0.0.61", ResourceId = 1812)]
public class CommandRateLimiter : CovalencePlugin
{
private int KickAfter;
private int CooldownMS;
private int ClearRateCountSeconds;
private bool LogExcessiveUsage;
private bool SendPlayerMessage;
Dictionary<string, DateTime> lastRun = new Dictionary<string, DateTime>();
Dictionary<string, Timer> rateTimer = new Dictionary<string, Timer>();
Dictionary<string, int> rateCount = new Dictionary<string, int>();
List<object> commandWhitelist = new List<object>();
private Dictionary<string, Dictionary<string, int>> spamLog = new Dictionary<string, Dictionary<string, int>>();
void OnServerInitialized()
{
Config["CooldownMS"] = CooldownMS = GetConfig("CooldownMS", 195);
Config["KickAfter"] = KickAfter = GetConfig("KickAfter", 10);
Config["ClearRateCountSeconds"] = ClearRateCountSeconds = GetConfig("ClearRateCountSeconds", 5);
Config["LogExcessiveUsage"] = LogExcessiveUsage = GetConfig("LogExcessiveUsage", false);
Config["SendPlayerMessage"] = SendPlayerMessage = GetConfig("SendPlayerMessage", true);
Config["CommandWhitelist"] = commandWhitelist = GetConfig("CommandWhitelist", new List<object>());
Config["Version"] = Version.ToString();
SaveConfig();
LoadMessages();
}
void LoadMessages()
{
lang.RegisterMessages(new Dictionary<string, string>
{
{"Player Message", "You are doing that too often"},
{"Kick Message", "Spamming"},
}, this);
}
void LoadDefaultConfig()
{
Config["CooldownMS"] = 195;
Config["KickAfter"] = 10;
Config["ClearRateCountSeconds"] = 5;
Config["LogExcessiveUsage"] = false;
Config["SendPlayerMessage"] = true;
Config["CommandWhitelist"] = new List<string>();
Config["Version"] = Version.ToString();
}
private T GetConfig<T>(string name, T defaultValue)
{
if (Config[name] == null)
{
return defaultValue;
}
return (T)Convert.ChangeType(Config[name], typeof(T));
}
object OnRunCommand(ConsoleSystem.Arg arg)
{
if (arg.cmd == null) return null;
if (arg.Player() == null)
{
return null;
}
IPlayer player = null;
#if RUST
player = covalence.Players.GetPlayer(arg.Player().UserIDString);
#endif
if (player == null) return null;
if (player.ConnectedPlayer == null) return null;
if (arg.isAdmin)
return null;
DateTime lastTime;
if (lastRun.TryGetValue(player.Id, out lastTime))
{
TimeSpan ts = DateTime.Now - lastTime;
if (ts.TotalMilliseconds < CooldownMS)
{
if (arg.cmd.name != null)
{
if (commandWhitelist.Contains(arg.cmd.name))
{
return null;
}
if (LogExcessiveUsage)
{
if (!spamLog.ContainsKey(player.Id))
{
spamLog.Add(player.Id, new Dictionary<string, int>());
timer.In(ClearRateCountSeconds, delegate()
{
List<string> msgs = new List<string>();
foreach (KeyValuePair<string, int> kvp in spamLog[player.Id])
{
msgs.Add(kvp.Key + " (" + kvp.Value + ")");
}
string cmds = string.Join(", ", msgs.ToArray());
PrintWarning(player.Name + " (" + player.Id + ") spamming commands: " + cmds);
spamLog.Remove(player.Id);
});
}
if (!spamLog[player.Id].ContainsKey(arg.cmd.name))
{
spamLog[player.Id].Add(arg.cmd.name, 1);
}
else
{
spamLog[player.Id][arg.cmd.name]++;
}
}
}
int c = 0;
bool kicked = false;
if (rateCount.TryGetValue(player.Id, out c))
{
rateCount[player.Id]++;
if (KickAfter > 0 && (c + 1) >= KickAfter)
{
player.ConnectedPlayer.Kick(GetMsg("Kick Message"));
kicked = true;
}
}
else
{
rateCount.Add(player.Id, 1);
}
if (ClearRateCountSeconds > 0)
{
Timer rtimer;
if (rateTimer.TryGetValue(player.Id, out rtimer))
{
if (!rtimer.Destroyed)
{
rtimer.Destroy();
}
rateTimer.Remove(player.Id);
}
timer.In(ClearRateCountSeconds, delegate()
{
if (rateCount.TryGetValue(player.Id, out c))
{
rateCount[player.Id] = 0;
}
});
}
if (player.ConnectedPlayer != null && SendPlayerMessage && !kicked)
{
player.Reply(GetMsg("Player Message"));
}
}
else
{
lastRun[player.Id] = DateTime.Now;
}
}
else
{
lastRun.Add(player.Id, DateTime.Now);
}
return null;
}
void OnUserDisconnected(IPlayer player)
{
int c = 0;
if (rateCount.TryGetValue(player.Id, out c))
{
rateCount.Remove(player.Id);
}
}
string GetMsg(string key, object userID = null)
{
return lang.GetMessage(key, this, userID == null ? null : userID.ToString());
}
}
}