Tor can now initialize peers

This commit is contained in:
Kevin F 2022-02-21 15:15:26 -06:00
parent d48af45210
commit 8eec2167c8
7 changed files with 32 additions and 20 deletions

View File

@ -1,3 +1,4 @@
from time import sleep
from typing import TYPE_CHECKING, Set
from os import urandom
import queue
@ -45,3 +46,5 @@ def start_gossip_threads(
add_onionr_thread(
gossip_client, 1, peer_set, block_queue, seed, initial_sleep=0)
onionrplugins.events.event('gossip_start', data=peer_set, threaded=True)
sleep(4)
onionrplugins.events.event('bootstrap', data=peer_set)

View File

@ -33,4 +33,4 @@ def gossip_client(
peer_set: Set['Peer'],
block_queue: Queue['Block'],
dandelion_seed: bytes):
onionrplugins.events.event('')
return

View File

@ -5,4 +5,5 @@ from torfilepaths import control_socket
def get_socks():
with Controller.from_socket_file(control_socket) as controller:
controller.authenticate()
return controller.get_listeners('SOCKS')

View File

@ -6,9 +6,11 @@ This default plugin handles "flow" messages
import sys
import os
import locale
from time import sleep
import traceback
from typing import Set, TYPE_CHECKING
import base64
from threading import Thread
import stem
from stem.control import Controller
@ -18,7 +20,6 @@ from utils import readstatic
import config
from filepaths import gossip_server_socket_file
from gossip.peer import Peer
locale.setlocale(locale.LC_ALL, '')
@ -51,6 +52,7 @@ PLUGIN_VERSION = '0.0.0'
bootstrap_file = f'{os.path.dirname(os.path.realpath(__file__))}/bootstrap.txt'
class OnionrTor:
def __init__(self):
return
@ -65,20 +67,28 @@ def on_bootstrap(api, data: Set[Peer] = None):
bootstrap_nodes: Set[str]
peers = data
socks_address, socks_port = get_socks()
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(10)
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:
s = tor_peer.get_socket()
tor_peer.get_socket()
except Exception as e:
logger.warn(
f"Could not connnect to Tor peer {transport_address} " +
@ -87,12 +97,12 @@ def on_bootstrap(api, data: Set[Peer] = None):
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):
# We don't do gossip logic
starttor.start_tor()
with Controller.from_socket_file(control_socket) as controller:
@ -105,15 +115,16 @@ def on_gossip_start(api, data: Set[Peer] = None):
if not key:
add_onion_resp = controller.create_ephemeral_hidden_service(
{'80': f'unix:{gossip_server_socket_file}'},
key_content='BEST', key_type='NEW')
key_content='BEST', key_type='NEW', detached=True)
config.set('tor.key', add_onion_resp.private_key, savefile=True)
new_address = 'Generated '
config.set('tor.transport_address', add_onion_resp.service_id)
config.set('tor.transport_address', add_onion_resp.service_id,
savefile=True)
else:
try:
add_onion_resp = controller.create_ephemeral_hidden_service(
{'80': f'unix:{gossip_server_socket_file}'},
key_content=key, key_type='ED25519-V3')
key_content=key, key_type='ED25519-V3', detached=True)
except stem.ProtocolError:
logger.error(
"Could not start Tor transport. Try restarting Onionr",
@ -124,4 +135,3 @@ def on_gossip_start(api, data: Set[Peer] = None):
f'{new_address}Tor transport address {add_onion_resp.service_id}' +
'.onion',
terminal=True)

View File

@ -1,3 +1,5 @@
from time import sleep
import stem.process
from utils.identifyhome import identify_home
@ -5,14 +7,13 @@ from utils.identifyhome import identify_home
from torfilepaths import control_socket
from torfilepaths import tor_data_dir
def start_tor():
def start_tor():
tor_process = stem.process.launch_tor_with_config(
config={
'SocksPort': 'auto OnionTrafficOnly',
'DataDirectory': tor_data_dir,
'ControlSocket': control_socket,
},
completion_percent=50,
take_ownership=True
completion_percent=100
)

View File

@ -10,6 +10,6 @@ class TorPeer:
def get_socket(self) -> socks.socksocket:
s = socks.socksocket()
s.set_proxy(socks.SOCKS4, self.socks_host, self.socks_host, rdns=True)
s.set_proxy(socks.SOCKS4, self.socks_host, self.socks_port, rdns=True)
s.connect((self.onion_address, 80))
return s

View File

@ -1,6 +1,6 @@
{
"advanced": {
"security_auditing": true
"security_auditing": false
},
"allocations": {
"disk": 1073741824
@ -12,7 +12,6 @@
"dev_mode": false,
"display_header": true,
"ephemeral_tunnels": false,
"hide_created_blocks": true,
"insert_deniable_blocks": true,
"max_block_age": 2678400,
"minimum_block_pow": 5,
@ -22,9 +21,7 @@
"security_level": 0,
"show_notifications": true,
"store_plaintext_blocks": true,
"upload_mixing": false,
"use_bootstrap_list": true,
"use_subprocess_pow_if_possible": true
"use_bootstrap_list": true
},
"log": {
"console": {
@ -33,7 +30,7 @@
},
"file": {
"output": true,
"remove_on_exit": true
"remove_on_exit": false
},
"verbosity": "default"
},