mirror of
https://github.com/webtorrent/bittorrent-tracker.git
synced 2025-01-19 04:31:36 +00:00
parent
556f672028
commit
ebb86f728f
@ -10,8 +10,6 @@ var EventEmitter = require('events').EventEmitter
|
||||
var extend = require('extend.js')
|
||||
var get = require('simple-get')
|
||||
var hat = require('hat')
|
||||
var http = require('http')
|
||||
var https = require('https')
|
||||
var inherits = require('inherits')
|
||||
var once = require('once')
|
||||
var url = require('url')
|
||||
|
@ -32,8 +32,7 @@ function parseHttpRequest (req, options) {
|
||||
? req.headers['x-forwarded-for'] || req.connection.remoteAddress
|
||||
: req.connection.remoteAddress.replace(REMOVE_IPV4_MAPPED_IPV6_RE, '') // force ipv4
|
||||
params.addr = (common.IPV6_RE.test(params.ip) ? '[' + params.ip + ']' : params.ip) + ':' + params.port
|
||||
|
||||
} else if (s[0] === '/scrape') { // unofficial scrape message
|
||||
} else if (s[0] === '/scrape') {
|
||||
params.action = common.ACTIONS.SCRAPE
|
||||
if (typeof params.info_hash === 'string')
|
||||
params.info_hash = [ params.info_hash ]
|
||||
@ -45,7 +44,6 @@ function parseHttpRequest (req, options) {
|
||||
return common.binaryToHex(binaryInfoHash)
|
||||
})
|
||||
}
|
||||
|
||||
} else {
|
||||
throw new Error('Invalid action in HTTP request: ' + params.action)
|
||||
}
|
||||
|
@ -23,7 +23,6 @@ function parseUdpRequest (msg, rinfo) {
|
||||
|
||||
if (params.action === common.ACTIONS.CONNECT) {
|
||||
// No further params
|
||||
|
||||
} else if (params.action === common.ACTIONS.ANNOUNCE) {
|
||||
params.info_hash = msg.slice(16, 36).toString('hex') // 20 bytes
|
||||
params.peer_id = msg.slice(36, 56).toString('hex') // 20 bytes
|
||||
@ -51,13 +50,11 @@ function parseUdpRequest (msg, rinfo) {
|
||||
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
|
||||
// TODO: support multiple info_hash scrape
|
||||
if (msg.length > 36) throw new Error('multiple info_hash scrape not supported')
|
||||
|
||||
params.info_hash = [ msg.slice(16, 36).toString('hex') ] // 20 bytes
|
||||
|
||||
} else {
|
||||
throw new Error('Invalid action in UDP packet: ' + params.action)
|
||||
}
|
||||
|
@ -28,7 +28,6 @@ Swarm.prototype.announce = function (params, cb) {
|
||||
incomplete: self.incomplete,
|
||||
peers: self._getPeers(params.numwant)
|
||||
})
|
||||
|
||||
} else {
|
||||
cb(new Error('invalid event'))
|
||||
}
|
||||
|
@ -32,6 +32,7 @@
|
||||
"devDependencies": {
|
||||
"magnet-uri": "^4.0.0",
|
||||
"parse-torrent": "^3.0.1",
|
||||
"standard": "^1.0.0",
|
||||
"tape": "^3.0.3"
|
||||
},
|
||||
"homepage": "http://webtorrent.io",
|
||||
@ -52,6 +53,6 @@
|
||||
"url": "git://github.com/feross/bittorrent-tracker.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "tape test/*.js"
|
||||
"test": "standard && tape test/*.js"
|
||||
}
|
||||
}
|
||||
|
22
server.js
22
server.js
@ -18,7 +18,6 @@ var parseUdpRequest = require('./lib/parse_udp')
|
||||
// Use random port above 1024
|
||||
portfinder.basePort = Math.floor(Math.random() * 60000) + 1025
|
||||
|
||||
|
||||
inherits(Server, EventEmitter)
|
||||
|
||||
/**
|
||||
@ -183,11 +182,7 @@ Server.prototype.onUdpRequest = function (msg, rinfo) {
|
||||
response.transactionId = params.transactionId
|
||||
response.connectionId = params.connectionId
|
||||
var buf = makeUdpPacket(response)
|
||||
self._udpSocket.send(buf, 0, buf.length, rinfo.port, rinfo.address, function () {
|
||||
try {
|
||||
socket.close()
|
||||
} catch (err) {}
|
||||
})
|
||||
self._udpSocket.send(buf, 0, buf.length, rinfo.port, rinfo.address)
|
||||
})
|
||||
}
|
||||
|
||||
@ -274,15 +269,17 @@ Server.prototype._onScrape = function (params, cb) {
|
||||
}
|
||||
|
||||
function makeUdpPacket (params) {
|
||||
var packet
|
||||
switch (params.action) {
|
||||
case common.ACTIONS.CONNECT:
|
||||
return Buffer.concat([
|
||||
packet = Buffer.concat([
|
||||
common.toUInt32(common.ACTIONS.CONNECT),
|
||||
common.toUInt32(params.transactionId),
|
||||
params.connectionId
|
||||
])
|
||||
break
|
||||
case common.ACTIONS.ANNOUNCE:
|
||||
return Buffer.concat([
|
||||
packet = Buffer.concat([
|
||||
common.toUInt32(common.ACTIONS.ANNOUNCE),
|
||||
common.toUInt32(params.transactionId),
|
||||
common.toUInt32(params.interval),
|
||||
@ -290,6 +287,7 @@ function makeUdpPacket (params) {
|
||||
common.toUInt32(params.complete),
|
||||
params.peers
|
||||
])
|
||||
break
|
||||
case common.ACTIONS.SCRAPE:
|
||||
var firstInfoHash = Object.keys(params.files)[0]
|
||||
var scrapeInfo = firstInfoHash ? {
|
||||
@ -297,20 +295,24 @@ function makeUdpPacket (params) {
|
||||
incomplete: params.files[firstInfoHash].incomplete,
|
||||
completed: params.files[firstInfoHash].complete // TODO: this only provides a lower-bound
|
||||
} : {}
|
||||
return Buffer.concat([
|
||||
packet = Buffer.concat([
|
||||
common.toUInt32(common.ACTIONS.SCRAPE),
|
||||
common.toUInt32(params.transactionId),
|
||||
common.toUInt32(scrapeInfo.complete),
|
||||
common.toUInt32(scrapeInfo.completed),
|
||||
common.toUInt32(scrapeInfo.incomplete)
|
||||
])
|
||||
break
|
||||
case common.ACTIONS.ERROR:
|
||||
return Buffer.concat([
|
||||
packet = Buffer.concat([
|
||||
common.toUInt32(common.ACTIONS.ERROR),
|
||||
common.toUInt32(params.transactionId || 0),
|
||||
new Buffer(params.message, 'utf8')
|
||||
])
|
||||
break
|
||||
default:
|
||||
throw new Error('Action not implemented: ' + params.action)
|
||||
break
|
||||
}
|
||||
return packet
|
||||
}
|
||||
|
@ -53,7 +53,6 @@ test('large torrent: client.start()', function (t) {
|
||||
t.pass('server close')
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
})
|
||||
})
|
||||
|
@ -83,7 +83,6 @@ function testClientStop (t, serverType) {
|
||||
t.pass('server close')
|
||||
})
|
||||
})
|
||||
|
||||
}, 1000)
|
||||
})
|
||||
}
|
||||
@ -113,7 +112,6 @@ function testClientUpdate (t, serverType) {
|
||||
client.start()
|
||||
|
||||
client.once('update', function () {
|
||||
|
||||
client.once('update', function (data) {
|
||||
// received an update!
|
||||
t.equal(data.announce, announceUrl)
|
||||
|
@ -138,7 +138,6 @@ test('server: all info_hash scrape', function (t) {
|
||||
parsedBitlove.announce = [ announceUrl ]
|
||||
|
||||
server.once('listening', function () {
|
||||
|
||||
// announce a torrent to the tracker
|
||||
var client = new Client(peerId, port, parsedBitlove)
|
||||
client.on('error', function (err) {
|
||||
@ -147,7 +146,6 @@ test('server: all info_hash scrape', function (t) {
|
||||
client.start()
|
||||
|
||||
server.once('start', function () {
|
||||
|
||||
// now do a scrape of everything by omitting the info_hash param
|
||||
get.concat(scrapeUrl, function (err, data, res) {
|
||||
if (err) throw err
|
||||
|
Loading…
Reference in New Issue
Block a user