diff --git a/app/browser/webtorrent.js b/app/browser/webtorrent.js index a47ebcc4160..f5a44945c9d 100644 --- a/app/browser/webtorrent.js +++ b/app/browser/webtorrent.js @@ -1,8 +1,6 @@ const electron = require('electron') const ipc = electron.ipcMain const messages = require('../../js/constants/messages') -const Filtering = require('../filtering') -const { getTargetMagnetUrl } = require('../../js/lib/appUrlUtil') // Set to see communication between WebTorrent and torrent viewer tabs const DEBUG_IPC = false @@ -13,29 +11,8 @@ if (DEBUG_IPC) console.log('WebTorrent IPC debugging enabled') let server = null let channels = {} -function handleMangetUrl (details, isPrivate) { - const result = { - resourceName: module.exports.resourceName, - redirectURL: null, - cancel: false - } - - if (details.resourceType !== 'mainFrame') { - return result - } - - const magnetUrl = getTargetMagnetUrl(details.url) - if (magnetUrl) { - result.redirectUrl = magnetUrl - } - - return result -} - // Receive messages via the window process, ultimately from the UI in a process function init (state, action) { - Filtering.registerBeforeRequestFilteringCB(handleMangetUrl) - if (DEBUG_IPC) console.log('WebTorrent IPC init') ipc.on(messages.TORRENT_MESSAGE, function (e, msg) { if (server === null) { diff --git a/app/extensions.js b/app/extensions.js index 651d153afdd..9462727cf8f 100644 --- a/app/extensions.js +++ b/app/extensions.js @@ -163,7 +163,7 @@ let generateBraveManifest = () => { let generateTorrentManifest = () => { let cspDirectives = { 'default-src': '\'self\'', - 'media-src': '\'self\'', + 'media-src': '\'self\' http://localhost:*', 'form-action': '\'none\'', 'referrer': 'no-referrer', 'style-src': '\'self\' \'unsafe-inline\'', @@ -175,7 +175,7 @@ let generateTorrentManifest = () => { let devServer = 'localhost:' + process.env.npm_package_config_port cspDirectives['default-src'] = '\'self\' http://' + devServer cspDirectives['connect-src'] = '\'self\' http://' + devServer + ' ws://' + devServer - cspDirectives['media-src'] = '\'self\' http://' + devServer + cspDirectives['media-src'] = '\'self\' http://localhost:* http://' + devServer cspDirectives['frame-src'] = '\'self\' http://' + devServer cspDirectives['style-src'] = '\'self\' \'unsafe-inline\' http://' + devServer } diff --git a/app/filtering.js b/app/filtering.js index 0b8a67487e8..1b678768a02 100644 --- a/app/filtering.js +++ b/app/filtering.js @@ -515,6 +515,19 @@ function registerForDownloadListener (session) { }) } +function registerForMagnetHandler (session) { + const webtorrentUrl = appUrlUtil.getTorrentExtUrl('webtorrent.html') + try { + if (getSetting(settings.TORRENT_VIEWER_ENABLED)) { + session.protocol.registerNavigatorHandler('magnet', `${webtorrentUrl}#%s`) + } else { + session.protocol.unregisterNavigatorHandler('magnet', `${webtorrentUrl}#%s`) + } + } catch (e) { + console.warn('Could not register magnet URL handler, are oyu using the latest electron?') + } +} + function initSession (ses, partition) { initializedPartitions[partition] = true registeredSessions[partition] = ses @@ -531,7 +544,8 @@ function initForPartition (partition) { registerForBeforeSendHeaders, registerPermissionHandler, registerForHeadersReceived, - registerForDownloadListener] + registerForDownloadListener, + registerForMagnetHandler] let options = {} if (isSessionPartition(partition)) { options.parent_partition = '' diff --git a/js/components/frame.js b/js/components/frame.js index fde5c961dc2..c0fba63874b 100644 --- a/js/components/frame.js +++ b/js/components/frame.js @@ -21,7 +21,7 @@ const debounce = require('../lib/debounce') const getSetting = require('../settings').getSetting const config = require('../constants/config') const settings = require('../constants/settings') -const {aboutUrls, getTargetMagnetUrl, isSourceMagnetUrl, isSourceAboutUrl, isTargetAboutUrl, getTargetAboutUrl, getBaseUrl, isIntermediateAboutPage} = require('../lib/appUrlUtil') +const {aboutUrls, isSourceMagnetUrl, isSourceAboutUrl, isTargetAboutUrl, getTargetAboutUrl, getBaseUrl, isIntermediateAboutPage} = require('../lib/appUrlUtil') const {isFrameError} = require('../../app/common/lib/httpUtil') const locale = require('../l10n') const appConfig = require('../constants/appConfig') @@ -260,8 +260,6 @@ class Frame extends ImmutableComponent { if (isSourceAboutUrl(newSrc)) { newSrc = getTargetAboutUrl(newSrc) - } else if (isTorrentViewerURL(newSrc)) { - newSrc = getTargetMagnetUrl(newSrc) } let guestInstanceId = null diff --git a/js/lib/appUrlUtil.js b/js/lib/appUrlUtil.js index f4e457ed75c..c7ba0e5748a 100644 --- a/js/lib/appUrlUtil.js +++ b/js/lib/appUrlUtil.js @@ -163,7 +163,9 @@ module.exports.isTargetAboutUrl = function (input) { * Example: getTargetMagnetUrl('magnet:...') -> 'chrome-extension://<...>.html#magnet:...' */ module.exports.getTargetMagnetUrl = function (input) { - if (!input.startsWith('magnet:')) return null + if (!input.startsWith('magnet:')) { + return null + } const url = module.exports.getTorrentExtUrl('webtorrent.html') return [url, input].join('#') } @@ -175,7 +177,7 @@ module.exports.getTargetMagnetUrl = function (input) { */ module.exports.getSourceMagnetUrl = function (input) { if (getBaseUrl(input) !== module.exports.getTorrentExtUrl('webtorrent.html')) return null - const url = getHash(input) + const url = decodeURIComponent(getHash(input)) return url } diff --git a/js/webtorrent/entry.js b/js/webtorrent/entry.js index 05690b52632..026dd7a9543 100644 --- a/js/webtorrent/entry.js +++ b/js/webtorrent/entry.js @@ -17,7 +17,7 @@ require('../../node_modules/font-awesome/css/font-awesome.css') // UI state object. Pure function from `state` -> React element. const state = { - torrentID: window.location.hash.substring(1), + torrentID: window.decodeURIComponent(window.location.hash.substring(1)), parsedTorrent: null, client: null, torrent: null,