mirror of
https://github.com/aljazceru/nutshell.git
synced 2026-01-23 18:44:19 +01:00
tor works
This commit is contained in:
@@ -8,8 +8,12 @@ CASHU_DIR=~/.cashu
|
|||||||
MINT_HOST=127.0.0.1
|
MINT_HOST=127.0.0.1
|
||||||
MINT_PORT=3338
|
MINT_PORT=3338
|
||||||
|
|
||||||
SOCKS_HOST = localhost
|
# use builtin tor
|
||||||
SOCKS_PORT = 9050
|
TOR=TRUE
|
||||||
|
|
||||||
|
# use custom tor proxy
|
||||||
|
SOCKS_HOST=localhost
|
||||||
|
SOCKS_PORT=9050
|
||||||
|
|
||||||
# MINT
|
# MINT
|
||||||
|
|
||||||
|
|||||||
@@ -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()))
|
CASHU_DIR = CASHU_DIR.replace("~", str(Path.home()))
|
||||||
assert len(CASHU_DIR), "CASHU_DIR not defined"
|
assert len(CASHU_DIR), "CASHU_DIR not defined"
|
||||||
|
|
||||||
|
TOR = env.bool("TOR", default=True)
|
||||||
|
|
||||||
SOCKS_HOST = env.str("SOCKS_HOST", default=None)
|
SOCKS_HOST = env.str("SOCKS_HOST", default=None)
|
||||||
SOCKS_PORT = env.int("SOCKS_PORT", default=9050)
|
SOCKS_PORT = env.int("SOCKS_PORT", default=9050)
|
||||||
|
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
26406
|
98454
|
||||||
@@ -15,32 +15,30 @@ class TorProxy:
|
|||||||
self.tor_proc = None
|
self.tor_proc = None
|
||||||
self.pid_file = os.path.join(self.base_path, "tor.pid")
|
self.pid_file = os.path.join(self.base_path, "tor.pid")
|
||||||
self.tor_pid = None
|
self.tor_pid = None
|
||||||
logger.info(f"Tor running: {self.is_running()}")
|
self.startup_finished = True
|
||||||
logger.info(
|
self.tor_running = self.is_running()
|
||||||
|
logger.debug(f"Tor running: {self.tor_running}")
|
||||||
|
logger.debug(
|
||||||
f"Tor port open: {self.is_port_open()}",
|
f"Tor port open: {self.is_port_open()}",
|
||||||
)
|
)
|
||||||
logger.info(f"Tor binary path: {self.tor_path()}")
|
logger.debug(f"Tor binary path: {self.tor_path()}")
|
||||||
logger.info(f"Tor config path: {self.tor_config_path()}")
|
logger.debug(f"Tor config path: {self.tor_config_path()}")
|
||||||
logger.info(f"Tor PID in tor.pid: {self.read_pid()}")
|
logger.debug(f"Tor PID in tor.pid: {self.read_pid()}")
|
||||||
logger.info(f"Tor PID running: {self.signal_pid(self.read_pid())}")
|
logger.debug(f"Tor PID running: {self.signal_pid(self.read_pid())}")
|
||||||
self.run_daemon()
|
|
||||||
|
if not self.tor_running:
|
||||||
|
logger.debug("Starting")
|
||||||
|
self.run_daemon()
|
||||||
|
|
||||||
def run_daemon(self):
|
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(
|
self.tor_proc = subprocess.Popen(
|
||||||
[f"{self.tor_path()}", "--defaults-torrc", f"{self.tor_config_path()}"],
|
[f"{self.tor_path()}", "--defaults-torrc", f"{self.tor_config_path()}"],
|
||||||
shell=False,
|
shell=False,
|
||||||
|
close_fds=True,
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
stderr=subprocess.STDOUT,
|
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:
|
with open(self.pid_file, "w", encoding="utf-8") as f:
|
||||||
f.write(str(self.tor_proc.pid))
|
f.write(str(self.tor_proc.pid))
|
||||||
|
|
||||||
@@ -71,6 +69,34 @@ class TorProxy:
|
|||||||
def tor_config_path(self):
|
def tor_config_path(self):
|
||||||
return os.path.join(self.base_path, "torrc")
|
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):
|
def is_port_open(self):
|
||||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
location = ("127.0.0.1", 9050)
|
location = ("127.0.0.1", 9050)
|
||||||
@@ -81,9 +107,6 @@ class TorProxy:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def is_running(self):
|
|
||||||
return self.tor_proc is not None
|
|
||||||
|
|
||||||
def read_pid(self):
|
def read_pid(self):
|
||||||
if not os.path.isfile(self.pid_file):
|
if not os.path.isfile(self.pid_file):
|
||||||
return None
|
return None
|
||||||
@@ -101,7 +124,6 @@ class TorProxy:
|
|||||||
"""
|
"""
|
||||||
if not pid:
|
if not pid:
|
||||||
return False
|
return False
|
||||||
print(f"running {pid} with signal={signal}")
|
|
||||||
if not int(pid) > 0:
|
if not int(pid) > 0:
|
||||||
return False
|
return False
|
||||||
pid = int(pid)
|
pid = int(pid)
|
||||||
@@ -115,6 +137,7 @@ class TorProxy:
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
tor = TorProxy()
|
tor = TorProxy()
|
||||||
time.sleep(5)
|
tor.wait_until_startup()
|
||||||
logger.info("Killing Tor")
|
# time.sleep(5)
|
||||||
tor.stop_daemon()
|
# logger.debug("Killing Tor")
|
||||||
|
# tor.stop_daemon()
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ from cashu.core.script import (
|
|||||||
step2_carol_sign_tx,
|
step2_carol_sign_tx,
|
||||||
)
|
)
|
||||||
from cashu.core.secp import PublicKey
|
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.core.split import amount_split
|
||||||
from cashu.wallet.crud import (
|
from cashu.wallet.crud import (
|
||||||
get_keyset,
|
get_keyset,
|
||||||
@@ -50,6 +50,8 @@ from cashu.wallet.crud import (
|
|||||||
update_proof_reserved,
|
update_proof_reserved,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
from cashu.tor.tor import TorProxy
|
||||||
|
|
||||||
|
|
||||||
class LedgerAPI:
|
class LedgerAPI:
|
||||||
keys: Dict[int, str]
|
keys: Dict[int, str]
|
||||||
@@ -62,13 +64,22 @@ class LedgerAPI:
|
|||||||
|
|
||||||
def _set_requests(self):
|
def _set_requests(self):
|
||||||
s = requests.Session()
|
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 = {
|
proxies = {
|
||||||
"http": f"socks5://{SOCKS_HOST}:{SOCKS_PORT}",
|
"http": f"socks5://{socks_host}:{socks_port}",
|
||||||
"https": f"socks5://{SOCKS_HOST}:{SOCKS_PORT}",
|
"https": f"socks5://{socks_host}:{socks_port}",
|
||||||
}
|
}
|
||||||
s.proxies.update(proxies)
|
s.proxies.update(proxies)
|
||||||
s.headers.update({"User-Agent": scrts.token_urlsafe(8)})
|
s.headers.update({"User-Agent": scrts.token_urlsafe(8)})
|
||||||
|
print(s.get(self.url + "/keys").json())
|
||||||
|
|
||||||
return s
|
return s
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user