creating, seeding and downloading torrent complete
This commit is contained in:
parent
ccd26d19b4
commit
64cbb24cda
@ -13,3 +13,46 @@ footer {
|
|||||||
bottom:0;
|
bottom:0;
|
||||||
left:0;
|
left:0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#output video {
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
#progressBar {
|
||||||
|
height: 5px;
|
||||||
|
width: 0%;
|
||||||
|
background-color: #35b44f;
|
||||||
|
transition: width .4s ease-in-out;
|
||||||
|
}
|
||||||
|
body.is-seed .show-seed {
|
||||||
|
display: inline;
|
||||||
|
}
|
||||||
|
body.is-seed .show-leech {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
.show-seed {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
#status code {
|
||||||
|
font-size: 90%;
|
||||||
|
font-weight: 700;
|
||||||
|
margin-left: 3px;
|
||||||
|
margin-right: 3px;
|
||||||
|
border-bottom: 1px dashed rgba(255,255,255,0.3);
|
||||||
|
}
|
||||||
|
|
||||||
|
.is-seed #hero {
|
||||||
|
background-color: #154820;
|
||||||
|
transition: .5s .5s background-color ease-in-out;
|
||||||
|
}
|
||||||
|
#hero {
|
||||||
|
background-color: #2a3749;
|
||||||
|
}
|
||||||
|
#status {
|
||||||
|
color: #fff;
|
||||||
|
font-size: 17px;
|
||||||
|
padding: 5px;
|
||||||
|
}
|
||||||
|
a:link, a:visited {
|
||||||
|
color: #30a247;
|
||||||
|
text-decoration: none;
|
||||||
|
}
|
||||||
1
public/js/drag-drop.js
Normal file
1
public/js/drag-drop.js
Normal file
File diff suppressed because one or more lines are too long
@ -1,49 +1,10 @@
|
|||||||
|
window.$ = window.jQuery = require('jquery');
|
||||||
|
var bootstrap = require('bootstrap');
|
||||||
|
|
||||||
let buffer = require('buffer')
|
let buffer = require('buffer')
|
||||||
var client = new WebTorrent()
|
var client = new WebTorrent()
|
||||||
|
|
||||||
client.on('error', function (err) {
|
client.on('error', function (err) {
|
||||||
console.error('ERROR: ' + err.message)
|
console.error('ERROR: ' + err.message)
|
||||||
})
|
})
|
||||||
// You can pass in a DOM node or a selector string!
|
|
||||||
DragDrop('.drop-div', function (files, pos, fileList, directories) {
|
|
||||||
console.log('Here are the dropped files', files)
|
|
||||||
console.log('Dropped at coordinates', pos.x, pos.y)
|
|
||||||
console.log('Here is the raw FileList object if you need it:', fileList)
|
|
||||||
console.log('Here is the list of directories:', directories)
|
|
||||||
|
|
||||||
client.seed(files, function (torrent) {
|
|
||||||
console.log(torrent);
|
|
||||||
console.log('Client is seeding ' + torrent.magnetURI)
|
|
||||||
|
|
||||||
let t = ``;
|
|
||||||
|
|
||||||
t += `Client is seeding <strong>${torrent.magnetURI}</strong>`;
|
|
||||||
|
|
||||||
t += `<p>Name: ${torrent.name}</p>`;
|
|
||||||
t += `<p>Name: ${torrent.path}</p>`;
|
|
||||||
var hexdata = torrent.torrentFile.toString('hex');
|
|
||||||
t += `<p>Torrent: ${hexdata}</p>`;
|
|
||||||
t += `<p></p>`;
|
|
||||||
|
|
||||||
t += `<h5>Files hash</h5>`;
|
|
||||||
for (let h = 0; h < torrent._hashes.length; h++) {
|
|
||||||
const hash = torrent._hashes[h];
|
|
||||||
t += `<p>${hash}</p>`;
|
|
||||||
}
|
|
||||||
|
|
||||||
t += `<h5>Peers</h5>`;
|
|
||||||
for (let p = 0; p < torrent._peers.length; p++) {
|
|
||||||
const peer = torrent._peers[p];
|
|
||||||
t += `<p>Peer: ${peer}</p>`;
|
|
||||||
}
|
|
||||||
|
|
||||||
t += `<h5>Trackers</h5>`;
|
|
||||||
for (let r = 0; r < torrent.discovery._announce.length; r++) {
|
|
||||||
const tracker = torrent.discovery._announce[r];
|
|
||||||
t += `<p>${tracker}</p>`;
|
|
||||||
}
|
|
||||||
|
|
||||||
$("#seed-result").html(t);
|
|
||||||
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|||||||
@ -3,7 +3,6 @@ const router = express.Router();
|
|||||||
const { check, validationResult } = require('express-validator/check')
|
const { check, validationResult } = require('express-validator/check')
|
||||||
const { matchedData } = require('express-validator/filter')
|
const { matchedData } = require('express-validator/filter')
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
//const fetch = require('node-fetch');
|
|
||||||
|
|
||||||
var path = require('path')
|
var path = require('path')
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
|
|||||||
@ -1,133 +1,9 @@
|
|||||||
<!doctype html>
|
<%include partials/header.ejs %>
|
||||||
<html>
|
|
||||||
<body>
|
<div class="container">
|
||||||
<h1>Download files using the WebTorrent protocol (BitTorrent over WebRTC).</h1>
|
<h1>Download files using the WebTorrent protocol (BitTorrent over WebRTC).</h1>
|
||||||
|
<small><a target="_blank" href="https://github.com/webtorrent/webtorrent/blob/master/docs/free-torrents.md">Sample torrents</a></small>
|
||||||
|
|
||||||
<form>
|
|
||||||
<label for="torrentId">Download from a magnet link: </label>
|
|
||||||
<input name="torrentId", placeholder="magnet:" value="magnet:?xt=urn:btih:08ada5a7a6183aae1e09d831df6748d566095a10&dn=Sintel&tr=udp%3A%2F%2Fexplodie.org%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.empire-js.us%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&ws=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2F&xs=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2Fsintel.torrent">
|
|
||||||
<button type="submit">Download</button>
|
|
||||||
</form>
|
|
||||||
|
|
||||||
<h2>Log</h2>
|
|
||||||
<div class="log"></div>
|
|
||||||
|
|
||||||
<!-- Include the latest version of WebTorrent -->
|
|
||||||
<script src="https://cdn.jsdelivr.net/webtorrent/latest/webtorrent.min.js"></script>
|
|
||||||
|
|
||||||
<script>
|
|
||||||
var client = new WebTorrent()
|
|
||||||
|
|
||||||
client.on('error', function (err) {
|
|
||||||
console.error('ERROR: ' + err.message)
|
|
||||||
})
|
|
||||||
|
|
||||||
document.querySelector('form').addEventListener('submit', function (e) {
|
|
||||||
e.preventDefault() // Prevent page refresh
|
|
||||||
|
|
||||||
var torrentId = document.querySelector('form input[name=torrentId]').value
|
|
||||||
log('Adding ' + torrentId)
|
|
||||||
client.add(torrentId, onTorrent)
|
|
||||||
})
|
|
||||||
|
|
||||||
function onTorrent (torrent) {
|
|
||||||
log('Got torrent metadata!')
|
|
||||||
log(
|
|
||||||
'Torrent info hash: ' + torrent.infoHash + ' ' +
|
|
||||||
'<a href="' + torrent.magnetURI + '" target="_blank">[Magnet URI]</a> ' +
|
|
||||||
'<a href="' + torrent.torrentFileBlobURL + '" target="_blank" download="' + torrent.name + '.torrent">[Download .torrent]</a>'
|
|
||||||
)
|
|
||||||
|
|
||||||
// Print out progress every 5 seconds
|
|
||||||
var interval = setInterval(function () {
|
|
||||||
log('Progress: ' + (torrent.progress * 100).toFixed(1) + '%')
|
|
||||||
}, 5000)
|
|
||||||
|
|
||||||
torrent.on('done', function () {
|
|
||||||
log('Progress: 100%')
|
|
||||||
clearInterval(interval)
|
|
||||||
})
|
|
||||||
|
|
||||||
// Render all files into to the page
|
|
||||||
torrent.files.forEach(function (file) {
|
|
||||||
file.appendTo('.log')
|
|
||||||
log('(Blob URLs only work if the file is loaded from a server. "http//localhost" works. "file://" does not.)')
|
|
||||||
file.getBlobURL(function (err, url) {
|
|
||||||
if (err) return log(err.message)
|
|
||||||
log('File done.')
|
|
||||||
log('<a href="' + url + '">Download full file: ' + file.name + '</a>')
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
function log (str) {
|
|
||||||
var p = document.createElement('p')
|
|
||||||
p.innerHTML = str
|
|
||||||
document.querySelector('.log').appendChild(p)
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
|
|
||||||
HTML example with status showing UI
|
|
||||||
|
|
||||||
This complete HTML example mimics the UI of the webtorrent.io homepage. It downloads the sintel.torrent file, streams it in the browser and outputs some statistics to the user (peers, progress, remaining time, speed...).
|
|
||||||
|
|
||||||
You can try it right now on CodePen to see what it looks like and play around with it!
|
|
||||||
|
|
||||||
Feel free to replace torrentId with other torrent files, or magnet links, but keep in mind that the browser can only download torrents that are seeded by WebRTC peers (web peers). Use WebTorrent Desktop or Instant.io to seed torrents to the WebTorrent network.
|
|
||||||
|
|
||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>WebTorrent video player</title>
|
|
||||||
<style>
|
|
||||||
#output video {
|
|
||||||
width: 100%;
|
|
||||||
}
|
|
||||||
#progressBar {
|
|
||||||
height: 5px;
|
|
||||||
width: 0%;
|
|
||||||
background-color: #35b44f;
|
|
||||||
transition: width .4s ease-in-out;
|
|
||||||
}
|
|
||||||
body.is-seed .show-seed {
|
|
||||||
display: inline;
|
|
||||||
}
|
|
||||||
body.is-seed .show-leech {
|
|
||||||
display: none;
|
|
||||||
}
|
|
||||||
.show-seed {
|
|
||||||
display: none;
|
|
||||||
}
|
|
||||||
#status code {
|
|
||||||
font-size: 90%;
|
|
||||||
font-weight: 700;
|
|
||||||
margin-left: 3px;
|
|
||||||
margin-right: 3px;
|
|
||||||
border-bottom: 1px dashed rgba(255,255,255,0.3);
|
|
||||||
}
|
|
||||||
|
|
||||||
.is-seed #hero {
|
|
||||||
background-color: #154820;
|
|
||||||
transition: .5s .5s background-color ease-in-out;
|
|
||||||
}
|
|
||||||
#hero {
|
|
||||||
background-color: #2a3749;
|
|
||||||
}
|
|
||||||
#status {
|
|
||||||
color: #fff;
|
|
||||||
font-size: 17px;
|
|
||||||
padding: 5px;
|
|
||||||
}
|
|
||||||
a:link, a:visited {
|
|
||||||
color: #30a247;
|
|
||||||
text-decoration: none;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<div id="hero">
|
<div id="hero">
|
||||||
<div id="output">
|
<div id="output">
|
||||||
<div id="progressBar"></div>
|
<div id="progressBar"></div>
|
||||||
@ -136,12 +12,11 @@ Feel free to replace torrentId with other torrent files, or magnet links, but ke
|
|||||||
<!-- Statistics -->
|
<!-- Statistics -->
|
||||||
<div id="status">
|
<div id="status">
|
||||||
<div>
|
<div>
|
||||||
<span class="show-leech">Downloading </span>
|
|
||||||
<span class="show-seed">Seeding </span>
|
<span class="show-seed">Seeding </span>
|
||||||
<code>
|
<label for="torrent_url">Enter a .torrent file url: (Torrent file must contain .mp4, .webm, .avi, .pdf or .mp3 file only)</label>
|
||||||
<!-- Informative link to the torrent file -->
|
<input type="text" name="torrent_url" id="torrent_url" class="form-control">
|
||||||
<a id="torrentLink" href="https://webtorrent.io/torrents/sintel.torrent">sintel.torrent</a>
|
<button id="btn-download" class="btn btn-primary">Start Download</button>
|
||||||
</code>
|
<span class="show-leech">Downloading torent file</span>
|
||||||
<span class="show-leech"> from </span>
|
<span class="show-leech"> from </span>
|
||||||
<span class="show-seed"> to </span>
|
<span class="show-seed"> to </span>
|
||||||
<code id="numPeers">0 peers</code>.
|
<code id="numPeers">0 peers</code>.
|
||||||
@ -155,14 +30,20 @@ Feel free to replace torrentId with other torrent files, or magnet links, but ke
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<!-- Include the latest version of WebTorrent -->
|
|
||||||
<script src="https://cdn.jsdelivr.net/webtorrent/latest/webtorrent.min.js"></script>
|
|
||||||
|
|
||||||
<!-- Moment is used to show a human-readable remaining time -->
|
</div>
|
||||||
<script src="http://momentjs.com/downloads/moment.min.js"></script>
|
|
||||||
|
<%include partials/footer.ejs %>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
var torrentId = 'https://webtorrent.io/torrents/sintel.torrent'
|
|
||||||
|
function file_ext(file) {
|
||||||
|
return filename.substring(filename.lastIndexOf('.')+1, filename.length) || filename;
|
||||||
|
}
|
||||||
|
|
||||||
|
//http://www.ytsyify.org/[EZTV]/(eztvtorrent.net)%20The%20Originals%20(S05E04)%20(480p).torrent
|
||||||
|
$(document).on('click', '#btn-download', function() {
|
||||||
|
var torrentId = $('#torrent_url').val();
|
||||||
|
|
||||||
var client = new WebTorrent()
|
var client = new WebTorrent()
|
||||||
|
|
||||||
@ -176,14 +57,34 @@ Feel free to replace torrentId with other torrent files, or magnet links, but ke
|
|||||||
var $uploadSpeed = document.querySelector('#uploadSpeed')
|
var $uploadSpeed = document.querySelector('#uploadSpeed')
|
||||||
var $downloadSpeed = document.querySelector('#downloadSpeed')
|
var $downloadSpeed = document.querySelector('#downloadSpeed')
|
||||||
|
|
||||||
|
if ($.trim(torrentId).length<1) {
|
||||||
|
alert("Please specify a valid .torrent file.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Download the torrent
|
// Download the torrent
|
||||||
client.add(torrentId, function (torrent) {
|
client.add(torrentId, function (torrent) {
|
||||||
|
|
||||||
// Torrents can contain many files. Let's use the .mp4 file
|
// Torrents can contain many files. Let's use the .mp4 file
|
||||||
var file = torrent.files.find(function (file) {
|
var file = torrent.files.find(function (file) {
|
||||||
|
if (file.name.endsWith('.mp4')) {
|
||||||
return file.name.endsWith('.mp4')
|
return file.name.endsWith('.mp4')
|
||||||
|
} else if (file.name.endsWith('.webm')) {
|
||||||
|
return file.name.endsWith('.webm')
|
||||||
|
} else if (file.name.endsWith('.avi')) {
|
||||||
|
return file.name.endsWith('.avi')
|
||||||
|
} else if (file.name.endsWith('.pdf')) {
|
||||||
|
return file.name.endsWith('.pdf')
|
||||||
|
} else if (file.name.endsWith('.mp3')) {
|
||||||
|
return file.name.endsWith('.mp3')
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if (typeof file==undefined) {
|
||||||
|
alert("Unsupported files: Torrent file must contain .mp4, .webm, .avi, .pdf or .mp3 file only.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// Stream the file in the browser
|
// Stream the file in the browser
|
||||||
file.appendTo('#output')
|
file.appendTo('#output')
|
||||||
|
|
||||||
@ -233,6 +134,6 @@ Feel free to replace torrentId with other torrent files, or magnet links, but ke
|
|||||||
unit = units[exponent]
|
unit = units[exponent]
|
||||||
return (neg ? '-' : '') + num + ' ' + unit
|
return (neg ? '-' : '') + num + ' ' + unit
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
@ -2,7 +2,8 @@
|
|||||||
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
|
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
|
||||||
<script src="https://cdn.jsdelivr.net/npm/webtorrent@latest/webtorrent.min.js"></script>
|
<script src="https://cdn.jsdelivr.net/npm/webtorrent@latest/webtorrent.min.js"></script>
|
||||||
<script src="https://rawgit.com/feross/drag-drop/master/dragdrop.min.js"></script>
|
<script src="https://rawgit.com/feross/drag-drop/master/dragdrop.min.js"></script>
|
||||||
|
<!-- Moment is used to show a human-readable remaining time -->
|
||||||
|
<script src="http://momentjs.com/downloads/moment.min.js"></script>
|
||||||
<script src="/js/bundle.js"></script>
|
<script src="/js/bundle.js"></script>
|
||||||
|
|
||||||
<!-- FOOTER -->
|
<!-- FOOTER -->
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
<%include partials/header.ejs %>
|
<%include partials/header.ejs %>
|
||||||
|
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<p id='#drag'>hello</p>
|
<p id='#drag'>Drop a file to make it a torrent file:</p>
|
||||||
<div class="card drop-div" id="_drop"></div>
|
<div class="card drop-div" id="_drop"></div>
|
||||||
|
|
||||||
<div class="card">
|
<div class="card">
|
||||||
@ -11,7 +11,58 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<%include partials/footer.ejs %>
|
<%include partials/footer.ejs %>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
//const funcs = require('./public/js/main')
|
$(document).ready(function() {
|
||||||
funcs.seedTorrent()
|
seeding();
|
||||||
|
});
|
||||||
|
|
||||||
|
// You can pass in a DOM node or a selector string!
|
||||||
|
let seeding = () => {
|
||||||
|
DragDrop('.drop-div', function (files, pos, fileList, directories) {
|
||||||
|
console.log('Here are the dropped files', files)
|
||||||
|
console.log('Dropped at coordinates', pos.x, pos.y)
|
||||||
|
console.log('Here is the raw FileList object if you need it:', fileList)
|
||||||
|
console.log('Here is the list of directories:', directories)
|
||||||
|
|
||||||
|
var client = new WebTorrent()
|
||||||
|
|
||||||
|
client.seed(files, function (torrent) {
|
||||||
|
console.log(torrent);
|
||||||
|
console.log('Client is seeding ' + torrent.magnetURI)
|
||||||
|
|
||||||
|
let t = ``;
|
||||||
|
|
||||||
|
t += `Client is seeding <strong>${torrent.magnetURI}</strong>`;
|
||||||
|
|
||||||
|
t += `<p>Name: ${torrent.name}</p>`;
|
||||||
|
t += `<p>Path: ${torrent.path}</p>`;
|
||||||
|
var hexdata = torrent.torrentFile.toString('hex');
|
||||||
|
t += `<p>Torrent: ${hexdata}</p>`;
|
||||||
|
t += `<p></p>`;
|
||||||
|
|
||||||
|
t += `<h5>Files hash</h5>`;
|
||||||
|
for (let h = 0; h < torrent._hashes.length; h++) {
|
||||||
|
const hash = torrent._hashes[h];
|
||||||
|
t += `<p>${hash}</p>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
t += `<h5>Peers</h5>`;
|
||||||
|
for (let p = 0; p < torrent._peers.length; p++) {
|
||||||
|
const peer = torrent._peers[p];
|
||||||
|
t += `<p>Peer: ${peer}</p>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
t += `<h5>Trackers</h5>`;
|
||||||
|
for (let r = 0; r < torrent.discovery._announce.length; r++) {
|
||||||
|
const tracker = torrent.discovery._announce[r];
|
||||||
|
t += `<p>${tracker}</p>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
$("#seed-result").html(t);
|
||||||
|
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
Loading…
Reference in New Issue
Block a user