-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
140 lines (135 loc) · 3.73 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
__dirname = process.execPath.substr(0,process.execPath.lastIndexOf('/')); // nodejs shim
var express = require('express');
var fs = require('fs');
var path = require('path');
var http = require('http');
var os = require('os');
var md5 = require('md5');
var api = express();
var bodyParser = require('body-parser');
var morgan = require('morgan');
var request = require('sync-request');
var peerflix = require('peerflix');
var mkdirp = require('mkdirp');
var rimraf = require('rimraf');
var parseTorrent = require('parse-torrent');
var config = JSON.parse(fs.readFileSync(path.resolve(__dirname, 'config.json')));
var torrents = {};
var tmpDir = path.join(os.tmpdir(), 'showstream');
var runTorrent = function(url, req, res) {
var id = md5(url);
console.log('runTorrent started for %s', id);
console.log('Starting peerflix...');
torrents[id] = {
atime: Math.floor(Date.now() / 1000),
peerflix: peerflix(url, {
path: tmpDir,
port: config.peerflix_port,
buffer: (5 * 1024 * 1024).toString(),
connections: 100,
upload: 2
}),
stream: ''
};
torrents[id].peerflix.on('ready', function() {
console.log('Peerflix ready. ');
var stream = 'http://127.0.0.1:' + config.peerflix_port + '/0';
console.log('Torrent available at %s', stream);
torrents[id].stream = stream;
res.send({
status: 'ok',
id: id,
stream: stream
});
});
};
api.use(bodyParser.json());
api.use(morgan('dev'));
api.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'OPTIONS, POST, GET, PUT, DELETE');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
next();
});
api.get('/', function(req, res) {
res.send({
status: 'ok',
version: config.version,
torrents: Object.keys(torrents)
});
});
api.get('/page', function(req, res) {
var url = req.query.url;
if(url == '' || !url || url == null || typeof url == 'undefined') {
res.status(422).send({
status: 'error',
error: "URL not set, can't request page."
});
return;
}
request.get(url).pipe(res);
});
api.get('/stream', function(req, res) {
var url = req.query.url;
if(url == '' || !url || url == null || typeof url == 'undefined') {
res.status(422).send({
status: 'error',
error: "URL not set, can't add torrent."
});
return;
}
var protocol = url.split(':')[0];
if(protocol != 'http' && protocol != 'https' && protocol != 'magnet') {
res.status(422).send({
status: 'error',
error: "URL is not a valid path to a torrent file or a valid magnet link."
});
return;
}
console.log('Attemptin to stream %s', url);
if(protocol == 'magent') {
runTorrent(url, req, res);
} else {
console.log('Parsing remote torrent...');
parseTorrent.remote(url, function(err, torrent) {
runTorrent(parseTorrent.toMagnetURI(torrent), req, res);
});
}
});
api.get('/torrent/:id', function(req, res) {
var id = req.params.id;
if(typeof torrents[id] == 'undefined') {
res.status(422).send({
status: 'error',
error: "Provided ID is not valid or doesn't exist. "
});
return;
}
res.send(torrents[id]);
});
api.get('/ping/:id', function(req, res) {
var id = req.params.id;
console.log("Received ping for %s", id);
if(typeof torrents[id] == 'undefined') {
res.status(422).send({
status: 'error',
error: "Provided ID is not valid or doesn't exist. "
});
return;
}
torrents[id].atime = Math.floor(Date.now() / 1000);
res.send({
status: 'ok'
});
});
api.get('/kill', function(req, res) {
res.send({
status: 'ok'
});
process.exit(0);
});
var server = http.createServer(api);
server.listen(config.webui_port, server.webui_host);
server.on('listening', function() {
console.log('Listening on %s:%s', server.address().address, server.address().port);
});