plugin example of connecting to dir conn

This commit is contained in:
Kevin Froman 2019-03-31 12:16:09 -05:00
parent 063330cfee
commit faf42071eb
3 changed files with 31 additions and 3 deletions

View File

@ -39,8 +39,8 @@ def bootstrap_client_service(peer, core_inst=None, bootstrap_timeout=300):
bootstrap_app = Flask(__name__) bootstrap_app = Flask(__name__)
http_server = WSGIServer(('127.0.0.1', bootstrap_port), bootstrap_app, log=None) http_server = WSGIServer(('127.0.0.1', bootstrap_port), bootstrap_app, log=None)
try: try:
core_inst.onionrInst.communicatorInst assert core_inst.onionrInst.communicatorInst is not None
except AttributeError: except (AttributeError, AssertionError) as e:
pass pass
else: else:
core_inst.onionrInst.communicatorInst.service_greenlets.append(http_server) core_inst.onionrInst.communicatorInst.service_greenlets.append(http_server)

View File

@ -50,6 +50,12 @@ class ConnectionServer:
@service_app.route('/ping') @service_app.route('/ping')
def get_ping(): def get_ping():
return "pong!" 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 @service_app.after_request
def afterReq(resp): def afterReq(resp):

View File

@ -20,8 +20,9 @@
# Imports some useful libraries # Imports some useful libraries
import locale, sys, os import locale, sys, os
locale.setlocale(locale.LC_ALL, '') locale.setlocale(locale.LC_ALL, '')
import onionrservices, logger
from onionrservices import bootstrapservice
plugin_name = 'clandestine' plugin_name = 'clandestine'
PLUGIN_VERSION = '0.0.0' PLUGIN_VERSION = '0.0.0'
@ -31,9 +32,29 @@ from . import controlapi, peerserver
flask_blueprint = controlapi.flask_blueprint flask_blueprint = controlapi.flask_blueprint
direct_blueprint = peerserver.direct_blueprint direct_blueprint = peerserver.direct_blueprint
def exit_with_error(text=''):
if text != '':
logger.error(text)
sys.exit(1)
class Clandestine: class Clandestine:
def __init__(self, pluginapi): def __init__(self, pluginapi):
self.myCore = pluginapi.get_core() 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): def on_init(api, data = None):
''' '''
@ -44,4 +65,5 @@ def on_init(api, data = None):
pluginapi = api pluginapi = api
chat = Clandestine(pluginapi) chat = Clandestine(pluginapi)
api.commands.register(['clandestine'], chat.create)
return return