mirror of
https://github.com/webtorrent/bittorrent-tracker.git
synced 2024-12-14 19:36:24 +00:00
Merge pull request #155 form jakefb/master
This commit is contained in:
commit
bbb8edcaa5
@ -32,7 +32,7 @@ This module is used by [WebTorrent](http://webtorrent.io).
|
|||||||
- Robust and well-tested
|
- Robust and well-tested
|
||||||
- Comprehensive test suite (runs entirely offline, so it's reliable)
|
- Comprehensive test suite (runs entirely offline, so it's reliable)
|
||||||
- Used by popular clients: [WebTorrent](http://webtorrent.io), [peerflix](https://github.com/mafintosh/peerflix), and [playback](https://mafintosh.github.io/playback/)
|
- Used by popular clients: [WebTorrent](http://webtorrent.io), [peerflix](https://github.com/mafintosh/peerflix), and [playback](https://mafintosh.github.io/playback/)
|
||||||
- Tracker statistics available via web interface at `/stats`
|
- Tracker statistics available via web interface at `/stats` or JSON data at `/stats.json`
|
||||||
|
|
||||||
Also see [bittorrent-dht](https://github.com/feross/bittorrent-dht).
|
Also see [bittorrent-dht](https://github.com/feross/bittorrent-dht).
|
||||||
|
|
||||||
|
32
server.js
32
server.js
@ -150,7 +150,7 @@ function Server (opts) {
|
|||||||
return count
|
return count
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.method === 'GET' && req.url === '/stats') {
|
if (req.method === 'GET' && (req.url === '/stats' || req.url === '/stats.json')) {
|
||||||
infoHashes.forEach(function (infoHash) {
|
infoHashes.forEach(function (infoHash) {
|
||||||
var peers = self.torrents[infoHash].peers
|
var peers = self.torrents[infoHash].peers
|
||||||
var keys = Object.keys(peers)
|
var keys = Object.keys(peers)
|
||||||
@ -185,13 +185,29 @@ function Server (opts) {
|
|||||||
var isIPv4 = function (peer) { return peer.ipv4 }
|
var isIPv4 = function (peer) { return peer.ipv4 }
|
||||||
var isIPv6 = function (peer) { return peer.ipv6 }
|
var isIPv6 = function (peer) { return peer.ipv6 }
|
||||||
|
|
||||||
res.end('<h1>' + infoHashes.length + ' torrents (' + activeTorrents + ' active)</h1>\n' +
|
var stats = {
|
||||||
'<h2>Connected Peers: ' + Object.keys(allPeers).length + '</h2>\n' +
|
torrents: infoHashes.length,
|
||||||
'<h3>Peers Seeding Only: ' + countPeers(isSeederOnly) + '</h3>\n' +
|
activeTorrents: activeTorrents,
|
||||||
'<h3>Peers Leeching Only: ' + countPeers(isLeecherOnly) + '</h3>\n' +
|
peersAll: Object.keys(allPeers).length,
|
||||||
'<h3>Peers Seeding & Leeching: ' + countPeers(isSeederAndLeecher) + '</h3>\n' +
|
peersSeederOnly: countPeers(isSeederOnly),
|
||||||
'<h3>IPv4 Peers: ' + countPeers(isIPv4) + '</h3>\n' +
|
peersLeecherOnly: countPeers(isLeecherOnly),
|
||||||
'<h3>IPv6 Peers: ' + countPeers(isIPv6) + '</h3>\n')
|
peersSeederAndLeecher: countPeers(isSeederAndLeecher),
|
||||||
|
peersIPv4: countPeers(isIPv4),
|
||||||
|
peersIPv6: countPeers(isIPv6)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.url === '/stats.json' || req.headers['content-type'] === 'application/json') {
|
||||||
|
res.write(JSON.stringify(stats))
|
||||||
|
res.end()
|
||||||
|
} else if (req.url === '/stats') {
|
||||||
|
res.end('<h1>' + stats.torrents + ' torrents (' + stats.activeTorrents + ' active)</h1>\n' +
|
||||||
|
'<h2>Connected Peers: ' + stats.peersAll + '</h2>\n' +
|
||||||
|
'<h3>Peers Seeding Only: ' + stats.peersSeederOnly + '</h3>\n' +
|
||||||
|
'<h3>Peers Leeching Only: ' + stats.peersLeecherOnly + '</h3>\n' +
|
||||||
|
'<h3>Peers Seeding & Leeching: ' + stats.peersSeederAndLeecher + '</h3>\n' +
|
||||||
|
'<h3>IPv4 Peers: ' + stats.peersIPv4 + '</h3>\n' +
|
||||||
|
'<h3>IPv6 Peers: ' + stats.peersIPv6 + '</h3>\n')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
152
test/stats.js
Normal file
152
test/stats.js
Normal file
@ -0,0 +1,152 @@
|
|||||||
|
var Buffer = require('safe-buffer').Buffer
|
||||||
|
var Client = require('../')
|
||||||
|
var commonTest = require('./common')
|
||||||
|
var fixtures = require('webtorrent-fixtures')
|
||||||
|
var get = require('simple-get')
|
||||||
|
var test = require('tape')
|
||||||
|
|
||||||
|
var peerId = Buffer.from('01234567890123456789')
|
||||||
|
|
||||||
|
function parseHtml (html) {
|
||||||
|
var extractValue = new RegExp('[^v^h](\\d+)')
|
||||||
|
var array = html.replace('torrents', '\n').split('\n').filter(function (line) {
|
||||||
|
return line && line.trim().length > 0
|
||||||
|
}).map(function (line) {
|
||||||
|
var a = extractValue.exec(line)
|
||||||
|
return parseInt(a[1])
|
||||||
|
})
|
||||||
|
var i = 0
|
||||||
|
return {
|
||||||
|
torrents: array[i++],
|
||||||
|
activeTorrents: array[i++],
|
||||||
|
peersAll: array[i++],
|
||||||
|
peersSeederOnly: array[i++],
|
||||||
|
peersLeecherOnly: array[i++],
|
||||||
|
peersSeederAndLeecher: array[i++],
|
||||||
|
peersIPv4: array[i++],
|
||||||
|
peersIPv6: array[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test('server: get empty stats', function (t) {
|
||||||
|
t.plan(11)
|
||||||
|
|
||||||
|
commonTest.createServer(t, 'http', function (server, announceUrl) {
|
||||||
|
var url = announceUrl.replace('/announce', '/stats')
|
||||||
|
|
||||||
|
get.concat(url, function (err, res, data) {
|
||||||
|
t.error(err)
|
||||||
|
|
||||||
|
var stats = parseHtml(data.toString())
|
||||||
|
t.equal(res.statusCode, 200)
|
||||||
|
t.equal(stats.torrents, 0)
|
||||||
|
t.equal(stats.activeTorrents, 0)
|
||||||
|
t.equal(stats.peersAll, 0)
|
||||||
|
t.equal(stats.peersSeederOnly, 0)
|
||||||
|
t.equal(stats.peersLeecherOnly, 0)
|
||||||
|
t.equal(stats.peersSeederAndLeecher, 0)
|
||||||
|
t.equal(stats.peersIPv4, 0)
|
||||||
|
t.equal(stats.peersIPv6, 0)
|
||||||
|
|
||||||
|
server.close(function () { t.pass('server closed') })
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('server: get empty stats with json header', function (t) {
|
||||||
|
t.plan(11)
|
||||||
|
|
||||||
|
commonTest.createServer(t, 'http', function (server, announceUrl) {
|
||||||
|
var opts = {
|
||||||
|
url: announceUrl.replace('/announce', '/stats'),
|
||||||
|
headers: {
|
||||||
|
'content-type': 'json'
|
||||||
|
},
|
||||||
|
json: true
|
||||||
|
}
|
||||||
|
|
||||||
|
get.concat(opts, function (err, res, stats) {
|
||||||
|
t.error(err)
|
||||||
|
|
||||||
|
t.equal(res.statusCode, 200)
|
||||||
|
t.equal(stats.torrents, 0)
|
||||||
|
t.equal(stats.activeTorrents, 0)
|
||||||
|
t.equal(stats.peersAll, 0)
|
||||||
|
t.equal(stats.peersSeederOnly, 0)
|
||||||
|
t.equal(stats.peersLeecherOnly, 0)
|
||||||
|
t.equal(stats.peersSeederAndLeecher, 0)
|
||||||
|
t.equal(stats.peersIPv4, 0)
|
||||||
|
t.equal(stats.peersIPv6, 0)
|
||||||
|
|
||||||
|
server.close(function () { t.pass('server closed') })
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('server: get empty stats on stats.json', function (t) {
|
||||||
|
t.plan(11)
|
||||||
|
|
||||||
|
commonTest.createServer(t, 'http', function (server, announceUrl) {
|
||||||
|
var opts = {
|
||||||
|
url: announceUrl.replace('/announce', '/stats.json'),
|
||||||
|
json: true
|
||||||
|
}
|
||||||
|
|
||||||
|
get.concat(opts, function (err, res, stats) {
|
||||||
|
t.error(err)
|
||||||
|
|
||||||
|
t.equal(res.statusCode, 200)
|
||||||
|
t.equal(stats.torrents, 0)
|
||||||
|
t.equal(stats.activeTorrents, 0)
|
||||||
|
t.equal(stats.peersAll, 0)
|
||||||
|
t.equal(stats.peersSeederOnly, 0)
|
||||||
|
t.equal(stats.peersLeecherOnly, 0)
|
||||||
|
t.equal(stats.peersSeederAndLeecher, 0)
|
||||||
|
t.equal(stats.peersIPv4, 0)
|
||||||
|
t.equal(stats.peersIPv6, 0)
|
||||||
|
|
||||||
|
server.close(function () { t.pass('server closed') })
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('server: get leecher stats.json', function (t) {
|
||||||
|
t.plan(10)
|
||||||
|
|
||||||
|
commonTest.createServer(t, 'http', function (server, announceUrl) {
|
||||||
|
// announce a torrent to the tracker
|
||||||
|
var client = new Client({
|
||||||
|
infoHash: fixtures.leaves.parsedTorrent.infoHash,
|
||||||
|
announce: announceUrl,
|
||||||
|
peerId: peerId,
|
||||||
|
port: 6881
|
||||||
|
})
|
||||||
|
client.on('error', function (err) { t.error(err) })
|
||||||
|
client.on('warning', function (err) { t.error(err) })
|
||||||
|
|
||||||
|
client.start()
|
||||||
|
|
||||||
|
server.once('start', function () {
|
||||||
|
var opts = {
|
||||||
|
url: announceUrl.replace('/announce', '/stats.json'),
|
||||||
|
json: true
|
||||||
|
}
|
||||||
|
|
||||||
|
get.concat(opts, function (err, res, stats) {
|
||||||
|
t.error(err)
|
||||||
|
console.log(stats)
|
||||||
|
|
||||||
|
t.equal(res.statusCode, 200)
|
||||||
|
t.equal(stats.torrents, 1)
|
||||||
|
t.equal(stats.activeTorrents, 1)
|
||||||
|
t.equal(stats.peersAll, 1)
|
||||||
|
t.equal(stats.peersSeederOnly, 0)
|
||||||
|
t.equal(stats.peersLeecherOnly, 1)
|
||||||
|
t.equal(stats.peersSeederAndLeecher, 0)
|
||||||
|
|
||||||
|
client.destroy(function () { t.pass('client destroyed') })
|
||||||
|
server.close(function () { t.pass('server closed') })
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
Loading…
Reference in New Issue
Block a user