From 5c91212fabe1e4c674acbf0ff84fb2ef4674e86d Mon Sep 17 00:00:00 2001 From: SomberNight Date: Mon, 25 Oct 2021 17:46:51 +0200 Subject: [PATCH] dns hacks: dns via proxy: special-case "localhost" string fix https://github.com/spesmilo/electrum/issues/7546 --- electrum/dns_hacks.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/electrum/dns_hacks.py b/electrum/dns_hacks.py index 7805464d3..c26a010ff 100644 --- a/electrum/dns_hacks.py +++ b/electrum/dns_hacks.py @@ -26,7 +26,11 @@ def configure_dns_depending_on_proxy(is_proxy: bool) -> None: socket._getaddrinfo = socket.getaddrinfo if is_proxy: # prevent dns leaks, see http://stackoverflow.com/questions/13184205/dns-over-proxy - socket.getaddrinfo = lambda *args: [(socket.AF_INET, socket.SOCK_STREAM, 6, '', (args[0], args[1]))] + def getaddrinfo(host, port, *args, **kwargs): + if _is_force_system_dns_for_host(host): + return socket._getaddrinfo(host, port, *args, **kwargs) + return [(socket.AF_INET, socket.SOCK_STREAM, 6, '', (host, port))] + socket.getaddrinfo = getaddrinfo else: if sys.platform == 'win32': # On Windows, socket.getaddrinfo takes a mutex, and might hold it for up to 10 seconds @@ -56,6 +60,10 @@ def _prepare_windows_dns_hack(): thread_name_prefix='dns_resolver') +def _is_force_system_dns_for_host(host: str) -> bool: + return str(host) in ('localhost', 'localhost.',) + + def _fast_getaddrinfo(host, *args, **kwargs): def needs_dns_resolving(host): try: @@ -63,7 +71,7 @@ def _fast_getaddrinfo(host, *args, **kwargs): return False # already valid IP except ValueError: pass # not an IP - if str(host) in ('localhost', 'localhost.',): + if _is_force_system_dns_for_host(host): return False return True