small fix and clarifying code and log messages

pull/716/head
sebres 2014-06-10 10:24:55 +02:00
parent bb0a181056
commit bb6655e696
3 changed files with 12 additions and 10 deletions

View File

@ -292,10 +292,10 @@ class Actions(JailThread, Mapping):
if self.__banManager.addBanTicket(bTicket): if self.__banManager.addBanTicket(bTicket):
# report ticket to observer, to check time should be increased and hereafter observer writes ban to database (asynchronous) # report ticket to observer, to check time should be increased and hereafter observer writes ban to database (asynchronous)
if Observers.Main and not bTicket.getRestored(): if Observers.Main is not None and not bTicket.getRestored():
Observers.Main.add('banFound', bTicket, self._jail, btime) Observers.Main.add('banFound', bTicket, self._jail, btime)
logSys.notice("[%s] %sBan %s (%d # %s -> %s)", self._jail.name, ('' if not bTicket.getRestored() else 'Restore '), logSys.notice("[%s] %sBan %s (%s # %s -> %s)", self._jail.name, ('' if not bTicket.getRestored() else 'Restore '),
aInfo["ip"], bTicket.getBanCount()+(1 if not bTicket.getRestored() else 0), *logtime) aInfo["ip"], (bTicket.getBanCount() if bTicket.getRestored() else '_'), *logtime)
# do actions : # do actions :
for name, action in self._actions.iteritems(): for name, action in self._actions.iteritems():
try: try:

View File

@ -427,7 +427,7 @@ class Filter(JailThread):
tick = FailTicket(ip, unixTime, lines) tick = FailTicket(ip, unixTime, lines)
self.failManager.addFailure(tick) self.failManager.addFailure(tick)
# report to observer - failure was found, for possibly increasing of it retry counter (asynchronous) # report to observer - failure was found, for possibly increasing of it retry counter (asynchronous)
if Observers.Main: if Observers.Main is not None:
Observers.Main.add('failureFound', self.failManager, self.jail, tick) Observers.Main.add('failureFound', self.failManager, self.jail, tick)
## ##

View File

@ -343,14 +343,15 @@ class ObserverThread(threading.Thread):
retryCount = 1 retryCount = 1
timeOfBan = None timeOfBan = None
try: try:
maxRetry = failManager.getMaxRetry()
db = jail.database db = jail.database
if db is not None: if db is not None:
for banCount, timeOfBan, lastBanTime in db.getBan(ip, jail): for banCount, timeOfBan, lastBanTime in db.getBan(ip, jail):
retryCount = ((1 << (banCount if banCount < 20 else 20))/2 + 1) retryCount = ((1 << (banCount if banCount < 20 else 20))/2 + 1)
# if lastBanTime == -1 or timeOfBan + lastBanTime * 2 > MyTime.time(): # if lastBanTime == -1 or timeOfBan + lastBanTime * 2 > MyTime.time():
# retryCount = failManager.getMaxRetry() # retryCount = maxRetry
break break
retryCount = min(retryCount, failManager.getMaxRetry()) retryCount = min(retryCount, maxRetry)
# check this ticket already known (line was already processed and in the database and will be restored from there): # check this ticket already known (line was already processed and in the database and will be restored from there):
if timeOfBan is not None and unixTime <= timeOfBan: if timeOfBan is not None and unixTime <= timeOfBan:
logSys.info("[%s] Ignore failure %s before last ban %s < %s, restored" logSys.info("[%s] Ignore failure %s before last ban %s < %s, restored"
@ -360,15 +361,16 @@ class ObserverThread(threading.Thread):
if retryCount <= 1: if retryCount <= 1:
return return
# retry counter was increased - add it again: # retry counter was increased - add it again:
logSys.info("[%s] Found %s, bad - %s, %s # -> %s, ban", jail.name, ip, logSys.info("[%s] Found %s, bad - %s, %s # -> %s%s", jail.name, ip,
datetime.datetime.fromtimestamp(unixTime).strftime("%Y-%m-%d %H:%M:%S"), banCount, retryCount) datetime.datetime.fromtimestamp(unixTime).strftime("%Y-%m-%d %H:%M:%S"), banCount, retryCount,
(', Ban' if retryCount >= maxRetry else ''))
# remove matches from this ticket, because a ticket was already added by filter self # remove matches from this ticket, because a ticket was already added by filter self
ticket.setMatches(None) ticket.setMatches(None)
# retryCount-1, because a ticket was already once incremented by filter self # retryCount-1, because a ticket was already once incremented by filter self
failManager.addFailure(ticket, retryCount - 1, True) failManager.addFailure(ticket, retryCount - 1, True)
# after observe we have increased count >= maxretry ... # after observe we have increased count >= maxretry ...
if retryCount >= failManager.getMaxRetry(): if retryCount >= maxRetry:
# perform the banning of the IP now (again) # perform the banning of the IP now (again)
# [todo]: this code part will be used multiple times - optimize it later. # [todo]: this code part will be used multiple times - optimize it later.
try: # pragma: no branch - exception is the only way out try: # pragma: no branch - exception is the only way out
@ -464,7 +466,7 @@ class ObserverThread(threading.Thread):
# if ban time was prolonged - log again with new ban time: # if ban time was prolonged - log again with new ban time:
if btime != oldbtime: if btime != oldbtime:
logSys.notice("[%s] Increase Ban %s (%d # %s -> %s)", jail.name, logSys.notice("[%s] Increase Ban %s (%d # %s -> %s)", jail.name,
ip, ticket.getBanCount()+1, *logtime) ip, ticket.getBanCount(), *logtime)
# add ticket to database, but only if was not restored (not already read from database): # add ticket to database, but only if was not restored (not already read from database):
if jail.database is not None and not ticket.getRestored(): if jail.database is not None and not ticket.getRestored():
# add to database always only after ban time was calculated an not yet already banned: # add to database always only after ban time was calculated an not yet already banned: