2022-05-31 00:52:03 +00:00
|
|
|
import shelve
|
2022-02-26 07:07:18 +00:00
|
|
|
from threading import Thread
|
|
|
|
from time import sleep
|
|
|
|
import os
|
2022-06-05 20:11:53 +00:00
|
|
|
import dbm
|
2022-05-31 00:52:03 +00:00
|
|
|
import traceback
|
|
|
|
from typing import Callable
|
2022-02-26 07:07:18 +00:00
|
|
|
|
|
|
|
from gossip.peer import Peer
|
2022-03-21 06:03:53 +00:00
|
|
|
from gossip.peerset import gossip_peer_set
|
2022-05-31 00:52:03 +00:00
|
|
|
from utils.identifyhome import identify_home
|
2022-02-26 07:07:18 +00:00
|
|
|
import logger
|
|
|
|
import config
|
|
|
|
from getsocks import get_socks
|
|
|
|
|
|
|
|
from torpeer import TorPeer
|
2022-05-31 00:52:03 +00:00
|
|
|
from torfilepaths import control_socket, peer_database_file
|
2022-02-26 07:07:18 +00:00
|
|
|
|
|
|
|
bootstrap_file = f'{os.path.dirname(os.path.realpath(__file__))}/bootstrap.txt'
|
|
|
|
|
2022-03-20 18:00:33 +00:00
|
|
|
|
2022-05-31 00:52:03 +00:00
|
|
|
def load_existing_peers(callback: Callable):
|
|
|
|
"""Load peers saved to disk"""
|
|
|
|
peer_address: str = ''
|
|
|
|
|
|
|
|
# the peers here are saved on clean shutdown
|
|
|
|
with shelve.open(peer_database_file, 'r') as tor_peer_db:
|
|
|
|
peer_address: str = tor_peer_db.nextkey()
|
|
|
|
while peer_address:
|
|
|
|
Thread(
|
|
|
|
target=callback,
|
|
|
|
args=[tor_peer_db[peer_address]],
|
|
|
|
name=f'{peer_address} connection attempt',
|
|
|
|
daemon=True).start()
|
|
|
|
|
|
|
|
|
2022-02-26 07:07:18 +00:00
|
|
|
def on_bootstrap(api, data):
|
|
|
|
|
2022-05-31 00:52:03 +00:00
|
|
|
callback_func = data['callback']
|
|
|
|
|
2022-02-26 07:07:18 +00:00
|
|
|
try:
|
2022-05-31 00:52:03 +00:00
|
|
|
load_existing_peers(callback_func)
|
2022-06-05 20:11:53 +00:00
|
|
|
except dbm.error:
|
2022-05-31 00:52:03 +00:00
|
|
|
try:
|
|
|
|
with open(bootstrap_file, 'r') as bootstrap_file_obj:
|
|
|
|
bootstrap_nodes = set(bootstrap_file_obj.read().split(','))
|
|
|
|
except FileNotFoundError:
|
|
|
|
bootstrap_nodes = set()
|
2022-08-03 02:06:30 +00:00
|
|
|
except Exception as _:
|
2022-06-05 20:11:53 +00:00
|
|
|
logger.warn(traceback.format_exc(), terminal=True)
|
2022-05-31 00:52:03 +00:00
|
|
|
return
|
|
|
|
else:
|
|
|
|
return
|
2022-02-26 07:07:18 +00:00
|
|
|
|
|
|
|
while not os.path.exists(control_socket):
|
|
|
|
sleep(0.1)
|
|
|
|
|
|
|
|
while not config.get('tor.transport_address'):
|
|
|
|
sleep(1)
|
2022-08-03 05:23:22 +00:00
|
|
|
try:
|
|
|
|
config.reload()
|
|
|
|
except Exception:
|
|
|
|
logger.error(traceback.format_exc(), terminal=True)
|
2022-02-26 07:07:18 +00:00
|
|
|
|
|
|
|
socks_address, socks_port = get_socks()[0]
|
|
|
|
|
|
|
|
for address in bootstrap_nodes:
|
2022-08-03 02:06:30 +00:00
|
|
|
if address == config.get('tor.transport_address').replace('.onion', '') or not address:
|
2022-02-26 07:07:18 +00:00
|
|
|
continue
|
2022-03-20 18:00:33 +00:00
|
|
|
assert not address.endswith('.onion')
|
|
|
|
address += '.onion'
|
2022-02-26 07:07:18 +00:00
|
|
|
# Tell the gossip logic that this peer is ready to connect
|
|
|
|
# it will add it to data['peer_set'] if it responds to ping
|
|
|
|
Thread(
|
2022-05-31 00:52:03 +00:00
|
|
|
target=callback_func,
|
2022-03-21 06:03:53 +00:00
|
|
|
args=[TorPeer(socks_address, socks_port, address)],
|
2022-02-26 07:07:18 +00:00
|
|
|
daemon=True).start()
|
|
|
|
|