From eaea7d9b7da8657fb9ab2d9cd7fe30cc0d691256 Mon Sep 17 00:00:00 2001 From: Beaudan Date: Thu, 28 Feb 2019 16:44:05 +1100 Subject: [PATCH 1/3] Catch errors when starting local server and retry every 30 seconds --- libtextsecure/message_receiver.js | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index 2913c79a1..28053d386 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -83,13 +83,7 @@ MessageReceiver.prototype.extend({ this.onEmpty(); } }); - window.lokiSnodeAPI.getMyLokiIp().then(myLokiIp => { - localLokiServer.start(localServerPort, myLokiIp).then(port => { - window.log.info(`Local Server started at ${myLokiIp}:${port}`); - libloki.api.broadcastOnlineStatus(); - localLokiServer.on('message', this.handleP2pMessage.bind(this)); - }); - }); + this.startLocalServer(); // TODO: Rework this socket stuff to work with online messaging const useWebSocket = false; @@ -120,6 +114,28 @@ MessageReceiver.prototype.extend({ // all cached envelopes are processed. this.incoming = [this.pending]; }, + async startLocalServer() { + let myLokiIp; + let myServerPort; + try { + myLokiIp = await window.lokiSnodeAPI.getMyLokiIp(); + } catch (e) { + window.log.warn( + 'Failed to get my loki address to bind server to, will retry in 30 seconds' + ); + setTimeout(this.startLocalServer.bind(this), 30 * 1000); + } + try { + myServerPort = await localLokiServer.start(localServerPort, myLokiIp); + } catch (e) { + window.log.warn('Failed to start local loki server, will retry in 30 seconds'); + setTimeout(this.startLocalServer.bind(this), 30 * 1000); + } + + window.log.info(`Local Server started at ${myLokiIp}:${myServerPort}`); + libloki.api.broadcastOnlineStatus(); + localLokiServer.on('message', this.handleP2pMessage.bind(this)); + }, handleP2pMessage(message) { this.httpPollingResource.handleMessage(message, true); }, From 8c20a31dd44995fd2dd2cec8081b38f9d5bbab28 Mon Sep 17 00:00:00 2001 From: Beaudan Date: Fri, 1 Mar 2019 12:42:57 +1100 Subject: [PATCH 2/3] Review suggestions --- js/modules/loki_snode_api.js | 11 +++++++++-- libtextsecure/errors.js | 17 +++++++++++++++++ libtextsecure/message_receiver.js | 24 ++++++++++++------------ 3 files changed, 38 insertions(+), 14 deletions(-) diff --git a/js/modules/loki_snode_api.js b/js/modules/loki_snode_api.js index fad22234d..9f805bb8b 100644 --- a/js/modules/loki_snode_api.js +++ b/js/modules/loki_snode_api.js @@ -50,8 +50,15 @@ class LokiSnodeAPI { } async getMyLokiIp() { - const address = await resolveCname(this.localUrl); - return resolve4(address); + try { + const address = await resolveCname(this.localUrl); + return resolve4(address); + } catch (e) { + throw new window.textsecure.LokiIpError( + 'Failed to resolve localhost.loki', + e + ); + } } async getMyLokiAddress() { diff --git a/libtextsecure/errors.js b/libtextsecure/errors.js index 7f972492e..d6788957c 100644 --- a/libtextsecure/errors.js +++ b/libtextsecure/errors.js @@ -167,6 +167,22 @@ } inherit(ReplayableError, DNSResolutionError); + function LokiIpError(message, resolutionError) { + this.name = 'LokiIpError'; + this.message = message; + this.error = resolutionError; + + Error.call(this, message); + + // Maintains proper stack trace, where our error was thrown (only available on V8) + // via https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error + if (Error.captureStackTrace) { + Error.captureStackTrace(this); + } + + appendStack(this, resolutionError); + } + window.textsecure.UnregisteredUserError = UnregisteredUserError; window.textsecure.SendMessageNetworkError = SendMessageNetworkError; window.textsecure.IncomingIdentityKeyError = IncomingIdentityKeyError; @@ -178,4 +194,5 @@ window.textsecure.PoWError = PoWError; window.textsecure.EmptySwarmError = EmptySwarmError; window.textsecure.DNSResolutionError = DNSResolutionError; + window.textsecure.LokiIpError = LokiIpError; })(); diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index 28053d386..3530a6008 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -119,22 +119,22 @@ MessageReceiver.prototype.extend({ let myServerPort; try { myLokiIp = await window.lokiSnodeAPI.getMyLokiIp(); - } catch (e) { - window.log.warn( - 'Failed to get my loki address to bind server to, will retry in 30 seconds' - ); - setTimeout(this.startLocalServer.bind(this), 30 * 1000); - } - try { myServerPort = await localLokiServer.start(localServerPort, myLokiIp); + window.log.info(`Local Server started at ${myLokiIp}:${myServerPort}`); + libloki.api.broadcastOnlineStatus(); + localLokiServer.on('message', this.handleP2pMessage.bind(this)); } catch (e) { - window.log.warn('Failed to start local loki server, will retry in 30 seconds'); + if (e instanceof textsecure.LokiIpError) { + window.log.warn( + 'Failed to get my loki address to bind server to, will retry in 30 seconds' + ); + } else { + window.log.warn( + 'Failed to start local loki server, will retry in 30 seconds' + ); + } setTimeout(this.startLocalServer.bind(this), 30 * 1000); } - - window.log.info(`Local Server started at ${myLokiIp}:${myServerPort}`); - libloki.api.broadcastOnlineStatus(); - localLokiServer.on('message', this.handleP2pMessage.bind(this)); }, handleP2pMessage(message) { this.httpPollingResource.handleMessage(message, true); From 5a23dbb687fa9a6875531a5d62cca7e67dd140d5 Mon Sep 17 00:00:00 2001 From: Beaudan Date: Fri, 1 Mar 2019 13:49:59 +1100 Subject: [PATCH 3/3] Review comments and add local server listener before starting --- libtextsecure/message_receiver.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index 3530a6008..83d75c497 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -83,6 +83,7 @@ MessageReceiver.prototype.extend({ this.onEmpty(); } }); + localLokiServer.on('message', this.handleP2pMessage.bind(this)); this.startLocalServer(); // TODO: Rework this socket stuff to work with online messaging @@ -115,14 +116,14 @@ MessageReceiver.prototype.extend({ this.incoming = [this.pending]; }, async startLocalServer() { - let myLokiIp; - let myServerPort; try { - myLokiIp = await window.lokiSnodeAPI.getMyLokiIp(); - myServerPort = await localLokiServer.start(localServerPort, myLokiIp); + const myLokiIp = await window.lokiSnodeAPI.getMyLokiIp(); + const myServerPort = await localLokiServer.start( + localServerPort, + myLokiIp + ); window.log.info(`Local Server started at ${myLokiIp}:${myServerPort}`); libloki.api.broadcastOnlineStatus(); - localLokiServer.on('message', this.handleP2pMessage.bind(this)); } catch (e) { if (e instanceof textsecure.LokiIpError) { window.log.warn(