diff --git a/onionr/apiservers/private/register_private_blueprints.py b/onionr/apiservers/private/register_private_blueprints.py index c9960b88..35f22286 100644 --- a/onionr/apiservers/private/register_private_blueprints.py +++ b/onionr/apiservers/private/register_private_blueprints.py @@ -19,6 +19,7 @@ ''' import os from httpapi import security, friendsapi, profilesapi, configapi, insertblock, miscclientapi, onionrsitesapi, apiutils +from httpapi import directconnections def register_private_blueprints(private_api, app): app.register_blueprint(security.client.ClientAPISecurity(private_api).client_api_security_bp) app.register_blueprint(friendsapi.friends) @@ -30,4 +31,5 @@ def register_private_blueprints(private_api, app): app.register_blueprint(onionrsitesapi.site_api) app.register_blueprint(apiutils.shutdown.shutdown_bp) app.register_blueprint(miscclientapi.staticfiles.static_files_bp) + app.register_blueprint(directconnections.DirectConnectionManagement(private_api).direct_conn_management_bp) return app \ No newline at end of file diff --git a/onionr/httpapi/directconnections/__init__.py b/onionr/httpapi/directconnections/__init__.py index 39fb31e5..08b70eae 100644 --- a/onionr/httpapi/directconnections/__init__.py +++ b/onionr/httpapi/directconnections/__init__.py @@ -21,21 +21,25 @@ import threading from flask import Response from flask import Blueprint +from flask import g import deadsimplekv import filepaths import onionrservices +def _get_communicator(g): + return g.too_many.get_by_string("OnionrCommunicatorDaemon") + class DirectConnectionManagement: def __init__(self, client_api): direct_conn_management_bp = Blueprint('direct_conn_management', __name__) self.direct_conn_management_bp = direct_conn_management_bp - communicator = client_api._too_many.get('OnionrCommunicatorDaemon') - cache = communicator.deadsimplekv(filepaths.cached_storage) + cache = deadsimplekv.DeadSimpleKV(filepaths.cached_storage) @direct_conn_management_bp.route('/dc-client/isconnected/') def is_connected(pubkey): + communicator = _get_communicator(g) resp = "" if pubkey in communicator.direct_connection_clients: resp = communicator.direct_connection_clients[pubkey] @@ -43,9 +47,10 @@ class DirectConnectionManagement: @direct_conn_management_bp.route('/dc-client/connect/') def make_new_connection(pubkey): + communicator = _get_communicator(g) resp = "pending" if pubkey in communicator.direct_connection_clients: - resp = communicator.active_services[pubkey] + resp = communicator.direct_connection_clients[pubkey] """Spawn a thread that will create the client and eventually add it to the communicator.active_services diff --git a/onionr/onionrservices/__init__.py b/onionr/onionrservices/__init__.py index a6a44d90..975d6e90 100755 --- a/onionr/onionrservices/__init__.py +++ b/onionr/onionrservices/__init__.py @@ -55,7 +55,5 @@ class OnionrServices: @staticmethod def create_client(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 + address = bootstrapservice.bootstrap_client_service(peer, comm_inst) + return address diff --git a/onionr/onionrservices/bootstrapservice.py b/onionr/onionrservices/bootstrapservice.py index ed731acd..b824ae24 100755 --- a/onionr/onionrservices/bootstrapservice.py +++ b/onionr/onionrservices/bootstrapservice.py @@ -26,7 +26,7 @@ from . import httpheaders from onionrutils import stringvalidators, epoch import config, onionrblocks, filepaths import deadsimplekv as simplekv -def bootstrap_client_service(peer, onionr_inst=None, bootstrap_timeout=300): +def bootstrap_client_service(peer, comm_inst=None, bootstrap_timeout=300): ''' Bootstrap client services ''' @@ -38,11 +38,11 @@ def bootstrap_client_service(peer, onionr_inst=None, bootstrap_timeout=300): bootstrap_app = Flask(__name__) http_server = WSGIServer(('127.0.0.1', bootstrap_port), bootstrap_app, log=None) try: - assert onionr_inst.communicatorInst is not None + assert comm_inst is not None except (AttributeError, AssertionError) as e: pass else: - onionr_inst.communicatorInst.service_greenlets.append(http_server) + comm_inst.service_greenlets.append(http_server) bootstrap_address = '' shutdown = False @@ -83,6 +83,8 @@ def bootstrap_client_service(peer, onionr_inst=None, bootstrap_timeout=300): except TypeError: pass # This line reached when server is shutdown by being bootstrapped - + # Add the address to the client pool + if not comm_inst is None: + comm_inst.direct_connection_clients[peer] = bs_id # Now that the bootstrap server has received a server, return the address return key_store.get(bs_id) diff --git a/onionr/static-data/bootstrap-nodes.txt b/onionr/static-data/bootstrap-nodes.txt index 0201f0fd..e69de29b 100755 --- a/onionr/static-data/bootstrap-nodes.txt +++ b/onionr/static-data/bootstrap-nodes.txt @@ -1 +0,0 @@ -3msj7fgyxgpfsjvvtcji7a4tkjbna6jmpealv6mun7435jjyptctfxyd.onion \ No newline at end of file diff --git a/onionr/static-data/www/chat/index.html b/onionr/static-data/www/chat/index.html index ccc31c25..03731997 100755 --- a/onionr/static-data/www/chat/index.html +++ b/onionr/static-data/www/chat/index.html @@ -14,7 +14,8 @@ - + + diff --git a/onionr/static-data/www/chat/js/main.js b/onionr/static-data/www/chat/js/main.js index 8373f380..20c8fb62 100755 --- a/onionr/static-data/www/chat/js/main.js +++ b/onionr/static-data/www/chat/js/main.js @@ -40,6 +40,8 @@ fetch('/friends/list', { for(var k in resp) keys.push(k) for (var i = 0; i < keys.length; i++){ friendList[keys[i]] = resp[keys[i]]['name'] + // Create a connection to each peer + createConnection(keys[i]) } createConvoList() }) diff --git a/onionr/static-data/www/shared/direct-connections.js b/onionr/static-data/www/shared/direct-connections.js index e7c4c884..5bab83f1 100644 --- a/onionr/static-data/www/shared/direct-connections.js +++ b/onionr/static-data/www/shared/direct-connections.js @@ -23,33 +23,37 @@ let waitForConnection = function(pubkey){ headers: { "token": webpass }}) + .then((resp) => resp.text()) .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 - } + 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){ + // Tells the Onionr daemon to create a client connection to a remote peer for generic direct connections + + // If the pubkey is already connected, don't bother if (direct_connections.hasOwnProperty(pubkey)){ return } + + // Do the request, then spawn a function to wait for the connection to be created fetch('/dc-client/connect/' + pubkey, { headers: { "token": webpass }}) + .then((resp) => resp.text()) .then(function(resp) { - if (resp.ok){ - if (resp.text === "pending"){ - setTimeout(function(){waitForConnection(pubkey)}, 3000) - } + alert(resp) + if (resp === "pending"){ + setTimeout(function(){waitForConnection(pubkey)}, 3000) } }) } \ No newline at end of file