initial commit

This commit is contained in:
ale 2022-10-14 23:13:16 +02:00
commit d601f329e8
9 changed files with 265 additions and 0 deletions

8
Dockerfile Normal file
View File

@ -0,0 +1,8 @@
FROM node:16-buster-slim
#RUN apt update && apt install -y git python && apt clean
RUN npm i bittorrent-tracker -g
COPY --chown=node:node . /home/node/wtshare
USER node
WORKDIR /home/node/wtshare
RUN npm i
ENTRYPOINT ["/bin/bash", "/home/node/wtshare/entrypoint.sh"]

25
README.md Normal file
View File

@ -0,0 +1,25 @@
# wtshare - WebTorrent Share
This project can be used with JS only, but if you like you can use `docker` and `docker-compose` to deploy
### Configuration and Recommendations
```
If you want to use this project consider using it behind a proxy, you need to set some variables in the file `main.js` file to work properly
```
### Build
```bash
docker-compose build --no-cache
```
### Run
```bash
docker-compose up -d
```
### License
MIT

18
docker-compose.yml Normal file
View File

@ -0,0 +1,18 @@
version: '2'
services:
wtshare:
build: ./
image: wtshare
container_name: wtshare
hostname: wtshare
restart: always
ports:
- 3000:3000
- 8888:8888
- 8888:8888/udp
networks:
wtsharenet:
networks:
wtsharenet:

3
entrypoint.sh Normal file
View File

@ -0,0 +1,3 @@
#!/bin/bash
bittorrent-tracker -p 8888 --http --ws --udp --http-hostname 0.0.0.0 --udp-hostname 0.0.0.0 &
npm start

9
index.js Normal file
View File

@ -0,0 +1,9 @@
const finalhandler = require('finalhandler'),
http = require('http'),
serveStatic = require('serve-static'),
serve = serveStatic('public/', { 'index': ['index.html'] }),
server = http.createServer(onRequest = (req, res) => {
serve(req, res, finalhandler(req, res))
}).listen(3000, () => {
console.log(`Listening on: ${server.address().address}:${server.address().port}`)
})

22
package.json Normal file
View File

@ -0,0 +1,22 @@
{
"name": "wtshare",
"version": "1.0.0",
"description": "WebTorrent Share",
"main": "index.js",
"private": true,
"scripts": {
"start": "node index.js",
"install": "cp node_modules/webtorrent/webtorrent.min.js node_modules/jquery/dist/jquery.min.js node_modules/file-saver/dist/FileSaver.min.js public/js/ && cp node_modules/font-awesome/css/font-awesome.min.css public/css/ && cp -r node_modules/font-awesome/fonts public/"
},
"author": "ale",
"license": "MIT",
"dependencies": {
"browserify": "*",
"file-saver": "*",
"finalhandler": "*",
"font-awesome": "*",
"jquery": "*",
"serve-static": "*",
"webtorrent": "*"
}
}

15
public/css/main.css Normal file
View File

@ -0,0 +1,15 @@
body {
margin: 0 auto;
text-align: center;
}
.holder {
border: dotted #404040 3px;
border-radius: 25px;
min-height: 150px;
padding: 2rem;
margin: 2rem;
vertical-align: middle;
}
.white-border {
border: solid black 3px;
}

29
public/index.html Normal file
View File

@ -0,0 +1,29 @@
<!DOCTYPE html>
<html>
<head>
<title>WebTorrent</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="css/main.css">
<link rel="stylesheet" type="text/css" href="css/font-awesome.min.css">
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/FileSaver.min.js"></script>
<script type="text/javascript" src="js/webtorrent.min.js"></script>
<script type="text/javascript" src="js/main.js"></script>
</head>
<body>
<h2>WebTorrent</h2>
<h3 id="status">Not Supported</h3>
<div id="holder" class="holder white-border">Drag&Drop files here to share</div>
<div id="title"></div>
<div><span id="progress">0</span>% of <span id="size">0</span>Mb</div>
<h4><span id="peers">0</span> <span class="fa fa-users"></span><br /><br /><span id="downloading">0</span>Kbps - <span
id="downloaded">0</span>Mb <span class="fa fa-arrow-down"></span><br /><br /><span id="uploading">0</span>Kbps -
<span id="uploaded">0</span>Mb <span class="fa fa-arrow-up"></span>
</h4>
<p><button class="btn btn-primary" onclick="downloadTorrent(window.torrent);"><span class="fa fa-download"></span> .torrent</button></p>
<div><a id="hash" target="_blank"></a></div>
</body>
</html>

