Skip to content
This repository was archived by the owner on Feb 1, 2024. It is now read-only.
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
3 changes: 3 additions & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,8 @@
- crapthings ([email protected])
- daiyu ([email protected])
- LI ([email protected])
- Jimmy Wärting ([email protected])
- Justin Kalland ([email protected])
- greenkeeper[bot] (23040076+greenkeeper[bot]@users.noreply.github.com)

#### Generated by tools/update-authors.sh.
13 changes: 9 additions & 4 deletions client.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
const { Buffer } = require('safe-buffer')
const debug = require('debug')('bittorrent-tracker:client')
const EventEmitter = require('events')
const once = require('once')
const parallel = require('run-parallel')
const Peer = require('simple-peer')
const uniq = require('uniq')
const url = require('url')

const common = require('./lib/common')
const HTTPTracker = require('./lib/client/http-tracker') // empty object in browser
Expand Down Expand Up @@ -62,7 +60,7 @@ class Client extends EventEmitter {
this._wrtc = typeof opts.wrtc === 'function' ? opts.wrtc() : opts.wrtc

let announce = typeof opts.announce === 'string'
? [ opts.announce ]
? [opts.announce]
: opts.announce == null ? [] : opts.announce

// Remove trailing slash from trackers to catch duplicates
Expand All @@ -85,7 +83,14 @@ class Client extends EventEmitter {

this._trackers = announce
.map(announceUrl => {
const parsedUrl = url.parse(announceUrl)
let parsedUrl
try {
parsedUrl = new URL(announceUrl)
} catch (err) {
nextTickWarn(new Error(`Invalid tracker URL: ${announceUrl}`))
return null
}

const port = parsedUrl.port
if (port < 0 || port > 65535) {
nextTickWarn(new Error(`Invalid tracker port: ${announceUrl}`))
Expand Down
6 changes: 2 additions & 4 deletions lib/client/udp-tracker.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
const arrayRemove = require('unordered-array-remove')
const BN = require('bn.js')
const Buffer = require('safe-buffer').Buffer
const compact2string = require('compact2string')
const debug = require('debug')('bittorrent-tracker:udp-tracker')
const dgram = require('dgram')
const randombytes = require('randombytes')
const url = require('url')

const common = require('../common')
const Tracker = require('./tracker')
Expand Down Expand Up @@ -73,7 +71,7 @@ class UDPTracker extends Tracker {
_request (opts) {
const self = this
if (!opts) opts = {}
const parsedUrl = url.parse(this.announceUrl)
const parsedUrl = new URL(this.announceUrl)
let transactionId = genTransactionId()
let socket = dgram.createSocket('udp4')

Expand Down Expand Up @@ -176,7 +174,7 @@ class UDPTracker extends Tracker {
}
const infoHashes = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0)
? opts.infoHash.map(infoHash => { return infoHash.toString('hex') })
: [ (opts.infoHash && opts.infoHash.toString('hex')) || self.client.infoHash ]
: [(opts.infoHash && opts.infoHash.toString('hex')) || self.client.infoHash]

for (let i = 0, len = (msg.length - 8) / 12; i < len; i += 1) {
self.client.emit('scrape', {
Expand Down
3 changes: 1 addition & 2 deletions lib/common-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
* These are separate from common.js so they can be skipped when bundling for the browser.
*/

var Buffer = require('safe-buffer').Buffer
var querystring = require('querystring')

exports.IPV4_RE = /^[\d.]+$/
exports.IPV6_RE = /^[\da-fA-F:]+$/
exports.REMOVE_IPV4_MAPPED_IPV6_RE = /^::ffff:/

exports.CONNECTION_ID = Buffer.concat([ toUInt32(0x417), toUInt32(0x27101980) ])
exports.CONNECTION_ID = Buffer.concat([toUInt32(0x417), toUInt32(0x27101980)])
exports.ACTIONS = { CONNECT: 0, ANNOUNCE: 1, SCRAPE: 2, ERROR: 3 }
exports.EVENTS = { update: 0, completed: 1, started: 2, stopped: 3 }
exports.EVENT_IDS = {
Expand Down
2 changes: 0 additions & 2 deletions lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
* Functions/constants needed by both the client and server.
*/

var Buffer = require('safe-buffer').Buffer

exports.DEFAULT_ANNOUNCE_PEERS = 50
exports.MAX_ANNOUNCE_PEERS = 82

Expand Down
2 changes: 1 addition & 1 deletion lib/server/parse-http.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function parseHttpRequest (req, opts) {
} else if (opts.action === 'scrape' || s[0] === '/scrape') {
params.action = common.ACTIONS.SCRAPE

if (typeof params.info_hash === 'string') params.info_hash = [ params.info_hash ]
if (typeof params.info_hash === 'string') params.info_hash = [params.info_hash]
if (Array.isArray(params.info_hash)) {
params.info_hash = params.info_hash.map(function (binaryInfoHash) {
if (typeof binaryInfoHash !== 'string' || binaryInfoHash.length !== 20) {
Expand Down
2 changes: 1 addition & 1 deletion lib/server/parse-websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function parseWebSocketRequest (socket, opts, params) {
} else if (params.action === 'scrape') {
params.action = common.ACTIONS.SCRAPE

if (typeof params.info_hash === 'string') params.info_hash = [ params.info_hash ]
if (typeof params.info_hash === 'string') params.info_hash = [params.info_hash]
if (Array.isArray(params.info_hash)) {
params.info_hash = params.info_hash.map(function (binaryInfoHash) {
if (typeof binaryInfoHash !== 'string' || binaryInfoHash.length !== 20) {
Expand Down
22 changes: 12 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "bittorrent-tracker",
"description": "Simple, robust, BitTorrent tracker (client & server) implementation",
"version": "9.11.0",
"version": "9.13.0",
"author": {
"name": "WebTorrent, LLC",
"email": "[email protected]",
Expand All @@ -19,7 +19,7 @@
"dependencies": {
"bencode": "^2.0.0",
"bittorrent-peerid": "^1.0.2",
"bn.js": "^4.4.0",
"bn.js": "^5.0.0",
"compact2string": "^1.2.0",
"debug": "^4.0.1",
"ip": "^1.0.1",
Expand All @@ -30,14 +30,13 @@
"randombytes": "^2.0.3",
"run-parallel": "^1.1.2",
"run-series": "^1.0.2",
"safe-buffer": "^5.0.0",
"simple-get": "^3.0.0",
"simple-peer": "^9.0.0",
"simple-websocket": "^7.0.1",
"simple-websocket": "^8.0.0",
"string2compact": "^1.1.1",
"uniq": "^1.0.1",
"unordered-array-remove": "^1.0.2",
"ws": "^6.0.0"
"ws": "^7.0.0"
},
"devDependencies": {
"electron-webrtc": "^0.3.0",
Expand All @@ -46,9 +45,8 @@
"tape": "^4.0.0",
"webtorrent-fixtures": "^1.3.0"
},
"optionalDependencies": {
"bufferutil": "^4.0.0",
"utf-8-validate": "^5.0.1"
"engines": {
"node": ">=10"
},
"keywords": [
"bittorrent",
Expand All @@ -62,12 +60,16 @@
],
"license": "MIT",
"main": "index.js",
"optionalDependencies": {
"bufferutil": "^4.0.0",
"utf-8-validate": "^5.0.1"
},
"repository": {
"type": "git",
"url": "git://github.com/webtorrent/bittorrent-tracker.git"
},
"scripts": {
"update-authors": "./tools/update-authors.sh",
"test": "standard && tape test/*.js"
"test": "standard && tape test/*.js",
"update-authors": "./tools/update-authors.sh"
}
}
15 changes: 8 additions & 7 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const { Buffer } = require('safe-buffer')
const bencode = require('bencode')
const debug = require('debug')('bittorrent-tracker:server')
const dgram = require('dgram')
Expand All @@ -15,6 +14,8 @@ const parseHttpRequest = require('./lib/server/parse-http')
const parseUdpRequest = require('./lib/server/parse-udp')
const parseWebSocketRequest = require('./lib/server/parse-websocket')

const hasOwnProperty = Object.prototype.hasOwnProperty

/**
* BitTorrent tracker server.
*
Expand Down Expand Up @@ -147,7 +148,7 @@ class Server extends EventEmitter {
let key

for (key in allPeers) {
if (allPeers.hasOwnProperty(key) && filterFunction(allPeers[key])) {
if (hasOwnProperty.call(allPeers, key) && filterFunction(allPeers[key])) {
count++
}
}
Expand All @@ -158,7 +159,7 @@ class Server extends EventEmitter {
function groupByClient () {
const clients = {}
for (const key in allPeers) {
if (allPeers.hasOwnProperty(key)) {
if (hasOwnProperty.call(allPeers, key)) {
const peer = allPeers[key]

if (!clients[peer.client.client]) {
Expand All @@ -179,10 +180,10 @@ class Server extends EventEmitter {
function printClients (clients) {
let html = '<ul>\n'
for (const name in clients) {
if (clients.hasOwnProperty(name)) {
if (hasOwnProperty.call(clients, name)) {
const client = clients[name]
for (const version in client) {
if (client.hasOwnProperty(version)) {
if (hasOwnProperty.call(client, version)) {
html += `<li><strong>${name}</strong> ${version} : ${client[version]}</li>\n`
}
}
Expand All @@ -203,7 +204,7 @@ class Server extends EventEmitter {
const peer = peers.peek(peerId)
if (peer == null) return // peers.peek() can evict the peer

if (!allPeers.hasOwnProperty(peerId)) {
if (!hasOwnProperty.call(allPeers, peerId)) {
allPeers[peerId] = {
ipv4: false,
ipv6: false,
Expand Down Expand Up @@ -247,7 +248,7 @@ class Server extends EventEmitter {
clients: groupByClient()
}

if (req.url === '/stats.json' || req.headers['accept'] === 'application/json') {
if (req.url === '/stats.json' || req.headers.accept === 'application/json') {
res.write(JSON.stringify(stats))
res.end()
} else if (req.url === '/stats') {
Expand Down
1 change: 0 additions & 1 deletion test/client-large-torrent.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var Buffer = require('safe-buffer').Buffer
var Client = require('../')
var common = require('./common')
var fixtures = require('webtorrent-fixtures')
Expand Down
1 change: 0 additions & 1 deletion test/client-magnet.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var Buffer = require('safe-buffer').Buffer
var Client = require('../')
var common = require('./common')
var fixtures = require('webtorrent-fixtures')
Expand Down
1 change: 0 additions & 1 deletion test/client-ws-socket-pool.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var Buffer = require('safe-buffer').Buffer
var Client = require('../')
var common = require('./common')
var fixtures = require('webtorrent-fixtures')
Expand Down
3 changes: 1 addition & 2 deletions test/client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var Buffer = require('safe-buffer').Buffer
var Client = require('../')
var common = require('./common')
var fixtures = require('webtorrent-fixtures')
Expand Down Expand Up @@ -357,7 +356,7 @@ function testClientAnnounceWithNumWant (t, serverType) {
common.createServer(t, serverType, function (server, announceUrl) {
var client1 = new Client({
infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: [ announceUrl ],
announce: [announceUrl],
peerId: peerId1,
port: port,
wrtc: {}
Expand Down
1 change: 0 additions & 1 deletion test/destroy.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var Buffer = require('safe-buffer').Buffer
var Client = require('../')
var common = require('./common')
var fixtures = require('webtorrent-fixtures')
Expand Down
7 changes: 3 additions & 4 deletions test/evict.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var Buffer = require('safe-buffer').Buffer
var Client = require('../')
var common = require('./common')
var test = require('tape')
Expand Down Expand Up @@ -31,7 +30,7 @@ function serverTest (t, serverType, serverFamily) {

var client1 = new Client({
infoHash: infoHash,
announce: [ announceUrl ],
announce: [announceUrl],
peerId: peerId,
port: 6881,
wrtc: wrtc
Expand All @@ -43,7 +42,7 @@ function serverTest (t, serverType, serverFamily) {
client1.once('update', function (data) {
var client2 = new Client({
infoHash: infoHash,
announce: [ announceUrl ],
announce: [announceUrl],
peerId: peerId2,
port: 6882,
wrtc: wrtc
Expand All @@ -68,7 +67,7 @@ function serverTest (t, serverType, serverFamily) {

var client3 = new Client({
infoHash: infoHash,
announce: [ announceUrl ],
announce: [announceUrl],
peerId: peerId3,
port: 6880,
wrtc: wrtc
Expand Down
1 change: 0 additions & 1 deletion test/filter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var Buffer = require('safe-buffer').Buffer
var Client = require('../')
var common = require('./common')
var fixtures = require('webtorrent-fixtures')
Expand Down
1 change: 0 additions & 1 deletion test/querystring.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var Buffer = require('safe-buffer').Buffer
var common = require('../lib/common')
var test = require('tape')

Expand Down
1 change: 0 additions & 1 deletion test/request-handler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var Buffer = require('safe-buffer').Buffer
var Client = require('../')
var common = require('./common')
var fixtures = require('webtorrent-fixtures')
Expand Down
5 changes: 2 additions & 3 deletions test/scrape.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var bencode = require('bencode')
var Buffer = require('safe-buffer').Buffer
var Client = require('../')
var common = require('./common')
var commonLib = require('../lib/common')
Expand Down Expand Up @@ -119,7 +118,7 @@ function clientScrapeMulti (t, serverType) {

commonTest.createServer(t, serverType, function (server, announceUrl) {
Client.scrape({
infoHash: [ infoHash1, infoHash2 ],
infoHash: [infoHash1, infoHash2],
announce: announceUrl
}, function (err, results) {
t.error(err)
Expand Down Expand Up @@ -161,7 +160,7 @@ test('server: multiple info_hash scrape (manual http request)', function (t) {
var scrapeUrl = announceUrl.replace('/announce', '/scrape')

var url = scrapeUrl + '?' + commonLib.querystringStringify({
info_hash: [ binaryInfoHash1, binaryInfoHash2 ]
info_hash: [binaryInfoHash1, binaryInfoHash2]
})

get.concat(url, function (err, res, data) {
Expand Down
7 changes: 3 additions & 4 deletions test/server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var Buffer = require('safe-buffer').Buffer
var Client = require('../')
var common = require('./common')
var test = require('tape')
Expand Down Expand Up @@ -33,7 +32,7 @@ function serverTest (t, serverType, serverFamily) {

var client1 = new Client({
infoHash: infoHash,
announce: [ announceUrl ],
announce: [announceUrl],
peerId: peerId,
port: 6881,
wrtc: wrtc
Expand Down Expand Up @@ -93,7 +92,7 @@ function serverTest (t, serverType, serverFamily) {

var client2 = new Client({
infoHash: infoHash,
announce: [ announceUrl ],
announce: [announceUrl],
peerId: peerId2,
port: 6882,
wrtc: wrtc
Expand All @@ -113,7 +112,7 @@ function serverTest (t, serverType, serverFamily) {

var client3 = new Client({
infoHash: infoHash,
announce: [ announceUrl ],
announce: [announceUrl],
peerId: peerId3,
port: 6880,
wrtc: wrtc
Expand Down
Loading