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