diff options
author | fisch0920 <fisch0920@gmail.com> | 2014-05-16 13:08:01 +0400 |
---|---|---|
committer | fisch0920 <fisch0920@gmail.com> | 2014-05-16 13:08:01 +0400 |
commit | 196d9eee696f101b664c6c64e2531080ac24576d (patch) | |
tree | f9bea6f6d789823c38f44aa991e6c331733f646d /index.js | |
parent | d6d66ecccc175351a1a4af6aad3c3f8362f22173 (diff) |
support peer blocklist
Diffstat (limited to 'index.js')
-rw-r--r-- | index.js | 26 |
1 files changed, 24 insertions, 2 deletions
@@ -17,8 +17,9 @@ inherits(WebTorrent, Client) function WebTorrent (opts) { var self = this + opts = opts || {} + if (opts.blocklist) opts.blocklist = parseBlocklist(opts.blocklist) Client.call(self, opts) - if (!opts) opts = {} if (opts.list) { return @@ -88,7 +89,6 @@ WebTorrent.prototype._onTorrent = function (torrent) { var self = this // if no index specified, use largest file - // TODO: support torrent index selection correctly -- this doesn't work yet if (typeof torrent.index !== 'number') { var largestFile = torrent.files.reduce(function (a, b) { return a.length > b.length ? a : b @@ -158,3 +158,25 @@ WebTorrent.prototype._onRequest = function (req, res) { } pump(file.createReadStream(range), res) } + +// +// HELPER METHODS +// + +function parseBlocklist (filename) { + // TODO: support gzipped files + var blocklistData = fs.readFileSync(filename, { encoding: 'utf8' }) + var blocklist = [] + blocklistData.split('\n').forEach(function(line) { + var match = null + if ((match = /^\s*([^#].*)\s*:\s*([a-f0-9.:]+?)\s*-\s*([a-f0-9.:]+?)\s*$/.exec(line))) { + blocklist.push({ + reason: match[1], + startAddress: match[2], + endAddress: match[3] + }) + } + }) + return blocklist +} + |