Skip to content

Commit

Permalink
Merge pull request #1037 from neuroscr/patchopensnapps
Browse files Browse the repository at this point in the history
Fix tokens for File Server/Open Groups And other important snode comm fixes
  • Loading branch information
neuroscr committed Apr 1, 2020
2 parents 9dbfac3 + 9ade8cb commit acdb62f
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 18 deletions.
13 changes: 7 additions & 6 deletions js/modules/loki_app_dot_net_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ const serverRequest = async (endpoint, options = {}) => {
));
} else {
// disable check for .loki
process.env.NODE_TLS_REJECT_UNAUTHORIZED = url.host.match(/\.loki$/i)
process.env.NODE_TLS_REJECT_UNAUTHORIZED = host.match(/\.loki$/i)
? '0'
: '1';
result = await nodeFetch(url, fetchOptions);
Expand Down Expand Up @@ -298,7 +298,7 @@ const serverRequest = async (endpoint, options = {}) => {
url
);
}
if (mode === '_sendToProxy') {
if (mode === 'sendToProxy') {
// if we can detect, certain types of failures, we can retry...
if (e.code === 'ECONNRESET') {
// retry with counter?
Expand Down Expand Up @@ -658,7 +658,7 @@ class LokiAppDotNetServerAPI {

try {
const res = await this.proxyFetch(
`${this.baseServerUrl}/loki/v1/submit_challenge`,
new URL(`${this.baseServerUrl}/loki/v1/submit_challenge`),
fetchOptions,
{ textResponse: true }
);
Expand All @@ -683,7 +683,8 @@ class LokiAppDotNetServerAPI {
}
const urlStr = urlObj.toString();
const endpoint = urlStr.replace(`${this.baseServerUrl}/`, '');
const { response, result } = await this._sendToProxy(
const { response, result } = await sendToProxy(
this.pubKey,
endpoint,
finalOptions,
options
Expand All @@ -694,8 +695,8 @@ class LokiAppDotNetServerAPI {
json: () => response,
};
}
const urlStr = urlObj.toString();
if (urlStr.match(/\.loki\//)) {
const host = urlObj.host.toLowerCase();
if (host.match(/\.loki$/)) {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
}
const result = nodeFetch(urlObj, fetchOptions, options);
Expand Down
21 changes: 21 additions & 0 deletions js/modules/loki_message_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,18 @@ class LokiMessageAPI {
targetNode
);

// do not return true if we get false here...
if (result === false) {
log.warn(
`loki_message:::_sendToNode - Got false from ${targetNode.ip}:${
targetNode.port
}`
);
successiveFailures += 1;
// eslint-disable-next-line no-continue
continue;
}

// Make sure we aren't doing too much PoW
const currentDifficulty = window.storage.get('PoWDifficulty', null);
if (
Expand Down Expand Up @@ -387,6 +399,15 @@ class LokiMessageAPI {
nodeData
);

if (result === false) {
// make a note of it because of caller doesn't care...
log.warn(
`loki_message:::_retrieveNextMessages - lokiRpc returned false to ${
nodeData.ip
}:${nodeData.port}`
);
}

return result.messages || [];
}

Expand Down
47 changes: 41 additions & 6 deletions js/modules/loki_rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,24 @@ const processOnionResponse = async (reqIdx, response, sharedKey, useAesGcm) => {
const sendToProxy = async (options = {}, targetNode, retryNumber = 0) => {
const _ = window.Lodash;

const snodePool = await lokiSnodeAPI.getRandomSnodePool();
let snodePool = await lokiSnodeAPI.getRandomSnodePool();

if (snodePool.length < 2) {
log.error(
'Not enough service nodes for a proxy request, only have: ',
snodePool.length
'lokiRpc::sendToProxy - Not enough service nodes for a proxy request, only have:',
snodePool.length,
'snode, attempting refresh'
);
return false;
await lokiSnodeAPI.refreshRandomPool();
snodePool = await lokiSnodeAPI.getRandomSnodePool();
if (snodePool.length < 2) {
log.error(
'lokiRpc::sendToProxy - Not enough service nodes for a proxy request, only have:',
snodePool.length,
'failing'
);
return false;
}
}

// Making sure the proxy node is not the same as the target node:
Expand Down Expand Up @@ -292,7 +302,11 @@ const sendToProxy = async (options = {}, targetNode, retryNumber = 0) => {
// it's likely a net problem or an actual problem on the target node
// lets mark the target node bad for now
// we'll just rotate it back in if it's a net problem
log.warn(`Failing ${targetNode.ip}:${targetNode.port} after 5 retries`);
log.warn(
`lokiRpc:::sendToProxy - Failing ${targetNode.ip}:${
targetNode.port
} after 5 retries`
);
if (options.ourPubKey) {
lokiSnodeAPI.unreachableNode(options.ourPubKey, targetNode);
}
Expand Down Expand Up @@ -329,7 +343,11 @@ const sendToProxy = async (options = {}, targetNode, retryNumber = 0) => {
// avoid base64 decode failure
// usually a 500 but not always
// could it be a timeout?
log.warn('Server did not return any data for', options, targetNode);
log.warn(
'lokiRpc:::sendToProxy - Server did not return any data for',
options,
targetNode
);
return false;
}

Expand Down Expand Up @@ -404,6 +422,7 @@ const sendToProxy = async (options = {}, targetNode, retryNumber = 0) => {
};

// A small wrapper around node-fetch which deserializes response
// returns nodeFetch response or false
const lokiFetch = async (url, options = {}, targetNode = null) => {
const timeout = options.timeout || 10000;
const method = options.method || 'GET';
Expand Down Expand Up @@ -454,6 +473,22 @@ const lokiFetch = async (url, options = {}, targetNode = null) => {

if (window.lokiFeatureFlags.useSnodeProxy && targetNode) {
const result = await sendToProxy(fetchOptions, targetNode);
if (result === false) {
// should we retry?
log.warn(`lokiRpc:::lokiFetch - sendToProxy returned false`);
// one case is:
// snodePool didn't have enough
// even after a refresh
// likely a network disconnect?
// but not all cases...
/*
log.warn(
'lokiRpc:::lokiFetch - useSnodeProxy failure, could not refresh randomPool, offline?'
);
*/
// pass the false value up
return false;
}
// if not result, maybe we should throw??
return result ? result.json() : {};
}
Expand Down
23 changes: 21 additions & 2 deletions js/modules/loki_snode_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,36 @@ class LokiSnodeAPI {
async selectGuardNodes() {
const _ = window.Lodash;

const nodePool = await this.getRandomSnodePool();
let nodePool = await this.getRandomSnodePool();

if (nodePool.length === 0) {
log.error(`Could not select guarn nodes: node pool is empty`);
return [];
}

const shuffled = _.shuffle(nodePool);
let shuffled = _.shuffle(nodePool);

let guardNodes = [];

const DESIRED_GUARD_COUNT = 3;
if (shuffled.length < DESIRED_GUARD_COUNT) {
log.error(
`Could not select guarn nodes: node pool is not big enough, pool size ${
shuffled.length
}, need ${DESIRED_GUARD_COUNT}, attempting to refresh randomPool`
);
await this.refreshRandomPool();
nodePool = await this.getRandomSnodePool();
shuffled = _.shuffle(nodePool);
if (shuffled.length < DESIRED_GUARD_COUNT) {
log.error(
`Could not select guarn nodes: node pool is not big enough, pool size ${
shuffled.length
}, need ${DESIRED_GUARD_COUNT}, failing...`
);
return [];
}
}

// The use of await inside while is intentional:
// we only want to repeat if the await fails
Expand Down Expand Up @@ -490,6 +508,7 @@ class LokiSnodeAPI {
throw new window.textsecure.SeedNodeError('Failed to contact seed node');
}
log.info('loki_snodes:::refreshRandomPoolPromise - RESOLVED');
delete this.refreshRandomPoolPromise; // clear any lock
}

// unreachableNode.url is like 9hrje1bymy7hu6nmtjme9idyu3rm8gr3mkstakjyuw1997t7w4ny.snode
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "session-messenger-desktop",
"productName": "Session",
"description": "Private messaging from your desktop",
"version": "1.0.4",
"version": "1.0.5",
"license": "GPL-3.0",
"author": {
"name": "Loki Project",
Expand Down
11 changes: 8 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6065,6 +6065,11 @@ lodash-es@^4.2.1:
resolved "https://registry.yarnpkg.com/lodash-es/-/lodash-es-4.17.15.tgz#21bd96839354412f23d7a10340e5eac6ee455d78"
integrity sha512-rlrc3yU3+JNOpZ9zj5pQtxnx2THmvRykwL4Xlxoa8I9lHBlVbbyPhgyPMioxVZ4NqyxaVVtaJnzsyOidQIhyyQ==

lodash-es@^4.2.1:
version "4.17.15"
resolved "https://registry.yarnpkg.com/lodash-es/-/lodash-es-4.17.15.tgz#21bd96839354412f23d7a10340e5eac6ee455d78"
integrity sha512-rlrc3yU3+JNOpZ9zj5pQtxnx2THmvRykwL4Xlxoa8I9lHBlVbbyPhgyPMioxVZ4NqyxaVVtaJnzsyOidQIhyyQ==

lodash.assign@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/lodash.assign/-/lodash.assign-4.2.0.tgz#0d99f3ccd7a6d261d19bdaeb9245005d285808e7"
Expand Down Expand Up @@ -8746,8 +8751,8 @@ redent@^1.0.0:
resolved "https://registry.yarnpkg.com/redent/-/redent-1.0.0.tgz#cf916ab1fd5f1f16dfb20822dd6ec7f730c2afde"
integrity sha1-z5Fqsf1fHxbfsggi3W7H9zDCr94=
dependencies:
indent-string "^2.1.0"
strip-indent "^1.0.1"
loose-envify "^1.4.0"
symbol-observable "^1.2.0"

reduce-css-calc@^1.2.6:
version "1.3.0"
Expand Down Expand Up @@ -11213,7 +11218,7 @@ write-file-atomic@^1.1.4:
dependencies:
graceful-fs "^4.1.11"
imurmurhash "^0.1.4"
slide "^1.1.5"
signal-exit "^3.0.2"

write-file-atomic@^3.0.0:
version "3.0.3"
Expand Down

0 comments on commit acdb62f

Please sign in to comment.