2014-06-07 22:10:12 +00:00
|
|
|
|
module.exports = Server
|
|
|
|
|
|
2016-05-30 06:12:23 +00:00
|
|
|
|
var Buffer = require('safe-buffer').Buffer
|
2014-06-12 00:57:19 +00:00
|
|
|
|
var bencode = require('bencode')
|
2017-01-21 02:34:33 +00:00
|
|
|
|
var debug = require('debug')('bittorrent-tracker:server')
|
2014-06-07 22:10:12 +00:00
|
|
|
|
var dgram = require('dgram')
|
|
|
|
|
var EventEmitter = require('events').EventEmitter
|
|
|
|
|
var http = require('http')
|
|
|
|
|
var inherits = require('inherits')
|
2016-06-07 12:34:38 +00:00
|
|
|
|
var peerid = require('bittorrent-peerid')
|
2014-12-10 15:47:41 +00:00
|
|
|
|
var series = require('run-series')
|
2014-12-10 15:51:35 +00:00
|
|
|
|
var string2compact = require('string2compact')
|
2015-03-27 03:19:06 +00:00
|
|
|
|
var WebSocketServer = require('ws').Server
|
2014-06-07 22:10:12 +00:00
|
|
|
|
|
2014-12-10 15:47:41 +00:00
|
|
|
|
var common = require('./lib/common')
|
2015-07-29 07:26:44 +00:00
|
|
|
|
var Swarm = require('./lib/server/swarm')
|
|
|
|
|
var parseHttpRequest = require('./lib/server/parse-http')
|
|
|
|
|
var parseUdpRequest = require('./lib/server/parse-udp')
|
|
|
|
|
var parseWebSocketRequest = require('./lib/server/parse-websocket')
|
2014-12-09 23:44:45 +00:00
|
|
|
|
|
2014-06-07 22:10:12 +00:00
|
|
|
|
inherits(Server, EventEmitter)
|
|
|
|
|
|
|
|
|
|
/**
|
2015-03-27 03:19:06 +00:00
|
|
|
|
* BitTorrent tracker server.
|
2014-06-07 22:10:12 +00:00
|
|
|
|
*
|
2015-03-27 03:19:06 +00:00
|
|
|
|
* HTTP service which responds to GET requests from torrent clients. Requests include
|
|
|
|
|
* metrics from clients that help the tracker keep overall statistics about the torrent.
|
|
|
|
|
* Responses include a peer list that helps the client participate in the torrent.
|
2014-06-07 22:10:12 +00:00
|
|
|
|
*
|
2015-03-20 04:47:47 +00:00
|
|
|
|
* @param {Object} opts options object
|
|
|
|
|
* @param {Number} opts.interval tell clients to announce on this interval (ms)
|
|
|
|
|
* @param {Number} opts.trustProxy trust 'x-forwarded-for' header from reverse proxy
|
|
|
|
|
* @param {boolean} opts.http start an http server? (default: true)
|
|
|
|
|
* @param {boolean} opts.udp start a udp server? (default: true)
|
2015-03-27 03:19:06 +00:00
|
|
|
|
* @param {boolean} opts.ws start a websocket server? (default: true)
|
2016-03-30 07:31:51 +00:00
|
|
|
|
* @param {boolean} opts.stats enable web-based statistics? (default: true)
|
2015-03-20 04:47:47 +00:00
|
|
|
|
* @param {function} opts.filter black/whitelist fn for disallowing/allowing torrents
|
2014-06-07 22:10:12 +00:00
|
|
|
|
*/
|
|
|
|
|
function Server (opts) {
|
|
|
|
|
var self = this
|
|
|
|
|
if (!(self instanceof Server)) return new Server(opts)
|
|
|
|
|
EventEmitter.call(self)
|
2015-03-27 03:19:06 +00:00
|
|
|
|
if (!opts) opts = {}
|
|
|
|
|
|
|
|
|
|
debug('new server %s', JSON.stringify(opts))
|
2014-06-07 22:10:12 +00:00
|
|
|
|
|
2015-07-29 08:47:09 +00:00
|
|
|
|
self.intervalMs = opts.interval
|
2015-01-05 18:55:58 +00:00
|
|
|
|
? opts.interval
|
|
|
|
|
: 10 * 60 * 1000 // 10 min
|
2014-06-07 22:10:12 +00:00
|
|
|
|
|
|
|
|
|
self._trustProxy = !!opts.trustProxy
|
2015-01-29 22:59:08 +00:00
|
|
|
|
if (typeof opts.filter === 'function') self._filter = opts.filter
|
2014-06-07 22:10:12 +00:00
|
|
|
|
|
2016-06-08 21:57:01 +00:00
|
|
|
|
self.peersCacheLength = opts.peersCacheLength
|
|
|
|
|
self.peersCacheTtl = opts.peersCacheTtl
|
|
|
|
|
|
2015-06-10 05:32:04 +00:00
|
|
|
|
self._listenCalled = false
|
2014-08-17 02:05:56 +00:00
|
|
|
|
self.listening = false
|
2015-06-10 05:25:25 +00:00
|
|
|
|
self.destroyed = false
|
2014-06-07 22:10:12 +00:00
|
|
|
|
self.torrents = {}
|
|
|
|
|
|
2015-03-27 03:19:06 +00:00
|
|
|
|
self.http = null
|
2015-05-02 19:15:14 +00:00
|
|
|
|
self.udp4 = null
|
|
|
|
|
self.udp6 = null
|
2015-03-27 03:19:06 +00:00
|
|
|
|
self.ws = null
|
|
|
|
|
|
|
|
|
|
// start an http tracker unless the user explictly says no
|
2014-06-07 22:10:12 +00:00
|
|
|
|
if (opts.http !== false) {
|
2015-01-29 22:59:08 +00:00
|
|
|
|
self.http = http.createServer()
|
2015-07-02 23:24:03 +00:00
|
|
|
|
self.http.on('error', function (err) { self._onError(err) })
|
2015-01-29 22:59:08 +00:00
|
|
|
|
self.http.on('listening', onListening)
|
2015-12-16 11:40:46 +00:00
|
|
|
|
|
|
|
|
|
// Add default http request handler on next tick to give user the chance to add
|
|
|
|
|
// their own handler first. Handle requests untouched by user's handler.
|
|
|
|
|
process.nextTick(function () {
|
|
|
|
|
self.http.on('request', function (req, res) {
|
|
|
|
|
if (res.headersSent) return
|
|
|
|
|
self.onHttpRequest(req, res)
|
|
|
|
|
})
|
|
|
|
|
})
|
2014-06-07 22:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-27 03:19:06 +00:00
|
|
|
|
// start a udp tracker unless the user explicitly says no
|
2014-06-07 22:10:12 +00:00
|
|
|
|
if (opts.udp !== false) {
|
2015-05-30 04:05:00 +00:00
|
|
|
|
var isNode10 = /^v0.10./.test(process.version)
|
|
|
|
|
|
|
|
|
|
self.udp4 = self.udp = dgram.createSocket(
|
|
|
|
|
isNode10 ? 'udp4' : { type: 'udp4', reuseAddr: true }
|
|
|
|
|
)
|
2015-07-02 23:24:03 +00:00
|
|
|
|
self.udp4.on('message', function (msg, rinfo) { self.onUdpRequest(msg, rinfo) })
|
|
|
|
|
self.udp4.on('error', function (err) { self._onError(err) })
|
2015-05-02 19:15:14 +00:00
|
|
|
|
self.udp4.on('listening', onListening)
|
|
|
|
|
|
2015-05-30 04:05:00 +00:00
|
|
|
|
self.udp6 = dgram.createSocket(
|
|
|
|
|
isNode10 ? 'udp6' : { type: 'udp6', reuseAddr: true }
|
|
|
|
|
)
|
2015-07-02 23:24:03 +00:00
|
|
|
|
self.udp6.on('message', function (msg, rinfo) { self.onUdpRequest(msg, rinfo) })
|
|
|
|
|
self.udp6.on('error', function (err) { self._onError(err) })
|
2015-05-02 19:15:14 +00:00
|
|
|
|
self.udp6.on('listening', onListening)
|
2014-08-07 05:35:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-27 03:19:06 +00:00
|
|
|
|
// start a websocket tracker (for WebTorrent) unless the user explicitly says no
|
2015-07-08 16:44:00 +00:00
|
|
|
|
if (opts.ws !== false) {
|
2015-03-27 03:19:06 +00:00
|
|
|
|
if (!self.http) {
|
|
|
|
|
self.http = http.createServer()
|
2015-07-02 23:24:03 +00:00
|
|
|
|
self.http.on('error', function (err) { self._onError(err) })
|
2015-03-27 03:19:06 +00:00
|
|
|
|
self.http.on('listening', onListening)
|
2015-12-15 23:37:53 +00:00
|
|
|
|
|
2015-12-16 11:41:07 +00:00
|
|
|
|
// Add default http request handler on next tick to give user the chance to add
|
|
|
|
|
// their own handler first. Handle requests untouched by user's handler.
|
2015-12-15 23:37:53 +00:00
|
|
|
|
process.nextTick(function () {
|
|
|
|
|
self.http.on('request', function (req, res) {
|
2015-12-16 11:41:07 +00:00
|
|
|
|
if (res.headersSent) return
|
|
|
|
|
// For websocket trackers, we only need to handle the UPGRADE http method.
|
|
|
|
|
// Return 404 for all other request types.
|
2015-12-15 23:37:53 +00:00
|
|
|
|
res.statusCode = 404
|
|
|
|
|
res.end('404 Not Found')
|
|
|
|
|
})
|
|
|
|
|
})
|
2015-03-27 03:19:06 +00:00
|
|
|
|
}
|
2017-02-08 21:53:50 +00:00
|
|
|
|
self.ws = new WebSocketServer({
|
|
|
|
|
server: self.http,
|
2017-02-08 21:47:54 +00:00
|
|
|
|
perMessageDeflate: false,
|
|
|
|
|
clientTracking: false
|
|
|
|
|
})
|
2016-03-29 06:38:23 +00:00
|
|
|
|
self.ws.address = function () {
|
|
|
|
|
return self.http.address()
|
|
|
|
|
}
|
2015-07-02 23:24:03 +00:00
|
|
|
|
self.ws.on('error', function (err) { self._onError(err) })
|
|
|
|
|
self.ws.on('connection', function (socket) { self.onWebSocketConnection(socket) })
|
2015-03-27 03:19:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-29 22:03:24 +00:00
|
|
|
|
if (opts.stats !== false) {
|
|
|
|
|
if (!self.http) {
|
|
|
|
|
self.http = http.createServer()
|
|
|
|
|
self.http.on('error', function (err) { self._onError(err) })
|
|
|
|
|
self.http.on('listening', onListening)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Http handler for '/stats' route
|
2016-03-30 07:31:51 +00:00
|
|
|
|
self.http.on('request', function (req, res) {
|
2016-03-29 22:03:24 +00:00
|
|
|
|
if (res.headersSent) return
|
|
|
|
|
|
|
|
|
|
var infoHashes = Object.keys(self.torrents)
|
|
|
|
|
var activeTorrents = 0
|
|
|
|
|
var allPeers = {}
|
|
|
|
|
|
|
|
|
|
function countPeers (filterFunction) {
|
|
|
|
|
var count = 0
|
|
|
|
|
var key
|
|
|
|
|
|
|
|
|
|
for (key in allPeers) {
|
|
|
|
|
if (allPeers.hasOwnProperty(key) && filterFunction(allPeers[key])) {
|
|
|
|
|
count++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return count
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-07 12:34:38 +00:00
|
|
|
|
function groupByClient () {
|
|
|
|
|
var clients = {}
|
|
|
|
|
for (var key in allPeers) {
|
|
|
|
|
if (allPeers.hasOwnProperty(key)) {
|
|
|
|
|
var peer = allPeers[key]
|
|
|
|
|
|
|
|
|
|
if (!clients[peer.client.client]) {
|
|
|
|
|
clients[peer.client.client] = {}
|
|
|
|
|
}
|
|
|
|
|
var client = clients[peer.client.client]
|
|
|
|
|
// If the client is not known show 8 chars from peerId as version
|
|
|
|
|
var version = peer.client.version || new Buffer(peer.peerId, 'hex').toString().substring(0, 8)
|
|
|
|
|
if (!client[version]) {
|
|
|
|
|
client[version] = 0
|
|
|
|
|
}
|
|
|
|
|
client[version]++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return clients
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function printClients (clients) {
|
|
|
|
|
var html = '<ul>\n'
|
|
|
|
|
for (var name in clients) {
|
|
|
|
|
if (clients.hasOwnProperty(name)) {
|
|
|
|
|
var client = clients[name]
|
|
|
|
|
for (var version in client) {
|
|
|
|
|
if (client.hasOwnProperty(version)) {
|
|
|
|
|
html += '<li><strong>' + name + '</strong> ' + version + ' : ' + client[version] + '</li>\n'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
html += '</ul>'
|
|
|
|
|
return html
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-07 08:49:27 +00:00
|
|
|
|
if (req.method === 'GET' && (req.url === '/stats' || req.url === '/stats.json')) {
|
2016-03-29 22:03:24 +00:00
|
|
|
|
infoHashes.forEach(function (infoHash) {
|
|
|
|
|
var peers = self.torrents[infoHash].peers
|
2016-06-10 08:00:19 +00:00
|
|
|
|
var keys = peers.keys
|
2016-03-29 22:03:24 +00:00
|
|
|
|
if (keys.length > 0) activeTorrents++
|
|
|
|
|
|
|
|
|
|
keys.forEach(function (peerId) {
|
2016-06-08 21:57:01 +00:00
|
|
|
|
// Don't mark the peer as most recently used for stats
|
|
|
|
|
var peer = peers.peek(peerId)
|
2016-08-10 04:39:30 +00:00
|
|
|
|
if (peer == null) return // peers.peek() can evict the peer
|
|
|
|
|
|
|
|
|
|
if (!allPeers.hasOwnProperty(peerId)) {
|
|
|
|
|
allPeers[peerId] = {
|
|
|
|
|
ipv4: false,
|
|
|
|
|
ipv6: false,
|
|
|
|
|
seeder: false,
|
|
|
|
|
leecher: false
|
2016-08-09 11:30:40 +00:00
|
|
|
|
}
|
2016-08-10 04:39:30 +00:00
|
|
|
|
}
|
2016-08-09 11:30:40 +00:00
|
|
|
|
|
2016-08-10 04:39:30 +00:00
|
|
|
|
if (peer.ip.indexOf(':') >= 0) {
|
|
|
|
|
allPeers[peerId].ipv6 = true
|
|
|
|
|
} else {
|
|
|
|
|
allPeers[peerId].ipv4 = true
|
2016-03-29 22:03:24 +00:00
|
|
|
|
}
|
2016-08-10 04:39:30 +00:00
|
|
|
|
|
|
|
|
|
if (peer.complete) {
|
|
|
|
|
allPeers[peerId].seeder = true
|
|
|
|
|
} else {
|
|
|
|
|
allPeers[peerId].leecher = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
allPeers[peerId].peerId = peer.peerId
|
|
|
|
|
allPeers[peerId].client = peerid(peer.peerId)
|
2016-03-29 22:03:24 +00:00
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
var isSeederOnly = function (peer) { return peer.seeder && peer.leecher === false }
|
|
|
|
|
var isLeecherOnly = function (peer) { return peer.leecher && peer.seeder === false }
|
|
|
|
|
var isSeederAndLeecher = function (peer) { return peer.seeder && peer.leecher }
|
|
|
|
|
var isIPv4 = function (peer) { return peer.ipv4 }
|
|
|
|
|
var isIPv6 = function (peer) { return peer.ipv6 }
|
|
|
|
|
|
2016-06-07 08:49:27 +00:00
|
|
|
|
var stats = {
|
|
|
|
|
torrents: infoHashes.length,
|
|
|
|
|
activeTorrents: activeTorrents,
|
|
|
|
|
peersAll: Object.keys(allPeers).length,
|
|
|
|
|
peersSeederOnly: countPeers(isSeederOnly),
|
|
|
|
|
peersLeecherOnly: countPeers(isLeecherOnly),
|
|
|
|
|
peersSeederAndLeecher: countPeers(isSeederAndLeecher),
|
|
|
|
|
peersIPv4: countPeers(isIPv4),
|
2016-06-07 12:34:38 +00:00
|
|
|
|
peersIPv6: countPeers(isIPv6),
|
|
|
|
|
clients: groupByClient()
|
2016-06-02 02:27:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-12 22:34:26 +00:00
|
|
|
|
if (req.url === '/stats.json' || req.headers['accept'] === 'application/json') {
|
2016-06-07 08:49:27 +00:00
|
|
|
|
res.write(JSON.stringify(stats))
|
2016-06-02 02:27:14 +00:00
|
|
|
|
res.end()
|
2016-06-07 08:49:27 +00:00
|
|
|
|
} else if (req.url === '/stats') {
|
|
|
|
|
res.end('<h1>' + stats.torrents + ' torrents (' + stats.activeTorrents + ' active)</h1>\n' +
|
|
|
|
|
'<h2>Connected Peers: ' + stats.peersAll + '</h2>\n' +
|
|
|
|
|
'<h3>Peers Seeding Only: ' + stats.peersSeederOnly + '</h3>\n' +
|
|
|
|
|
'<h3>Peers Leeching Only: ' + stats.peersLeecherOnly + '</h3>\n' +
|
|
|
|
|
'<h3>Peers Seeding & Leeching: ' + stats.peersSeederAndLeecher + '</h3>\n' +
|
|
|
|
|
'<h3>IPv4 Peers: ' + stats.peersIPv4 + '</h3>\n' +
|
2016-06-07 12:34:38 +00:00
|
|
|
|
'<h3>IPv6 Peers: ' + stats.peersIPv6 + '</h3>\n' +
|
|
|
|
|
'<h3>Clients:</h3>\n' +
|
|
|
|
|
printClients(stats.clients)
|
|
|
|
|
)
|
2016-06-02 02:27:14 +00:00
|
|
|
|
}
|
2016-03-29 22:03:24 +00:00
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-02 19:15:14 +00:00
|
|
|
|
var num = !!self.http + !!self.udp4 + !!self.udp6
|
2014-08-07 05:35:43 +00:00
|
|
|
|
function onListening () {
|
|
|
|
|
num -= 1
|
2014-08-17 02:05:56 +00:00
|
|
|
|
if (num === 0) {
|
|
|
|
|
self.listening = true
|
2015-03-27 03:19:06 +00:00
|
|
|
|
debug('listening')
|
2015-01-29 22:59:08 +00:00
|
|
|
|
self.emit('listening')
|
2014-08-17 02:05:56 +00:00
|
|
|
|
}
|
2014-06-07 22:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-13 22:28:23 +00:00
|
|
|
|
Server.prototype._onError = function (err) {
|
|
|
|
|
var self = this
|
|
|
|
|
self.emit('error', err)
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-01 22:42:13 +00:00
|
|
|
|
Server.prototype.listen = function (/* port, hostname, onlistening */) {
|
2014-06-07 22:10:12 +00:00
|
|
|
|
var self = this
|
2015-04-01 22:42:13 +00:00
|
|
|
|
|
2015-06-10 05:32:04 +00:00
|
|
|
|
if (self._listenCalled || self.listening) throw new Error('server already listening')
|
|
|
|
|
self._listenCalled = true
|
|
|
|
|
|
2015-04-01 22:42:13 +00:00
|
|
|
|
var lastArg = arguments[arguments.length - 1]
|
|
|
|
|
if (typeof lastArg === 'function') self.once('listening', lastArg)
|
|
|
|
|
|
|
|
|
|
var port = toNumber(arguments[0]) || arguments[0] || 0
|
|
|
|
|
var hostname = typeof arguments[1] !== 'function' ? arguments[1] : undefined
|
|
|
|
|
|
2015-12-15 23:19:44 +00:00
|
|
|
|
debug('listen (port: %o hostname: %o)', port, hostname)
|
2014-08-17 02:05:56 +00:00
|
|
|
|
|
2015-05-04 04:00:38 +00:00
|
|
|
|
function isObject (obj) {
|
|
|
|
|
return typeof obj === 'object' && obj !== null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var httpPort = isObject(port) ? (port.http || 0) : port
|
|
|
|
|
var udpPort = isObject(port) ? (port.udp || 0) : port
|
|
|
|
|
|
|
|
|
|
// binding to :: only receives IPv4 connections if the bindv6only sysctl is set 0,
|
|
|
|
|
// which is the default on many operating systems
|
2016-02-23 20:51:28 +00:00
|
|
|
|
var httpHostname = isObject(hostname) ? hostname.http : hostname
|
2015-05-04 04:00:38 +00:00
|
|
|
|
var udp4Hostname = isObject(hostname) ? hostname.udp : hostname
|
|
|
|
|
var udp6Hostname = isObject(hostname) ? hostname.udp6 : hostname
|
|
|
|
|
|
|
|
|
|
if (self.http) self.http.listen(httpPort, httpHostname)
|
|
|
|
|
if (self.udp4) self.udp4.bind(udpPort, udp4Hostname)
|
|
|
|
|
if (self.udp6) self.udp6.bind(udpPort, udp6Hostname)
|
2014-06-07 22:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Server.prototype.close = function (cb) {
|
|
|
|
|
var self = this
|
2016-03-19 05:35:01 +00:00
|
|
|
|
if (!cb) cb = noop
|
2015-03-27 03:19:06 +00:00
|
|
|
|
debug('close')
|
|
|
|
|
|
2015-01-29 22:59:08 +00:00
|
|
|
|
self.listening = false
|
2015-06-10 05:25:25 +00:00
|
|
|
|
self.destroyed = true
|
2015-03-27 03:19:06 +00:00
|
|
|
|
|
2015-05-02 19:15:14 +00:00
|
|
|
|
if (self.udp4) {
|
2015-03-27 03:19:06 +00:00
|
|
|
|
try {
|
2015-05-02 19:15:14 +00:00
|
|
|
|
self.udp4.close()
|
|
|
|
|
} catch (err) {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (self.udp6) {
|
|
|
|
|
try {
|
|
|
|
|
self.udp6.close()
|
2015-03-27 03:19:06 +00:00
|
|
|
|
} catch (err) {}
|
2014-06-07 22:10:12 +00:00
|
|
|
|
}
|
2015-03-27 03:19:06 +00:00
|
|
|
|
|
|
|
|
|
if (self.ws) {
|
|
|
|
|
try {
|
|
|
|
|
self.ws.close()
|
|
|
|
|
} catch (err) {}
|
2014-06-07 22:10:12 +00:00
|
|
|
|
}
|
2015-03-27 03:19:06 +00:00
|
|
|
|
|
|
|
|
|
if (self.http) self.http.close(cb)
|
|
|
|
|
else cb(null)
|
2014-06-07 22:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-03 18:50:23 +00:00
|
|
|
|
Server.prototype.createSwarm = function (infoHash, cb) {
|
2014-07-11 05:05:56 +00:00
|
|
|
|
var self = this
|
2015-02-19 20:56:09 +00:00
|
|
|
|
if (Buffer.isBuffer(infoHash)) infoHash = infoHash.toString('hex')
|
2016-01-03 18:50:23 +00:00
|
|
|
|
|
|
|
|
|
process.nextTick(function () {
|
|
|
|
|
var swarm = self.torrents[infoHash] = new Swarm(infoHash, self)
|
|
|
|
|
cb(null, swarm)
|
|
|
|
|
})
|
2014-06-07 22:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-03 18:50:23 +00:00
|
|
|
|
Server.prototype.getSwarm = function (infoHash, cb) {
|
2015-07-05 00:13:07 +00:00
|
|
|
|
var self = this
|
|
|
|
|
if (Buffer.isBuffer(infoHash)) infoHash = infoHash.toString('hex')
|
2016-01-03 18:50:23 +00:00
|
|
|
|
|
|
|
|
|
process.nextTick(function () {
|
|
|
|
|
cb(null, self.torrents[infoHash])
|
|
|
|
|
})
|
2015-07-05 00:13:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-27 03:19:06 +00:00
|
|
|
|
Server.prototype.onHttpRequest = function (req, res, opts) {
|
2014-06-07 22:10:12 +00:00
|
|
|
|
var self = this
|
2015-03-27 03:19:06 +00:00
|
|
|
|
if (!opts) opts = {}
|
|
|
|
|
opts.trustProxy = opts.trustProxy || self._trustProxy
|
2014-12-12 01:55:40 +00:00
|
|
|
|
|
2014-12-08 22:42:05 +00:00
|
|
|
|
var params
|
|
|
|
|
try {
|
2015-03-27 03:19:06 +00:00
|
|
|
|
params = parseHttpRequest(req, opts)
|
2015-02-04 19:34:46 +00:00
|
|
|
|
params.httpReq = req
|
|
|
|
|
params.httpRes = res
|
2014-12-08 22:42:05 +00:00
|
|
|
|
} catch (err) {
|
|
|
|
|
res.end(bencode.encode({
|
2014-12-12 01:55:40 +00:00
|
|
|
|
'failure reason': err.message
|
2014-12-08 22:42:05 +00:00
|
|
|
|
}))
|
2014-06-07 22:10:12 +00:00
|
|
|
|
|
2014-12-08 22:42:05 +00:00
|
|
|
|
// even though it's an error for the client, it's just a warning for the server.
|
|
|
|
|
// don't crash the server because a client sent bad data :)
|
2014-12-12 01:55:40 +00:00
|
|
|
|
self.emit('warning', err)
|
2014-12-08 22:42:05 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
2014-11-26 07:27:02 +00:00
|
|
|
|
|
2015-03-27 03:19:06 +00:00
|
|
|
|
self._onRequest(params, function (err, response) {
|
2014-12-09 01:35:05 +00:00
|
|
|
|
if (err) {
|
2014-12-11 15:24:33 +00:00
|
|
|
|
self.emit('warning', err)
|
2014-12-09 01:35:05 +00:00
|
|
|
|
response = {
|
|
|
|
|
'failure reason': err.message
|
2014-11-26 07:27:02 +00:00
|
|
|
|
}
|
2014-06-07 22:10:12 +00:00
|
|
|
|
}
|
2015-08-23 22:11:21 +00:00
|
|
|
|
if (self.destroyed) return res.end()
|
2014-06-07 22:10:12 +00:00
|
|
|
|
|
2014-12-09 22:18:20 +00:00
|
|
|
|
delete response.action // only needed for UDP encoding
|
2014-06-12 00:57:19 +00:00
|
|
|
|
res.end(bencode.encode(response))
|
2015-01-29 22:59:08 +00:00
|
|
|
|
|
|
|
|
|
if (params.action === common.ACTIONS.ANNOUNCE) {
|
2015-06-24 08:48:59 +00:00
|
|
|
|
self.emit(common.EVENT_NAMES[params.event], params.addr, params)
|
2015-01-29 22:59:08 +00:00
|
|
|
|
}
|
2014-12-09 01:35:05 +00:00
|
|
|
|
})
|
2014-06-07 22:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-13 23:11:59 +00:00
|
|
|
|
Server.prototype.onUdpRequest = function (msg, rinfo) {
|
2014-06-07 22:10:12 +00:00
|
|
|
|
var self = this
|
|
|
|
|
|
2014-12-08 23:22:36 +00:00
|
|
|
|
var params
|
|
|
|
|
try {
|
|
|
|
|
params = parseUdpRequest(msg, rinfo)
|
|
|
|
|
} catch (err) {
|
2014-12-09 01:35:05 +00:00
|
|
|
|
self.emit('warning', err)
|
|
|
|
|
// Do not reply for parsing errors
|
|
|
|
|
return
|
2014-06-07 22:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-27 03:19:06 +00:00
|
|
|
|
self._onRequest(params, function (err, response) {
|
2014-12-09 01:35:05 +00:00
|
|
|
|
if (err) {
|
2014-12-11 15:24:33 +00:00
|
|
|
|
self.emit('warning', err)
|
2014-12-09 01:35:05 +00:00
|
|
|
|
response = {
|
2015-01-29 22:59:08 +00:00
|
|
|
|
action: common.ACTIONS.ERROR,
|
2014-12-09 01:35:05 +00:00
|
|
|
|
'failure reason': err.message
|
2014-11-26 07:27:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-10 05:25:25 +00:00
|
|
|
|
if (self.destroyed) return
|
2014-12-11 20:53:39 +00:00
|
|
|
|
|
2014-12-09 01:35:05 +00:00
|
|
|
|
response.transactionId = params.transactionId
|
|
|
|
|
response.connectionId = params.connectionId
|
2015-01-29 22:59:08 +00:00
|
|
|
|
|
2014-12-09 01:35:05 +00:00
|
|
|
|
var buf = makeUdpPacket(response)
|
2015-05-01 21:20:53 +00:00
|
|
|
|
|
|
|
|
|
try {
|
2015-05-02 19:15:14 +00:00
|
|
|
|
var udp = (rinfo.family === 'IPv4') ? self.udp4 : self.udp6
|
|
|
|
|
udp.send(buf, 0, buf.length, rinfo.port, rinfo.address)
|
2015-05-01 21:20:53 +00:00
|
|
|
|
} catch (err) {
|
|
|
|
|
self.emit('warning', err)
|
|
|
|
|
}
|
2015-01-29 22:59:08 +00:00
|
|
|
|
|
|
|
|
|
if (params.action === common.ACTIONS.ANNOUNCE) {
|
2015-06-24 08:48:59 +00:00
|
|
|
|
self.emit(common.EVENT_NAMES[params.event], params.addr, params)
|
2015-01-29 22:59:08 +00:00
|
|
|
|
}
|
2014-12-09 01:35:05 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
2014-11-26 07:27:02 +00:00
|
|
|
|
|
2016-03-03 10:10:54 +00:00
|
|
|
|
Server.prototype.onWebSocketConnection = function (socket, opts) {
|
2015-03-27 03:19:06 +00:00
|
|
|
|
var self = this
|
2016-03-03 10:10:54 +00:00
|
|
|
|
if (!opts) opts = {}
|
|
|
|
|
opts.trustProxy = opts.trustProxy || self._trustProxy
|
2016-03-19 05:35:01 +00:00
|
|
|
|
|
2015-03-29 08:08:26 +00:00
|
|
|
|
socket.peerId = null // as hex
|
2016-03-18 22:02:12 +00:00
|
|
|
|
socket.infoHashes = [] // swarms that this socket is participating in
|
2016-03-29 06:38:23 +00:00
|
|
|
|
socket.onSend = function (err) {
|
|
|
|
|
self._onWebSocketSend(socket, err)
|
|
|
|
|
}
|
2016-03-18 22:06:05 +00:00
|
|
|
|
|
2016-03-29 06:38:23 +00:00
|
|
|
|
socket.onMessageBound = function (params) {
|
|
|
|
|
self._onWebSocketRequest(socket, opts, params)
|
|
|
|
|
}
|
2016-03-18 22:55:07 +00:00
|
|
|
|
socket.on('message', socket.onMessageBound)
|
2016-03-18 22:06:05 +00:00
|
|
|
|
|
2016-03-29 06:38:23 +00:00
|
|
|
|
socket.onErrorBound = function (err) {
|
|
|
|
|
self._onWebSocketError(socket, err)
|
|
|
|
|
}
|
2016-03-18 22:55:07 +00:00
|
|
|
|
socket.on('error', socket.onErrorBound)
|
2016-03-18 22:06:05 +00:00
|
|
|
|
|
2016-03-29 06:38:23 +00:00
|
|
|
|
socket.onCloseBound = function () {
|
|
|
|
|
self._onWebSocketClose(socket)
|
|
|
|
|
}
|
2016-03-18 22:55:07 +00:00
|
|
|
|
socket.on('close', socket.onCloseBound)
|
2015-03-27 03:19:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-03 10:10:54 +00:00
|
|
|
|
Server.prototype._onWebSocketRequest = function (socket, opts, params) {
|
2015-03-27 03:19:06 +00:00
|
|
|
|
var self = this
|
|
|
|
|
|
|
|
|
|
try {
|
2016-03-03 10:10:54 +00:00
|
|
|
|
params = parseWebSocketRequest(socket, opts, params)
|
2015-03-27 03:19:06 +00:00
|
|
|
|
} catch (err) {
|
|
|
|
|
socket.send(JSON.stringify({
|
2016-03-01 18:22:31 +00:00
|
|
|
|
'failure reason': err.message
|
2015-03-27 03:19:06 +00:00
|
|
|
|
}), socket.onSend)
|
|
|
|
|
|
|
|
|
|
// even though it's an error for the client, it's just a warning for the server.
|
|
|
|
|
// don't crash the server because a client sent bad data :)
|
|
|
|
|
self.emit('warning', err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-29 08:08:26 +00:00
|
|
|
|
if (!socket.peerId) socket.peerId = params.peer_id // as hex
|
2015-03-27 03:19:06 +00:00
|
|
|
|
|
|
|
|
|
self._onRequest(params, function (err, response) {
|
2017-02-21 09:50:30 +00:00
|
|
|
|
if (self.destroyed || socket.destroyed) return
|
2015-03-27 03:19:06 +00:00
|
|
|
|
if (err) {
|
2016-03-01 01:35:04 +00:00
|
|
|
|
socket.send(JSON.stringify({
|
2016-03-16 03:06:39 +00:00
|
|
|
|
action: params.action === common.ACTIONS.ANNOUNCE ? 'announce' : 'scrape',
|
2016-03-01 01:35:04 +00:00
|
|
|
|
'failure reason': err.message,
|
|
|
|
|
info_hash: common.hexToBinary(params.info_hash)
|
|
|
|
|
}), socket.onSend)
|
|
|
|
|
|
2015-03-27 03:19:06 +00:00
|
|
|
|
self.emit('warning', err)
|
2016-03-01 01:35:04 +00:00
|
|
|
|
return
|
2015-03-27 03:19:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-16 03:06:39 +00:00
|
|
|
|
response.action = params.action === common.ACTIONS.ANNOUNCE ? 'announce' : 'scrape'
|
2015-03-27 03:19:06 +00:00
|
|
|
|
|
2016-03-16 03:06:39 +00:00
|
|
|
|
var peers
|
|
|
|
|
if (response.action === 'announce') {
|
|
|
|
|
peers = response.peers
|
|
|
|
|
delete response.peers
|
2015-12-05 08:18:52 +00:00
|
|
|
|
|
2016-03-18 22:57:18 +00:00
|
|
|
|
if (socket.infoHashes.indexOf(params.info_hash) === -1) {
|
|
|
|
|
socket.infoHashes.push(params.info_hash)
|
|
|
|
|
}
|
2016-03-18 22:02:12 +00:00
|
|
|
|
|
2016-03-16 03:06:39 +00:00
|
|
|
|
response.info_hash = common.hexToBinary(params.info_hash)
|
2015-12-05 08:18:52 +00:00
|
|
|
|
|
2016-03-16 03:06:39 +00:00
|
|
|
|
// WebSocket tracker should have a shorter interval – default: 2 minutes
|
|
|
|
|
response.interval = Math.ceil(self.intervalMs / 1000 / 5)
|
|
|
|
|
}
|
2015-03-27 03:19:06 +00:00
|
|
|
|
|
2017-01-30 00:25:16 +00:00
|
|
|
|
// Skip sending update back for 'answer' announce messages – not needed
|
|
|
|
|
if (!params.answer) {
|
|
|
|
|
socket.send(JSON.stringify(response), socket.onSend)
|
|
|
|
|
debug('sent response %s to %s', JSON.stringify(response), params.peer_id)
|
|
|
|
|
}
|
2015-03-27 03:19:06 +00:00
|
|
|
|
|
2016-03-19 05:49:13 +00:00
|
|
|
|
if (Array.isArray(params.offers)) {
|
|
|
|
|
debug('got %s offers from %s', params.offers.length, params.peer_id)
|
2015-03-27 03:19:06 +00:00
|
|
|
|
debug('got %s peers from swarm %s', peers.length, params.info_hash)
|
|
|
|
|
peers.forEach(function (peer, i) {
|
|
|
|
|
peer.socket.send(JSON.stringify({
|
2016-03-24 05:04:07 +00:00
|
|
|
|
action: 'announce',
|
2015-03-27 03:19:06 +00:00
|
|
|
|
offer: params.offers[i].offer,
|
|
|
|
|
offer_id: params.offers[i].offer_id,
|
2015-03-29 08:08:26 +00:00
|
|
|
|
peer_id: common.hexToBinary(params.peer_id),
|
|
|
|
|
info_hash: common.hexToBinary(params.info_hash)
|
2015-07-22 23:30:52 +00:00
|
|
|
|
}), peer.socket.onSend)
|
2015-03-27 03:19:06 +00:00
|
|
|
|
debug('sent offer to %s from %s', peer.peerId, params.peer_id)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (params.answer) {
|
|
|
|
|
debug('got answer %s from %s', JSON.stringify(params.answer), params.peer_id)
|
|
|
|
|
|
2016-01-03 18:50:23 +00:00
|
|
|
|
self.getSwarm(params.info_hash, function (err, swarm) {
|
2017-02-21 09:51:08 +00:00
|
|
|
|
if (self.destroyed) return
|
2016-01-03 18:50:23 +00:00
|
|
|
|
if (err) return self.emit('warning', err)
|
|
|
|
|
if (!swarm) {
|
|
|
|
|
return self.emit('warning', new Error('no swarm with that `info_hash`'))
|
|
|
|
|
}
|
2016-06-08 21:57:01 +00:00
|
|
|
|
// Mark the destination peer as recently used in cache
|
|
|
|
|
var toPeer = swarm.peers.get(params.to_peer_id)
|
2016-01-03 18:50:23 +00:00
|
|
|
|
if (!toPeer) {
|
|
|
|
|
return self.emit('warning', new Error('no peer with that `to_peer_id`'))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toPeer.socket.send(JSON.stringify({
|
2016-03-24 05:04:07 +00:00
|
|
|
|
action: 'announce',
|
2016-01-03 18:50:23 +00:00
|
|
|
|
answer: params.answer,
|
|
|
|
|
offer_id: params.offer_id,
|
|
|
|
|
peer_id: common.hexToBinary(params.peer_id),
|
|
|
|
|
info_hash: common.hexToBinary(params.info_hash)
|
|
|
|
|
}), toPeer.socket.onSend)
|
|
|
|
|
debug('sent answer to %s from %s', toPeer.peerId, params.peer_id)
|
2015-03-27 03:19:06 +00:00
|
|
|
|
|
2016-01-03 18:50:23 +00:00
|
|
|
|
done()
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
done()
|
2015-03-27 03:19:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-03 18:50:23 +00:00
|
|
|
|
function done () {
|
|
|
|
|
// emit event once the announce is fully "processed"
|
|
|
|
|
if (params.action === common.ACTIONS.ANNOUNCE) {
|
|
|
|
|
self.emit(common.EVENT_NAMES[params.event], params.peer_id, params)
|
|
|
|
|
}
|
2015-03-27 03:19:06 +00:00
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-03 01:40:36 +00:00
|
|
|
|
Server.prototype._onWebSocketSend = function (socket, err) {
|
|
|
|
|
var self = this
|
|
|
|
|
if (err) self._onWebSocketError(socket, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Server.prototype._onWebSocketClose = function (socket) {
|
|
|
|
|
var self = this
|
|
|
|
|
debug('websocket close %s', socket.peerId)
|
2017-02-21 09:50:30 +00:00
|
|
|
|
socket.destroyed = true
|
2017-02-03 01:40:36 +00:00
|
|
|
|
|
|
|
|
|
if (socket.peerId) {
|
2017-02-03 04:51:07 +00:00
|
|
|
|
socket.infoHashes.slice(0).forEach(function (infoHash) {
|
2017-02-03 01:40:36 +00:00
|
|
|
|
var swarm = self.torrents[infoHash]
|
|
|
|
|
if (swarm) {
|
|
|
|
|
swarm.announce({
|
|
|
|
|
type: 'ws',
|
|
|
|
|
event: 'stopped',
|
|
|
|
|
numwant: 0,
|
|
|
|
|
peer_id: socket.peerId
|
|
|
|
|
}, noop)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ignore all future errors
|
|
|
|
|
socket.onSend = noop
|
|
|
|
|
socket.on('error', noop)
|
|
|
|
|
|
|
|
|
|
socket.peerId = null
|
|
|
|
|
socket.infoHashes = null
|
|
|
|
|
|
|
|
|
|
if (typeof socket.onMessageBound === 'function') {
|
|
|
|
|
socket.removeListener('message', socket.onMessageBound)
|
|
|
|
|
}
|
|
|
|
|
socket.onMessageBound = null
|
|
|
|
|
|
|
|
|
|
if (typeof socket.onErrorBound === 'function') {
|
|
|
|
|
socket.removeListener('error', socket.onErrorBound)
|
|
|
|
|
}
|
|
|
|
|
socket.onErrorBound = null
|
|
|
|
|
|
|
|
|
|
if (typeof socket.onCloseBound === 'function') {
|
|
|
|
|
socket.removeListener('close', socket.onCloseBound)
|
|
|
|
|
}
|
|
|
|
|
socket.onCloseBound = null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Server.prototype._onWebSocketError = function (socket, err) {
|
|
|
|
|
var self = this
|
|
|
|
|
debug('websocket error %s', err.message || err)
|
|
|
|
|
self.emit('warning', err)
|
|
|
|
|
self._onWebSocketClose(socket)
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-09 01:35:05 +00:00
|
|
|
|
Server.prototype._onRequest = function (params, cb) {
|
2015-03-27 03:19:06 +00:00
|
|
|
|
var self = this
|
2014-12-09 01:35:05 +00:00
|
|
|
|
if (params && params.action === common.ACTIONS.CONNECT) {
|
|
|
|
|
cb(null, { action: common.ACTIONS.CONNECT })
|
|
|
|
|
} else if (params && params.action === common.ACTIONS.ANNOUNCE) {
|
2015-03-27 03:19:06 +00:00
|
|
|
|
self._onAnnounce(params, cb)
|
2014-12-09 01:35:05 +00:00
|
|
|
|
} else if (params && params.action === common.ACTIONS.SCRAPE) {
|
2015-03-27 03:19:06 +00:00
|
|
|
|
self._onScrape(params, cb)
|
2014-12-09 01:35:05 +00:00
|
|
|
|
} else {
|
|
|
|
|
cb(new Error('Invalid action'))
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-11-26 07:27:02 +00:00
|
|
|
|
|
2014-12-09 01:35:05 +00:00
|
|
|
|
Server.prototype._onAnnounce = function (params, cb) {
|
|
|
|
|
var self = this
|
2015-03-27 03:19:06 +00:00
|
|
|
|
|
2016-01-03 18:50:23 +00:00
|
|
|
|
self.getSwarm(params.info_hash, function (err, swarm) {
|
|
|
|
|
if (err) return cb(err)
|
|
|
|
|
if (swarm) {
|
|
|
|
|
announce(swarm)
|
|
|
|
|
} else {
|
|
|
|
|
createSwarm()
|
|
|
|
|
}
|
|
|
|
|
})
|
2015-07-05 00:13:07 +00:00
|
|
|
|
|
|
|
|
|
function createSwarm () {
|
|
|
|
|
if (self._filter) {
|
2017-03-02 06:54:40 +00:00
|
|
|
|
self._filter(params.info_hash, params, function (err) {
|
|
|
|
|
// Precense of err means that this info_hash is disallowed
|
|
|
|
|
if (err) {
|
|
|
|
|
cb(err)
|
2015-07-08 17:13:52 +00:00
|
|
|
|
} else {
|
2016-01-03 18:50:23 +00:00
|
|
|
|
self.createSwarm(params.info_hash, function (err, swarm) {
|
|
|
|
|
if (err) return cb(err)
|
|
|
|
|
announce(swarm)
|
|
|
|
|
})
|
2015-03-27 03:19:06 +00:00
|
|
|
|
}
|
|
|
|
|
})
|
2015-07-05 00:13:07 +00:00
|
|
|
|
} else {
|
2016-01-03 18:50:23 +00:00
|
|
|
|
self.createSwarm(params.info_hash, function (err, swarm) {
|
|
|
|
|
if (err) return cb(err)
|
|
|
|
|
announce(swarm)
|
|
|
|
|
})
|
2015-07-05 00:13:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-03 18:50:23 +00:00
|
|
|
|
function announce (swarm) {
|
2015-07-05 00:13:07 +00:00
|
|
|
|
if (!params.event || params.event === 'empty') params.event = 'update'
|
|
|
|
|
swarm.announce(params, function (err, response) {
|
|
|
|
|
if (err) return cb(err)
|
|
|
|
|
|
|
|
|
|
if (!response.action) response.action = common.ACTIONS.ANNOUNCE
|
2015-07-29 08:47:09 +00:00
|
|
|
|
if (!response.interval) response.interval = Math.ceil(self.intervalMs / 1000)
|
2015-07-05 00:13:07 +00:00
|
|
|
|
|
|
|
|
|
if (params.compact === 1) {
|
|
|
|
|
var peers = response.peers
|
|
|
|
|
|
|
|
|
|
// Find IPv4 peers
|
|
|
|
|
response.peers = string2compact(peers.filter(function (peer) {
|
|
|
|
|
return common.IPV4_RE.test(peer.ip)
|
|
|
|
|
}).map(function (peer) {
|
|
|
|
|
return peer.ip + ':' + peer.port
|
|
|
|
|
}))
|
|
|
|
|
// Find IPv6 peers
|
|
|
|
|
response.peers6 = string2compact(peers.filter(function (peer) {
|
|
|
|
|
return common.IPV6_RE.test(peer.ip)
|
|
|
|
|
}).map(function (peer) {
|
|
|
|
|
return '[' + peer.ip + ']:' + peer.port
|
|
|
|
|
}))
|
|
|
|
|
} else if (params.compact === 0) {
|
|
|
|
|
// IPv6 peers are not separate for non-compact responses
|
|
|
|
|
response.peers = response.peers.map(function (peer) {
|
|
|
|
|
return {
|
2016-01-02 22:36:10 +00:00
|
|
|
|
'peer id': common.hexToBinary(peer.peerId),
|
2015-07-05 00:13:07 +00:00
|
|
|
|
ip: peer.ip,
|
|
|
|
|
port: peer.port
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
} // else, return full peer objects (used for websocket responses)
|
|
|
|
|
|
2015-07-08 17:13:52 +00:00
|
|
|
|
cb(null, response)
|
2015-07-05 00:13:07 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
2014-12-09 01:35:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Server.prototype._onScrape = function (params, cb) {
|
|
|
|
|
var self = this
|
2014-12-11 20:53:39 +00:00
|
|
|
|
|
2014-12-12 10:02:11 +00:00
|
|
|
|
if (params.info_hash == null) {
|
2014-12-09 01:35:05 +00:00
|
|
|
|
// if info_hash param is omitted, stats for all torrents are returned
|
|
|
|
|
// TODO: make this configurable!
|
|
|
|
|
params.info_hash = Object.keys(self.torrents)
|
2014-06-07 22:10:12 +00:00
|
|
|
|
}
|
2014-12-11 20:53:39 +00:00
|
|
|
|
|
2014-12-10 15:47:41 +00:00
|
|
|
|
series(params.info_hash.map(function (infoHash) {
|
|
|
|
|
return function (cb) {
|
2016-01-03 18:50:23 +00:00
|
|
|
|
self.getSwarm(infoHash, function (err, swarm) {
|
|
|
|
|
if (err) return cb(err)
|
|
|
|
|
if (swarm) {
|
|
|
|
|
swarm.scrape(params, function (err, scrapeInfo) {
|
|
|
|
|
if (err) return cb(err)
|
|
|
|
|
cb(null, {
|
|
|
|
|
infoHash: infoHash,
|
|
|
|
|
complete: (scrapeInfo && scrapeInfo.complete) || 0,
|
|
|
|
|
incomplete: (scrapeInfo && scrapeInfo.incomplete) || 0
|
|
|
|
|
})
|
2015-07-05 00:13:07 +00:00
|
|
|
|
})
|
2016-01-03 18:50:23 +00:00
|
|
|
|
} else {
|
|
|
|
|
cb(null, { infoHash: infoHash, complete: 0, incomplete: 0 })
|
|
|
|
|
}
|
|
|
|
|
})
|
2014-12-09 01:35:05 +00:00
|
|
|
|
}
|
2014-12-10 15:47:41 +00:00
|
|
|
|
}), function (err, results) {
|
|
|
|
|
if (err) return cb(err)
|
|
|
|
|
|
2014-12-12 10:02:11 +00:00
|
|
|
|
var response = {
|
|
|
|
|
action: common.ACTIONS.SCRAPE,
|
|
|
|
|
files: {},
|
2015-07-29 08:47:09 +00:00
|
|
|
|
flags: { min_request_interval: Math.ceil(self.intervalMs / 1000) }
|
2014-12-12 10:02:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-10 15:47:41 +00:00
|
|
|
|
results.forEach(function (result) {
|
2014-12-12 10:02:11 +00:00
|
|
|
|
response.files[common.hexToBinary(result.infoHash)] = {
|
2015-07-05 00:13:07 +00:00
|
|
|
|
complete: result.complete || 0,
|
|
|
|
|
incomplete: result.incomplete || 0,
|
|
|
|
|
downloaded: result.complete || 0 // TODO: this only provides a lower-bound
|
2014-12-10 15:47:41 +00:00
|
|
|
|
}
|
2014-06-07 22:10:12 +00:00
|
|
|
|
})
|
|
|
|
|
|
2014-12-10 15:47:41 +00:00
|
|
|
|
cb(null, response)
|
|
|
|
|
})
|
2014-06-07 22:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-09 01:35:05 +00:00
|
|
|
|
function makeUdpPacket (params) {
|
2015-01-27 02:16:01 +00:00
|
|
|
|
var packet
|
2014-12-09 01:35:05 +00:00
|
|
|
|
switch (params.action) {
|
|
|
|
|
case common.ACTIONS.CONNECT:
|
2015-01-27 02:16:01 +00:00
|
|
|
|
packet = Buffer.concat([
|
2014-12-09 01:35:05 +00:00
|
|
|
|
common.toUInt32(common.ACTIONS.CONNECT),
|
|
|
|
|
common.toUInt32(params.transactionId),
|
|
|
|
|
params.connectionId
|
|
|
|
|
])
|
2015-01-27 02:16:01 +00:00
|
|
|
|
break
|
2014-12-09 01:35:05 +00:00
|
|
|
|
case common.ACTIONS.ANNOUNCE:
|
2015-01-27 02:16:01 +00:00
|
|
|
|
packet = Buffer.concat([
|
2014-12-09 01:35:05 +00:00
|
|
|
|
common.toUInt32(common.ACTIONS.ANNOUNCE),
|
|
|
|
|
common.toUInt32(params.transactionId),
|
2015-01-05 18:55:58 +00:00
|
|
|
|
common.toUInt32(params.interval),
|
2014-12-09 01:35:05 +00:00
|
|
|
|
common.toUInt32(params.incomplete),
|
|
|
|
|
common.toUInt32(params.complete),
|
|
|
|
|
params.peers
|
|
|
|
|
])
|
2015-01-27 02:16:01 +00:00
|
|
|
|
break
|
2014-12-09 01:35:05 +00:00
|
|
|
|
case common.ACTIONS.SCRAPE:
|
2015-05-02 00:36:07 +00:00
|
|
|
|
var scrapeResponse = [
|
2014-12-09 01:35:05 +00:00
|
|
|
|
common.toUInt32(common.ACTIONS.SCRAPE),
|
2015-05-02 00:36:07 +00:00
|
|
|
|
common.toUInt32(params.transactionId)
|
|
|
|
|
]
|
|
|
|
|
for (var infoHash in params.files) {
|
|
|
|
|
var file = params.files[infoHash]
|
|
|
|
|
scrapeResponse.push(
|
|
|
|
|
common.toUInt32(file.complete),
|
|
|
|
|
common.toUInt32(file.downloaded), // TODO: this only provides a lower-bound
|
|
|
|
|
common.toUInt32(file.incomplete)
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
packet = Buffer.concat(scrapeResponse)
|
2015-01-27 02:16:01 +00:00
|
|
|
|
break
|
2014-12-09 01:35:05 +00:00
|
|
|
|
case common.ACTIONS.ERROR:
|
2015-01-27 02:16:01 +00:00
|
|
|
|
packet = Buffer.concat([
|
2014-12-09 01:35:05 +00:00
|
|
|
|
common.toUInt32(common.ACTIONS.ERROR),
|
|
|
|
|
common.toUInt32(params.transactionId || 0),
|
2016-05-30 06:12:23 +00:00
|
|
|
|
Buffer.from(String(params['failure reason']))
|
2014-12-09 01:35:05 +00:00
|
|
|
|
])
|
2015-01-27 02:16:01 +00:00
|
|
|
|
break
|
|
|
|
|
default:
|
|
|
|
|
throw new Error('Action not implemented: ' + params.action)
|
2015-01-29 02:18:47 +00:00
|
|
|
|
}
|
2015-01-27 02:16:01 +00:00
|
|
|
|
return packet
|
2014-12-09 01:35:05 +00:00
|
|
|
|
}
|
2015-03-27 03:19:06 +00:00
|
|
|
|
|
2015-05-01 21:17:11 +00:00
|
|
|
|
function toNumber (x) {
|
|
|
|
|
x = Number(x)
|
|
|
|
|
return x >= 0 ? x : false
|
|
|
|
|
}
|
2016-03-19 05:35:01 +00:00
|
|
|
|
|
|
|
|
|
function noop () {}
|