-
-
Notifications
You must be signed in to change notification settings - Fork 335
Web-based tracker statistics. Fix #74 #142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -120,6 +120,80 @@ function Server (opts) { | |
| self.ws.on('connection', function (socket) { self.onWebSocketConnection(socket) }) | ||
| } | ||
|
|
||
| if (opts.stats !== false) { | ||
| if (!self.http) { | ||
| self.http = http.createServer() | ||
| self.http.on('error', function (err) { self._onError(err) }) | ||
| self.http.on('listening', onListening) | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! This is a great way to do this. |
||
|
|
||
| // Http handler for '/stats' route | ||
| self.http.on('request', function (req, res, opts) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should just be |
||
| if (res.headersSent) return | ||
|
|
||
| var infoHashes = Object.keys(self.torrents) | ||
| var activeTorrents = 0 | ||
| var allPeers = {} | ||
|
|
||
| function countPeers (filterFunction) { | ||
| var count = 0 | ||
| var key | ||
|
|
||
| for (key in allPeers) { | ||
| if (allPeers.hasOwnProperty(key) && filterFunction(allPeers[key])) { | ||
| count++ | ||
| } | ||
| } | ||
|
|
||
| return count | ||
| } | ||
|
|
||
| if (req.method === 'GET' && req.url === '/stats') { | ||
| infoHashes.forEach(function (infoHash) { | ||
| var peers = self.torrents[infoHash].peers | ||
| var keys = Object.keys(peers) | ||
| if (keys.length > 0) activeTorrents++ | ||
|
|
||
| keys.forEach(function (peerId) { | ||
| if (!allPeers.hasOwnProperty(peerId)) { | ||
| allPeers[peerId] = { | ||
| ipv4: false, | ||
| ipv6: false, | ||
| seeder: false, | ||
| leecher: false | ||
| } | ||
| } | ||
| var peer = peers[peerId] | ||
| if (peer.ip.indexOf(':') >= 0) { | ||
| allPeers[peerId].ipv6 = true | ||
| } else { | ||
| allPeers[peerId].ipv4 = true | ||
| } | ||
| if (peer.complete) { | ||
| allPeers[peerId].seeder = true | ||
| } else { | ||
| allPeers[peerId].leecher = true | ||
| } | ||
| }) | ||
| }) | ||
|
|
||
| var isSeederOnly = function (peer) { return peer.seeder && peer.leecher === false } | ||
| var isLeecherOnly = function (peer) { return peer.leecher && peer.seeder === false } | ||
| var isSeederAndLeecher = function (peer) { return peer.seeder && peer.leecher } | ||
| var isIPv4 = function (peer) { return peer.ipv4 } | ||
| var isIPv6 = function (peer) { return peer.ipv6 } | ||
|
|
||
| res.end('<h1>' + infoHashes.length + ' torrents (' + activeTorrents + ' active)</h1>\n' + | ||
| '<h2>Connected Peers: ' + Object.keys(allPeers).length + '</h2>\n' + | ||
| '<h3>Peers Seeding Only: ' + countPeers(isSeederOnly) + '</h3>\n' + | ||
| '<h3>Peers Leeching Only: ' + countPeers(isLeecherOnly) + '</h3>\n' + | ||
| '<h3>Peers Seeding & Leeching: ' + countPeers(isSeederAndLeecher) + '</h3>\n' + | ||
| '<h3>IPv4 Peers: ' + countPeers(isIPv4) + '</h3>\n' + | ||
| '<h3>IPv6 Peers: ' + countPeers(isIPv6) + '</h3>\n') | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| var num = !!self.http + !!self.udp4 + !!self.udp6 | ||
| function onListening () { | ||
| num -= 1 | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be necessary. We can solve this with the default option to
minimist. I'll fix it in a follow up commit, and you can see how to use it.