add command line server

Fixes #26
This commit is contained in:
Feross Aboukhadijeh 2015-03-19 21:47:47 -07:00
parent 8f977ee7d3
commit 9ace2f5d3a
3 changed files with 102 additions and 5 deletions

95
bin/cmd.js Executable file
View File

@ -0,0 +1,95 @@
#!/usr/bin/env node
var minimist = require('minimist')
var Server = require('../').Server
var argv = minimist(process.argv.slice(2), {
alias: {
h: 'help',
p: 'port',
q: 'quiet',
s: 'silent',
v: 'version'
},
boolean: [
'help',
'http',
'quiet',
'silent',
'trust-proxy',
'udp',
'version'
],
default: {
http: true,
port: 8000,
udp: true
}
})
if (argv.version) {
console.log(require('../package.json').version)
process.exit(0)
}
if (argv.help) {
console.log(function () {
/*
bittorrent-tracker - Start a bittorrent tracker server
Usage:
bittorrent-tracker
Options:
-p, --port [number] change the port [default: 8000]
--trust-proxy trust 'x-forwarded-for' header from reverse proxy
--interval tell clients to announce on this interval (ms)
--http enable http server [default: true]
--udp enable udp server [default: true]
-q, --quiet only show error output
-s, --silent show no output
-v, --version print the current version
Please report bugs! https://github.com/feross/bittorrent-tracker/issues
*/
}.toString().split(/\n/).slice(2, -2).join('\n'))
process.exit(0)
}
if (argv.silent) argv.quiet = true
var server = new Server({
http: argv.http,
interval: argv.interval,
trustProxy: argv['trust-proxy'],
udp: argv.udp
})
server.on('error', function (err) {
if (!argv.silent) console.error('ERROR: ' + err.message)
})
server.on('warning', function (err) {
if (!argv.quiet) console.log('WARNING: ' + err.message)
})
server.on('update', function (addr) {
if (!argv.quiet) console.log('update: ' + addr)
})
server.on('complete', function (addr) {
if (!argv.quiet) console.log('complete: ' + addr)
})
server.on('start', function (addr) {
if (!argv.quiet) console.log('start: ' + addr)
})
server.on('stop', function (addr) {
if (!argv.quiet) console.log('stop: ' + addr)
})
server.listen(argv.port, function () {
if (server.http && !argv.quiet) {
console.log('http server listening on ' + server.http.address().port)
}
if (server.udp && !argv.quiet) {
console.log('udp server listening on ' + server.udp.address().port)
}
})

View File

@ -22,6 +22,7 @@
"hat": "0.0.3",
"inherits": "^2.0.1",
"ip": "^0.3.0",
"minimist": "^1.1.1",
"once": "^1.3.0",
"run-series": "^1.0.2",
"simple-get": "^1.3.0",

View File

@ -24,11 +24,12 @@ inherits(Server, EventEmitter)
* keep overall statistics about the torrent. The response includes a peer list that
* helps the client participate in the torrent.
*
* @param {Object} opts options
* @param {Number} opts.interval interval in ms that clients should announce on
* @param {Number} opts.trustProxy Trust 'x-forwarded-for' header from reverse proxy
* @param {boolean} opts.http Start an http server? (default: true)
* @param {boolean} opts.udp Start a udp server? (default: true)
* @param {Object} opts options object
* @param {Number} opts.interval tell clients to announce on this interval (ms)
* @param {Number} opts.trustProxy trust 'x-forwarded-for' header from reverse proxy
* @param {boolean} opts.http start an http server? (default: true)
* @param {boolean} opts.udp start a udp server? (default: true)
* @param {function} opts.filter black/whitelist fn for disallowing/allowing torrents
*/
function Server (opts) {
var self = this