From 3a3c1340a92a2e46fc33aaeaf0d1b1f2f2088594 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Thu, 3 Sep 2020 12:57:05 +0200 Subject: [PATCH] assume IP info is the same for any /24 subnet --- respodns/ip_info.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/respodns/ip_info.py b/respodns/ip_info.py index ec1a380..8c08fdb 100644 --- a/respodns/ip_info.py +++ b/respodns/ip_info.py @@ -1,5 +1,4 @@ from collections import namedtuple -from sys import stderr from time import time CacheLine = namedtuple("CacheLine", ("time", "code")) @@ -7,6 +6,11 @@ CacheLine = namedtuple("CacheLine", ("time", "code")) one_month = 365.25 / 12 * 24 * 60 * 60 # in seconds +def mask_ip(ip): + # this assumes IP info is the same for a /24 subnet. + return ".".join(ip.split(".")[:3]) + ".0" + + class IpInfoBase: pass @@ -172,7 +176,8 @@ class IpInfoByIpApi(IpInfoBase): continue ip, time, code = row[0], float(row[1]), row[2] info = CacheLine(time, code) - self.stored[ip] = info + masked = mask_ip(ip) + self.stored[masked] = info def flush(self): from csv import writer @@ -183,7 +188,8 @@ class IpInfoByIpApi(IpInfoBase): handle.writerow(self.csv_header) for ip, info in self.stored.items(): timestr = "{:.2f}".format(info.time) - handle.writerow([ip, timestr, info.code]) + masked = mask_ip(ip) + handle.writerow([masked, timestr, info.code]) def cache(self, ip, info=None, timestamp=None): if self.stored is None: @@ -191,8 +197,10 @@ class IpInfoByIpApi(IpInfoBase): now = time() if timestamp is None else timestamp + masked = mask_ip(ip) + if info is None: - cached = self.stored.get(ip, None) + cached = self.stored.get(masked, None) if cached is None: return None if now > cached.time + self.expiry: @@ -200,7 +208,7 @@ class IpInfoByIpApi(IpInfoBase): return cached else: assert isinstance(info, CacheLine), type(info) - self.stored[ip] = info + self.stored[masked] = info async def find_country(self, ip, db=None): now = time()