Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support SOCKS over Unix domain sockets #115

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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