This commit is contained in:
callebtc
2022-10-29 22:55:32 +02:00
parent 77c66880c1
commit d6fa754a6d
3 changed files with 31 additions and 13 deletions

View File

@@ -9,10 +9,10 @@ from loguru import logger
class TorProxy: class TorProxy:
def __init__(self): def __init__(self, keep_alive=False):
self.base_path = pathlib.Path(__file__).parent.resolve() self.base_path = pathlib.Path(__file__).parent.resolve()
self.platform = platform.system() self.platform = platform.system()
self.keep_alive = 60 * 60 # seconds self.keep_alive = 60 * 60 if keep_alive else 0 # seconds
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
@@ -32,14 +32,15 @@ class TorProxy:
self.run_daemon() self.run_daemon()
def run_daemon(self): def run_daemon(self):
cmd = [
f"{self.tor_path()}",
"--defaults-torrc",
f"{self.tor_config_path()}",
]
if self.keep_alive and platform.system() != "Windows":
cmd = ["timeout", f"{self.keep_alive}"] + cmd
self.tor_proc = subprocess.Popen( self.tor_proc = subprocess.Popen(
[ cmd,
"timeout",
"20",
f"{self.tor_path()}",
"--defaults-torrc",
f"{self.tor_config_path()}",
],
shell=False, shell=False,
close_fds=True, close_fds=True,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
@@ -68,7 +69,7 @@ class TorProxy:
# make sure that file has correct permissions # make sure that file has correct permissions
try: try:
logger.debug(f"Setting permissions of {PATHS[platform.system()]} to 755") logger.debug(f"Setting permissions of {PATHS[platform.system()]} to 755")
os.chmod(PATHS[platform.system()], 755) os.chmod(PATHS[platform.system()], 0o755)
except: except:
raise Exception("error setting permissions for tor binary.") raise Exception("error setting permissions for tor binary.")
return PATHS[platform.system()] return PATHS[platform.system()]

View File

@@ -64,8 +64,7 @@ class LedgerAPI:
def _set_requests(self): def _set_requests(self):
s = requests.Session() s = requests.Session()
if TOR: if TOR:
# overwrite custom settings tor = TorProxy(keep_alive=True)
tor = TorProxy()
tor.wait_until_startup() tor.wait_until_startup()
socks_host, socks_port = "localhost", 9050 socks_host, socks_port = "localhost", 9050
else: else:
@@ -78,7 +77,6 @@ class LedgerAPI:
} }
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

19
tests/test_tor.py Normal file
View File

@@ -0,0 +1,19 @@
import requests
from cashu.tor.tor import TorProxy
def test_tor_setup():
s = requests.Session()
tor = TorProxy(keep_alive=False)
tor.wait_until_startup()
socks_host, socks_port = "localhost", 9050
proxies = {
"http": f"socks5://{socks_host}:{socks_port}",
"https": f"socks5://{socks_host}:{socks_port}",
}
s.proxies.update(proxies)
resp = s.get("https://google.com")
resp.raise_for_status()