From 79068480370ab3f3f910316069518c8195c9c6d4 Mon Sep 17 00:00:00 2001 From: jakefb Date: Thu, 2 Jun 2016 14:27:14 +1200 Subject: [PATCH] Add json to tracker stats --- server.js | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/server.js b/server.js index 34778da..4043314 100644 --- a/server.js +++ b/server.js @@ -150,7 +150,7 @@ function Server (opts) { return count } - if (req.method === 'GET' && req.url === '/stats') { + if (req.method === 'GET' && req.url === '/stats' || 'stats.json') { infoHashes.forEach(function (infoHash) { var peers = self.torrents[infoHash].peers var keys = Object.keys(peers) @@ -185,13 +185,37 @@ function Server (opts) { var isIPv4 = function (peer) { return peer.ipv4 } var isIPv6 = function (peer) { return peer.ipv6 } - res.end('

' + infoHashes.length + ' torrents (' + activeTorrents + ' active)

\n' + - '

Connected Peers: ' + Object.keys(allPeers).length + '

\n' + - '

Peers Seeding Only: ' + countPeers(isSeederOnly) + '

\n' + - '

Peers Leeching Only: ' + countPeers(isLeecherOnly) + '

\n' + - '

Peers Seeding & Leeching: ' + countPeers(isSeederAndLeecher) + '

\n' + - '

IPv4 Peers: ' + countPeers(isIPv4) + '

\n' + - '

IPv6 Peers: ' + countPeers(isIPv6) + '

\n') + var torrents = infoHashes.length + var peersAll = Object.keys(allPeers).length + var peersSeederOnly = countPeers(isSeederOnly) + var peersLeecherOnly = countPeers(isLeecherOnly) + var peersSeederAndLeecher = countPeers(isSeederAndLeecher) + var peersIPv4 = countPeers(isIPv4) + var peersIPv6 = countPeers(isIPv6) + + if (req.url === '/stats') { + res.end('

' + torrents + ' torrents (' + activeTorrents + ' active)

\n' + + '

Connected Peers: ' + peersAll + '

\n' + + '

Peers Seeding Only: ' + peersSeederOnly + '

\n' + + '

Peers Leeching Only: ' + peersLeecherOnly + '

\n' + + '

Peers Seeding & Leeching: ' + peersSeederAndLeecher + '

\n' + + '

IPv4 Peers: ' + peersIPv4 + '

\n' + + '

IPv6 Peers: ' + peersIPv6 + '

\n') + } + + if (req.url === '/stats.json') { + res.write(JSON.stringify({ + torrents: torrents, + activeTorrents: activeTorrents, + peersAll: peersAll, + peersSeederOnly: peersSeederOnly, + peersLeecherOnly: peersLeecherOnly, + peersSeederAndLeecher: peersSeederAndLeecher, + peersIPv4: peersIPv4, + peersIPv6: peersIPv6 + })) + res.end() + } } }) }