A few small bugfixes

This commit is contained in:
Scott Nonnenberg 2018-11-08 17:23:07 -08:00
parent 59d048ca06
commit c5f9fae444
2 changed files with 36 additions and 23 deletions

View file

@ -160,6 +160,7 @@ function _createSocket(url, { certificateAuthority, proxyUrl }) {
return new WebSocket(url, null, null, null, requestOptions);
}
const FIVE_MINUTES = 1000 * 60 * 5;
const agents = {
unauth: null,
auth: null,
@ -175,16 +176,21 @@ function _promiseAjax(providedUrl, options) {
typeof options.timeout !== 'undefined' ? options.timeout : 10000;
const { proxyUrl } = options;
const agentType = options.unathenticated ? 'unauth' : 'auth';
const agentType = options.unauthenticated ? 'unauth' : 'auth';
if (!agents[agentType]) {
if (proxyUrl) {
agents[agentType] = new ProxyAgent(proxyUrl);
} else {
agents[agentType] = new Agent();
const { timestamp } = agents[agentType] || {};
if (!timestamp || timestamp + FIVE_MINUTES < Date.now()) {
if (timestamp) {
log.info(`Cycling agent for type ${agentType}`);
}
agents[agentType] = {
agent: proxyUrl
? new ProxyAgent(proxyUrl)
: new Agent({ keepAlive: true }),
timestamp: Date.now(),
};
}
const agent = agents[agentType];
const { agent } = agents[agentType];
const fetchOptions = {
method: options.type,
@ -436,7 +442,7 @@ function initialize({ url, cdnUrl, certificateAuthority, proxyUrl }) {
message =
'The server rejected our query, please file a bug report.';
}
e.message = message;
e.message = `${message} (original: ${e.message})`;
throw e;
});
}

View file

@ -287,6 +287,9 @@ MessageReceiver.prototype.extend({
}
envelope.id = envelope.serverGuid || window.getGuid();
envelope.serverTimestamp = envelope.serverTimestamp
? envelope.serverTimestamp.toNumber()
: null;
return this.addToCache(envelope, plaintext).then(
async () => {
@ -404,6 +407,11 @@ MessageReceiver.prototype.extend({
);
}
const envelope = textsecure.protobuf.Envelope.decode(envelopePlaintext);
envelope.id = envelope.serverGuid || item.id;
envelope.source = envelope.source || item.source;
envelope.sourceDevice = envelope.sourceDevice || item.sourceDevice;
envelope.serverTimestamp =
envelope.serverTimestamp || item.serverTimestamp;
const { decrypted } = item;
if (decrypted) {
@ -516,15 +524,19 @@ MessageReceiver.prototype.extend({
}
if (item.get('version') === 2) {
item.set(
'decrypted',
await MessageReceiver.arrayBufferToStringBase64(plaintext)
);
item.set({
source: envelope.source,
sourceDevice: envelope.sourceDevice,
serverTimestamp: envelope.serverTimestamp,
decrypted: await MessageReceiver.arrayBufferToStringBase64(plaintext),
});
} else {
item.set(
'decrypted',
await MessageReceiver.arrayBufferToString(plaintext)
);
item.set({
source: envelope.source,
sourceDevice: envelope.sourceDevice,
serverTimestamp: envelope.serverTimestamp,
decrypted: await MessageReceiver.arrayBufferToString(plaintext),
});
}
return textsecure.storage.unprocessed.save(item.attributes);
@ -691,12 +703,7 @@ MessageReceiver.prototype.extend({
.decrypt(
window.Signal.Metadata.createCertificateValidator(serverTrustRoot),
ciphertext.toArrayBuffer(),
Math.min(
envelope.serverTimestamp
? envelope.serverTimestamp.toNumber()
: Date.now(),
Date.now()
),
Math.min(envelope.serverTimestamp || Date.now(), Date.now()),
me
)
.then(
@ -737,7 +744,7 @@ MessageReceiver.prototype.extend({
throw error;
}
return this.removeFromCache().then(() => {
return this.removeFromCache(envelope).then(() => {
throw error;
});
}