From 370dee5abbe4ffe370d165ccdc9e4e3aeaafc26c Mon Sep 17 00:00:00 2001 From: Beaudan Brown Date: Fri, 8 Nov 2019 14:28:43 +1100 Subject: [PATCH] Ensure we have the correct apis and listeners during the pairing process. Catch upnp error. Remove redundant friend requests --- js/background.js | 17 ++++++++++++----- libloki/api.js | 19 +++++++++++++------ libtextsecure/message_receiver.js | 8 -------- libtextsecure/sendmessage.js | 3 ++- 4 files changed, 27 insertions(+), 20 deletions(-) diff --git a/js/background.js b/js/background.js index 5aaec5be3..ffa504b4b 100644 --- a/js/background.js +++ b/js/background.js @@ -244,10 +244,13 @@ // singleton to relay events to libtextsecure/message_receiver window.lokiPublicChatAPI = new window.LokiPublicChatAPI(ourKey); // singleton to interface the File server - window.lokiFileServerAPI = new window.LokiFileServerAPI(ourKey); - await window.lokiFileServerAPI.establishConnection( - window.getDefaultFileServer() - ); + // If already exists we registered as a secondary device + if (!window.lokiFileServerAPI) { + window.lokiFileServerAPI = new window.LokiFileServerAPI(ourKey); + await window.lokiFileServerAPI.establishConnection( + window.getDefaultFileServer() + ); + } // are there limits on tracking, is this unneeded? // window.mixpanel.track("Desktop boot"); window.lokiP2pAPI = new window.LokiP2pAPI(ourKey); @@ -262,7 +265,6 @@ if (storage.get('isSecondaryDevice')) { window.lokiFileServerAPI.updateOurDeviceMapping(); } - Whisper.events.trigger('apisReady'); }; function mapOldThemeToNew(theme) { @@ -957,6 +959,10 @@ if (Whisper.Registration.ongoingSecondaryDeviceRegistration()) { const ourKey = textsecure.storage.user.getNumber(); window.lokiMessageAPI = new window.LokiMessageAPI(ourKey); + window.lokiFileServerAPI = new window.LokiFileServerAPI(ourKey); + await window.lokiFileServerAPI.establishConnection( + window.getDefaultFileServer() + ); window.localLokiServer = null; window.lokiPublicChatAPI = null; window.feeds = []; @@ -967,6 +973,7 @@ options ); messageReceiver.addEventListener('message', onMessageReceived); + messageReceiver.addEventListener('contact', onContactReceived); window.textsecure.messaging = new textsecure.MessageSender( USERNAME, PASSWORD diff --git a/libloki/api.js b/libloki/api.js index e606c554b..a1b3f350f 100644 --- a/libloki/api.js +++ b/libloki/api.js @@ -38,15 +38,22 @@ let p2pPort = null; let type; - if (!window.localLokiServer || !window.localLokiServer.isListening()) { - type = textsecure.protobuf.LokiAddressMessage.Type.HOST_UNREACHABLE; - } else { - // clearnet change: getMyLokiAddress -> getMyClearIP - // const myLokiAddress = await window.lokiSnodeAPI.getMyLokiAddress(); - const myIp = await window.lokiSnodeAPI.getMyClearIp(); + let myIp; + if (window.localLokiServer && window.localLokiServer.isListening()) { + try { + // clearnet change: getMyLokiAddress -> getMyClearIP + // const myLokiAddress = await window.lokiSnodeAPI.getMyLokiAddress(); + myIp = await window.lokiSnodeAPI.getMyClearIp(); + } catch (e) { + log.warn(`Failed to get clear IP for local server ${e}`); + } + } + if (myIp) { p2pAddress = `https://${myIp}`; p2pPort = window.localLokiServer.getPublicPort(); type = textsecure.protobuf.LokiAddressMessage.Type.HOST_REACHABLE; + } else { + type = textsecure.protobuf.LokiAddressMessage.Type.HOST_UNREACHABLE; } const lokiAddressMessage = new textsecure.protobuf.LokiAddressMessage({ diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index e62c8494e..3a6ca8e69 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -1130,14 +1130,6 @@ MessageReceiver.prototype.extend({ // This call already removes the envelope from the cache await this.handleContacts(envelope, syncMessage.contacts); removedFromCache = true; - if (window.initialisedAPI) { - await this.sendFriendRequestsToSyncContacts(syncMessage.contacts); - } else { - // We need to wait here because initAPIs hasn't been called yet - Whisper.events.once('apisReady', async () => { - await this.sendFriendRequestsToSyncContacts(syncMessage.contacts); - }); - } } } else { window.log.warn('Unimplemented pairing authorisation message type'); diff --git a/libtextsecure/sendmessage.js b/libtextsecure/sendmessage.js index 31f354cee..abf0705ad 100644 --- a/libtextsecure/sendmessage.js +++ b/libtextsecure/sendmessage.js @@ -570,7 +570,8 @@ MessageSender.prototype = { )) // Don't send to ourselves .filter(pubKey => pubKey !== textsecure.storage.user.getNumber()); - if (allOurDevices.length === 0) { + if (allOurDevices.includes(contactConversation.id) || !primaryDeviceKey || allOurDevices.length === 0) { + // If we havn't got a primaryDeviceKey then we are in the middle of pairing return Promise.resolve(); }