Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

Intro to BitTorrent & WebTorrent

Intro to BitTorrent & WebTorrent

Learn about how BitTorrent and WebTorrent work!

WebTorrent website: http://webtorrent.io

Feross Aboukhadijeh

November 21, 2014
Tweet

More Decks by Feross Aboukhadijeh

Other Decks in Technology

Transcript

  1. REQUEST 0 0 16384 BitTorrent request BitTorrent response PIECE 0

    0 <data> method piece index byte offset length method piece index byte offset piece data
  2. Peer 1 Peer 2 Peer 3 0 1 2 3

    4 0 1 2 3 4 0 1 2 3 4 You 0 1 2 3 4
  3. Peer 1 Peer 2 Peer 3 0 1 2 3

    4 0 1 2 3 4 0 1 2 3 4 You 0 1 2 3 4
  4. Peer 1 Peer 2 Peer 3 0 1 2 3

    4 0 1 2 3 4 0 1 2 3 4 You 0 1 2 3 4 0 1 3
  5. Peer 1 Peer 2 Peer 3 0 1 2 3

    4 0 1 2 3 4 0 1 2 3 4 You 0 1 2 3 4 0 1 3 2 4
  6. Peer 1 Peer 2 Peer 3 0 1 2 3

    4 0 1 2 3 4 0 1 2 3 4 You 0 1 2 3 4 0 2 3 4 0 1 2
  7. Request same piece from multiple peers Tit-for-tat Help peers that

    help us Preempt slow peers Download rarest piece first Preserve torrent health Incentivize peers to trade with us
  8. Torrent file { name: 'My awesome file', pieces:[ '1f9c3f59beec079715ec53324bde8569e4a0b4eb', 'ec42307d4ce5557b5d3964c5ef55d354cf4a6ecc',

    '7bf1bcaf79d11fa5e0be06593c8faafc0c2ba2cf', '76d71c5b01526b23007f9e9929beafc5151e6511' ], announce: 'http://tracker.example.com' }
  9. Torrent file { name: 'My awesome file', pieces:[ '1f9c3f59beec079715ec53324bde8569e4a0b4eb', 'ec42307d4ce5557b5d3964c5ef55d354cf4a6ecc',

    '7bf1bcaf79d11fa5e0be06593c8faafc0c2ba2cf', '76d71c5b01526b23007f9e9929beafc5151e6511' ], announce: 'http://tracker.example.com' } Info Hash: d2474e86c95b19b8bcfdb92bc12c9d44667cfa36
  10. Cryptographic hashes • “One-way function” • Same input always produces

    same output • Given output, very very hard to find input
  11. Decentralization HTTP Torrent files Magnet links Find URIs Search engines,

    friends Search engines, friends Search engines, friend Find peers DNS (Domain Name System) Tracker server DHT (Distributed Hash Tabl File transfer Single HTTP server Many BitTorrent peers Many BitTorrent peers
  12. Decentralization HTTP Torrent files Magnet links Find URIs Search engines,

    friends Search engines, friends Search engines, friend Find peers DNS (Domain Name System) Tracker server DHT (Distributed Hash Tabl File transfer Single HTTP server Many BitTorrent peers Many BitTorrent peers
  13. Decentralization HTTP Torrent files Magnet links Find URIs Search engines,

    friends Search engines, friends Search engines, friend Find peers DNS (Domain Name System) Tracker server DHT (Distributed Hash Tabl File transfer Single HTTP server Many BitTorrent peers Many BitTorrent peers
  14. • if new peer would make bucket K+1 • if

    our ID is in the bucket • split the bucket, & add the new peer • else • ping all peers in bucket • if some peer is dead • replace dead peer with new peer • else • throw away new peer Bucket size cannot surpass K
  15. put(key, value) • if key is “near” our node ID

    • store the { key, value } • else • throw it away
  16. put(key, value) • if key is “near” our node ID

    • store the { key, value } • else • throw it away put( 'e88e8338c572a06e3c801b29f519df532b3e76f6', '12.34.56.78:3000' )
  17. lookup(key) • pick K closest nodes, send them lookup(key) message

    • if they return new DHT nodes (that are closer) • send them lookup(key) message & recurse • else if they return peers (they are a tracker!) • we're done – connect to the peers!
  18. Distributed Hash Table • Fast O(log n) lookup, where n

    is number of nodes • Over time, table fills with stable, long-running nodes
  19. Decentralization HTTP Torrent files Magnet links Find URIs Search engines,

    friends Search engines, friends Search engines, friends Find peers DNS (Domain Name System) Tracker server DHT (Distributed Hash Table) File transfer Single HTTP server Many BitTorrent peers Many BitTorrent peers
  20. “A project to bring realtime communication (RTC) to the open

    web platform” - Justin Uberti @ Google I/O 2012
  21. Usage: webtorrent <options> <torrent> Download the torrent, given as: *

    magnet uri * http/https url to .torrent file * filesystem path to .torrent file * info hash (as hex string) Options: --airplay stream to Apple TV (AirPlay) --chromecast stream to Chromecast --vlc stream in VLC --mplayer stream in MPlayer --mpv stream in MPV --omx [jack] stream in omx (jack=local|hdmi) -p, --port [number] change the http port [default: 9000] -b, --blocklist [path] use the specified blocklist -t, --subtitles [file] load subtitles file -l, --list list available files in torrent -n, --no-quit do not quit webtorrent on vlc exit -r, --remove remove downloaded files on exit -q, --quiet silence stdout
  22. new RTCPeerConnection(constraints, config) <offer> <offer> pc Server new RTCPeerConnection(…) createOffer()

    getUserMedia() addStream() <stream> “negotiationneeded” setLocalDescription()
  23. new RTCPeerConnection(constraints, config) <offer> <offer> pc Server pc new RTCPeerConnection(…)

    createOffer() getUserMedia() addStream() <stream> “negotiationneeded” setLocalDescription()
  24. setRemoteDescription() new RTCPeerConnection(constraints, config) <offer> <offer> pc Server pc new

    RTCPeerConnection(…) createOffer() getUserMedia() addStream() <stream> “negotiationneeded” setLocalDescription()
  25. setRemoteDescription() new RTCPeerConnection(constraints, config) <offer> <offer> pc Server pc new

    RTCPeerConnection(…) createOffer() getUserMedia() addStream() <stream> getUserMedia() “negotiationneeded” setLocalDescription()
  26. setRemoteDescription() new RTCPeerConnection(constraints, config) <offer> <offer> pc Server pc new

    RTCPeerConnection(…) createOffer() getUserMedia() addStream() <stream> getUserMedia() <stream> “negotiationneeded” setLocalDescription()
  27. setRemoteDescription() new RTCPeerConnection(constraints, config) <offer> <offer> setLocalDescription() pc Server pc

    new RTCPeerConnection(…) createOffer() getUserMedia() addStream() <stream> getUserMedia() addStream() <stream> “negotiationneeded”
  28. setRemoteDescription() new RTCPeerConnection(constraints, config) <offer> <offer> setLocalDescription() pc Server pc

    new RTCPeerConnection(…) createOffer() createAnswer() getUserMedia() addStream() <stream> getUserMedia() addStream() <stream> “negotiationneeded”
  29. setRemoteDescription() new RTCPeerConnection(constraints, config) <offer> <offer> setLocalDescription() pc Server pc

    new RTCPeerConnection(…) createOffer() createAnswer() <answer> getUserMedia() addStream() <stream> getUserMedia() addStream() <stream> “negotiationneeded”
  30. setRemoteDescription() new RTCPeerConnection(constraints, config) <offer> <offer> setLocalDescription() pc Server pc

    new RTCPeerConnection(…) createOffer() setLocalDescription() createAnswer() <answer> getUserMedia() addStream() <stream> getUserMedia() addStream() <stream> “negotiationneeded”
  31. setRemoteDescription() new RTCPeerConnection(constraints, config) <offer> <offer> setLocalDescription() pc Server pc

    new RTCPeerConnection(…) createOffer() setLocalDescription() createAnswer() <answer> getUserMedia() addStream() <stream> getUserMedia() addStream() <stream> “negotiationneeded”
  32. setRemoteDescription() new RTCPeerConnection(constraints, config) <offer> <offer> setLocalDescription() pc Server pc

    new RTCPeerConnection(…) createOffer() setLocalDescription() createAnswer() <answer> <answer> getUserMedia() addStream() <stream> getUserMedia() addStream() <stream> “negotiationneeded”
  33. setRemoteDescription() new RTCPeerConnection(constraints, config) <offer> <offer> setLocalDescription() pc Server pc

    new RTCPeerConnection(…) createOffer() setLocalDescription() createAnswer() <answer> <answer> setRemoteDescription() getUserMedia() addStream() <stream> getUserMedia() addStream() <stream> “negotiationneeded”
  34. setRemoteDescription() new RTCPeerConnection(constraints, config) <offer> <offer> setLocalDescription() “icecandidate” <candidate> pc

    Server pc new RTCPeerConnection(…) createOffer() setLocalDescription() createAnswer() <answer> <answer> setRemoteDescription() getUserMedia() addStream() <stream> getUserMedia() addStream() <stream> “negotiationneeded”
  35. setRemoteDescription() new RTCPeerConnection(constraints, config) <offer> <offer> setLocalDescription() “icecandidate” <candidate> pc

    Server ⋮ pc new RTCPeerConnection(…) createOffer() setLocalDescription() createAnswer() <answer> <answer> setRemoteDescription() getUserMedia() addStream() <stream> getUserMedia() addStream() <stream> “negotiationneeded”
  36. setRemoteDescription() new RTCPeerConnection(constraints, config) <offer> <offer> setLocalDescription() “icecandidate” <candidate> pc

    Server <candidate> ⋮ pc new RTCPeerConnection(…) createOffer() setLocalDescription() createAnswer() <answer> <answer> setRemoteDescription() getUserMedia() addStream() <stream> getUserMedia() addStream() <stream> “negotiationneeded”
  37. setRemoteDescription() new RTCPeerConnection(constraints, config) <offer> <offer> setLocalDescription() “icecandidate” <candidate> pc

    Server <candidate> addIceCandidate() ⋮ pc new RTCPeerConnection(…) createOffer() setLocalDescription() createAnswer() <answer> <answer> setRemoteDescription() getUserMedia() addStream() <stream> getUserMedia() addStream() <stream> “negotiationneeded”
  38. setRemoteDescription() new RTCPeerConnection(constraints, config) <offer> <offer> setLocalDescription() “icecandidate” <candidate> <candidate>

    “icecandidate” pc Server <candidate> addIceCandidate() ⋮ pc new RTCPeerConnection(…) createOffer() setLocalDescription() createAnswer() <answer> <answer> setRemoteDescription() getUserMedia() addStream() <stream> getUserMedia() addStream() <stream> “negotiationneeded”
  39. setRemoteDescription() new RTCPeerConnection(constraints, config) <offer> <offer> setLocalDescription() “icecandidate” <candidate> <candidate>

    “icecandidate” pc Server <candidate> addIceCandidate() ⋮ ⋮ pc new RTCPeerConnection(…) createOffer() setLocalDescription() createAnswer() <answer> <answer> setRemoteDescription() getUserMedia() addStream() <stream> getUserMedia() addStream() <stream> “negotiationneeded”
  40. setRemoteDescription() new RTCPeerConnection(constraints, config) <offer> <offer> setLocalDescription() “icecandidate” <candidate> <candidate>

    “icecandidate” pc Server <candidate> addIceCandidate() <candidate> ⋮ ⋮ pc new RTCPeerConnection(…) createOffer() setLocalDescription() createAnswer() <answer> <answer> setRemoteDescription() getUserMedia() addStream() <stream> getUserMedia() addStream() <stream> “negotiationneeded”
  41. setRemoteDescription() new RTCPeerConnection(constraints, config) <offer> <offer> setLocalDescription() “icecandidate” <candidate> <candidate>

    “icecandidate” pc Server <candidate> addIceCandidate() <candidate> ⋮ ⋮ pc new RTCPeerConnection(…) createOffer() setLocalDescription() createAnswer() <answer> <answer> setRemoteDescription() addIceCandidate() getUserMedia() addStream() <stream> getUserMedia() addStream() <stream> “negotiationneeded”
  42. setRemoteDescription() new RTCPeerConnection(constraints, config) <offer> <offer> setLocalDescription() “icecandidate” <candidate> <candidate>

    “icecandidate” pc Server <candidate> addIceCandidate() <candidate> ⋮ ⋮ pc new RTCPeerConnection(…) createOffer() setLocalDescription() createAnswer() <answer> <answer> setRemoteDescription() addIceCandidate() getUserMedia() addStream() <stream> getUserMedia() addStream() <stream> “negotiationneeded” “addstream”
  43. setRemoteDescription() new RTCPeerConnection(constraints, config) <offer> <offer> setLocalDescription() “icecandidate” <candidate> <candidate>

    “icecandidate” pc Server <candidate> addIceCandidate() <candidate> ⋮ ⋮ pc new RTCPeerConnection(…) createOffer() setLocalDescription() createAnswer() <answer> <answer> setRemoteDescription() addIceCandidate() getUserMedia() addStream() <stream> getUserMedia() addStream() <stream> “negotiationneeded” “addstream”
  44. setRemoteDescription() new RTCPeerConnection(constraints, config) <offer> <offer> setLocalDescription() “icecandidate” <candidate> <candidate>

    “icecandidate” pc Server <candidate> addIceCandidate() <candidate> ⋮ ⋮ pc new RTCPeerConnection(…) createOffer() setLocalDescription() createAnswer() <answer> <answer> setRemoteDescription() addIceCandidate() getUserMedia() addStream() <stream> getUserMedia() addStream() <stream> “negotiationneeded” “addstream” “addstream”
  45. setRemoteDescription() new RTCPeerConnection(constraints, config) <offer> <offer> setLocalDescription() “icecandidate” <candidate> <candidate>

    “icecandidate” pc Server <candidate> addIceCandidate() <candidate> ⋮ ⋮ pc new RTCPeerConnection(…) createOffer() setLocalDescription() createAnswer() <answer> <answer> setRemoteDescription() addIceCandidate() getUserMedia() addStream() <stream> getUserMedia() addStream() <stream> “negotiationneeded” “addstream” “addstream”