Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Add WebTorrent support #5342

Closed
wants to merge 48 commits into from
Closed

Add WebTorrent support #5342

wants to merge 48 commits into from

Conversation

dcposch
Copy link
Contributor

@dcposch dcposch commented Nov 2, 2016

See #3256

This is still pretty raw. It demonstrates torrent support in Brave.

Visit a magnet URL

screen shot 2016-11-01 at 8 03 05 pm

Bookmark a magnet link

screen shot 2016-11-01 at 8 04 02 pm

Play files in a torrent

brave-webtorrent-1

Here are some of the things we still need to do:

  • Design nicer UX
  • Implement nicer UX
  • Probably move WebTorrent to its own process. Currently it's running in the Brave extension content script, with some of the CSP restrictions disabled.
  • Add tests
  • Security review (sort of---cc @diracdeltas )

Feedback appreciated!

@BrendanEich @diracdeltas @ayumi @bbondy

@bbondy
Copy link
Member

bbondy commented Nov 2, 2016

Could you add some sample torrent and magnet links that would demonstrate the added things nicely? Thanks!

@bbondy bbondy changed the title Add WebTorrent support (work in progress) [WIP] Add WebTorrent support Nov 2, 2016
@dcposch
Copy link
Contributor Author

dcposch commented Nov 2, 2016

@bbondy sure, @feross collected some here: https://codepen.io/ferossity/full/qaezaB/

Right now you have to paste magnet links into the URL bar. Clicking a magnet link still tries to open an external app. I'll try to fix that later today.

@dcposch
Copy link
Contributor Author

dcposch commented Nov 2, 2016

@bbondy you can click on magnet links now

do you think this commit is a reasonable way to implement that?

0834c98

// * Move WebTorrent to its own renderer process, similar to the way it's done in
// WebTorrent Desktop
// * Remove this CSP exception:
cspDirectives['connect-src'] = '*'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bbondy do these CSP rules apply only to the braveExtension, or to all extensions?

Copy link
Member

Choose a reason for hiding this comment

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

@dcposch only to the braveExtension, iirc. for security reasons, i would prefer that WebTorrent be its own "extension" similar to how we handle PDF reader (PDF.JS)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done!

