mirror of
https://github.com/webtorrent/bittorrent-tracker.git
synced 2025-01-31 10:31:36 +00:00
Merge pull request #46 from feross/astro-fixes
swarm.announce() should always call callback
This commit is contained in:
commit
66b71db8f5
@ -1,16 +1,16 @@
|
|||||||
|
module.exports = parseHttpRequest
|
||||||
|
|
||||||
var common = require('./common')
|
var common = require('./common')
|
||||||
|
|
||||||
var REMOVE_IPV6_RE = /^::ffff:/
|
var REMOVE_IPV6_RE = /^::ffff:/
|
||||||
|
|
||||||
module.exports = parseHttpRequest
|
|
||||||
|
|
||||||
function parseHttpRequest (req, options) {
|
function parseHttpRequest (req, options) {
|
||||||
var s = req.url.split('?')
|
var s = req.url.split('?')
|
||||||
var params = common.querystringParse(s[1])
|
var params = common.querystringParse(s[1])
|
||||||
|
|
||||||
if (s[0] === '/announce') {
|
if (s[0] === '/announce') {
|
||||||
params.action = common.ACTIONS.ANNOUNCE
|
params.action = common.ACTIONS.ANNOUNCE
|
||||||
|
|
||||||
params.peer_id = typeof params.peer_id === 'string' && common.binaryToUtf8(params.peer_id)
|
params.peer_id = typeof params.peer_id === 'string' && common.binaryToUtf8(params.peer_id)
|
||||||
params.port = Number(params.port)
|
params.port = Number(params.port)
|
||||||
|
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
|
module.exports = parseUdpRequest
|
||||||
|
|
||||||
var bufferEqual = require('buffer-equal')
|
var bufferEqual = require('buffer-equal')
|
||||||
var ipLib = require('ip')
|
var ipLib = require('ip')
|
||||||
var common = require('./common')
|
var common = require('./common')
|
||||||
|
|
||||||
|
|
||||||
module.exports = parseUdpRequest
|
|
||||||
|
|
||||||
function parseUdpRequest (msg, rinfo) {
|
function parseUdpRequest (msg, rinfo) {
|
||||||
if (msg.length < 16) {
|
if (msg.length < 16) {
|
||||||
throw new Error('received packet is too short')
|
throw new Error('received packet is too short')
|
||||||
@ -20,7 +19,7 @@ function parseUdpRequest (msg, rinfo) {
|
|||||||
transactionId: msg.readUInt32BE(12)
|
transactionId: msg.readUInt32BE(12)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: randomize:
|
// TODO: randomize
|
||||||
if (!bufferEqual(params.connectionId, common.CONNECTION_ID)) {
|
if (!bufferEqual(params.connectionId, common.CONNECTION_ID)) {
|
||||||
throw new Error('received packet with invalid connection id')
|
throw new Error('received packet with invalid connection id')
|
||||||
}
|
}
|
||||||
|
49
lib/swarm.js
49
lib/swarm.js
@ -1,7 +1,7 @@
|
|||||||
var debug = require('debug')('bittorrent-tracker')
|
|
||||||
|
|
||||||
module.exports = Swarm
|
module.exports = Swarm
|
||||||
|
|
||||||
|
var debug = require('debug')('bittorrent-tracker')
|
||||||
|
|
||||||
// Regard this as the default implementation of an interface that you
|
// Regard this as the default implementation of an interface that you
|
||||||
// need to support when overriding Server.getSwarm()
|
// need to support when overriding Server.getSwarm()
|
||||||
function Swarm (infoHash, server) {
|
function Swarm (infoHash, server) {
|
||||||
@ -19,30 +19,27 @@ Swarm.prototype.announce = function (params, cb) {
|
|||||||
if (!params.event || params.event === 'empty') params.event = 'update'
|
if (!params.event || params.event === 'empty') params.event = 'update'
|
||||||
var fn = '_onAnnounce_' + params.event
|
var fn = '_onAnnounce_' + params.event
|
||||||
if (self[fn]) {
|
if (self[fn]) {
|
||||||
self[fn](params, peer, function (err) {
|
self[fn](params, peer) // process event
|
||||||
// event processed, prepare response:
|
|
||||||
|
|
||||||
if (params.left === 0 && peer) peer.complete = true
|
if (params.left === 0 && peer) peer.complete = true
|
||||||
|
|
||||||
// send peers
|
cb(null, {
|
||||||
var peers = self._getPeers(params.numwant)
|
complete: self.complete,
|
||||||
|
incomplete: self.incomplete,
|
||||||
cb(null, {
|
peers: self._getPeers(params.numwant)
|
||||||
complete: self.complete,
|
|
||||||
incomplete: self.incomplete,
|
|
||||||
peers: peers
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
cb(new Error('invalid event'))
|
cb(new Error('invalid event'))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Swarm.prototype._onAnnounce_started = function (params, peer, cb) {
|
Swarm.prototype._onAnnounce_started = function (params, peer) {
|
||||||
if (peer) {
|
if (peer) {
|
||||||
debug('unexpected `started` event from peer that is already in swarm')
|
debug('unexpected `started` event from peer that is already in swarm')
|
||||||
return this._onAnnounce_update() // treat as an update
|
return this._onAnnounce_update(params, peer) // treat as an update
|
||||||
}
|
}
|
||||||
|
|
||||||
if (params.left === 0) this.complete += 1
|
if (params.left === 0) this.complete += 1
|
||||||
else this.incomplete += 1
|
else this.incomplete += 1
|
||||||
peer = this.peers[params.addr] = {
|
peer = this.peers[params.addr] = {
|
||||||
@ -51,48 +48,42 @@ Swarm.prototype._onAnnounce_started = function (params, peer, cb) {
|
|||||||
peerId: params.peer_id
|
peerId: params.peer_id
|
||||||
}
|
}
|
||||||
this.emit('start', params.addr)
|
this.emit('start', params.addr)
|
||||||
|
|
||||||
cb()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Swarm.prototype._onAnnounce_stopped = function (params, peer, cb) {
|
Swarm.prototype._onAnnounce_stopped = function (params, peer) {
|
||||||
if (!peer) {
|
if (!peer) {
|
||||||
debug('unexpected `stopped` event from peer that is not in swarm')
|
debug('unexpected `stopped` event from peer that is not in swarm')
|
||||||
return // do nothing
|
return // do nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
if (peer.complete) this.complete -= 1
|
if (peer.complete) this.complete -= 1
|
||||||
else this.incomplete -= 1
|
else this.incomplete -= 1
|
||||||
this.peers[params.addr] = null
|
this.peers[params.addr] = null
|
||||||
this.emit('stop', params.addr)
|
this.emit('stop', params.addr)
|
||||||
|
|
||||||
cb()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Swarm.prototype._onAnnounce_completed = function (params, peer, cb) {
|
Swarm.prototype._onAnnounce_completed = function (params, peer) {
|
||||||
if (!peer) {
|
if (!peer) {
|
||||||
debug('unexpected `completed` event from peer that is not in swarm')
|
debug('unexpected `completed` event from peer that is not in swarm')
|
||||||
return start() // treat as a start
|
return this._onAnnounce_started(params, peer) // treat as a start
|
||||||
}
|
}
|
||||||
if (peer.complete) {
|
if (peer.complete) {
|
||||||
debug('unexpected `completed` event from peer that is already marked as completed')
|
debug('unexpected `completed` event from peer that is already marked as completed')
|
||||||
return // do nothing
|
return // do nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
this.complete += 1
|
this.complete += 1
|
||||||
this.incomplete -= 1
|
this.incomplete -= 1
|
||||||
peer.complete = true
|
peer.complete = true
|
||||||
this.emit('complete', params.addr)
|
this.emit('complete', params.addr)
|
||||||
|
|
||||||
cb()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Swarm.prototype._onAnnounce_update = function (params, peer, cb) {
|
Swarm.prototype._onAnnounce_update = function (params, peer) {
|
||||||
if (!peer) {
|
if (!peer) {
|
||||||
debug('unexpected `update` event from peer that is not in swarm')
|
debug('unexpected `update` event from peer that is not in swarm')
|
||||||
return start() // treat as a start
|
return this._onAnnounce_started(params, peer) // treat as a start
|
||||||
}
|
}
|
||||||
this.emit('update', params.addr)
|
this.emit('update', params.addr)
|
||||||
|
|
||||||
cb()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Swarm.prototype._getPeers = function (numwant) {
|
Swarm.prototype._getPeers = function (numwant) {
|
||||||
@ -110,7 +101,7 @@ Swarm.prototype._getPeers = function (numwant) {
|
|||||||
return peers
|
return peers
|
||||||
}
|
}
|
||||||
|
|
||||||
Swarm.prototype.scrape = function (infoHash, params, cb) {
|
Swarm.prototype.scrape = function (params, cb) {
|
||||||
cb(null, {
|
cb(null, {
|
||||||
complete: this.complete,
|
complete: this.complete,
|
||||||
incomplete: this.incomplete
|
incomplete: this.incomplete
|
||||||
|
13
server.js
13
server.js
@ -147,7 +147,7 @@ Server.prototype._onHttpRequest = function (req, res) {
|
|||||||
// even though it's an error for the client, it's just a warning for the server.
|
// 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 :)
|
// don't crash the server because a client sent bad data :)
|
||||||
self.emit('warning', error)
|
self.emit('warning', error)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -185,7 +185,7 @@ Server.prototype._onUdpRequest = function (msg, rinfo) {
|
|||||||
'failure reason': err.message
|
'failure reason': err.message
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var socket = dgram.createSocket('udp4')
|
var socket = dgram.createSocket('udp4')
|
||||||
response.transactionId = params.transactionId
|
response.transactionId = params.transactionId
|
||||||
response.connectionId = params.connectionId
|
response.connectionId = params.connectionId
|
||||||
@ -199,7 +199,6 @@ Server.prototype._onUdpRequest = function (msg, rinfo) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Server.prototype._onRequest = function (params, cb) {
|
Server.prototype._onRequest = function (params, cb) {
|
||||||
var response
|
|
||||||
if (params && params.action === common.ACTIONS.CONNECT) {
|
if (params && params.action === common.ACTIONS.CONNECT) {
|
||||||
cb(null, { action: common.ACTIONS.CONNECT })
|
cb(null, { action: common.ACTIONS.CONNECT })
|
||||||
} else if (params && params.action === common.ACTIONS.ANNOUNCE) {
|
} else if (params && params.action === common.ACTIONS.ANNOUNCE) {
|
||||||
@ -230,7 +229,7 @@ Server.prototype._onAnnounce = function (params, cb) {
|
|||||||
|
|
||||||
Server.prototype._onScrape = function (params, cb) {
|
Server.prototype._onScrape = function (params, cb) {
|
||||||
var self = this
|
var self = this
|
||||||
|
|
||||||
if (typeof params.info_hash === 'string') {
|
if (typeof params.info_hash === 'string') {
|
||||||
params.info_hash = [ params.info_hash ]
|
params.info_hash = [ params.info_hash ]
|
||||||
} else if (params.info_hash == null) {
|
} else if (params.info_hash == null) {
|
||||||
@ -238,13 +237,13 @@ Server.prototype._onScrape = function (params, cb) {
|
|||||||
// TODO: make this configurable!
|
// TODO: make this configurable!
|
||||||
params.info_hash = Object.keys(self.torrents)
|
params.info_hash = Object.keys(self.torrents)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Array.isArray(params.info_hash)) {
|
if (!Array.isArray(params.info_hash)) {
|
||||||
var err = new Error('invalid info_hash')
|
var err = new Error('invalid info_hash')
|
||||||
self.emit('warning', err)
|
self.emit('warning', err)
|
||||||
return cb(err)
|
return cb(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var response = {
|
var response = {
|
||||||
action: common.ACTIONS.SCRAPE,
|
action: common.ACTIONS.SCRAPE,
|
||||||
files: {},
|
files: {},
|
||||||
@ -256,7 +255,7 @@ Server.prototype._onScrape = function (params, cb) {
|
|||||||
series(params.info_hash.map(function (infoHash) {
|
series(params.info_hash.map(function (infoHash) {
|
||||||
var swarm = self.getSwarm(infoHash)
|
var swarm = self.getSwarm(infoHash)
|
||||||
return function (cb) {
|
return function (cb) {
|
||||||
swarm.scrape(infoHash, params, function (err, scrapeInfo) {
|
swarm.scrape(params, function (err, scrapeInfo) {
|
||||||
cb(err, scrapeInfo && {
|
cb(err, scrapeInfo && {
|
||||||
infoHash: infoHash,
|
infoHash: infoHash,
|
||||||
complete: scrapeInfo.complete || 0,
|
complete: scrapeInfo.complete || 0,
|
||||||
|
Loading…
Reference in New Issue
Block a user