"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "fail2ban/server/ticket.py" between
fail2ban-0.10.5.tar.gz and fail2ban-0.11.1.tar.gz

About:

ticket.py  (fail2ban-0.10.5):ticket.py  (fail2ban-0.11.1)
skipping to change at line 27 skipping to change at line 27
# along with Fail2Ban; if not, write to the Free Software # along with Fail2Ban; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA . # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA .
# Author: Cyril Jaquier # Author: Cyril Jaquier
# #
__author__ = "Cyril Jaquier" __author__ = "Cyril Jaquier"
__copyright__ = "Copyright (c) 2004 Cyril Jaquier" __copyright__ = "Copyright (c) 2004 Cyril Jaquier"
__license__ = "GPL" __license__ = "GPL"
import sys
from ..helpers import getLogger from ..helpers import getLogger
from .ipdns import IPAddr from .ipdns import IPAddr
from .mytime import MyTime from .mytime import MyTime
# Gets the instance of the logger. # Gets the instance of the logger.
logSys = getLogger(__name__) logSys = getLogger(__name__)
class Ticket(object): class Ticket(object):
__slots__ = ('_ip', '_flags', '_banCount', '_banTime', '_time', '_data', '_retry', '_lastReset')
MAX_TIME = 0X7FFFFFFFFFFF ;# 4461763-th year MAX_TIME = 0X7FFFFFFFFFFF ;# 4461763-th year
RESTORED = 0x01 RESTORED = 0x01
BANNED = 0x08 BANNED = 0x08
def __init__(self, ip=None, time=None, matches=None, data={}, ticket=None ): def __init__(self, ip=None, time=None, matches=None, data={}, ticket=None ):
"""Ticket constructor """Ticket constructor
@param ip the IP address @param ip the IP address
skipping to change at line 63 skipping to change at line 62
self._banCount = 0; self._banCount = 0;
self._banTime = None; self._banTime = None;
self._time = time if time is not None else MyTime.time() self._time = time if time is not None else MyTime.time()
self._data = {'matches': matches or [], 'failures': 0} self._data = {'matches': matches or [], 'failures': 0}
if data is not None: if data is not None:
for k,v in data.iteritems(): for k,v in data.iteritems():
if v is not None: if v is not None:
self._data[k] = v self._data[k] = v
if ticket: if ticket:
# ticket available - copy whole information from ticket: # ticket available - copy whole information from ticket:
self.__dict__.update(i for i in ticket.__dict__.iteritems self.update(ticket)
() if i[0] in self.__dict__) #self.__dict__.update(i for i in ticket.__dict__.iteritem
s() if i[0] in self.__dict__)
def __str__(self): def __str__(self):
return "%s: ip=%s time=%s #attempts=%d matches=%r" % \ return "%s: ip=%s time=%s bantime=%s bancount=%s #attempts=%d mat
(self.__class__.__name__.split('.')[-1], self.__ip, se ches=%r" % \
lf._time, (self.__class__.__name__.split('.')[-1], self._i
self._data['failures'], self._data.get('matches', p, self._time,
[])) self._banTime, self._banCount,
self._data['failures'], self._data.get('m
atches', []))
def __repr__(self): def __repr__(self):
return str(self) return str(self)
def __eq__(self, other): def __eq__(self, other):
try: try:
return self.__ip == other.__ip and \ return self._ip == other._ip and \
round(self._time, 2) == round(other._time, 2) and \ round(self._time, 2) == round(other._time, 2) and \
self._data == other._data self._data == other._data
except AttributeError: except AttributeError:
return False return False
def update(self, ticket):
for n in ticket.__slots__:
v = getattr(ticket, n, None)
if v is not None:
setattr(self, n, v)
def setIP(self, value): def setIP(self, value):
# guarantee using IPAddr instead of unicode, str for the IP # guarantee using IPAddr instead of unicode, str for the IP
if isinstance(value, basestring): if isinstance(value, basestring):
value = IPAddr(value) value = IPAddr(value)
self.__ip = value self._ip = value
def getID(self): def getID(self):
return self._data.get('fid', self.__ip) return self._data.get('fid', self._ip)
def getIP(self): def getIP(self):
return self.__ip return self._ip
def setTime(self, value): def setTime(self, value):
self._time = value self._time = value
def getTime(self): def getTime(self):
return self._time return self._time
def setBanTime(self, value): def setBanTime(self, value):
self._banTime = value; self._banTime = value
def getBanTime(self, defaultBT=None): def getBanTime(self, defaultBT=None):
return (self._banTime if self._banTime is not None else defaultBT ) return (self._banTime if self._banTime is not None else defaultBT )
def setBanCount(self, value): def setBanCount(self, value, always=False):
self._banCount = value; if always or value > self._banCount:
self._banCount = value
def incrBanCount(self, value = 1): def incrBanCount(self, value=1):
self._banCount += value; self._banCount += value
def getBanCount(self): def getBanCount(self):
return self._banCount; return self._banCount;
def getEndOfBanTime(self, defaultBT=None): def getEndOfBanTime(self, defaultBT=None):
bantime = (self._banTime if self._banTime is not None else defaul tBT) bantime = (self._banTime if self._banTime is not None else defaul tBT)
# permanent # permanent
if bantime == -1: if bantime == -1:
return Ticket.MAX_TIME return Ticket.MAX_TIME
# unban time (end of ban): # unban time (end of ban):
skipping to change at line 219 skipping to change at line 227
def banEpoch(self): def banEpoch(self):
return getattr(self, '_banEpoch', 0) return getattr(self, '_banEpoch', 0)
@banEpoch.setter @banEpoch.setter
def banEpoch(self, value): def banEpoch(self, value):
self._banEpoch = value self._banEpoch = value
class FailTicket(Ticket): class FailTicket(Ticket):
def __init__(self, ip=None, time=None, matches=None, data={}, ticket=None ): def __init__(self, ip=None, time=None, matches=None, data={}, ticket=None ):
# this class variables: # this class variables:
self.__retry = 0 self._retry = 0
self.__lastReset = None self._lastReset = None
# create/copy using default ticket constructor: # create/copy using default ticket constructor:
Ticket.__init__(self, ip, time, matches, data, ticket) Ticket.__init__(self, ip, time, matches, data, ticket)
# init: # init:
if ticket is None: if ticket is None:
self.__lastReset = time if time is not None else self.get self._lastReset = time if time is not None else self.getT
Time() ime()
if not self.__retry: if not self._retry:
self.__retry = self._data['failures']; self._retry = self._data['failures'];
def setRetry(self, value): def setRetry(self, value):
""" Set artificial retry count, normally equal failures / attempt , """ Set artificial retry count, normally equal failures / attempt ,
used in incremental features (BanTimeIncr) to increase retry coun t for bad IPs used in incremental features (BanTimeIncr) to increase retry coun t for bad IPs
""" """
self.__retry = value self._retry = value
if not self._data['failures']: if not self._data['failures']:
self._data['failures'] = 1 self._data['failures'] = 1
if not value: if not value:
self._data['failures'] = 0 self._data['failures'] = 0
self._data['matches'] = [] self._data['matches'] = []
def getRetry(self): def getRetry(self):
""" Returns failures / attempt count or """ Returns failures / attempt count or
artificial retry count increased for bad IPs artificial retry count increased for bad IPs
""" """
return max(self.__retry, self._data['failures']) return max(self._retry, self._data['failures'])
def inc(self, matches=None, attempt=1, count=1): def inc(self, matches=None, attempt=1, count=1):
self.__retry += count self._retry += count
self._data['failures'] += attempt self._data['failures'] += attempt
if matches: if matches:
# we should duplicate "matches", because possibly referen ced to multiple tickets: # we should duplicate "matches", because possibly referen ced to multiple tickets:
if self._data['matches']: if self._data['matches']:
self._data['matches'] = self._data['matches'] + m atches self._data['matches'] = self._data['matches'] + m atches
else: else:
self._data['matches'] = matches self._data['matches'] = matches
def setLastTime(self, value): def setLastTime(self, value):
if value > self._time: if value > self._time:
self._time = value self._time = value
def getLastTime(self): def getLastTime(self):
return self._time return self._time
def getLastReset(self): def getLastReset(self):
return self.__lastReset return self._lastReset
def setLastReset(self, value): def setLastReset(self, value):
self.__lastReset = value self._lastReset = value
@staticmethod
def wrap(o):
o.__class__ = FailTicket
return o
## ##
# Ban Ticket. # Ban Ticket.
# #
# This class extends the Ticket class. It is mainly used by the BanManager. # This class extends the Ticket class. It is mainly used by the BanManager.
class BanTicket(Ticket): class BanTicket(FailTicket):
pass
@staticmethod
def wrap(o):
o.__class__ = BanTicket
return o
 End of changes. 20 change blocks. 
29 lines changed or deleted 43 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)