From 2e2e47b202eb9559025571a8832d9258e02c87f6 Mon Sep 17 00:00:00 2001 From: Sean Whalen Date: Fri, 15 Feb 2019 19:14:08 -0500 Subject: [PATCH] 6.1.2 - Use local Public Suffix List file instead of downloading it --- CHANGELOG.md | 5 +++++ parsedmarc/__init__.py | 2 +- parsedmarc/utils.py | 42 ++++++++++++++++++++---------------------- setup.py | 2 +- 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d8e4c6..3fcf667 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +6.1.2 +----- + +- Use local Public Suffix List file instead of downloading it + 6.1.1 ----- diff --git a/parsedmarc/__init__.py b/parsedmarc/__init__.py index b6d0731..92b97ba 100644 --- a/parsedmarc/__init__.py +++ b/parsedmarc/__init__.py @@ -38,7 +38,7 @@ from parsedmarc.utils import is_outlook_msg, convert_outlook_msg from parsedmarc.utils import timestamp_to_human, human_timestamp_to_datetime from parsedmarc.utils import parse_email -__version__ = "6.1.1" +__version__ = "6.1.2" logging.basicConfig( format='%(levelname)8s:%(filename)s:%(lineno)d:' diff --git a/parsedmarc/utils.py b/parsedmarc/utils.py index adcfd51..1ef8ec1 100644 --- a/parsedmarc/utils.py +++ b/parsedmarc/utils.py @@ -26,12 +26,9 @@ import geoip2.errors import requests import publicsuffix2 -__version__ = "6.1.1" - -USER_AGENT = "Mozilla/5.0 ((0 {1})) parsedmarc/{2}".format( +USER_AGENT = "Mozilla/5.0 ((0 {1})) parsedmarc".format( platform.system(), platform.release(), - __version__ ) @@ -70,7 +67,7 @@ def decode_base64(data): return base64.b64decode(data) -def get_base_domain(domain): +def get_base_domain(domain, use_fresh_psl=False): """ Gets the base domain name for the given domain @@ -78,11 +75,9 @@ def get_base_domain(domain): Results are based on a list of public domain suffixes at https://publicsuffix.org/list/public_suffix_list.dat. - This file is saved to the current working directory, - where it is used as a cache file for 24 hours. - Args: domain (str): A domain or subdomain + use_fresh_psl (bool): Download a fresh Public Suffix List Returns: str: The base domain of the given domain @@ -98,21 +93,24 @@ def get_base_domain(domain): with open(psl_path, "w", encoding="utf-8") as fresh_psl_file: fresh_psl_file.write(fresh_psl) - if not os.path.exists(psl_path): - download_psl() - else: - psl_age = datetime.now() - datetime.fromtimestamp( - os.stat(psl_path).st_mtime) - if psl_age > timedelta(hours=24): - try: - download_psl() - except Exception as error: - logger.warning( - "Failed to download an updated PSL {0}".format(error)) - with open(psl_path, encoding="utf-8") as psl_file: - psl = publicsuffix2.PublicSuffixList(psl_file) + if use_fresh_psl: + if not os.path.exists(psl_path): + download_psl() + else: + psl_age = datetime.now() - datetime.fromtimestamp( + os.stat(psl_path).st_mtime) + if psl_age > timedelta(hours=24): + try: + download_psl() + except Exception as error: + logger.warning( + "Failed to download an updated PSL {0}".format(error)) + with open(psl_path, encoding="utf-8") as psl_file: + psl = publicsuffix2.PublicSuffixList(psl_file) - return psl.get_public_suffix(domain) + return psl.get_public_suffix(domain) + else: + return publicsuffix2.get_public_suffix(domain) def query_dns(domain, record_type, cache=None, nameservers=None, timeout=2.0): diff --git a/setup.py b/setup.py index 696a0b2..8db06be 100644 --- a/setup.py +++ b/setup.py @@ -14,7 +14,7 @@ from setuptools import setup from codecs import open from os import path -__version__ = "6.1.1" +__version__ = "6.1.2" description = "A Python package and CLI for parsing aggregate and " \ "forensic DMARC reports"