diff --git a/lib/common.js b/lib/common.js index cb83d5b..727e239 100644 --- a/lib/common.js +++ b/lib/common.js @@ -4,6 +4,9 @@ var querystring = require('querystring') +exports.NUM_ANNOUNCE_PEERS = 50 +exports.MAX_ANNOUNCE_PEERS = 82 + exports.CONNECTION_ID = Buffer.concat([ toUInt32(0x417), toUInt32(0x27101980) ]) exports.ACTIONS = { CONNECT: 0, ANNOUNCE: 1, SCRAPE: 2, ERROR: 3 } exports.EVENTS = { update: 0, completed: 1, started: 2, stopped: 3 } diff --git a/lib/parse_http.js b/lib/parse_http.js new file mode 100644 index 0000000..0f7a775 --- /dev/null +++ b/lib/parse_http.js @@ -0,0 +1,60 @@ +var common = require('./common') + +var REMOVE_IPV6_RE = /^::ffff:/ + +module.exports = parseHttpRequest + +function parseHttpRequest (req, options) { + var s = req.url.split('?') + var params = common.querystringParse(s[1]) + + if (s[0] === '/announce') { + params.action = common.ACTIONS.ANNOUNCE + + params.peer_id = typeof params.peer_id === 'string' && common.binaryToUtf8(params.peer_id) + params.port = Number(params.port) + + if (typeof params.info_hash !== 'string') throw new Error('invalid info_hash') + if (params.info_hash.length !== 20) throw new Error('invalid info_hash length') + if (typeof params.peer_id !== 'string') throw new Error('invalid peer_id') + if (params.peer_id.length !== 20) throw new Error('invalid peer_id length') + if (!params.port) throw new Error('invalid port') + + params.left = Number(params.left) + params.compact = Number(params.compact) + + params.ip = options.trustProxy + ? req.headers['x-forwarded-for'] || req.connection.remoteAddress + : req.connection.remoteAddress.replace(REMOVE_IPV6_RE, '') // force ipv4 + params.addr = params.ip + ':' + params.port // TODO: ipv6 brackets? + + params.numwant = Math.min( + Number(params.numwant) || common.NUM_ANNOUNCE_PEERS, + common.MAX_ANNOUNCE_PEERS + ) + + return params + } else if (s[0] === '/scrape') { // unofficial scrape message + params.action = common.ACTIONS.SCRAPE + + if (typeof params.info_hash === 'string') { + params.info_hash = [ params.info_hash ] + } + + if (params.info_hash) { + if (!Array.isArray(params.info_hash)) throw new Error('invalid info_hash array') + + params.info_hash = params.info_hash.map(function (infoHash) { + if (infoHash.length !== 20) { + throw new Error('invalid info_hash') + } + + return infoHash + }) + } + + return params + } else { + return null + } +} diff --git a/lib/parse_udp.js b/lib/parse_udp.js new file mode 100644 index 0000000..411d5b2 --- /dev/null +++ b/lib/parse_udp.js @@ -0,0 +1,79 @@ +var bufferEqual = require('buffer-equal') +var common = require('./common') + + +module.exports = parseUdpRequest + +function parseUdpRequest (msg, rinfo) { + if (msg.length < 16) { + throw new Error('received packet is too short') + } + + if (rinfo.family !== 'IPv4') { + throw new Error('udp tracker does not support IPv6') + } + + var params = { + connectionId: msg.slice(0, 8), // 64-bit + action: msg.readUInt32BE(8), + transactionId: msg.readUInt32BE(12) + } + + // TODO: randomize: + if (!bufferEqual(params.connectionId, common.CONNECTION_ID)) { + throw new Error('received packet with invalid connection id') + } + + if (params.action === common.ACTIONS.CONNECT) { + // No further params + } else if (params.action === common.ACTIONS.ANNOUNCE) { + params.info_hash = msg.slice(16, 36).toString('binary') // 20 bytes + params.peer_id = msg.slice(36, 56).toString('utf8') // 20 bytes + params.downloaded = fromUInt64(msg.slice(56, 64)) // TODO: track this? + params.left = fromUInt64(msg.slice(64, 72)) + params.uploaded = fromUInt64(msg.slice(72, 80)) // TODO: track this? + params.event = msg.readUInt32BE(80) + params.event = common.EVENT_IDS[params.event] + if (!params.event) throw new Error('invalid event') // early return + params.ip = msg.readUInt32BE(84) // optional + params.ip = params.ip ? + ipLib.toString(params.ip) : + params.ip = rinfo.address + params.key = msg.readUInt32BE(88) // TODO: what is this for? + params.numwant = msg.readUInt32BE(92) // optional + // never send more than MAX_ANNOUNCE_PEERS or else the UDP packet will get bigger than + // 512 bytes which is not safe + params.numwant = Math.min(params.numwant || common.NUM_ANNOUNCE_PEERS, common.MAX_ANNOUNCE_PEERS) + params.port = msg.readUInt16BE(96) || rinfo.port // optional + params.addr = params.ip + ':' + params.port // TODO: ipv6 brackets + params.compact = 1 // udp is always compact + + } else if (params.action === common.ACTIONS.SCRAPE) { // scrape message + params.info_hash = msg.slice(16, 36).toString('binary') // 20 bytes + + // TODO: support multiple info_hash scrape + if (msg.length > 36) { + throw new Error('multiple info_hash scrape not supported') + } + } else { + return null + } + + return params +} + +// HELPER FUNCTIONS + +var TWO_PWR_32 = (1 << 16) * 2 + +/** + * Return the closest floating-point representation to the buffer value. Precision will be + * lost for big numbers. + */ +function fromUInt64 (buf) { + var high = buf.readUInt32BE(0) | 0 // force + var low = buf.readUInt32BE(4) | 0 + var lowUnsigned = (low >= 0) ? low : TWO_PWR_32 + low + + return high * TWO_PWR_32 + lowUnsigned +} diff --git a/server.js b/server.js index 93df5af..d84de01 100644 --- a/server.js +++ b/server.js @@ -1,8 +1,6 @@ module.exports = Server -module.exports.parseHttpRequest = parseHttpRequest var bencode = require('bencode') -var bufferEqual = require('buffer-equal') var common = require('./lib/common') var debug = require('debug')('bittorrent-tracker') var dgram = require('dgram') @@ -13,12 +11,12 @@ var ipLib = require('ip') var portfinder = require('portfinder') var string2compact = require('string2compact') +var parseHttpRequest = require('./lib/parse_http') +var parseUdpRequest = require('./lib/parse_udp') + // Use random port above 1024 portfinder.basePort = Math.floor(Math.random() * 60000) + 1025 -var NUM_ANNOUNCE_PEERS = 50 -var MAX_ANNOUNCE_PEERS = 82 -var REMOVE_IPV6_RE = /^::ffff:/ inherits(Server, EventEmitter) @@ -369,119 +367,6 @@ Server.prototype._getPeersCompact = function (swarm, numwant) { } -function parseHttpRequest (req, options) { - var s = req.url.split('?') - var params = common.querystringParse(s[1]) - - if (s[0] === '/announce') { - params.action = common.ACTIONS.ANNOUNCE - - params.peer_id = typeof params.peer_id === 'string' && common.binaryToUtf8(params.peer_id) - params.port = Number(params.port) - - if (typeof params.info_hash !== 'string') throw new Error('invalid info_hash') - if (params.info_hash.length !== 20) throw new Error('invalid info_hash length') - if (typeof params.peer_id !== 'string') throw new Error('invalid peer_id') - if (params.peer_id.length !== 20) throw new Error('invalid peer_id length') - if (!params.port) throw new Error('invalid port') - - params.left = Number(params.left) - params.compact = Number(params.compact) - - params.ip = options.trustProxy - ? req.headers['x-forwarded-for'] || req.connection.remoteAddress - : req.connection.remoteAddress.replace(REMOVE_IPV6_RE, '') // force ipv4 - params.addr = params.ip + ':' + params.port // TODO: ipv6 brackets? - - params.numwant = Math.min( - Number(params.numwant) || NUM_ANNOUNCE_PEERS, - MAX_ANNOUNCE_PEERS - ) - - return params - } else if (s[0] === '/scrape') { // unofficial scrape message - params.action = common.ACTIONS.SCRAPE - - if (typeof params.info_hash === 'string') { - params.info_hash = [ params.info_hash ] - } - - if (params.info_hash) { - if (!Array.isArray(params.info_hash)) throw new Error('invalid info_hash array') - - params.info_hash = params.info_hash.map(function (infoHash) { - if (infoHash.length !== 20) { - throw new Error('invalid info_hash') - } - - return infoHash - }) - } - - return params - } else { - return null - } -} - -function parseUdpRequest (msg, rinfo) { - if (msg.length < 16) { - throw new Error('received packet is too short') - } - - if (rinfo.family !== 'IPv4') { - throw new Error('udp tracker does not support IPv6') - } - - var params = { - connectionId: msg.slice(0, 8), // 64-bit - action: msg.readUInt32BE(8), - transactionId: msg.readUInt32BE(12) - } - - // TODO: randomize: - if (!bufferEqual(params.connectionId, common.CONNECTION_ID)) { - throw new Error('received packet with invalid connection id') - } - - if (params.action === common.ACTIONS.CONNECT) { - // No further params - } else if (params.action === common.ACTIONS.ANNOUNCE) { - params.info_hash = msg.slice(16, 36).toString('binary') // 20 bytes - params.peer_id = msg.slice(36, 56).toString('utf8') // 20 bytes - params.downloaded = fromUInt64(msg.slice(56, 64)) // TODO: track this? - params.left = fromUInt64(msg.slice(64, 72)) - params.uploaded = fromUInt64(msg.slice(72, 80)) // TODO: track this? - params.event = msg.readUInt32BE(80) - params.event = common.EVENT_IDS[params.event] - if (!params.event) throw new Error('invalid event') // early return - params.ip = msg.readUInt32BE(84) // optional - params.ip = params.ip ? - ipLib.toString(params.ip) : - params.ip = rinfo.address - params.key = msg.readUInt32BE(88) // TODO: what is this for? - params.numwant = msg.readUInt32BE(92) // optional - // never send more than MAX_ANNOUNCE_PEERS or else the UDP packet will get bigger than - // 512 bytes which is not safe - params.numwant = Math.min(params.numwant || NUM_ANNOUNCE_PEERS, MAX_ANNOUNCE_PEERS) - params.port = msg.readUInt16BE(96) || rinfo.port // optional - params.addr = params.ip + ':' + params.port // TODO: ipv6 brackets - params.compact = 1 // udp is always compact - - } else if (params.action === common.ACTIONS.SCRAPE) { // scrape message - params.info_hash = msg.slice(16, 36).toString('binary') // 20 bytes - - // TODO: support multiple info_hash scrape - if (msg.length > 36) { - throw new Error('multiple info_hash scrape not supported') - } - } else { - return null - } - - return params -} - function makeUdpPacket (params) { switch (params.action) { case common.ACTIONS.CONNECT: @@ -523,19 +408,3 @@ function makeUdpPacket (params) { throw new Error('Action not implemented: ' + params.action) } } - -// HELPER FUNCTIONS - -var TWO_PWR_32 = (1 << 16) * 2 - -/** - * Return the closest floating-point representation to the buffer value. Precision will be - * lost for big numbers. - */ -function fromUInt64 (buf) { - var high = buf.readUInt32BE(0) | 0 // force - var low = buf.readUInt32BE(4) | 0 - var lowUnsigned = (low >= 0) ? low : TWO_PWR_32 + low - - return high * TWO_PWR_32 + lowUnsigned -}