This repository has been archived by the owner on Sep 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
144 lines (127 loc) · 4.05 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const http = require('http');
const path = require('path');
const bodyParser = require('body-parser');
const express = require('express');
const WebSocket = require('ws');
const magnet = require('magnet-uri');
const WebTorrent = require('webtorrent');
const isProduction = process.env.NODE_ENV === 'production';
if (!isProduction) require('dotenv').config();
var app = express();
const PORT = process.env.PORT || 4000;
const server = http.createServer(app);
const client = new WebTorrent();
const wss = new WebSocket.Server({
server: server,
perMessageDeflate: false
});
wss.on('connection', function connection(ws) {
ws.on('close', function(code, reaason) {
console.log('Connection closed: ', code, reaason);
});
ws.on('message', function incoming(message) {
console.log("Backend Socket received message:", message);
const parsedInfo = magnet.decode(message);
console.log(`Downloading ${parsedInfo.name}`);
client.add(
message,
{
path: path.join(__dirname, 'tmp')
},
torrent => {
client.on('torrent', function(torrent) {
// When torrent info is ready
let torrentObject = {
status: 'download:start',
name: parsedInfo.name,
hash: torrent.infoHash,
files: torrent.files.map(function(file) {
return {
name: file.name,
length: file.length,
path: file.path
};
})
};
ws.send(JSON.stringify(torrentObject), function(error) {
console.log('WS SEND ERROR', error);
});
});
torrent.on('download', function(bytes) {
let torrentObject = {
status: 'download:progress',
name: parsedInfo.name,
hash: torrent.infoHash,
files: torrent.files.map(function(file) {
return {
name: file.name,
length: file.length,
path: file.path
};
}),
stats: {
downloaded: torrent.downloaded,
speed: torrent.downloadSpeed,
progress: torrent.progress
}
};
console.log('total downloaded: ' + torrent.downloaded);
console.log('download speed: ' + torrent.downloadSpeed);
console.log('progress: ' + torrent.progress);
ws.send(JSON.stringify(torrentObject));
if (torrent.progress === 1) {
ws.send(
JSON.stringify(
Object.assign({}, torrentObject, {
status: 'download:complete'
})
)
);
}
});
torrent.on('done', () => {
console.log('Torrent download finished');
// Send status to the client
let torrentObject = {
name: parsedInfo.name,
status: 'download:complete',
hash: torrent.infoHash,
files: torrent.files.map(function(file) {
return {
name: file.name,
length: file.length,
path: file.path
};
}),
stats: {
downloaded: torrent.downloaded,
speed: torrent.downloadSpeed,
progress: torrent.progress
}
};
ws.send(JSON.stringify(torrentObject));
});
}
);
});
ws.send('Connection established');
console.log('New client connected');
});
app
.use(
bodyParser.urlencoded({
extended: true
})
)
.use(bodyParser.json())
.use('/', require('./routes'));
// Render the client routes if any other URL is passed in
// Do this only in production. The local client server is used during development
if (isProduction) {
app.use(express.static(path.resolve(__dirname, 'dist')));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'dist', 'index.html'));
});
}
// Must use http server as listener rather than express app
server.listen(PORT, () => console.log(`Listening on ${PORT}`));