work on chat

This commit is contained in:
Kevin Froman 2019-08-16 17:40:17 -05:00
parent bb6ce10985
commit 0d9e72eccd
8 changed files with 39 additions and 26 deletions

View File

@ -19,6 +19,7 @@
''' '''
import os import os
from httpapi import security, friendsapi, profilesapi, configapi, insertblock, miscclientapi, onionrsitesapi, apiutils from httpapi import security, friendsapi, profilesapi, configapi, insertblock, miscclientapi, onionrsitesapi, apiutils
from httpapi import directconnections
def register_private_blueprints(private_api, app): def register_private_blueprints(private_api, app):
app.register_blueprint(security.client.ClientAPISecurity(private_api).client_api_security_bp) app.register_blueprint(security.client.ClientAPISecurity(private_api).client_api_security_bp)
app.register_blueprint(friendsapi.friends) 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(onionrsitesapi.site_api)
app.register_blueprint(apiutils.shutdown.shutdown_bp) app.register_blueprint(apiutils.shutdown.shutdown_bp)
app.register_blueprint(miscclientapi.staticfiles.static_files_bp) app.register_blueprint(miscclientapi.staticfiles.static_files_bp)
app.register_blueprint(directconnections.DirectConnectionManagement(private_api).direct_conn_management_bp)
return app return app

View File

@ -21,21 +21,25 @@ import threading
from flask import Response from flask import Response
from flask import Blueprint from flask import Blueprint
from flask import g
import deadsimplekv import deadsimplekv
import filepaths import filepaths
import onionrservices import onionrservices
def _get_communicator(g):
return g.too_many.get_by_string("OnionrCommunicatorDaemon")
class DirectConnectionManagement: class DirectConnectionManagement:
def __init__(self, client_api): def __init__(self, client_api):
direct_conn_management_bp = Blueprint('direct_conn_management', __name__) direct_conn_management_bp = Blueprint('direct_conn_management', __name__)
self.direct_conn_management_bp = direct_conn_management_bp 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/<pubkey>') @direct_conn_management_bp.route('/dc-client/isconnected/<pubkey>')
def is_connected(pubkey): def is_connected(pubkey):
communicator = _get_communicator(g)
resp = "" resp = ""
if pubkey in communicator.direct_connection_clients: if pubkey in communicator.direct_connection_clients:
resp = communicator.direct_connection_clients[pubkey] resp = communicator.direct_connection_clients[pubkey]
@ -43,9 +47,10 @@ class DirectConnectionManagement:
@direct_conn_management_bp.route('/dc-client/connect/<pubkey>') @direct_conn_management_bp.route('/dc-client/connect/<pubkey>')
def make_new_connection(pubkey): def make_new_connection(pubkey):
communicator = _get_communicator(g)
resp = "pending" resp = "pending"
if pubkey in communicator.direct_connection_clients: 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 """Spawn a thread that will create the client and eventually add it to the
communicator.active_services communicator.active_services

View File

@ -55,7 +55,5 @@ class OnionrServices:
@staticmethod @staticmethod
def create_client(peer, comm_inst=None): def create_client(peer, comm_inst=None):
# Create ephemeral onion service to bootstrap connection # Create ephemeral onion service to bootstrap connection
address = bootstrapservice.bootstrap_client_service(peer) address = bootstrapservice.bootstrap_client_service(peer, comm_inst)
if not comm_inst is None:
comm_inst.direct_connection_clients[peer] = address
return address return address

View File

@ -26,7 +26,7 @@ from . import httpheaders
from onionrutils import stringvalidators, epoch from onionrutils import stringvalidators, epoch
import config, onionrblocks, filepaths import config, onionrblocks, filepaths
import deadsimplekv as simplekv 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 Bootstrap client services
''' '''
@ -38,11 +38,11 @@ def bootstrap_client_service(peer, onionr_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:
assert onionr_inst.communicatorInst is not None assert comm_inst is not None
except (AttributeError, AssertionError) as e: except (AttributeError, AssertionError) as e:
pass pass
else: else:
onionr_inst.communicatorInst.service_greenlets.append(http_server) comm_inst.service_greenlets.append(http_server)
bootstrap_address = '' bootstrap_address = ''
shutdown = False shutdown = False
@ -83,6 +83,8 @@ def bootstrap_client_service(peer, onionr_inst=None, bootstrap_timeout=300):
except TypeError: except TypeError:
pass pass
# This line reached when server is shutdown by being bootstrapped # 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 # Now that the bootstrap server has received a server, return the address
return key_store.get(bs_id) return key_store.get(bs_id)

View File

@ -1 +0,0 @@
3msj7fgyxgpfsjvvtcji7a4tkjbna6jmpealv6mun7435jjyptctfxyd.onion

View File

@ -14,7 +14,8 @@
<link rel="stylesheet" href="/chat/css/convos.css"> <link rel="stylesheet" href="/chat/css/convos.css">
<script defer src='/shared/navbar.js'></script> <script defer src='/shared/navbar.js'></script>
<script defer src='/shared/misc.js'></script> <script defer src='/shared/misc.js'></script>
<script defer src='/chat/messages/js'></script> <script defer src='/shared/direct-connections.js'></script>
<script defer src='/chat/messages.js'></script>
<script defer src='/chat/js/message-feed.js'></script> <script defer src='/chat/js/message-feed.js'></script>
<script defer src='/chat/js/main.js'></script> <script defer src='/chat/js/main.js'></script>
</head> </head>

View File

@ -40,6 +40,8 @@ fetch('/friends/list', {
for(var k in resp) keys.push(k) for(var k in resp) keys.push(k)
for (var i = 0; i < keys.length; i++){ for (var i = 0; i < keys.length; i++){
friendList[keys[i]] = resp[keys[i]]['name'] friendList[keys[i]] = resp[keys[i]]['name']
// Create a connection to each peer
createConnection(keys[i])
} }
createConvoList() createConvoList()
}) })

View File

@ -23,33 +23,37 @@ let waitForConnection = function(pubkey){
headers: { headers: {
"token": webpass "token": webpass
}}) }})
.then((resp) => resp.text())
.then(function(resp) { .then(function(resp) {
if (resp.ok){ if (resp.text === ""){
if (resp.text === ""){ // Try to get the client address again again in a few seconds
// Try to get the client address again again in a few seconds setTimeout(function(){waitForConnection(pubkey)}, 3000)
setTimeout(function(){waitForConnection(pubkey)}, 3000) }
} else{
else{ // add to the dc object
// add to the dc object direct_connections[pubkey] = resp
direct_connections[pubkey] = resp
}
} }
}) })
} }
let createConnection = function(pubkey){ 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)){ if (direct_connections.hasOwnProperty(pubkey)){
return return
} }
// Do the request, then spawn a function to wait for the connection to be created
fetch('/dc-client/connect/' + pubkey, { fetch('/dc-client/connect/' + pubkey, {
headers: { headers: {
"token": webpass "token": webpass
}}) }})
.then((resp) => resp.text())
.then(function(resp) { .then(function(resp) {
if (resp.ok){ alert(resp)
if (resp.text === "pending"){ if (resp === "pending"){
setTimeout(function(){waitForConnection(pubkey)}, 3000) setTimeout(function(){waitForConnection(pubkey)}, 3000)
}
} }
}) })
} }