Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions bin/cmd.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env node

var minimist = require('minimist')
var Server = require('../').Server
const minimist = require('minimist')
const Server = require('../').Server

var argv = minimist(process.argv.slice(2), {
const argv = minimist(process.argv.slice(2), {
alias: {
h: 'help',
p: 'port',
Expand Down Expand Up @@ -70,13 +70,13 @@ if (argv.help) {

if (argv.silent) argv.quiet = true

var allFalsy = !argv.http && !argv.udp && !argv.ws
const allFalsy = !argv.http && !argv.udp && !argv.ws

argv.http = allFalsy || argv.http
argv.udp = allFalsy || argv.udp
argv.ws = allFalsy || argv.ws

var server = new Server({
const server = new Server({
http: argv.http,
interval: argv.interval,
stats: argv.stats,
Expand Down Expand Up @@ -104,41 +104,41 @@ server.on('stop', function (addr) {
if (!argv.quiet) console.log('stop: ' + addr)
})

var hostname = {
const hostname = {
http: argv['http-hostname'],
udp4: argv['udp-hostname'],
udp6: argv['udp6-hostname']
}

server.listen(argv.port, hostname, function () {
if (server.http && argv.http && !argv.quiet) {
var httpAddr = server.http.address()
var httpHost = httpAddr.address !== '::' ? httpAddr.address : 'localhost'
var httpPort = httpAddr.port
const httpAddr = server.http.address()
const httpHost = httpAddr.address !== '::' ? httpAddr.address : 'localhost'
const httpPort = httpAddr.port
console.log('HTTP tracker: http://' + httpHost + ':' + httpPort + '/announce')
}
if (server.udp && !argv.quiet) {
var udpAddr = server.udp.address()
var udpHost = udpAddr.address
var udpPort = udpAddr.port
const udpAddr = server.udp.address()
const udpHost = udpAddr.address
const udpPort = udpAddr.port
console.log('UDP tracker: udp://' + udpHost + ':' + udpPort)
}
if (server.udp6 && !argv.quiet) {
var udp6Addr = server.udp6.address()
var udp6Host = udp6Addr.address !== '::' ? udp6Addr.address : 'localhost'
var udp6Port = udp6Addr.port
const udp6Addr = server.udp6.address()
const udp6Host = udp6Addr.address !== '::' ? udp6Addr.address : 'localhost'
const udp6Port = udp6Addr.port
console.log('UDP6 tracker: udp://' + udp6Host + ':' + udp6Port)
}
if (server.ws && !argv.quiet) {
var wsAddr = server.http.address()
var wsHost = wsAddr.address !== '::' ? wsAddr.address : 'localhost'
var wsPort = wsAddr.port
const wsAddr = server.http.address()
const wsHost = wsAddr.address !== '::' ? wsAddr.address : 'localhost'
const wsPort = wsAddr.port
console.log('WebSocket tracker: ws://' + wsHost + ':' + wsPort)
}
if (server.http && argv.stats && !argv.quiet) {
var statsAddr = server.http.address()
var statsHost = statsAddr.address !== '::' ? statsAddr.address : 'localhost'
var statsPort = statsAddr.port
const statsAddr = server.http.address()
const statsHost = statsAddr.address !== '::' ? statsAddr.address : 'localhost'
const statsPort = statsAddr.port
console.log('Tracker stats: http://' + statsHost + ':' + statsPort + '/stats')
}
})
14 changes: 7 additions & 7 deletions examples/express-embed/server.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
#!/usr/bin/env node

var Server = require('../..').Server
var express = require('express')
var app = express()
const Server = require('../..').Server
const express = require('express')
const app = express()

// https://wiki.theory.org/BitTorrentSpecification#peer_id
var whitelist = {
const whitelist = {
UT: true // uTorrent
}

var server = new Server({
const server = new Server({
http: false, // we do our own
udp: false, // not interested
ws: false, // not interested
filter: function (params) {
// black/whitelist for disallowing/allowing specific clients [default=allow all]
// this example only allows the uTorrent client
var client = params.peer_id[1] + params.peer_id[2]
const client = params.peer_id[1] + params.peer_id[2]
return whitelist[client]
}
})

var onHttpRequest = server.onHttpRequest.bind(server)
const onHttpRequest = server.onHttpRequest.bind(server)
app.get('/announce', onHttpRequest)
app.get('/scrape', onHttpRequest)

Expand Down
2 changes: 1 addition & 1 deletion lib/client/http-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class HTTPTracker extends Tracker {

// Otherwise, wait a short time for pending requests to complete, then force
// destroy them.
var timeout = setTimeout(destroyCleanup, common.DESTROY_TIMEOUT)
let timeout = setTimeout(destroyCleanup, common.DESTROY_TIMEOUT)

// But, if all pending requests complete before the timeout fires, do cleanup
// right away.
Expand Down
2 changes: 1 addition & 1 deletion lib/client/udp-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class UDPTracker extends Tracker {

// Otherwise, wait a short time for pending requests to complete, then force
// destroy them.
var timeout = setTimeout(destroyCleanup, common.DESTROY_TIMEOUT)
let timeout = setTimeout(destroyCleanup, common.DESTROY_TIMEOUT)

// But, if all pending requests complete before the timeout fires, do cleanup
// right away.
Expand Down
2 changes: 1 addition & 1 deletion lib/client/websocket-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class WebSocketTracker extends Tracker {

// Otherwise, wait a short time for potential responses to come in from the
// server, then force close the socket.
var timeout = setTimeout(destroyCleanup, common.DESTROY_TIMEOUT)
let timeout = setTimeout(destroyCleanup, common.DESTROY_TIMEOUT)

// But, if a response comes from the server before the timeout fires, do cleanup
// right away.
Expand Down
6 changes: 3 additions & 3 deletions lib/common-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* These are separate from common.js so they can be skipped when bundling for the browser.
*/

var querystring = require('querystring')
const querystring = require('querystring')

exports.IPV4_RE = /^[\d.]+$/
exports.IPV6_RE = /^[\da-fA-F:]+$/
Expand Down Expand Up @@ -38,7 +38,7 @@ exports.REQUEST_TIMEOUT = 15000
exports.DESTROY_TIMEOUT = 1000

function toUInt32 (n) {
var buf = Buffer.allocUnsafe(4)
const buf = Buffer.allocUnsafe(4)
buf.writeUInt32BE(n, 0)
return buf
}
Expand All @@ -61,7 +61,7 @@ exports.querystringParse = function (q) {
* @return {string}
*/
exports.querystringStringify = function (obj) {
var ret = querystring.stringify(obj, null, null, { encodeURIComponent: escape })
let ret = querystring.stringify(obj, null, null, { encodeURIComponent: escape })
ret = ret.replace(/[@*/+]/g, function (char) {
// `escape` doesn't encode the characters @*/+ so we do it manually
return '%' + char.charCodeAt(0).toString(16).toUpperCase()
Expand Down
2 changes: 1 addition & 1 deletion lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ exports.hexToBinary = function (str) {
return Buffer.from(str, 'hex').toString('binary')
}

var config = require('./common-node')
const config = require('./common-node')
Object.assign(exports, config)
6 changes: 3 additions & 3 deletions lib/server/parse-http.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module.exports = parseHttpRequest

var common = require('../common')
const common = require('../common')

function parseHttpRequest (req, opts) {
if (!opts) opts = {}
var s = req.url.split('?')
var params = common.querystringParse(s[1])
const s = req.url.split('?')
const params = common.querystringParse(s[1])
params.type = 'http'

if (opts.action === 'announce' || s[0] === '/announce') {
Expand Down
20 changes: 10 additions & 10 deletions lib/server/parse-udp.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module.exports = parseUdpRequest

var ipLib = require('ip')
var common = require('../common')
const ipLib = require('ip')
const common = require('../common')

function parseUdpRequest (msg, rinfo) {
if (msg.length < 16) throw new Error('received packet is too short')

var params = {
const params = {
connectionId: msg.slice(0, 8), // 64-bit
action: msg.readUInt32BE(8),
transactionId: msg.readUInt32BE(12),
Expand All @@ -29,7 +29,7 @@ function parseUdpRequest (msg, rinfo) {
params.event = common.EVENT_IDS[msg.readUInt32BE(80)]
if (!params.event) throw new Error('invalid event') // early return

var ip = msg.readUInt32BE(84) // optional
const ip = msg.readUInt32BE(84) // optional
params.ip = ip
? ipLib.toString(ip)
: rinfo.address
Expand All @@ -49,8 +49,8 @@ function parseUdpRequest (msg, rinfo) {
} else if (params.action === common.ACTIONS.SCRAPE) { // scrape message
if ((msg.length - 16) % 20 !== 0) throw new Error('invalid scrape message')
params.info_hash = []
for (var i = 0, len = (msg.length - 16) / 20; i < len; i += 1) {
var infoHash = msg.slice(16 + (i * 20), 36 + (i * 20)).toString('hex') // 20 bytes
for (let i = 0, len = (msg.length - 16) / 20; i < len; i += 1) {
const infoHash = msg.slice(16 + (i * 20), 36 + (i * 20)).toString('hex') // 20 bytes
params.info_hash.push(infoHash)
}
} else {
Expand All @@ -60,16 +60,16 @@ function parseUdpRequest (msg, rinfo) {
return params
}

var TWO_PWR_32 = (1 << 16) * 2
const TWO_PWR_32 = (1 << 16) * 2

/**
* Return the closest floating-point representation to the buffer value. Precision will be
* lost for big numbers.
*/
function fromUInt64 (buf) {
var high = buf.readUInt32BE(0) | 0 // force
var low = buf.readUInt32BE(4) | 0
var lowUnsigned = (low >= 0) ? low : TWO_PWR_32 + low
const high = buf.readUInt32BE(0) | 0 // force
const low = buf.readUInt32BE(4) | 0
const lowUnsigned = (low >= 0) ? low : TWO_PWR_32 + low

return (high * TWO_PWR_32) + lowUnsigned
}
2 changes: 1 addition & 1 deletion lib/server/parse-websocket.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = parseWebSocketRequest

var common = require('../common')
const common = require('../common')

function parseWebSocketRequest (socket, opts, params) {
if (!opts) opts = {}
Expand Down
30 changes: 15 additions & 15 deletions lib/server/swarm.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
module.exports = Swarm

var arrayRemove = require('unordered-array-remove')
var debug = require('debug')('bittorrent-tracker:swarm')
var LRU = require('lru')
var randomIterate = require('random-iterate')
const arrayRemove = require('unordered-array-remove')
const debug = require('debug')('bittorrent-tracker:swarm')
const LRU = require('lru')
const randomIterate = require('random-iterate')

// Regard this as the default implementation of an interface that you
// need to support when overriding Server.createSwarm() and Server.getSwarm()
function Swarm (infoHash, server) {
var self = this
const self = this
self.infoHash = infoHash
self.complete = 0
self.incomplete = 0
Expand All @@ -21,8 +21,8 @@ function Swarm (infoHash, server) {
// When a peer is evicted from the LRU store, send a synthetic 'stopped' event
// so the stats get updated correctly.
self.peers.on('evict', function (data) {
var peer = data.value
var params = {
const peer = data.value
const params = {
type: peer.type,
event: 'stopped',
numwant: 0,
Expand All @@ -34,10 +34,10 @@ function Swarm (infoHash, server) {
}

Swarm.prototype.announce = function (params, cb) {
var self = this
var id = params.type === 'ws' ? params.peer_id : params.addr
const self = this
const id = params.type === 'ws' ? params.peer_id : params.addr
// Mark the source peer as recently used in cache
var peer = self.peers.get(id)
const peer = self.peers.get(id)

if (params.event === 'started') {
self._onAnnounceStarted(params, peer, id)
Expand Down Expand Up @@ -95,7 +95,7 @@ Swarm.prototype._onAnnounceStopped = function (params, peer, id) {
// If it's a websocket, remove this swarm's infohash from the list of active
// swarms that this peer is participating in.
if (peer.socket && !peer.socket.destroyed) {
var index = peer.socket.infoHashes.indexOf(this.infoHash)
const index = peer.socket.infoHashes.indexOf(this.infoHash)
arrayRemove(peer.socket.infoHashes, index)
}

Expand Down Expand Up @@ -133,12 +133,12 @@ Swarm.prototype._onAnnounceUpdate = function (params, peer, id) {
}

Swarm.prototype._getPeers = function (numwant, ownPeerId, isWebRTC) {
var peers = []
var ite = randomIterate(this.peers.keys)
var peerId
const peers = []
const ite = randomIterate(this.peers.keys)
let peerId
while ((peerId = ite()) && peers.length < numwant) {
// Don't mark the peer as most recently used on announce
var peer = this.peers.peek(peerId)
const peer = this.peers.peek(peerId)
if (!peer) continue
if (isWebRTC && peer.peerId === ownPeerId) continue // don't send peer to itself
if ((isWebRTC && peer.type !== 'ws') || (!isWebRTC && peer.type === 'ws')) continue // send proper peer type
Expand Down
12 changes: 6 additions & 6 deletions test/client-large-torrent.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
var Client = require('../')
var common = require('./common')
var fixtures = require('webtorrent-fixtures')
var test = require('tape')
const Client = require('../')
const common = require('./common')
const fixtures = require('webtorrent-fixtures')
const test = require('tape')

var peerId = Buffer.from('01234567890123456789')
const peerId = Buffer.from('01234567890123456789')

function testLargeTorrent (t, serverType) {
t.plan(9)

common.createServer(t, serverType, function (server, announceUrl) {
var client = new Client({
const client = new Client({
infoHash: fixtures.sintel.parsedTorrent.infoHash,
peerId,
port: 6881,
Expand Down
16 changes: 8 additions & 8 deletions test/client-magnet.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
var Client = require('../')
var common = require('./common')
var fixtures = require('webtorrent-fixtures')
var magnet = require('magnet-uri')
var test = require('tape')
const Client = require('../')
const common = require('./common')
const fixtures = require('webtorrent-fixtures')
const magnet = require('magnet-uri')
const test = require('tape')

var peerId = Buffer.from('01234567890123456789')
const peerId = Buffer.from('01234567890123456789')

function testMagnet (t, serverType) {
t.plan(9)

var parsedTorrent = magnet(fixtures.leaves.magnetURI)
const parsedTorrent = magnet(fixtures.leaves.magnetURI)

common.createServer(t, serverType, function (server, announceUrl) {
var client = new Client({
const client = new Client({
infoHash: parsedTorrent.infoHash,
announce: announceUrl,
peerId,
Expand Down
Loading