Some refactor
This commit is contained in:
@@ -41,21 +41,19 @@ def key_str(key):
|
|||||||
|
|
||||||
|
|
||||||
class Onion(object):
|
class Onion(object):
|
||||||
def __init__(self):
|
tor_data_directory_name: str
|
||||||
self.tor_data_directory_name = None
|
tor_control_socket: str | None
|
||||||
self.tor_control_socket = None
|
tor_control_port: int | None
|
||||||
self.tor_control_port = None
|
tor_torrc: str
|
||||||
self.tor_torrc = None
|
tor_socks_port: int
|
||||||
self.tor_socks_port = None
|
tor_cookie_auth_file: str
|
||||||
self.tor_cookie_auth_file = None
|
tor_data_directory: tempfile.TemporaryDirectory
|
||||||
self.tor_data_directory = None
|
tor_path: str = dirs.get_tor_paths()
|
||||||
self.tor_path = dirs.get_tor_paths()
|
tor_proc: subprocess.Popen
|
||||||
self.tor_proc = None
|
c: Controller
|
||||||
# noinspection PyTypeChecker
|
connected_to_tor: bool = False
|
||||||
self.c: Controller = None
|
auth_string: str
|
||||||
self.connected_to_tor = False
|
graceful_close_onions: list = list()
|
||||||
self.auth_string = None
|
|
||||||
self.graceful_close_onions = []
|
|
||||||
|
|
||||||
def kill_same_tor(self):
|
def kill_same_tor(self):
|
||||||
for proc in psutil.process_iter(["pid", "name", "username"]):
|
for proc in psutil.process_iter(["pid", "name", "username"]):
|
||||||
@@ -115,9 +113,7 @@ class Onion(object):
|
|||||||
with open(self.tor_torrc, "w") as f:
|
with open(self.tor_torrc, "w") as f:
|
||||||
f.write(torrc_template)
|
f.write(torrc_template)
|
||||||
|
|
||||||
def connect(self, connect_timeout=120):
|
def connect(self, connect_timeout=60):
|
||||||
self.c = None
|
|
||||||
|
|
||||||
self.tor_data_directory = tempfile.TemporaryDirectory(
|
self.tor_data_directory = tempfile.TemporaryDirectory(
|
||||||
dir=dirs.build_tmp_dir()
|
dir=dirs.build_tmp_dir()
|
||||||
)
|
)
|
||||||
@@ -179,9 +175,11 @@ class Onion(object):
|
|||||||
try:
|
try:
|
||||||
self.tor_proc.terminate()
|
self.tor_proc.terminate()
|
||||||
print(
|
print(
|
||||||
"Taking too long to connect to Tor. Maybe you aren't connected to the Internet, "
|
"Taking too long to connect to Tor. Maybe you aren't "
|
||||||
"or have an inaccurate system clock?"
|
"connected to the Internet, or have an inaccurate "
|
||||||
|
"system clock?"
|
||||||
)
|
)
|
||||||
|
self.cleanup()
|
||||||
raise
|
raise
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
pass
|
pass
|
||||||
@@ -209,7 +207,12 @@ class Onion(object):
|
|||||||
)
|
)
|
||||||
return services[name]
|
return services[name]
|
||||||
|
|
||||||
def start_onion_service(self, name):
|
def start_onion_service(self, name: str) -> str:
|
||||||
|
"""
|
||||||
|
Starts onion service
|
||||||
|
:param name: Name of created service (must exist in data.storage)
|
||||||
|
:return: .onion url
|
||||||
|
"""
|
||||||
if name not in services.keys():
|
if name not in services.keys():
|
||||||
raise 'Service not created'
|
raise 'Service not created'
|
||||||
|
|
||||||
@@ -238,7 +241,8 @@ class Onion(object):
|
|||||||
service.key_content = res.private_key
|
service.key_content = res.private_key
|
||||||
|
|
||||||
self.auth_string = \
|
self.auth_string = \
|
||||||
base64.b64encode(f'{res.service_id}:descriptor:x25519:{service.client_auth_priv_key}'.encode()).decode()
|
base64.b64encode(f'{res.service_id}:descriptor:x25519:'
|
||||||
|
f'{service.client_auth_priv_key}'.encode()).decode()
|
||||||
|
|
||||||
services[name] = service
|
services[name] = service
|
||||||
|
|
||||||
@@ -291,7 +295,8 @@ class Onion(object):
|
|||||||
else:
|
else:
|
||||||
circuits = "circuits"
|
circuits = "circuits"
|
||||||
print(
|
print(
|
||||||
f"\rWaiting for {num_rend_circuits} Tor rendezvous {circuits} to close {symbols[symbols_i]} ",
|
f"\rWaiting for {num_rend_circuits} Tor rendezvous "
|
||||||
|
f"{circuits} to close {symbols[symbols_i]} ",
|
||||||
end="",
|
end="",
|
||||||
)
|
)
|
||||||
symbols_i = (symbols_i + 1) % len(symbols)
|
symbols_i = (symbols_i + 1) % len(symbols)
|
||||||
@@ -316,5 +321,7 @@ class Onion(object):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f'Cannot cleanup temporary directory: {e}')
|
print(f'Cannot cleanup temporary directory: {e}')
|
||||||
|
|
||||||
|
@property
|
||||||
def get_tor_socks_port(self):
|
def get_tor_socks_port(self):
|
||||||
|
assert isinstance(self.tor_socks_port, int)
|
||||||
return "127.0.0.1", self.tor_socks_port
|
return "127.0.0.1", self.tor_socks_port
|
||||||
|
|||||||
Reference in New Issue
Block a user