136
public/js/main.js Normal file
View File

@ -0,0 +1,136 @@
var downloadTorrent = function (torrent) {
saveAs(torrent.torrentFileBlobURL, torrent.name + '.torrent')
}
$(document).ready(function () {
var trackers = [
'ws://0.0.0.0:8888'
'http://0.0.0.0:8888/announce'
],
iceServers = [
{ urls: ['turn:???.com', 'stun:???.com'], username: '???', credential: '???' }
],
holder = $('.holder'),
input = $('.input'),
file = $('.file')
function torrentEvents(torrent, seeding) {
torrent.on('metadata', function () {
$('#title').text(torrent.name)
$('#hash').text('/#' + torrent.infoHash)
$('#hash').attr('href', '/#' + torrent.infoHash)
$('#size').text(Math.round((torrent.length / 1024 / 1024) * 100) / 100)
})
torrent.on('ready', function () {
$('#holder').empty()
window.torrent = torrent
})
torrent.on('download', function (bytes) {
$('#status').text('Downloading')
updateTorrent(torrent)
$('#progress').text(Math.round(torrent.progress * 10000) / 100 || '0')
})
torrent.on('wire', function (wire) {
$('#peers').text(torrent.numPeers || '0')
})
torrent.on('done', function () {
$('#status').text('Downloaded')
torrent.files.forEach(function (file) {
file.appendTo('#holder', { autoplay: true, mute: true }, function (err, elem) {
if (err) {
$('#status').text(err)
} else {
$('#status').text('Content loaded')
}
})
})
updateTorrent(torrent)
$('#downloading').text('0')
$('#uploading').text('0')
if (!seeding) {
torrent.files.forEach(function (data) {
data.getBlobURL(function (err, url) {
saveAs(url, data.name)
})
})
}
})
torrent.on('upload', function (data) {
$('#status').text('Uploading')
updateTorrent(torrent)
})
torrent.on('noPeers', function () {
$('#status').text('No Peers')
updateTorrent(torrent)
})
}
function seed(files) {
var client = new WebTorrent({
tracker: WebTorrent.WEBRTC_SUPPORT ? {
rtcConfig: {
iceServers: iceServers
}
} : false
}),
torrent = client.seed(files, {
announce: trackers
})
torrentEvents(torrent, true)
}
function updateTorrent(torrent) {
$('#downloading').text(Math.round(torrent.downloadSpeed / 10) / 100 || '0')
$('#downloaded').text(Math.round((torrent.downloaded / 1024 / 1024) * 100) / 100)
$('#uploading').text(Math.round(torrent.uploadSpeed / 10) / 100 || '0')
$('#uploaded').text(Math.round((torrent.uploaded / 1024 / 1024) * 100) / 100)
$('#peers').text(torrent.numPeers || 0)
}
holder.on('drop', function (event) {
event.preventDefault()
event.stopPropagation()
holder.removeClass('white-border')
if (event.originalEvent.dataTransfer.files) {
var files = event.originalEvent.dataTransfer.files
if (files.length > 1) {
var names = []
for (var i = 0; i < files.length; i++) {
names.push(files[i].name)
}
$('#title').text(names.join(', '))
}
else {
$('#title').text(files[0].name)
}
seed(files)
}
return false
})
.on('dragover', function (event) {
event.preventDefault()
event.stopPropagation()
holder.addClass('white-border')
return false
})
.on('dragleave', function (event) {
event.preventDefault()
event.stopPropagation()
holder.removeClass('white-border')
return false
})
if (!window.FileReader || !WebTorrent.WEBRTC_SUPPORT) {
$('#status').text('WebTorrent NOT Ready')
} else {
$('#status').text('WebTorrent Ready')
$('#holder').text('Drag&Drop files here to share')
if (window.location.hash.substr(1)) {
var client = new WebTorrent({
tracker: WebTorrent.WEBRTC_SUPPORT ? {
rtcConfig: {
iceServers: iceServers
}
} : false
}),
torrent = client.add(window.location.hash.substr(1), {
announce: trackers
})
torrentEvents(torrent)
}
}
})