From 7c57d95d1d07cd28ac69264043e2b567d78e9f20 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Tue, 2 Apr 2019 11:50:09 -0500 Subject: [PATCH] work on direct connections and clandestine --- onionr/api.py | 10 ++++++ onionr/communicatorutils/servicecreator.py | 5 +-- onionr/onionrservices/connectionserver.py | 2 +- .../default-plugins/clandestine/main.py | 34 ++++++++++++++++--- .../default-plugins/clandestine/peerserver.py | 2 ++ 5 files changed, 46 insertions(+), 7 deletions(-) diff --git a/onionr/api.py b/onionr/api.py index 7e552355..3df4741b 100755 --- a/onionr/api.py +++ b/onionr/api.py @@ -318,6 +318,16 @@ class API: @app.route('/friends/', endpoint='friendsindex') def loadContacts(): return send_from_directory('static-data/www/friends/', 'index.html') + + @app.route('/serviceactive/') + def serviceActive(pubkey): + try: + if pubkey in self._core.onionrInst.communicatorInst.active_services: + return Response('true') + except AttributeError as e: + print('attribute error', str(e)) + pass + return Response('false') @app.route('/board/', endpoint='boardContent') def boardContent(path): diff --git a/onionr/communicatorutils/servicecreator.py b/onionr/communicatorutils/servicecreator.py index ecd1543c..fbb4c5b9 100644 --- a/onionr/communicatorutils/servicecreator.py +++ b/onionr/communicatorutils/servicecreator.py @@ -11,8 +11,9 @@ def service_creator(daemon): bl = onionrblockapi.Block(b, core=core, decrypt=True) bs = utils.bytesToStr(bl.bcontent) + '.onion' if utils.validatePubKey(bl.signer) and utils.validateID(bs): + signer = utils.bytesToStr(bl.signer) daemon.active_services.append(b) - daemon.active_services.append(bl.signer) - daemon.services.create_server(bl.signer, bs) + daemon.active_services.append(signer) + daemon.services.create_server(signer, bs) daemon.decrementThreadCount('service_creator') \ No newline at end of file diff --git a/onionr/onionrservices/connectionserver.py b/onionr/onionrservices/connectionserver.py index dea9f393..27a0b55f 100644 --- a/onionr/onionrservices/connectionserver.py +++ b/onionr/onionrservices/connectionserver.py @@ -70,7 +70,7 @@ class ConnectionServer: response = controller.create_ephemeral_hidden_service({80: service_port}, await_publication = True, key_type='NEW', key_content = 'ED25519-V3') self.core_inst.keyStore.put('dc-' + response.service_id, self.core_inst._utils.bytesToStr(peer)) self.core_inst._utils.doPostRequest('http://' + address + '/bs/' + response.service_id, port=socks) - logger.info('hosting on %s with %s' % (response.service_id, peer)) + logger.info('hosting on %s with %s' % (response.service_id, peer)) http_server.serve_forever() self.core_inst.keyStore.delete('dc-' + response.service_id) http_server.stop() \ No newline at end of file diff --git a/onionr/static-data/default-plugins/clandestine/main.py b/onionr/static-data/default-plugins/clandestine/main.py index 3fab2dd0..8f202dac 100755 --- a/onionr/static-data/default-plugins/clandestine/main.py +++ b/onionr/static-data/default-plugins/clandestine/main.py @@ -19,7 +19,7 @@ ''' # Imports some useful libraries -import locale, sys, os +import locale, sys, os, threading, json locale.setlocale(locale.LC_ALL, '') import onionrservices, logger from onionrservices import bootstrapservice @@ -40,7 +40,30 @@ def exit_with_error(text=''): class Clandestine: def __init__(self, pluginapi): self.myCore = pluginapi.get_core() + self.peer = None + self.transport = None + self.shutdown = False + def _sender_loop(self): + print('Enter a message to send, with ctrl-d or -s on a new line.') + print('-c on a new line or ctrl-c stops') + message = '' + while not self.shutdown: + try: + message += input() + if message == '-s': + raise EOFError + elif message == '-c': + raise KeyboardInterrupt + else: + message += '\n' + except EOFError: + message = json.dumps({'m': message, 't': self.myCore._utils.getEpoch()}) + print(self.myCore._utils.doPostRequest('http://%s/clandestine/sendto' % (self.transport,), port=self.socks, data=message)) + message = '' + except KeyboardInterrupt: + self.shutdown = True + def create(self): try: peer = sys.argv[2] @@ -48,13 +71,16 @@ class Clandestine: exit_with_error('Invalid public key specified') except IndexError: exit_with_error('You must specify a peer public key') - + self.peer = peer # Ask peer for transport address by creating block for them peer_transport_address = bootstrapservice.bootstrap_client_service(peer, self.myCore) + self.transport = peer_transport_address + self.socks = self.myCore.config.get('tor.socksport') - 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 with', peer, 'on', peer_transport_address) + if self.myCore._utils.doGetRequest('http://%s/ping' % (peer_transport_address,), ignoreAPI=True, port=self.socks) == 'pong!': print('connected', peer_transport_address) + threading.Thread(target=self._sender_loop).start() def on_init(api, data = None): ''' diff --git a/onionr/static-data/default-plugins/clandestine/peerserver.py b/onionr/static-data/default-plugins/clandestine/peerserver.py index b5fb3913..64e4eaeb 100644 --- a/onionr/static-data/default-plugins/clandestine/peerserver.py +++ b/onionr/static-data/default-plugins/clandestine/peerserver.py @@ -47,6 +47,8 @@ def sendto(): msg = '' if msg == None or msg == '': msg = json.dumps({'m': 'hello world', 't': core_inst._utils.getEpoch()}) + else: + msg = json.dumps(msg) core_inst._utils.localCommand('/clandestine/addrec/%s' % (g.peer,), post=True, postData=msg) return Response('success')