From de8786dd1d81a9d42cef56f0c7a23d5f140c8e7e Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Wed, 5 Oct 2011 10:44:35 -0400 Subject: [PATCH] ENH: introduced usa of Ticket.__matches throughout --- server/actions.py | 2 ++ server/banmanager.py | 2 +- server/faildata.py | 18 +++++++++++++----- server/failmanager.py | 7 ++++--- server/filter.py | 2 +- 5 files changed, 21 insertions(+), 10 deletions(-) diff --git a/server/actions.py b/server/actions.py index 96684485..3f3a9211 100644 --- a/server/actions.py +++ b/server/actions.py @@ -161,6 +161,7 @@ class Actions(JailThread): aInfo["ip"] = bTicket.getIP() aInfo["failures"] = bTicket.getAttempt() aInfo["time"] = bTicket.getTime() + aInfo["matches"] = bTicket.getMatches() if self.__banManager.addBanTicket(bTicket): logSys.warn("[%s] Ban %s" % (self.jail.getName(), aInfo["ip"])) for action in self.__actions: @@ -201,6 +202,7 @@ class Actions(JailThread): aInfo["ip"] = ticket.getIP() aInfo["failures"] = ticket.getAttempt() aInfo["time"] = ticket.getTime() + aInfo["matches"] = ticket.getMatches() logSys.warn("[%s] Unban %s" % (self.jail.getName(), aInfo["ip"])) for action in self.__actions: action.execActionUnban(aInfo) diff --git a/server/banmanager.py b/server/banmanager.py index b84c3691..328ad032 100644 --- a/server/banmanager.py +++ b/server/banmanager.py @@ -133,7 +133,7 @@ class BanManager: ip = ticket.getIP() #lastTime = ticket.getTime() lastTime = MyTime.time() - banTicket = BanTicket(ip, lastTime) + banTicket = BanTicket(ip, lastTime, ticket.getMatches()) banTicket.setAttempt(ticket.getAttempt()) return banTicket createBanTicket = staticmethod(createBanTicket) diff --git a/server/faildata.py b/server/faildata.py index 3cc75c24..2b7959a7 100644 --- a/server/faildata.py +++ b/server/faildata.py @@ -38,16 +38,24 @@ class FailData: self.__retry = 0 self.__lastTime = 0 self.__lastReset = 0 - + self.__matches = [] + def setRetry(self, value): self.__retry = value - + # keep only the last matches or reset entirely + self.__matches = self.__matches[-min(len(self.__matches, value)):] \ + if value else [] + def getRetry(self): return self.__retry - - def inc(self): + + def getMatches(self): + return self.__matches + + def inc(self, matches=None): self.__retry += 1 - + self.__matches += matches or [] + def setLastTime(self, value): if value > self.__lastTime: self.__lastTime = value diff --git a/server/failmanager.py b/server/failmanager.py index c9dbdac1..73e5f242 100644 --- a/server/failmanager.py +++ b/server/failmanager.py @@ -91,16 +91,17 @@ class FailManager: self.__lock.acquire() ip = ticket.getIP() unixTime = ticket.getTime() + matches = ticket.getMatches() if self.__failList.has_key(ip): fData = self.__failList[ip] if fData.getLastReset() < unixTime - self.__maxTime: fData.setLastReset(unixTime) fData.setRetry(0) - fData.inc() + fData.inc(matches) fData.setLastTime(unixTime) else: fData = FailData() - fData.inc() + fData.inc(matches) fData.setLastReset(unixTime) fData.setLastTime(unixTime) self.__failList[ip] = fData @@ -139,7 +140,7 @@ class FailManager: if data.getRetry() >= self.__maxRetry: self.__delFailure(ip) # Create a FailTicket from BanData - failTicket = FailTicket(ip, data.getLastTime()) + failTicket = FailTicket(ip, data.getLastTime(), data.getMatches()) failTicket.setAttempt(data.getRetry()) return failTicket raise FailManagerEmpty diff --git a/server/filter.py b/server/filter.py index b8f75aa0..38151d90 100644 --- a/server/filter.py +++ b/server/filter.py @@ -281,7 +281,7 @@ class Filter(JailThread): logSys.debug("Ignore %s" % ip) continue logSys.debug("Found %s" % ip) - self.failManager.addFailure(FailTicket(ip, unixTime)) + self.failManager.addFailure(FailTicket(ip, unixTime, [line])) ## # Returns true if the line should be ignored.