From c7ed4f7ac13155f9fd2a37111fbebf203c023d14 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Mon, 14 Feb 2022 22:42:46 +0100 Subject: [PATCH] events: check mtime on geoip database Signed-off-by: Jens Langhammer --- authentik/events/geo.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/authentik/events/geo.py b/authentik/events/geo.py index cc6f5c764..55296a998 100644 --- a/authentik/events/geo.py +++ b/authentik/events/geo.py @@ -46,14 +46,18 @@ class GeoIPReader: LOGGER.warning("Failed to load GeoIP database", exc=exc) def __check_expired(self): - """Check if the geoip database has been opened longer than 8 hours, - and re-open it, as it will probably will have been re-downloaded""" - now = time() - diff = datetime.fromtimestamp(now) - datetime.fromtimestamp(self.__last_mtime) - diff_hours = diff.total_seconds() // 3600 - if diff_hours >= 8: - LOGGER.info("GeoIP databased loaded too long, re-opening", diff=diff) - self.__open() + """Check if the modification date of the GeoIP database has + changed, and reload it if so""" + path = CONFIG.y("geoip") + try: + mtime = stat(path).st_mtime + diff = self.__last_mtime < mtime + if diff > 0: + LOGGER.info("Found new GeoIP Database, reopening", diff=diff) + self.__open() + except OSError as exc: + LOGGER.warning("Failed to check GeoIP age", exc=exc) + return @property def enabled(self) -> bool: