-
Notifications
You must be signed in to change notification settings - Fork 0
/
oakstreaming-tracker.js
80 lines (55 loc) · 2.83 KB
/
oakstreaming-tracker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
var WebtorrentTracker = require('bittorrent-tracker').Server;
// An OakStreaming tracker is simply a specifically configured version of Feross's bittorrent-tracker Node.js module
// (https://github.com/feross/bittorrent-tracker).
// The purpose of an OakStreaming tracker is to establish WebRTC connections between WebTorrent instances
// of different OakStreaming instances. Every OakStreaming instance instantiates up to one WebTorrent instance.
var tracker = new WebtorrentTracker({
// This server should only connect to clients via WebSocket connections because OakStreaming instances support
// only this kind of connections to trackers.
udp: false,
http: false,
ws: true,
stats: true,
// Blacklist/whitelist function for allowing/disallowing torrents.
filter: function (infoHash, params, cb) {
// Blacklist/whitelist function for allowing/disallowing torrents. If this option is
// omitted, all torrents are allowed. It is possible to interface with a database or
// external system before deciding to allow/deny, because this function is async.
// It is possible to block by peer id (whitelisting torrent clients) or by secret
// key (private trackers). Full access to the original HTTP/UDP request parameters
// are available in `params`.
// For our use case, we allow all torrents.
cb(null)
// In addition to returning a boolean (null for allowed, an Error object for disallowed).
}
});
tracker.on('error', function (err) {
// Fatal server error!
console.log("Error: " + err.message)
})
tracker.on('warning', function (err) {
// Client sent bad data. Probably not a problem, just a buggy client.
console.log("Warning: " + err.message)
})
tracker.on('listening', function (){
// Fires when the WebSocket server is listening.
console.log('The OakStreaming tracker is running and listens on WebSocket port: ' + tracker.ws.address().port);
});
// Give the torrent tracker the command to listen on a specific hostname and port.
// tracker.listen(port, hostname, onlistening)
tracker.listen(8085, "localhost", function(){});
// This event listener is fired when a client connects to the tracker:
tracker.on('start', function (addr) {
console.log('Got start message from ' + addr)
})
tracker.on('complete', function (addr) {console.log("complete");})
tracker.on('update', function (addr) {console.log("update");})
tracker.on('stop', function (addr) {console.log("The client with the address " + addr + " disconnected")});
// Get info hashes for all torrents the torrent tracker manages.
//Object.keys(server.torrents)
// Get the number of seeders for a particular torrent.
//server.torrents[infoHash].complete
// Get the number of leechers for a particular torrent.
//server.torrents[infoHash].incomplete
// Get the peers who are in a particular torrent swarm.
//server.torrents[infoHash].peers