From 0487fa3cd1a0488b0765a12bdf23b54b8cd1d8a3 Mon Sep 17 00:00:00 2001 From: lilia Date: Wed, 7 Sep 2016 21:53:56 -0700 Subject: [PATCH] Add server fallback port 8443 // FREEBIE --- Gruntfile.js | 4 +- js/background.js | 9 ++-- js/libtextsecure.js | 87 ++++++++++++++++++++----------- libtextsecure/account_manager.js | 4 +- libtextsecure/api.js | 67 +++++++++++++++++------- libtextsecure/message_receiver.js | 8 +-- libtextsecure/sendmessage.js | 8 +-- 7 files changed, 121 insertions(+), 66 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 048a49e3..3cf90041 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -124,8 +124,8 @@ module.exports = function(grunt) { process: function(content, srcpath) { if (srcpath.match('background.js')) { return content.replace( - /textsecure-service-staging.whispersystems.org:4433/g, - 'textsecure-service-ca.whispersystems.org:4433').replace( + /textsecure-service-staging.whispersystems.org/g, + 'textsecure-service-ca.whispersystems.org').replace( /whispersystems-textsecure-attachments-staging.s3.amazonaws.com/g, 'whispersystems-textsecure-attachments.s3.amazonaws.com'); } else if (srcpath.match('expire.js')) { diff --git a/js/background.js b/js/background.js index 1417822e..b50db2a1 100644 --- a/js/background.js +++ b/js/background.js @@ -51,7 +51,8 @@ }); }); - var SERVER_URL = 'https://textsecure-service-staging.whispersystems.org:4433'; + var SERVER_URL = 'https://textsecure-service-staging.whispersystems.org'; + var SERVER_PORTS = [4433, 8443]; var ATTACHMENT_SERVER_URL = 'https://whispersystems-textsecure-attachments-staging.s3.amazonaws.com'; var messageReceiver; window.getSocketStatus = function() { @@ -64,7 +65,7 @@ window.getAccountManager = function() { var USERNAME = storage.get('number_id'); var PASSWORD = storage.get('password'); - return new textsecure.AccountManager(SERVER_URL, USERNAME, PASSWORD); + return new textsecure.AccountManager(SERVER_URL, SERVER_PORTS, USERNAME, PASSWORD); }; storage.fetch(); @@ -103,7 +104,7 @@ // initialize the socket and start listening for messages messageReceiver = new textsecure.MessageReceiver( - SERVER_URL, USERNAME, PASSWORD, mySignalingKey, ATTACHMENT_SERVER_URL + SERVER_URL, SERVER_PORTS, USERNAME, PASSWORD, mySignalingKey, ATTACHMENT_SERVER_URL ); messageReceiver.addEventListener('message', onMessageReceived); messageReceiver.addEventListener('receipt', onDeliveryReceipt); @@ -114,7 +115,7 @@ messageReceiver.addEventListener('error', onError); window.textsecure.messaging = new textsecure.MessageSender( - SERVER_URL, USERNAME, PASSWORD, ATTACHMENT_SERVER_URL + SERVER_URL, SERVER_PORTS, USERNAME, PASSWORD, ATTACHMENT_SERVER_URL ); if (firstRun === true && textsecure.storage.user.getDeviceId() != '1') { var syncRequest = new textsecure.SyncRequest(textsecure.messaging, messageReceiver); diff --git a/js/libtextsecure.js b/js/libtextsecure.js index 44f1ff3e..2e69b19d 100644 --- a/js/libtextsecure.js +++ b/js/libtextsecure.js @@ -37474,6 +37474,18 @@ window.textsecure.utils = function() { * vim: ts=4:sw=4:expandtab */ +function PortManager(ports) { + this.ports = ports; + this.idx = 0; +} + +PortManager.prototype = { + constructor: PortManager, + getPort: function() { + return this.ports[this.idx++]; + } +}; + var TextSecureServer = (function() { 'use strict'; @@ -37499,6 +37511,9 @@ var TextSecureServer = (function() { // Promise-based async xhr routine function promise_ajax(url, options) { return new Promise(function (resolve, reject) { + if (!url) { + url = options.host + ':' + options.port + '/' + options.path; + } console.log(options.type, url); var xhr = new XMLHttpRequest(); xhr.open(options.type, url, true /*async*/); @@ -37546,24 +37561,29 @@ var TextSecureServer = (function() { }); } - function ajax(url, options) { - options.stack = new Error().stack; // just in case, save stack here. - var count = 3; - - function retry(e) { - if (e.name === 'HTTPError' && e.code === -1 && count > 0) { - count = count - 1; + function retry_ajax(url, options, limit, count) { + count = count || 0; + limit = limit || 3; + if (options.ports) { + options.port = options.ports[count % options.ports.length]; + } + count++; + return promise_ajax(url, options).catch(function(e) { + if (e.name === 'HTTPError' && e.code === -1 && count < limit) { return new Promise(function(resolve) { setTimeout(function() { - resolve(promise_ajax(url, options).catch(retry)); - }, 1000 ); + resolve(retry_ajax(url, options, limit, count)); + }, 1000); }); } else { throw e; } - } + }); + } - return promise_ajax(url, options).catch(retry); + function ajax(url, options) { + options.stack = new Error().stack; // just in case, save stack here. + return retry_ajax(url, options); } function HTTPError(code, response, stack) { @@ -37581,17 +37601,18 @@ var TextSecureServer = (function() { } var URL_CALLS = { - accounts : "/v1/accounts", - devices : "/v1/devices", - keys : "/v2/keys", - messages : "/v1/messages", - attachment : "/v1/attachments" + accounts : "v1/accounts", + devices : "v1/devices", + keys : "v2/keys", + messages : "v1/messages", + attachment : "v1/attachments" }; - function TextSecureServer(url, username, password, attachment_server_url) { + function TextSecureServer(url, ports, username, password, attachment_server_url) { if (typeof url !== 'string') { throw new Error('Invalid server url'); } + this.portManager = new PortManager(ports); this.url = url; this.username = username; this.password = password; @@ -37610,11 +37631,17 @@ var TextSecureServer = (function() { TextSecureServer.prototype = { constructor: TextSecureServer, + getUrl: function() { + return this.url + ':' + this.portManager.getPort(); + }, ajax: function(param) { if (!param.urlParameters) { param.urlParameters = ''; } - return ajax(this.url + URL_CALLS[param.call] + param.urlParameters, { + return ajax(null, { + host : this.url, + ports : this.portManager.ports, + path : URL_CALLS[param.call] + param.urlParameters, type : param.httpType, data : param.jsonData && textsecure.utils.jsonThing(param.jsonData), contentType : 'application/json; charset=utf-8', @@ -37823,7 +37850,7 @@ var TextSecureServer = (function() { }, getMessageSocket: function() { return new WebSocket( - this.url.replace('https://', 'wss://') + this.getUrl().replace('https://', 'wss://') .replace('http://', 'ws://') + '/v1/websocket/?login=' + encodeURIComponent(this.username) + '&password=' + encodeURIComponent(this.password) @@ -37833,7 +37860,7 @@ var TextSecureServer = (function() { getProvisioningSocket: function () { console.log('opening provisioning socket', this.url); return new WebSocket( - this.url.replace('https://', 'wss://') + this.getUrl().replace('https://', 'wss://') .replace('http://', 'ws://') + '/v1/websocket/provisioning/?agent=OWD' ); @@ -37852,8 +37879,8 @@ var TextSecureServer = (function() { 'use strict'; window.textsecure = window.textsecure || {}; - function AccountManager(url, username, password) { - this.server = new TextSecureServer(url, username, password); + function AccountManager(url, ports, username, password) { + this.server = new TextSecureServer(url, ports, username, password); } AccountManager.prototype = { @@ -38032,12 +38059,12 @@ var TextSecureServer = (function() { * vim: ts=4:sw=4:expandtab */ -function MessageReceiver(url, username, password, signalingKey, attachment_server_url) { +function MessageReceiver(url, ports, username, password, signalingKey, attachment_server_url) { this.url = url; this.signalingKey = signalingKey; this.username = username; this.password = password; - this.server = new TextSecureServer(url, username, password, attachment_server_url); + this.server = new TextSecureServer(url, ports, username, password, attachment_server_url); var address = libsignal.SignalProtocolAddress.fromString(username); this.number = address.getName(); @@ -38502,8 +38529,8 @@ MessageReceiver.prototype.extend({ window.textsecure = window.textsecure || {}; -textsecure.MessageReceiver = function(url, username, password, signalingKey, attachment_server_url) { - var messageReceiver = new MessageReceiver(url, username, password, signalingKey, attachment_server_url); +textsecure.MessageReceiver = function(url, ports, username, password, signalingKey, attachment_server_url) { + var messageReceiver = new MessageReceiver(url, ports, username, password, signalingKey, attachment_server_url); this.addEventListener = messageReceiver.addEventListener.bind(messageReceiver); this.removeEventListener = messageReceiver.removeEventListener.bind(messageReceiver); this.getStatus = messageReceiver.getStatus.bind(messageReceiver); @@ -38835,8 +38862,8 @@ Message.prototype = { } }; -function MessageSender(url, username, password, attachment_server_url) { - this.server = new TextSecureServer(url, username, password, attachment_server_url); +function MessageSender(url, ports, username, password, attachment_server_url) { + this.server = new TextSecureServer(url, ports, username, password, attachment_server_url); this.pendingMessages = {}; } @@ -39187,8 +39214,8 @@ MessageSender.prototype = { window.textsecure = window.textsecure || {}; -textsecure.MessageSender = function(url, username, password, attachment_server_url) { - var sender = new MessageSender(url, username, password, attachment_server_url); +textsecure.MessageSender = function(url, ports, username, password, attachment_server_url) { + var sender = new MessageSender(url, ports, username, password, attachment_server_url); textsecure.replay.registerFunction(sender.tryMessageAgain.bind(sender), textsecure.replay.Type.ENCRYPT_MESSAGE); textsecure.replay.registerFunction(sender.retransmitMessage.bind(sender), textsecure.replay.Type.TRANSMIT_MESSAGE); textsecure.replay.registerFunction(sender.sendMessage.bind(sender), textsecure.replay.Type.REBUILD_MESSAGE); diff --git a/libtextsecure/account_manager.js b/libtextsecure/account_manager.js index 04a6bd3a..3bd641e9 100644 --- a/libtextsecure/account_manager.js +++ b/libtextsecure/account_manager.js @@ -7,8 +7,8 @@ 'use strict'; window.textsecure = window.textsecure || {}; - function AccountManager(url, username, password) { - this.server = new TextSecureServer(url, username, password); + function AccountManager(url, ports, username, password) { + this.server = new TextSecureServer(url, ports, username, password); } AccountManager.prototype = { diff --git a/libtextsecure/api.js b/libtextsecure/api.js index 32c727e1..0a08bd84 100644 --- a/libtextsecure/api.js +++ b/libtextsecure/api.js @@ -2,6 +2,18 @@ * vim: ts=4:sw=4:expandtab */ +function PortManager(ports) { + this.ports = ports; + this.idx = 0; +} + +PortManager.prototype = { + constructor: PortManager, + getPort: function() { + return this.ports[this.idx++]; + } +}; + var TextSecureServer = (function() { 'use strict'; @@ -27,6 +39,9 @@ var TextSecureServer = (function() { // Promise-based async xhr routine function promise_ajax(url, options) { return new Promise(function (resolve, reject) { + if (!url) { + url = options.host + ':' + options.port + '/' + options.path; + } console.log(options.type, url); var xhr = new XMLHttpRequest(); xhr.open(options.type, url, true /*async*/); @@ -74,24 +89,29 @@ var TextSecureServer = (function() { }); } - function ajax(url, options) { - options.stack = new Error().stack; // just in case, save stack here. - var count = 3; - - function retry(e) { - if (e.name === 'HTTPError' && e.code === -1 && count > 0) { - count = count - 1; + function retry_ajax(url, options, limit, count) { + count = count || 0; + limit = limit || 3; + if (options.ports) { + options.port = options.ports[count % options.ports.length]; + } + count++; + return promise_ajax(url, options).catch(function(e) { + if (e.name === 'HTTPError' && e.code === -1 && count < limit) { return new Promise(function(resolve) { setTimeout(function() { - resolve(promise_ajax(url, options).catch(retry)); - }, 1000 ); + resolve(retry_ajax(url, options, limit, count)); + }, 1000); }); } else { throw e; } - } + }); + } - return promise_ajax(url, options).catch(retry); + function ajax(url, options) { + options.stack = new Error().stack; // just in case, save stack here. + return retry_ajax(url, options); } function HTTPError(code, response, stack) { @@ -109,17 +129,18 @@ var TextSecureServer = (function() { } var URL_CALLS = { - accounts : "/v1/accounts", - devices : "/v1/devices", - keys : "/v2/keys", - messages : "/v1/messages", - attachment : "/v1/attachments" + accounts : "v1/accounts", + devices : "v1/devices", + keys : "v2/keys", + messages : "v1/messages", + attachment : "v1/attachments" }; - function TextSecureServer(url, username, password, attachment_server_url) { + function TextSecureServer(url, ports, username, password, attachment_server_url) { if (typeof url !== 'string') { throw new Error('Invalid server url'); } + this.portManager = new PortManager(ports); this.url = url; this.username = username; this.password = password; @@ -138,11 +159,17 @@ var TextSecureServer = (function() { TextSecureServer.prototype = { constructor: TextSecureServer, + getUrl: function() { + return this.url + ':' + this.portManager.getPort(); + }, ajax: function(param) { if (!param.urlParameters) { param.urlParameters = ''; } - return ajax(this.url + URL_CALLS[param.call] + param.urlParameters, { + return ajax(null, { + host : this.url, + ports : this.portManager.ports, + path : URL_CALLS[param.call] + param.urlParameters, type : param.httpType, data : param.jsonData && textsecure.utils.jsonThing(param.jsonData), contentType : 'application/json; charset=utf-8', @@ -351,7 +378,7 @@ var TextSecureServer = (function() { }, getMessageSocket: function() { return new WebSocket( - this.url.replace('https://', 'wss://') + this.getUrl().replace('https://', 'wss://') .replace('http://', 'ws://') + '/v1/websocket/?login=' + encodeURIComponent(this.username) + '&password=' + encodeURIComponent(this.password) @@ -361,7 +388,7 @@ var TextSecureServer = (function() { getProvisioningSocket: function () { console.log('opening provisioning socket', this.url); return new WebSocket( - this.url.replace('https://', 'wss://') + this.getUrl().replace('https://', 'wss://') .replace('http://', 'ws://') + '/v1/websocket/provisioning/?agent=OWD' ); diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index 6e8d98b9..bea440af 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -2,12 +2,12 @@ * vim: ts=4:sw=4:expandtab */ -function MessageReceiver(url, username, password, signalingKey, attachment_server_url) { +function MessageReceiver(url, ports, username, password, signalingKey, attachment_server_url) { this.url = url; this.signalingKey = signalingKey; this.username = username; this.password = password; - this.server = new TextSecureServer(url, username, password, attachment_server_url); + this.server = new TextSecureServer(url, ports, username, password, attachment_server_url); var address = libsignal.SignalProtocolAddress.fromString(username); this.number = address.getName(); @@ -472,8 +472,8 @@ MessageReceiver.prototype.extend({ window.textsecure = window.textsecure || {}; -textsecure.MessageReceiver = function(url, username, password, signalingKey, attachment_server_url) { - var messageReceiver = new MessageReceiver(url, username, password, signalingKey, attachment_server_url); +textsecure.MessageReceiver = function(url, ports, username, password, signalingKey, attachment_server_url) { + var messageReceiver = new MessageReceiver(url, ports, username, password, signalingKey, attachment_server_url); this.addEventListener = messageReceiver.addEventListener.bind(messageReceiver); this.removeEventListener = messageReceiver.removeEventListener.bind(messageReceiver); this.getStatus = messageReceiver.getStatus.bind(messageReceiver); diff --git a/libtextsecure/sendmessage.js b/libtextsecure/sendmessage.js index 7c41943b..33f6cac5 100644 --- a/libtextsecure/sendmessage.js +++ b/libtextsecure/sendmessage.js @@ -92,8 +92,8 @@ Message.prototype = { } }; -function MessageSender(url, username, password, attachment_server_url) { - this.server = new TextSecureServer(url, username, password, attachment_server_url); +function MessageSender(url, ports, username, password, attachment_server_url) { + this.server = new TextSecureServer(url, ports, username, password, attachment_server_url); this.pendingMessages = {}; } @@ -444,8 +444,8 @@ MessageSender.prototype = { window.textsecure = window.textsecure || {}; -textsecure.MessageSender = function(url, username, password, attachment_server_url) { - var sender = new MessageSender(url, username, password, attachment_server_url); +textsecure.MessageSender = function(url, ports, username, password, attachment_server_url) { + var sender = new MessageSender(url, ports, username, password, attachment_server_url); textsecure.replay.registerFunction(sender.tryMessageAgain.bind(sender), textsecure.replay.Type.ENCRYPT_MESSAGE); textsecure.replay.registerFunction(sender.retransmitMessage.bind(sender), textsecure.replay.Type.TRANSMIT_MESSAGE); textsecure.replay.registerFunction(sender.sendMessage.bind(sender), textsecure.replay.Type.REBUILD_MESSAGE);