From 782702c192e424d1ff439030fdc2d637fcb8d2ac Mon Sep 17 00:00:00 2001 From: Massimo Melina Date: Mon, 8 Jun 2020 12:40:00 +0200 Subject: [PATCH] smarter DoS-preventing grace period --- main.pas | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/main.pas b/main.pas index 3fa4298..dff3eae 100644 --- a/main.pas +++ b/main.pas @@ -1,4 +1,4 @@ -{ +{ Copyright (C) 2002-2014 Massimo Melina (www.rejetto.com) This file is part of HFS ~ HTTP File Server. @@ -3607,17 +3607,21 @@ function Tmainfrm.getFolderPage(folder:Tfile; cd:TconnData; otpl:Tobject):string const ip2availability: Tdictionary = NIL; const folderConcurrents: integer = 0; +const MAX_CONCURRENTS = 3; procedure updateAvailability(); var pair: Tpair; 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; @@ -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);