Onionr/static-data/default-plugins/tor/bootstrap.py

76 lines
2.1 KiB
Python
Raw Normal View History

2022-05-31 00:52:03 +00:00
import shelve
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
from gossip.peer import Peer
from gossip.peerset import gossip_peer_set
2022-05-31 00:52:03 +00:00
from utils.identifyhome import identify_home
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
bootstrap_file = f'{os.path.dirname(os.path.realpath(__file__))}/bootstrap.txt'
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()
def on_bootstrap(api, data):
2022-05-31 00:52:03 +00:00
callback_func = data['callback']
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
while not os.path.exists(control_socket):
sleep(0.1)
while not config.get('tor.transport_address'):
sleep(1)
config.reload()
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:
continue
assert not address.endswith('.onion')
address += '.onion'
# 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,
args=[TorPeer(socks_address, socks_port, address)],
daemon=True).start()