From 8222af964904d66e4515a166350edfe577b6b477 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Sat, 29 Oct 2022 20:51:30 +0200 Subject: [PATCH] tor works --- .env.example | 8 +++-- cashu/core/settings.py | 2 ++ cashu/tor/tor.pid | 2 +- cashu/tor/tor.py | 69 ++++++++++++++++++++++++++++-------------- cashu/wallet/wallet.py | 19 +++++++++--- 5 files changed, 70 insertions(+), 30 deletions(-) diff --git a/.env.example b/.env.example index cca95e4..564cfa6 100644 --- a/.env.example +++ b/.env.example @@ -8,8 +8,12 @@ CASHU_DIR=~/.cashu MINT_HOST=127.0.0.1 MINT_PORT=3338 -SOCKS_HOST = localhost -SOCKS_PORT = 9050 +# use builtin tor +TOR=TRUE + +# use custom tor proxy +SOCKS_HOST=localhost +SOCKS_PORT=9050 # MINT diff --git a/cashu/core/settings.py b/cashu/core/settings.py index b775f9b..b70d97e 100644 --- a/cashu/core/settings.py +++ b/cashu/core/settings.py @@ -24,6 +24,8 @@ CASHU_DIR = env.str("CASHU_DIR", default=os.path.join(str(Path.home()), ".cashu" CASHU_DIR = CASHU_DIR.replace("~", str(Path.home())) assert len(CASHU_DIR), "CASHU_DIR not defined" +TOR = env.bool("TOR", default=True) + SOCKS_HOST = env.str("SOCKS_HOST", default=None) SOCKS_PORT = env.int("SOCKS_PORT", default=9050) diff --git a/cashu/tor/tor.pid b/cashu/tor/tor.pid index fd7a393..a771b4a 100755 --- a/cashu/tor/tor.pid +++ b/cashu/tor/tor.pid @@ -1 +1 @@ -26406 \ No newline at end of file +98454 \ No newline at end of file diff --git a/cashu/tor/tor.py b/cashu/tor/tor.py index 51627c3..9054fc1 100755 --- a/cashu/tor/tor.py +++ b/cashu/tor/tor.py @@ -15,32 +15,30 @@ class TorProxy: self.tor_proc = None self.pid_file = os.path.join(self.base_path, "tor.pid") self.tor_pid = None - logger.info(f"Tor running: {self.is_running()}") - logger.info( + self.startup_finished = True + self.tor_running = self.is_running() + logger.debug(f"Tor running: {self.tor_running}") + logger.debug( f"Tor port open: {self.is_port_open()}", ) - logger.info(f"Tor binary path: {self.tor_path()}") - logger.info(f"Tor config path: {self.tor_config_path()}") - logger.info(f"Tor PID in tor.pid: {self.read_pid()}") - logger.info(f"Tor PID running: {self.signal_pid(self.read_pid())}") - self.run_daemon() + logger.debug(f"Tor binary path: {self.tor_path()}") + logger.debug(f"Tor config path: {self.tor_config_path()}") + logger.debug(f"Tor PID in tor.pid: {self.read_pid()}") + logger.debug(f"Tor PID running: {self.signal_pid(self.read_pid())}") + + if not self.tor_running: + logger.debug("Starting") + self.run_daemon() def run_daemon(self): - if self.is_port_open() and not self.is_running(): - raise Exception( - "Another Tor instance seems to be already running on port 9050." - ) - if self.is_running(): - logger.info("Tor proxy already running.") - return - self.tor_proc = subprocess.Popen( [f"{self.tor_path()}", "--defaults-torrc", f"{self.tor_config_path()}"], shell=False, + close_fds=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, ) - logger.info("Running tor daemon with pid {}".format(self.tor_proc.pid)) + logger.debug("Running tor daemon with pid {}".format(self.tor_proc.pid)) with open(self.pid_file, "w", encoding="utf-8") as f: f.write(str(self.tor_proc.pid)) @@ -71,6 +69,34 @@ class TorProxy: def tor_config_path(self): return os.path.join(self.base_path, "torrc") + def is_running(self): + # our tor proxy running from a previous session + if self.signal_pid(self.read_pid()): + logger.debug("Tor proxy already running.") + return True + # another tor proxy is running + if self.is_port_open(): + logger.debug( + "Another Tor instance seems to be already running on port 9050." + ) + return True + # current attached process running + return self.tor_proc and self.tor_proc.poll() is None + + def wait_until_startup(self): + if self.is_port_open(): + return + if self.tor_proc is None: + raise Exception("Tor proxy not attached.") + if not self.tor_proc.stdout: + raise Exception("could not get tor stdout.") + for line in self.tor_proc.stdout: + if "Bootstrapped 100%: Done" in str(line): + break + # tor is ready + self.startup_finished = True + return + def is_port_open(self): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) location = ("127.0.0.1", 9050) @@ -81,9 +107,6 @@ class TorProxy: except Exception as e: return False - def is_running(self): - return self.tor_proc is not None - def read_pid(self): if not os.path.isfile(self.pid_file): return None @@ -101,7 +124,6 @@ class TorProxy: """ if not pid: return False - print(f"running {pid} with signal={signal}") if not int(pid) > 0: return False pid = int(pid) @@ -115,6 +137,7 @@ class TorProxy: if __name__ == "__main__": tor = TorProxy() - time.sleep(5) - logger.info("Killing Tor") - tor.stop_daemon() + tor.wait_until_startup() + # time.sleep(5) + # logger.debug("Killing Tor") + # tor.stop_daemon() diff --git a/cashu/wallet/wallet.py b/cashu/wallet/wallet.py index ff0454f..ae081b7 100644 --- a/cashu/wallet/wallet.py +++ b/cashu/wallet/wallet.py @@ -35,7 +35,7 @@ from cashu.core.script import ( step2_carol_sign_tx, ) from cashu.core.secp import PublicKey -from cashu.core.settings import DEBUG, VERSION, SOCKS_HOST, SOCKS_PORT +from cashu.core.settings import DEBUG, VERSION, TOR, SOCKS_HOST, SOCKS_PORT from cashu.core.split import amount_split from cashu.wallet.crud import ( get_keyset, @@ -50,6 +50,8 @@ from cashu.wallet.crud import ( update_proof_reserved, ) +from cashu.tor.tor import TorProxy + class LedgerAPI: keys: Dict[int, str] @@ -62,13 +64,22 @@ class LedgerAPI: def _set_requests(self): s = requests.Session() - if SOCKS_HOST: + if TOR: + # overwrite custom settings + tor = TorProxy() + tor.wait_until_startup() + socks_host, socks_port = "localhost", 9050 + else: + socks_host, socks_port = SOCKS_HOST, SOCKS_PORT + + if socks_host and socks_port: proxies = { - "http": f"socks5://{SOCKS_HOST}:{SOCKS_PORT}", - "https": f"socks5://{SOCKS_HOST}:{SOCKS_PORT}", + "http": f"socks5://{socks_host}:{socks_port}", + "https": f"socks5://{socks_host}:{socks_port}", } s.proxies.update(proxies) s.headers.update({"User-Agent": scrts.token_urlsafe(8)}) + print(s.get(self.url + "/keys").json()) return s