From 2fd746821a98f1ebb0a49cfd61f8be97625ff3f2 Mon Sep 17 00:00:00 2001 From: Beaudan Date: Tue, 12 Feb 2019 13:50:19 +1100 Subject: [PATCH] Added function for getting your loki IP and bind the local server --- js/modules/loki_snode_api.js | 18 +++++++++++++++++- libloki/api.js | 2 +- libloki/local_loki_server.js | 4 ++-- libtextsecure/message_receiver.js | 11 ++++++----- 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/js/modules/loki_snode_api.js b/js/modules/loki_snode_api.js index 0d08349b5..89563eacd 100644 --- a/js/modules/loki_snode_api.js +++ b/js/modules/loki_snode_api.js @@ -9,6 +9,17 @@ const dns = require('dns'); const MINIMUM_SWARM_NODES = 1; const FAILURE_THRESHOLD = 3; +const resolve4 = url => + new Promise((resolve, reject) => { + dns.resolve4(url, (err, ip) => { + if (err) { + reject(err); + } else { + resolve(ip); + } + }); + }); + const resolveCname = url => new Promise((resolve, reject) => { dns.resolveCname(url, (err, address) => { @@ -33,7 +44,12 @@ class LokiSnodeAPI { this.contactSwarmNodes = {}; } - async getMySnodeAddress() { + async getMyLokiIp() { + const address = await resolveCname(this.localUrl); + return resolve4(address); + } + + async getMyLokiAddress() { /* resolve our local loki address */ return resolveCname(this.localUrl); } diff --git a/libloki/api.js b/libloki/api.js index 913da7873..2521fded3 100644 --- a/libloki/api.js +++ b/libloki/api.js @@ -24,7 +24,7 @@ } async function sendOnlineBroadcastMessage(pubKey, forceP2p = false) { - const myLokiAddress = await window.lokiSnodeAPI.getMySnodeAddress(); + const myLokiAddress = await window.lokiSnodeAPI.getMyLokiAddress(); const lokiAddressMessage = new textsecure.protobuf.LokiAddressMessage({ p2pAddress: `http://${myLokiAddress}`, p2pPort: parseInt(window.localServerPort, 10), diff --git a/libloki/local_loki_server.js b/libloki/local_loki_server.js index 979c30852..262b532ba 100644 --- a/libloki/local_loki_server.js +++ b/libloki/local_loki_server.js @@ -50,13 +50,13 @@ class LocalLokiServer extends EventEmitter { }); } - async start(port) { + async start(port, ip) { // Close the old server await this.close(); // Start a listening on new server return new Promise((res, rej) => { - this.server.listen(port, err => { + this.server.listen(port, ip, err => { if (err) { rej(err); } else { diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index 44d9081a5..3a0204a81 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -83,11 +83,12 @@ MessageReceiver.prototype.extend({ this.onEmpty(); } }); - - localLokiServer.start(localServerPort).then(port => { - window.log.info(`Local Server started at localhost:${port}`); - libloki.api.broadcastOnlineStatus(); - localLokiServer.on('message', this.handleP2pMessage.bind(this)); + window.lokiSnodeAPI.getMyLokiIp().then(myLokiIp => { + localLokiServer.start(localServerPort, myLokiIp).then(port => { + window.log.info(`Local Server started at localhost:${port}`); + libloki.api.broadcastOnlineStatus(); + localLokiServer.on('message', this.handleP2pMessage.bind(this)); + }); }); // TODO: Rework this socket stuff to work with online messaging