Browse Source

windows DNS resolution: handle IPv6

related: #5176, #4421
regtest_lnd
SomberNight 6 years ago
parent
commit
9b0773cf2b
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 42
      electrum/network.py

42
electrum/network.py

@ -516,30 +516,40 @@ class Network(PrintError):
@staticmethod @staticmethod
def _fast_getaddrinfo(host, *args, **kwargs): def _fast_getaddrinfo(host, *args, **kwargs):
def needs_dns_resolving(host2): def needs_dns_resolving(host):
try: try:
ipaddress.ip_address(host2) ipaddress.ip_address(host)
return False # already valid IP return False # already valid IP
except ValueError: except ValueError:
pass # not an IP pass # not an IP
if str(host) in ('localhost', 'localhost.',): if str(host) in ('localhost', 'localhost.',):
return False return False
return True return True
try: def resolve_with_dnspython(host):
if needs_dns_resolving(host): # try IPv6
answers = dns.resolver.query(host) try:
addr = str(answers[0]) answers = dns.resolver.query(host, dns.rdatatype.AAAA)
else: return str(answers[0])
addr = host except dns.exception.DNSException as e:
except dns.exception.DNSException as e: pass
# dns failed for some reason, e.g. dns.resolver.NXDOMAIN except BaseException as e:
# this is normal. Simply report back failure: print_error(f'dnspython failed to resolve dns (AAAA) with error: {e}')
raise socket.gaierror(11001, 'getaddrinfo failed') from e # try IPv4
except BaseException as e: try:
# Possibly internal error in dnspython :( see #4483 answers = dns.resolver.query(host, dns.rdatatype.A)
return str(answers[0])
except dns.exception.DNSException as e:
# dns failed for some reason, e.g. dns.resolver.NXDOMAIN
# this is normal. Simply report back failure:
raise socket.gaierror(11001, 'getaddrinfo failed') from e
except BaseException as e:
# Possibly internal error in dnspython :( see #4483
print_error(f'dnspython failed to resolve dns (A) with error: {e}')
# Fall back to original socket.getaddrinfo to resolve dns. # Fall back to original socket.getaddrinfo to resolve dns.
print_error('dnspython failed to resolve dns with error:', e) return host
addr = host addr = host
if needs_dns_resolving(host):
addr = resolve_with_dnspython(host)
return socket._getaddrinfo(addr, *args, **kwargs) return socket._getaddrinfo(addr, *args, **kwargs)
@log_exceptions @log_exceptions

Loading…
Cancel
Save