Skip to content

Commit 8e24a8c

Browse files
committedMay 20, 2021
fix: UDP url parsing
1 parent 2a12b75 commit 8e24a8c

File tree

4 files changed

+57
-15
lines changed

4 files changed

+57
-15
lines changed
 

‎client.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class Client extends EventEmitter {
8686
.map(announceUrl => {
8787
let parsedUrl
8888
try {
89-
parsedUrl = new URL(announceUrl)
89+
parsedUrl = common.parseUrl(announceUrl)
9090
} catch (err) {
9191
nextTickWarn(new Error(`Invalid tracker URL: ${announceUrl}`))
9292
return null

‎lib/client/udp-tracker.js

+1-14
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,7 @@ class UDPTracker extends Tracker {
7474
const self = this
7575
if (!opts) opts = {}
7676

77-
// HACK: Fix for WHATWG URL object not parsing non-standard URL schemes like
78-
// 'udp:'. Just replace it with 'http:' since we only need the `hostname`
79-
// and `port` properties.
80-
//
81-
// Note: Only affects Chrome and Firefox. Works fine in Node.js, Safari, and
82-
// Edge.
83-
//
84-
// Note: UDP trackers aren't used in the normal browser build, but they are
85-
// used in a Chrome App build (i.e. by Brave Browser).
86-
//
87-
// Bug reports:
88-
// - Chrome: https://bugs.chromium.org/p/chromium/issues/detail?id=734880
89-
// - Firefox: https://bugzilla.mozilla.org/show_bug.cgi?id=1374505
90-
let { hostname, port } = new URL(this.announceUrl.replace(/^udp:/, 'http:'))
77+
let { hostname, port } = common.parseUrl(this.announceUrl)
9178
if (port === '') port = 80
9279

9380
let transactionId = genTransactionId()

‎lib/common.js

+31
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,36 @@ exports.hexToBinary = function (str) {
1919
return Buffer.from(str, 'hex').toString('binary')
2020
}
2121

22+
// HACK: Fix for WHATWG URL object not parsing non-standard URL schemes like
23+
// 'udp:'. Just replace it with 'http:' since we only need a few properties.
24+
//
25+
// Note: Only affects Chrome and Firefox. Works fine in Node.js, Safari, and
26+
// Edge.
27+
//
28+
// Note: UDP trackers aren't used in the normal browser build, but they are
29+
// used in a Chrome App build (i.e. by Brave Browser).
30+
//
31+
// Bug reports:
32+
// - Chrome: https://bugs.chromium.org/p/chromium/issues/detail?id=734880
33+
// - Firefox: https://bugzilla.mozilla.org/show_bug.cgi?id=1374505
34+
exports.parseUrl = function (str) {
35+
const isUDP = str.match(/^udp:/)
36+
const parsedUrl = (isUDP) ? new URL(str.replace(/^udp:/, 'http:')) : new URL(str)
37+
38+
return {
39+
hash: parsedUrl.hash,
40+
host: parsedUrl.host,
41+
hostname: parsedUrl.hostname,
42+
href: isUDP ? parsedUrl.href.replace(/^http:/, 'udp:') : parsedUrl.href,
43+
origin: isUDP ? parsedUrl.origin.replace(/^http:/, 'udp:') : parsedUrl.origin,
44+
password: parsedUrl.password,
45+
pathname: parsedUrl.pathname,
46+
port: parsedUrl.port,
47+
protocol: isUDP ? 'udp:' : parsedUrl.protocol,
48+
search: parsedUrl.search,
49+
username: parsedUrl.username
50+
}
51+
}
52+
2253
const config = require('./common-node')
2354
Object.assign(exports, config)

‎test/client.js

+24
Original file line numberDiff line numberDiff line change
@@ -534,10 +534,34 @@ test('http: invalid tracker port', function (t) {
534534
testUnsupportedTracker(t, 'http://127.0.0.1:69691337/announce')
535535
})
536536

537+
test('http: invalid tracker url', function (t) {
538+
testUnsupportedTracker(t, 'http:')
539+
})
540+
541+
test('http: invalid tracker url with slash', function (t) {
542+
testUnsupportedTracker(t, 'http://')
543+
})
544+
537545
test('udp: invalid tracker port', function (t) {
538546
testUnsupportedTracker(t, 'udp://127.0.0.1:69691337')
539547
})
540548

549+
test('udp: invalid tracker url', function (t) {
550+
testUnsupportedTracker(t, 'udp:')
551+
})
552+
553+
test('udp: invalid tracker url with slash', function (t) {
554+
testUnsupportedTracker(t, 'udp://')
555+
})
556+
541557
test('ws: invalid tracker port', function (t) {
542558
testUnsupportedTracker(t, 'ws://127.0.0.1:69691337')
543559
})
560+
561+
test('ws: invalid tracker url', function (t) {
562+
testUnsupportedTracker(t, 'ws:')
563+
})
564+
565+
test('ws: invalid tracker url with slash', function (t) {
566+
testUnsupportedTracker(t, 'ws://')
567+
})

0 commit comments

Comments
 (0)
Please sign in to comment.