Skip to content

Commit ddee6b9

Browse files
committed
Fix URL constructor on udp:// urls in Chrome App environment
For: brave/brave-browser#5604 `bittorrent-tracker` is broken when run in a Chrome App environment. The issue is that the `URL` constructor is buggy Chromium. https://bugs.chromium.org/p/chromium/issues/detail?id=734880 We switched to `URL` from `require('url')` in `[email protected]`. Commit: webtorrent@93b4139 This code path was not exercised by `bittorrent-tracker`'s tests because UDP trackers are normally only used in a Node.js environment. Braves run the code in a Chrome extension environment which we don't test.
1 parent 91148ce commit ddee6b9

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

lib/client/udp-tracker.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,23 @@ class UDPTracker extends Tracker {
7171
_request (opts) {
7272
const self = this
7373
if (!opts) opts = {}
74-
const parsedUrl = new URL(this.announceUrl)
74+
75+
// HACK: Fix for WHATWG URL object not parsing non-standard URL schemes like
76+
// 'udp:'. Just replace it with 'http:' since we only need the `hostname`
77+
// and `port` properties.
78+
//
79+
// Note: Only affects Chrome and Firefox. Works fine in Node.js, Safari, and
80+
// Edge.
81+
//
82+
// Note: UDP trackers aren't used in the normal browser build, but they are
83+
// used in a Chrome App build (i.e. by Brave Browser).
84+
//
85+
// Bug reports:
86+
// - Chrome: https://bugs.chromium.org/p/chromium/issues/detail?id=734880
87+
// - Firefox: https://bugzilla.mozilla.org/show_bug.cgi?id=1374505
88+
let { hostname, port } = new URL(this.announceUrl.replace(/^udp:/, 'http:'))
89+
if (port === '') port = 80
90+
7591
let transactionId = genTransactionId()
7692
let socket = dgram.createSocket('udp4')
7793

@@ -204,10 +220,7 @@ class UDPTracker extends Tracker {
204220
}
205221

206222
function send (message) {
207-
if (!parsedUrl.port) {
208-
parsedUrl.port = 80
209-
}
210-
socket.send(message, 0, message.length, parsedUrl.port, parsedUrl.hostname)
223+
socket.send(message, 0, message.length, port, hostname)
211224
}
212225

213226
function announce (connectionId, opts) {

0 commit comments

Comments
 (0)