From 12b659ff994777de71e68bc3a56fbaa149385a80 Mon Sep 17 00:00:00 2001 From: SomberNight Date: Fri, 5 Nov 2021 23:55:55 +0100 Subject: [PATCH] daemon: change rpcsock default to "unix" where available The daemon by default listens for RPC over a socket. Before this change, this socket was a TCP/IP network socket (over localhost), but now we change it to be a unix domain socket by default (if available). This socket is used not only when sending commands over JSON-RPC, but also when using the CLI, and to a tiny extent even used when running the GUI. The type of socket used (and hence this change) is invisible to CLI and GUI users. JSON-RPC users might want to use either the --rpcsock CLI flag or set the "rpcsock" config key (to "tcp") to switch back to using a TCP/IP socket. In practice it seems unix domain sockets are available on all mainstream OSes/platforms, except for Windows. closes https://github.com/spesmilo/electrum/issues/7553 --- electrum/daemon.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/electrum/daemon.py b/electrum/daemon.py index c40d1f96b..33d46f306 100644 --- a/electrum/daemon.py +++ b/electrum/daemon.py @@ -33,6 +33,7 @@ from typing import Dict, Optional, Tuple, Iterable, Callable, Union, Sequence, M from base64 import b64decode, b64encode from collections import defaultdict import json +import socket import aiohttp from aiohttp import web, client_exceptions @@ -64,7 +65,12 @@ class DaemonNotRunning(Exception): def get_rpcsock_defaultpath(config: SimpleConfig): return os.path.join(config.path, 'daemon_rpc_socket') -def get_rpcsock_default_type(osname): +def get_rpcsock_default_type(): + # Use unix domain sockets when available, + # with the extra paranoia that in case windows "implements" them, + # we want to test it before making it the default there. + if hasattr(socket, 'AF_UNIX') and sys.platform != 'win32': + return 'unix' return 'tcp' def get_lockfile(config: SimpleConfig): @@ -245,7 +251,7 @@ class CommandsServer(AuthenticatedServer): self.fd = fd self.config = daemon.config sockettype = self.config.get('rpcsock', 'auto') - self.socktype = sockettype if sockettype != 'auto' else get_rpcsock_default_type(os.name) + self.socktype = sockettype if sockettype != 'auto' else get_rpcsock_default_type() self.sockpath = self.config.get('rpcsockpath', get_rpcsock_defaultpath(self.config)) self.host = self.config.get('rpchost', '127.0.0.1') self.port = self.config.get('rpcport', 0)