1
0
mirror of https://github.com/mail-in-a-box/mailinabox.git synced 2024-12-23 07:27: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:
return do_dns_update(env, force=request.form.get('force', '') == '1')
except Exception as e:
logging.exception('dns update exc')
return (str(e), 500)
@app.route('/dns/secondary-nameserver')
@ -763,7 +764,7 @@ def log_failed_login(request):
# APP
if __name__ == '__main__':
logging_level = logging.INFO
logging_level = logging.DEBUG
if "DEBUG" in os.environ:
# 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):
resolver = dns.resolver.get_default_resolver()
resolver.timeout = 10
resolver.lifetime = 10
values = []
for qname, rtype, value in custom_dns:
@ -1082,10 +1083,17 @@ def get_secondary_dns(custom_dns, mode=None):
# doesn't.
if not hostname.startswith("xfr:"):
if mode == "xfr":
response = dns.resolver.resolve(hostname+'.', "A", raise_on_no_answer=False)
values.extend(map(str, response))
response = dns.resolver.resolve(hostname+'.', "AAAA", raise_on_no_answer=False)
values.extend(map(str, response))
try:
response = resolver.resolve(hostname+'.', "A", raise_on_no_answer=False)
values.extend(map(str, response))
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))
except dns.exception.DNSException:
logging.debug("Secondary dns AAAA lookup exception %s", hostname)
continue
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.
resolver = dns.resolver.get_default_resolver()
resolver.timeout = 3
resolver.lifetime = 3
for item in hostnames:
if not item.startswith("xfr:"):
# Resolve hostname.
@ -1116,7 +1125,8 @@ def set_secondary_dns(hostnames, env):
raise ValueError("Could not resolve the IP address of %s." % item)
except (dns.resolver.Timeout):
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:
response = resolver.resolve(item, "A")
except (dns.resolver.NoNameservers, dns.resolver.NXDOMAIN, dns.resolver.NoAnswer):
@ -1128,6 +1138,7 @@ def set_secondary_dns(hostnames, env):
except (dns.resolver.Timeout):
raise ValueError("Could not resolve the IP address of %s due to timeout." % item)
resolver.timeout = 3
resolver.lifetime = 3
else:
# Validate IP address.
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.
resolver.timeout = 3
reaolver.lifetime = 3
# Do the query.
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):
# Host did not have an answer for this query; not sure what the
# 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
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.lifetime = 5
try:
response = resolver.resolve(qname, rtype, search=True)
except (dns.resolver.NoNameservers, dns.resolver.NXDOMAIN, dns.resolver.NoAnswer):
# Host did not have an answer for this query; not sure what the
# 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
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]"
# Normalize IP addresses. IP address --- especially IPv6 addresses --- can