diff --git a/config/default.json b/config/default.json index 09b15a766..77de93748 100644 --- a/config/default.json +++ b/config/default.json @@ -1,5 +1,6 @@ { "serverUrl": "random.snode", + "localUrl": "localhost.loki", "cdnUrl": "random.snode", "localServerPort": "8081", "messageServerPort": "8080", diff --git a/js/modules/loki_snode_api.js b/js/modules/loki_snode_api.js index a93fb02ca..89563eacd 100644 --- a/js/modules/loki_snode_api.js +++ b/js/modules/loki_snode_api.js @@ -9,29 +9,54 @@ 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) => { + if (err) { + reject(err); + } else { + resolve(address[0]); + } + }); + }); + class LokiSnodeAPI { - constructor({ url, swarmServerPort }) { - if (!is.string(url)) { + constructor({ serverUrl, localUrl, swarmServerPort }) { + if (!is.string(serverUrl)) { throw new Error('WebAPI.initialize: Invalid server url'); } - this.url = url; + this.serverUrl = serverUrl; + this.localUrl = localUrl; this.swarmServerPort = swarmServerPort ? `:${swarmServerPort}` : ''; this.swarmsPendingReplenish = {}; this.ourSwarmNodes = {}; this.contactSwarmNodes = {}; } + async getMyLokiIp() { + const address = await resolveCname(this.localUrl); + return resolve4(address); + } + + async getMyLokiAddress() { + /* resolve our local loki address */ + return resolveCname(this.localUrl); + } + getRandomSnodeAddress() { /* resolve random snode */ - return new Promise((resolve, reject) => { - dns.resolveCname(this.url, (err, address) => { - if (err) { - reject(err); - } else { - resolve(address[0]); - } - }); - }); + return resolveCname(this.serverUrl); } async unreachableNode(pubKey, nodeUrl) { diff --git a/libloki/api.js b/libloki/api.js index 2ecc28ebd..2521fded3 100644 --- a/libloki/api.js +++ b/libloki/api.js @@ -24,8 +24,9 @@ } async function sendOnlineBroadcastMessage(pubKey, forceP2p = false) { + const myLokiAddress = await window.lokiSnodeAPI.getMyLokiAddress(); const lokiAddressMessage = new textsecure.protobuf.LokiAddressMessage({ - p2pAddress: 'http://localhost', + p2pAddress: `http://${myLokiAddress}`, p2pPort: parseInt(window.localServerPort, 10), }); const content = new textsecure.protobuf.Content({ 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 diff --git a/main.js b/main.js index 043856dd0..253ba553c 100644 --- a/main.js +++ b/main.js @@ -144,6 +144,7 @@ function prepareURL(pathSegments, moreKeys) { version: app.getVersion(), buildExpiration: config.get('buildExpiration'), serverUrl: config.get('serverUrl'), + localUrl: config.get('localUrl'), cdnUrl: config.get('cdnUrl'), messageServerPort: config.get('messageServerPort'), swarmServerPort: config.get('swarmServerPort'), diff --git a/preload.js b/preload.js index 2b3e673b7..98d137d0d 100644 --- a/preload.js +++ b/preload.js @@ -289,7 +289,8 @@ window.WebAPI = initializeWebAPI({ const LokiSnodeAPI = require('./js/modules/loki_snode_api'); window.lokiSnodeAPI = new LokiSnodeAPI({ - url: config.serverUrl, + serverUrl: config.serverUrl, + localUrl: config.localUrl, swarmServerPort: config.swarmServerPort, });