Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a parameter to avoid to connection to mx #114

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions flanker/addresslib/address.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ def parse_list(address_list, strict=False, as_tuple=False, metrics=False):


@metrics_wrapper()
def validate_address(addr_spec, metrics=False, skip_remote_checks=False):
def validate_address(addr_spec, metrics=False, skip_remote_checks=False, mx_lookup=True):

"""
Given an addr-spec, runs the pre-parser, the parser, DNS MX checks,
MX existence checks, and if available, ESP specific grammar for the
Expand Down Expand Up @@ -305,11 +306,20 @@ def validate_address(addr_spec, metrics=False, skip_remote_checks=False):
_log.debug('failed tld check for %s', addr_spec)
return None, mtimes

# Here, we can set an option to discover MX from DNS, but not connect
# it is more fast

if skip_remote_checks:
return paddr, mtimes

connect_to_mx = True
if not mx_lookup:
connect_to_mx = False

# lookup if this domain has a mail exchanger
exchanger, mx_metrics = mail_exchanger_lookup(paddr.hostname, metrics=True)
exchanger, mx_metrics = \
flanker.addresslib.validate.mail_exchanger_lookup(paddr.hostname, metrics=True, connect_to_mx=connect_to_mx)

mtimes['mx_lookup'] = mx_metrics['mx_lookup']
mtimes['dns_lookup'] = mx_metrics['dns_lookup']
mtimes['mx_conn'] = mx_metrics['mx_conn']
Expand Down
20 changes: 12 additions & 8 deletions flanker/addresslib/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def plugin_for_esp(mail_exchanger):


@metrics_wrapper()
def mail_exchanger_lookup(domain, metrics=False):
def mail_exchanger_lookup(domain, metrics=False, connect_to_mx=True):
"""
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
Expand Down Expand Up @@ -164,13 +164,17 @@ def mail_exchanger_lookup(domain, metrics=False):
log.warning('failed mx lookup for %s', domain)
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:
log.warning('failed mx connection for %s/%s', domain, mx_hosts)
return None, mtimes

if connect_to_mx:
# 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:
log.warning('failed mx connection for %s/%s', domain, mx_hosts)
return None, mtimes
else:
mail_exchanger = mx_hosts[0]

# valid mx records, connected to mail exchanger, return True
_get_mx_cache()[domain] = mail_exchanger
Expand Down