diff --git a/lib/client/udp-tracker.js b/lib/client/udp-tracker.js index 24a3080..2402dbf 100644 --- a/lib/client/udp-tracker.js +++ b/lib/client/udp-tracker.js @@ -71,7 +71,23 @@ class UDPTracker extends Tracker { _request (opts) { const self = this if (!opts) opts = {} - const parsedUrl = new URL(this.announceUrl) + + // HACK: Fix for WHATWG URL object not parsing non-standard URL schemes like + // 'udp:'. Just replace it with 'http:' since we only need the `hostname` + // and `port` properties. + // + // Note: Only affects Chrome and Firefox. Works fine in Node.js, Safari, and + // Edge. + // + // Note: UDP trackers aren't used in the normal browser build, but they are + // used in a Chrome App build (i.e. by Brave Browser). + // + // Bug reports: + // - Chrome: https://bugs.chromium.org/p/chromium/issues/detail?id=734880 + // - Firefox: https://bugzilla.mozilla.org/show_bug.cgi?id=1374505 + let { hostname, port } = new URL(this.announceUrl.replace(/^udp:/, 'http:')) + if (port === '') port = 80 + let transactionId = genTransactionId() let socket = dgram.createSocket('udp4') @@ -204,10 +220,7 @@ class UDPTracker extends Tracker { } function send (message) { - if (!parsedUrl.port) { - parsedUrl.port = 80 - } - socket.send(message, 0, message.length, parsedUrl.port, parsedUrl.hostname) + socket.send(message, 0, message.length, port, hostname) } function announce (connectionId, opts) {