mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-08-23 17:47:31 +00:00
parent
9161d3a5a1
commit
9b0773cf2b
1 changed files with 26 additions and 16 deletions
|
@ -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
|
||||||
|
def resolve_with_dnspython(host):
|
||||||
|
# try IPv6
|
||||||
try:
|
try:
|
||||||
if needs_dns_resolving(host):
|
answers = dns.resolver.query(host, dns.rdatatype.AAAA)
|
||||||
answers = dns.resolver.query(host)
|
return str(answers[0])
|
||||||
addr = str(answers[0])
|
except dns.exception.DNSException as e:
|
||||||
else:
|
pass
|
||||||
addr = host
|
except BaseException as e:
|
||||||
|
print_error(f'dnspython failed to resolve dns (AAAA) with error: {e}')
|
||||||
|
# try IPv4
|
||||||
|
try:
|
||||||
|
answers = dns.resolver.query(host, dns.rdatatype.A)
|
||||||
|
return str(answers[0])
|
||||||
except dns.exception.DNSException as e:
|
except dns.exception.DNSException as e:
|
||||||
# dns failed for some reason, e.g. dns.resolver.NXDOMAIN
|
# dns failed for some reason, e.g. dns.resolver.NXDOMAIN
|
||||||
# this is normal. Simply report back failure:
|
# this is normal. Simply report back failure:
|
||||||
raise socket.gaierror(11001, 'getaddrinfo failed') from e
|
raise socket.gaierror(11001, 'getaddrinfo failed') from e
|
||||||
except BaseException as e:
|
except BaseException as e:
|
||||||
# Possibly internal error in dnspython :( see #4483
|
# 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…
Add table
Reference in a new issue