2015-09-07 21:53:43 +00:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
2014-11-28 06:03:42 +00:00
|
|
|
*/
|
|
|
|
;(function(){
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/*
|
|
|
|
* WebSocket-Resources
|
|
|
|
*
|
|
|
|
* Create a request-response interface over websockets using the
|
|
|
|
* WebSocket-Resources sub-protocol[1].
|
|
|
|
*
|
|
|
|
* var client = new WebSocketResource(socket, function(request) {
|
|
|
|
* request.respond(200, 'OK');
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* client.sendRequest({
|
|
|
|
* verb: 'PUT',
|
|
|
|
* path: '/v1/messages',
|
|
|
|
* body: '{ some: "json" }',
|
|
|
|
* success: function(message, status, request) {...},
|
|
|
|
* error: function(message, status, request) {...}
|
|
|
|
* });
|
|
|
|
*
|
2018-01-30 22:22:51 +00:00
|
|
|
* 1. https://github.com/signalapp/WebSocket-Resources
|
2014-11-28 06:03:42 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
var Request = function(options) {
|
|
|
|
this.verb = options.verb || options.type;
|
|
|
|
this.path = options.path || options.url;
|
|
|
|
this.body = options.body || options.data;
|
2016-02-25 00:39:37 +00:00
|
|
|
this.success = options.success;
|
|
|
|
this.error = options.error;
|
2014-11-28 06:03:42 +00:00
|
|
|
this.id = options.id;
|
|
|
|
|
|
|
|
if (this.id === undefined) {
|
|
|
|
var bits = new Uint32Array(2);
|
|
|
|
window.crypto.getRandomValues(bits);
|
|
|
|
this.id = dcodeIO.Long.fromBits(bits[0], bits[1], true);
|
|
|
|
}
|
2015-07-16 20:16:50 +00:00
|
|
|
|
|
|
|
if (this.body === undefined) {
|
|
|
|
this.body = null;
|
|
|
|
}
|
2014-11-28 06:03:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var IncomingWebSocketRequest = function(options) {
|
|
|
|
var request = new Request(options);
|
|
|
|
var socket = options.socket;
|
|
|
|
|
|
|
|
this.verb = request.verb;
|
|
|
|
this.path = request.path;
|
|
|
|
this.body = request.body;
|
|
|
|
|
|
|
|
this.respond = function(status, message) {
|
|
|
|
socket.send(
|
|
|
|
new textsecure.protobuf.WebSocketMessage({
|
|
|
|
type: textsecure.protobuf.WebSocketMessage.Type.RESPONSE,
|
|
|
|
response: { id: request.id, message: message, status: status }
|
|
|
|
}).encode().toArrayBuffer()
|
|
|
|
);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
var outgoing = {};
|
|
|
|
var OutgoingWebSocketRequest = function(options, socket) {
|
|
|
|
var request = new Request(options);
|
|
|
|
outgoing[request.id] = request;
|
|
|
|
socket.send(
|
|
|
|
new textsecure.protobuf.WebSocketMessage({
|
|
|
|
type: textsecure.protobuf.WebSocketMessage.Type.REQUEST,
|
|
|
|
request: {
|
|
|
|
verb : request.verb,
|
|
|
|
path : request.path,
|
|
|
|
body : request.body,
|
|
|
|
id : request.id
|
|
|
|
}
|
|
|
|
}).encode().toArrayBuffer()
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2015-07-28 20:22:06 +00:00
|
|
|
window.WebSocketResource = function(socket, opts) {
|
|
|
|
opts = opts || {};
|
|
|
|
var handleRequest = opts.handleRequest;
|
|
|
|
if (typeof handleRequest !== 'function') {
|
|
|
|
handleRequest = function(request) {
|
2015-07-28 21:57:09 +00:00
|
|
|
request.respond(404, 'Not found');
|
2015-07-28 20:22:06 +00:00
|
|
|
};
|
|
|
|
}
|
2014-11-28 06:03:42 +00:00
|
|
|
this.sendRequest = function(options) {
|
|
|
|
return new OutgoingWebSocketRequest(options, socket);
|
|
|
|
};
|
|
|
|
|
|
|
|
socket.onmessage = function(socketMessage) {
|
|
|
|
var blob = socketMessage.data;
|
Certificate pinning via node XMLHttpRequest implementation (#1394)
* Add certificate pinning on https service requests
Make https requests to the server using node apis instead of browser apis, so we
can specify our own CA list, which contains only our own CA.
This protects us from MITM by a rogue CA.
As a bonus, this let's us drop the use of non-standard ports and just use good
ol' default 443 all the time, at least for http requests.
// FREEBIE
* Make certificateAuthorities an option on requests
Modify node-based xhr implementation based on driverdan/node-XMLHttpRequest,
adding support for setting certificate authorities on each request.
This allows us to pin our master CA for requests to the server and cdn but not
to the s3 attachment server, for instance. Also fix an exception when sending
binary data in a request: it is submitted as an array buffer, and must be
converted to a node Buffer since we are now using a node based request api.
// FREEBIE
* Import node-based xhr implementation
Add a copy of https://github.com/driverdan/node-XMLHttpRequest@86ff70e, and
expose it to the renderer in the preload script.
In later commits this module will be extended to support custom certificate
authorities.
// FREEBIE
* Support "arraybuffer" responseType on requests
When fetching attachments, we want the result as binary data rather than a utf8
string. This lets our node-based XMLHttpRequest honor the responseType property
if it is set on the xhr.
Note that naively using the raw `.buffer` from a node Buffer won't work, since
it is a reuseable backing buffer that is often much larger than the actual
content defined by the Buffer's offset and length.
Instead, we'll prepare a return buffer based on the response's content length
header, and incrementally write chunks of data into it as they arrive.
// FREEBIE
* Switch to self-signed server endpoint
* Log more error info on failed requests
With the node-based xhr, relevant error info are stored in statusText and
responseText when a request fails.
// FREEBIE
* Add node-based websocket w/ support for custom CA
// FREEBIE
* Support handling array buffers instead of blobs
Our node-based websocket calls onmessage with an arraybuffer instead of a blob.
For robustness (on the off chance we switch or update the socket implementation
agian) I've kept the machinery for converting blobs to array buffers.
// FREEBIE
* Destroy all wacky server ports
// FREEBIE
2017-09-01 15:58:58 +00:00
|
|
|
var handleArrayBuffer = function(buffer) {
|
|
|
|
var message = textsecure.protobuf.WebSocketMessage.decode(buffer);
|
2014-11-28 06:03:42 +00:00
|
|
|
if (message.type === textsecure.protobuf.WebSocketMessage.Type.REQUEST ) {
|
|
|
|
handleRequest(
|
|
|
|
new IncomingWebSocketRequest({
|
|
|
|
verb : message.request.verb,
|
|
|
|
path : message.request.path,
|
|
|
|
body : message.request.body,
|
|
|
|
id : message.request.id,
|
|
|
|
socket : socket
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else if (message.type === textsecure.protobuf.WebSocketMessage.Type.RESPONSE ) {
|
|
|
|
var response = message.response;
|
|
|
|
var request = outgoing[response.id];
|
|
|
|
if (request) {
|
|
|
|
request.response = response;
|
|
|
|
var callback = request.error;
|
|
|
|
if (response.status >= 200 && response.status < 300) {
|
|
|
|
callback = request.success;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof callback === 'function') {
|
|
|
|
callback(response.message, response.status, request);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw 'Received response for unknown request ' + message.response.id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
Certificate pinning via node XMLHttpRequest implementation (#1394)
* Add certificate pinning on https service requests
Make https requests to the server using node apis instead of browser apis, so we
can specify our own CA list, which contains only our own CA.
This protects us from MITM by a rogue CA.
As a bonus, this let's us drop the use of non-standard ports and just use good
ol' default 443 all the time, at least for http requests.
// FREEBIE
* Make certificateAuthorities an option on requests
Modify node-based xhr implementation based on driverdan/node-XMLHttpRequest,
adding support for setting certificate authorities on each request.
This allows us to pin our master CA for requests to the server and cdn but not
to the s3 attachment server, for instance. Also fix an exception when sending
binary data in a request: it is submitted as an array buffer, and must be
converted to a node Buffer since we are now using a node based request api.
// FREEBIE
* Import node-based xhr implementation
Add a copy of https://github.com/driverdan/node-XMLHttpRequest@86ff70e, and
expose it to the renderer in the preload script.
In later commits this module will be extended to support custom certificate
authorities.
// FREEBIE
* Support "arraybuffer" responseType on requests
When fetching attachments, we want the result as binary data rather than a utf8
string. This lets our node-based XMLHttpRequest honor the responseType property
if it is set on the xhr.
Note that naively using the raw `.buffer` from a node Buffer won't work, since
it is a reuseable backing buffer that is often much larger than the actual
content defined by the Buffer's offset and length.
Instead, we'll prepare a return buffer based on the response's content length
header, and incrementally write chunks of data into it as they arrive.
// FREEBIE
* Switch to self-signed server endpoint
* Log more error info on failed requests
With the node-based xhr, relevant error info are stored in statusText and
responseText when a request fails.
// FREEBIE
* Add node-based websocket w/ support for custom CA
// FREEBIE
* Support handling array buffers instead of blobs
Our node-based websocket calls onmessage with an arraybuffer instead of a blob.
For robustness (on the off chance we switch or update the socket implementation
agian) I've kept the machinery for converting blobs to array buffers.
// FREEBIE
* Destroy all wacky server ports
// FREEBIE
2017-09-01 15:58:58 +00:00
|
|
|
|
|
|
|
if (blob instanceof ArrayBuffer) {
|
|
|
|
handleArrayBuffer(blob);
|
|
|
|
} else {
|
|
|
|
var reader = new FileReader();
|
|
|
|
reader.onload = function() {
|
|
|
|
handleArrayBuffer(reader.result);
|
|
|
|
};
|
|
|
|
reader.readAsArrayBuffer(blob);
|
|
|
|
}
|
2014-11-28 06:03:42 +00:00
|
|
|
};
|
2015-07-28 20:05:55 +00:00
|
|
|
|
|
|
|
if (opts.keepalive) {
|
2017-10-06 23:28:13 +00:00
|
|
|
this.keepalive = new KeepAlive(this, {
|
2015-07-28 20:05:55 +00:00
|
|
|
path : opts.keepalive.path,
|
|
|
|
disconnect : opts.keepalive.disconnect
|
|
|
|
});
|
2017-10-06 23:28:13 +00:00
|
|
|
var resetKeepAliveTimer = this.keepalive.reset.bind(this.keepalive);
|
2016-05-23 21:12:32 +00:00
|
|
|
socket.addEventListener('open', resetKeepAliveTimer);
|
2015-10-23 21:43:15 +00:00
|
|
|
socket.addEventListener('message', resetKeepAliveTimer);
|
2017-10-06 23:28:13 +00:00
|
|
|
socket.addEventListener('close', this.keepalive.stop.bind(this.keepalive));
|
2015-07-28 20:05:55 +00:00
|
|
|
}
|
2015-09-30 01:02:41 +00:00
|
|
|
|
2017-10-06 23:28:13 +00:00
|
|
|
socket.addEventListener('close', function() {
|
|
|
|
this.closed = true;
|
|
|
|
}.bind(this))
|
|
|
|
|
2015-09-30 01:02:41 +00:00
|
|
|
this.close = function(code, reason) {
|
2017-10-06 23:28:13 +00:00
|
|
|
if (this.closed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('WebSocketResource.close()');
|
|
|
|
if (!code) {
|
|
|
|
code = 3000;
|
|
|
|
}
|
|
|
|
if (this.keepalive) {
|
|
|
|
this.keepalive.stop();
|
|
|
|
}
|
|
|
|
|
2015-09-30 01:02:41 +00:00
|
|
|
socket.close(code, reason);
|
2017-10-06 23:28:13 +00:00
|
|
|
socket.onmessage = null;
|
|
|
|
|
|
|
|
// On linux the socket can wait a long time to emit its close event if we've
|
|
|
|
// lost the internet connection. On the order of minutes. This speeds that
|
|
|
|
// process up.
|
|
|
|
setTimeout(function() {
|
|
|
|
if (this.closed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.closed = true;
|
|
|
|
|
|
|
|
console.log('Dispatching our own socket close event');
|
|
|
|
var ev = new Event('close');
|
|
|
|
ev.code = code;
|
|
|
|
ev.reason = reason;
|
|
|
|
this.dispatchEvent(ev);
|
2017-10-23 21:38:43 +00:00
|
|
|
}.bind(this), 1000);
|
2015-09-30 01:02:41 +00:00
|
|
|
};
|
2015-07-28 20:05:55 +00:00
|
|
|
};
|
2017-10-06 23:28:13 +00:00
|
|
|
window.WebSocketResource.prototype = new textsecure.EventTarget();
|
|
|
|
|
2015-07-28 20:05:55 +00:00
|
|
|
|
|
|
|
function KeepAlive(websocketResource, opts) {
|
|
|
|
if (websocketResource instanceof WebSocketResource) {
|
|
|
|
opts = opts || {};
|
|
|
|
this.path = opts.path;
|
|
|
|
if (this.path === undefined) {
|
|
|
|
this.path = '/';
|
|
|
|
}
|
|
|
|
this.disconnect = opts.disconnect;
|
|
|
|
if (this.disconnect === undefined) {
|
2015-07-29 17:51:33 +00:00
|
|
|
this.disconnect = true;
|
2015-07-28 20:05:55 +00:00
|
|
|
}
|
|
|
|
this.wsr = websocketResource;
|
|
|
|
} else {
|
|
|
|
throw new TypeError('KeepAlive expected a WebSocketResource');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
KeepAlive.prototype = {
|
|
|
|
constructor: KeepAlive,
|
2015-10-05 22:05:24 +00:00
|
|
|
stop: function() {
|
2015-09-30 01:02:41 +00:00
|
|
|
clearTimeout(this.keepAliveTimer);
|
|
|
|
clearTimeout(this.disconnectTimer);
|
|
|
|
},
|
2015-07-28 20:05:55 +00:00
|
|
|
reset: function() {
|
|
|
|
clearTimeout(this.keepAliveTimer);
|
|
|
|
clearTimeout(this.disconnectTimer);
|
|
|
|
this.keepAliveTimer = setTimeout(function() {
|
|
|
|
if (this.disconnect) {
|
|
|
|
// automatically disconnect if server doesn't ack
|
|
|
|
this.disconnectTimer = setTimeout(function() {
|
2015-08-31 18:41:44 +00:00
|
|
|
clearTimeout(this.keepAliveTimer);
|
2015-07-28 20:05:55 +00:00
|
|
|
this.wsr.close(3001, 'No response to keepalive request');
|
|
|
|
}.bind(this), 1000);
|
|
|
|
} else {
|
|
|
|
this.reset();
|
|
|
|
}
|
2017-10-06 23:28:13 +00:00
|
|
|
console.log('Sending a keepalive message');
|
|
|
|
this.wsr.sendRequest({
|
|
|
|
verb: 'GET',
|
|
|
|
path: this.path,
|
|
|
|
success: this.reset.bind(this)
|
|
|
|
});
|
2015-07-28 20:05:55 +00:00
|
|
|
}.bind(this), 55000);
|
|
|
|
},
|
2014-11-28 06:03:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}());
|