@@ -0,0 +1,19 @@
(function () {
var queryString = window.location.search
Copy link
Member

Choose a reason for hiding this comment

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

looks like queryString is not used?

Copy link
Contributor

Choose a reason for hiding this comment

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

Will remove

module.exports.getSourceMagnetUrl = function (input) {
const url = module.exports.getAppUrl('webtorrent.html')
if (!input.startsWith(url)) return null
return input.substring(input.indexOf('#') + 1)
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't this check that the return value starts with 'magnet:?'

Copy link
Contributor

Choose a reason for hiding this comment

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

The target URL is internal-only and controlled by us, so I think that we can assume it will always be a magnet link or torrent file. This seems to be the assumption that is made by the analogous getSourceAboutUrl function. Correct me if I'm wrong?

Copy link
Member

Choose a reason for hiding this comment

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

that sounds right, assuming you won't define page-anchor URLs like chrome-extension://.../webtorrent.html#about or something.

@diracdeltas
Copy link
Member

how should this work for users who already have a magnet: URL protocol handler set up in Brave? ex: i usually set up put.io to handle magnet links

@feross
Copy link
Contributor

feross commented Nov 3, 2016

It's also worth noting that as currently architected in this PR, WebTorrent can only connect to WebRTC peers, and not normal TCP/uTP ones. This is because we're running in a chrome-extension:/// context that has no node integration. So we're using the browser version of WebTorrent (webtorrent.min.js) at the moment.

@feross
Copy link
Contributor

feross commented Nov 3, 2016

@diracdeltas As currently implemented, we ignore any protocol handlers registered by webpages through navigator.registerProtocolHandler().

Does Brave expose an interface for configuring protocol handlers yet?

Actually, here's an idea. What if we just add a new link to the Torrent Viewer page that says "Open in " for users who have a "magnet" protocol handler registered. Here's the current mock @dcposch is working on:

screen shot 2016-11-03 at 3 24 55 pm

So imagine another link added to that list. @diracdeltas Would that satisfy your use case?

@diracdeltas
Copy link
Member

What if we just add a new link to the Torrent Viewer page that says "Open in " for users who have a "magnet" protocol handler registered. Here's the current mock @dcposch is working on:

👍

@dcposch
Copy link
Contributor Author

dcposch commented Nov 4, 2016

New screenshots

The file list now consists of regular hyperlinks. You can link to individual files within a torrent now, using the ix magnet link parameter.

Right now, following one of those links is slow, because the new page has to connect to the swarm from scratch. That will be fixed once we move WebTorrent to its own process.

screen shot 2016-11-03 at 7 50 44 pm

screen shot 2016-11-03 at 7 50 48 pm

@dcposch
Copy link
Contributor Author

dcposch commented Nov 11, 2016

WebTorrent now runs in the browser process.

It communicates with the Torrent Viewer extension pages over IPC, and serves content via a streaming torrent-to-HTTP server.

image

@dcposch
Copy link
Contributor Author

dcposch commented Nov 11, 2016

Improved UI/UX coming soon, @feross has a PR! dcposch#7

@dcposch
Copy link
Contributor Author

dcposch commented Nov 11, 2016

Almost ready for review!

Work left to do

  • Merge WebTorrent extension progress dcposch/browser-laptop#7 and rebase on latest master
  • Fix refresh: hitting F5 should not try to open an external torrent app
  • Auto stop: once the last tab for a given infohash is closed, remove the torrent after a reasonable amount of time
  • Auto start: if a torrent is already active, go directly to the file list, don't show the Start button
  • [Maybe] No extra UI around a file. Just show the file contents. See WebTorrent extension progress dcposch/browser-laptop#7 (comment)
  • [Maybe] Better Incognito support
  • Manual testing. Open 20+ tabs full of magnet links. Hold down the Refresh button. Open the same video across multiple windows. Nothing should crash, glitch, or get slow.

@feross
Copy link
Contributor

feross commented Nov 14, 2016

Also:

  • Fix the way magnet links show up in the History tab.
  • Support right-click > "Open in New Tab" in File List.
  • Play MKVs instead of downloading them

@dcposch dcposch force-pushed the master branch 2 times, most recently from e896f82 to 349d13b Compare November 15, 2016 04:11
@@ -877,10 +887,27 @@ class Frame extends ImmutableComponent {
method = (currentDetail, originalDetail) =>
windowActions.setAutofillCreditCardDetail(currentDetail, originalDetail)
break
case messages.TORRENT_MESSAGE:
// Relay torrent IPC from the webview to a browser process
Copy link
Member

@diracdeltas diracdeltas Nov 15, 2016

Choose a reason for hiding this comment

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

I don't think this relaying is necessary; the webview can just call ipc.send instead of ipc.sendToHost to send a message to the browser process. See messages.CERT_ERROR_ACCEPTED for instance.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmm. that doesn't seem to work

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nvm, figured it out!

@dcposch
Copy link
Contributor Author

dcposch commented Nov 19, 2016

I think we're ready

Please try it out & let me know if there's anything that needs improvement!

Code review also appreciated. Rebased on the latest master earlier today: 5e36e37, a few commits after Brave 0.12.10

@bbondy @diracdeltas @bradleyrichter @feross @BrendanEich

In this PR

  • Support for magnet links. View a torrent, stream individual files, bookmark, save .torrents, download files.

Not in this PR

Here are some additional things you could do, probably after Brave 1.0

  • Support HTTP torrent URLs. Currently, navigating to a .torrent file will just download it. We could treat those the same way we treat magnet links. There are some UX challenges to consider. For example: how do you link to a single file within an HTTP .torrent URL? It's not as straightforward as appending &ix=<n> to a magnet URL. You might use a hash, eg http[s]://<...>.torrent#ix=<n>
  • History. Currently, magnet: links don't appear in the History tab. Looks like this is the same behavior as PDFs in the PDF Viewer.
  • Support torrents in the mobile versions of Brave.

Latest screenshots

Regular webpage with magnet links

Navigate https://codepen.io/ferossity/full/qaezaB/. Click one of the links.

screen shot 2016-11-18 at 5 14 22 pm

Magnet page

Brave doesn't connect to any peers or to the DHT until you give it permission. A short legal warning lets users know they're about to join a P2P network.

screen shot 2016-11-17 at 11 14 52 am

Click start.

File list

screen shot 2016-11-18 at 5 17 05 pm

screen shot 2016-11-18 at 4 42 48 pm

Right click on one of the named links, then click Open in New Tab.

Magnet page for an individual file

This is a magnet link with the same infohash as the one we were just on, but with an ix attribute specifying a single file inside the torrent.

Since that torrent is already active, there's not disclaimer and no start button, the media starts streaming immediately. WebTorrent downloads pieces from peers in the order that we need them so we can start playing right away.

screen shot 2016-11-18 at 6 27 58 pm

screen shot 2016-11-17 at 11 15 08 am

Download links

Back in the first tab, click Download. This creates a normal download. Under the hood, it uses the same streaming torrent-to-HTTP local server as the

screen shot 2016-11-18 at 4 42 41 pm

You can also click Download Torrent File to save a .torrent, eg for use with an external app.

screen shot 2016-11-18 at 6 28 18 pm

Bookmarks

You can bookmark a whole torrent or an individual file.

Preferences

You can disable the Torrent Viewer. In that case, magnet files will prompt the user to open an external app.

screen shot 2016-11-18 at 6 30 40 pm

LMK if this works for you!

@dcposch dcposch changed the title [WIP] Add WebTorrent support Add WebTorrent support Nov 21, 2016
@bbondy
Copy link
Member

bbondy commented Nov 22, 2016

Rebased to get rid of the recent conflict and merged to master
Great work @dcposch @feross @diracdeltas ! 👍 💯
eb004bd...05e1231

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants