mirror of https://github.com/fail2ban/fail2ban
ENH: remove some code duplication, enhance docstrings, uniformize naming
parent
7eafb0c206
commit
c84b6370c5
|
@ -998,7 +998,7 @@ class DNSUtils:
|
||||||
IP_CRE = re.compile("^(?:\d{1,3}\.){3}\d{1,3}$")
|
IP_CRE = re.compile("^(?:\d{1,3}\.){3}\d{1,3}$")
|
||||||
|
|
||||||
# todo: make configurable the expired time and max count of cache entries:
|
# todo: make configurable the expired time and max count of cache entries:
|
||||||
CACHE_dnsToIp = Utils.Cache(maxCount=1000, maxTime=5*60)
|
CACHE_nameToIp = Utils.Cache(maxCount=1000, maxTime=5*60)
|
||||||
CACHE_ipToName = Utils.Cache(maxCount=1000, maxTime=5*60)
|
CACHE_ipToName = Utils.Cache(maxCount=1000, maxTime=5*60)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@ -1007,7 +1007,7 @@ class DNSUtils:
|
||||||
Thanks to Kevin Drapel.
|
Thanks to Kevin Drapel.
|
||||||
"""
|
"""
|
||||||
# cache, also prevent long wait during retrieving of ip for wrong dns or lazy dns-system:
|
# cache, also prevent long wait during retrieving of ip for wrong dns or lazy dns-system:
|
||||||
v = DNSUtils.CACHE_dnsToIp.get(dns)
|
v = DNSUtils.CACHE_nameToIp.get(dns)
|
||||||
if v is not None:
|
if v is not None:
|
||||||
return v
|
return v
|
||||||
# retrieve ip (todo: use AF_INET6 for IPv6)
|
# retrieve ip (todo: use AF_INET6 for IPv6)
|
||||||
|
@ -1017,7 +1017,7 @@ class DNSUtils:
|
||||||
# todo: make configurable the expired time of cache entry:
|
# todo: make configurable the expired time of cache entry:
|
||||||
logSys.warning("Unable to find a corresponding IP address for %s: %s", dns, e)
|
logSys.warning("Unable to find a corresponding IP address for %s: %s", dns, e)
|
||||||
v = list()
|
v = list()
|
||||||
DNSUtils.CACHE_dnsToIp.set(dns, v)
|
DNSUtils.CACHE_nameToIp.set(dns, v)
|
||||||
return v
|
return v
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
|
@ -111,12 +111,13 @@ class Utils():
|
||||||
If shell is True (default), the specified command (may be a string) will be
|
If shell is True (default), the specified command (may be a string) will be
|
||||||
executed through the shell.
|
executed through the shell.
|
||||||
output : bool
|
output : bool
|
||||||
If output is True, the function returns tuple (success, stdoutdata, stderrdata, returncode)
|
If output is True, the function returns tuple (success, stdoutdata, stderrdata, returncode).
|
||||||
|
If False, just indication of success is returned
|
||||||
|
|
||||||
Returns
|
Returns
|
||||||
-------
|
-------
|
||||||
bool
|
bool or (bool, str, str, int)
|
||||||
True if the command succeeded.
|
True if the command succeeded and with stdout, stderr, returncode if output was set to True
|
||||||
|
|
||||||
Raises
|
Raises
|
||||||
------
|
------
|
||||||
|
@ -185,9 +186,10 @@ class Utils():
|
||||||
logSys.log(std_level, "%s -- stderr: %r", realCmd, stderr)
|
logSys.log(std_level, "%s -- stderr: %r", realCmd, stderr)
|
||||||
popen.stderr.close()
|
popen.stderr.close()
|
||||||
|
|
||||||
|
success = False
|
||||||
if retcode == 0:
|
if retcode == 0:
|
||||||
logSys.debug("%s -- returned successfully", realCmd)
|
logSys.debug("%s -- returned successfully", realCmd)
|
||||||
return True if not output else (True, stdout, stderr, retcode)
|
success = True
|
||||||
elif retcode is None:
|
elif retcode is None:
|
||||||
logSys.error("%s -- unable to kill PID %i" % (realCmd, popen.pid))
|
logSys.error("%s -- unable to kill PID %i" % (realCmd, popen.pid))
|
||||||
elif retcode < 0 or retcode > 128:
|
elif retcode < 0 or retcode > 128:
|
||||||
|
@ -200,13 +202,13 @@ class Utils():
|
||||||
logSys.error("%s -- returned %i" % (realCmd, retcode))
|
logSys.error("%s -- returned %i" % (realCmd, retcode))
|
||||||
if msg:
|
if msg:
|
||||||
logSys.info("HINT on %i: %s", retcode, msg % locals())
|
logSys.info("HINT on %i: %s", retcode, msg % locals())
|
||||||
return False if not output else (False, stdout, stderr, retcode)
|
return success if not output else (success, stdout, stderr, retcode)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def wait_for(cond, timeout, interval=None):
|
def wait_for(cond, timeout, interval=None):
|
||||||
"""Wait until condition expression `cond` is True, up to `timeout` sec
|
"""Wait until condition expression `cond` is True, up to `timeout` sec
|
||||||
"""
|
"""
|
||||||
ini = 1
|
ini = 1 # to delay initializations until/when necessary
|
||||||
while True:
|
while True:
|
||||||
ret = cond()
|
ret = cond()
|
||||||
if ret:
|
if ret:
|
||||||
|
|
Loading…
Reference in New Issue