1
0
mirror of https://github.com/mail-in-a-box/mailinabox.git synced 2024-12-25 07:47:05 +00:00

add dns exception handling

This commit is contained in:
KiekerJan 2022-04-17 22:56:30 +02:00
parent 9b252e0209
commit d35b068a73
3 changed files with 24 additions and 10 deletions

View File

@ -274,6 +274,7 @@ def dns_update():
try: try:
return do_dns_update(env, force=request.form.get('force', '') == '1') return do_dns_update(env, force=request.form.get('force', '') == '1')
except Exception as e: except Exception as e:
logging.exception('dns update exc')
return (str(e), 500) return (str(e), 500)
@app.route('/dns/secondary-nameserver') @app.route('/dns/secondary-nameserver')
@ -763,7 +764,7 @@ def log_failed_login(request):
# APP # APP
if __name__ == '__main__': if __name__ == '__main__':
logging_level = logging.INFO logging_level = logging.DEBUG
if "DEBUG" in os.environ: if "DEBUG" in os.environ:
# Turn on Flask debugging. # Turn on Flask debugging.

View File

@ -1065,6 +1065,7 @@ def set_custom_dns_record(qname, rtype, value, action, env):
def get_secondary_dns(custom_dns, mode=None): def get_secondary_dns(custom_dns, mode=None):
resolver = dns.resolver.get_default_resolver() resolver = dns.resolver.get_default_resolver()
resolver.timeout = 10 resolver.timeout = 10
resolver.lifetime = 10
values = [] values = []
for qname, rtype, value in custom_dns: for qname, rtype, value in custom_dns:
@ -1082,10 +1083,17 @@ def get_secondary_dns(custom_dns, mode=None):
# doesn't. # doesn't.
if not hostname.startswith("xfr:"): if not hostname.startswith("xfr:"):
if mode == "xfr": if mode == "xfr":
response = dns.resolver.resolve(hostname+'.', "A", raise_on_no_answer=False) try:
response = resolver.resolve(hostname+'.', "A", raise_on_no_answer=False)
values.extend(map(str, response)) values.extend(map(str, response))
response = dns.resolver.resolve(hostname+'.', "AAAA", raise_on_no_answer=False) except dns.exception.DNSException:
logging.debug("Secondary dns Alookup exception %s", hostname)
try:
response = resolver.resolve(hostname+'.', "AAAA", raise_on_no_answer=False)
values.extend(map(str, response)) values.extend(map(str, response))
except dns.exception.DNSException:
logging.debug("Secondary dns AAAA lookup exception %s", hostname)
continue continue
values.append(hostname) values.append(hostname)
@ -1103,6 +1111,7 @@ def set_secondary_dns(hostnames, env):
# Validate that all hostnames are valid and that all zone-xfer IP addresses are valid. # Validate that all hostnames are valid and that all zone-xfer IP addresses are valid.
resolver = dns.resolver.get_default_resolver() resolver = dns.resolver.get_default_resolver()
resolver.timeout = 3 resolver.timeout = 3
resolver.lifetime = 3
for item in hostnames: for item in hostnames:
if not item.startswith("xfr:"): if not item.startswith("xfr:"):
# Resolve hostname. # Resolve hostname.
@ -1116,7 +1125,8 @@ def set_secondary_dns(hostnames, env):
raise ValueError("Could not resolve the IP address of %s." % item) raise ValueError("Could not resolve the IP address of %s." % item)
except (dns.resolver.Timeout): except (dns.resolver.Timeout):
resolver.timeout = 5 resolver.timeout = 5
logging.warning('Timeout on resolving ipv4 address re-trying') resolver.lifetime = 5
logging.debug('Timeout on resolving ipv4 address re-trying')
try: try:
response = resolver.resolve(item, "A") response = resolver.resolve(item, "A")
except (dns.resolver.NoNameservers, dns.resolver.NXDOMAIN, dns.resolver.NoAnswer): except (dns.resolver.NoNameservers, dns.resolver.NXDOMAIN, dns.resolver.NoAnswer):
@ -1128,6 +1138,7 @@ def set_secondary_dns(hostnames, env):
except (dns.resolver.Timeout): except (dns.resolver.Timeout):
raise ValueError("Could not resolve the IP address of %s due to timeout." % item) raise ValueError("Could not resolve the IP address of %s due to timeout." % item)
resolver.timeout = 3 resolver.timeout = 3
resolver.lifetime = 3
else: else:
# Validate IP address. # Validate IP address.
try: try:

View File

@ -802,6 +802,7 @@ def query_dns(qname, rtype, nxdomain='[Not Set]', at=None, as_list=False):
# Set a timeout so that a non-responsive server doesn't hold us back. # Set a timeout so that a non-responsive server doesn't hold us back.
resolver.timeout = 3 resolver.timeout = 3
reaolver.lifetime = 3
# Do the query. # Do the query.
try: try:
@ -809,20 +810,21 @@ def query_dns(qname, rtype, nxdomain='[Not Set]', at=None, as_list=False):
except (dns.resolver.NoNameservers, dns.resolver.NXDOMAIN, dns.resolver.NoAnswer): except (dns.resolver.NoNameservers, dns.resolver.NXDOMAIN, dns.resolver.NoAnswer):
# Host did not have an answer for this query; not sure what the # Host did not have an answer for this query; not sure what the
# difference is between the two exceptions. # difference is between the two exceptions.
logging.info("No result for dns lookup %s, %s", qname, rtype) logging.debug("No result for dns lookup %s, %s", qname, rtype)
return nxdomain return nxdomain
except dns.exception.Timeout: except dns.exception.Timeout:
logging.info("Timeout on dns lookup %s, %s. Retrying", qname, rtype) logging.debug("Timeout on dns lookup %s, %s. Retrying", qname, rtype)
resolver.timeout = 5 resolver.timeout = 5
resolver.lifetime = 5
try: try:
response = resolver.resolve(qname, rtype, search=True) response = resolver.resolve(qname, rtype, search=True)
except (dns.resolver.NoNameservers, dns.resolver.NXDOMAIN, dns.resolver.NoAnswer): except (dns.resolver.NoNameservers, dns.resolver.NXDOMAIN, dns.resolver.NoAnswer):
# Host did not have an answer for this query; not sure what the # Host did not have an answer for this query; not sure what the
# difference is between the two exceptions. # difference is between the two exceptions.
logging.info("No result for dns lookup %s, %s (2)", qname, rtype) logging.debug("No result for dns lookup %s, %s (2)", qname, rtype)
return nxdomain return nxdomain
except dns.exception.Timeout: except dns.exception.Timeout:
logging.info("Timeout on dns lookup %s, %s.", qname, rtype) logging.debug("Timeout on dns lookup %s, %s.", qname, rtype)
return "[timeout]" return "[timeout]"
# Normalize IP addresses. IP address --- especially IPv6 addresses --- can # Normalize IP addresses. IP address --- especially IPv6 addresses --- can