From 602bc62897a2aa2d1397d640f9813f70111b666e Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Fri, 16 Aug 2019 15:41:56 -0500 Subject: [PATCH] work on chat --- onionr/communicator/__init__.py | 3 + onionr/communicatorutils/servicecreator.py | 2 +- onionr/httpapi/directconnections/__init__.py | 26 +++++++-- onionr/httpapi/miscclientapi/endpoints.py | 3 +- onionr/onionrservices/__init__.py | 4 +- onionr/onionrservices/connectionserver.py | 6 +- .../www/shared/direct-connections.js | 55 +++++++++++++++++++ 7 files changed, 88 insertions(+), 11 deletions(-) create mode 100644 onionr/static-data/www/shared/direct-connections.js diff --git a/onionr/communicator/__init__.py b/onionr/communicator/__init__.py index 27dbf806..70f668dc 100755 --- a/onionr/communicator/__init__.py +++ b/onionr/communicator/__init__.py @@ -136,6 +136,9 @@ class OnionrCommunicatorDaemon: else: self.services = None + # {peer_pubkey: ephemeral_address}, the address to reach them + self.direct_connection_clients = {} + # This timer creates deniable blocks, in an attempt to further obfuscate block insertion metadata if config.get('general.insert_deniable_blocks', True): deniableBlockTimer = OnionrCommunicatorTimers(self, deniableinserts.insert_deniable_block, 180, myArgs=[self], requiresPeer=True, maxThreads=1) diff --git a/onionr/communicatorutils/servicecreator.py b/onionr/communicatorutils/servicecreator.py index 6fa15346..17f6a19c 100755 --- a/onionr/communicatorutils/servicecreator.py +++ b/onionr/communicatorutils/servicecreator.py @@ -36,4 +36,4 @@ def service_creator(daemon): daemon.active_services.append(signer) daemon.services.create_server(signer, bs, daemon) - daemon.decrementThreadCount('service_creator') \ No newline at end of file + daemon.decrementThreadCount('service_creator') diff --git a/onionr/httpapi/directconnections/__init__.py b/onionr/httpapi/directconnections/__init__.py index 2ab59578..02c5595e 100644 --- a/onionr/httpapi/directconnections/__init__.py +++ b/onionr/httpapi/directconnections/__init__.py @@ -18,10 +18,9 @@ along with this program. If not, see . ''' from flask import Response, Blueprint +import deadsimplekv -def _in_pool(pubkey, communicator): - if pubkey in communicator.active_services - +import filepaths class DirectConnectionManagement: def __init__(self, client_api): @@ -29,6 +28,23 @@ class DirectConnectionManagement: self.direct_conn_management_bp = direct_conn_management_bp communicator = client_api._too_many.get('OnionrCommunicatorDaemon') - @direct_conn_management_bp.route('/isconnected/') + cache = communicator.deadsimplekv(filepaths.cached_storage) + + @direct_conn_management_bp.route('/dc-client/isconnected/') def is_connected(pubkey): - return \ No newline at end of file + resp = "" + if pubkey in communicator.direct_connection_clients: + resp = communicator.direct_connection_clients[pubkey] + return Response(resp) + + @direct_conn_management_bp.route('/dc-client/connect/') + def make_new_connection(pubkey): + resp = "pending" + if pubkey in communicator.direct_connection_clients: + resp = communicator.active_services[pubkey] + + """Spawn a thread that will create the client and eventually add it to the + communicator.active_services + """ + + return Response(resp) \ No newline at end of file diff --git a/onionr/httpapi/miscclientapi/endpoints.py b/onionr/httpapi/miscclientapi/endpoints.py index 70f6eaf5..9924258f 100644 --- a/onionr/httpapi/miscclientapi/endpoints.py +++ b/onionr/httpapi/miscclientapi/endpoints.py @@ -86,7 +86,6 @@ class PrivateEndpoints: @private_endpoints_bp.route('/getstats') def getStats(): # returns node stats - #return Response("disabled") while True: try: return Response(client_api._too_many.get(SerializedData).getStats()) @@ -107,4 +106,4 @@ class PrivateEndpoints: @private_endpoints_bp.route('/gettorsocks') def get_tor_socks(): - return Response(str(client_api._too_many.get(NetController).socksPort)) \ No newline at end of file + return Response(str(client_api._too_many.get(NetController).socksPort)) diff --git a/onionr/onionrservices/__init__.py b/onionr/onionrservices/__init__.py index 78ce5f3e..8b7cff99 100755 --- a/onionr/onionrservices/__init__.py +++ b/onionr/onionrservices/__init__.py @@ -52,7 +52,9 @@ class OnionrServices: else: return False - def create_client(self, peer): + def create_client(self, peer, comm_inst=None): # Create ephemeral onion service to bootstrap connection address = bootstrapservice.bootstrap_client_service(peer) + if not comm_inst is None: + comm_inst.direct_connection_clients[peer] = address return address \ No newline at end of file diff --git a/onionr/onionrservices/connectionserver.py b/onionr/onionrservices/connectionserver.py index 726f9179..a63e94b5 100755 --- a/onionr/onionrservices/connectionserver.py +++ b/onionr/onionrservices/connectionserver.py @@ -80,8 +80,10 @@ class ConnectionServer: raise ConnectionError('Could not reach %s bootstrap address %s' % (peer, address)) else: # If no connection error, create the service and save it to local global key store - key_store.put('dc-' + response.service_id, bytesconverter.bytes_to_str(peer)) + peer = bytesconverter.bytes_to_str(peer) + key_store.put('dc-' + peer, response.service_id) + key_store.flush() logger.info('hosting on %s with %s' % (response.service_id, peer)) http_server.serve_forever() http_server.stop() - key_store.delete('dc-' + response.service_id) \ No newline at end of file + key_store.delete('dc-' + peer) diff --git a/onionr/static-data/www/shared/direct-connections.js b/onionr/static-data/www/shared/direct-connections.js new file mode 100644 index 00000000..e7c4c884 --- /dev/null +++ b/onionr/static-data/www/shared/direct-connections.js @@ -0,0 +1,55 @@ +/* + Onionr - Private P2P Communication + + Functions to detect + + 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 +*/ +direct_connections = {} + +let waitForConnection = function(pubkey){ + fetch('/dc-client/isconnected/' + pubkey, { + headers: { + "token": webpass + }}) + .then(function(resp) { + if (resp.ok){ + if (resp.text === ""){ + // Try to get the client address again again in a few seconds + setTimeout(function(){waitForConnection(pubkey)}, 3000) + } + else{ + // add to the dc object + direct_connections[pubkey] = resp + } + } + }) +} + +let createConnection = function(pubkey){ + if (direct_connections.hasOwnProperty(pubkey)){ + return + } + fetch('/dc-client/connect/' + pubkey, { + headers: { + "token": webpass + }}) + .then(function(resp) { + if (resp.ok){ + if (resp.text === "pending"){ + setTimeout(function(){waitForConnection(pubkey)}, 3000) + } + } + }) +} \ No newline at end of file