Skip to content

Commit

Permalink
support --blocklist option
Browse files Browse the repository at this point in the history
cc @fisch0920
  • Loading branch information
feross committed Aug 17, 2014
1 parent 94407de commit 9a029d1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion bin/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function getRuntime () {

var client = new WebTorrent({
list: argv.list,
blocklist: argv.blocklist, // TODO: handle this option in bittorrent-client
blocklist: argv.blocklist,
port: argv.port
})
.on('error', errorAndExit)
Expand Down
18 changes: 13 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ var FSStorage = require('./lib/fs-storage')
var http = require('http')
var inherits = require('inherits')
var mime = require('mime')
var once = require('once')
var parallel = require('run-parallel')
var pump = require('pump')
var rangeParser = require('range-parser')
Expand All @@ -23,14 +22,12 @@ function WebTorrent (opts) {
var self = this
opts = opts || {}
debug('new webtorrent')
if (opts.blocklist) opts.blocklist = parseBlocklist(opts.blocklist)

Client.call(self, opts)

self.listening = false

if (opts.list) {
return
}
if (opts.list) return

if (opts.port !== false) {
// start http server
Expand Down Expand Up @@ -205,5 +202,16 @@ WebTorrent.prototype._onRequest = function (req, res) {
if (req.method === 'HEAD') res.end()
pump(file.createReadStream(range), res)
}
}

// TODO: support gzipped files
var blocklistRe = /^\s*[^#].*?\s*:\s*([a-f0-9.:]+?)\s*-\s*([a-f0-9.:]+?)\s*$/
function parseBlocklist (filename) {
var blocklistData = fs.readFileSync(filename, 'utf8')
var blocklist = []
blocklistData.split('\n').forEach(function (line) {
var match = blocklistRe.exec(line)
if (match) blocklist.push({ start: match[1], end: match[2] })
})
return blocklist
}

0 comments on commit 9a029d1

Please sign in to comment.