/ bin / cmd.js
cmd.js
  1  #!/usr/bin/env node
  2  
  3  import minimist from 'minimist'
  4  import { Server } from '../index.js'
  5  
  6  const argv = minimist(process.argv.slice(2), {
  7    alias: {
  8      h: 'help',
  9      p: 'port',
 10      q: 'quiet',
 11      s: 'silent',
 12      v: 'version'
 13    },
 14    boolean: [
 15      'help',
 16      'http',
 17      'quiet',
 18      'silent',
 19      'trust-proxy',
 20      'udp',
 21      'version',
 22      'ws',
 23      'stats'
 24    ],
 25    string: [
 26      'http-hostname',
 27      'udp-hostname',
 28      'udp6-hostname'
 29    ],
 30    default: {
 31      port: 8000,
 32      stats: true
 33    }
 34  })
 35  
 36  if (argv.version) {
 37    console.log(require('../package.json').version)
 38    process.exit(0)
 39  }
 40  
 41  if (argv.help) {
 42    console.log((() => {
 43    /*
 44    bittorrent-tracker - Start a bittorrent tracker server
 45  
 46    Usage:
 47      bittorrent-tracker [OPTIONS]
 48  
 49    If no --http, --udp, or --ws option is supplied, all tracker types will be started.
 50  
 51    Options:
 52      -p, --port [number]           change the port [default: 8000]
 53          --http-hostname [string]  change the http server hostname [default: '::']
 54          --udp-hostname [string]   change the udp hostname [default: '0.0.0.0']
 55          --udp6-hostname [string]  change the udp6 hostname [default: '::']
 56          --trust-proxy             trust 'x-forwarded-for' header from reverse proxy
 57          --interval                client announce interval (ms) [default: 600000]
 58          --http                    enable http server
 59          --udp                     enable udp server
 60          --ws                      enable websocket server
 61          --stats                   enable web-based statistics (default: true)
 62      -q, --quiet                   only show error output
 63      -s, --silent                  show no output
 64      -v, --version                 print the current version
 65  
 66    */
 67    }).toString().split(/\n/).slice(2, -2).join('\n'))
 68    process.exit(0)
 69  }
 70  
 71  if (argv.silent) argv.quiet = true
 72  
 73  const allFalsy = !argv.http && !argv.udp && !argv.ws
 74  
 75  argv.http = allFalsy || argv.http
 76  argv.udp = allFalsy || argv.udp
 77  argv.ws = allFalsy || argv.ws
 78  
 79  const server = new Server({
 80    http: argv.http,
 81    interval: argv.interval,
 82    stats: argv.stats,
 83    trustProxy: argv['trust-proxy'],
 84    udp: argv.udp,
 85    ws: argv.ws
 86  })
 87  
 88  server.on('error', err => {
 89    if (!argv.silent) console.error(`${new Date().toISOString()} ERROR: ${err.message}`)
 90  })
 91  server.on('warning', err => {
 92    if (!argv.quiet) console.log(`${new Date().toISOString()} WARNING: ${err.message}`)
 93  })
 94  server.on('update', addr => {
 95    if (!argv.quiet) console.log(`${new Date().toISOString()} update: ${addr}`)
 96  })
 97  server.on('complete', addr => {
 98    if (!argv.quiet) console.log(`${new Date().toISOString()} complete: ${addr}`)
 99  })
100  server.on('start', addr => {
101    if (!argv.quiet) console.log(`${new Date().toISOString()} start: ${addr}`)
102  })
103  server.on('stop', addr => {
104    if (!argv.quiet) console.log(`${new Date().toISOString()} stop: ${addr}`)
105  })
106  
107  const hostname = {
108    http: argv['http-hostname'],
109    udp4: argv['udp-hostname'],
110    udp6: argv['udp6-hostname']
111  }
112  
113  server.listen(argv.port, hostname, () => {
114    if (server.http && argv.http && !argv.quiet) {
115      const httpAddr = server.http.address()
116      const httpHost = httpAddr.address !== '::' ? httpAddr.address : 'localhost'
117      const httpPort = httpAddr.port
118      console.log(`${new Date().toISOString()} HTTP tracker: http://${httpHost}:${httpPort}/announce`)
119    }
120    if (server.udp && !argv.quiet) {
121      const udpAddr = server.udp.address()
122      const udpHost = udpAddr.address
123      const udpPort = udpAddr.port
124      console.log(`${new Date().toISOString()} UDP tracker: udp://${udpHost}:${udpPort}`)
125    }
126    if (server.udp6 && !argv.quiet) {
127      const udp6Addr = server.udp6.address()
128      const udp6Host = udp6Addr.address !== '::' ? udp6Addr.address : 'localhost'
129      const udp6Port = udp6Addr.port
130      console.log(`${new Date().toISOString()} UDP6 tracker: udp://${udp6Host}:${udp6Port}`)
131    }
132    if (server.ws && !argv.quiet) {
133      const wsAddr = server.http.address()
134      const wsHost = wsAddr.address !== '::' ? wsAddr.address : 'localhost'
135      const wsPort = wsAddr.port
136      console.log(`${new Date().toISOString()} WebSocket tracker: ws://${wsHost}:${wsPort}`)
137    }
138    if (server.http && argv.stats && !argv.quiet) {
139      const statsAddr = server.http.address()
140      const statsHost = statsAddr.address !== '::' ? statsAddr.address : 'localhost'
141      const statsPort = statsAddr.port
142      console.log(`${new Date().toISOString()} Tracker stats: http://${statsHost}:${statsPort}/stats`)
143    }
144  })