assume IP info is the same for any /24 subnet

This commit is contained in:
Connor Olding 2020-09-03 12:57:05 +02:00
parent 0603081c49
commit 3a3c1340a9

View file

@ -1,5 +1,4 @@
from collections import namedtuple from collections import namedtuple
from sys import stderr
from time import time from time import time
CacheLine = namedtuple("CacheLine", ("time", "code")) CacheLine = namedtuple("CacheLine", ("time", "code"))
@ -7,6 +6,11 @@ CacheLine = namedtuple("CacheLine", ("time", "code"))
one_month = 365.25 / 12 * 24 * 60 * 60 # in seconds 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: class IpInfoBase:
pass pass
@ -172,7 +176,8 @@ class IpInfoByIpApi(IpInfoBase):
continue continue
ip, time, code = row[0], float(row[1]), row[2] ip, time, code = row[0], float(row[1]), row[2]
info = CacheLine(time, code) info = CacheLine(time, code)
self.stored[ip] = info masked = mask_ip(ip)
self.stored[masked] = info
def flush(self): def flush(self):
from csv import writer from csv import writer
@ -183,7 +188,8 @@ class IpInfoByIpApi(IpInfoBase):
handle.writerow(self.csv_header) handle.writerow(self.csv_header)
for ip, info in self.stored.items(): for ip, info in self.stored.items():
timestr = "{:.2f}".format(info.time) 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): def cache(self, ip, info=None, timestamp=None):
if self.stored is None: if self.stored is None:
@ -191,8 +197,10 @@ class IpInfoByIpApi(IpInfoBase):
now = time() if timestamp is None else timestamp now = time() if timestamp is None else timestamp
masked = mask_ip(ip)
if info is None: if info is None:
cached = self.stored.get(ip, None) cached = self.stored.get(masked, None)
if cached is None: if cached is None:
return None return None
if now > cached.time + self.expiry: if now > cached.time + self.expiry:
@ -200,7 +208,7 @@ class IpInfoByIpApi(IpInfoBase):
return cached return cached
else: else:
assert isinstance(info, CacheLine), type(info) assert isinstance(info, CacheLine), type(info)
self.stored[ip] = info self.stored[masked] = info
async def find_country(self, ip, db=None): async def find_country(self, ip, db=None):
now = time() now = time()