Resolve HOST_* to *_ADDRESS only if *_ADDRESS is not already set

This commit is contained in:
Michael Wyraz
2019-08-23 08:43:59 +02:00
parent 1f0b45e47b
commit de2f166bd1
12 changed files with 67 additions and 36 deletions

View File

@@ -3,6 +3,9 @@ from flask import current_app as app
import re
import urllib
import ipaddress
import socket
import tenacity
SUPPORTED_AUTH_METHODS = ["none", "plain"]
@@ -88,4 +91,18 @@ def get_server(protocol, authenticated=False):
hostname, port = extract_host_port(app.config['AUTHSMTP_ADDRESS'], 10025)
else:
hostname, port = extract_host_port(app.config['SMTP_ADDRESS'], 25)
try:
# test if hostname is already resolved to an ip adddress
ipaddress.ip_address(hostname)
except:
# hostname is not an ip address - so we need to resolve it
hostname = resolve_hostname(hostname)
return hostname, port
@tenacity.retry(stop=tenacity.stop_after_attempt(100),
wait=tenacity.wait_random(min=2, max=5))
def resolve_hostname(hostname):
""" This function uses system DNS to resolve a hostname.
It is capable of retrying in case the host is not immediately available
"""
return socket.gethostbyname(hostname)