Skip to content

Commit

Permalink
Support SOCKS over Unix domain sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyRand committed May 7, 2021
1 parent 557cb58 commit c8d027a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
1 change: 1 addition & 0 deletions electrumx/server/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def __init__(self, coin=None):
self.force_proxy = self.boolean('FORCE_PROXY', False)
self.tor_proxy_host = self.default('TOR_PROXY_HOST', 'localhost')
self.tor_proxy_port = self.integer('TOR_PROXY_PORT', None)
self.tor_proxy_path = self.default('TOR_PROXY_IPC_PATH', None)

# Misc

Expand Down
26 changes: 16 additions & 10 deletions electrumx/server/peers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

import aiohttp
from aiorpcx import (Event, Notification, RPCError, RPCSession, SOCKSError,
SOCKSProxy, TaskGroup, TaskTimeout, connect_rs,
handler_invocation, ignore_after, sleep)
SOCKSProxy, TaskGroup, TaskTimeout, UnixAddress,
connect_rs, handler_invocation, ignore_after, sleep)
from aiorpcx.jsonrpc import CodeMessageError

from electrumx.lib.peer import Peer
Expand Down Expand Up @@ -185,15 +185,21 @@ async def _detect_proxy(self):
If found self.proxy is set to a SOCKSProxy instance, otherwise None.
'''
host = self.env.tor_proxy_host
if self.env.tor_proxy_port is None:
ports = [9050, 9150, 1080]
else:
ports = [self.env.tor_proxy_port]
path = self.env.tor_proxy_path
if path is None:
host = self.env.tor_proxy_host
if self.env.tor_proxy_port is None:
ports = [9050, 9150, 1080]
else:
ports = [self.env.tor_proxy_port]
while True:
self.logger.info(f'trying to detect proxy on "{host}" '
f'ports {ports}')
proxy = await SOCKSProxy.auto_detect_at_host(host, ports, None)
if path is None:
self.logger.info(f'trying to detect proxy on "{host}" '
f'ports {ports}')
proxy = await SOCKSProxy.auto_detect_at_host(host, ports, None)
else:
self.logger.info(f'trying to detect proxy on "{path}"')
proxy = await SOCKSProxy.auto_detect_at_address(UnixAddress(path), None)
if proxy:
self.proxy = proxy
self.logger.info(f'detected {proxy}')
Expand Down
7 changes: 5 additions & 2 deletions electrumx/server/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@

import attr
import pylru
from aiorpcx import (Event, JSONRPCAutoDetect, JSONRPCConnection,
from aiorpcx import (Event, JSONRPCAutoDetect, JSONRPCConnection, NetAddress,
ReplyAndDisconnect, Request, RPCError, RPCSession,
TaskGroup, handler_invocation, serve_rs, serve_ws, sleep)
TaskGroup, UnixAddress, handler_invocation, serve_rs,
serve_ws, sleep)

import electrumx
import electrumx.lib.util as util
Expand Down Expand Up @@ -1236,6 +1237,8 @@ def is_tor(self):
proxy_address = self.peer_mgr.proxy_address()
if not proxy_address:
return False
if isinstance(proxy_address, UnixAddress):
proxy_address = NetAddress("127.0.0.1", 9150)
return self.remote_address().host == proxy_address.host

async def replaced_banner(self, banner):
Expand Down

0 comments on commit c8d027a

Please sign in to comment.