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 Apr 8, 2021
1 parent 557cb58 commit 335349d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 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

0 comments on commit 335349d

Please sign in to comment.