Skip to content

Commit

Permalink
smarter DoS-preventing grace period
Browse files Browse the repository at this point in the history
  • Loading branch information
rejetto committed Jun 8, 2020
1 parent 1fc5894 commit 782702c
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions main.pas
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
Copyright (C) 2002-2014 Massimo Melina (www.rejetto.com)
This file is part of HFS ~ HTTP File Server.
Expand Down Expand Up @@ -3607,17 +3607,21 @@ function Tmainfrm.getFolderPage(folder:Tfile; cd:TconnData; otpl:Tobject):string

const ip2availability: Tdictionary<string,Tdatetime> = NIL;
const folderConcurrents: integer = 0;
const MAX_CONCURRENTS = 3;

procedure updateAvailability();
var
pair: Tpair<string,Tdatetime>;
t: Tdatetime;
begin
dec(folderConcurrents);
t:=now();
ip2availability[cd.address]:=t+1/SECONDS;
if folderConcurrents = MAX_CONCURRENTS then // serving multiple addresses at max capacity, let's give a grace period for others
ip2availability[cd.address]:=t + 1/SECONDS
else
ip2availability.Remove(cd.address);
dec(folderConcurrents);
// purge leftovers
for pair in ip2availability do
for pair in ip2availability do
if pair.Value < t then
ip2availability.Remove(pair.Key);
end;
Expand All @@ -3631,7 +3635,7 @@ function Tmainfrm.getFolderPage(folder:Tfile; cd:TconnData; otpl:Tobject):string
exit(FALSE);
except
end;
if folderConcurrents >= 3 then // max number of concurrent folder loading, others are postponed
if folderConcurrents >= MAX_CONCURRENTS then // max number of concurrent folder loading, others are postponed
exit(FALSE);
inc(folderConcurrents);
ip2availability.AddOrSetValue(cd.address, now()+1);
Expand Down

0 comments on commit 782702c

Please sign in to comment.