From a4460c0931f80ed878c2ad28558e1b9bd0a9ae44 Mon Sep 17 00:00:00 2001 From: Russell Jones Date: Mon, 18 Nov 2013 11:33:15 -0800 Subject: [PATCH] If DNS can't resolve the first time, try again. Also cleaned up code for mail exchanger lookup. --- flanker/addresslib/address.py | 8 +-- flanker/addresslib/validate.py | 84 ++++++++++++++++-------- setup.py | 2 +- tests/addresslib/metrics_test.py | 8 +-- tests/addresslib/plugins/aol_test.py | 2 +- tests/addresslib/plugins/gmail_test.py | 2 +- tests/addresslib/plugins/google_test.py | 2 +- tests/addresslib/plugins/hotmail_test.py | 2 +- tests/addresslib/plugins/icloud_test.py | 2 +- tests/addresslib/plugins/yahoo_test.py | 2 +- tests/addresslib/validator_test.py | 15 ++--- 11 files changed, 77 insertions(+), 52 deletions(-) diff --git a/flanker/addresslib/address.py b/flanker/addresslib/address.py index 913757f..6f30e43 100644 --- a/flanker/addresslib/address.py +++ b/flanker/addresslib/address.py @@ -192,12 +192,12 @@ def validate_address(addr_spec, metrics=False): return None, mtimes # lookup if this domain has a mail exchanger - has_exchanger, exchanger, mx_metrics = \ + exchanger, mx_metrics = \ flanker.addresslib.validate.mail_exchanger_lookup(addr_parts[-1], metrics=True) mtimes['mx_lookup'] = mx_metrics['mx_lookup'] mtimes['dns_lookup'] = mx_metrics['dns_lookup'] mtimes['mx_conn'] = mx_metrics['mx_conn'] - if has_exchanger is False: + if exchanger is None: return None, mtimes # lookup custom local-part grammar if it exists @@ -249,13 +249,13 @@ def validate_list(addr_list, as_tuple=False, metrics=False): for paddr in parsed_addresses: # lookup if this domain has a mail exchanger - has_exchanger, exchanger, mx_metrics = \ + exchanger, mx_metrics = \ flanker.addresslib.validate.mail_exchanger_lookup(paddr.hostname, metrics=True) mtimes['mx_lookup'] += mx_metrics['mx_lookup'] mtimes['dns_lookup'] += mx_metrics['dns_lookup'] mtimes['mx_conn'] += mx_metrics['mx_conn'] - if has_exchanger is False: + if exchanger is None: ulist.append(paddr.full_spec()) continue diff --git a/flanker/addresslib/validate.py b/flanker/addresslib/validate.py index 1b30081..da74d18 100644 --- a/flanker/addresslib/validate.py +++ b/flanker/addresslib/validate.py @@ -117,13 +117,65 @@ def mail_exchanger_lookup(domain, metrics=False): Looks up the mail exchanger for a domain. If MX records exist they will be returned, if not it will attempt to fallback to A records, if neither exist None will be returned. + ''' + mtimes = {'mx_lookup': 0, 'dns_lookup': 0, 'mx_conn': 0} + mx_cache = flanker.addresslib.mx_cache + + # look in cache + bstart = time.time() + in_cache, cache_value = lookup_exchanger_in_cache(domain) + mtimes['mx_lookup'] = time.time() - bstart + if in_cache: + return cache_value, mtimes + # dns lookup on domain + bstart = time.time() + mx_hosts = lookup_domain(domain) + mtimes['dns_lookup'] = time.time() - bstart + if mx_hosts is None: + # try one more time + bstart = time.time() + mx_hosts = lookup_domain(domain) + mtimes['dns_lookup'] += time.time() - bstart + if mx_hosts is None: + mx_cache[domain] = False + return None, mtimes + + # test connecting to the mx exchanger + bstart = time.time() + mail_exchanger = connect_to_mail_exchanger(mx_hosts) + mtimes['mx_conn'] = time.time() - bstart + if mail_exchanger is None: + mx_cache[domain] = False + return None, mtimes + + # valid mx records, connected to mail exchanger, return True + mx_cache[domain] = mail_exchanger + return mail_exchanger, mtimes + + +def lookup_exchanger_in_cache(domain): + ''' Uses a cache to store the results of the mail exchanger lookup to speed up lookup times. The default is redis, but this can be overidden by your own cache as long as it conforms to the same interface as that of a dict. See the implimentation of the redis cache in the flanker.addresslib.driver package for more details if you wish to implement your own cache. + ''' + mx_cache = flanker.addresslib.mx_cache + + lookup = mx_cache[domain] + if lookup is None: + return (False, None) + if lookup == 'False': + return (True, None) + else: + return (True, lookup) + + +def lookup_domain(domain): + ''' The dnspython package is used for dns lookups. The dnspython package uses the dns server specified by your operating system. Just like the cache, this can be overridden by your own dns lookup method of choice as long @@ -131,41 +183,15 @@ def mail_exchanger_lookup(domain, metrics=False): implimentation of the dnspython lookup in the flanker.addresslib.driver package for more details. ''' - mtimes = {'mx_lookup': 0, 'dns_lookup': 0, 'mx_conn': 0} - mx_cache = flanker.addresslib.mx_cache dns_lookup = flanker.addresslib.dns_lookup - # if we have the mx lookup cached, return the cached result - bstart = time.time() - lookup = mx_cache[domain] - mtimes['mx_lookup'] = time.time() - bstart - if lookup is not None: - if lookup == 'False': - return (False, None, mtimes) - else: - return (True, lookup, mtimes) - - # check if mx records exist fqdn = domain if domain[-1] == '.' else ''.join([domain, '.']) - bstart = time.time() mx_hosts = dns_lookup[fqdn] - mtimes['dns_lookup'] = time.time() - bstart - if len(mx_hosts) == 0: - mx_cache[domain] = False - return (False, None, mtimes) - # check if we can connect to the mail exchanger - bstart = time.time() - mail_exchanger = connect_to_mail_exchanger(mx_hosts) - mtimes['mx_conn'] = time.time() - bstart - if mail_exchanger is None: - mx_cache[domain] = False - return (False, None, mtimes) - - # valid mx records, connected to mail exchanger, return True - mx_cache[domain] = mail_exchanger - return (True, mail_exchanger, mtimes) + if len(mx_hosts) == 0: + return None + return mx_hosts def connect_to_mail_exchanger(mx_hosts): diff --git a/setup.py b/setup.py index 5a20269..07e5f69 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ from setuptools import setup, find_packages setup(name='flanker', - version="0.3.1", + version="0.3.2", description="Mailgun Parsing Tools", long_description='', classifiers=[], diff --git a/tests/addresslib/metrics_test.py b/tests/addresslib/metrics_test.py index 00e5db2..92ddd2a 100644 --- a/tests/addresslib/metrics_test.py +++ b/tests/addresslib/metrics_test.py @@ -12,14 +12,14 @@ def mock_exchanger_lookup(arg, metrics=False): mtimes = {'mx_lookup': 10, 'dns_lookup': 20, 'mx_conn': 30} if metrics is True: if arg == 'ai' or arg == 'mailgun.org' or arg == 'fakecompany.mailgun.org': - return (True, '', mtimes) + return ('', mtimes) else: - return (False, '', mtimes) + return (None, mtimes) else: if arg == 'ai' or arg == 'mailgun.org' or arg == 'fakecompany.mailgun.org': - return (True, '') + return '' else: - return (False, '') + return None def test_metrics_parse(): diff --git a/tests/addresslib/plugins/aol_test.py b/tests/addresslib/plugins/aol_test.py index ce3e0e6..634602d 100644 --- a/tests/addresslib/plugins/aol_test.py +++ b/tests/addresslib/plugins/aol_test.py @@ -18,7 +18,7 @@ SAMPLE_MX = 'sample.mx.aol.com' @nottest def mock_exchanger_lookup(arg, metrics=False): mtimes = {'mx_lookup': 0, 'dns_lookup': 0, 'mx_conn': 0} - return (True, SAMPLE_MX, mtimes) + return (SAMPLE_MX, mtimes) def test_exchanger_lookup(): diff --git a/tests/addresslib/plugins/gmail_test.py b/tests/addresslib/plugins/gmail_test.py index dd80057..2aa0d92 100644 --- a/tests/addresslib/plugins/gmail_test.py +++ b/tests/addresslib/plugins/gmail_test.py @@ -19,7 +19,7 @@ ATOM_STR = string.ascii_letters + string.digits + '!#$%&\'*+-/=?^_`{|}~' @nottest def mock_exchanger_lookup(arg, metrics=False): mtimes = {'mx_lookup': 0, 'dns_lookup': 0, 'mx_conn': 0} - return (True, SAMPLE_MX, mtimes) + return (SAMPLE_MX, mtimes) def test_exchanger_lookup(): diff --git a/tests/addresslib/plugins/google_test.py b/tests/addresslib/plugins/google_test.py index bb8184b..260c937 100644 --- a/tests/addresslib/plugins/google_test.py +++ b/tests/addresslib/plugins/google_test.py @@ -19,7 +19,7 @@ ATOM_STR = string.ascii_letters + string.digits + '!#$%&\'*+-/=?^_`{|}~' @nottest def mock_exchanger_lookup(arg, metrics=False): mtimes = {'mx_lookup': 0, 'dns_lookup': 0, 'mx_conn': 0} - return (True, SAMPLE_MX, mtimes) + return (SAMPLE_MX, mtimes) def test_exchanger_lookup(): diff --git a/tests/addresslib/plugins/hotmail_test.py b/tests/addresslib/plugins/hotmail_test.py index 2edf726..f12dd7d 100644 --- a/tests/addresslib/plugins/hotmail_test.py +++ b/tests/addresslib/plugins/hotmail_test.py @@ -18,7 +18,7 @@ SAMPLE_MX = 'mx0.hotmail.com' @nottest def mock_exchanger_lookup(arg, metrics=False): mtimes = {'mx_lookup': 0, 'dns_lookup': 0, 'mx_conn': 0} - return (True, SAMPLE_MX, mtimes) + return (SAMPLE_MX, mtimes) def test_exchanger_lookup(): diff --git a/tests/addresslib/plugins/icloud_test.py b/tests/addresslib/plugins/icloud_test.py index bc7e886..8588d02 100644 --- a/tests/addresslib/plugins/icloud_test.py +++ b/tests/addresslib/plugins/icloud_test.py @@ -18,7 +18,7 @@ SAMPLE_MX = 'sample.icloud.com.akadns.net' @nottest def mock_exchanger_lookup(arg, metrics=False): mtimes = {'mx_lookup': 0, 'dns_lookup': 0, 'mx_conn': 0} - return (True, SAMPLE_MX, mtimes) + return (SAMPLE_MX, mtimes) def test_exchanger_lookup(): diff --git a/tests/addresslib/plugins/yahoo_test.py b/tests/addresslib/plugins/yahoo_test.py index 6e1fd2c..3582c5f 100644 --- a/tests/addresslib/plugins/yahoo_test.py +++ b/tests/addresslib/plugins/yahoo_test.py @@ -18,7 +18,7 @@ SAMPLE_MX = 'sample.yahoodns.net' @nottest def mock_exchanger_lookup(arg, metrics=False): mtimes = {'mx_lookup': 0, 'dns_lookup': 0, 'mx_conn': 0} - return (True, SAMPLE_MX, mtimes) + return (SAMPLE_MX, mtimes) def test_exchanger_lookup(): diff --git a/tests/addresslib/validator_test.py b/tests/addresslib/validator_test.py index e44dd4a..9e64dc7 100644 --- a/tests/addresslib/validator_test.py +++ b/tests/addresslib/validator_test.py @@ -61,15 +61,14 @@ def mock_exchanger_lookup(arg, metrics=False): mtimes = {'mx_lookup': 10, 'dns_lookup': 20, 'mx_conn': 30} if metrics is True: if arg == 'ai' or arg == 'mailgun.org' or arg == 'fakecompany.mailgun.org': - return (True, '', mtimes) + return ('', mtimes) else: - return (False, '', mtimes) + return (None, mtimes) else: if arg == 'ai' or arg == 'mailgun.org' or arg == 'fakecompany.mailgun.org': - return (True, '') + return '' else: - return (False, '') - + return None def test_abridged_mailbox_valid_set(): for line in ABRIDGED_LOCALPART_VALID_TESTS.split('\n'): @@ -225,14 +224,14 @@ def test_mx_lookup_metrics(): with patch.object(validate, 'mail_exchanger_lookup') as mock_method: mock_method.side_effect = mock_exchanger_lookup - a, b, metrics = validate.mail_exchanger_lookup('example.com', metrics=True) + a, metrics = validate.mail_exchanger_lookup('example.com', metrics=True) assert_equal(metrics['mx_lookup'], 10) assert_equal(metrics['dns_lookup'], 20) assert_equal(metrics['mx_conn'], 30) # ensure values are unpacked correctly - a, b = validate.mail_exchanger_lookup('example.com', metrics=False) - a, b = validate.mail_exchanger_lookup('example.com', metrics=False) + a = validate.mail_exchanger_lookup('example.com', metrics=False) + a = validate.mail_exchanger_lookup('example.com', metrics=False) def test_validate_address_metrics(): with patch.object(validate, 'mail_exchanger_lookup') as mock_method: -- 2.43.0