mirror of
https://github.com/oxen-io/session-desktop.git
synced 2023-12-14 02:12:57 +01:00
Do port null check inside rpc call
This commit is contained in:
parent
52c191fc94
commit
eea2b8e6a7
6 changed files with 35 additions and 43 deletions
|
@ -4,7 +4,6 @@
|
|||
"cdnUrl": "random.snode",
|
||||
"contentProxyUrl": "random.snode",
|
||||
"localServerPort": "8081",
|
||||
"snodeServerPort": "8080",
|
||||
"defaultPoWDifficulty": "100",
|
||||
"disableAutoUpdate": false,
|
||||
"updatesUrl": "https://updates2.signal.org/desktop",
|
||||
|
|
|
@ -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
|
||||
);
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
1
main.js
1
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'),
|
||||
|
|
|
@ -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');
|
||||
|
||||
|
|
Loading…
Reference in a new issue