From faf42071eb38f67420697e856c2eb16333dc00b7 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Sun, 31 Mar 2019 12:16:09 -0500 Subject: [PATCH] plugin example of connecting to dir conn --- onionr/onionrservices/bootstrapservice.py | 4 ++-- onionr/onionrservices/connectionserver.py | 6 +++++ .../default-plugins/clandestine/main.py | 24 ++++++++++++++++++- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/onionr/onionrservices/bootstrapservice.py b/onionr/onionrservices/bootstrapservice.py index a2952563..160b716d 100644 --- a/onionr/onionrservices/bootstrapservice.py +++ b/onionr/onionrservices/bootstrapservice.py @@ -39,8 +39,8 @@ def bootstrap_client_service(peer, core_inst=None, bootstrap_timeout=300): bootstrap_app = Flask(__name__) http_server = WSGIServer(('127.0.0.1', bootstrap_port), bootstrap_app, log=None) try: - core_inst.onionrInst.communicatorInst - except AttributeError: + assert core_inst.onionrInst.communicatorInst is not None + except (AttributeError, AssertionError) as e: pass else: core_inst.onionrInst.communicatorInst.service_greenlets.append(http_server) diff --git a/onionr/onionrservices/connectionserver.py b/onionr/onionrservices/connectionserver.py index ad34cae1..dea9f393 100644 --- a/onionr/onionrservices/connectionserver.py +++ b/onionr/onionrservices/connectionserver.py @@ -50,6 +50,12 @@ class ConnectionServer: @service_app.route('/ping') def get_ping(): return "pong!" + + @service_app.route('/shutdown') + def shutdown_server(): + core_inst.onionrInst.communicatorInst.service_greenlets.remove(http_server) + http_server.stop() + return Response('goodbye') @service_app.after_request def afterReq(resp): diff --git a/onionr/static-data/default-plugins/clandestine/main.py b/onionr/static-data/default-plugins/clandestine/main.py index 9a1e75a2..3fab2dd0 100755 --- a/onionr/static-data/default-plugins/clandestine/main.py +++ b/onionr/static-data/default-plugins/clandestine/main.py @@ -20,8 +20,9 @@ # Imports some useful libraries import locale, sys, os - locale.setlocale(locale.LC_ALL, '') +import onionrservices, logger +from onionrservices import bootstrapservice plugin_name = 'clandestine' PLUGIN_VERSION = '0.0.0' @@ -31,9 +32,29 @@ from . import controlapi, peerserver flask_blueprint = controlapi.flask_blueprint direct_blueprint = peerserver.direct_blueprint +def exit_with_error(text=''): + if text != '': + logger.error(text) + sys.exit(1) + class Clandestine: def __init__(self, pluginapi): self.myCore = pluginapi.get_core() + + def create(self): + try: + peer = sys.argv[2] + if not self.myCore._utils.validatePubKey(peer): + exit_with_error('Invalid public key specified') + except IndexError: + exit_with_error('You must specify a peer public key') + + # Ask peer for transport address by creating block for them + peer_transport_address = bootstrapservice.bootstrap_client_service(peer, self.myCore) + + print(peer_transport_address) + if self.myCore._utils.doGetRequest('http://%s/ping' % (peer_transport_address,), ignoreAPI=True, port=self.myCore.config.get('tor.socksport')) == 'pong!': + print('connected', peer_transport_address) def on_init(api, data = None): ''' @@ -44,4 +65,5 @@ def on_init(api, data = None): pluginapi = api chat = Clandestine(pluginapi) + api.commands.register(['clandestine'], chat.create) return