Skip to content

Commit

Permalink
Broadcast through tor automatically if running
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtoth committed Jul 1, 2019
1 parent 015742d commit 2dedeec
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion config.ini_sample
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,12 @@ disable_mempool_fee_histogram = false

# Parameter for broadcasting unconfirmed transactions
# Options are:
# * tor-or-own-node (use tor if tor is running locally, otherwise own-node)
# * own-node (broadcast using the connected full node)
# * tor (broadcast to random nodes over tor)
# * system <cmd> %s (save transaction to file, and invoke system command
# with file path as parameter %s)
broadcast_method = own-node
broadcast_method = tor-or-own-node

# For tor broadcasting (broadcast_method = tor) configure
# the tor proxy host and port below
Expand Down
29 changes: 29 additions & 0 deletions electrumpersonalserver/server/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from ipaddress import ip_network, ip_address
import logging
import tempfile
import socket

from electrumpersonalserver.server.jsonrpc import JsonRpc, JsonRpcError
import electrumpersonalserver.server.hashes as hashes
Expand Down Expand Up @@ -49,6 +50,24 @@
bestblockhash = [None]
txid_blockhash_map = {}

def get_tor_hostport():
# Probable ports for Tor to listen at
host = "127.0.0.1"
ports = [9050, 9150]
for port in ports:
try:
s = (socket._socketobject if hasattr(socket, "_socketobject")
else socket.socket)(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(0.1)
s.connect((host, port))
# Tor responds uniquely to HTTP-like requests
s.send(b"GET\n")
if b"Tor is not an HTTP Proxy" in s.recv(1024):
return (host, port)
except socket.error:
pass
return None

def send_response(sock, query, result):
logger = logging.getLogger('ELECTRUMPERSONALSERVER')
response = {"jsonrpc": "2.0", "result": result, "id": query["id"]}
Expand Down Expand Up @@ -241,6 +260,16 @@ def handle_query(sock, line, rpc, txmonitor, disable_mempool_fee_histogram,
result = txreport["txid"]
logger.info('Broadcasting tx ' + txreport["txid"] + " with " +
"broadcast method: " + broadcast_method)
if broadcast_method == "tor-or-own-node":
tor_hostport = get_tor_hostport()
if tor_hostport is not None:
logger.info("Tor detected at " + str(tor_hostport) +
". Broadcasting through tor.")
broadcast_method = "tor"
else:
logger.info("Could not detect tor. Broadcasting through " +
"own node.")
broadcast_method = "own-node"
if broadcast_method == "own-node":
if not rpc.call("getnetworkinfo", [])["localrelay"]:
error = "Broadcast disabled when using blocksonly"
Expand Down

0 comments on commit 2dedeec

Please sign in to comment.