bootstrap logic separated out of plugin and announcement logic mostly completed

This commit is contained in:
Kevin F 2022-02-26 01:07:18 -06:00
parent 34f9ffbf6b
commit 5337b0aba4
7 changed files with 94 additions and 50 deletions

View File

@ -11,6 +11,7 @@ if TYPE_CHECKING:
from onionrthreads import add_onionr_thread from onionrthreads import add_onionr_thread
import onionrplugins import onionrplugins
from .connectpeer import connect_peer
from .client import gossip_client from .client import gossip_client
from .server import gossip_server from .server import gossip_server
from .commands import GossipCommands from .commands import GossipCommands
@ -46,5 +47,5 @@ def start_gossip_threads(
add_onionr_thread( add_onionr_thread(
gossip_client, 1, peer_set, block_queue, seed, initial_sleep=0) gossip_client, 1, peer_set, block_queue, seed, initial_sleep=0)
onionrplugins.events.event('gossip_start', data=peer_set, threaded=True) onionrplugins.events.event('gossip_start', data=peer_set, threaded=True)
sleep(4) onionrplugins.events.event(
onionrplugins.events.event('bootstrap', data=peer_set) 'bootstrap', data={'peer_set': peer_set, 'callback': connect_peer})

View File

@ -20,7 +20,7 @@ from gossip.phase import DandelionPhase
from onionrthreads import add_onionr_thread from onionrthreads import add_onionr_thread
from .announce import do_announce from .announce import do_announce
from .peerexchange import get_new_peers #from .peerexchange import get_new_peers
""" """
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
@ -54,9 +54,11 @@ def gossip_client(
data={'peers': peer_set, 'new_peers': new_peer_set}) data={'peers': peer_set, 'new_peers': new_peer_set})
add_onionr_thread(do_announce, 3600, peer_set, initial_sleep=10) add_onionr_thread(do_announce, 3600, peer_set, initial_sleep=10)
"""
add_onionr_thread( add_onionr_thread(
get_new_peers, get_new_peers,
1200, peer_set, _trigger_new_peers_event, initial_sleep=5) 1200, peer_set, _trigger_new_peers_event, initial_sleep=5)
"""
dandelion_phase = DandelionPhase(dandelion_seed, 30) dandelion_phase = DandelionPhase(dandelion_seed, 30)

11
src/gossip/connectpeer.py Normal file
View File

@ -0,0 +1,11 @@
import logger
def connect_peer(peer_set, peer):
try:
peer.get_socket()
except Exception:
logger.warn(f"Could not connect to {peer.transport_address}")
else:
peer_set.add(peer)
logger.info(f"connected to {peer.transport_address}")

View File

@ -3,6 +3,7 @@ from typing import TYPE_CHECKING
from typing import Set from typing import Set
from queue import Queue from queue import Queue
from .connectpeer import connect_peer
from onionrplugins import onionrevents from onionrplugins import onionrevents
@ -53,8 +54,10 @@ def gossip_server(
address = await reader.read(56) address = await reader.read(56)
onionrevents.event( onionrevents.event(
'announce_rec', 'announce_rec',
data={'peer_set': peer_set, 'address': address}, data={'peer_set': peer_set,
threaded=False) 'address': address,
'callback': connect_peer},
threaded=True)
writer.write(int(1).to_bytes(1, 'big')) writer.write(int(1).to_bytes(1, 'big'))
await asyncio.wait_for(_read_announce(), 10) await asyncio.wait_for(_read_announce(), 10)

View File

@ -0,0 +1,25 @@
import config
import logger
from getsocks import get_socks
from torpeer import TorPeer
def on_announce_rec(api, data=None):
socks_address, socks_port = get_socks()[0]
announced = data['address']
try:
announced = announced.decode('utf-8')
except AttributeError:
pass
if announced == config.get('tor.transport_address'):
logger.warn("Recieved announcement for our own node, which shouldnt happen")
return
announced += '.onion'
data['callback'](
data['peer_set'],
TorPeer(socks_address, socks_port, announced))

View File

@ -0,0 +1,44 @@
from threading import Thread
from time import sleep
import os
from gossip.peer import Peer
import logger
import config
from getsocks import get_socks
from torpeer import TorPeer
from torfilepaths import control_socket
bootstrap_file = f'{os.path.dirname(os.path.realpath(__file__))}/bootstrap.txt'
def on_bootstrap(api, data):
try:
with open(bootstrap_file, 'r') as bootstrap_file_obj:
bootstrap_nodes = set(bootstrap_file_obj.read().split(','))
except FileNotFoundError:
bootstrap_nodes = set()
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:
if address == config.get('tor.transport_address'):
continue
if 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(
target=data['callback'],
args=[data['peer_set'], TorPeer(socks_address, socks_port, address)],
daemon=True).start()

View File

@ -28,7 +28,9 @@ sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))
import starttor import starttor
from torpeer import TorPeer from torpeer import TorPeer
from torfilepaths import control_socket from torfilepaths import control_socket
from getsocks import get_socks
from bootstrap import on_bootstrap
from announce import on_announce_rec
""" """
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
@ -50,9 +52,6 @@ plugin_name = 'tor'
PLUGIN_VERSION = '0.0.0' PLUGIN_VERSION = '0.0.0'
bootstrap_file = f'{os.path.dirname(os.path.realpath(__file__))}/bootstrap.txt'
class OnionrTor: class OnionrTor:
def __init__(self): def __init__(self):
return return
@ -70,47 +69,6 @@ def on_get_our_transport(api, data=None):
callback_func(for_peer, config.get('tor.transport_address')) callback_func(for_peer, config.get('tor.transport_address'))
def on_announce_rec(api, data=None):
print("got announce rec event")
def on_bootstrap(api, data: Set[Peer] = None):
bootstrap_nodes: Set[str]
peers = data
try:
with open(bootstrap_file, 'r') as bootstrap_file_obj:
bootstrap_nodes = set(bootstrap_file_obj.read().split(','))
except FileNotFoundError:
bootstrap_nodes = set()
while not os.path.exists(control_socket):
sleep(0.1)
socks_address, socks_port = get_socks()[0]
sleep(5)
for transport_address in bootstrap_nodes:
config.reload()
if config.get('tor.transport_address') == transport_address:
# ignore if its our own
continue
if not transport_address.endswith('.onion'):
transport_address += '.onion'
tor_peer = TorPeer(socks_address, socks_port, transport_address)
try:
tor_peer.get_socket()
except Exception:
logger.warn(
f"Could not connnect to Tor peer {transport_address} " +
"see logs for more info",
terminal=True)
logger.warn(traceback.format_exc())
continue
peers.add(tor_peer)
logger.info(f"Connected to {len(peers)} Tor peers", terminal=True)
def on_gossip_start(api, data: Set[Peer] = None): def on_gossip_start(api, data: Set[Peer] = None):
# We don't do gossip logic # We don't do gossip logic