bittorrent-dht
Advanced tools
Comparing version 3.0.2 to 3.0.3
@@ -74,6 +74,7 @@ module.exports = DHT | ||
self.nodeId = idToBuffer(opts.nodeId || hat(160)) | ||
self.ipv = opts.ipv || 4 | ||
self.nodeIdHex = idToHexString(self.nodeId) | ||
self._debug('new DHT %s', idToHexString(self.nodeId)) | ||
self._debug('new DHT %s', self.nodeIdHex) | ||
self.ipv = opts.ipv || 4 | ||
self.ready = false | ||
@@ -300,10 +301,29 @@ self.listening = false | ||
var self = this | ||
if (self._destroyed) throw new Error('dht is destroyed') | ||
if (nodeId.length !== 20) throw new Error('invalid node id length') | ||
self._addNode(addr, nodeId, from) | ||
} | ||
/** | ||
* Internal version of `addNode` that doesn't throw errors on invalid arguments, but | ||
* silently fails instead. Useful for dealing with potentially bad data from the network. | ||
* @param {string} addr | ||
* @param {string|Buffer} nodeId | ||
* @param {string=} from addr | ||
*/ | ||
DHT.prototype._addNode = function (addr, nodeId, from) { | ||
var self = this | ||
if (self._destroyed) return | ||
nodeId = idToBuffer(nodeId) | ||
if (self._addrIsSelf(addr)) { | ||
// self._debug('skipping adding %s since that is us!', addr) | ||
if (nodeId.length !== 20) { | ||
self._debug('skipping addNode %s %s; invalid id length', addr, idToHexString(nodeId)) | ||
return | ||
} | ||
if (self._addrIsSelf(addr) || bufferEqual(nodeId, self.nodeId)) { | ||
self._debug('skip addNode %s %s; that is us!', addr, idToHexString(nodeId)) | ||
return | ||
} | ||
var contact = { | ||
@@ -412,3 +432,3 @@ id: nodeId, | ||
.forEach(function (contact) { | ||
self.addNode(contact.addr, contact.id, contact.from) | ||
self._addNode(contact.addr, contact.id, contact.from) | ||
}) | ||
@@ -497,3 +517,2 @@ | ||
var self = this | ||
id = idToBuffer(id) | ||
if (typeof opts === 'function') { | ||
@@ -507,6 +526,9 @@ cb = opts | ||
id = idToBuffer(id) | ||
var idHex = idToHexString(id) | ||
if (self._destroyed) return cb(new Error('dht is destroyed')) | ||
if (!self.listening) return self.listen(self.lookup.bind(self, id, opts, cb)) | ||
if (id.length !== 20) throw new Error('invalid node id / info hash length') | ||
var idHex = idToHexString(id) | ||
self._debug('lookup %s %s', (opts.findNode ? '(find_node)' : '(get_peers)'), idHex) | ||
@@ -542,3 +564,3 @@ | ||
function add (contact) { | ||
if (self._addrIsSelf(contact.addr)) return | ||
if (self._addrIsSelf(contact.addr) || bufferEqual(contact.id, self.nodeId)) return | ||
if (contact.token) tokenful.add(contact) | ||
@@ -667,5 +689,4 @@ | ||
if (nodeId) { | ||
// TODO: verify that this a valid length for a nodeId | ||
// self._debug('adding (potentially) new node %s %s', idToHexString(nodeId), addr) | ||
self.addNode(addr, nodeId, addr) | ||
self._addNode(addr, nodeId, addr) | ||
} | ||
@@ -823,3 +844,3 @@ | ||
res.nodes.forEach(function (node) { | ||
self.addNode(node.addr, node.id, addr) | ||
self._addNode(node.addr, node.id, addr) | ||
}) | ||
@@ -890,3 +911,3 @@ } | ||
res.nodes.forEach(function (node) { | ||
self.addNode(node.addr, node.id, addr) | ||
self._addNode(node.addr, node.id, addr) | ||
}) | ||
@@ -1168,3 +1189,3 @@ } | ||
var args = [].slice.call(arguments) | ||
args[0] = '[' + idToHexString(self.nodeId).substring(0, 7) + '] ' + args[0] | ||
args[0] = '[' + self.nodeIdHex.substring(0, 7) + '] ' + args[0] | ||
debug.apply(null, args) | ||
@@ -1171,0 +1192,0 @@ } |
{ | ||
"name": "bittorrent-dht", | ||
"description": "Simple, robust, BitTorrent DHT implementation", | ||
"version": "3.0.2", | ||
"version": "3.0.3", | ||
"author": { | ||
@@ -16,3 +16,3 @@ "name": "Feross Aboukhadijeh", | ||
"bencode": "^0.7.0", | ||
"buffer-equal": "^0.0.1", | ||
"buffer-equal": "0.0.1", | ||
"compact2string": "^1.2.0", | ||
@@ -19,0 +19,0 @@ "debug": "^2.1.0", |
59863
1588
Updatedbuffer-equal@0.0.1