2022-12-05 22:06:54 +00:00
|
|
|
|
import bencode from 'bencode'
|
|
|
|
|
import Debug from 'debug'
|
|
|
|
|
import dgram from 'dgram'
|
|
|
|
|
import EventEmitter from 'events'
|
|
|
|
|
import http from 'http'
|
|
|
|
|
import peerid from 'bittorrent-peerid'
|
|
|
|
|
import series from 'run-series'
|
|
|
|
|
import string2compact from 'string2compact'
|
2023-02-01 14:19:06 +00:00
|
|
|
|
import { WebSocketServer } from 'ws'
|
2023-05-26 16:54:30 +00:00
|
|
|
|
import { hex2bin } from 'uint8-util'
|
2022-12-05 22:06:54 +00:00
|
|
|
|
|
|
|
|
|
import common from './lib/common.js'
|
|
|
|
|
import Swarm from './lib/server/swarm.js'
|
|
|
|
|
import parseHttpRequest from './lib/server/parse-http.js'
|
|
|
|
|
import parseUdpRequest from './lib/server/parse-udp.js'
|
|
|
|
|
import parseWebSocketRequest from './lib/server/parse-websocket.js'
|
|
|
|
|
|
|
|
|
|
const debug = Debug('bittorrent-tracker:server')
|
2019-07-05 20:12:24 +00:00
|
|
|
|
const hasOwnProperty = Object.prototype.hasOwnProperty
|
|
|
|
|
|
2014-06-07 22:10:12 +00:00
|
|
|
|
/**
|
2015-03-27 03:19:06 +00:00
|
|
|
|
* BitTorrent tracker server.
|
2014-06-07 22:10:12 +00:00
|
|
|
|
*
|
2015-03-27 03:19:06 +00:00
|
|
|
|
* HTTP service which responds to GET requests from torrent clients. Requests include
|
|
|
|
|
* metrics from clients that help the tracker keep overall statistics about the torrent.
|
|
|
|
|
* Responses include a peer list that helps the client participate in the torrent.
|
2014-06-07 22:10:12 +00:00
|
|
|
|
*
|
2021-01-28 03:27:39 +00:00
|
|
|
|
* @param {Object} opts options object
|
|
|
|
|
* @param {Number} opts.interval tell clients to announce on this interval (ms)
|
|
|
|
|
* @param {Number} opts.trustProxy trust 'x-forwarded-for' header from reverse proxy
|
|
|
|
|
* @param {boolean|Object} opts.http start an http server?, or options for http.createServer (default: true)
|
|
|
|
|
* @param {boolean|Object} opts.udp start a udp server?, or extra options for dgram.createSocket (default: true)
|
|
|
|
|
* @param {boolean|Object} opts.ws start a websocket server?, or extra options for new WebSocketServer (default: true)
|
|
|
|
|
* @param {boolean} opts.stats enable web-based statistics? (default: true)
|
|
|
|
|
* @param {function} opts.filter black/whitelist fn for disallowing/allowing torrents
|
2014-06-07 22:10:12 +00:00
|
|
|
|
*/
|
2018-10-02 16:26:35 +00:00
|
|
|
|
class Server extends EventEmitter {
|
|
|
|
|
constructor (opts = {}) {
|
|
|
|
|
super()
|
|
|
|
|
debug('new server %s', JSON.stringify(opts))
|
2014-08-07 05:35:43 +00:00
|
|
|
|
|
2018-10-03 09:49:45 +00:00
|
|
|
|
this.intervalMs = opts.interval
|
2018-10-02 16:26:35 +00:00
|
|
|
|
? opts.interval
|
|
|
|
|
: 10 * 60 * 1000 // 10 min
|
|
|
|
|
|
2018-10-03 09:49:45 +00:00
|
|
|
|
this._trustProxy = !!opts.trustProxy
|
|
|
|
|
if (typeof opts.filter === 'function') this._filter = opts.filter
|
2018-10-02 16:26:35 +00:00
|
|
|
|
|
2018-10-03 09:49:45 +00:00
|
|
|
|
this.peersCacheLength = opts.peersCacheLength
|
|
|
|
|
this.peersCacheTtl = opts.peersCacheTtl
|
2018-10-02 16:26:35 +00:00
|
|
|
|
|
2018-10-03 09:49:45 +00:00
|
|
|
|
this._listenCalled = false
|
|
|
|
|
this.listening = false
|
|
|
|
|
this.destroyed = false
|
|
|
|
|
this.torrents = {}
|
2018-10-02 16:26:35 +00:00
|
|
|
|
|
2018-10-03 09:49:45 +00:00
|
|
|
|
this.http = null
|
|
|
|
|
this.udp4 = null
|
|
|
|
|
this.udp6 = null
|
|
|
|
|
this.ws = null
|
2018-10-02 16:26:35 +00:00
|
|
|
|
|
|
|
|
|
// start an http tracker unless the user explictly says no
|
|
|
|
|
if (opts.http !== false) {
|
2021-01-28 03:27:39 +00:00
|
|
|
|
this.http = http.createServer(isObject(opts.http) ? opts.http : undefined)
|
2018-10-03 09:49:45 +00:00
|
|
|
|
this.http.on('error', err => { this._onError(err) })
|
|
|
|
|
this.http.on('listening', onListening)
|
2015-12-15 23:37:53 +00:00
|
|
|
|
|
2015-12-16 11:41:07 +00:00
|
|
|
|
// Add default http request handler on next tick to give user the chance to add
|
|
|
|
|
// their own handler first. Handle requests untouched by user's handler.
|
2018-10-02 16:26:35 +00:00
|
|
|
|
process.nextTick(() => {
|
2018-10-03 09:49:45 +00:00
|
|
|
|
this.http.on('request', (req, res) => {
|
2015-12-16 11:41:07 +00:00
|
|
|
|
if (res.headersSent) return
|
2018-10-03 09:49:45 +00:00
|
|
|
|
this.onHttpRequest(req, res)
|
2015-12-15 23:37:53 +00:00
|
|
|
|
})
|
|
|
|
|
})
|
2015-03-27 03:19:06 +00:00
|
|
|
|
}
|
2018-10-02 16:26:35 +00:00
|
|
|
|
|
|
|
|
|
// start a udp tracker unless the user explicitly says no
|
|
|
|
|
if (opts.udp !== false) {
|
2021-01-28 03:27:39 +00:00
|
|
|
|
this.udp4 = this.udp = dgram.createSocket({
|
|
|
|
|
type: 'udp4',
|
|
|
|
|
reuseAddr: true,
|
|
|
|
|
...(isObject(opts.udp) ? opts.udp : undefined)
|
|
|
|
|
})
|
2018-10-03 09:49:45 +00:00
|
|
|
|
this.udp4.on('message', (msg, rinfo) => { this.onUdpRequest(msg, rinfo) })
|
|
|
|
|
this.udp4.on('error', err => { this._onError(err) })
|
|
|
|
|
this.udp4.on('listening', onListening)
|
2018-10-02 16:26:35 +00:00
|
|
|
|
|
2021-01-28 03:27:39 +00:00
|
|
|
|
this.udp6 = dgram.createSocket({
|
|
|
|
|
type: 'udp6',
|
|
|
|
|
reuseAddr: true,
|
|
|
|
|
...(isObject(opts.udp) ? opts.udp : undefined)
|
|
|
|
|
})
|
2018-10-03 09:49:45 +00:00
|
|
|
|
this.udp6.on('message', (msg, rinfo) => { this.onUdpRequest(msg, rinfo) })
|
|
|
|
|
this.udp6.on('error', err => { this._onError(err) })
|
|
|
|
|
this.udp6.on('listening', onListening)
|
2016-03-29 06:38:23 +00:00
|
|
|
|
}
|
2015-03-27 03:19:06 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
// start a websocket tracker (for WebTorrent) unless the user explicitly says no
|
|
|
|
|
if (opts.ws !== false) {
|
2021-01-28 03:27:39 +00:00
|
|
|
|
const noServer = isObject(opts.ws) && opts.ws.noServer
|
|
|
|
|
if (!this.http && !noServer) {
|
2018-10-03 09:49:45 +00:00
|
|
|
|
this.http = http.createServer()
|
|
|
|
|
this.http.on('error', err => { this._onError(err) })
|
|
|
|
|
this.http.on('listening', onListening)
|
2018-10-02 16:26:35 +00:00
|
|
|
|
|
|
|
|
|
// Add default http request handler on next tick to give user the chance to add
|
|
|
|
|
// their own handler first. Handle requests untouched by user's handler.
|
|
|
|
|
process.nextTick(() => {
|
2018-10-03 09:49:45 +00:00
|
|
|
|
this.http.on('request', (req, res) => {
|
2018-10-02 16:26:35 +00:00
|
|
|
|
if (res.headersSent) return
|
|
|
|
|
// For websocket trackers, we only need to handle the UPGRADE http method.
|
|
|
|
|
// Return 404 for all other request types.
|
|
|
|
|
res.statusCode = 404
|
|
|
|
|
res.end('404 Not Found')
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
2018-10-03 09:49:45 +00:00
|
|
|
|
this.ws = new WebSocketServer({
|
2021-01-28 03:27:39 +00:00
|
|
|
|
server: noServer ? undefined : this.http,
|
2018-10-02 16:26:35 +00:00
|
|
|
|
perMessageDeflate: false,
|
2021-01-28 03:27:39 +00:00
|
|
|
|
clientTracking: false,
|
|
|
|
|
...(isObject(opts.ws) ? opts.ws : undefined)
|
2018-10-02 16:26:35 +00:00
|
|
|
|
})
|
2021-01-28 20:16:53 +00:00
|
|
|
|
|
|
|
|
|
this.ws.address = () => {
|
|
|
|
|
if (noServer) {
|
|
|
|
|
throw new Error('address() unavailable with { noServer: true }')
|
2021-01-28 03:27:39 +00:00
|
|
|
|
}
|
2021-01-28 20:16:53 +00:00
|
|
|
|
return this.http.address()
|
2018-10-02 16:26:35 +00:00
|
|
|
|
}
|
2021-01-28 20:16:53 +00:00
|
|
|
|
|
2018-10-03 09:49:45 +00:00
|
|
|
|
this.ws.on('error', err => { this._onError(err) })
|
|
|
|
|
this.ws.on('connection', (socket, req) => {
|
2018-10-02 16:26:35 +00:00
|
|
|
|
// Note: socket.upgradeReq was removed in ws@3.0.0, so re-add it.
|
|
|
|
|
// https://github.com/websockets/ws/pull/1099
|
|
|
|
|
socket.upgradeReq = req
|
2018-10-03 09:49:45 +00:00
|
|
|
|
this.onWebSocketConnection(socket)
|
2018-10-02 16:26:35 +00:00
|
|
|
|
})
|
2016-03-29 22:03:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
if (opts.stats !== false) {
|
2018-10-03 09:49:45 +00:00
|
|
|
|
if (!this.http) {
|
|
|
|
|
this.http = http.createServer()
|
|
|
|
|
this.http.on('error', err => { this._onError(err) })
|
|
|
|
|
this.http.on('listening', onListening)
|
2018-10-02 16:26:35 +00:00
|
|
|
|
}
|
2016-03-29 22:03:24 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
// Http handler for '/stats' route
|
2018-10-03 09:49:45 +00:00
|
|
|
|
this.http.on('request', (req, res) => {
|
2018-10-02 16:26:35 +00:00
|
|
|
|
if (res.headersSent) return
|
|
|
|
|
|
2018-10-03 09:49:45 +00:00
|
|
|
|
const infoHashes = Object.keys(this.torrents)
|
2018-10-02 16:26:35 +00:00
|
|
|
|
let activeTorrents = 0
|
|
|
|
|
const allPeers = {}
|
2016-03-29 22:03:24 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
function countPeers (filterFunction) {
|
|
|
|
|
let count = 0
|
|
|
|
|
let key
|
2016-03-29 22:03:24 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
for (key in allPeers) {
|
2019-07-05 20:12:24 +00:00
|
|
|
|
if (hasOwnProperty.call(allPeers, key) && filterFunction(allPeers[key])) {
|
2018-10-02 16:26:35 +00:00
|
|
|
|
count++
|
|
|
|
|
}
|
2016-03-29 22:03:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
return count
|
|
|
|
|
}
|
2016-03-29 22:03:24 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
function groupByClient () {
|
|
|
|
|
const clients = {}
|
|
|
|
|
for (const key in allPeers) {
|
2019-07-05 20:12:24 +00:00
|
|
|
|
if (hasOwnProperty.call(allPeers, key)) {
|
2018-10-02 16:26:35 +00:00
|
|
|
|
const peer = allPeers[key]
|
2016-06-07 12:34:38 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
if (!clients[peer.client.client]) {
|
|
|
|
|
clients[peer.client.client] = {}
|
|
|
|
|
}
|
|
|
|
|
const client = clients[peer.client.client]
|
|
|
|
|
// If the client is not known show 8 chars from peerId as version
|
|
|
|
|
const version = peer.client.version || Buffer.from(peer.peerId, 'hex').toString().substring(0, 8)
|
|
|
|
|
if (!client[version]) {
|
|
|
|
|
client[version] = 0
|
|
|
|
|
}
|
|
|
|
|
client[version]++
|
2016-06-07 12:34:38 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-02 16:26:35 +00:00
|
|
|
|
return clients
|
2016-06-07 12:34:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
function printClients (clients) {
|
|
|
|
|
let html = '<ul>\n'
|
|
|
|
|
for (const name in clients) {
|
2019-07-05 20:12:24 +00:00
|
|
|
|
if (hasOwnProperty.call(clients, name)) {
|
2018-10-02 16:26:35 +00:00
|
|
|
|
const client = clients[name]
|
|
|
|
|
for (const version in client) {
|
2019-07-05 20:12:24 +00:00
|
|
|
|
if (hasOwnProperty.call(client, version)) {
|
2018-10-02 16:26:35 +00:00
|
|
|
|
html += `<li><strong>${name}</strong> ${version} : ${client[version]}</li>\n`
|
|
|
|
|
}
|
2016-06-07 12:34:38 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-02 16:26:35 +00:00
|
|
|
|
html += '</ul>'
|
|
|
|
|
return html
|
2016-06-07 12:34:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
if (req.method === 'GET' && (req.url === '/stats' || req.url === '/stats.json')) {
|
|
|
|
|
infoHashes.forEach(infoHash => {
|
2018-10-03 09:49:45 +00:00
|
|
|
|
const peers = this.torrents[infoHash].peers
|
2018-10-02 16:26:35 +00:00
|
|
|
|
const keys = peers.keys
|
|
|
|
|
if (keys.length > 0) activeTorrents++
|
|
|
|
|
|
|
|
|
|
keys.forEach(peerId => {
|
|
|
|
|
// Don't mark the peer as most recently used for stats
|
|
|
|
|
const peer = peers.peek(peerId)
|
|
|
|
|
if (peer == null) return // peers.peek() can evict the peer
|
|
|
|
|
|
2019-07-05 20:12:24 +00:00
|
|
|
|
if (!hasOwnProperty.call(allPeers, peerId)) {
|
2018-10-02 16:26:35 +00:00
|
|
|
|
allPeers[peerId] = {
|
|
|
|
|
ipv4: false,
|
|
|
|
|
ipv6: false,
|
|
|
|
|
seeder: false,
|
|
|
|
|
leecher: false
|
|
|
|
|
}
|
2016-08-09 11:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
if (peer.ip.includes(':')) {
|
|
|
|
|
allPeers[peerId].ipv6 = true
|
|
|
|
|
} else {
|
|
|
|
|
allPeers[peerId].ipv4 = true
|
|
|
|
|
}
|
2016-08-10 04:39:30 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
if (peer.complete) {
|
|
|
|
|
allPeers[peerId].seeder = true
|
|
|
|
|
} else {
|
|
|
|
|
allPeers[peerId].leecher = true
|
|
|
|
|
}
|
2016-08-10 04:39:30 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
allPeers[peerId].peerId = peer.peerId
|
|
|
|
|
allPeers[peerId].client = peerid(peer.peerId)
|
|
|
|
|
})
|
2016-03-29 22:03:24 +00:00
|
|
|
|
})
|
|
|
|
|
|
2021-06-15 01:54:41 +00:00
|
|
|
|
const isSeederOnly = peer => peer.seeder && peer.leecher === false
|
|
|
|
|
const isLeecherOnly = peer => peer.leecher && peer.seeder === false
|
|
|
|
|
const isSeederAndLeecher = peer => peer.seeder && peer.leecher
|
|
|
|
|
const isIPv4 = peer => peer.ipv4
|
|
|
|
|
const isIPv6 = peer => peer.ipv6
|
2018-10-02 16:26:35 +00:00
|
|
|
|
|
|
|
|
|
const stats = {
|
|
|
|
|
torrents: infoHashes.length,
|
|
|
|
|
activeTorrents,
|
|
|
|
|
peersAll: Object.keys(allPeers).length,
|
|
|
|
|
peersSeederOnly: countPeers(isSeederOnly),
|
|
|
|
|
peersLeecherOnly: countPeers(isLeecherOnly),
|
|
|
|
|
peersSeederAndLeecher: countPeers(isSeederAndLeecher),
|
|
|
|
|
peersIPv4: countPeers(isIPv4),
|
|
|
|
|
peersIPv6: countPeers(isIPv6),
|
|
|
|
|
clients: groupByClient()
|
|
|
|
|
}
|
2016-06-02 02:27:14 +00:00
|
|
|
|
|
2019-07-28 03:35:35 +00:00
|
|
|
|
if (req.url === '/stats.json' || req.headers.accept === 'application/json') {
|
2020-03-29 17:32:31 +00:00
|
|
|
|
res.setHeader('Content-Type', 'application/json')
|
|
|
|
|
res.end(JSON.stringify(stats))
|
2018-10-02 16:26:35 +00:00
|
|
|
|
} else if (req.url === '/stats') {
|
2020-03-29 17:51:49 +00:00
|
|
|
|
res.setHeader('Content-Type', 'text/html')
|
2018-10-03 09:49:45 +00:00
|
|
|
|
res.end(`
|
|
|
|
|
<h1>${stats.torrents} torrents (${stats.activeTorrents} active)</h1>
|
|
|
|
|
<h2>Connected Peers: ${stats.peersAll}</h2>
|
|
|
|
|
<h3>Peers Seeding Only: ${stats.peersSeederOnly}</h3>
|
|
|
|
|
<h3>Peers Leeching Only: ${stats.peersLeecherOnly}</h3>
|
|
|
|
|
<h3>Peers Seeding & Leeching: ${stats.peersSeederAndLeecher}</h3>
|
|
|
|
|
<h3>IPv4 Peers: ${stats.peersIPv4}</h3>
|
|
|
|
|
<h3>IPv6 Peers: ${stats.peersIPv6}</h3>
|
|
|
|
|
<h3>Clients:</h3>
|
|
|
|
|
${printClients(stats.clients)}
|
|
|
|
|
`.replace(/^\s+/gm, '')) // trim left
|
2018-10-02 16:26:35 +00:00
|
|
|
|
}
|
2016-06-02 02:27:14 +00:00
|
|
|
|
}
|
2018-10-02 16:26:35 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-03 09:49:45 +00:00
|
|
|
|
let num = !!this.http + !!this.udp4 + !!this.udp6
|
|
|
|
|
const self = this
|
2018-10-02 16:26:35 +00:00
|
|
|
|
function onListening () {
|
|
|
|
|
num -= 1
|
|
|
|
|
if (num === 0) {
|
|
|
|
|
self.listening = true
|
|
|
|
|
debug('listening')
|
|
|
|
|
self.emit('listening')
|
2016-03-29 22:03:24 +00:00
|
|
|
|
}
|
2018-10-02 16:26:35 +00:00
|
|
|
|
}
|
2016-03-29 22:03:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
_onError (err) {
|
2018-10-02 16:37:46 +00:00
|
|
|
|
this.emit('error', err)
|
2014-06-07 22:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
listen (...args) /* port, hostname, onlistening */{
|
2018-10-02 16:33:00 +00:00
|
|
|
|
if (this._listenCalled || this.listening) throw new Error('server already listening')
|
|
|
|
|
this._listenCalled = true
|
2014-07-13 22:28:23 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
const lastArg = args[args.length - 1]
|
2018-10-02 16:33:00 +00:00
|
|
|
|
if (typeof lastArg === 'function') this.once('listening', lastArg)
|
2015-04-01 22:42:13 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
const port = toNumber(args[0]) || args[0] || 0
|
|
|
|
|
const hostname = typeof args[1] !== 'function' ? args[1] : undefined
|
2015-06-10 05:32:04 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
debug('listen (port: %o hostname: %o)', port, hostname)
|
2015-04-01 22:42:13 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
const httpPort = isObject(port) ? (port.http || 0) : port
|
|
|
|
|
const udpPort = isObject(port) ? (port.udp || 0) : port
|
2015-04-01 22:42:13 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
// binding to :: only receives IPv4 connections if the bindv6only sysctl is set 0,
|
|
|
|
|
// which is the default on many operating systems
|
|
|
|
|
const httpHostname = isObject(hostname) ? hostname.http : hostname
|
|
|
|
|
const udp4Hostname = isObject(hostname) ? hostname.udp : hostname
|
|
|
|
|
const udp6Hostname = isObject(hostname) ? hostname.udp6 : hostname
|
2014-08-17 02:05:56 +00:00
|
|
|
|
|
2018-10-02 16:33:00 +00:00
|
|
|
|
if (this.http) this.http.listen(httpPort, httpHostname)
|
|
|
|
|
if (this.udp4) this.udp4.bind(udpPort, udp4Hostname)
|
|
|
|
|
if (this.udp6) this.udp6.bind(udpPort, udp6Hostname)
|
2015-05-04 04:00:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
close (cb = noop) {
|
|
|
|
|
debug('close')
|
2015-05-04 04:00:38 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
this.listening = false
|
|
|
|
|
this.destroyed = true
|
2015-05-04 04:00:38 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
if (this.udp4) {
|
|
|
|
|
try {
|
|
|
|
|
this.udp4.close()
|
|
|
|
|
} catch (err) {}
|
|
|
|
|
}
|
2014-06-07 22:10:12 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
if (this.udp6) {
|
|
|
|
|
try {
|
|
|
|
|
this.udp6.close()
|
|
|
|
|
} catch (err) {}
|
|
|
|
|
}
|
2015-03-27 03:19:06 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
if (this.ws) {
|
|
|
|
|
try {
|
|
|
|
|
this.ws.close()
|
|
|
|
|
} catch (err) {}
|
|
|
|
|
}
|
2015-03-27 03:19:06 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
if (this.http) this.http.close(cb)
|
|
|
|
|
else cb(null)
|
2015-05-02 19:15:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
createSwarm (infoHash, cb) {
|
2023-02-01 02:43:44 +00:00
|
|
|
|
if (ArrayBuffer.isView(infoHash)) infoHash = infoHash.toString('hex')
|
2015-03-27 03:19:06 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
process.nextTick(() => {
|
|
|
|
|
const swarm = this.torrents[infoHash] = new Server.Swarm(infoHash, this)
|
|
|
|
|
cb(null, swarm)
|
|
|
|
|
})
|
2014-06-07 22:10:12 +00:00
|
|
|
|
}
|
2015-03-27 03:19:06 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
getSwarm (infoHash, cb) {
|
2023-02-01 02:43:44 +00:00
|
|
|
|
if (ArrayBuffer.isView(infoHash)) infoHash = infoHash.toString('hex')
|
2015-07-05 00:13:07 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
process.nextTick(() => {
|
|
|
|
|
cb(null, this.torrents[infoHash])
|
|
|
|
|
})
|
2014-12-08 22:42:05 +00:00
|
|
|
|
}
|
2014-11-26 07:27:02 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
onHttpRequest (req, res, opts = {}) {
|
|
|
|
|
opts.trustProxy = opts.trustProxy || this._trustProxy
|
2014-06-07 22:10:12 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
let params
|
|
|
|
|
try {
|
|
|
|
|
params = parseHttpRequest(req, opts)
|
|
|
|
|
params.httpReq = req
|
|
|
|
|
params.httpRes = res
|
|
|
|
|
} catch (err) {
|
|
|
|
|
res.end(bencode.encode({
|
|
|
|
|
'failure reason': err.message
|
|
|
|
|
}))
|
2015-01-29 22:59:08 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
// 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 :)
|
|
|
|
|
this.emit('warning', err)
|
|
|
|
|
return
|
2015-01-29 22:59:08 +00:00
|
|
|
|
}
|
2014-06-07 22:10:12 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
this._onRequest(params, (err, response) => {
|
|
|
|
|
if (err) {
|
|
|
|
|
this.emit('warning', err)
|
|
|
|
|
response = {
|
|
|
|
|
'failure reason': err.message
|
|
|
|
|
}
|
2014-11-26 07:27:02 +00:00
|
|
|
|
}
|
2018-10-02 16:26:35 +00:00
|
|
|
|
if (this.destroyed) return res.end()
|
2014-12-11 20:53:39 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
delete response.action // only needed for UDP encoding
|
|
|
|
|
res.end(bencode.encode(response))
|
2015-01-29 22:59:08 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
if (params.action === common.ACTIONS.ANNOUNCE) {
|
|
|
|
|
this.emit(common.EVENT_NAMES[params.event], params.addr, params)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
2015-05-01 21:20:53 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
onUdpRequest (msg, rinfo) {
|
|
|
|
|
let params
|
2015-05-01 21:20:53 +00:00
|
|
|
|
try {
|
2018-10-02 16:26:35 +00:00
|
|
|
|
params = parseUdpRequest(msg, rinfo)
|
2015-05-01 21:20:53 +00:00
|
|
|
|
} catch (err) {
|
2018-10-02 16:26:35 +00:00
|
|
|
|
this.emit('warning', err)
|
|
|
|
|
// Do not reply for parsing errors
|
|
|
|
|
return
|
2015-05-01 21:20:53 +00:00
|
|
|
|
}
|
2015-01-29 22:59:08 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
this._onRequest(params, (err, response) => {
|
|
|
|
|
if (err) {
|
|
|
|
|
this.emit('warning', err)
|
|
|
|
|
response = {
|
|
|
|
|
action: common.ACTIONS.ERROR,
|
|
|
|
|
'failure reason': err.message
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (this.destroyed) return
|
2014-11-26 07:27:02 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
response.transactionId = params.transactionId
|
|
|
|
|
response.connectionId = params.connectionId
|
2016-03-19 05:35:01 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
const buf = makeUdpPacket(response)
|
2016-03-18 22:06:05 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
try {
|
|
|
|
|
const udp = (rinfo.family === 'IPv4') ? this.udp4 : this.udp6
|
|
|
|
|
udp.send(buf, 0, buf.length, rinfo.port, rinfo.address)
|
|
|
|
|
} catch (err) {
|
|
|
|
|
this.emit('warning', err)
|
|
|
|
|
}
|
2016-03-18 22:06:05 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
if (params.action === common.ACTIONS.ANNOUNCE) {
|
|
|
|
|
this.emit(common.EVENT_NAMES[params.event], params.addr, params)
|
|
|
|
|
}
|
|
|
|
|
})
|
2016-03-29 06:38:23 +00:00
|
|
|
|
}
|
2016-03-18 22:06:05 +00:00
|
|
|
|
|
2018-10-02 16:37:46 +00:00
|
|
|
|
onWebSocketConnection (socket, opts = {}) {
|
2018-10-02 16:33:00 +00:00
|
|
|
|
opts.trustProxy = opts.trustProxy || this._trustProxy
|
2015-03-27 03:19:06 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
socket.peerId = null // as hex
|
|
|
|
|
socket.infoHashes = [] // swarms that this socket is participating in
|
|
|
|
|
socket.onSend = err => {
|
2018-10-02 16:33:00 +00:00
|
|
|
|
this._onWebSocketSend(socket, err)
|
2018-10-02 16:26:35 +00:00
|
|
|
|
}
|
2015-03-27 03:19:06 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
socket.onMessageBound = params => {
|
2018-10-02 16:33:00 +00:00
|
|
|
|
this._onWebSocketRequest(socket, opts, params)
|
2018-10-02 16:26:35 +00:00
|
|
|
|
}
|
|
|
|
|
socket.on('message', socket.onMessageBound)
|
2015-03-27 03:19:06 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
socket.onErrorBound = err => {
|
2018-10-02 16:33:00 +00:00
|
|
|
|
this._onWebSocketError(socket, err)
|
2018-10-02 16:26:35 +00:00
|
|
|
|
}
|
|
|
|
|
socket.on('error', socket.onErrorBound)
|
|
|
|
|
|
|
|
|
|
socket.onCloseBound = () => {
|
2018-10-02 16:33:00 +00:00
|
|
|
|
this._onWebSocketClose(socket)
|
2018-10-02 16:26:35 +00:00
|
|
|
|
}
|
|
|
|
|
socket.on('close', socket.onCloseBound)
|
2015-03-27 03:19:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
_onWebSocketRequest (socket, opts, params) {
|
|
|
|
|
try {
|
|
|
|
|
params = parseWebSocketRequest(socket, opts, params)
|
|
|
|
|
} catch (err) {
|
2016-03-01 01:35:04 +00:00
|
|
|
|
socket.send(JSON.stringify({
|
2018-10-02 16:26:35 +00:00
|
|
|
|
'failure reason': err.message
|
2016-03-01 01:35:04 +00:00
|
|
|
|
}), socket.onSend)
|
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
// 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 :)
|
2018-10-03 09:49:45 +00:00
|
|
|
|
this.emit('warning', err)
|
2016-03-01 01:35:04 +00:00
|
|
|
|
return
|
2015-03-27 03:19:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
if (!socket.peerId) socket.peerId = params.peer_id // as hex
|
2015-03-27 03:19:06 +00:00
|
|
|
|
|
2018-10-03 09:49:45 +00:00
|
|
|
|
this._onRequest(params, (err, response) => {
|
|
|
|
|
if (this.destroyed || socket.destroyed) return
|
2018-10-02 16:26:35 +00:00
|
|
|
|
if (err) {
|
|
|
|
|
socket.send(JSON.stringify({
|
|
|
|
|
action: params.action === common.ACTIONS.ANNOUNCE ? 'announce' : 'scrape',
|
|
|
|
|
'failure reason': err.message,
|
2023-05-26 16:54:30 +00:00
|
|
|
|
info_hash: hex2bin(params.info_hash)
|
2018-10-02 16:26:35 +00:00
|
|
|
|
}), socket.onSend)
|
2015-12-05 08:18:52 +00:00
|
|
|
|
|
2018-10-03 09:49:45 +00:00
|
|
|
|
this.emit('warning', err)
|
2018-10-02 16:26:35 +00:00
|
|
|
|
return
|
2016-03-18 22:57:18 +00:00
|
|
|
|
}
|
2016-03-18 22:02:12 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
response.action = params.action === common.ACTIONS.ANNOUNCE ? 'announce' : 'scrape'
|
2015-12-05 08:18:52 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
let peers
|
|
|
|
|
if (response.action === 'announce') {
|
|
|
|
|
peers = response.peers
|
|
|
|
|
delete response.peers
|
2015-03-27 03:19:06 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
if (!socket.infoHashes.includes(params.info_hash)) {
|
|
|
|
|
socket.infoHashes.push(params.info_hash)
|
|
|
|
|
}
|
2015-03-27 03:19:06 +00:00
|
|
|
|
|
2023-05-26 16:54:30 +00:00
|
|
|
|
response.info_hash = hex2bin(params.info_hash)
|
2015-03-27 03:19:06 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
// WebSocket tracker should have a shorter interval – default: 2 minutes
|
2018-10-03 09:49:45 +00:00
|
|
|
|
response.interval = Math.ceil(this.intervalMs / 1000 / 5)
|
2018-10-02 16:26:35 +00:00
|
|
|
|
}
|
2015-03-27 03:19:06 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
// Skip sending update back for 'answer' announce messages – not needed
|
|
|
|
|
if (!params.answer) {
|
|
|
|
|
socket.send(JSON.stringify(response), socket.onSend)
|
|
|
|
|
debug('sent response %s to %s', JSON.stringify(response), params.peer_id)
|
|
|
|
|
}
|
2016-01-03 18:50:23 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
if (Array.isArray(params.offers)) {
|
|
|
|
|
debug('got %s offers from %s', params.offers.length, params.peer_id)
|
|
|
|
|
debug('got %s peers from swarm %s', peers.length, params.info_hash)
|
|
|
|
|
peers.forEach((peer, i) => {
|
|
|
|
|
peer.socket.send(JSON.stringify({
|
|
|
|
|
action: 'announce',
|
|
|
|
|
offer: params.offers[i].offer,
|
|
|
|
|
offer_id: params.offers[i].offer_id,
|
2023-05-26 16:54:30 +00:00
|
|
|
|
peer_id: hex2bin(params.peer_id),
|
|
|
|
|
info_hash: hex2bin(params.info_hash)
|
2018-10-02 16:26:35 +00:00
|
|
|
|
}), peer.socket.onSend)
|
|
|
|
|
debug('sent offer to %s from %s', peer.peerId, params.peer_id)
|
|
|
|
|
})
|
|
|
|
|
}
|
2015-03-27 03:19:06 +00:00
|
|
|
|
|
2018-10-03 09:49:45 +00:00
|
|
|
|
const done = () => {
|
|
|
|
|
// emit event once the announce is fully "processed"
|
|
|
|
|
if (params.action === common.ACTIONS.ANNOUNCE) {
|
|
|
|
|
this.emit(common.EVENT_NAMES[params.event], params.peer_id, params)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
if (params.answer) {
|
|
|
|
|
debug('got answer %s from %s', JSON.stringify(params.answer), params.peer_id)
|
2015-03-27 03:19:06 +00:00
|
|
|
|
|
2018-10-03 09:49:45 +00:00
|
|
|
|
this.getSwarm(params.info_hash, (err, swarm) => {
|
|
|
|
|
if (this.destroyed) return
|
|
|
|
|
if (err) return this.emit('warning', err)
|
2018-10-02 16:26:35 +00:00
|
|
|
|
if (!swarm) {
|
2018-10-03 09:49:45 +00:00
|
|
|
|
return this.emit('warning', new Error('no swarm with that `info_hash`'))
|
2018-10-02 16:26:35 +00:00
|
|
|
|
}
|
|
|
|
|
// Mark the destination peer as recently used in cache
|
|
|
|
|
const toPeer = swarm.peers.get(params.to_peer_id)
|
|
|
|
|
if (!toPeer) {
|
2018-10-03 09:49:45 +00:00
|
|
|
|
return this.emit('warning', new Error('no peer with that `to_peer_id`'))
|
2018-10-02 16:26:35 +00:00
|
|
|
|
}
|
2015-03-27 03:19:06 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
toPeer.socket.send(JSON.stringify({
|
|
|
|
|
action: 'announce',
|
|
|
|
|
answer: params.answer,
|
|
|
|
|
offer_id: params.offer_id,
|
2023-05-26 16:54:30 +00:00
|
|
|
|
peer_id: hex2bin(params.peer_id),
|
|
|
|
|
info_hash: hex2bin(params.info_hash)
|
2018-10-02 16:26:35 +00:00
|
|
|
|
}), toPeer.socket.onSend)
|
|
|
|
|
debug('sent answer to %s from %s', toPeer.peerId, params.peer_id)
|
2017-02-03 01:40:36 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
done()
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
done()
|
|
|
|
|
}
|
2017-02-03 01:40:36 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
_onWebSocketSend (socket, err) {
|
2018-10-02 16:33:00 +00:00
|
|
|
|
if (err) this._onWebSocketError(socket, err)
|
2018-10-02 16:26:35 +00:00
|
|
|
|
}
|
2017-02-03 01:40:36 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
_onWebSocketClose (socket) {
|
|
|
|
|
debug('websocket close %s', socket.peerId)
|
|
|
|
|
socket.destroyed = true
|
2017-02-03 01:40:36 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
if (socket.peerId) {
|
|
|
|
|
socket.infoHashes.slice(0).forEach(infoHash => {
|
2018-10-02 16:33:00 +00:00
|
|
|
|
const swarm = this.torrents[infoHash]
|
2018-10-02 16:26:35 +00:00
|
|
|
|
if (swarm) {
|
|
|
|
|
swarm.announce({
|
|
|
|
|
type: 'ws',
|
|
|
|
|
event: 'stopped',
|
|
|
|
|
numwant: 0,
|
|
|
|
|
peer_id: socket.peerId
|
2021-07-22 01:07:06 +00:00
|
|
|
|
})
|
2018-10-02 16:26:35 +00:00
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
2017-02-03 01:40:36 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
// ignore all future errors
|
|
|
|
|
socket.onSend = noop
|
|
|
|
|
socket.on('error', noop)
|
|
|
|
|
|
|
|
|
|
socket.peerId = null
|
|
|
|
|
socket.infoHashes = null
|
2017-02-03 01:40:36 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
if (typeof socket.onMessageBound === 'function') {
|
|
|
|
|
socket.removeListener('message', socket.onMessageBound)
|
|
|
|
|
}
|
|
|
|
|
socket.onMessageBound = null
|
|
|
|
|
|
|
|
|
|
if (typeof socket.onErrorBound === 'function') {
|
|
|
|
|
socket.removeListener('error', socket.onErrorBound)
|
|
|
|
|
}
|
|
|
|
|
socket.onErrorBound = null
|
|
|
|
|
|
|
|
|
|
if (typeof socket.onCloseBound === 'function') {
|
|
|
|
|
socket.removeListener('close', socket.onCloseBound)
|
|
|
|
|
}
|
|
|
|
|
socket.onCloseBound = null
|
2017-02-03 01:40:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
_onWebSocketError (socket, err) {
|
|
|
|
|
debug('websocket error %s', err.message || err)
|
2018-10-02 16:33:00 +00:00
|
|
|
|
this.emit('warning', err)
|
|
|
|
|
this._onWebSocketClose(socket)
|
2018-10-02 16:26:35 +00:00
|
|
|
|
}
|
2017-02-03 01:40:36 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
_onRequest (params, cb) {
|
|
|
|
|
if (params && params.action === common.ACTIONS.CONNECT) {
|
|
|
|
|
cb(null, { action: common.ACTIONS.CONNECT })
|
|
|
|
|
} else if (params && params.action === common.ACTIONS.ANNOUNCE) {
|
2018-10-02 16:33:00 +00:00
|
|
|
|
this._onAnnounce(params, cb)
|
2018-10-02 16:26:35 +00:00
|
|
|
|
} else if (params && params.action === common.ACTIONS.SCRAPE) {
|
2018-10-02 16:33:00 +00:00
|
|
|
|
this._onScrape(params, cb)
|
2018-10-02 16:26:35 +00:00
|
|
|
|
} else {
|
|
|
|
|
cb(new Error('Invalid action'))
|
|
|
|
|
}
|
2014-12-09 01:35:05 +00:00
|
|
|
|
}
|
2014-11-26 07:27:02 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
_onAnnounce (params, cb) {
|
|
|
|
|
const self = this
|
2018-04-30 04:20:51 +00:00
|
|
|
|
|
2018-10-03 09:49:45 +00:00
|
|
|
|
if (this._filter) {
|
|
|
|
|
this._filter(params.info_hash, params, err => {
|
2018-10-02 16:26:35 +00:00
|
|
|
|
// Presence of `err` means that this announce request is disallowed
|
|
|
|
|
if (err) return cb(err)
|
2015-03-27 03:19:06 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
getOrCreateSwarm((err, swarm) => {
|
|
|
|
|
if (err) return cb(err)
|
|
|
|
|
announce(swarm)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
getOrCreateSwarm((err, swarm) => {
|
2018-04-30 04:17:57 +00:00
|
|
|
|
if (err) return cb(err)
|
|
|
|
|
announce(swarm)
|
2015-03-27 03:19:06 +00:00
|
|
|
|
})
|
2018-10-02 16:26:35 +00:00
|
|
|
|
}
|
2018-04-30 04:20:51 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
// Get existing swarm, or create one if one does not exist
|
|
|
|
|
function getOrCreateSwarm (cb) {
|
|
|
|
|
self.getSwarm(params.info_hash, (err, swarm) => {
|
2018-04-30 04:17:57 +00:00
|
|
|
|
if (err) return cb(err)
|
2018-10-02 16:26:35 +00:00
|
|
|
|
if (swarm) return cb(null, swarm)
|
|
|
|
|
self.createSwarm(params.info_hash, (err, swarm) => {
|
|
|
|
|
if (err) return cb(err)
|
|
|
|
|
cb(null, swarm)
|
|
|
|
|
})
|
2018-04-30 04:17:57 +00:00
|
|
|
|
})
|
2018-10-02 16:26:35 +00:00
|
|
|
|
}
|
2015-07-05 00:13:07 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
function announce (swarm) {
|
|
|
|
|
if (!params.event || params.event === 'empty') params.event = 'update'
|
|
|
|
|
swarm.announce(params, (err, response) => {
|
|
|
|
|
if (err) return cb(err)
|
2015-07-05 00:13:07 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
if (!response.action) response.action = common.ACTIONS.ANNOUNCE
|
|
|
|
|
if (!response.interval) response.interval = Math.ceil(self.intervalMs / 1000)
|
|
|
|
|
|
|
|
|
|
if (params.compact === 1) {
|
|
|
|
|
const peers = response.peers
|
|
|
|
|
|
|
|
|
|
// Find IPv4 peers
|
2021-06-15 01:54:41 +00:00
|
|
|
|
response.peers = string2compact(peers.filter(peer => common.IPV4_RE.test(peer.ip)).map(peer => `${peer.ip}:${peer.port}`))
|
2018-10-02 16:26:35 +00:00
|
|
|
|
// Find IPv6 peers
|
2021-06-15 01:54:41 +00:00
|
|
|
|
response.peers6 = string2compact(peers.filter(peer => common.IPV6_RE.test(peer.ip)).map(peer => `[${peer.ip}]:${peer.port}`))
|
2018-10-02 16:26:35 +00:00
|
|
|
|
} else if (params.compact === 0) {
|
|
|
|
|
// IPv6 peers are not separate for non-compact responses
|
2021-06-15 01:54:41 +00:00
|
|
|
|
response.peers = response.peers.map(peer => ({
|
2023-05-26 16:54:30 +00:00
|
|
|
|
'peer id': hex2bin(peer.peerId),
|
2021-06-15 01:54:41 +00:00
|
|
|
|
ip: peer.ip,
|
|
|
|
|
port: peer.port
|
|
|
|
|
}))
|
2018-10-02 16:26:35 +00:00
|
|
|
|
} // else, return full peer objects (used for websocket responses)
|
2015-07-05 00:13:07 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
cb(null, response)
|
|
|
|
|
})
|
|
|
|
|
}
|
2015-07-05 00:13:07 +00:00
|
|
|
|
}
|
2014-12-09 01:35:05 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
_onScrape (params, cb) {
|
|
|
|
|
if (params.info_hash == null) {
|
|
|
|
|
// if info_hash param is omitted, stats for all torrents are returned
|
|
|
|
|
// TODO: make this configurable!
|
2018-10-03 09:49:45 +00:00
|
|
|
|
params.info_hash = Object.keys(this.torrents)
|
2018-10-02 16:26:35 +00:00
|
|
|
|
}
|
2014-12-11 20:53:39 +00:00
|
|
|
|
|
2021-06-15 01:54:41 +00:00
|
|
|
|
series(params.info_hash.map(infoHash => cb => {
|
|
|
|
|
this.getSwarm(infoHash, (err, swarm) => {
|
|
|
|
|
if (err) return cb(err)
|
|
|
|
|
if (swarm) {
|
|
|
|
|
swarm.scrape(params, (err, scrapeInfo) => {
|
|
|
|
|
if (err) return cb(err)
|
|
|
|
|
cb(null, {
|
|
|
|
|
infoHash,
|
|
|
|
|
complete: (scrapeInfo && scrapeInfo.complete) || 0,
|
|
|
|
|
incomplete: (scrapeInfo && scrapeInfo.incomplete) || 0
|
2016-01-03 18:50:23 +00:00
|
|
|
|
})
|
2021-06-15 01:54:41 +00:00
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
cb(null, { infoHash, complete: 0, incomplete: 0 })
|
|
|
|
|
}
|
|
|
|
|
})
|
2018-10-02 16:26:35 +00:00
|
|
|
|
}), (err, results) => {
|
|
|
|
|
if (err) return cb(err)
|
|
|
|
|
|
|
|
|
|
const response = {
|
|
|
|
|
action: common.ACTIONS.SCRAPE,
|
|
|
|
|
files: {},
|
2018-10-03 09:49:45 +00:00
|
|
|
|
flags: { min_request_interval: Math.ceil(this.intervalMs / 1000) }
|
2018-10-02 16:26:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
results.forEach(result => {
|
2023-05-26 16:54:30 +00:00
|
|
|
|
response.files[hex2bin(result.infoHash)] = {
|
2018-10-02 16:26:35 +00:00
|
|
|
|
complete: result.complete || 0,
|
|
|
|
|
incomplete: result.incomplete || 0,
|
|
|
|
|
downloaded: result.complete || 0 // TODO: this only provides a lower-bound
|
2016-01-03 18:50:23 +00:00
|
|
|
|
}
|
|
|
|
|
})
|
2014-12-10 15:47:41 +00:00
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
cb(null, response)
|
2014-06-07 22:10:12 +00:00
|
|
|
|
})
|
2018-10-02 16:26:35 +00:00
|
|
|
|
}
|
2014-06-07 22:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 16:26:35 +00:00
|
|
|
|
Server.Swarm = Swarm
|
|
|
|
|
|
2014-12-09 01:35:05 +00:00
|
|
|
|
function makeUdpPacket (params) {
|
2018-10-02 16:26:35 +00:00
|
|
|
|
let packet
|
2014-12-09 01:35:05 +00:00
|
|
|
|
switch (params.action) {
|
2018-12-20 19:59:53 +00:00
|
|
|
|
case common.ACTIONS.CONNECT: {
|
2015-01-27 02:16:01 +00:00
|
|
|
|
packet = Buffer.concat([
|
2014-12-09 01:35:05 +00:00
|
|
|
|
common.toUInt32(common.ACTIONS.CONNECT),
|
|
|
|
|
common.toUInt32(params.transactionId),
|
|
|
|
|
params.connectionId
|
|
|
|
|
])
|
2015-01-27 02:16:01 +00:00
|
|
|
|
break
|
2018-12-20 19:59:53 +00:00
|
|
|
|
}
|
|
|
|
|
case common.ACTIONS.ANNOUNCE: {
|
2015-01-27 02:16:01 +00:00
|
|
|
|
packet = Buffer.concat([
|
2014-12-09 01:35:05 +00:00
|
|
|
|
common.toUInt32(common.ACTIONS.ANNOUNCE),
|
|
|
|
|
common.toUInt32(params.transactionId),
|
2015-01-05 18:55:58 +00:00
|
|
|
|
common.toUInt32(params.interval),
|
2014-12-09 01:35:05 +00:00
|
|
|
|
common.toUInt32(params.incomplete),
|
|
|
|
|
common.toUInt32(params.complete),
|
|
|
|
|
params.peers
|
|
|
|
|
])
|
2015-01-27 02:16:01 +00:00
|
|
|
|
break
|
2018-12-20 19:59:53 +00:00
|
|
|
|
}
|
|
|
|
|
case common.ACTIONS.SCRAPE: {
|
2018-12-20 19:52:05 +00:00
|
|
|
|
const scrapeResponse = [
|
2014-12-09 01:35:05 +00:00
|
|
|
|
common.toUInt32(common.ACTIONS.SCRAPE),
|
2015-05-02 00:36:07 +00:00
|
|
|
|
common.toUInt32(params.transactionId)
|
|
|
|
|
]
|
2018-10-02 16:26:35 +00:00
|
|
|
|
for (const infoHash in params.files) {
|
|
|
|
|
const file = params.files[infoHash]
|
2015-05-02 00:36:07 +00:00
|
|
|
|
scrapeResponse.push(
|
|
|
|
|
common.toUInt32(file.complete),
|
|
|
|
|
common.toUInt32(file.downloaded), // TODO: this only provides a lower-bound
|
|
|
|
|
common.toUInt32(file.incomplete)
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
packet = Buffer.concat(scrapeResponse)
|
2015-01-27 02:16:01 +00:00
|
|
|
|
break
|
2018-12-20 19:59:53 +00:00
|
|
|
|
}
|
|
|
|
|
case common.ACTIONS.ERROR: {
|
2015-01-27 02:16:01 +00:00
|
|
|
|
packet = Buffer.concat([
|
2014-12-09 01:35:05 +00:00
|
|
|
|
common.toUInt32(common.ACTIONS.ERROR),
|
|
|
|
|
common.toUInt32(params.transactionId || 0),
|
2016-05-30 06:12:23 +00:00
|
|
|
|
Buffer.from(String(params['failure reason']))
|
2014-12-09 01:35:05 +00:00
|
|
|
|
])
|
2015-01-27 02:16:01 +00:00
|
|
|
|
break
|
2018-12-20 19:59:53 +00:00
|
|
|
|
}
|
2015-01-27 02:16:01 +00:00
|
|
|
|
default:
|
2018-10-02 16:26:35 +00:00
|
|
|
|
throw new Error(`Action not implemented: ${params.action}`)
|
2015-01-29 02:18:47 +00:00
|
|
|
|
}
|
2015-01-27 02:16:01 +00:00
|
|
|
|
return packet
|
2014-12-09 01:35:05 +00:00
|
|
|
|
}
|
2015-03-27 03:19:06 +00:00
|
|
|
|
|
2021-01-28 03:27:39 +00:00
|
|
|
|
function isObject (obj) {
|
|
|
|
|
return typeof obj === 'object' && obj !== null
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-01 21:17:11 +00:00
|
|
|
|
function toNumber (x) {
|
|
|
|
|
x = Number(x)
|
|
|
|
|
return x >= 0 ? x : false
|
|
|
|
|
}
|
2016-03-19 05:35:01 +00:00
|
|
|
|
|
|
|
|
|
function noop () {}
|
2018-10-02 16:26:35 +00:00
|
|
|
|
|
2022-12-05 22:06:54 +00:00
|
|
|
|
export default Server
|