From 9d535bb25d52968cb72fdb8a77a3f061a0a9d076 Mon Sep 17 00:00:00 2001 From: Kagami Hiiragi Date: Tue, 24 Feb 2015 11:47:56 +0300 Subject: [PATCH] Refactor decodeVersion --- lib/messages.js | 4 ++-- lib/net/base.js | 15 ++++++++++++++- lib/net/tcp.js | 11 +---------- lib/net/ws.browser.js | 12 ++---------- lib/net/ws.js | 17 ++--------------- 5 files changed, 21 insertions(+), 38 deletions(-) diff --git a/lib/messages.js b/lib/messages.js index d993892..46bdd04 100644 --- a/lib/messages.js +++ b/lib/messages.js @@ -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=} opts.streamNumbers - Streams accepted by the - * node (1 by default) + * node ([1] by default) * @return {Buffer} Encoded message. */ encode: function(opts) { diff --git a/lib/net/base.js b/lib/net/base.js index b579f3e..b18f8c6 100644 --- a/lib/net/base.js +++ b/lib/net/base.js @@ -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; }; diff --git a/lib/net/tcp.js b/lib/net/tcp.js index 70787ae..35d8dbb 100644 --- a/lib/net/tcp.js +++ b/lib/net/tcp.js @@ -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) { diff --git a/lib/net/ws.browser.js b/lib/net/ws.browser.js index 2483684..12ce60a 100644 --- a/lib/net/ws.browser.js +++ b/lib/net/ws.browser.js @@ -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) { diff --git a/lib/net/ws.js b/lib/net/ws.js index 7a9921d..368e198 100644 --- a/lib/net/ws.js +++ b/lib/net/ws.js @@ -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) {