diff --git a/AUTHORS.md b/AUTHORS.md index 3d185d1f..0f21e9a1 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -43,5 +43,8 @@ - crapthings (crapthings@gmail.com) - daiyu (qqdaiyu55@gmail.com) - LI (kslrwang@gmail.com) +- Jimmy Wärting (jimmy@warting.se) +- Justin Kalland (justin@kalland.ch) +- greenkeeper[bot] (23040076+greenkeeper[bot]@users.noreply.github.com) #### Generated by tools/update-authors.sh. diff --git a/client.js b/client.js index a49bde53..9560fe48 100644 --- a/client.js +++ b/client.js @@ -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 @@ -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 @@ -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}`)) diff --git a/lib/client/udp-tracker.js b/lib/client/udp-tracker.js index 56e1b8a7..24a30800 100644 --- a/lib/client/udp-tracker.js +++ b/lib/client/udp-tracker.js @@ -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') @@ -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') @@ -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', { diff --git a/lib/common-node.js b/lib/common-node.js index b06d0004..7a9e87d6 100644 --- a/lib/common-node.js +++ b/lib/common-node.js @@ -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 = { diff --git a/lib/common.js b/lib/common.js index 90d12f55..f735a856 100644 --- a/lib/common.js +++ b/lib/common.js @@ -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 diff --git a/lib/server/parse-http.js b/lib/server/parse-http.js index bf6cb2c4..85a1c872 100644 --- a/lib/server/parse-http.js +++ b/lib/server/parse-http.js @@ -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) { diff --git a/lib/server/parse-websocket.js b/lib/server/parse-websocket.js index ec4e606d..4416008a 100644 --- a/lib/server/parse-websocket.js +++ b/lib/server/parse-websocket.js @@ -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) { diff --git a/package.json b/package.json index d7ad198d..bd55af19 100644 --- a/package.json +++ b/package.json @@ -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": "feross@webtorrent.io", @@ -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", @@ -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", @@ -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", @@ -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" } } diff --git a/server.js b/server.js index 10eb8393..80f5cf52 100644 --- a/server.js +++ b/server.js @@ -1,4 +1,3 @@ -const { Buffer } = require('safe-buffer') const bencode = require('bencode') const debug = require('debug')('bittorrent-tracker:server') const dgram = require('dgram') @@ -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. * @@ -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++ } } @@ -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]) { @@ -179,10 +180,10 @@ class Server extends EventEmitter { function printClients (clients) { let html = '