Refactor decodeVersion
This commit is contained in:
parent
c916023393
commit
9d535bb25d
|
@ -173,9 +173,9 @@ var version = exports.version = {
|
||||||
* to self (unique per node.js process by default)
|
* to self (unique per node.js process by default)
|
||||||
* @param {(Array|string|Buffer)=} opts.userAgent -
|
* @param {(Array|string|Buffer)=} opts.userAgent -
|
||||||
* [User agent]{@link module:bitmessage/user-agent} of the node
|
* [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
|
* @param {Array<number>=} opts.streamNumbers - Streams accepted by the
|
||||||
* node (1 by default)
|
* node ([1] by default)
|
||||||
* @return {Buffer} Encoded message.
|
* @return {Buffer} Encoded message.
|
||||||
*/
|
*/
|
||||||
encode: function(opts) {
|
encode: function(opts) {
|
||||||
|
|
|
@ -17,6 +17,8 @@ var PPromise = require("../platform").Promise;
|
||||||
var structs = require("../structs");
|
var structs = require("../structs");
|
||||||
var messages = require("../messages");
|
var messages = require("../messages");
|
||||||
|
|
||||||
|
var ServicesBitfield = structs.ServicesBitfield;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base transport class. Allows to use single class for both client and
|
* Base transport class. Allows to use single class for both client and
|
||||||
* server modes (as separate instances).
|
* server modes (as separate instances).
|
||||||
|
@ -131,10 +133,12 @@ function intersects(a, b) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decode and validate version message.
|
// Decode and validate version message.
|
||||||
BaseTransport.prototype._decodeVersion = function(payload) {
|
BaseTransport.prototype._decodeVersion = function(payload, opts) {
|
||||||
|
opts = opts || {};
|
||||||
var version;
|
var version;
|
||||||
try {
|
try {
|
||||||
version = messages.version.decodePayload(payload);
|
version = messages.version.decodePayload(payload);
|
||||||
|
@ -159,6 +163,15 @@ BaseTransport.prototype._decodeVersion = function(payload) {
|
||||||
"first 10 peer's streams: " + version.streamNumbers.slice(0, 10)
|
"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;
|
return version;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -45,7 +45,6 @@ var messages = require("../messages");
|
||||||
var BaseTransport = require("./base");
|
var BaseTransport = require("./base");
|
||||||
|
|
||||||
var assert = util.assert;
|
var assert = util.assert;
|
||||||
var ServicesBitfield = structs.ServicesBitfield;
|
|
||||||
var getmsg = BaseTransport._getmsg;
|
var getmsg = BaseTransport._getmsg;
|
||||||
var unmap = BaseTransport._unmap;
|
var unmap = BaseTransport._unmap;
|
||||||
|
|
||||||
|
@ -150,20 +149,12 @@ TcpTransport.prototype._setupClient = function(client, incoming) {
|
||||||
if (verackSent) {
|
if (verackSent) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
version = self._decodeVersion(payload);
|
version = self._decodeVersion(payload, {network: true});
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
self.emit("error", err);
|
self.emit("error", err);
|
||||||
return client.end();
|
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");
|
self.send("verack");
|
||||||
verackSent = true;
|
verackSent = true;
|
||||||
if (incoming) {
|
if (incoming) {
|
||||||
|
|
|
@ -62,7 +62,7 @@ WsTransport.prototype.connect = function(url, protocols) {
|
||||||
decoded = structs.message.decode(buf);
|
decoded = structs.message.decode(buf);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return self.emit("warning", new Error(
|
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);
|
self.emit("message", decoded.command, decoded.payload, decoded);
|
||||||
|
@ -76,20 +76,12 @@ WsTransport.prototype.connect = function(url, protocols) {
|
||||||
if (verackSent) {
|
if (verackSent) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
version = self._decodeVersion(payload);
|
version = self._decodeVersion(payload, {gateway: true});
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
self.emit("error", err);
|
self.emit("error", err);
|
||||||
return client.close();
|
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");
|
self.send("verack");
|
||||||
verackSent = true;
|
verackSent = true;
|
||||||
if (verackReceived) {
|
if (verackReceived) {
|
||||||
|
|
|
@ -123,31 +123,18 @@ WsTransport.prototype._setupClient = function(client, incoming) {
|
||||||
// High-level message processing.
|
// High-level message processing.
|
||||||
self.on("message", function(command, payload) {
|
self.on("message", function(command, payload) {
|
||||||
var version;
|
var version;
|
||||||
|
var veropts = incoming ? {mobile: true} : {gateway: true};
|
||||||
if (!established) {
|
if (!established) {
|
||||||
if (command === "version") {
|
if (command === "version") {
|
||||||
if (verackSent) {
|
if (verackSent) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
version = self._decodeVersion(payload);
|
version = self._decodeVersion(payload, veropts);
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
self.emit("error", err);
|
self.emit("error", err);
|
||||||
return client.close();
|
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");
|
self.send("verack");
|
||||||
verackSent = true;
|
verackSent = true;
|
||||||
if (incoming) {
|
if (incoming) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user