Commit 12fd0f14 authored by Thomas Capricelli's avatar Thomas Capricelli
Browse files

new importer, for "IPsum" aggregated list

parent 20d829d54d35
# -*- coding: utf-8
# vim: set fileencoding=utf-8
# system
from ipaddress import ip_address
import requests
# django
from import BaseCommand, CommandError
# projet
from main.models import Reason, Server, IPWhiteList, ReportBadIP
feed_url = ""
# We only import those with such a score, at least
class Command(BaseCommand):
help = """
Aggregated feed on github.
def handle(self, *args, **options):
self.reason = Reason.objects.get(name="Imported")
self.server, _ = Server.objects.get_or_create(name="IPsum", user=None)
# Fetch data
# don't use stream=True: the file is not that big, but we are very slow to process it
# and the connection will break/timeout
r = requests.get(feed_url)
for line in r.iter_lines():
line = line.decode('ascii')
# ignore comments, blank lines
if line.startswith("#") or len(line)==0: continue
# Format, the ip appears in <number> different sources/feeds
ip, number = line.split()
if number<MINIMUM_NUMBER: break
def addoneip(self, ip):
if IPWhiteList.check_against(ip_address(ip)):
self.stdout.write('%s: IP whitelisted, no report created' % ip))
# Report ips
if ReportBadIP.create_or_update_report(self.server, ip, self.reason):
self.stdout.write('%s: new report created' % ip))
self.stdout.write('%s: report updated' % ip))
# 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