Remove default port and use port from storage server responses

pull/296/head
Beaudan 6 years ago
parent eea2b8e6a7
commit 6b2b9ce926

@ -5,6 +5,8 @@
"contentProxyUrl": "random.snode",
"localServerPort": "8081",
"defaultPoWDifficulty": "100",
"seedNodeUrl": "13.238.53.205",
"seedNodePort": "22023",
"disableAutoUpdate": false,
"updatesUrl": "https://updates2.signal.org/desktop",
"updatesPublicKey":

@ -170,8 +170,8 @@ class LokiMessageAPI {
async openSendConnection(params) {
while (!_.isEmpty(this.sendingSwarmNodes[params.timestamp])) {
const url = this.sendingSwarmNodes[params.timestamp].shift();
const successfulSend = await this.sendToNode(url, params);
const snode = this.sendingSwarmNodes[params.timestamp].shift();
const successfulSend = await this.sendToNode(snode.address, snode.port, params);
if (successfulSend) {
return true;
}
@ -179,14 +179,14 @@ class LokiMessageAPI {
return false;
}
async sendToNode(url, params) {
async sendToNode(address, port, params) {
let successiveFailures = 0;
while (successiveFailures < 3) {
await sleepFor(successiveFailures * 500);
try {
const result = await rpc(
`https://${url}`,
this.snodeServerPort,
`https://${address}`,
port,
'store',
params
);
@ -222,8 +222,8 @@ class LokiMessageAPI {
}
}
}
log.error(`Failed to send to node: ${url}`);
await lokiSnodeAPI.unreachableNode(params.pubKey, url);
log.error(`Failed to send to node: ${address}`);
await lokiSnodeAPI.unreachableNode(params.pubKey, address);
return false;
}

@ -5,6 +5,7 @@ const { parse } = require('url');
const LOKI_EPHEMKEY_HEADER = 'X-Loki-EphemKey';
const endpointBase = '/v1/storage_rpc';
const seedEndpointBase = '/json_rpc';
const decryptResponse = async (response, address) => {
try {
@ -101,11 +102,15 @@ const fetch = async (url, options = {}) => {
};
// Wrapper for a JSON RPC request
const rpc = (address, port, method, params, options = {}) => {
const rpc = (address, port, method, params, options = {}, seedRequest = false) => {
const headers = options.headers || {};
const portString = port ? `:${port}` : '';
const url = `${address}${portString}${endpointBase}`;
const endpoint = seedRequest ? seedEndpointBase : endpointBase;
const url = `${address}${portString}${endpoint}`;
// TODO: The jsonrpc and body field will be ignored on storage server
const body = {
jsonrpc: '2.0',
id: '0',
method,
params,
};
@ -123,6 +128,7 @@ const rpc = (address, port, method, params, options = {}) => {
return fetch(url, fetchOptions);
};
module.exports = {
rpc,
};

@ -65,9 +65,29 @@ class LokiSnodeAPI {
return resolveCname(this.localUrl);
}
getRandomSnodeAddress() {
async getRandomSnodeAddress() {
/* resolve random snode */
return resolveCname(this.serverUrl);
if (this.randomSnodePool.length === 0) {
await this.initialiseRandomPool();
}
return this.randomSnodePool[Math.floor(Math.random() * this.randomSnodePool.length)];
}
async initialiseRandomPool() {
const result = await rpc(
`http://${window.seedNodeUrl}`,
window.seedNodePort,
'get_service_nodes',
{}, // Params
{}, // Options
true // Seed request
);
const snodes = result.result.service_node_states;
this.randomSnodePool = snodes.map(snode => ({
address: snode.public_ip,
port: snode.storage_port,
})
);
}
async unreachableNode(pubKey, nodeUrl) {
@ -121,10 +141,11 @@ class LokiSnodeAPI {
async updateOurSwarmNodes(newNodes) {
this.ourSwarmNodes = {};
const ps = newNodes.map(async url => {
const lastHash = await window.Signal.Data.getLastHashBySnode(url);
this.ourSwarmNodes[url] = {
const ps = newNodes.map(async snode => {
const lastHash = await window.Signal.Data.getLastHashBySnode(snode.address);
this.ourSwarmNodes[snode.address] = {
lastHash,
port: snode.port,
};
});
await Promise.all(ps);
@ -167,11 +188,11 @@ class LokiSnodeAPI {
async getSwarmNodes(pubKey) {
// TODO: Hit multiple random nodes and merge lists?
const nodeUrl = await this.getRandomSnodeAddress();
const { address, port } = await this.getRandomSnodeAddress();
const result = await rpc(
`https://${nodeUrl}`,
this.snodeServerPort,
`https://${address}`,
port,
'get_snodes_for_pubkey',
{
pubKey,

@ -156,6 +156,8 @@ function prepareURL(pathSegments, moreKeys) {
cdnUrl: config.get('cdnUrl'),
localServerPort: config.get('localServerPort'),
defaultPoWDifficulty: config.get('defaultPoWDifficulty'),
seedNodeUrl: config.get('seedNodeUrl'),
seedNodePort: config.get('seedNodePort'),
certificateAuthority: config.get('certificateAuthority'),
environment: config.environment,
node_version: process.versions.node,

@ -288,6 +288,8 @@ window.WebAPI = initializeWebAPI({
proxyUrl: config.proxyUrl,
});
window.seedNodeUrl = config.seedNodeUrl;
window.seedNodePort = config.seedNodePort;
const LokiSnodeAPI = require('./js/modules/loki_snode_api');
window.lokiSnodeAPI = new LokiSnodeAPI({

Loading…
Cancel
Save