Commit eba9b018 authored by Thomas Capricelli's avatar Thomas Capricelli
Browse files

rewrite / clean

user reports were checked twice against generic WhiteList
parent af6b9233b837
......@@ -29,20 +29,37 @@ class Command(BaseCommand):
for each user..
"""
def handle(self, *args, **options):
self.check_ip_global()
self.check_ip_users()
def check_ip_global(self):
self.stdout.write("Checking %d reports against general white list (%d items)" % (
ReportBadIP.objects.count(),
IPWhiteList.objects.filter(user__isnull=True).count(),
# per user
for user in User.objects.all():
self.check_one_case(user)
# global, last because it takes a lot of time
self.check_one_case(None)
def check_one_case(self, user):
# get proper sets
reports = ReportBadIP.objects.filter(server__user=user).distinct().order_by('updated')
if user is None:
whitelist = IPWhiteList.objects.filter(user__isnull=True)
info = "General, checking %d reports against general WhiteList (%%d items)"
else:
whitelist = user.ipwhitelist_set
info = "User %s, checking %%d reports against both user WhiteList (%%d items) and global one." % user
if reports.count()*whitelist.count()==0: return # optimization :-)
self.stdout.write(info % (
reports.count(),
whitelist.count(),
))
for report in ReportBadIP.objects.all():
for report in reports:
# Check
ipwl = IPWhiteList.check_against(ip_address(report.badip.ip))
ipwl = IPWhiteList.check_against(ip_address(report.badip.ip), user)
if not ipwl: continue
# Display
self.stdout.write(self.style.WARNING('Report %d (ip=%s, reason=%s, updated=%s) fells in white list %d (%s)' % (
self.stdout.write(self.style.WARNING('Report %d (ip=%s, reason=%s, updated=%s) fells in WhiteList %d (%s)' % (
report.id,
str(report.badip),
str(report.reason),
......@@ -53,34 +70,5 @@ class Command(BaseCommand):
self.stdout.write("\t%s%s" % (baseurl, reverse("info_ip", args=[report.badip.ip,])))
self.stdout.write("\t%s%s" % (baseurl, reverse("admin:main_ipwhitelist_change", args=[ipwl.id])))
def check_ip_users(self):
for user in User.objects.all():
# we dont user user.user_ip_reports, it includes the global
reports = ReportBadIP.objects.filter(server__user=user).distinct().order_by('updated')
whitelist = user.ipwhitelist_set
if reports.count()*whitelist.count()==0: continue
self.stdout.write("User %s, checking %d reports against user white list (%d items)" % (
str(user),
reports.count(),
whitelist.count(),
))
for report in reports:
# Check
ipwl = IPWhiteList.check_against(ip_address(report.badip.ip), user)
if not ipwl: continue
# Display
self.stdout.write(self.style.WARNING('Report %d (ip=%s, reason=%s, updated=%s) fells in white list %d (%s)' % (
report.id,
str(report.badip),
str(report.reason),
str(report.updated),
ipwl.id,
str(ipwl.network),
)))
self.stdout.write("\t%s%s" % (baseurl, reverse("info_ip", args=[report.badip.ip,])))
self.stdout.write("\t%s%s" % (baseurl, reverse("admin:main_ipwhitelist_change", args=[ipwl.id])))
# vim: ai ts=4 sts=4 et sw=4
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment