Skip to content

Commit d7a651f

Browse files
committed
Fixes for PR webtorrent#155
1 parent d6bdd0e commit d7a651f

File tree

3 files changed

+173
-29
lines changed

3 files changed

+173
-29
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ This module is used by [WebTorrent](http://webtorrent.io).
3232
- Robust and well-tested
3333
- Comprehensive test suite (runs entirely offline, so it's reliable)
3434
- Used by popular clients: [WebTorrent](http://webtorrent.io), [peerflix](https://github.com/mafintosh/peerflix), and [playback](https://mafintosh.github.io/playback/)
35-
- Tracker statistics available via web interface at `/stats`
35+
- Tracker statistics available via web interface at `/stats` or JSON data at `/stats.json`
3636

3737
Also see [bittorrent-dht](https://github.com/feross/bittorrent-dht).
3838

server.js

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ function Server (opts) {
150150
return count
151151
}
152152

153-
if (req.method === 'GET' && req.url === '/stats' || 'stats.json') {
153+
if (req.method === 'GET' && (req.url === '/stats' || req.url === '/stats.json')) {
154154
infoHashes.forEach(function (infoHash) {
155155
var peers = self.torrents[infoHash].peers
156156
var keys = Object.keys(peers)
@@ -185,36 +185,28 @@ function Server (opts) {
185185
var isIPv4 = function (peer) { return peer.ipv4 }
186186
var isIPv6 = function (peer) { return peer.ipv6 }
187187

188-
var torrents = infoHashes.length
189-
var peersAll = Object.keys(allPeers).length
190-
var peersSeederOnly = countPeers(isSeederOnly)
191-
var peersLeecherOnly = countPeers(isLeecherOnly)
192-
var peersSeederAndLeecher = countPeers(isSeederAndLeecher)
193-
var peersIPv4 = countPeers(isIPv4)
194-
var peersIPv6 = countPeers(isIPv6)
195-
196-
if (req.url === '/stats') {
197-
res.end('<h1>' + torrents + ' torrents (' + activeTorrents + ' active)</h1>\n' +
198-
'<h2>Connected Peers: ' + peersAll + '</h2>\n' +
199-
'<h3>Peers Seeding Only: ' + peersSeederOnly + '</h3>\n' +
200-
'<h3>Peers Leeching Only: ' + peersLeecherOnly + '</h3>\n' +
201-
'<h3>Peers Seeding & Leeching: ' + peersSeederAndLeecher + '</h3>\n' +
202-
'<h3>IPv4 Peers: ' + peersIPv4 + '</h3>\n' +
203-
'<h3>IPv6 Peers: ' + peersIPv6 + '</h3>\n')
188+
var stats = {
189+
torrents: infoHashes.length,
190+
activeTorrents: activeTorrents,
191+
peersAll: Object.keys(allPeers).length,
192+
peersSeederOnly: countPeers(isSeederOnly),
193+
peersLeecherOnly: countPeers(isLeecherOnly),
194+
peersSeederAndLeecher: countPeers(isSeederAndLeecher),
195+
peersIPv4: countPeers(isIPv4),
196+
peersIPv6: countPeers(isIPv6)
204197
}
205198

206-
if (req.url === '/stats.json') {
207-
res.write(JSON.stringify({
208-
torrents: torrents,
209-
activeTorrents: activeTorrents,
210-
peersAll: peersAll,
211-
peersSeederOnly: peersSeederOnly,
212-
peersLeecherOnly: peersLeecherOnly,
213-
peersSeederAndLeecher: peersSeederAndLeecher,
214-
peersIPv4: peersIPv4,
215-
peersIPv6: peersIPv6
216-
}))
199+
if (req.url === '/stats.json' || req.headers['content-type'] === 'application/json') {
200+
res.write(JSON.stringify(stats))
217201
res.end()
202+
} else if (req.url === '/stats') {
203+
res.end('<h1>' + stats.torrents + ' torrents (' + stats.activeTorrents + ' active)</h1>\n' +
204+
'<h2>Connected Peers: ' + stats.peersAll + '</h2>\n' +
205+
'<h3>Peers Seeding Only: ' + stats.peersSeederOnly + '</h3>\n' +
206+
'<h3>Peers Leeching Only: ' + stats.peersLeecherOnly + '</h3>\n' +
207+
'<h3>Peers Seeding & Leeching: ' + stats.peersSeederAndLeecher + '</h3>\n' +
208+
'<h3>IPv4 Peers: ' + stats.peersIPv4 + '</h3>\n' +
209+
'<h3>IPv6 Peers: ' + stats.peersIPv6 + '</h3>\n')
218210
}
219211
}
220212
})

test/stats.js

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
var Buffer = require('safe-buffer').Buffer
2+
var Client = require('../')
3+
var commonTest = require('./common')
4+
var fixtures = require('webtorrent-fixtures')
5+
var get = require('simple-get')
6+
var test = require('tape')
7+
8+
var peerId = Buffer.from('01234567890123456789')
9+
10+
function parseHtml (html) {
11+
var extractValue = new RegExp('[^v^h](\\d+)')
12+
var array = html.replace('torrents', '\n').split('\n').filter(function (line) {
13+
return line && line.trim().length > 0
14+
}).map(function (line) {
15+
var a = extractValue.exec(line)
16+
return parseInt(a[1])
17+
})
18+
var i = 0
19+
return {
20+
torrents: array[i++],
21+
activeTorrents: array[i++],
22+
peersAll: array[i++],
23+
peersSeederOnly: array[i++],
24+
peersLeecherOnly: array[i++],
25+
peersSeederAndLeecher: array[i++],
26+
peersIPv4: array[i++],
27+
peersIPv6: array[i]
28+
}
29+
}
30+
31+
test('server: get empty stats', function (t) {
32+
t.plan(11)
33+
34+
commonTest.createServer(t, 'http', function (server, announceUrl) {
35+
var url = announceUrl.replace('/announce', '/stats')
36+
37+
get.concat(url, function (err, res, data) {
38+
t.error(err)
39+
40+
var stats = parseHtml(data.toString())
41+
t.equal(res.statusCode, 200)
42+
t.equal(stats.torrents, 0)
43+
t.equal(stats.activeTorrents, 0)
44+
t.equal(stats.peersAll, 0)
45+
t.equal(stats.peersSeederOnly, 0)
46+
t.equal(stats.peersLeecherOnly, 0)
47+
t.equal(stats.peersSeederAndLeecher, 0)
48+
t.equal(stats.peersIPv4, 0)
49+
t.equal(stats.peersIPv6, 0)
50+
51+
server.close(function () { t.pass('server closed') })
52+
})
53+
})
54+
})
55+
56+
test('server: get empty stats with json header', function (t) {
57+
t.plan(11)
58+
59+
commonTest.createServer(t, 'http', function (server, announceUrl) {
60+
var opts = {
61+
url: announceUrl.replace('/announce', '/stats'),
62+
headers: {
63+
'content-type': 'json'
64+
},
65+
json: true
66+
}
67+
68+
get.concat(opts, function (err, res, stats) {
69+
t.error(err)
70+
71+
t.equal(res.statusCode, 200)
72+
t.equal(stats.torrents, 0)
73+
t.equal(stats.activeTorrents, 0)
74+
t.equal(stats.peersAll, 0)
75+
t.equal(stats.peersSeederOnly, 0)
76+
t.equal(stats.peersLeecherOnly, 0)
77+
t.equal(stats.peersSeederAndLeecher, 0)
78+
t.equal(stats.peersIPv4, 0)
79+
t.equal(stats.peersIPv6, 0)
80+
81+
server.close(function () { t.pass('server closed') })
82+
})
83+
})
84+
})
85+
86+
test('server: get empty stats on stats.json', function (t) {
87+
t.plan(11)
88+
89+
commonTest.createServer(t, 'http', function (server, announceUrl) {
90+
var opts = {
91+
url: announceUrl.replace('/announce', '/stats.json'),
92+
json: true
93+
}
94+
95+
get.concat(opts, function (err, res, stats) {
96+
t.error(err)
97+
98+
t.equal(res.statusCode, 200)
99+
t.equal(stats.torrents, 0)
100+
t.equal(stats.activeTorrents, 0)
101+
t.equal(stats.peersAll, 0)
102+
t.equal(stats.peersSeederOnly, 0)
103+
t.equal(stats.peersLeecherOnly, 0)
104+
t.equal(stats.peersSeederAndLeecher, 0)
105+
t.equal(stats.peersIPv4, 0)
106+
t.equal(stats.peersIPv6, 0)
107+
108+
server.close(function () { t.pass('server closed') })
109+
})
110+
})
111+
})
112+
113+
test('server: get leecher stats.json', function (t) {
114+
t.plan(10)
115+
116+
commonTest.createServer(t, 'http', function (server, announceUrl) {
117+
// announce a torrent to the tracker
118+
var client = new Client({
119+
infoHash: fixtures.leaves.parsedTorrent.infoHash,
120+
announce: announceUrl,
121+
peerId: peerId,
122+
port: 6881
123+
})
124+
client.on('error', function (err) { t.error(err) })
125+
client.on('warning', function (err) { t.error(err) })
126+
127+
client.start()
128+
129+
server.once('start', function () {
130+
var opts = {
131+
url: announceUrl.replace('/announce', '/stats.json'),
132+
json: true
133+
}
134+
135+
get.concat(opts, function (err, res, stats) {
136+
t.error(err)
137+
console.log(stats)
138+
139+
t.equal(res.statusCode, 200)
140+
t.equal(stats.torrents, 1)
141+
t.equal(stats.activeTorrents, 1)
142+
t.equal(stats.peersAll, 1)
143+
t.equal(stats.peersSeederOnly, 0)
144+
t.equal(stats.peersLeecherOnly, 1)
145+
t.equal(stats.peersSeederAndLeecher, 0)
146+
147+
client.destroy(function () { t.pass('client destroyed') })
148+
server.close(function () { t.pass('server closed') })
149+
})
150+
})
151+
})
152+
})

0 commit comments

Comments
 (0)