bitmessage-js/lib/net/ws.js

248 lines
7.3 KiB
JavaScript
Raw Normal View History

2015-02-03 21:13:27 +01:00
/**
2015-02-09 22:56:55 +01:00
* WebSocket transport. Needed because browsers can't handle TCP sockets
* so we use separate WebSocket server to proxy messages into TCP data
2015-02-10 13:57:55 +01:00
* packets. Available for both Node.js and Browser platforms.
* **NOTE**: `WsTransport` is exported as a module.
2015-02-14 11:53:24 +01:00
* @example var WsTransport = require("bitmessage/lib/net/ws");
2015-02-14 12:00:27 +01:00
* @module bitmessage/net/ws
2015-02-03 21:13:27 +01:00
*/
"use strict";
2015-02-09 22:56:55 +01:00
var objectAssign = Object.assign || require("object-assign");
2015-02-03 21:13:27 +01:00
var inherits = require("inherits");
var WebSocket = require("ws"); // jshint ignore:line
var assert = require("../_util").assert;
2015-02-09 22:56:55 +01:00
var PPromise = require("../platform").Promise;
var structs = require("../structs");
var messages = require("../messages");
2015-02-06 10:47:40 +01:00
var BaseTransport = require("./base");
2015-02-03 21:13:27 +01:00
var WebSocketServer = WebSocket.Server;
var ServicesBitfield = structs.ServicesBitfield;
2015-02-09 22:56:55 +01:00
var getmsg = BaseTransport._getmsg;
var unmap = BaseTransport._unmap;
2015-02-03 21:13:27 +01:00
/**
2015-02-14 12:05:25 +01:00
* WebSocket transport class. Implements
* [base transport interface]{@link
* module:bitmessage/net/base.BaseTransport}.
2015-02-10 19:23:20 +01:00
* @param {Object=} opts - Transport options
2015-02-10 15:59:49 +01:00
* @param {Array} opts.seeds - Bootstrap nodes (none by default)
* @param {Object} opts.services -
* [Service features]{@link module:bitmessage/structs.ServicesBitfield}
* provided by this node (`NODE_MOBILE` for Browser and `NODE_MOBILE` +
* `NODE_GATEWAY` for Node by default)
2015-02-10 15:59:49 +01:00
* @param {(Array|string|Buffer)} opts.userAgent -
* [User agent]{@link module:bitmessage/user-agent} of this node
* (user agent of bitmessage library by default)
* @param {number[]} opts.streams - Streams accepted by this node ([1]
* by default)
2015-02-14 16:11:18 +01:00
* @param {number} opts.port - Incoming port of this node, makes sence
* only on Node platform (18444 by default)
2015-02-03 21:13:27 +01:00
* @constructor
* @static
*/
2015-02-10 13:57:55 +01:00
function WsTransport(opts) {
WsTransport.super_.call(this);
2015-02-09 22:56:55 +01:00
objectAssign(this, opts);
this.seeds = this.seeds || [];
this.services = this.services || ServicesBitfield().set([
ServicesBitfield.NODE_MOBILE,
ServicesBitfield.NODE_GATEWAY,
]);
this.streams = this.streams || [1];
2015-02-14 16:11:18 +01:00
this.port = this.port || 18444;
2015-02-03 21:13:27 +01:00
}
2015-02-10 13:57:55 +01:00
inherits(WsTransport, BaseTransport);
2015-02-03 21:13:27 +01:00
2015-02-10 13:57:55 +01:00
WsTransport.prototype._sendVersion = function() {
2015-02-09 22:56:55 +01:00
return this.send(messages.version.encode({
services: this.services,
userAgent: this.userAgent,
streams: this.streams,
2015-02-09 22:56:55 +01:00
port: this.port,
remoteHost: this._client._socket.remoteAddress,
remotePort: this._client._socket.remotePort,
}));
};
2015-02-10 13:57:55 +01:00
WsTransport.prototype._handleTimeout = function() {
2015-02-09 22:56:55 +01:00
var client = this._client;
// TODO(Kagami): We may also want to close connection if it wasn't
// established within minute.
client._socket.setTimeout(20000);
client._socket.on("timeout", function() {
client.close();
});
this.on("established", function() {
// Raise timeout up to 10 minutes per spec.
// TODO(Kagami): Send ping frame every 5 minutes as PyBitmessage.
client._socket.setTimeout(600000);
});
};
2015-02-17 13:35:40 +01:00
WsTransport.prototype._setupClient = function(client, incoming) {
2015-02-09 22:56:55 +01:00
var self = this;
self._client = client;
var verackSent = false;
var verackReceived = false;
var established = false;
client.on("open", function() {
// NOTE(Kagami): This handler shouldn't be called at all for
2015-02-17 13:35:40 +01:00
// incoming connections but let's be sure.
if (!incoming) {
2015-02-09 22:56:55 +01:00
// NOTE(Kagami): We may set timeout only after connection was
// opened because socket may not yet be available when
// `_setupClient` is called.
self._handleTimeout();
self.emit("open");
2015-02-10 13:57:55 +01:00
self._sendVersion();
2015-02-09 22:56:55 +01:00
}
});
client.on("message", function(data, flags) {
var decoded;
if (!flags.binary) {
// TODO(Kagami): Send `error` message and ban node for some time
// if there were too many errors?
return self.emit("warning", new Error("Peer sent non-binary data"));
2015-02-09 22:56:55 +01:00
}
try {
decoded = structs.message.decode(data);
} catch (err) {
return self.emit("warning", new Error(
"Message decoding error: " + err.message
2015-02-09 22:56:55 +01:00
));
}
self.emit("message", decoded.command, decoded.payload);
2015-02-09 22:56:55 +01:00
});
// High-level message processing.
self.on("message", function(command, payload) {
var version;
2015-02-24 09:47:56 +01:00
var veropts = incoming ? {mobile: true} : {gateway: true};
2015-02-09 22:56:55 +01:00
if (!established) {
if (command === "version") {
if (verackSent) {
return;
}
try {
2015-02-24 09:47:56 +01:00
version = self._decodeVersion(payload, veropts);
} catch(err) {
self.emit("error", err);
return client.close();
}
2015-02-09 22:56:55 +01:00
self.send("verack");
verackSent = true;
2015-02-17 13:35:40 +01:00
if (incoming) {
2015-02-10 13:57:55 +01:00
self._sendVersion();
2015-02-09 22:56:55 +01:00
} else if (verackReceived) {
established = true;
self.emit("established", version);
2015-02-09 22:56:55 +01:00
}
} else if (command === "verack") {
verackReceived = true;
if (verackSent) {
established = true;
self.emit("established", version);
2015-02-09 22:56:55 +01:00
}
}
}
});
client.on("error", function(err) {
self.emit("error", err);
});
client.on("close", function() {
self.emit("close");
delete self._client;
});
};
2015-02-10 13:57:55 +01:00
WsTransport.prototype.bootstrap = function() {
2015-02-09 22:56:55 +01:00
return PPromise.resolve([].concat(this.seeds));
};
2015-02-10 13:57:55 +01:00
/**
* Connect to a WebSocket node. Connection arguments are the same as for
* [WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket).
*/
WsTransport.prototype.connect = function(address, protocols, options) {
2015-02-03 21:13:27 +01:00
assert(!this._client, "Already connected");
2015-02-09 22:56:55 +01:00
assert(!this._server, "Already listening");
// `new` doesn't work with `apply`, so passing all possible arguments
// manually.
this._setupClient(new WebSocket(address, protocols, options));
2015-02-03 21:13:27 +01:00
};
2015-02-10 13:57:55 +01:00
/**
* Listen for incoming WebSocket connections. Listen arguments are the
* same as for
* [WebSocketServer](https://github.com/websockets/ws#server-example).
* Available only for Node platform.
*/
WsTransport.prototype.listen = function(options, callback) {
2015-02-09 22:56:55 +01:00
assert(!this._client, "Already connected");
2015-02-03 21:13:27 +01:00
assert(!this._server, "Already listening");
2015-02-09 22:56:55 +01:00
var self = this;
var server = self._server = new WebSocketServer(options, callback);
server.on("connection", function(client) {
2015-02-22 11:12:23 +01:00
var opts = objectAssign({}, self);
delete opts._server;
var transport = new self.constructor(opts);
2015-02-17 13:35:40 +01:00
var incoming = true;
transport._setupClient(client, incoming);
2015-02-09 22:56:55 +01:00
transport._handleTimeout();
2015-02-17 13:35:40 +01:00
var addr = client._socket.remoteAddress;
var port = client._socket.remotePort;
2015-02-09 22:56:55 +01:00
self.emit("connection", transport, unmap(addr), port);
});
server.on("error", function(err) {
self.emit("error", err);
});
2015-02-23 12:00:31 +01:00
// `ws` doesn't emit "close" event by default.
server._server.on("close", function() {
self.emit("close");
delete self._server;
});
2015-02-09 22:56:55 +01:00
};
2015-02-10 13:57:55 +01:00
WsTransport.prototype.send = function() {
2015-02-09 22:56:55 +01:00
if (this._client) {
// TODO(Kagami): `mask: true` doesn't work with Chromium 40. File a
// bug to ws bugtracker.
this._client.send(getmsg(arguments), {binary: true});
} else {
throw new Error("Not connected");
}
};
2015-02-10 13:57:55 +01:00
WsTransport.prototype.broadcast = function() {
2015-02-09 22:56:55 +01:00
var data = getmsg(arguments);
if (this._server) {
this._server.clients.forEach(function(client) {
client.send(data, {binary: true});
});
} else {
throw new Error("Not listening");
}
};
2015-02-10 13:57:55 +01:00
WsTransport.prototype.close = function() {
2015-02-09 22:56:55 +01:00
if (this._client) {
this._client.close();
} else if (this._server) {
this._server.close();
}
2015-02-03 21:13:27 +01:00
};
2015-02-10 13:57:55 +01:00
module.exports = WsTransport;