diff --git a/config/default.json b/config/default.json index c2b0f0e2f..a9ec560cc 100644 --- a/config/default.json +++ b/config/default.json @@ -4,7 +4,6 @@ "cdnUrl": "random.snode", "contentProxyUrl": "random.snode", "localServerPort": "8081", - "snodeServerPort": "8080", "defaultPoWDifficulty": "100", "disableAutoUpdate": false, "updatesUrl": "https://updates2.signal.org/desktop", diff --git a/js/modules/loki_message_api.js b/js/modules/loki_message_api.js index 75de95d4b..f58bb9b04 100644 --- a/js/modules/loki_message_api.js +++ b/js/modules/loki_message_api.js @@ -43,9 +43,7 @@ const trySendP2p = async (pubKey, data64, isPing, messageEventData) => { return false; } try { - const port = p2pDetails.port ? `:${p2pDetails.port}` : ''; - - await rpc(p2pDetails.address, port, 'store', { + await rpc(p2pDetails.address, p2pDetails.port, 'store', { data: data64, }); lokiP2pAPI.setContactOnline(pubKey); @@ -68,9 +66,30 @@ const trySendP2p = async (pubKey, data64, isPing, messageEventData) => { } }; +const retrieveNextMessages = async (nodeUrl, nodeData, ourKey) => { + const params = { + pubKey: ourKey, + lastHash: nodeData.lastHash || '', + }; + const options = { + timeout: 40000, + headers: { + [LOKI_LONGPOLL_HEADER]: true, + }, + }; + + const result = await rpc( + `https://${nodeUrl}`, + nodeData.port, + 'retrieve', + params, + options + ); + return result.messages || []; +} + class LokiMessageAPI { - constructor({ snodeServerPort }) { - this.snodeServerPort = snodeServerPort ? `:${snodeServerPort}` : ''; + constructor() { this.jobQueue = new window.JobQueue(); this.sendingSwarmNodes = {}; } @@ -208,46 +227,24 @@ class LokiMessageAPI { return false; } - async retrieveNextMessages(nodeUrl, nodeData, ourKey) { - const params = { - pubKey: ourKey, - lastHash: nodeData.lastHash || '', - }; - const options = { - timeout: 40000, - headers: { - [LOKI_LONGPOLL_HEADER]: true, - }, - }; - - const result = await rpc( - `https://${nodeUrl}`, - this.snodeServerPort, - 'retrieve', - params, - options - ); - return result.messages || []; - } - async openConnection(callback) { const ourKey = window.textsecure.storage.user.getNumber(); while (!_.isEmpty(this.ourSwarmNodes)) { - const url = Object.keys(this.ourSwarmNodes)[0]; - const nodeData = this.ourSwarmNodes[url]; - delete this.ourSwarmNodes[url]; + const address = Object.keys(this.ourSwarmNodes)[0]; + const nodeData = this.ourSwarmNodes[address]; + delete this.ourSwarmNodes[address]; let successiveFailures = 0; while (successiveFailures < 3) { await sleepFor(successiveFailures * 1000); try { - let messages = await this.retrieveNextMessages(url, nodeData, ourKey); + let messages = await retrieveNextMessages(address, nodeData, ourKey); successiveFailures = 0; if (messages.length) { const lastMessage = _.last(messages); nodeData.lashHash = lastMessage.hash; lokiSnodeAPI.updateLastHash( - url, + address, lastMessage.hash, lastMessage.expiration ); diff --git a/js/modules/loki_rpc.js b/js/modules/loki_rpc.js index 1e0f52975..c33f368e9 100644 --- a/js/modules/loki_rpc.js +++ b/js/modules/loki_rpc.js @@ -103,7 +103,8 @@ const fetch = async (url, options = {}) => { // Wrapper for a JSON RPC request const rpc = (address, port, method, params, options = {}) => { const headers = options.headers || {}; - const url = `${address}${port}${endpointBase}`; + const portString = port ? `:${port}` : ''; + const url = `${address}${portString}${endpointBase}`; const body = { method, params, diff --git a/js/modules/loki_snode_api.js b/js/modules/loki_snode_api.js index 1ae318961..4f97afd7a 100644 --- a/js/modules/loki_snode_api.js +++ b/js/modules/loki_snode_api.js @@ -32,13 +32,13 @@ const resolveCname = url => }); class LokiSnodeAPI { - constructor({ serverUrl, localUrl, snodeServerPort }) { + constructor({ serverUrl, localUrl }) { if (!is.string(serverUrl)) { throw new Error('WebAPI.initialize: Invalid server url'); } this.serverUrl = serverUrl; this.localUrl = localUrl; - this.snodeServerPort = snodeServerPort ? `:${snodeServerPort}` : ''; + this.randomSnodePool = []; this.swarmsPendingReplenish = {}; this.ourSwarmNodes = {}; this.contactSwarmNodes = {}; @@ -60,7 +60,7 @@ class LokiSnodeAPI { } } - async getMyLokiAddress() { + getMyLokiAddress() { /* resolve our local loki address */ return resolveCname(this.localUrl); } diff --git a/main.js b/main.js index eaad6252c..455299a98 100644 --- a/main.js +++ b/main.js @@ -154,7 +154,6 @@ function prepareURL(pathSegments, moreKeys) { serverUrl: config.get('serverUrl'), localUrl: config.get('localUrl'), cdnUrl: config.get('cdnUrl'), - snodeServerPort: config.get('snodeServerPort'), localServerPort: config.get('localServerPort'), defaultPoWDifficulty: config.get('defaultPoWDifficulty'), certificateAuthority: config.get('certificateAuthority'), diff --git a/preload.js b/preload.js index 5cad3915d..55dfb9772 100644 --- a/preload.js +++ b/preload.js @@ -293,17 +293,13 @@ const LokiSnodeAPI = require('./js/modules/loki_snode_api'); window.lokiSnodeAPI = new LokiSnodeAPI({ serverUrl: config.serverUrl, localUrl: config.localUrl, - snodeServerPort: config.snodeServerPort, }); window.LokiP2pAPI = require('./js/modules/loki_p2p_api'); const LokiMessageAPI = require('./js/modules/loki_message_api'); -window.lokiMessageAPI = new LokiMessageAPI({ - url: config.serverUrl, - snodeServerPort: config.snodeServerPort, -}); +window.lokiMessageAPI = new LokiMessageAPI(); const LocalLokiServer = require('./libloki/modules/local_loki_server');