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
2 changes: 1 addition & 1 deletion client.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class Client extends EventEmitter {
.map(announceUrl => {
let parsedUrl
try {
parsedUrl = new URL(announceUrl)
parsedUrl = common.parseUrl(announceUrl)
} catch (err) {
nextTickWarn(new Error(`Invalid tracker URL: ${announceUrl}`))
return null
Expand Down
15 changes: 1 addition & 14 deletions lib/client/udp-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,7 @@ class UDPTracker extends Tracker {
const self = this
if (!opts) opts = {}

// 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:'))
let { hostname, port } = common.parseUrl(this.announceUrl)
if (port === '') port = 80

let transactionId = genTransactionId()
Expand Down
26 changes: 26 additions & 0 deletions lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,31 @@ exports.hexToBinary = function (str) {
return Buffer.from(str, 'hex').toString('binary')
}

// HACK: Fix for WHATWG URL object not parsing non-standard URL schemes like
// 'udp:'. Just replace it with 'http:' since we only need a few 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
exports.parseUrl = function (str) {
const url = new URL(str.replace(/^udp:/, 'http:'))

if (str.match(/^udp:/)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Since it's just being used to get a boolean, I prefer to use /^udp:/.test(str).

Object.defineProperties(url, {
href: { value: url.href.replace(/^http/, 'udp') },
protocol: { value: url.protocol.replace(/^http/, 'udp') },
origin: { value: url.origin.replace(/^http/, 'udp') }
})
}

return url
}

const config = require('./common-node')
Object.assign(exports, config)
24 changes: 24 additions & 0 deletions test/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,10 +534,34 @@ test('http: invalid tracker port', function (t) {
testUnsupportedTracker(t, 'http://127.0.0.1:69691337/announce')
})

test('http: invalid tracker url', function (t) {
testUnsupportedTracker(t, 'http:')
})

test('http: invalid tracker url with slash', function (t) {
testUnsupportedTracker(t, 'http://')
})

test('udp: invalid tracker port', function (t) {
testUnsupportedTracker(t, 'udp://127.0.0.1:69691337')
})

test('udp: invalid tracker url', function (t) {
testUnsupportedTracker(t, 'udp:')
})

test('udp: invalid tracker url with slash', function (t) {
testUnsupportedTracker(t, 'udp://')
})

test('ws: invalid tracker port', function (t) {
testUnsupportedTracker(t, 'ws://127.0.0.1:69691337')
})

test('ws: invalid tracker url', function (t) {
testUnsupportedTracker(t, 'ws:')
})

test('ws: invalid tracker url with slash', function (t) {
testUnsupportedTracker(t, 'ws://')
})