Add leaky bucket ratelimiting system and use it in e2 core (#1251)

* Add rate limit system util and add e2 ratelimiting
This commit is contained in:
Redox 2022-10-30 18:38:40 +01:00 committed by GitHub
parent 9ce8fe00c1
commit ced6c7d36e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 4 deletions

View File

@ -1,7 +1,27 @@
E2Lib.RegisterExtension("pac", true)
util.AddNetworkString("pac_e2_setkeyvalue_str")
util.AddNetworkString("pac_e2_setkeyvalue_num")
util.AddNetworkString("pac_e2_setkeyvalue_vec")
util.AddNetworkString("pac_e2_setkeyvalue_ang")
local enabledConvar = CreateConVar("pac_e2_ratelimit_enable", "1", {FCVAR_ARCHIVE}, "If the e2 ratelimit should be enabled.", 0, 1)
local rate = CreateConVar("pac_e2_ratelimit_refill", "0.025", {FCVAR_ARCHIVE}, "The speed at which the ratelimit buffer refills.", 0, 1000)
local buffer = CreateConVar("pac_e2_ratelimit_buffer", "300", {FCVAR_ARCHIVE}, "How large the ratelimit buffer should be.", 0, 1000)
local function canRunFunction(self)
if not enabledConvar:GetBool() then return true end
local allowed = pac.RatelimitPlayer(self.player, "e2_extension", buffer:GetInt(), rate:GetInt())
if not allowed then
E2Lib.raiseException("pac3 e2 ratelimit exceeded")
return false
end
return true
end
e2function void pacSetKeyValue(entity owner, string global_id, string key, string value)
if not canRunFunction(self) then return end
net.Start("pac_e2_setkeyvalue_str")
net.WriteEntity(self.player)
net.WriteEntity(owner)
@ -12,8 +32,8 @@ e2function void pacSetKeyValue(entity owner, string global_id, string key, strin
net.Broadcast()
end
util.AddNetworkString("pac_e2_setkeyvalue_num")
e2function void pacSetKeyValue(entity owner, string global_id, string key, number value)
if not canRunFunction(self) then return end
net.Start("pac_e2_setkeyvalue_num")
net.WriteEntity(self.player)
net.WriteEntity(owner)
@ -24,8 +44,8 @@ e2function void pacSetKeyValue(entity owner, string global_id, string key, numbe
net.Broadcast()
end
util.AddNetworkString("pac_e2_setkeyvalue_vec")
e2function void pacSetKeyValue(entity owner, string global_id, string key, vector value)
if not canRunFunction(self) then return end
net.Start("pac_e2_setkeyvalue_vec")
net.WriteEntity(self.player)
net.WriteEntity(owner)
@ -36,8 +56,8 @@ e2function void pacSetKeyValue(entity owner, string global_id, string key, vecto
net.Broadcast()
end
util.AddNetworkString("pac_e2_setkeyvalue_ang")
e2function void pacSetKeyValue(entity owner, string global_id, string key, angle value)
if not canRunFunction(self) then return end
net.Start("pac_e2_setkeyvalue_ang")
net.WriteEntity(self.player)
net.WriteEntity(owner)
@ -46,4 +66,4 @@ e2function void pacSetKeyValue(entity owner, string global_id, string key, angle
net.WriteAngle(Angle(value[1], value[2], value[3]))
net.Broadcast()
end
end

View File

@ -1,3 +1,6 @@
local CurTime = CurTime
local math_Clamp = math.Clamp
function pac.dprint(fmt, ...)
if pac.debug then
MsgN("\n")
@ -42,3 +45,33 @@ function pac.RemoveHook(str, id)
hook.Remove(str, id)
end
function pac.RatelimitPlayer( ply, name, buffer, refill )
local ratelimitName = "pac_ratelimit_" .. name
local checkName = "pac_ratelimit_check_" .. name
if not ply[ratelimitName] then ply[ratelimitName] = buffer end
local curTime = CurTime()
if not ply[checkName] then ply[checkName] = curTime end
local dripSize = curTime - ply[checkName]
ply[checkName] = curTime
local drip = dripSize / refill
local newVal = ply[ratelimitName] + drip
ply[ratelimitName] = math_Clamp(newVal, 0, buffer)
if ply[ratelimitName] >= 1 then
ply[ratelimitName] = ply[ratelimitName] - 1
return true
else
return false
end
end
function pac.GetRateLimitPlayerBuffer( ply, name )
local ratelimitName = "pac_ratelimit_" .. name
return ply[ratelimitName] or 0
end

View File

@ -1,3 +1,5 @@
local IsValid = IsValid
hook.Add("pac_Initialized", "pac_e2_extension", function()
if E2Helper then
E2Helper.Descriptions["pacSetKeyValue"] = "Sets a property value on given part. Part unique id is recommended but you can also input name."
@ -8,6 +10,7 @@ local function SetKeyValue(ply, ent, unique_id, key, val)
local set = "Set" .. key
local part = pac.GetPartFromUniqueID(pac.Hash(ply), unique_id)
if not IsValid( part ) then return end
if part:GetRootPart():GetOwner() == ent then
if key == "EventHide" then