mirror of https://github.com/fail2ban/fail2ban
ENH: asip and @iparg helpers to centralize and uniformize conversion from str to IPAddr
Defined in the same file (and before) as IPAddr to avoid looping of importspull/1374/head
parent
dcfa8c5aa4
commit
ce54db79bc
|
@ -22,12 +22,15 @@ __copyright__ = "Copyright (c) 2004 Cyril Jaquier, 2011-2013 Yaroslav Halchenko"
|
||||||
__license__ = "GPL"
|
__license__ = "GPL"
|
||||||
|
|
||||||
import codecs
|
import codecs
|
||||||
|
import inspect
|
||||||
import fcntl
|
import fcntl
|
||||||
import locale
|
import locale
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
from functools import wraps
|
||||||
|
|
||||||
from .failmanager import FailManagerEmpty, FailManager
|
from .failmanager import FailManagerEmpty, FailManager
|
||||||
from .ticket import FailTicket
|
from .ticket import FailTicket
|
||||||
from .jailthread import JailThread
|
from .jailthread import JailThread
|
||||||
|
@ -909,6 +912,29 @@ class DNSUtils:
|
||||||
|
|
||||||
return ipList
|
return ipList
|
||||||
|
|
||||||
|
def asip(ip):
|
||||||
|
"""A little helper to guarantee ip being an IPAddr instance"""
|
||||||
|
return ip if isinstance(ip, IPAddr) else IPAddr(ip)
|
||||||
|
|
||||||
|
def iparg(f):
|
||||||
|
"""A helper decorator to simplify use of asip throughout the code"""
|
||||||
|
args = inspect.getargspec(f).args
|
||||||
|
# I consider clarity better than trying to avoid any duplication here
|
||||||
|
# also better to make a decision at code parsing stage, not within the
|
||||||
|
# actual decorator function (i.e. checkip)
|
||||||
|
if args and args[0] == 'self':
|
||||||
|
# method -- just above simpler version
|
||||||
|
@wraps(f)
|
||||||
|
def checkip(self, ip, *argv, **kwargs):
|
||||||
|
return f(self, asip(ip), *argv, **kwargs)
|
||||||
|
else:
|
||||||
|
@wraps(f)
|
||||||
|
def checkip(ip, *argv, **kwargs):
|
||||||
|
return f(asip(ip), *argv, **kwargs)
|
||||||
|
|
||||||
|
return checkip
|
||||||
|
|
||||||
|
|
||||||
##
|
##
|
||||||
# Class for IP address handling.
|
# Class for IP address handling.
|
||||||
#
|
#
|
||||||
|
@ -984,8 +1010,8 @@ class IPAddr:
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.ntoa()
|
return self.ntoa()
|
||||||
|
|
||||||
|
@iparg
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
other = other if isinstance(other, IPAddr) else IPAddr(other)
|
|
||||||
if not self.valid and not other.valid: return self.raw == other.raw
|
if not self.valid and not other.valid: return self.raw == other.raw
|
||||||
if not self.valid or not other.valid: return False
|
if not self.valid or not other.valid: return False
|
||||||
if self.addr != other.addr: return False
|
if self.addr != other.addr: return False
|
||||||
|
@ -993,21 +1019,23 @@ class IPAddr:
|
||||||
if self.plen != other.plen: return False
|
if self.plen != other.plen: return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@iparg
|
||||||
def __ne__(self, other):
|
def __ne__(self, other):
|
||||||
other = other if isinstance(other, IPAddr) else IPAddr(other)
|
|
||||||
if not self.valid and not other.valid: return self.raw != other.raw
|
if not self.valid and not other.valid: return self.raw != other.raw
|
||||||
if self.addr != other.addr: return True
|
if self.addr != other.addr: return True
|
||||||
if self.family != other.family: return True
|
if self.family != other.family: return True
|
||||||
if self.plen != other.plen: return True
|
if self.plen != other.plen: return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@iparg
|
||||||
def __lt__(self, other):
|
def __lt__(self, other):
|
||||||
other = other if isinstance(other, IPAddr) else IPAddr(other)
|
|
||||||
return self.family < other.family or self.addr < other.addr
|
return self.family < other.family or self.addr < other.addr
|
||||||
|
|
||||||
|
@iparg
|
||||||
def __add__(self, other):
|
def __add__(self, other):
|
||||||
return "%s%s" % (self, other)
|
return "%s%s" % (self, other)
|
||||||
|
|
||||||
|
@iparg
|
||||||
def __radd__(self, other):
|
def __radd__(self, other):
|
||||||
return "%s%s" % (other, self)
|
return "%s%s" % (other, self)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue