Skip to content

Commit

Permalink
Don't hardcode the attachment server url
Browse files Browse the repository at this point in the history
There may come a day when we may need to change this url from the server
side. On that day, clients should continue to operate normally. The
service should be able to change attachment server locations without
requiring a client update.

// FREEBIE
  • Loading branch information
liliakai committed Mar 10, 2017
1 parent 43de0cc commit 611bbae
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 70 deletions.
4 changes: 1 addition & 3 deletions Gruntfile.js
Expand Up @@ -125,9 +125,7 @@ module.exports = function(grunt) {
if (srcpath.match('background.js')) {
return content.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');
'textsecure-service-ca.whispersystems.org');
} else if (srcpath.match('expire.js')) {
var gitinfo = grunt.config.get('gitinfo');
var commited = gitinfo.local.branch.current.lastCommitTime;
Expand Down
5 changes: 2 additions & 3 deletions js/background.js
Expand Up @@ -35,7 +35,6 @@

var SERVER_URL = 'https://textsecure-service-staging.whispersystems.org';
var SERVER_PORTS = [80, 4433, 8443];
var ATTACHMENT_SERVER_URL = 'https://whispersystems-textsecure-attachments-staging.s3.amazonaws.com';
var messageReceiver;
window.getSocketStatus = function() {
if (messageReceiver) {
Expand Down Expand Up @@ -108,7 +107,7 @@

// initialize the socket and start listening for messages
messageReceiver = new textsecure.MessageReceiver(
SERVER_URL, SERVER_PORTS, USERNAME, PASSWORD, mySignalingKey, ATTACHMENT_SERVER_URL
SERVER_URL, SERVER_PORTS, USERNAME, PASSWORD, mySignalingKey
);
messageReceiver.addEventListener('message', onMessageReceived);
messageReceiver.addEventListener('receipt', onDeliveryReceipt);
Expand All @@ -119,7 +118,7 @@
messageReceiver.addEventListener('error', onError);

window.textsecure.messaging = new textsecure.MessageSender(
SERVER_URL, SERVER_PORTS, USERNAME, PASSWORD, ATTACHMENT_SERVER_URL
SERVER_URL, SERVER_PORTS, USERNAME, PASSWORD
);
if (firstRun === true && textsecure.storage.user.getDeviceId() != '1') {
if (!storage.get('theme-setting') && textsecure.storage.get('userAgent') === 'OWI') {
Expand Down
41 changes: 9 additions & 32 deletions js/libtextsecure.js
Expand Up @@ -37660,25 +37660,14 @@ var TextSecureServer = (function() {
attachment : "v1/attachments"
};

function TextSecureServer(url, ports, username, password, attachment_server_url) {
function TextSecureServer(url, ports, username, password) {
if (typeof url !== 'string') {
throw new Error('Invalid server url');
}
this.portManager = new PortManager(ports);
this.url = url;
this.username = username;
this.password = password;

this.attachment_id_regex = RegExp("^https:\/\/.*\/(\\d+)\?");
if (attachment_server_url) {
// strip trailing /
attachment_server_url = attachment_server_url.replace(/\/$/,'');
// and escape
attachment_server_url = attachment_server_url.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
this.attachment_id_regex = RegExp( "^" + attachment_server_url + "\/(\\d+)\?");
}


}

TextSecureServer.prototype = {
Expand Down Expand Up @@ -37882,11 +37871,6 @@ var TextSecureServer = (function() {
urlParameters : '/' + id,
validateResponse : {location: 'string'}
}).then(function(response) {
var match = response.location.match(this.attachment_id_regex);
if (!match) {
console.log('Invalid attachment url for incoming message', response.location);
throw new Error('Received invalid attachment url');
}
return ajax(response.location, {
type : "GET",
responseType: "arraybuffer",
Expand All @@ -37899,13 +37883,6 @@ var TextSecureServer = (function() {
call : 'attachment',
httpType : 'GET',
}).then(function(response) {
// Extract the id as a string from the location url
// (workaround for ids too large for Javascript numbers)
var match = response.location.match(this.attachment_id_regex);
if (!match) {
console.log('Invalid attachment url for outgoing message', response.location);
throw new Error('Received invalid attachment url');
}
return ajax(response.location, {
type : "PUT",
contentType : "application/octet-stream",
Expand Down Expand Up @@ -38226,12 +38203,12 @@ var TextSecureServer = (function() {
* vim: ts=4:sw=4:expandtab
*/

function MessageReceiver(url, ports, username, password, signalingKey, attachment_server_url) {
function MessageReceiver(url, ports, username, password, signalingKey) {
this.url = url;
this.signalingKey = signalingKey;
this.username = username;
this.password = password;
this.server = new TextSecureServer(url, ports, username, password, attachment_server_url);
this.server = new TextSecureServer(url, ports, username, password);

var address = libsignal.SignalProtocolAddress.fromString(username);
this.number = address.getName();
Expand Down Expand Up @@ -38702,8 +38679,8 @@ MessageReceiver.prototype.extend({

window.textsecure = window.textsecure || {};

textsecure.MessageReceiver = function(url, ports, username, password, signalingKey, attachment_server_url) {
var messageReceiver = new MessageReceiver(url, ports, username, password, signalingKey, attachment_server_url);
textsecure.MessageReceiver = function(url, ports, username, password, signalingKey) {
var messageReceiver = new MessageReceiver(url, ports, username, password, signalingKey);
this.addEventListener = messageReceiver.addEventListener.bind(messageReceiver);
this.removeEventListener = messageReceiver.removeEventListener.bind(messageReceiver);
this.getStatus = messageReceiver.getStatus.bind(messageReceiver);
Expand Down Expand Up @@ -39051,8 +39028,8 @@ Message.prototype = {
}
};

function MessageSender(url, ports, username, password, attachment_server_url) {
this.server = new TextSecureServer(url, ports, username, password, attachment_server_url);
function MessageSender(url, ports, username, password) {
this.server = new TextSecureServer(url, ports, username, password);
this.pendingMessages = {};
}

Expand Down Expand Up @@ -39459,8 +39436,8 @@ MessageSender.prototype = {

window.textsecure = window.textsecure || {};

textsecure.MessageSender = function(url, ports, username, password, attachment_server_url) {
var sender = new MessageSender(url, ports, username, password, attachment_server_url);
textsecure.MessageSender = function(url, ports, username, password) {
var sender = new MessageSender(url, ports, username, password);
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);
Expand Down
25 changes: 1 addition & 24 deletions libtextsecure/api.js
Expand Up @@ -139,25 +139,14 @@ var TextSecureServer = (function() {
attachment : "v1/attachments"
};

function TextSecureServer(url, ports, username, password, attachment_server_url) {
function TextSecureServer(url, ports, username, password) {
if (typeof url !== 'string') {
throw new Error('Invalid server url');
}
this.portManager = new PortManager(ports);
this.url = url;
this.username = username;
this.password = password;

this.attachment_id_regex = RegExp("^https:\/\/.*\/(\\d+)\?");
if (attachment_server_url) {
// strip trailing /
attachment_server_url = attachment_server_url.replace(/\/$/,'');
// and escape
attachment_server_url = attachment_server_url.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
this.attachment_id_regex = RegExp( "^" + attachment_server_url + "\/(\\d+)\?");
}


}

TextSecureServer.prototype = {
Expand Down Expand Up @@ -361,11 +350,6 @@ var TextSecureServer = (function() {
urlParameters : '/' + id,
validateResponse : {location: 'string'}
}).then(function(response) {
var match = response.location.match(this.attachment_id_regex);
if (!match) {
console.log('Invalid attachment url for incoming message', response.location);
throw new Error('Received invalid attachment url');
}
return ajax(response.location, {
type : "GET",
responseType: "arraybuffer",
Expand All @@ -378,13 +362,6 @@ var TextSecureServer = (function() {
call : 'attachment',
httpType : 'GET',
}).then(function(response) {
// Extract the id as a string from the location url
// (workaround for ids too large for Javascript numbers)
var match = response.location.match(this.attachment_id_regex);
if (!match) {
console.log('Invalid attachment url for outgoing message', response.location);
throw new Error('Received invalid attachment url');
}
return ajax(response.location, {
type : "PUT",
contentType : "application/octet-stream",
Expand Down
8 changes: 4 additions & 4 deletions libtextsecure/message_receiver.js
Expand Up @@ -2,12 +2,12 @@
* vim: ts=4:sw=4:expandtab
*/

function MessageReceiver(url, ports, username, password, signalingKey, attachment_server_url) {
function MessageReceiver(url, ports, username, password, signalingKey) {
this.url = url;
this.signalingKey = signalingKey;
this.username = username;
this.password = password;
this.server = new TextSecureServer(url, ports, username, password, attachment_server_url);
this.server = new TextSecureServer(url, ports, username, password);

var address = libsignal.SignalProtocolAddress.fromString(username);
this.number = address.getName();
Expand Down Expand Up @@ -478,8 +478,8 @@ MessageReceiver.prototype.extend({

window.textsecure = window.textsecure || {};

textsecure.MessageReceiver = function(url, ports, username, password, signalingKey, attachment_server_url) {
var messageReceiver = new MessageReceiver(url, ports, username, password, signalingKey, attachment_server_url);
textsecure.MessageReceiver = function(url, ports, username, password, signalingKey) {
var messageReceiver = new MessageReceiver(url, ports, username, password, signalingKey);
this.addEventListener = messageReceiver.addEventListener.bind(messageReceiver);
this.removeEventListener = messageReceiver.removeEventListener.bind(messageReceiver);
this.getStatus = messageReceiver.getStatus.bind(messageReceiver);
Expand Down
8 changes: 4 additions & 4 deletions libtextsecure/sendmessage.js
Expand Up @@ -104,8 +104,8 @@ Message.prototype = {
}
};

function MessageSender(url, ports, username, password, attachment_server_url) {
this.server = new TextSecureServer(url, ports, username, password, attachment_server_url);
function MessageSender(url, ports, username, password) {
this.server = new TextSecureServer(url, ports, username, password);
this.pendingMessages = {};
}

Expand Down Expand Up @@ -512,8 +512,8 @@ MessageSender.prototype = {

window.textsecure = window.textsecure || {};

textsecure.MessageSender = function(url, ports, username, password, attachment_server_url) {
var sender = new MessageSender(url, ports, username, password, attachment_server_url);
textsecure.MessageSender = function(url, ports, username, password) {
var sender = new MessageSender(url, ports, username, password);
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);
Expand Down

0 comments on commit 611bbae

Please sign in to comment.