work on torgossip

This commit is contained in:
Kevin Froman 2021-02-06 06:35:18 +00:00
parent b720f2f1d5
commit ad103ee8b0
3 changed files with 54 additions and 33 deletions

View File

@ -1,9 +1,14 @@
''' """Onionr - Private P2P Communication.
Onionr - Private P2P Communication
This file deals with configuration management. Onionr events API
''' """
''' from threading import Thread
import config
import logger
import onionrplugins as plugins
from . import onionrpluginapi as pluginapi
"""
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
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
@ -16,22 +21,18 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
''' """
from threading import Thread
import config, logger
import onionrplugins as plugins
from . import onionrpluginapi as pluginapi
def get_pluginapi(data): def get_pluginapi(data):
return pluginapi.SharedAPI(data) return pluginapi.SharedAPI(data)
def __event_caller(event_name, data={}): def __event_caller(event_name, data={}):
''' """
DO NOT call this function, this is for threading code only. DO NOT call this function, this is for threading code only.
Instead, call onionrevents.event Instead, call onionrevents.event
''' """
disabled = config.get('plugins.disabled') disabled = config.get('plugins.disabled')
for plugin in plugins.get_enabled_plugins(): for plugin in plugins.get_enabled_plugins():
if plugin in disabled: continue if plugin in disabled: continue
@ -44,10 +45,9 @@ def __event_caller(event_name, data = {}):
logger.warn('Event "%s" failed for plugin "%s".' % (event_name, plugin), terminal=True) logger.warn('Event "%s" failed for plugin "%s".' % (event_name, plugin), terminal=True)
logger.debug((event_name + ' - ' + plugin + ' - ' + str(e)), terminal=True) logger.debug((event_name + ' - ' + plugin + ' - ' + str(e)), terminal=True)
def event(event_name, data = {}, threaded = True): def event(event_name, data = {}, threaded = True):
''' """Call an event on all plugins (if defined)."""
Calls an event on all plugins (if defined)
'''
if threaded: if threaded:
thread = Thread(target = __event_caller, args = (event_name, data)) thread = Thread(target = __event_caller, args = (event_name, data))
@ -56,10 +56,9 @@ def event(event_name, data = {}, threaded = True):
else: else:
__event_caller(event_name, data) __event_caller(event_name, data)
def call(plugin, event_name, data = None, pluginapi = None): def call(plugin, event_name, data = None, pluginapi = None):
''' """Call an event on a plugin if one is defined."""
Calls an event on a plugin if one is defined
'''
if not plugin is None: if not plugin is None:
try: try:

View File

@ -4,15 +4,21 @@ Torgossip client
Create streams to random peers Create streams to random peers
""" """
from base64 import b32encode
from os import path from os import path
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from random import SystemRandom from random import SystemRandom
import socks as socket
from stem import control
from netcontroller.torcontrol.onionserviceonline import service_online_recently from netcontroller.torcontrol.onionserviceonline import service_online_recently
from netcontroller.torcontrol import torcontroller from netcontroller.torcontrol import torcontroller
from .constants import GOSSIP_PORT
if TYPE_CHECKING: if TYPE_CHECKING:
from .peerdb import TorGossipPeers from .peerdb import TorGossipPeers
from stem.control import Controller
""" """
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
@ -38,16 +44,29 @@ def _add_bootstrap_peers(peer_db: 'TorGossipPeers'):
peer_db.add_peer(peer) peer_db.add_peer(peer)
def _client_pool(shared_state, controller): def _client_pool(shared_state, controller: 'Controller'):
peer_db: 'TorGossipPeers' = shared_state.get_by_string('TorGossipPeers') peer_db: 'TorGossipPeers' = shared_state.get_by_string('TorGossipPeers')
socket_pool = {} socket_pool = {}
socks_port = shared_state.get_by_string('NetController').socksPort
peers = peer_db.get_highest_score_peers(20) peers = peer_db.get_highest_score_peers(20)
SystemRandom().shuffle(peers) SystemRandom().shuffle(peers)
for peer in peers: for peer in peers:
if peer in socket_pool:
continue
if not service_online_recently(controller, peer): if not service_online_recently(controller, peer):
continue continue
s = socket.socksocket()
s.set_proxy(
socket.SOCKS5, '127.0.0.1', socks_port, rdns=True)
try:
socket_pool[peer] = s.connect(
(b32encode(peer).decode().lower() + ".onion", 2021))
except socket.GeneralProxyError:
s.close()
def client_pool(shared_state): def client_pool(shared_state):
controller = torcontroller.get_controller() controller = torcontroller.get_controller()

View File

@ -1,12 +1,14 @@
""" """
Onionr - Private P2P Communication Onionr - Private P2P Communication
This default plugin allows users to encrypt/decrypt messages without using blocks Default plugin which allows users to encrypt/decrypt messages w/o using blocks
""" """
from inspect import trace
import locale import locale
locale.setlocale(locale.LC_ALL, '') locale.setlocale(locale.LC_ALL, '')
import sys import sys
import os import os
import traceback
from threading import Thread from threading import Thread
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__))) sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))
""" """
@ -25,12 +27,14 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
""" """
plugin_name = 'torgossip' plugin_name = 'torgossip'
import logger # noqa
try: try:
from server import start_server from server import start_server
from peerdb import TorGossipPeers from peerdb import TorGossipPeers
from runtest import torgossip_runtest from runtest import torgossip_runtest
except Exception as e: except Exception as _: # noqa
print(repr(e)) logger.error(traceback.format_exc(), terminal=True)
def on_init(api, data=None): def on_init(api, data=None):
@ -40,6 +44,5 @@ def on_init(api, data=None):
shared_state.get(TorGossipPeers) shared_state.get(TorGossipPeers)
Thread(target=start_server, daemon=True, args=[shared_state]).start() Thread(target=start_server, daemon=True, args=[shared_state]).start()