bittorrent-tracker/lib/server/swarm.js

156 lines
4.6 KiB
JavaScript
Raw Normal View History

2014-12-10 15:47:41 +00:00
module.exports = Swarm
2017-01-21 02:34:33 +00:00
var debug = require('debug')('bittorrent-tracker:swarm')
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
// need to support when overriding Server.createSwarm() and Server.getSwarm()
2014-12-10 15:47:41 +00:00
function Swarm (infoHash, server) {
var self = this
self.infoHash = infoHash
self.complete = 0
self.incomplete = 0
self.peers = new LRU({
max: server.peersCacheLength || 1000,
2017-02-03 01:40:52 +00:00
maxAge: server.peersCacheTtl || 20 * 60 * 1000 // 20 minutes
})
// When a peer is evicted from the LRU store, send a synthetic 'stopped' event
// so the stats get updated correctly.
self.peers.on('evict', function (data) {
var peer = data.value
var params = {
type: peer.type,
event: 'stopped',
numwant: 0,
peer_id: peer.peerId
}
self._onAnnounceStopped(params, peer, peer.peerId)
// When a websocket peer is evicted, and it's not in any other swarms, close
// the websocket to conserve server resources.
if (peer.socket && peer.socket.infoHashes.length === 0) {
2017-02-03 04:28:38 +00:00
try {
peer.socket.close()
peer.socket = null
} catch (err) {}
}
})
2014-12-10 15:47:41 +00:00
}
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
// Mark the source peer as recently used in cache
var peer = self.peers.get(id)
2014-12-10 15:47:41 +00:00
if (params.event === 'started') {
self._onAnnounceStarted(params, peer, id)
} else if (params.event === 'stopped') {
self._onAnnounceStopped(params, peer, id)
} else if (params.event === 'completed') {
self._onAnnounceCompleted(params, peer, id)
} else if (params.event === 'update') {
self._onAnnounceUpdate(params, peer, id)
} else {
cb(new Error('invalid event'))
return
2014-12-10 15:47:41 +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)
})
}
2014-12-10 15:47:41 +00:00
Swarm.prototype.scrape = function (params, cb) {
cb(null, {
complete: this.complete,
incomplete: this.incomplete
})
}
Swarm.prototype._onAnnounceStarted = function (params, peer, id) {
if (peer) {
debug('unexpected `started` event from peer that is already in swarm')
2016-08-08 15:25:25 +00:00
return this._onAnnounceUpdate(params, peer, id) // treat as an update
}
if (params.left === 0) this.complete += 1
else this.incomplete += 1
peer = this.peers.set(id, {
type: params.type,
complete: params.left === 0,
peerId: params.peer_id, // as hex
ip: params.ip,
port: params.port,
socket: params.socket // only websocket
})
}
2014-12-10 15:47:41 +00:00
Swarm.prototype._onAnnounceStopped = function (params, peer, id) {
if (!peer) {
debug('unexpected `stopped` event from peer that is not in swarm')
return // do nothing
2014-12-10 15:47:41 +00:00
}
if (peer.complete) this.complete -= 1
else this.incomplete -= 1
// If it's a websocket, remove this swarm's infohash from the list of active
// swarms that this peer is participating in.
if (peer.socket) {
var index = peer.socket.infoHashes.indexOf(this.infoHash)
peer.socket.infoHashes.splice(index, 1)
}
this.peers.remove(id)
}
Swarm.prototype._onAnnounceCompleted = function (params, peer, id) {
if (!peer) {
debug('unexpected `completed` event from peer that is not in swarm')
2016-08-08 15:25:25 +00:00
return this._onAnnounceStarted(params, peer, id) // treat as a start
2014-12-10 15:47:41 +00:00
}
if (peer.complete) {
debug('unexpected `completed` event from peer that is already completed')
return this._onAnnounceUpdate(params, peer, id) // treat as an update
}
this.complete += 1
this.incomplete -= 1
peer.complete = true
this.peers.set(id, peer)
}
2014-12-10 15:47:41 +00:00
Swarm.prototype._onAnnounceUpdate = function (params, peer, id) {
if (!peer) {
debug('unexpected `update` event from peer that is not in swarm')
2016-08-08 15:25:25 +00:00
return this._onAnnounceStarted(params, peer, id) // treat as a start
}
if (!peer.complete && params.left === 0) {
this.complete += 1
this.incomplete -= 1
peer.complete = true
}
this.peers.set(id, peer)
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 = []
var ite = randomIterate(this.peers.keys)
var peerId
while ((peerId = ite()) && peers.length < numwant) {
// Don't mark the peer as most recently used on announce
var peer = this.peers.peek(peerId)
if (!peer) continue
2015-12-05 08:41:56 +00:00
if (isWebRTC && peer.peerId === ownPeerId) continue // don't send peer to itself
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
}
return peers
2014-12-10 15:47:41 +00:00
}