bgneal@506: """ bgneal@506: The rate_limit_clear command is used to clear IP addresses out from our rate bgneal@506: limit protection database. bgneal@506: bgneal@506: """ bgneal@506: from optparse import make_option bgneal@506: import re bgneal@506: bgneal@506: from django.core.management.base import BaseCommand bgneal@506: import redis bgneal@506: bgneal@508: from core.services import get_redis_connection bgneal@508: bgneal@508: bgneal@506: IP_RE = re.compile(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$') bgneal@506: bgneal@506: bgneal@506: class Command(BaseCommand): bgneal@506: help = """Remove IP addresses from the rate limit protection datastore.""" bgneal@506: option_list = list(BaseCommand.option_list) + [ bgneal@506: make_option("--purge", action="store_true", bgneal@506: help="Purge all IP addresses"), bgneal@506: ] bgneal@506: bgneal@506: def handle(self, *args, **kwargs): bgneal@506: try: bgneal@508: con = get_redis_connection() bgneal@506: bgneal@506: # get all rate-limit keys bgneal@506: keys = con.keys('rate-limit-*') bgneal@506: bgneal@506: # if purging, delete them all... bgneal@506: if kwargs['purge']: bgneal@506: if keys: bgneal@506: con.delete(*keys) bgneal@506: return bgneal@506: bgneal@506: # otherwise delete the ones the user asked for bgneal@506: ips = [] bgneal@506: for ip in args: bgneal@506: if IP_RE.match(ip): bgneal@506: key = 'rate-limit-%s' % ip bgneal@506: if key in keys: bgneal@506: ips.append(key) bgneal@506: else: bgneal@506: self.stdout.write('%s not found\n' % ip) bgneal@506: else: bgneal@506: self.stderr.write('invalid IP address %s\n' % ip) bgneal@506: bgneal@506: if ips: bgneal@506: con.delete(*ips) bgneal@506: bgneal@506: except redis.RedisError, e: bgneal@506: self.stderr.write('%s\n' % e)