2015-09-07 21:53:43 +00:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
2014-05-04 06:34:13 +00:00
|
|
|
*/
|
|
|
|
|
2016-09-08 04:53:56 +00:00
|
|
|
function PortManager(ports) {
|
|
|
|
this.ports = ports;
|
|
|
|
this.idx = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
PortManager.prototype = {
|
|
|
|
constructor: PortManager,
|
|
|
|
getPort: function() {
|
|
|
|
return this.ports[this.idx++];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-08-28 22:37:45 +00:00
|
|
|
var TextSecureServer = (function() {
|
2014-10-20 00:53:17 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-11-25 20:07:23 +00:00
|
|
|
function validateResponse(response, schema) {
|
|
|
|
try {
|
|
|
|
for (var i in schema) {
|
|
|
|
switch (schema[i]) {
|
|
|
|
case 'object':
|
|
|
|
case 'string':
|
|
|
|
case 'number':
|
|
|
|
if (typeof response[i] !== schema[i]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch(ex) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-28 22:37:45 +00:00
|
|
|
// Promise-based async xhr routine
|
2015-10-11 02:07:00 +00:00
|
|
|
function promise_ajax(url, options) {
|
2015-07-13 18:52:49 +00:00
|
|
|
return new Promise(function (resolve, reject) {
|
2016-09-08 04:53:56 +00:00
|
|
|
if (!url) {
|
|
|
|
url = options.host + ':' + options.port + '/' + options.path;
|
|
|
|
}
|
2015-09-17 06:45:01 +00:00
|
|
|
console.log(options.type, url);
|
2015-07-13 18:52:49 +00:00
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
xhr.open(options.type, url, true /*async*/);
|
2015-03-25 00:04:41 +00:00
|
|
|
|
2015-07-13 18:52:49 +00:00
|
|
|
if ( options.responseType ) {
|
|
|
|
xhr[ 'responseType' ] = options.responseType;
|
2015-03-25 00:04:41 +00:00
|
|
|
}
|
2015-07-13 18:52:49 +00:00
|
|
|
if (options.user && options.password) {
|
|
|
|
xhr.setRequestHeader("Authorization", "Basic " + btoa(getString(options.user) + ":" + getString(options.password)));
|
2015-03-25 00:04:41 +00:00
|
|
|
}
|
2015-07-13 18:52:49 +00:00
|
|
|
if (options.contentType) {
|
|
|
|
xhr.setRequestHeader( "Content-Type", options.contentType );
|
2015-03-25 00:04:41 +00:00
|
|
|
}
|
2015-12-04 18:45:53 +00:00
|
|
|
xhr.setRequestHeader( 'X-Signal-Agent', 'OWD' );
|
|
|
|
|
2015-07-13 18:52:49 +00:00
|
|
|
|
|
|
|
xhr.onload = function() {
|
|
|
|
var result = xhr.response;
|
|
|
|
if ( (!xhr.responseType || xhr.responseType === "text") &&
|
|
|
|
typeof xhr.responseText === "string" ) {
|
|
|
|
result = xhr.responseText;
|
|
|
|
}
|
|
|
|
if (options.dataType === 'json') {
|
|
|
|
try { result = JSON.parse(xhr.responseText + ''); } catch(e) {}
|
2015-11-25 20:07:23 +00:00
|
|
|
if (options.validateResponse) {
|
|
|
|
if (!validateResponse(result, options.validateResponse)) {
|
2015-11-28 00:17:37 +00:00
|
|
|
console.log(options.type, url, xhr.status, 'Error');
|
|
|
|
reject(HTTPError(xhr.status, result, options.stack));
|
2015-11-25 20:07:23 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-13 18:52:49 +00:00
|
|
|
}
|
|
|
|
if ( 0 <= xhr.status && xhr.status < 400) {
|
2015-09-30 18:17:19 +00:00
|
|
|
console.log(options.type, url, xhr.status, 'Success');
|
2015-07-13 18:52:49 +00:00
|
|
|
resolve(result, xhr.status);
|
|
|
|
} else {
|
2015-09-30 18:17:19 +00:00
|
|
|
console.log(options.type, url, xhr.status, 'Error');
|
2015-10-11 19:11:56 +00:00
|
|
|
reject(HTTPError(xhr.status, result, options.stack));
|
2015-07-13 18:52:49 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
xhr.onerror = function() {
|
2015-09-30 18:17:19 +00:00
|
|
|
console.log(options.type, url, xhr.status, 'Error');
|
2015-10-11 19:11:56 +00:00
|
|
|
reject(HTTPError(xhr.status, null, options.stack));
|
2015-07-13 18:52:49 +00:00
|
|
|
};
|
|
|
|
xhr.send( options.data || null );
|
|
|
|
});
|
2015-04-30 19:13:04 +00:00
|
|
|
}
|
|
|
|
|
2016-09-08 04:53:56 +00:00
|
|
|
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) {
|
2015-10-11 02:07:00 +00:00
|
|
|
return new Promise(function(resolve) {
|
|
|
|
setTimeout(function() {
|
2016-09-08 04:53:56 +00:00
|
|
|
resolve(retry_ajax(url, options, limit, count));
|
|
|
|
}, 1000);
|
2015-10-11 02:07:00 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
2016-09-08 04:53:56 +00:00
|
|
|
});
|
|
|
|
}
|
2015-10-11 02:07:00 +00:00
|
|
|
|
2016-09-08 04:53:56 +00:00
|
|
|
function ajax(url, options) {
|
|
|
|
options.stack = new Error().stack; // just in case, save stack here.
|
|
|
|
return retry_ajax(url, options);
|
2015-10-11 02:07:00 +00:00
|
|
|
}
|
|
|
|
|
2015-07-10 18:28:47 +00:00
|
|
|
function HTTPError(code, response, stack) {
|
|
|
|
if (code > 999 || code < 100) {
|
|
|
|
code = -1;
|
|
|
|
}
|
2015-07-28 00:51:17 +00:00
|
|
|
var e = new Error();
|
2015-07-10 18:28:47 +00:00
|
|
|
e.name = 'HTTPError';
|
|
|
|
e.code = code;
|
2015-07-13 18:52:49 +00:00
|
|
|
e.stack = stack;
|
2015-07-10 18:28:47 +00:00
|
|
|
if (response) {
|
|
|
|
e.response = response;
|
|
|
|
}
|
|
|
|
return e;
|
2015-04-30 19:13:04 +00:00
|
|
|
}
|
2015-03-25 00:04:41 +00:00
|
|
|
|
2015-08-28 22:37:45 +00:00
|
|
|
var URL_CALLS = {
|
2016-09-08 04:53:56 +00:00
|
|
|
accounts : "v1/accounts",
|
|
|
|
devices : "v1/devices",
|
|
|
|
keys : "v2/keys",
|
|
|
|
messages : "v1/messages",
|
|
|
|
attachment : "v1/attachments"
|
2014-10-20 00:53:17 +00:00
|
|
|
};
|
2014-03-22 22:45:01 +00:00
|
|
|
|
2016-09-08 04:53:56 +00:00
|
|
|
function TextSecureServer(url, ports, username, password, attachment_server_url) {
|
2015-09-25 00:59:57 +00:00
|
|
|
if (typeof url !== 'string') {
|
|
|
|
throw new Error('Invalid server url');
|
|
|
|
}
|
2016-09-08 04:53:56 +00:00
|
|
|
this.portManager = new PortManager(ports);
|
2015-08-28 22:37:45 +00:00
|
|
|
this.url = url;
|
|
|
|
this.username = username;
|
|
|
|
this.password = password;
|
2015-11-24 20:40:00 +00:00
|
|
|
|
|
|
|
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+)\?");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-28 22:37:45 +00:00
|
|
|
}
|
2015-06-19 22:32:25 +00:00
|
|
|
|
2015-08-28 22:37:45 +00:00
|
|
|
TextSecureServer.prototype = {
|
|
|
|
constructor: TextSecureServer,
|
2016-09-08 04:53:56 +00:00
|
|
|
getUrl: function() {
|
|
|
|
return this.url + ':' + this.portManager.getPort();
|
|
|
|
},
|
2015-08-28 22:37:45 +00:00
|
|
|
ajax: function(param) {
|
|
|
|
if (!param.urlParameters) {
|
|
|
|
param.urlParameters = '';
|
|
|
|
}
|
2016-09-08 04:53:56 +00:00
|
|
|
return ajax(null, {
|
|
|
|
host : this.url,
|
|
|
|
ports : this.portManager.ports,
|
|
|
|
path : URL_CALLS[param.call] + param.urlParameters,
|
2015-08-28 22:37:45 +00:00
|
|
|
type : param.httpType,
|
|
|
|
data : param.jsonData && textsecure.utils.jsonThing(param.jsonData),
|
|
|
|
contentType : 'application/json; charset=utf-8',
|
|
|
|
dataType : 'json',
|
|
|
|
user : this.username,
|
2015-11-25 20:07:23 +00:00
|
|
|
password : this.password,
|
|
|
|
validateResponse: param.validateResponse
|
2015-08-28 22:37:45 +00:00
|
|
|
}).catch(function(e) {
|
|
|
|
var code = e.code;
|
|
|
|
if (code === 200) {
|
|
|
|
// happens sometimes when we get no response
|
|
|
|
// (TODO: Fix server to return 204? instead)
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
var message;
|
|
|
|
switch (code) {
|
|
|
|
case -1:
|
|
|
|
message = "Failed to connect to the server, please check your network connection.";
|
|
|
|
break;
|
|
|
|
case 413:
|
|
|
|
message = "Rate limit exceeded, please try again later.";
|
|
|
|
break;
|
|
|
|
case 403:
|
|
|
|
message = "Invalid code, please try again.";
|
|
|
|
break;
|
|
|
|
case 417:
|
|
|
|
// TODO: This shouldn't be a thing?, but its in the API doc?
|
|
|
|
message = "Number already registered.";
|
|
|
|
break;
|
|
|
|
case 401:
|
|
|
|
message = "Invalid authentication, most likely someone re-registered and invalidated our registration.";
|
|
|
|
break;
|
|
|
|
case 404:
|
2015-11-13 18:15:03 +00:00
|
|
|
message = "Number is not registered.";
|
2015-08-28 22:37:45 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
message = "The server rejected our query, please file a bug report.";
|
|
|
|
}
|
|
|
|
e.message = message
|
|
|
|
throw e;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
requestVerificationSMS: function(number) {
|
|
|
|
return this.ajax({
|
|
|
|
call : 'accounts',
|
|
|
|
httpType : 'GET',
|
|
|
|
urlParameters : '/sms/code/' + number,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
requestVerificationVoice: function(number) {
|
|
|
|
return this.ajax({
|
|
|
|
call : 'accounts',
|
|
|
|
httpType : 'GET',
|
|
|
|
urlParameters : '/voice/code/' + number,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
confirmCode: function(number, code, password, signaling_key, registrationId, deviceName) {
|
2015-06-18 18:00:58 +00:00
|
|
|
var jsonData = {
|
|
|
|
signalingKey : btoa(getString(signaling_key)),
|
|
|
|
supportsSms : false,
|
|
|
|
fetchesMessages : true,
|
|
|
|
registrationId : registrationId,
|
|
|
|
};
|
2015-11-25 20:07:23 +00:00
|
|
|
|
|
|
|
var call, urlPrefix, schema;
|
2015-06-18 18:00:58 +00:00
|
|
|
if (deviceName) {
|
|
|
|
jsonData.name = deviceName;
|
2015-11-25 20:07:23 +00:00
|
|
|
call = 'devices';
|
|
|
|
urlPrefix = '/';
|
|
|
|
schema = { deviceId: 'number' };
|
|
|
|
} else {
|
|
|
|
call = 'accounts';
|
|
|
|
urlPrefix = '/code/';
|
2015-06-18 18:00:58 +00:00
|
|
|
}
|
2015-11-25 20:07:23 +00:00
|
|
|
|
2015-08-28 22:37:45 +00:00
|
|
|
this.username = number;
|
|
|
|
this.password = password;
|
|
|
|
return this.ajax({
|
2014-10-20 00:53:17 +00:00
|
|
|
call : call,
|
|
|
|
httpType : 'PUT',
|
|
|
|
urlParameters : urlPrefix + code,
|
2015-11-25 20:07:23 +00:00
|
|
|
jsonData : jsonData,
|
|
|
|
validateResponse : schema
|
2014-10-20 00:53:17 +00:00
|
|
|
});
|
2015-08-28 22:37:45 +00:00
|
|
|
},
|
|
|
|
getDevices: function(number) {
|
|
|
|
return this.ajax({
|
|
|
|
call : 'devices',
|
|
|
|
httpType : 'GET',
|
|
|
|
});
|
|
|
|
},
|
|
|
|
registerKeys: function(genKeys) {
|
|
|
|
var keys = {};
|
|
|
|
keys.identityKey = btoa(getString(genKeys.identityKey));
|
2015-10-10 22:12:06 +00:00
|
|
|
keys.signedPreKey = {
|
|
|
|
keyId: genKeys.signedPreKey.keyId,
|
|
|
|
publicKey: btoa(getString(genKeys.signedPreKey.publicKey)),
|
|
|
|
signature: btoa(getString(genKeys.signedPreKey.signature))
|
|
|
|
};
|
2014-10-20 00:53:17 +00:00
|
|
|
|
2015-08-28 22:37:45 +00:00
|
|
|
keys.preKeys = [];
|
|
|
|
var j = 0;
|
2015-10-10 22:12:06 +00:00
|
|
|
for (var i in genKeys.preKeys) {
|
|
|
|
keys.preKeys[j++] = {
|
|
|
|
keyId: genKeys.preKeys[i].keyId,
|
|
|
|
publicKey: btoa(getString(genKeys.preKeys[i].publicKey))
|
|
|
|
};
|
|
|
|
}
|
2015-04-28 23:26:41 +00:00
|
|
|
|
2015-11-04 18:31:50 +00:00
|
|
|
// This is just to make the server happy
|
|
|
|
// (v2 clients should choke on publicKey)
|
2015-08-28 22:37:45 +00:00
|
|
|
keys.lastResortKey = {keyId: 0x7fffFFFF, publicKey: btoa("42")};
|
2014-10-20 00:53:17 +00:00
|
|
|
|
2015-08-28 22:37:45 +00:00
|
|
|
return this.ajax({
|
|
|
|
call : 'keys',
|
|
|
|
httpType : 'PUT',
|
|
|
|
jsonData : keys,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
getMyKeys: function(number, deviceId) {
|
|
|
|
return this.ajax({
|
|
|
|
call : 'keys',
|
|
|
|
httpType : 'GET',
|
2015-11-25 20:07:23 +00:00
|
|
|
validateResponse : {count: 'number'}
|
2015-08-28 22:37:45 +00:00
|
|
|
}).then(function(res) {
|
2015-11-25 20:07:23 +00:00
|
|
|
return res.count;
|
2015-08-28 22:37:45 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
getKeysForNumber: function(number, deviceId) {
|
|
|
|
if (deviceId === undefined)
|
|
|
|
deviceId = "*";
|
2014-10-20 00:53:17 +00:00
|
|
|
|
2015-08-28 22:37:45 +00:00
|
|
|
return this.ajax({
|
|
|
|
call : 'keys',
|
|
|
|
httpType : 'GET',
|
|
|
|
urlParameters : "/" + number + "/" + deviceId,
|
2015-11-25 20:07:23 +00:00
|
|
|
validateResponse : {identityKey: 'string', devices: 'object'}
|
2015-08-28 22:37:45 +00:00
|
|
|
}).then(function(res) {
|
2015-11-25 20:07:23 +00:00
|
|
|
if (res.devices.constructor !== Array) {
|
|
|
|
throw new Error("Invalid response");
|
|
|
|
}
|
2015-08-28 22:37:45 +00:00
|
|
|
res.identityKey = StringView.base64ToBytes(res.identityKey);
|
2016-03-13 21:15:16 +00:00
|
|
|
res.devices.forEach(function(device) {
|
|
|
|
if ( !validateResponse(device, {signedPreKey: 'object', preKey: 'object'}) ||
|
|
|
|
!validateResponse(device.signedPreKey, {publicKey: 'string', signature: 'string'}) ||
|
|
|
|
!validateResponse(device.preKey, {publicKey: 'string'})) {
|
2015-11-25 20:07:23 +00:00
|
|
|
throw new Error("Invalid response");
|
|
|
|
}
|
2016-03-13 21:15:16 +00:00
|
|
|
device.signedPreKey.publicKey = StringView.base64ToBytes(device.signedPreKey.publicKey);
|
|
|
|
device.signedPreKey.signature = StringView.base64ToBytes(device.signedPreKey.signature);
|
|
|
|
device.preKey.publicKey = StringView.base64ToBytes(device.preKey.publicKey);
|
|
|
|
});
|
2015-08-28 22:37:45 +00:00
|
|
|
return res;
|
|
|
|
});
|
|
|
|
},
|
2015-11-17 20:00:41 +00:00
|
|
|
sendMessages: function(destination, messageArray, timestamp) {
|
|
|
|
var jsonData = { messages: messageArray, timestamp: timestamp};
|
2014-10-20 00:53:17 +00:00
|
|
|
|
2015-08-28 22:37:45 +00:00
|
|
|
return this.ajax({
|
|
|
|
call : 'messages',
|
|
|
|
httpType : 'PUT',
|
|
|
|
urlParameters : '/' + destination,
|
|
|
|
jsonData : jsonData,
|
2014-10-20 00:53:17 +00:00
|
|
|
});
|
2015-08-28 22:37:45 +00:00
|
|
|
},
|
|
|
|
getAttachment: function(id) {
|
|
|
|
return this.ajax({
|
|
|
|
call : 'attachment',
|
|
|
|
httpType : 'GET',
|
|
|
|
urlParameters : '/' + id,
|
2015-11-25 20:07:23 +00:00
|
|
|
validateResponse : {location: 'string'}
|
2015-08-28 22:37:45 +00:00
|
|
|
}).then(function(response) {
|
2015-11-24 20:40:00 +00:00
|
|
|
var match = response.location.match(this.attachment_id_regex);
|
|
|
|
if (!match) {
|
2015-12-08 00:52:45 +00:00
|
|
|
console.log('Invalid attachment url for incoming message', response.location);
|
|
|
|
throw new Error('Received invalid attachment url');
|
2015-11-24 20:40:00 +00:00
|
|
|
}
|
2015-08-28 22:37:45 +00:00
|
|
|
return ajax(response.location, {
|
|
|
|
type : "GET",
|
|
|
|
responseType: "arraybuffer",
|
|
|
|
contentType : "application/octet-stream"
|
|
|
|
});
|
2015-11-24 20:40:00 +00:00
|
|
|
}.bind(this));
|
2015-08-28 22:37:45 +00:00
|
|
|
},
|
|
|
|
putAttachment: function(encryptedBin) {
|
|
|
|
return this.ajax({
|
|
|
|
call : 'attachment',
|
|
|
|
httpType : 'GET',
|
|
|
|
}).then(function(response) {
|
2015-11-24 20:40:00 +00:00
|
|
|
// 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) {
|
2015-12-08 00:52:45 +00:00
|
|
|
console.log('Invalid attachment url for outgoing message', response.location);
|
|
|
|
throw new Error('Received invalid attachment url');
|
2015-11-24 20:40:00 +00:00
|
|
|
}
|
2015-08-28 22:37:45 +00:00
|
|
|
return ajax(response.location, {
|
|
|
|
type : "PUT",
|
|
|
|
contentType : "application/octet-stream",
|
|
|
|
data : encryptedBin,
|
|
|
|
processData : false,
|
|
|
|
}).then(function() {
|
2015-11-24 20:40:00 +00:00
|
|
|
return match[1];
|
|
|
|
}.bind(this));
|
|
|
|
}.bind(this));
|
2015-08-28 22:37:45 +00:00
|
|
|
},
|
|
|
|
getMessageSocket: function() {
|
|
|
|
return new WebSocket(
|
2016-09-08 04:53:56 +00:00
|
|
|
this.getUrl().replace('https://', 'wss://')
|
2015-08-28 22:37:45 +00:00
|
|
|
.replace('http://', 'ws://')
|
|
|
|
+ '/v1/websocket/?login=' + encodeURIComponent(this.username)
|
|
|
|
+ '&password=' + encodeURIComponent(this.password)
|
2015-12-04 18:45:53 +00:00
|
|
|
+ '&agent=OWD'
|
2015-08-28 22:37:45 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
getProvisioningSocket: function () {
|
2016-02-29 12:55:51 +00:00
|
|
|
console.log('opening provisioning socket', this.url);
|
2015-08-28 22:37:45 +00:00
|
|
|
return new WebSocket(
|
2016-09-08 04:53:56 +00:00
|
|
|
this.getUrl().replace('https://', 'wss://')
|
2015-08-28 22:37:45 +00:00
|
|
|
.replace('http://', 'ws://')
|
2015-12-04 18:45:53 +00:00
|
|
|
+ '/v1/websocket/provisioning/?agent=OWD'
|
2015-08-28 22:37:45 +00:00
|
|
|
);
|
|
|
|
}
|
2014-10-20 00:53:17 +00:00
|
|
|
};
|
|
|
|
|
2015-08-28 22:37:45 +00:00
|
|
|
return TextSecureServer;
|
|
|
|
})();
|