Load and save tor peers
This commit is contained in:
parent
158178d6fc
commit
421c6da25a
@ -97,10 +97,11 @@ if config.get('advanced.security_auditing', True):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def onionr_main():
|
def onionr_main():
|
||||||
"""Onionr entrypoint, start command processor"""
|
"""Onionr entrypoint, start command processor
|
||||||
|
|
||||||
|
Entrypoint for daemon is commands/daemonlaunch/__init__.py
|
||||||
|
"""
|
||||||
parser.register()
|
parser.register()
|
||||||
|
|
||||||
|
|
||||||
|
@ -101,8 +101,9 @@ def stream_from_peers():
|
|||||||
raise
|
raise
|
||||||
# Tell them to keep streaming
|
# Tell them to keep streaming
|
||||||
sock.sendall(int(1).to_bytes(1, 'big'))
|
sock.sendall(int(1).to_bytes(1, 'big'))
|
||||||
except BrokenPipeError:
|
except (BrokenPipeError, TimeoutError) as e:
|
||||||
pass
|
logger.info(f"{e} when streaming peers", terminal=True)
|
||||||
|
logger.debug(traceback.format_exc())
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.warn(traceback.format_exc(), terminal=True)
|
logger.warn(traceback.format_exc(), terminal=True)
|
||||||
finally:
|
finally:
|
||||||
|
@ -117,6 +117,7 @@ def daemon():
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
apiservers.private_api.start()
|
apiservers.private_api.start()
|
||||||
|
events.event('shutdown', threaded=False)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -1,26 +1,55 @@
|
|||||||
|
import shelve
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
from time import sleep
|
from time import sleep
|
||||||
import os
|
import os
|
||||||
|
import traceback
|
||||||
|
from typing import Callable
|
||||||
|
|
||||||
from gossip.peer import Peer
|
from gossip.peer import Peer
|
||||||
from gossip.peerset import gossip_peer_set
|
from gossip.peerset import gossip_peer_set
|
||||||
|
from utils.identifyhome import identify_home
|
||||||
import logger
|
import logger
|
||||||
import config
|
import config
|
||||||
from getsocks import get_socks
|
from getsocks import get_socks
|
||||||
|
|
||||||
from torpeer import TorPeer
|
from torpeer import TorPeer
|
||||||
from torfilepaths import control_socket
|
from torfilepaths import control_socket, peer_database_file
|
||||||
|
|
||||||
bootstrap_file = f'{os.path.dirname(os.path.realpath(__file__))}/bootstrap.txt'
|
bootstrap_file = f'{os.path.dirname(os.path.realpath(__file__))}/bootstrap.txt'
|
||||||
|
|
||||||
|
|
||||||
|
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):
|
def on_bootstrap(api, data):
|
||||||
|
|
||||||
|
callback_func = data['callback']
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(bootstrap_file, 'r') as bootstrap_file_obj:
|
load_existing_peers(callback_func)
|
||||||
bootstrap_nodes = set(bootstrap_file_obj.read().split(','))
|
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
bootstrap_nodes = set()
|
try:
|
||||||
|
with open(bootstrap_file, 'r') as bootstrap_file_obj:
|
||||||
|
bootstrap_nodes = set(bootstrap_file_obj.read().split(','))
|
||||||
|
except FileNotFoundError:
|
||||||
|
bootstrap_nodes = set()
|
||||||
|
except Exception as e:
|
||||||
|
logger.warn(traceback.format_exc())
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
return
|
||||||
|
|
||||||
while not os.path.exists(control_socket):
|
while not os.path.exists(control_socket):
|
||||||
sleep(0.1)
|
sleep(0.1)
|
||||||
@ -39,7 +68,7 @@ def on_bootstrap(api, data):
|
|||||||
# Tell the gossip logic that this peer is ready to connect
|
# Tell the gossip logic that this peer is ready to connect
|
||||||
# it will add it to data['peer_set'] if it responds to ping
|
# it will add it to data['peer_set'] if it responds to ping
|
||||||
Thread(
|
Thread(
|
||||||
target=data['callback'],
|
target=callback_func,
|
||||||
args=[TorPeer(socks_address, socks_port, address)],
|
args=[TorPeer(socks_address, socks_port, address)],
|
||||||
daemon=True).start()
|
daemon=True).start()
|
||||||
|
|
||||||
|
@ -0,0 +1 @@
|
|||||||
|
b3u6g7syd6ddicwoxe7ydelqnldyr6g5skvvmjzgh6duwjk6jhv2ixqd
|
@ -30,6 +30,7 @@ from torfilepaths import control_socket
|
|||||||
|
|
||||||
from bootstrap import on_bootstrap
|
from bootstrap import on_bootstrap
|
||||||
from announce import on_announce_rec
|
from announce import on_announce_rec
|
||||||
|
from shutdown import on_shutdown_event
|
||||||
|
|
||||||
"""
|
"""
|
||||||
This program is free software: you can redistribute it and/or modify
|
This program is free software: you can redistribute it and/or modify
|
||||||
@ -92,7 +93,8 @@ def on_gossip_start(api, data: Set[Peer] = None):
|
|||||||
try:
|
try:
|
||||||
add_onion_resp = controller.create_ephemeral_hidden_service(
|
add_onion_resp = controller.create_ephemeral_hidden_service(
|
||||||
{'80': f'unix:{gossip_server_socket_file}'},
|
{'80': f'unix:{gossip_server_socket_file}'},
|
||||||
key_content=key, key_type='ED25519-V3', detached=True, await_publication=True)
|
key_content=key, key_type='ED25519-V3',
|
||||||
|
detached=True, await_publication=True)
|
||||||
except stem.ProtocolError:
|
except stem.ProtocolError:
|
||||||
logger.error(
|
logger.error(
|
||||||
"Could not start Tor transport. Try restarting Onionr",
|
"Could not start Tor transport. Try restarting Onionr",
|
||||||
|
12
static-data/default-plugins/tor/shutdown.py
Normal file
12
static-data/default-plugins/tor/shutdown.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import shelve
|
||||||
|
from gossip.peerset import gossip_peer_set
|
||||||
|
|
||||||
|
from torpeer import TorPeer
|
||||||
|
from torfilepaths import peer_database_file
|
||||||
|
|
||||||
|
|
||||||
|
def on_shutdown_event(api, data=None):
|
||||||
|
with shelve.open(peer_database_file, 'c') as db:
|
||||||
|
for peer in gossip_peer_set:
|
||||||
|
if isinstance(peer, TorPeer):
|
||||||
|
db[peer.onion_address] = peer
|
@ -1,3 +1,4 @@
|
|||||||
from utils.identifyhome import identify_home
|
from utils.identifyhome import identify_home
|
||||||
control_socket = f'{identify_home()}/torcontrol.sock'
|
control_socket = f'{identify_home()}/torcontrol.sock'
|
||||||
tor_data_dir = f'{identify_home()}/tordata'
|
tor_data_dir = f'{identify_home()}/tordata'
|
||||||
|
peer_database_file = f'{identify_home()}/tor-peers'
|
||||||
|
Loading…
Reference in New Issue
Block a user