def __init__(self): BaseBackend.__init__(self, models.HistoryListing) self.netdata = NetData() self.ipcalc = IPCalc()
class Peering(BaseBackend): name = "Peering" def __init__(self): BaseBackend.__init__(self, models.Peering) self.config = Config() self.encryption = Encryption(self.config["blacklist.keystore.psk"]) self.ipcalc = IPCalc() self.netdata = NetData() #@transaction.commit_manually def bulk_import(self, import_data): t_start = mktime(datetime.now().timetuple()) data = [] failed = [] num_failed = 0 for entry in import_data: if entry[0] == "": num_failed += 1 failed.append(entry) continue tmp = {} try: user = User.objects.get(username=entry[3]) except User.DoesNotExist: user = User(username=entry[3]) user.save() try: asnum = models.ASNum.objects.get(asnum=entry[1]) except models.ASNum.DoesNotExist: ## TODO: fix unknown RIR asnum = models.ASNum( asnum=entry[1], rir=models.RIR.objects.get(name="RIPE") ) asnum.save() (asn, asn_name, whois_data, net, cc) = self.netdata.get_whois_data(entry[0]) try: country = models.Country.objects.get(code=cc) except models.Country.DoesNotExist: country = models.Country(code=cc, name=cc) country.save() try: asnum = models.ASNum.objects.get(asnum=asn) except models.ASNum.DoesNotExist: asnum = models.ASNum( asnum=asnum, name=asn_name, whois_data=whois_data, country=country, ) asnum.save() (n, m) = net.split("/") try: subnet = models.Subnet.objects.get(subnet=self.ipcalc.dqtoi(n)) except models.Subnet.DoesNotExist: subnet = models.Subnet( subnet=self.ipcalc.dqtoi(n), mask=m, name=asn_name, asnum=asnum, ) subnet.save() try: ip = models.IP.objects.get(ip=self.ipcalc.dqtoi(entry[0])) except models.IP.DoesNotExist: ip = models.IP( ip=self.ipcalc.dqtoi(entry[0]), subnet=subnet, ) ip.save() try: peer = models.Host.objects.get(hostname=entry[2]) except models.Host.DoesNotExist: peer = models.Host( hostname=entry[2], ip=ip, owner=user, ) peer.save() try: key = models.Key.objects.get(name="%s-bgp" % (entry[2])) except models.Key.DoesNotExist: key = models.Key( name="%s-bgp" % (entry[2]), data=self.encryption.encrypt(entry[4]) ) key.save() tmp["peer"] = peer tmp["asnum"] = asnum tmp["owner"] = user tmp["key"] = key data.append(tmp) for entry in data: peering = models.Peering( peer=entry["peer"], asnum=entry["asnum"], user=entry["owner"], key=entry["key"] ) peering.save() #transaction.commit() #reset_queries() t_end = mktime(datetime.now().timetuple()) return (t_end - t_start, num_failed, failed)
def __init__(self): BaseBackend.__init__(self, models.Peering) self.config = Config() self.encryption = Encryption(self.config["blacklist.keystore.psk"]) self.ipcalc = IPCalc() self.netdata = NetData()
class Peering(BaseBackend): name = "Peering" def __init__(self): BaseBackend.__init__(self, models.Peering) self.config = Config() self.encryption = Encryption(self.config["blacklist.keystore.psk"]) self.ipcalc = IPCalc() self.netdata = NetData() #@transaction.commit_manually def bulk_import(self, import_data): t_start = mktime(datetime.now().timetuple()) data = [] failed = [] num_failed = 0 for entry in import_data: if entry[0] == "": num_failed += 1 failed.append(entry) continue tmp = {} try: user = User.objects.get(username=entry[3]) except User.DoesNotExist: user = User(username=entry[3]) user.save() try: asnum = models.ASNum.objects.get(asnum=entry[1]) except models.ASNum.DoesNotExist: ## TODO: fix unknown RIR asnum = models.ASNum(asnum=entry[1], rir=models.RIR.objects.get(name="RIPE")) asnum.save() (asn, asn_name, whois_data, net, cc) = self.netdata.get_whois_data(entry[0]) try: country = models.Country.objects.get(code=cc) except models.Country.DoesNotExist: country = models.Country(code=cc, name=cc) country.save() try: asnum = models.ASNum.objects.get(asnum=asn) except models.ASNum.DoesNotExist: asnum = models.ASNum( asnum=asnum, name=asn_name, whois_data=whois_data, country=country, ) asnum.save() (n, m) = net.split("/") try: subnet = models.Subnet.objects.get(subnet=self.ipcalc.dqtoi(n)) except models.Subnet.DoesNotExist: subnet = models.Subnet( subnet=self.ipcalc.dqtoi(n), mask=m, name=asn_name, asnum=asnum, ) subnet.save() try: ip = models.IP.objects.get(ip=self.ipcalc.dqtoi(entry[0])) except models.IP.DoesNotExist: ip = models.IP( ip=self.ipcalc.dqtoi(entry[0]), subnet=subnet, ) ip.save() try: peer = models.Host.objects.get(hostname=entry[2]) except models.Host.DoesNotExist: peer = models.Host( hostname=entry[2], ip=ip, owner=user, ) peer.save() try: key = models.Key.objects.get(name="%s-bgp" % (entry[2])) except models.Key.DoesNotExist: key = models.Key(name="%s-bgp" % (entry[2]), data=self.encryption.encrypt(entry[4])) key.save() tmp["peer"] = peer tmp["asnum"] = asnum tmp["owner"] = user tmp["key"] = key data.append(tmp) for entry in data: peering = models.Peering(peer=entry["peer"], asnum=entry["asnum"], user=entry["owner"], key=entry["key"]) peering.save() #transaction.commit() #reset_queries() t_end = mktime(datetime.now().timetuple()) return (t_end - t_start, num_failed, failed)
def __init__(self): BaseBackend.__init__(self, models.IP) self.netdata = NetData() self.ipcalc = IPCalc() self.subnet = Subnet()
class IP(BaseBackend): name = "IP" def __init__(self): BaseBackend.__init__(self, models.IP) self.netdata = NetData() self.ipcalc = IPCalc() self.subnet = Subnet() def has(self, *args, **kwargs): (dq, mask) = self.ipcalc.parse_addr(kwargs['ip']) ip = self.ipcalc.dqtoi(dq) return BaseBackend.has(self, ip=ip, mask=mask) def get(self, *args, **kwargs): (dq, mask) = self.ipcalc.parse_addr(kwargs['ip']) ip = self.ipcalc.dqtoi(dq) return BaseBackend.get(self, *args, ip=ip) def add(self, *args, **kwargs): af = self.ipcalc.af(kwargs['ip']) if not af in [4, 6]: return (False, "No AF found for %s" % (ip)) if "/" in kwargs['ip']: (ip, mask) = kwargs['ip'].split('/') first = self.ipcalc.dqtoi(ip) last = first + self.ipcalc.size(kwargs['ip']) else: ip = kwargs['ip'] if af == 4: mask = 32 elif af == 6: mask = 128 first = self.ipcalc.dqtoi(ip) last = first if self.has(ip=ip, mask=mask): return (True, self.get(ip=ip, mask=mask)) try: (asn, asn_name, whois_data, net, cc) = self.netdata.get_whois_data(kwargs["ip"]) except ValueError: return (False, "Cannot fetch whois data") if net == "0.0.0.0/0": net = None else: if not self.subnet.has(subnet=net): (result, net) = self.subnet.add(subnet=net, asn=asn, asn_name=asn_name, whois_data=whois_data, cc=cc) if not result: return (False, net) else: (result, net) = self.subnet.get(subnet=net) if not result: return (False, net) ip = models.IP(ip=first, mask=mask, last=last, subnet=net, af=af) ip.save() return (True, ip)
class IP(BaseBackend): name = "IP" def __init__(self): BaseBackend.__init__(self, models.IP) self.netdata = NetData() self.ipcalc = IPCalc() self.subnet = Subnet() def has(self, *args, **kwargs): (dq, mask) = self.ipcalc.parse_addr(kwargs['ip']) ip = self.ipcalc.dqtoi(dq) return BaseBackend.has(self, ip=ip, mask=mask) def get(self, *args, **kwargs): (dq , mask) = self.ipcalc.parse_addr(kwargs['ip']) ip = self.ipcalc.dqtoi(dq) return BaseBackend.get(self, *args, ip=ip) def add(self, *args, **kwargs): af = self.ipcalc.af(kwargs['ip']) if not af in [4,6]: return (False, "No AF found for %s" % (ip)) if "/" in kwargs['ip']: (ip, mask) = kwargs['ip'].split('/') first = self.ipcalc.dqtoi(ip) last=first + self.ipcalc.size(kwargs['ip']) else: ip = kwargs['ip'] if af == 4: mask = 32 elif af == 6: mask = 128 first = self.ipcalc.dqtoi(ip) last = first if self.has(ip=ip, mask=mask): return (True, self.get(ip=ip, mask=mask)) try: (asn, asn_name, whois_data, net, cc) = self.netdata.get_whois_data(kwargs["ip"]) except ValueError: return (False, "Cannot fetch whois data") if net == "0.0.0.0/0": net = None else: if not self.subnet.has(subnet=net): (result, net) = self.subnet.add(subnet=net, asn=asn, asn_name=asn_name, whois_data=whois_data, cc=cc) if not result: return (False, net) else: (result, net) = self.subnet.get(subnet=net) if not result: return (False, net) ip = models.IP( ip=first, mask=mask, last=last, subnet=net, af=af ) ip.save() return (True, ip)
["APNIC", "whois.lacnic.net"], ["LACNIC", "whois.lacnic.net"], ["AfriNIC", "whois.afrinic.net"], ["Unknown", "localhost"], ] error_dir = "%DJANGO_ROOT%/scripts/registry-mgmt/errors" cache_dir = "%DJANGO_ROOT%/scripts/registry-mgmt/cache" asn_name_cache = {} asn_name_cache_file = "%s/asn_name.txt" % (cache_dir) subnet_asnum_cache = {} subnet_asnum_cache_file = "%s/subnet_asnum.txt" % (cache_dir) country_rir_cache = {} ipcalc = IPCalc() netdata = NetData() def prepare_cache(): if not asn_name_cache and os.path.exists(asn_name_cache_file): for line in open(asn_name_cache_file, "r").readlines(): (asnum, name) = line.split("||") asn_name_cache[asnum] = name.strip() if not subnet_asnum_cache and os.path.exists(subnet_asnum_cache_file): for line in open(subnet_asnum_cache_file, "r").readlines(): (net, asnum) = line.split("||") asnum = asnum.strip() if asnum == "-1": subnet_asnum_cache[net] = None else: