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
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

View File

@ -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/<pubkey>')
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/<pubkey>')
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

View File

@ -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
address = bootstrapservice.bootstrap_client_service(peer, comm_inst)
return address

View File

@ -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)

View File

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

View File

@ -14,7 +14,8 @@
<link rel="stylesheet" href="/chat/css/convos.css">
<script defer src='/shared/navbar.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/main.js'></script>
</head>

View File

@ -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()
})

View File

@ -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)
}
})
}