diff --git a/Gruntfile.js b/Gruntfile.js index de38eccd6..6e4025ea2 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -53,6 +53,7 @@ module.exports = function(grunt) { 'libtextsecure/protobufs.js', 'libtextsecure/websocket.js', 'libtextsecure/websocket-resources.js', + 'libtextsecure/keepalive.js', 'libtextsecure/helpers.js', 'libtextsecure/stringview.js', 'libtextsecure/api.js', diff --git a/js/libtextsecure.js b/js/libtextsecure.js index e6dd64756..c127d9bda 100644 --- a/js/libtextsecure.js +++ b/js/libtextsecure.js @@ -38602,6 +38602,48 @@ TextSecureWebSocket = function (url, opts) { }()); +/* vim: ts=4:sw=4:expandtab + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +function KeepAlive(websocketResource, socket) { + if (websocketResource instanceof WebSocketResource) { + this.wsr = websocketResource; + this.socket = socket; + this.reset(); + } else { + throw new TypeError('KeepAlive expected a WebSocketResource'); + } +} + +KeepAlive.prototype = { + constructor: KeepAlive, + reset: function() { + clearTimeout(this.keepAliveTimer); + clearTimeout(this.disconnectTimer); + this.keepAliveTimer = setTimeout(function() { + this.disconnectTimer = setTimeout(this.socket.close, 10000); + this.wsr.sendRequest({ + verb: 'GET', + path: '/v1/keepalive', + success: this.reset.bind(this) + }); + }.bind(this), 55000); + }, +}; + /* vim: ts=4:sw=4:expandtab * * This program is free software: you can redistribute it and/or modify @@ -39319,7 +39361,7 @@ TextSecureServer = function () { return textsecure.protocol_wrapper.createIdentityKeyRecvSocket().then(function(cryptoInfo) { return new Promise(function(resolve) { var socket = TextSecureServer.getTempWebsocket(); - new WebSocketResource(socket, function(request) { + var wsr = new WebSocketResource(socket, function(request) { if (request.path == "/v1/address" && request.verb == "PUT") { var proto = textsecure.protobuf.ProvisioningUuid.decode(request.body); setProvisioningUrl([ @@ -39348,6 +39390,7 @@ TextSecureServer = function () { console.log('Unknown websocket message', request.path); } }); + new KeepAlive(wsr, socket); }); }).then(function() { return generateKeys(100, progressCallback); @@ -39495,25 +39538,11 @@ function generateKeys(count, progressCallback) { } this.wsr = new WebSocketResource(this.socket, this.handleRequest.bind(this)); - this.resetKeepAliveTimer(); + this.keepalive = new KeepAlive(this.wsr, this.socket); }, - resetKeepAliveTimer: function() { - clearTimeout(this.keepAliveTimer); - clearTimeout(this.disconnectTimer); - this.keepAliveTimer = setTimeout(function() { - if (this.getStatus() === WebSocket.OPEN) { - this.wsr.sendRequest({ - verb: 'GET', - path: '/v1/keepalive', - success: this.resetKeepAliveTimer.bind(this) - }); - } - this.disconnectTimer = setTimeout(this.socket.close, 30000); - }.bind(this), 55000); - }, handleRequest: function(request) { - this.resetKeepAliveTimer(); + this.keepalive.reset(); // TODO: handle different types of requests. for now we only expect // PUT /messages textsecure.crypto.decryptWebsocketMessage(request.body).then(function(plaintext) { diff --git a/libtextsecure/account_manager.js b/libtextsecure/account_manager.js index 70e24d596..7df05a5ba 100644 --- a/libtextsecure/account_manager.js +++ b/libtextsecure/account_manager.js @@ -44,7 +44,7 @@ return textsecure.protocol_wrapper.createIdentityKeyRecvSocket().then(function(cryptoInfo) { return new Promise(function(resolve) { var socket = TextSecureServer.getTempWebsocket(); - new WebSocketResource(socket, function(request) { + var wsr = new WebSocketResource(socket, function(request) { if (request.path == "/v1/address" && request.verb == "PUT") { var proto = textsecure.protobuf.ProvisioningUuid.decode(request.body); setProvisioningUrl([ @@ -73,6 +73,7 @@ console.log('Unknown websocket message', request.path); } }); + new KeepAlive(wsr, socket); }); }).then(function() { return generateKeys(100, progressCallback); diff --git a/libtextsecure/keepalive.js b/libtextsecure/keepalive.js new file mode 100644 index 000000000..1dcfccbe5 --- /dev/null +++ b/libtextsecure/keepalive.js @@ -0,0 +1,41 @@ +/* vim: ts=4:sw=4:expandtab + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +function KeepAlive(websocketResource, socket) { + if (websocketResource instanceof WebSocketResource) { + this.wsr = websocketResource; + this.socket = socket; + this.reset(); + } else { + throw new TypeError('KeepAlive expected a WebSocketResource'); + } +} + +KeepAlive.prototype = { + constructor: KeepAlive, + reset: function() { + clearTimeout(this.keepAliveTimer); + clearTimeout(this.disconnectTimer); + this.keepAliveTimer = setTimeout(function() { + this.disconnectTimer = setTimeout(this.socket.close, 10000); + this.wsr.sendRequest({ + verb: 'GET', + path: '/v1/keepalive', + success: this.reset.bind(this) + }); + }.bind(this), 55000); + }, +}; diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index aa400c4d3..4850603d6 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -45,25 +45,11 @@ } this.wsr = new WebSocketResource(this.socket, this.handleRequest.bind(this)); - this.resetKeepAliveTimer(); + this.keepalive = new KeepAlive(this.wsr, this.socket); }, - resetKeepAliveTimer: function() { - clearTimeout(this.keepAliveTimer); - clearTimeout(this.disconnectTimer); - this.keepAliveTimer = setTimeout(function() { - if (this.getStatus() === WebSocket.OPEN) { - this.wsr.sendRequest({ - verb: 'GET', - path: '/v1/keepalive', - success: this.resetKeepAliveTimer.bind(this) - }); - } - this.disconnectTimer = setTimeout(this.socket.close, 30000); - }.bind(this), 55000); - }, handleRequest: function(request) { - this.resetKeepAliveTimer(); + this.keepalive.reset(); // TODO: handle different types of requests. for now we only expect // PUT /messages textsecure.crypto.decryptWebsocketMessage(request.body).then(function(plaintext) {