2014-12-10 15:47:41 +00:00
|
|
|
module.exports = Swarm
|
|
|
|
|
2014-12-11 20:53:39 +00:00
|
|
|
var debug = require('debug')('bittorrent-tracker')
|
2016-06-08 21:57:01 +00:00
|
|
|
var LRU = require('lru')
|
2015-05-19 11:32:09 +00:00
|
|
|
var randomIterate = require('random-iterate')
|
2014-12-11 20:53:39 +00:00
|
|
|
|
2014-12-10 15:47:41 +00:00
|
|
|
// Regard this as the default implementation of an interface that you
|
2016-01-03 18:50:23 +00:00
|
|
|
// need to support when overriding Server.createSwarm() and Server.getSwarm()
|
2014-12-10 15:47:41 +00:00
|
|
|
function Swarm (infoHash, server) {
|
2016-06-08 21:57:01 +00:00
|
|
|
this.peers = new LRU({
|
|
|
|
max: server.peersCacheLength || 10000,
|
|
|
|
maxAge: server.peersCacheTtl || 900 // 900s = 15 minutes
|
|
|
|
})
|
2014-12-10 15:47:41 +00:00
|
|
|
this.complete = 0
|
|
|
|
this.incomplete = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
Swarm.prototype.announce = function (params, cb) {
|
|
|
|
var self = this
|
2016-03-12 01:21:19 +00:00
|
|
|
var id = params.type === 'ws' ? params.peer_id : params.addr
|
2016-06-08 21:57:01 +00:00
|
|
|
// Mark the source peer as recently used in cache
|
|
|
|
var peer = self.peers.get(id)
|
2014-12-10 15:47:41 +00:00
|
|
|
|
2015-07-27 22:19:18 +00:00
|
|
|
if (params.event === 'started') {
|
|
|
|
self._onAnnounceStarted(params, peer)
|
|
|
|
} else if (params.event === 'stopped') {
|
|
|
|
self._onAnnounceStopped(params, peer)
|
|
|
|
} else if (params.event === 'completed') {
|
|
|
|
self._onAnnounceCompleted(params, peer)
|
|
|
|
} else if (params.event === 'update') {
|
|
|
|
self._onAnnounceUpdate(params, peer)
|
2014-12-10 16:01:34 +00:00
|
|
|
} else {
|
|
|
|
cb(new Error('invalid event'))
|
2015-07-27 22:19:18 +00:00
|
|
|
return
|
2014-12-10 15:47:41 +00:00
|
|
|
}
|
2015-07-27 22:19:18 +00:00
|
|
|
cb(null, {
|
|
|
|
complete: self.complete,
|
|
|
|
incomplete: self.incomplete,
|
2015-12-05 08:41:56 +00:00
|
|
|
peers: self._getPeers(params.numwant, params.peer_id, !!params.socket)
|
2015-07-27 22:19:18 +00:00
|
|
|
})
|
2014-12-10 16:01:34 +00:00
|
|
|
}
|
2014-12-10 15:47:41 +00:00
|
|
|
|
2015-03-27 03:19:06 +00:00
|
|
|
Swarm.prototype.scrape = function (params, cb) {
|
|
|
|
cb(null, {
|
|
|
|
complete: this.complete,
|
|
|
|
incomplete: this.incomplete
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-07-27 22:19:18 +00:00
|
|
|
Swarm.prototype._onAnnounceStarted = function (params, peer) {
|
2014-12-10 16:01:34 +00:00
|
|
|
if (peer) {
|
|
|
|
debug('unexpected `started` event from peer that is already in swarm')
|
2015-07-27 22:46:49 +00:00
|
|
|
return this._onAnnounceUpdate(params, peer) // treat as an update
|
2014-12-10 16:01:34 +00:00
|
|
|
}
|
2014-12-11 21:43:11 +00:00
|
|
|
|
2014-12-10 16:01:34 +00:00
|
|
|
if (params.left === 0) this.complete += 1
|
|
|
|
else this.incomplete += 1
|
2016-03-12 01:21:19 +00:00
|
|
|
var id = params.type === 'ws' ? params.peer_id : params.addr
|
2016-06-08 21:57:01 +00:00
|
|
|
peer = this.peers.set(id, {
|
2016-03-13 14:51:08 +00:00
|
|
|
type: params.type,
|
2015-04-21 03:02:05 +00:00
|
|
|
complete: params.left === 0,
|
2015-03-29 08:08:26 +00:00
|
|
|
peerId: params.peer_id, // as hex
|
2016-03-13 14:51:08 +00:00
|
|
|
ip: params.ip,
|
|
|
|
port: params.port,
|
2015-03-27 03:19:06 +00:00
|
|
|
socket: params.socket // only websocket
|
2016-06-08 21:57:01 +00:00
|
|
|
})
|
2014-12-10 16:01:34 +00:00
|
|
|
}
|
2014-12-10 15:47:41 +00:00
|
|
|
|
2015-07-27 22:19:18 +00:00
|
|
|
Swarm.prototype._onAnnounceStopped = function (params, peer) {
|
2014-12-10 16:01:34 +00:00
|
|
|
if (!peer) {
|
|
|
|
debug('unexpected `stopped` event from peer that is not in swarm')
|
|
|
|
return // do nothing
|
2014-12-10 15:47:41 +00:00
|
|
|
}
|
2014-12-11 21:43:11 +00:00
|
|
|
|
2014-12-10 16:01:34 +00:00
|
|
|
if (peer.complete) this.complete -= 1
|
|
|
|
else this.incomplete -= 1
|
2016-03-12 01:21:19 +00:00
|
|
|
var id = params.type === 'ws' ? params.peer_id : params.addr
|
2016-06-08 21:57:01 +00:00
|
|
|
this.peers.remove(id)
|
2014-12-10 16:01:34 +00:00
|
|
|
}
|
|
|
|
|
2015-07-27 22:19:18 +00:00
|
|
|
Swarm.prototype._onAnnounceCompleted = function (params, peer) {
|
2014-12-10 16:01:34 +00:00
|
|
|
if (!peer) {
|
|
|
|
debug('unexpected `completed` event from peer that is not in swarm')
|
2015-07-27 22:46:49 +00:00
|
|
|
return this._onAnnounceStarted(params, peer) // treat as a start
|
2014-12-10 15:47:41 +00:00
|
|
|
}
|
2014-12-10 16:01:34 +00:00
|
|
|
if (peer.complete) {
|
|
|
|
debug('unexpected `completed` event from peer that is already marked as completed')
|
|
|
|
return // do nothing
|
|
|
|
}
|
2014-12-11 21:43:11 +00:00
|
|
|
|
2014-12-10 16:01:34 +00:00
|
|
|
this.complete += 1
|
|
|
|
this.incomplete -= 1
|
|
|
|
peer.complete = true
|
|
|
|
}
|
2014-12-10 15:47:41 +00:00
|
|
|
|
2015-07-27 22:19:18 +00:00
|
|
|
Swarm.prototype._onAnnounceUpdate = function (params, peer) {
|
2014-12-10 16:01:34 +00:00
|
|
|
if (!peer) {
|
|
|
|
debug('unexpected `update` event from peer that is not in swarm')
|
2015-07-27 22:46:49 +00:00
|
|
|
return this._onAnnounceStarted(params, peer) // treat as a start
|
2014-12-10 16:01:34 +00:00
|
|
|
}
|
2015-05-03 23:38:16 +00:00
|
|
|
|
|
|
|
if (!peer.complete && params.left === 0) {
|
|
|
|
this.complete += 1
|
|
|
|
this.incomplete -= 1
|
|
|
|
peer.complete = true
|
|
|
|
}
|
2014-12-10 15:47:41 +00:00
|
|
|
}
|
|
|
|
|
2015-12-05 08:41:56 +00:00
|
|
|
Swarm.prototype._getPeers = function (numwant, ownPeerId, isWebRTC) {
|
2014-12-10 15:47:41 +00:00
|
|
|
var peers = []
|
2016-06-08 21:57:01 +00:00
|
|
|
var ite = randomIterate(Object.keys(this.peers.cache))
|
2015-07-27 22:19:18 +00:00
|
|
|
var peerId
|
|
|
|
while ((peerId = ite()) && peers.length < numwant) {
|
2016-06-08 21:57:01 +00:00
|
|
|
// Don't mark the peer as most recently used on announce
|
|
|
|
var peer = this.peers.peek(peerId)
|
2015-12-05 08:41:56 +00:00
|
|
|
if (isWebRTC && peer.peerId === ownPeerId) continue // don't send peer to itself
|
2016-03-13 14:51:08 +00:00
|
|
|
if ((isWebRTC && peer.type !== 'ws') || (!isWebRTC && peer.type === 'ws')) continue // send proper peer type
|
2015-12-05 08:41:56 +00:00
|
|
|
peers.push(peer)
|
2014-12-10 15:47:41 +00:00
|
|
|
}
|
2015-07-27 22:19:18 +00:00
|
|
|
return peers
|
2014-12-10 15:47:41 +00:00
|
|
|
}
|