Error-proofed DNS-resolution

This commit is contained in:
Geoffrey Frogeye 2019-11-10 22:18:27 +01:00
parent 2f1af3c850
commit 0801bd9e44

View file

@ -10,10 +10,12 @@ import re
import sys import sys
import dns.resolver import dns.resolver
import dns.exception
import progressbar import progressbar
import regexes import regexes
DNS_TIMEOUT = 5.0
def is_subdomain_matching(subdomain: str) -> bool: def is_subdomain_matching(subdomain: str) -> bool:
""" """
@ -21,9 +23,20 @@ def is_subdomain_matching(subdomain: str) -> bool:
""" """
# TODO Look at the whole chain rather than the last one # TODO Look at the whole chain rather than the last one
try: try:
query = dns.resolver.query(subdomain, 'A') query = dns.resolver.query(subdomain, 'A', lifetime=DNS_TIMEOUT)
except dns.resolver.NXDOMAIN: except dns.resolver.NXDOMAIN:
return False return False
except dns.resolver.NoAnswer:
return False
except dns.resolver.YXDOMAIN:
print(f"Query name too long for {subdomain}", file=sys.stderr)
return False
except dns.resolver.NoNameservers:
print(f"All nameservers broken for {subdomain}", file=sys.stderr)
return False
except dns.exception.Timeout:
print(f"Timeout for {subdomain}", file=sys.stderr)
return False
canonical = query.canonical_name.to_text() canonical = query.canonical_name.to_text()
for regex in regexes.REGEXES: for regex in regexes.REGEXES:
if re.match(regex, canonical): if re.match(regex, canonical):