mirror of https://github.com/fail2ban/fail2ban
Merge branch 'f2b-perfom-prepare-716' into ban-time-incr
commit
6daa152c0c
|
@ -67,6 +67,9 @@ def get_opt_parser():
|
||||||
Option('-f', "--fast", action="store_true",
|
Option('-f', "--fast", action="store_true",
|
||||||
dest="fast",
|
dest="fast",
|
||||||
help="Try to increase speed of the tests, decreasing of wait intervals, memory database"),
|
help="Try to increase speed of the tests, decreasing of wait intervals, memory database"),
|
||||||
|
Option('-i', "--ignore", action="store_true",
|
||||||
|
dest="negate_re",
|
||||||
|
help="negate [regexps] filter to ignore tests matched specified regexps"),
|
||||||
Option("-t", "--log-traceback", action='store_true',
|
Option("-t", "--log-traceback", action='store_true',
|
||||||
help="Enrich log-messages with compressed tracebacks"),
|
help="Enrich log-messages with compressed tracebacks"),
|
||||||
Option("--full-traceback", action='store_true',
|
Option("--full-traceback", action='store_true',
|
||||||
|
|
|
@ -98,6 +98,25 @@ class MyTime:
|
||||||
else:
|
else:
|
||||||
return time.localtime(MyTime.myTime)
|
return time.localtime(MyTime.myTime)
|
||||||
|
|
||||||
|
## precreate/precompile primitives used in str2seconds:
|
||||||
|
|
||||||
|
## preparing expression:
|
||||||
|
_str2sec_prep = re.compile(r"(?i)(?<=[a-z])(\d)")
|
||||||
|
## finally expression:
|
||||||
|
_str2sec_fini = re.compile(r"(\d)\s+(\d)")
|
||||||
|
## wrapper for each sub part:
|
||||||
|
_str2sec_subpart = r"(?i)(?<=[\d\s])(%s)\b"
|
||||||
|
## parts to be replaced - pair of (regexp x replacement):
|
||||||
|
_str2sec_parts = (
|
||||||
|
(re.compile(_str2sec_subpart % r"days?|da|dd?"), "*"+str(24*60*60)),
|
||||||
|
(re.compile(_str2sec_subpart % r"weeks?|wee?|ww?"), "*"+str(7*24*60*60)),
|
||||||
|
(re.compile(_str2sec_subpart % r"months?|mon?"), "*"+str((365*3+366)*24*60*60/4/12)),
|
||||||
|
(re.compile(_str2sec_subpart % r"years?|yea?|yy?"), "*"+str((365*3+366)*24*60*60/4)),
|
||||||
|
(re.compile(_str2sec_subpart % r"seconds?|sec?|ss?"), "*"+str(1)),
|
||||||
|
(re.compile(_str2sec_subpart % r"minutes?|min?|mm?"), "*"+str(60)),
|
||||||
|
(re.compile(_str2sec_subpart % r"hours?|hou?|hh?"), "*"+str(60*60)),
|
||||||
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def str2seconds(val):
|
def str2seconds(val):
|
||||||
"""Wraps string expression like "1h 2m 3s" into number contains seconds (3723).
|
"""Wraps string expression like "1h 2m 3s" into number contains seconds (3723).
|
||||||
|
@ -120,13 +139,9 @@ class MyTime:
|
||||||
if isinstance(val, (int, long, float, complex)):
|
if isinstance(val, (int, long, float, complex)):
|
||||||
return val
|
return val
|
||||||
# replace together standing abbreviations, example '1d12h' -> '1d 12h':
|
# replace together standing abbreviations, example '1d12h' -> '1d 12h':
|
||||||
val = re.sub(r"(?i)(?<=[a-z])(\d)", r" \1", val)
|
val = MyTime._str2sec_prep.sub(r" \1", val)
|
||||||
# replace abbreviation with expression:
|
# replace abbreviation with expression:
|
||||||
for rexp, rpl in (
|
for rexp, rpl in MyTime._str2sec_parts:
|
||||||
(r"days?|da|dd?", 24*60*60), (r"weeks?|wee?|ww?", 7*24*60*60), (r"months?|mon?", (365*3+366)*24*60*60/4/12),
|
val = rexp.sub(rpl, val)
|
||||||
(r"years?|yea?|yy?", (365*3+366)*24*60*60/4),
|
val = MyTime._str2sec_fini.sub(r"\1+\2", val)
|
||||||
(r"seconds?|sec?|ss?", 1), (r"minutes?|min?|mm?", 60), (r"hours?|hou?|hh?", 60*60),
|
|
||||||
):
|
|
||||||
val = re.sub(r"(?i)(?<=[\d\s])(%s)\b" % rexp, "*"+str(rpl), val)
|
|
||||||
val = re.sub(r"(\d)\s+(\d)", r"\1+\2", val);
|
|
||||||
return eval(val)
|
return eval(val)
|
||||||
|
|
|
@ -55,6 +55,7 @@ class F2B(optparse.Values):
|
||||||
def __init__(self, opts={}):
|
def __init__(self, opts={}):
|
||||||
self.__dict__ = opts.__dict__ if opts else {
|
self.__dict__ = opts.__dict__ if opts else {
|
||||||
'fast': False, 'memory_db':False, 'no_gamin': False, 'no_network': False,
|
'fast': False, 'memory_db':False, 'no_gamin': False, 'no_network': False,
|
||||||
|
"negate_re": False,
|
||||||
}
|
}
|
||||||
if self.fast:
|
if self.fast:
|
||||||
self.memory_db = True
|
self.memory_db = True
|
||||||
|
@ -158,7 +159,8 @@ def gatherTests(regexps=None, opts=None):
|
||||||
for test in suite:
|
for test in suite:
|
||||||
s = str(test)
|
s = str(test)
|
||||||
for r in self._regexps:
|
for r in self._regexps:
|
||||||
if r.search(s):
|
m = r.search(s)
|
||||||
|
if (m if not opts.negate_re else not m):
|
||||||
matched.append(test)
|
matched.append(test)
|
||||||
break
|
break
|
||||||
for test in matched:
|
for test in matched:
|
||||||
|
|
Loading…
Reference in New Issue