-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (47 loc) · 1.41 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
const express = require('express');
const app = express();
const port = process.env.PORT || 4000;
const WebTorrent = require('./dependencies/webtorrent-hybrid-wrtc');
console.info('WebTorrent version %s', WebTorrent.VERSION);
// *
// Create webtorrent instance
const client = new WebTorrent();
let checkInterval = false;
// *
// Middleware
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', "*");
res.header("Access-Control-Allow-Credentials", true);
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Range");
next();
});
/**
* Request
*
*/
app.get('/request/:infoHash', function(req, res) {
const infoHash = req.params.infoHash.toLowerCase();
let torrent = client.get(infoHash);
// Torrent already exist
if ( torrent ) {
console.log('torrent already exist, restart seeding');
torrent.destroy();
}
torrent = client.add(infoHash, {
announce: [
"wss://tracker.btorrent.xyz",
"wss://tracker.openwebtorrent.com",
"wss://tracker.fastcast.nz"
]
});
torrent.on('metadata', () => {
console.log('on metadata', torrent.infoHash);
});
// Log peer count
checkInterval && clearInterval(checkInterval);
checkInterval = setInterval(() => {console.log('%s wires active', torrent.wires.length)}, 10000);
res.send(torrent.infoHash);
});
app.listen(port, () => {
console.log('client listen on %s', port);
});