|
|
|
@ -21,12 +21,14 @@ Usage:
|
|
|
|
|
import time
|
|
|
|
|
from globals import log
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _time_caps(minutes, hours, days):
|
|
|
|
|
return {
|
|
|
|
|
'min': minutes,
|
|
|
|
|
'hour': hours,
|
|
|
|
|
'day': days,
|
|
|
|
|
}
|
|
|
|
|
"min": minutes,
|
|
|
|
|
"hour": hours,
|
|
|
|
|
"day": days,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Limits(object):
|
|
|
|
|
"""
|
|
|
|
@ -38,7 +40,7 @@ class Limits(object):
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, whitelist=None, limits=None):
|
|
|
|
|
self.intervals = ['min', 'hour', 'day']
|
|
|
|
|
self.intervals = ["min", "hour", "day"]
|
|
|
|
|
|
|
|
|
|
self.divisor = _time_caps(60, 3600, 86400)
|
|
|
|
|
self.last_update = _time_caps(0, 0, 0)
|
|
|
|
@ -54,10 +56,10 @@ class Limits(object):
|
|
|
|
|
self.whitelist = []
|
|
|
|
|
|
|
|
|
|
self.counter = {
|
|
|
|
|
'min': {},
|
|
|
|
|
'hour': {},
|
|
|
|
|
'day': {},
|
|
|
|
|
}
|
|
|
|
|
"min": {},
|
|
|
|
|
"hour": {},
|
|
|
|
|
"day": {},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self._clear_counters_if_needed()
|
|
|
|
|
|
|
|
|
@ -69,13 +71,15 @@ class Limits(object):
|
|
|
|
|
def _limit_exceeded(self, interval, ip_address):
|
|
|
|
|
visits = self.counter[interval][ip_address]
|
|
|
|
|
limit = self._get_limit(interval)
|
|
|
|
|
return visits > limit
|
|
|
|
|
return visits > limit
|
|
|
|
|
|
|
|
|
|
def _get_limit(self, interval):
|
|
|
|
|
return self.limit[interval]
|
|
|
|
|
|
|
|
|
|
def _report_excessive_visits(self, interval, ip_address):
|
|
|
|
|
log("%s LIMITED [%s for %s]" % (ip_address, self._get_limit(interval), interval))
|
|
|
|
|
log(
|
|
|
|
|
"%s LIMITED [%s for %s]" % (ip_address, self._get_limit(interval), interval)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def check_ip(self, ip_address):
|
|
|
|
|
"""
|
|
|
|
@ -89,8 +93,10 @@ class Limits(object):
|
|
|
|
|
self._log_visit(interval, ip_address)
|
|
|
|
|
if self._limit_exceeded(interval, ip_address):
|
|
|
|
|
self._report_excessive_visits(interval, ip_address)
|
|
|
|
|
return ("Not so fast! Number of queries per %s is limited to %s"
|
|
|
|
|
% (interval, self._get_limit(interval)))
|
|
|
|
|
return "Not so fast! Number of queries per %s is limited to %s" % (
|
|
|
|
|
interval,
|
|
|
|
|
self._get_limit(interval),
|
|
|
|
|
)
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
|