diff --git a/src/onionrplugins/onionrevents.py b/src/onionrplugins/onionrevents.py index b0bba935..9a1248cc 100755 --- a/src/onionrplugins/onionrevents.py +++ b/src/onionrplugins/onionrevents.py @@ -1,37 +1,38 @@ -''' - Onionr - Private P2P Communication +"""Onionr - Private P2P Communication. - This file deals with configuration management. -''' -''' - 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 - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -''' +Onionr events API +""" from threading import Thread -import config, logger +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 +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +""" def get_pluginapi(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. Instead, call onionrevents.event - ''' + """ disabled = config.get('plugins.disabled') for plugin in plugins.get_enabled_plugins(): 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.debug((event_name + ' - ' + plugin + ' - ' + str(e)), terminal=True) + def event(event_name, data = {}, threaded = True): - ''' - Calls an event on all plugins (if defined) - ''' + """Call an event on all plugins (if defined).""" if threaded: thread = Thread(target = __event_caller, args = (event_name, data)) @@ -56,10 +56,9 @@ def event(event_name, data = {}, threaded = True): else: __event_caller(event_name, data) + def call(plugin, event_name, data = None, pluginapi = None): - ''' - Calls an event on a plugin if one is defined - ''' + """Call an event on a plugin if one is defined.""" if not plugin is None: try: diff --git a/static-data/default-plugins/torgossip/client.py b/static-data/default-plugins/torgossip/client.py index 169c7d5d..86ad1cce 100644 --- a/static-data/default-plugins/torgossip/client.py +++ b/static-data/default-plugins/torgossip/client.py @@ -4,15 +4,21 @@ Torgossip client Create streams to random peers """ +from base64 import b32encode from os import path from typing import TYPE_CHECKING from random import SystemRandom +import socks as socket +from stem import control + from netcontroller.torcontrol.onionserviceonline import service_online_recently from netcontroller.torcontrol import torcontroller +from .constants import GOSSIP_PORT if TYPE_CHECKING: from .peerdb import TorGossipPeers + from stem.control import Controller """ 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 @@ -38,16 +44,29 @@ def _add_bootstrap_peers(peer_db: 'TorGossipPeers'): 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') socket_pool = {} + socks_port = shared_state.get_by_string('NetController').socksPort peers = peer_db.get_highest_score_peers(20) SystemRandom().shuffle(peers) for peer in peers: + if peer in socket_pool: + continue if not service_online_recently(controller, peer): 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): controller = torcontroller.get_controller() diff --git a/static-data/default-plugins/torgossip/main.py b/static-data/default-plugins/torgossip/main.py index a921bb17..85482678 100755 --- a/static-data/default-plugins/torgossip/main.py +++ b/static-data/default-plugins/torgossip/main.py @@ -1,12 +1,14 @@ """ 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 locale.setlocale(locale.LC_ALL, '') import sys import os +import traceback from threading import Thread sys.path.insert(0, os.path.dirname(os.path.realpath(__file__))) """ @@ -25,12 +27,14 @@ along with this program. If not, see . """ plugin_name = 'torgossip' +import logger # noqa + try: from server import start_server from peerdb import TorGossipPeers from runtest import torgossip_runtest -except Exception as e: - print(repr(e)) +except Exception as _: # noqa + logger.error(traceback.format_exc(), terminal=True) def on_init(api, data=None): @@ -40,6 +44,5 @@ def on_init(api, data=None): shared_state.get(TorGossipPeers) - Thread(target=start_server, daemon=True, args=[shared_state]).start()