New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

bittorrent-dht

Package Overview
Dependencies
Maintainers
1
Versions
137
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bittorrent-dht - npm Package Compare versions

Comparing version 2.0.1 to 2.1.0

103

client.js

@@ -32,3 +32,3 @@ module.exports = DHT

var MESSAGE_TYPE = {
var MESSAGE_TYPE = module.exports.MESSAGE_TYPE = {
QUERY: 'q',

@@ -38,3 +38,3 @@ RESPONSE: 'r',

}
var ERROR_TYPE = {
var ERROR_TYPE = module.exports.ERROR_TYPE = {
GENERIC: 201,

@@ -46,3 +46,3 @@ SERVER: 202,

var LOCAL_HOSTS = []
var LOCAL_HOSTS = { 4: [], 6: [] }
var interfaces = os.networkInterfaces()

@@ -52,3 +52,4 @@ for (var i in interfaces) {

var face = interfaces[i][j]
if (face.family === 'IPv4') LOCAL_HOSTS.push(face.address)
if (face.family === 'IPv4') LOCAL_HOSTS[4].push(face.address)
if (face.family === 'IPv6') LOCAL_HOSTS[6].push(face.address)
}

@@ -70,5 +71,5 @@ }

if (!opts) opts = {}
if (!opts.nodeId) opts.nodeId = hat(160)
self.nodeId = idToBuffer(opts.nodeId)
self.nodeId = idToBuffer(opts.nodeId || hat(160))
self.ipv = opts.ipv || 4

@@ -84,2 +85,14 @@ self._debug('new DHT %s', idToHexString(self.nodeId))

/**
* Query Handlers table
* @type {Object} string -> function
*/
self.queryHandler = {
ping: self._onPing,
find_node: self._onFindNode,
get_peers: self._onGetPeers,
announce_peer: self._onAnnouncePeer
}
/**
* Routing table

@@ -121,3 +134,3 @@ * @type {KBucket}

// Create socket and attach listeners
self.socket = dgram.createSocket('udp4')
self.socket = dgram.createSocket('udp' + self.ipv)
self.socket.on('message', self._onData.bind(self))

@@ -444,3 +457,3 @@ self.socket.on('listening', self._onListening.bind(self))

var addrData = self._getAddrData(contact.addr)
dns.lookup(addrData[0], 4, function (err, host) {
dns.lookup(addrData[0], self.ipv, function (err, host) {
if (err) return cb(null, null)

@@ -646,12 +659,6 @@ contact.addr = host + ':' + addrData[1]

if (query === 'ping') {
self._onPing(addr, message)
} else if (query === 'find_node') {
self._onFindNode(addr, message)
} else if (query === 'get_peers') {
self._onGetPeers(addr, message)
} else if (query === 'announce_peer') {
self._onAnnouncePeer(addr, message)
if (typeof self.queryHandler[query] === 'function') {
self.queryHandler[query].call(self, addr, message)
} else {
var errMessage = 'unexpected query type ' + query
var errMessage = 'unexpected query type'
self._debug(errMessage)

@@ -674,3 +681,4 @@ self._sendError(addr, message.t, ERROR_TYPE.METHOD_UNKNOWN, errMessage)

var transaction = self.transactions[addr] && self.transactions[addr][transactionId]
var transaction = self.transactions && self.transactions[addr]
&& self.transactions[addr][transactionId]

@@ -721,10 +729,8 @@ var err = null

/**
* Send "ping" query to given addr.
* @param {string} addr
* @param {function} cb called with response
*/
DHT.prototype._sendPing = function (addr, cb) {
DHT.prototype.query = function (data, addr, cb) {
var self = this
if (!data.a) data.a = {}
if (!data.a.id) data.a.id = self.nodeId
var transactionId = self._getTransactionId(addr, cb)

@@ -734,8 +740,7 @@ var message = {

y: MESSAGE_TYPE.QUERY,
q: 'ping',
a: {
id: self.nodeId
}
q: data.q,
a: data.a
}
self._debug('sent ping to ' + addr)
self._debug('sent %s %s to %s', data.q, JSON.stringify(data.a), addr)
self._send(addr, message)

@@ -745,2 +750,12 @@ }

/**
* Send "ping" query to given addr.
* @param {string} addr
* @param {function} cb called with response
*/
DHT.prototype._sendPing = function (addr, cb) {
var self = this
self.query({ q: 'ping' }, addr, cb)
}
/**
* Called when another node sends a "ping" query.

@@ -784,6 +799,3 @@ * @param {string} addr

var transactionId = self._getTransactionId(addr, onResponse)
var message = {
t: transactionIdToBuffer(transactionId),
y: MESSAGE_TYPE.QUERY,
var data = {
q: 'find_node',

@@ -795,4 +807,4 @@ a: {

}
self._debug('sent find_node %s to %s', idToHexString(nodeId), addr)
self._send(addr, message)
self.query(data, addr, onResponse)
}

@@ -861,6 +873,3 @@

var transactionId = self._getTransactionId(addr, onResponse)
var message = {
t: transactionIdToBuffer(transactionId),
y: MESSAGE_TYPE.QUERY,
var data = {
q: 'get_peers',

@@ -872,4 +881,4 @@ a: {

}
self._debug('sent get_peers %s to %s', idToHexString(infoHash), addr)
self._send(addr, message)
self.query(data, addr, onResponse)
}

@@ -932,6 +941,3 @@

var transactionId = self._getTransactionId(addr, cb)
var message = {
t: transactionIdToBuffer(transactionId),
y: MESSAGE_TYPE.QUERY,
var data = {
q: 'announce_peer',

@@ -946,5 +952,4 @@ a: {

}
self._debug('sent announce_peer %s %s to %s with token %s', idToHexString(infoHash),
port, addr, idToHexString(token))
self._send(addr, message)
self.query(data, addr, cb)
}

@@ -1146,3 +1151,3 @@

return self.port &&
LOCAL_HOSTS.some(function (host) { return host + ':' + self.port === addr })
LOCAL_HOSTS[self.ipv].some(function (host) { return host + ':' + self.port === addr })
}

@@ -1149,0 +1154,0 @@

{
"name": "bittorrent-dht",
"description": "Simple, robust, BitTorrent DHT implementation",
"version": "2.0.1",
"version": "2.1.0",
"author": {

@@ -6,0 +6,0 @@ "name": "Feross Aboukhadijeh",

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc