From 1a06cb5cbe939fddf3b1a5b2dbefe2475e0dbd94 Mon Sep 17 00:00:00 2001 From: lilia Date: Tue, 28 Jul 2015 14:58:55 -0700 Subject: [PATCH] Update consumers of WebsocketResources --- js/libtextsecure.js | 62 ++++++++++++++++--------------- libtextsecure/account_manager.js | 59 ++++++++++++++--------------- libtextsecure/message_receiver.js | 3 +- 3 files changed, 64 insertions(+), 60 deletions(-) diff --git a/js/libtextsecure.js b/js/libtextsecure.js index d0457fd9..e8ee6e0b 100644 --- a/js/libtextsecure.js +++ b/js/libtextsecure.js @@ -39387,36 +39387,37 @@ TextSecureServer = function () { return textsecure.protocol_wrapper.createIdentityKeyRecvSocket().then(function(cryptoInfo) { return new Promise(function(resolve) { var socket = TextSecureServer.getTempWebsocket(); - var wsr = new WebSocketResource(socket, function(request) { - if (request.path == "/v1/address" && request.verb == "PUT") { - var proto = textsecure.protobuf.ProvisioningUuid.decode(request.body); - setProvisioningUrl([ - 'tsdevice:/?uuid=', proto.uuid, '&pub_key=', - encodeURIComponent(btoa(getString(cryptoInfo.pubKey))) - ].join('')); - request.respond(200, 'OK'); - } else if (request.path == "/v1/message" && request.verb == "PUT") { - var envelope = textsecure.protobuf.ProvisionEnvelope.decode(request.body, 'binary'); - request.respond(200, 'OK'); - wsr.close(); - resolve(cryptoInfo.decryptAndHandleDeviceInit(envelope).then(function(provisionMessage) { - return confirmNumber(provisionMessage.number).then(function(deviceName) { - if (typeof deviceName !== 'string' || deviceName.length == 0) { - throw new Error('Invalid device name'); - } - return createAccount( - provisionMessage.number, - provisionMessage.provisioningCode, - provisionMessage.identityKeyPair, - deviceName - ); - }); - })); - } else { - console.log('Unknown websocket message', request.path); + var wsr = new WebSocketResource(socket, { + keepalive: { path: '/v1/keepalive' }, + handleRequest: function(request) { + if (request.path == "/v1/address" && request.verb == "PUT") { + var proto = textsecure.protobuf.ProvisioningUuid.decode(request.body); + setProvisioningUrl([ + 'tsdevice:/?uuid=', proto.uuid, '&pub_key=', + encodeURIComponent(btoa(getString(cryptoInfo.pubKey))) + ].join('')); + request.respond(200, 'OK'); + } else if (request.path == "/v1/message" && request.verb == "PUT") { + var envelope = textsecure.protobuf.ProvisionEnvelope.decode(request.body, 'binary'); + request.respond(200, 'OK'); + wsr.close(); + resolve(cryptoInfo.decryptAndHandleDeviceInit(envelope).then(function(provisionMessage) { + return confirmNumber(provisionMessage.number).then(function(deviceName) { + if (typeof deviceName !== 'string' || deviceName.length == 0) { + throw new Error('Invalid device name'); + } + return createAccount( + provisionMessage.number, + provisionMessage.provisioningCode, + provisionMessage.identityKeyPair, + deviceName + ); + }); + })); + } else { + console.log('Unknown websocket message', request.path); + } } - }, { - keepalive: { path: '/v1/keepalive' } }); }); }).then(function() { @@ -39564,7 +39565,8 @@ function generateKeys(count, progressCallback) { } }; - this.wsr = new WebSocketResource(this.socket, this.handleRequest.bind(this), { + this.wsr = new WebSocketResource(this.socket, { + handleRequest: this.handleRequest.bind(this), keepalive: { path: '/v1/keepalive', disconnect: true } }); diff --git a/libtextsecure/account_manager.js b/libtextsecure/account_manager.js index e5a11413..b2342fa2 100644 --- a/libtextsecure/account_manager.js +++ b/libtextsecure/account_manager.js @@ -44,36 +44,37 @@ return textsecure.protocol_wrapper.createIdentityKeyRecvSocket().then(function(cryptoInfo) { return new Promise(function(resolve) { var socket = TextSecureServer.getTempWebsocket(); - var wsr = new WebSocketResource(socket, function(request) { - if (request.path == "/v1/address" && request.verb == "PUT") { - var proto = textsecure.protobuf.ProvisioningUuid.decode(request.body); - setProvisioningUrl([ - 'tsdevice:/?uuid=', proto.uuid, '&pub_key=', - encodeURIComponent(btoa(getString(cryptoInfo.pubKey))) - ].join('')); - request.respond(200, 'OK'); - } else if (request.path == "/v1/message" && request.verb == "PUT") { - var envelope = textsecure.protobuf.ProvisionEnvelope.decode(request.body, 'binary'); - request.respond(200, 'OK'); - wsr.close(); - resolve(cryptoInfo.decryptAndHandleDeviceInit(envelope).then(function(provisionMessage) { - return confirmNumber(provisionMessage.number).then(function(deviceName) { - if (typeof deviceName !== 'string' || deviceName.length == 0) { - throw new Error('Invalid device name'); - } - return createAccount( - provisionMessage.number, - provisionMessage.provisioningCode, - provisionMessage.identityKeyPair, - deviceName - ); - }); - })); - } else { - console.log('Unknown websocket message', request.path); + var wsr = new WebSocketResource(socket, { + keepalive: { path: '/v1/keepalive' }, + handleRequest: function(request) { + if (request.path == "/v1/address" && request.verb == "PUT") { + var proto = textsecure.protobuf.ProvisioningUuid.decode(request.body); + setProvisioningUrl([ + 'tsdevice:/?uuid=', proto.uuid, '&pub_key=', + encodeURIComponent(btoa(getString(cryptoInfo.pubKey))) + ].join('')); + request.respond(200, 'OK'); + } else if (request.path == "/v1/message" && request.verb == "PUT") { + var envelope = textsecure.protobuf.ProvisionEnvelope.decode(request.body, 'binary'); + request.respond(200, 'OK'); + wsr.close(); + resolve(cryptoInfo.decryptAndHandleDeviceInit(envelope).then(function(provisionMessage) { + return confirmNumber(provisionMessage.number).then(function(deviceName) { + if (typeof deviceName !== 'string' || deviceName.length == 0) { + throw new Error('Invalid device name'); + } + return createAccount( + provisionMessage.number, + provisionMessage.provisioningCode, + provisionMessage.identityKeyPair, + deviceName + ); + }); + })); + } else { + console.log('Unknown websocket message', request.path); + } } - }, { - keepalive: { path: '/v1/keepalive' } }); }); }).then(function() { diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index ef2f251b..7b8c3ae2 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -44,7 +44,8 @@ } }; - this.wsr = new WebSocketResource(this.socket, this.handleRequest.bind(this), { + this.wsr = new WebSocketResource(this.socket, { + handleRequest: this.handleRequest.bind(this), keepalive: { path: '/v1/keepalive', disconnect: true } });