From 705962e9e786d9720be133acfc2d7399bd5730a8 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Sun, 4 Aug 2019 23:08:56 -0500 Subject: [PATCH] progress removing onionr.py --- onionr/apiservers/private/__init__.py | 3 +- onionr/apiservers/public/__init__.py | 1 + .../onionrcommunicatortimers.py | 3 +- onionr/httpapi/miscclientapi/endpoints.py | 6 +- onionr/onionrcommands/daemonlaunch.py | 2 + onionr/onionrcommands/openwebinterface.py | 5 +- onionr/onionrcommands/parser/arguments.py | 5 +- onionr/onionrevents.py | 2 +- onionr/onionrpluginapi.py | 94 +------------------ onionr/serializeddata.py | 1 - .../static-data/default-plugins/cliui/main.py | 2 - .../default-plugins/encrypt/main.py | 12 ++- 12 files changed, 26 insertions(+), 110 deletions(-) diff --git a/onionr/apiservers/private/__init__.py b/onionr/apiservers/private/__init__.py index fe3e67dd..7f475163 100644 --- a/onionr/apiservers/private/__init__.py +++ b/onionr/apiservers/private/__init__.py @@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . ''' -import base64, os +import base64, os, time import flask from gevent.pywsgi import WSGIServer from onionrutils import epoch @@ -40,6 +40,7 @@ class PrivateAPI: This initialization defines all of the API entry points and handlers for the endpoints and errors This also saves the used host (random localhost IP address) to the data folder in host.txt ''' + time.sleep(0.3) self.config = config self.serializer = serializeddata.SerializedData() self.startTime = epoch.get_epoch() diff --git a/onionr/apiservers/public/__init__.py b/onionr/apiservers/public/__init__.py index ee350f85..ad301ec8 100644 --- a/onionr/apiservers/public/__init__.py +++ b/onionr/apiservers/public/__init__.py @@ -38,6 +38,7 @@ class PublicAPI: The new client api server, isolated from the public api ''' def __init__(self): + time.sleep(1) app = flask.Flask('PublicAPI') app.config['MAX_CONTENT_LENGTH'] = 5 * 1024 * 1024 self.i2pEnabled = config.get('i2p.host', False) diff --git a/onionr/communicatorutils/onionrcommunicatortimers.py b/onionr/communicatorutils/onionrcommunicatortimers.py index b0fac130..a6b8c408 100755 --- a/onionr/communicatorutils/onionrcommunicatortimers.py +++ b/onionr/communicatorutils/onionrcommunicatortimers.py @@ -51,8 +51,7 @@ class OnionrCommunicatorTimers: if self.makeThread: for i in range(self.threadAmount): if self.daemonInstance.threadCounts[self.timerFunction.__name__] >= self.maxThreads: - pass - #logger.debug('%s is currently using the maximum number of threads, not starting another.' % self.timerFunction.__name__, terminal=True) + logger.debug('%s is currently using the maximum number of threads, not starting another.' % self.timerFunction.__name__, terminal=True) else: self.daemonInstance.threadCounts[self.timerFunction.__name__] += 1 newThread = threading.Thread(target=self.timerFunction, args=self.args, daemon=True) diff --git a/onionr/httpapi/miscclientapi/endpoints.py b/onionr/httpapi/miscclientapi/endpoints.py index 8459c67f..467ba6e3 100644 --- a/onionr/httpapi/miscclientapi/endpoints.py +++ b/onionr/httpapi/miscclientapi/endpoints.py @@ -21,6 +21,7 @@ from flask import Response, Blueprint, request, send_from_directory, abort from httpapi import apiutils import onionrcrypto, config from netcontroller import NetController +from serializeddata import SerializedData pub_key = onionrcrypto.pub_key class PrivateEndpoints: def __init__(self, client_api): @@ -97,8 +98,9 @@ class PrivateEndpoints: #return Response("disabled") while True: try: - return Response(client_api.serializer.getStats()) - except AttributeError: + return Response(client_api._too_many.get(SerializedData).getStats()) + except AttributeError as e: + print("damn", e) pass @private_endpoints_bp.route('/getuptime') diff --git a/onionr/onionrcommands/daemonlaunch.py b/onionr/onionrcommands/daemonlaunch.py index 30bd4138..23e57f9e 100755 --- a/onionr/onionrcommands/daemonlaunch.py +++ b/onionr/onionrcommands/daemonlaunch.py @@ -33,6 +33,7 @@ from etc import onionrvalues, cleanup from onionrcrypto import getourkeypair from utils import hastor, logoheader from . import version +import serializeddata def _proper_shutdown(): localcommand.local_command('shutdown') @@ -57,6 +58,7 @@ def daemon(): Thread(target=shared_state.get(apiservers.ClientAPI).start, daemon=True).start() Thread(target=shared_state.get(apiservers.PublicAPI).start, daemon=True).start() + shared_state.get(serializeddata.SerializedData) shared_state.share_object() # share the parent object to the threads apiHost = '' diff --git a/onionr/onionrcommands/openwebinterface.py b/onionr/onionrcommands/openwebinterface.py index b0cacfa4..c4752b5d 100755 --- a/onionr/onionrcommands/openwebinterface.py +++ b/onionr/onionrcommands/openwebinterface.py @@ -20,12 +20,13 @@ import webbrowser import logger from onionrutils import getclientapiserver -def open_home(o_inst): +import config +def open_home(): try: url = getclientapiserver.get_client_API_server() except FileNotFoundError: logger.error('Onionr seems to not be running (could not get api host)', terminal=True) else: - url = 'http://%s/#%s' % (url, o_inst.config.get('client.webpassword')) + url = 'http://%s/#%s' % (url, config.get('client.webpassword')) logger.info('If Onionr does not open automatically, use this URL: ' + url, terminal=True) webbrowser.open_new_tab(url) \ No newline at end of file diff --git a/onionr/onionrcommands/parser/arguments.py b/onionr/onionrcommands/parser/arguments.py index 7479a476..e9a1df3f 100644 --- a/onionr/onionrcommands/parser/arguments.py +++ b/onionr/onionrcommands/parser/arguments.py @@ -1,4 +1,4 @@ -from .. import onionrstatistics, version, daemonlaunch, keyadders +from .. import onionrstatistics, version, daemonlaunch, keyadders, openwebinterface import onionrexceptions import onionrevents as events def get_arguments(): @@ -8,7 +8,8 @@ def get_arguments(): ('version'): version.version, ('start', 'daemon'): daemonlaunch.start, ('stop', 'kill'): daemonlaunch.kill_daemon, - ('add-address', 'addaddress', 'addadder'): keyadders.add_address + ('add-address', 'addaddress', 'addadder'): keyadders.add_address, + ('openhome', 'gui', 'openweb', 'open-home', 'open-web'): openwebinterface.open_home } return args diff --git a/onionr/onionrevents.py b/onionr/onionrevents.py index 62e8dec7..d0187188 100755 --- a/onionr/onionrevents.py +++ b/onionr/onionrevents.py @@ -60,7 +60,7 @@ def call(plugin, event_name, data = None, pluginapi = None): try: attribute = 'on_' + str(event_name).lower() if pluginapi is None: - pluginapi = get_pluginapi() + pluginapi = get_pluginapi(data) if hasattr(plugin, attribute): return getattr(plugin, attribute)(pluginapi, data) diff --git a/onionr/onionrpluginapi.py b/onionr/onionrpluginapi.py index c25e9984..0cce713c 100755 --- a/onionr/onionrpluginapi.py +++ b/onionr/onionrpluginapi.py @@ -21,28 +21,6 @@ import onionrplugins, logger from onionrutils import localcommand from coredb import daemonqueue -class DaemonAPI: - def __init__(self, pluginapi): - self.pluginapi = pluginapi - - def start(self): - self.pluginapi.get_onionr().daemon() - - return - - def stop(self): - self.pluginapi.get_onionr().killDaemon() - - return - - def queue(self, command, data = ''): - return daemonqueue.daemon_queue_add(command, data) - - def local_command(self, command): - return localcommand.local_command(command) - - def queue_pop(self): - return daemonqueue.daemon_queue() class PluginAPI: def __init__(self, pluginapi): @@ -64,7 +42,7 @@ class PluginAPI: onionrplugins.disable(name) def event(self, name, data = {}): - events.event(name, data = data, onionr = self.pluginapi.get_onionr()) + events.event(name, data = data) def is_enabled(self, name): return onionrplugins.is_enabled(name) @@ -85,72 +63,13 @@ class CommandAPI: def __init__(self, pluginapi): self.pluginapi = pluginapi - def register(self, names, call = None): - if isinstance(names, str): - names = [names] - - for name in names: - self.pluginapi.get_onionr().addCommand(name, call) - - def unregister(self, names): - if isinstance(names, str): - names = [names] - - for name in names: - self.pluginapi.get_onionr().delCommand(name) - - return - - def register_help(self, names, description): - if isinstance(names, str): - names = [names] - - for name in names: - self.pluginapi.get_onionr().addHelp(name, description) - - return - - def unregister_help(self, names): - if isinstance(names, str): - names = [names] - - for name in names: - self.pluginapi.get_onionr().delHelp(name) - - return - def call(self, name): self.pluginapi.get_onionr().execute(name) - return - - def get_commands(self): - return self.pluginapi.get_onionr().getCommands() - -class WebAPI: - def __init__(self, pluginapi): - self.pluginapi = pluginapi - - def register_callback(self, action, callback, scope = 'public'): - return self.pluginapi.get_onionr().api.setCallback(action, callback, scope = scope) - - def unregister_callback(self, action, scope = 'public'): - return self.pluginapi.get_onionr().api.removeCallback(action, scope = scope) - - def get_callback(self, action, scope = 'public'): - return self.pluginapi.get_onionr().api.getCallback(action, scope= scope) - - def get_callbacks(self, scope = None): - return self.pluginapi.get_onionr().api.getCallbacks(scope = scope) - class SharedAPI: def __init__(self, data): self.data = data - - self.daemon = DaemonAPI(self) self.plugins = PluginAPI(self) - self.commands = CommandAPI(self) - self.web = WebAPI(self) def get_data(self): return self.data @@ -159,13 +78,4 @@ class SharedAPI: return self.daemon def get_pluginapi(self): - return self.plugins - - def get_commandapi(self): - return self.commands - - def get_webapi(self): - return self.web - - def is_development_mode(self): - return self.get_onionr()._developmentMode + return self.plugins \ No newline at end of file diff --git a/onionr/serializeddata.py b/onionr/serializeddata.py index 034e147a..6ec127f9 100755 --- a/onionr/serializeddata.py +++ b/onionr/serializeddata.py @@ -31,7 +31,6 @@ class SerializedData: etc } ''' - self._too_many = None def getStats(self): '''Return statistics about our node''' diff --git a/onionr/static-data/default-plugins/cliui/main.py b/onionr/static-data/default-plugins/cliui/main.py index 888e2491..7ecdac77 100755 --- a/onionr/static-data/default-plugins/cliui/main.py +++ b/onionr/static-data/default-plugins/cliui/main.py @@ -114,6 +114,4 @@ def on_init(api, data = None): # by simply referencing the variable `pluginapi`. pluginapi = api ui = OnionrCLIUI(api) - api.commands.register('interactive', ui.start) - api.commands.register_help('interactive', 'Open the CLI interface') return diff --git a/onionr/static-data/default-plugins/encrypt/main.py b/onionr/static-data/default-plugins/encrypt/main.py index 6bb7346e..cd914c31 100755 --- a/onionr/static-data/default-plugins/encrypt/main.py +++ b/onionr/static-data/default-plugins/encrypt/main.py @@ -107,6 +107,12 @@ class PlainEncryption: logger.info("Message has good signature.", terminal=True) return +def on_decrypt_cmd(api, data=None): + PlainEncryption(api).decrypt() + +def on_encrypt_cmd(api, data=None): + PlainEncryption(api).encrypt() + def on_init(api, data = None): ''' This event is called after Onionr is initialized, but before the command @@ -114,8 +120,4 @@ def on_init(api, data = None): just the client is running. ''' pluginapi = api - encrypt = PlainEncryption(pluginapi) - api.commands.register(['encrypt'], encrypt.encrypt) - api.commands.register(['decrypt'], encrypt.decrypt) - - return + encrypt = PlainEncryption(pluginapi) \ No newline at end of file