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

improve tor resilience #1161

Merged
merged 1 commit into from
May 12, 2021
Merged
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
4 changes: 4 additions & 0 deletions src/cryptoadvance/specter/managers/config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ def generate_torrc_password(self, overwrite=False):
self.data["torrc_password"] = secrets.token_urlsafe(16)
self._save()
logger.info(f"Generated torrc_password in {self.data_file}")
else:
logger.info(
f"torrc_password in {self.data_file} already set. Skipping generation"
)

def update_hwi_bridge_url(self, url, user):
"""update the hwi bridge url to use"""
Expand Down
2 changes: 1 addition & 1 deletion src/cryptoadvance/specter/server_endpoints/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def tor():
res = requests_session.get(
# "http://expyuzz4wqqyqhjn.onion", # Tor Project onion website (seems to be down)
"https://protonirockerxow.onion", # Proton mail onion website
timeout=10,
timeout=30,
)
tor_connectable = res.status_code == 200
if tor_connectable:
Expand Down
24 changes: 24 additions & 0 deletions src/cryptoadvance/specter/tor_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
import os
import platform
import signal
import socket
import subprocess
from io import StringIO

import psutil

from .specter_error import SpecterError

logger = logging.getLogger(__name__)


Expand All @@ -21,9 +24,14 @@ def __init__(
self.tor_daemon_path = tor_daemon_path
self.tor_config_path = tor_config_path
self.tor_daemon_proc = None
self.is_running() # throws an exception if port 9050 is open (which looks like another tor-process is running)

def start_tor_daemon(self, cleanup_at_exit=True):
if self.is_running():
if self.tor_daemon_proc is None:
raise Exception(
"Tor Daemon running but not via this Controller instance."
)
return

self.tor_daemon_proc = subprocess.Popen(
Expand All @@ -37,6 +45,8 @@ def start_tor_daemon(self, cleanup_at_exit=True):
)

def get_logs(self):
if self.is_running():
return "Cannot return logs as tor is still running"
logs = ""
newline = self.tor_daemon_proc.stdout.readline().decode("ascii")
while newline != "":
Expand All @@ -55,8 +65,22 @@ def get_hashed_password(self, password):
return hashed_pw

def is_running(self):
if not self.tor_daemon_proc and self.is_port_open():
raise SpecterError(
"Port 9050 is open but tor_daemon_proc is not existing. Probably another Tor-Daemon is running?!"
)
return self.tor_daemon_proc and self.tor_daemon_proc.poll() is None

def is_port_open(self):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
location = ("127.0.0.1", 9050)
try:
s.connect(location)
s.close()
return True
except Exception as e:
return False

def stop_tor_daemon(self):
timeout = 50 # in secs
if self.tor_daemon_proc:
Expand Down