- Added the class MyTime. Replaces call to time.time() and time.gmtime(). A fixed time value can be set for testing purpose

git-svn-id: https://fail2ban.svn.sourceforge.net/svnroot/fail2ban/trunk@418 a942ae1a-1317-0410-a47c-b1dcaea8d605
0.x
Cyril Jaquier 2006-10-18 22:30:57 +00:00
parent 7cae060ec5
commit af41290fc6
8 changed files with 65 additions and 8 deletions

View File

@ -38,6 +38,7 @@ server/__init__.py
server/dateepoch.py
server/banmanager.py
server/datetemplate.py
server/mytime.py
testcases/banmanagertestcase.py
testcases/failmanagertestcase.py
testcases/clientreadertestcase.py

View File

@ -27,6 +27,7 @@ __license__ = "GPL"
from banmanager import BanManager
from jailthread import JailThread
from action import Action
from mytime import MyTime
import time, logging
# Gets the instance of the logger.
@ -146,7 +147,7 @@ class Actions(JailThread):
# Unban IP address which are outdated.
def __checkUnBan(self):
for ticket in self.__banManager.unBanList(time.time()):
for ticket in self.__banManager.unBanList(MyTime.time()):
aInfo = dict()
aInfo["ip"] = ticket.getIP()
logSys.warn("[%s] Unban %s" % (self.jail.getName(), aInfo["ip"]))

View File

@ -26,6 +26,7 @@ __license__ = "GPL"
from banticket import BanTicket
from threading import Lock
from mytime import MyTime
import time, logging
# Gets the instance of the logger.
@ -112,7 +113,7 @@ class BanManager:
def createBanTicket(ticket):
ip = ticket.getIP()
#lastTime = ticket.getTime()
lastTime = time.time()
lastTime = MyTime.time()
banTicket = BanTicket(ip, lastTime)
banTicket.setAttempt(ticket.getAttempt())
return banTicket

View File

@ -25,6 +25,7 @@ __date__ = "$Date: 2006-09-04 21:19:58 +0200 (Mon, 04 Sep 2006) $"
__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
__license__ = "GPL"
from mytime import MyTime
import time
from datetemplate import DateTemplate
@ -74,10 +75,10 @@ class DateStrptime(DateTemplate):
date = list(time.strptime(conv, self.getPattern()))
if date[0] < 2000:
# There is probably no year field in the logs
date[0] = time.gmtime()[0]
date[0] = MyTime.gmtime()[0]
# Bug fix for #1241756
# If the date is greater than the current time, we suppose
# that the log is not from this year but from the year before
if time.mktime(date) > time.time():
if time.mktime(date) > MyTime.time():
date[0] -= 1
return date

View File

@ -28,6 +28,7 @@ from failmanager import FailManager
from failticket import FailTicket
from jailthread import JailThread
from datedetector import DateDetector
from mytime import MyTime
import time, logging, re
@ -342,7 +343,7 @@ class Filter(JailThread):
# Gets all the failure in the log file.
#
# Gets all the failure in the log file which are newer than
# time.time()-self.findTime. When a failure is detected, a FailTicket
# MyTime.time()-self.findTime. When a failure is detected, a FailTicket
# is created and is added to the FailManager.
def getFailures(self, filename):
@ -369,7 +370,7 @@ class Filter(JailThread):
for element in self.findFailure(line):
ip = element[0]
unixTime = element[1]
if unixTime < time.time()-self.__findTime:
if unixTime < MyTime.time()-self.__findTime:
break
if self.inIgnoreIPList(ip):
logSys.debug("Ignore "+ip)

View File

@ -26,6 +26,7 @@ __license__ = "GPL"
from failmanager import FailManagerEmpty
from filter import Filter
from mytime import MyTime
import time, logging, gamin
@ -109,7 +110,7 @@ class FilterGamin(Filter):
ticket = self.failManager.toBan()
self.jail.putFailTicket(ticket)
except FailManagerEmpty:
self.failManager.cleanup(time.time())
self.failManager.cleanup(MyTime.time())
self.dateDetector.sortTemplate()
self.modified = False
time.sleep(self.getSleepTime())

View File

@ -26,6 +26,7 @@ __license__ = "GPL"
from failmanager import FailManagerEmpty
from filter import Filter
from mytime import MyTime
import time, logging, os
@ -104,7 +105,7 @@ class FilterPoll(Filter):
ticket = self.failManager.toBan()
self.jail.putFailTicket(ticket)
except FailManagerEmpty:
self.failManager.cleanup(time.time())
self.failManager.cleanup(MyTime.time())
self.dateDetector.sortTemplate()
self.modified = False
time.sleep(self.getSleepTime())

50
server/mytime.py Normal file
View File

@ -0,0 +1,50 @@
# This file is part of Fail2Ban.
#
# Fail2Ban is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Fail2Ban is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Fail2Ban; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Author: Cyril Jaquier
#
# $Revision: 321 $
__author__ = "Cyril Jaquier"
__version__ = "$Revision: 321 $"
__date__ = "$Date: 2006-09-04 21:19:58 +0200 (Mon, 04 Sep 2006) $"
__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
__license__ = "GPL"
import time
class MyTime:
myTime = None
@staticmethod
def setTime(t):
MyTime.myTime = t
@staticmethod
def time():
if MyTime.myTime == None:
return time.time()
else:
return MyTime.myTime
@staticmethod
def gmtime():
if MyTime.myTime == None:
return time.gmtime()
else:
return time.gmtime(MyTime.myTime)