mirror of
https://github.com/webtorrent/bittorrent-tracker.git
synced 2025-01-31 02:21:36 +00:00
Merge pull request #362 from webtorrent/fix_udp_url
This commit is contained in:
commit
76f6d41808
@ -86,7 +86,7 @@ class Client extends EventEmitter {
|
|||||||
.map(announceUrl => {
|
.map(announceUrl => {
|
||||||
let parsedUrl
|
let parsedUrl
|
||||||
try {
|
try {
|
||||||
parsedUrl = new URL(announceUrl)
|
parsedUrl = common.parseUrl(announceUrl)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
nextTickWarn(new Error(`Invalid tracker URL: ${announceUrl}`))
|
nextTickWarn(new Error(`Invalid tracker URL: ${announceUrl}`))
|
||||||
return null
|
return null
|
||||||
|
@ -74,20 +74,7 @@ class UDPTracker extends Tracker {
|
|||||||
const self = this
|
const self = this
|
||||||
if (!opts) opts = {}
|
if (!opts) opts = {}
|
||||||
|
|
||||||
// HACK: Fix for WHATWG URL object not parsing non-standard URL schemes like
|
let { hostname, port } = common.parseUrl(this.announceUrl)
|
||||||
// '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
|
if (port === '') port = 80
|
||||||
|
|
||||||
let transactionId = genTransactionId()
|
let transactionId = genTransactionId()
|
||||||
|
@ -19,5 +19,31 @@ exports.hexToBinary = function (str) {
|
|||||||
return Buffer.from(str, 'hex').toString('binary')
|
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:/)) {
|
||||||
|
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')
|
const config = require('./common-node')
|
||||||
Object.assign(exports, config)
|
Object.assign(exports, config)
|
||||||
|
@ -534,10 +534,34 @@ test('http: invalid tracker port', function (t) {
|
|||||||
testUnsupportedTracker(t, 'http://127.0.0.1:69691337/announce')
|
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) {
|
test('udp: invalid tracker port', function (t) {
|
||||||
testUnsupportedTracker(t, 'udp://127.0.0.1:69691337')
|
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) {
|
test('ws: invalid tracker port', function (t) {
|
||||||
testUnsupportedTracker(t, 'ws://127.0.0.1:69691337')
|
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://')
|
||||||
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user