don't dns resolve raw IP addresses

This commit is contained in:
SomberNight 2018-07-02 00:59:28 +02:00
parent e2b6d2d0f7
commit c6124549cd
No known key found for this signature in database
GPG key ID: B33B5F232C6271E9

View file

@ -32,12 +32,14 @@ import threading
import socket import socket
import json import json
import sys import sys
import ipaddress
import dns import dns
import dns.resolver import dns.resolver
import socks import socks
from . import util from . import util
from .util import print_error
from . import bitcoin from . import bitcoin
from .bitcoin import COIN from .bitcoin import COIN
from . import constants from . import constants
@ -452,9 +454,23 @@ class Network(util.DaemonThread):
# On Windows, socket.getaddrinfo takes a mutex, and might hold it for up to 10 seconds # On Windows, socket.getaddrinfo takes a mutex, and might hold it for up to 10 seconds
# when dns-resolving. To speed it up drastically, we resolve dns ourselves, outside that lock. # when dns-resolving. To speed it up drastically, we resolve dns ourselves, outside that lock.
# see #4421 # see #4421
def fast_getaddrinfo(host, *args, **kwargs): socket.getaddrinfo = self._fast_getaddrinfo
else:
socket.getaddrinfo = socket._getaddrinfo
@staticmethod
def _fast_getaddrinfo(host, *args, **kwargs):
def needs_dns_resolving(host2):
try: try:
if str(host) not in ('localhost', 'localhost.',): ipaddress.ip_address(host2)
return False # already valid IP
except ValueError:
pass # not an IP
if str(host) in ('localhost', 'localhost.',):
return False
return True
try:
if needs_dns_resolving(host):
answers = dns.resolver.query(host) answers = dns.resolver.query(host)
addr = str(answers[0]) addr = str(answers[0])
else: else:
@ -466,12 +482,9 @@ class Network(util.DaemonThread):
except BaseException as e: except BaseException as e:
# Possibly internal error in dnspython :( see #4483 # Possibly internal error in dnspython :( see #4483
# Fall back to original socket.getaddrinfo to resolve dns. # Fall back to original socket.getaddrinfo to resolve dns.
self.print_error('dnspython failed to resolve dns with error:', e) print_error('dnspython failed to resolve dns with error:', e)
addr = host addr = host
return socket._getaddrinfo(addr, *args, **kwargs) return socket._getaddrinfo(addr, *args, **kwargs)
socket.getaddrinfo = fast_getaddrinfo
else:
socket.getaddrinfo = socket._getaddrinfo
@with_interface_lock @with_interface_lock
def start_network(self, protocol, proxy): def start_network(self, protocol, proxy):