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 typing import TYPE_CHECKING, Set
from os import urandom from os import urandom
import queue import queue
@ -45,3 +46,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('bootstrap', data=peer_set)

View File

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

View File

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

View File

@ -6,9 +6,11 @@ This default plugin handles "flow" messages
import sys import sys
import os import os
import locale import locale
from time import sleep
import traceback import traceback
from typing import Set, TYPE_CHECKING from typing import Set, TYPE_CHECKING
import base64 import base64
from threading import Thread
import stem import stem
from stem.control import Controller from stem.control import Controller
@ -18,7 +20,6 @@ from utils import readstatic
import config import config
from filepaths import gossip_server_socket_file from filepaths import gossip_server_socket_file
from gossip.peer import Peer from gossip.peer import Peer
locale.setlocale(locale.LC_ALL, '') 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' bootstrap_file = f'{os.path.dirname(os.path.realpath(__file__))}/bootstrap.txt'
class OnionrTor: class OnionrTor:
def __init__(self): def __init__(self):
return return
@ -65,20 +67,28 @@ def on_bootstrap(api, data: Set[Peer] = None):
bootstrap_nodes: Set[str] bootstrap_nodes: Set[str]
peers = data peers = data
socks_address, socks_port = get_socks()
try: try:
with open(bootstrap_file, 'r') as bootstrap_file_obj: with open(bootstrap_file, 'r') as bootstrap_file_obj:
bootstrap_nodes = set(bootstrap_file_obj.read().split(',')) bootstrap_nodes = set(bootstrap_file_obj.read().split(','))
except FileNotFoundError: except FileNotFoundError:
bootstrap_nodes = set() 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: 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'): if not transport_address.endswith('.onion'):
transport_address += '.onion' transport_address += '.onion'
tor_peer = TorPeer(socks_address, socks_port, transport_address) tor_peer = TorPeer(socks_address, socks_port, transport_address)
try: try:
s = tor_peer.get_socket() tor_peer.get_socket()
except Exception as e: except Exception as e:
logger.warn( logger.warn(
f"Could not connnect to Tor peer {transport_address} " + 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()) logger.warn(traceback.format_exc())
continue continue
peers.add(tor_peer) 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
starttor.start_tor() starttor.start_tor()
with Controller.from_socket_file(control_socket) as controller: 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: if not key:
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='BEST', key_type='NEW') key_content='BEST', key_type='NEW', detached=True)
config.set('tor.key', add_onion_resp.private_key, savefile=True) config.set('tor.key', add_onion_resp.private_key, savefile=True)
new_address = 'Generated ' 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: else:
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') key_content=key, key_type='ED25519-V3', detached=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",
@ -124,4 +135,3 @@ def on_gossip_start(api, data: Set[Peer] = None):
f'{new_address}Tor transport address {add_onion_resp.service_id}' + f'{new_address}Tor transport address {add_onion_resp.service_id}' +
'.onion', '.onion',
terminal=True) terminal=True)

View File

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

View File

@ -10,6 +10,6 @@ class TorPeer:
def get_socket(self) -> socks.socksocket: def get_socket(self) -> socks.socksocket:
s = 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)) s.connect((self.onion_address, 80))
return s return s

View File

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