From 6426a5eb9ba452f1ab0167d8c300856a24303c3c Mon Sep 17 00:00:00 2001 From: Beaudan Date: Fri, 8 Feb 2019 12:13:05 +1100 Subject: [PATCH] Added ability to look up our .loki address and now send that with our LokiAddress messages --- config/default.json | 1 + js/modules/loki_snode_api.js | 33 +++++++++++++++++++++------------ libloki/api.js | 3 ++- main.js | 1 + preload.js | 3 ++- 5 files changed, 27 insertions(+), 14 deletions(-) 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..0d08349b5 100644 --- a/js/modules/loki_snode_api.js +++ b/js/modules/loki_snode_api.js @@ -9,29 +9,38 @@ const dns = require('dns'); const MINIMUM_SWARM_NODES = 1; const FAILURE_THRESHOLD = 3; +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 getMySnodeAddress() { + /* 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..913da7873 100644 --- a/libloki/api.js +++ b/libloki/api.js @@ -24,8 +24,9 @@ } async function sendOnlineBroadcastMessage(pubKey, forceP2p = false) { + const myLokiAddress = await window.lokiSnodeAPI.getMySnodeAddress(); 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/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, });