Refactor decodeVersion

This commit is contained in:
Kagami Hiiragi 2015-02-24 11:47:56 +03:00
parent c916023393
commit 9d535bb25d
5 changed files with 21 additions and 38 deletions

View File

@ -173,9 +173,9 @@ var version = exports.version = {
* to self (unique per node.js process by default)
* @param {(Array|string|Buffer)=} opts.userAgent -
* [User agent]{@link module:bitmessage/user-agent} of the node
* (bitmessage's by default)
* (user agent of bitmessage library by default)
* @param {Array<number>=} opts.streamNumbers - Streams accepted by the
* node (1 by default)
* node ([1] by default)
* @return {Buffer} Encoded message.
*/
encode: function(opts) {

View File

@ -17,6 +17,8 @@ var PPromise = require("../platform").Promise;
var structs = require("../structs");
var messages = require("../messages");
var ServicesBitfield = structs.ServicesBitfield;
/**
* Base transport class. Allows to use single class for both client and
* server modes (as separate instances).
@ -131,10 +133,12 @@ function intersects(a, b) {
}
}
}
return false;
}
// Decode and validate version message.
BaseTransport.prototype._decodeVersion = function(payload) {
BaseTransport.prototype._decodeVersion = function(payload, opts) {
opts = opts || {};
var version;
try {
version = messages.version.decodePayload(payload);
@ -159,6 +163,15 @@ BaseTransport.prototype._decodeVersion = function(payload) {
"first 10 peer's streams: " + version.streamNumbers.slice(0, 10)
);
}
if (opts.network && !version.services.get(ServicesBitfield.NODE_NETWORK)) {
throw new Error("Not a normal network node: " + version.services);
}
if (opts.gateway && !version.services.get(ServicesBitfield.NODE_GATEWAY)) {
throw new Error("Not a gateway node: " + version.services);
}
if (opts.mobile && !version.services.get(ServicesBitfield.NODE_MOBILE)) {
throw new Error("Not a mobile node: " + version.services);
}
return version;
};

View File

@ -45,7 +45,6 @@ var messages = require("../messages");
var BaseTransport = require("./base");
var assert = util.assert;
var ServicesBitfield = structs.ServicesBitfield;
var getmsg = BaseTransport._getmsg;
var unmap = BaseTransport._unmap;
@ -150,20 +149,12 @@ TcpTransport.prototype._setupClient = function(client, incoming) {
if (verackSent) {
return;
}
try {
version = self._decodeVersion(payload);
version = self._decodeVersion(payload, {network: true});
} catch(err) {
self.emit("error", err);
return client.end();
}
if (!version.services.get(ServicesBitfield.NODE_NETWORK)) {
self.emit("error", new Error(
"Not a normal network node: " + version.services
));
return client.end();
}
self.send("verack");
verackSent = true;
if (incoming) {

View File

@ -62,7 +62,7 @@ WsTransport.prototype.connect = function(url, protocols) {
decoded = structs.message.decode(buf);
} catch (err) {
return self.emit("warning", new Error(
"Message decoding error from " + url + ": " + err
"Message decoding error: " + err.message
));
}
self.emit("message", decoded.command, decoded.payload, decoded);
@ -76,20 +76,12 @@ WsTransport.prototype.connect = function(url, protocols) {
if (verackSent) {
return;
}
try {
version = self._decodeVersion(payload);
version = self._decodeVersion(payload, {gateway: true});
} catch(err) {
self.emit("error", err);
return client.close();
}
if (!version.services.get(ServicesBitfield.NODE_GATEWAY)) {
self.emit("error", new Error(
"Not a gateway node: " + version.services
));
return client.close();
}
self.send("verack");
verackSent = true;
if (verackReceived) {

View File

@ -123,31 +123,18 @@ WsTransport.prototype._setupClient = function(client, incoming) {
// High-level message processing.
self.on("message", function(command, payload) {
var version;
var veropts = incoming ? {mobile: true} : {gateway: true};
if (!established) {
if (command === "version") {
if (verackSent) {
return;
}
try {
version = self._decodeVersion(payload);
version = self._decodeVersion(payload, veropts);
} catch(err) {
self.emit("error", err);
return client.close();
}
if (incoming && !version.services.get(ServicesBitfield.NODE_MOBILE)) {
self.emit("error", new Error(
"Not a mobile node: " + version.services
));
return client.close();
}
if (!incoming && !version.services.get(ServicesBitfield.NODE_GATEWAY)) {
self.emit("error", new Error(
"Not a gateway node: " + version.services
));
return client.close();
}
self.send("verack");
verackSent = true;
if (incoming) {